-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathattribute-types.go
More file actions
193 lines (172 loc) · 5.11 KB
/
attribute-types.go
File metadata and controls
193 lines (172 loc) · 5.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package HLS
import (
"errors"
"fmt"
"strconv"
"strings"
)
// decimal-integer: an unquoted string of characters from the set
// [0..9] expressing an integer in base-10 arithmetic in the range
// from 0 to 2^64-1 (18446744073709551615). A decimal-integer may be
// from 1 to 20 characters long.
func IsDecimalInteger(value string) bool {
if len(value) < 1 || len(value) > 20 {
return false
}
for _, char := range value {
if char < '0' || char > '9' {
return false
}
}
return true
}
// hexadecimal-sequence: an unquoted string of characters from the
// set [0..9] and [A..F]. The maximum
// length of a hexadecimal-sequence depends on its AttributeNames.
func IsHexadecimalSequence(value string) bool {
if value == "" {
return false
}
for _, char := range value {
if !(char >= '0' && char <= '9') && !(char >= 'A' && char <= 'Z') {
return false
}
}
return true
}
// decimal-floating-point: an unquoted string of characters from the
// set [0..9] and '.' that expresses a non-negative floating-point
// number in decimal positional notation.
func IsDecimalFloatingPoint(value string) bool {
if value == "" {
return false
} else if value[0] == '.' || value[len(value)-1] == '.' {
return false
}
var dot bool
for _, char := range value {
if (char < '0' || char > '9') && char != '.' || dot && char == '.' {
return false
}
if char == '.' {
dot = true
}
}
return true
}
// signed-decimal-floating-point: an unquoted string of characters
// from the set [0..9], '-', and '.' that expresses a signed
// floating-point number in decimal positional notation.
func IsSignedDecimalFloatingPoint(value string) bool {
value = strings.TrimLeft(value, "-")
if value == "" {
return false
}
return IsDecimalFloatingPoint(value)
}
// The following characters MUST NOT appear in a
// string: line feed (0xA), carriage return (0xD), or double
// quote (0x22).
func IsString(value string) bool {
//\n, \r, "
for _, char := range value {
if char == '\n' || char == '\r' || char == '"' {
return false
}
}
return true
}
// quoted-string: a string of characters within a pair of double
// quotes (0x22). The following characters MUST NOT appear in a
// quoted-string: line feed (0xA), carriage return (0xD), or double
// quote (0x22). Quoted-string AttributeValues SHOULD be constructed
// so that byte-wise comparison is sufficient to test two quoted-
// string AttributeValues for equality. Note that this implies case-
// sensitive comparison.
func IsQuotedString(value string) bool {
if len(value) < 2 {
return false
} else if value[0] != '"' || value[len(value)-1] != '"' || len(value)-1 == 0 {
return false
}
//Make sure end and start of the value is quote and not end != start
return IsString(strings.Trim(value, `"`))
}
// WrapQuotes add double quotes around the value and return it.
func WrapQuotes(value string) string {
return `"` + value + `"`
}
// enumerated-string: an unquoted character string from a set that is
// explicitly defined by the AttributeName. An enumerated-string
// will never contain double quotes ("), commas (,), or whitespace.
func IsEnumeratedString(value string) bool {
if value == "" {
return false
}
for _, char := range value {
if char == ',' || char == ' ' || char == '"' {
return false
}
}
return true
}
// decimal-resolution: two decimal-integers separated by the "x"
// character. The first integer is a horizontal pixel dimension
// (width); the second is a vertical pixel dimension (height).
func IsDecimalResolution(value string) bool {
value = strings.TrimSpace(value)
if value == "" {
return false
}
values := strings.Split(value, "x")
if len(values) != 2 {
return false
}
return IsDecimalInteger(values[0]) && IsDecimalInteger(values[1])
}
// Resolution is used for representing a resolution.
type Resolution struct {
Width, Height int
}
// ToDecimalResolution returns the Width and Height of the resolution in order separated by 'x'.
func (resolution *Resolution) ToDecimalResolution() string {
return fmt.Sprintf(`%vx%v`, resolution.Width, resolution.Height)
}
var (
InvalidDecimalResolution error = errors.New("Invalid decimal resolution")
)
// decimalResolution: two decimal-integers separated by the "x"
// character. The first integer is a horizontal pixel dimension
// (width); the second is a vertical pixel dimension (height).
//
// ParseResolution parses decimalResolution and returns Resolution
func ParseResolution(decimalResolution string) (Resolution, error) {
//1024x720
resolution := Resolution{}
if !IsDecimalResolution(decimalResolution) {
return resolution, InvalidDecimalResolution
}
var i int
for i < len(decimalResolution) && decimalResolution[i] != 'x' {
i++
}
if i+1 >= len(decimalResolution) || i == 0 {
return resolution, InvalidDecimalResolution
}
width, err := strconv.Atoi(decimalResolution[:i])
if err != nil {
return resolution, InvalidDecimalResolution
}
height, err := strconv.Atoi(decimalResolution[i+1:])
if err != nil {
return resolution, InvalidDecimalResolution
}
resolution.Width = width
resolution.Height = height
return resolution, nil
}
/*
func RemoveQuotes(quotedString string) string{
return ""
}
*/