-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathstrings.go
208 lines (195 loc) · 4.71 KB
/
strings.go
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package jsonslice
// readQuotedKey reads quoted key. Allocates memory if necessasry.
// Consumes right bound.
// Returns key []byte -- sliced or allocated (without quotes), i after last quote
func readQuotedKey(path []byte, i int) ([]byte, int, error) {
l := len(path)
var err error
var esc []byte
var result []byte
bound := path[i] // ' or "
i++
s := i
copying := false
for i < l && path[i] != bound {
if path[i] == '\\' {
// get escaped value
before := i
esc, i, err = readEscape(path, i)
if err != nil {
return nil, i, err
}
if !copying {
copying = true // start copying
result = append(result, path[s:before]...) // copy from the start
}
result = append(result, esc...)
s = i // we want to bulk append the rest
} else {
i++
}
}
if i == l { // no bound
return nil, i, errUnexpectedStringEnd
}
if copying {
result = append(result, path[s:i]...)
} else {
result = path[s:i]
}
return result, i + 1, nil
}
// readEscape reads escape sequence. path[i] must be '\' symbol.
// Supports \n, \r, \t, \0, \', \", \\, \x, \u.
// Returns UTF-8 rune as []byte, next character pointer or error.
func readEscape(path []byte, i int) ([]byte, int, error) {
l := len(path)
i++
if i == l {
return nil, i, errPathUnexpectedEnd
}
switch path[i] {
case 'n':
return []byte{'\x0A'}, i + 1, nil
case 'r':
return []byte{'\x0D'}, i + 1, nil
case 't':
return []byte{'\x09'}, i + 1, nil
case '0':
return []byte{'\x00'}, i + 1, nil
case '\'':
return []byte{'\''}, i + 1, nil
case '"':
return []byte{'"'}, i + 1, nil
case '\\':
return []byte{'\\'}, i + 1, nil
case 'x':
return readHexByte(path, i+1)
case 'u':
return readUnicodeSequence(4, path, i+1)
case 'U':
return readUnicodeSequence(8, path, i+1)
}
return nil, i, errPathUnknownEscape
}
func readHexByte(path []byte, i int) ([]byte, int, error) {
value := uint32(0)
var err error
l := len(path)
for num := 0; i < l && num < 2; i++ {
value, err = readNextHexNum(value, path[i])
if err != nil {
return nil, i, err
}
num++
}
return []byte{byte(value)}, i, nil
}
func readNextHexNum(value uint32, ch byte) (uint32, error) {
if ch >= '0' && ch <= '9' {
value = (value << 4) + uint32(ch-'0')
} else if ch >= 'A' && ch <= 'F' {
value = (value << 4) + uint32(ch-'A'+10)
} else if ch >= 'a' && ch <= 'f' {
value = (value << 4) + uint32(ch-'a'+10)
} else {
return value, errPathUnknownEscape
}
return value, nil
}
func readUnicodeSequence(length int, path []byte, i int) ([]byte, int, error) {
var err error
codepoint := uint32(0)
l := len(path)
num := 0
for ; i < l && num < length; i++ {
codepoint, err = readNextHexNum(codepoint, path[i])
if err != nil {
return nil, i, err
}
num++
}
if i == l && num < length {
return nil, i, errPathUnexpectedEnd
}
res, err := codepointToUTF8(codepoint)
return res, i, err
}
func codepointToUTF8(codepoint uint32) ([]byte, error) {
if codepoint <= 0x7F {
return []byte{byte(codepoint)}, nil
} else if codepoint <= 0x7FF {
return []byte{
0xC0 + byte((codepoint>>6)&0x3F),
0x80 + byte(codepoint&0x3F),
}, nil
} else if codepoint <= 0xFFFF {
return []byte{
0xE0 + byte((codepoint>>12)&0x3F),
0x80 + byte((codepoint>>6)&0x3F),
0x80 + byte(codepoint&0x3F),
}, nil
} else if codepoint <= 0x1FFFFF {
return []byte{
0xF0 + byte((codepoint>>18)&0x3F),
0x80 + byte((codepoint>>12)&0x3F),
0x80 + byte((codepoint>>6)&0x3F),
0x80 + byte(codepoint&0x3F),
}, nil
} else if codepoint <= 0x3FFFFFF {
return []byte{
0xF8 + byte((codepoint>>24)&0x3F),
0x80 + byte((codepoint>>18)&0x3F),
0x80 + byte((codepoint>>12)&0x3F),
0x80 + byte((codepoint>>6)&0x3F),
0x80 + byte(codepoint&0x3F),
}, nil
} else if codepoint <= 0x7FFFFFFF {
return []byte{
0xFC + byte((codepoint>>30)&0x3F),
0x80 + byte((codepoint>>24)&0x3F),
0x80 + byte((codepoint>>18)&0x3F),
0x80 + byte((codepoint>>12)&0x3F),
0x80 + byte((codepoint>>6)&0x3F),
0x80 + byte(codepoint&0x3F),
}, nil
} else {
return nil, errPathUnknownEscape
}
}
func toInt(buf []byte) int {
if len(buf) == 0 {
return cEmpty
}
n := 0
sign := 1
for _, ch := range buf {
if ch == '-' {
sign = -1
continue
}
if ch >= '0' && ch <= '9' {
n = n*10 + int(ch-'0')
} else {
return cNAN
}
}
return n * sign
}
func readTerminatorBounded(path []byte, i int, terminators []byte) ([]byte, int, error) {
l := len(path)
s := i
if path[i] == '-' { // CAUTION: allow '-' at the start (to support negative numbers)
i++
}
for i < l {
if bytein(path[i], terminators) {
if path[i] == '*' && s-i == 0 {
i++ // CAUTION: usually '*' is a terminator but not in '.*' so skip it
}
break
}
i++
}
return path[s:i], i, nil
}