-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsegment.go
More file actions
335 lines (311 loc) · 9.45 KB
/
segment.go
File metadata and controls
335 lines (311 loc) · 9.45 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
package protopath
import (
"strings"
"unicode"
"unicode/utf8"
)
// EncodingOption option that configures segments encoding.
type EncodingOption interface {
configure(encodingPreference) encodingPreference
}
type encodingOptionFunc func(encodingPreference) encodingPreference
func (fn encodingOptionFunc) configure(pref encodingPreference) encodingPreference { return fn(pref) }
// PreferSquareBracketsWrapping returns an encoding option that triggers usage of square brackets wrapping while encoding when path segment value contains dot character. By default, without this option, characters escaping is always performed.
//
// This option is conflicting with ForceSquareBracketsWrapping. When it appears after this option will override it.
func PreferSquareBracketsWrapping() EncodingOption {
return encodingOptionFunc(func(pref encodingPreference) encodingPreference {
return pref & ^forceWrapping | preferWrapping
})
}
// ForceSquareBracketsWrapping returns an encoding option forces usage of square brackets wrapping even if no wrapping or escaping is required.
//
// This option is conflicting with PreferSquareBracketsWrapping. When it appears after this option will override it.
func ForceSquareBracketsWrapping() EncodingOption {
return encodingOptionFunc(func(pref encodingPreference) encodingPreference {
return pref | forceWrapping
})
}
// Segment is a single fragment of the path.
type Segment struct {
encoded string
isValid bool // invalid segments have no decoded value
decoded string
}
// NewSegment creates a new segment, by encoding the provided value. Newly created segments belong to a path made of a single segment - the newly created one. Returned segments are always valid - if the provided value contains a rune that is not properly UTF-8 encoded, is out of range, or is not the shortest possible UTF-8 encoding for that rune, the rune will be replaced by the Unicode replacement character. Otherwise, the segment decoded value will match the given value.
func NewSegment(value string, opts ...EncodingOption) Segment {
pref := encodingPreference(0)
for _, o := range opts {
pref = o.configure(pref)
}
return encodeSegmentWithPreferences(value, pref)
}
// IsValid informs if the segment is valid. Invalid segments have improperly encoded segment value, that cannot be decoded.
func (s Segment) IsValid() bool {
return s.isValid
}
// Encoded returns encoded (marshaled) value of the segment (as is appears in proto path).
func (s Segment) Encoded() string {
return s.encoded
}
// Value returns decoded (unmarshaled) value of the segment (usable value).
func (s Segment) Value() string {
return s.decoded
}
func (s Segment) Equal(o Segment) bool {
if s.isValid != o.isValid {
return false
}
if !s.isValid {
return s.encoded == o.encoded
}
return s.decoded == o.decoded
}
func (s Segment) Compare(o Segment) int {
if !s.isValid {
if o.isValid {
return -1
}
return strings.Compare(s.encoded, o.encoded)
}
if !o.isValid {
return 1
}
return strings.Compare(s.decoded, o.decoded)
}
func decodeFirstPathSegment(path string) Segment {
copyFrom, wrapped := 0, false
if next, _ := nextByte(path); next == '[' {
copyFrom, wrapped = 1, true
}
decoded := []byte{}
for i := copyFrom; i < len(path); {
if path[i] >= utf8.RuneSelf {
r, size := utf8.DecodeRuneInString(string(path[i:]))
if r == utf8.RuneError && size == 1 {
return Segment{encoded: path, isValid: false} // invalid UTF-8 string
}
i += size
continue
}
if isUnescapedAsciiForIdentifier(path[i]) {
i++
continue
}
if wrapped && path[i] == '.' {
i++
continue
}
decoded = append(decoded, path[copyFrom:i]...)
switch {
case path[i] == '.': // && !wrapped
return Segment{encoded: path[:i], decoded: string(decoded), isValid: true} // valid identifier segment value
case !wrapped && path[i] == '[':
return Segment{encoded: path[:i], decoded: string(decoded), isValid: true} // valid identifier segment value
case wrapped && path[i] == ']':
if next, hasNext := nextByte(path[i+1:]); hasNext && next != '[' && next != '.' {
return Segment{encoded: path, isValid: false} // invalid segment value - invalid data after wrapped segment
}
return Segment{encoded: path[:i+1], decoded: string(decoded), isValid: true} // valid wrapped segment value
case path[i] == '\\':
r, escapeSize, ok := parseEscaped(path[i+1:])
if !ok {
return Segment{encoded: path, isValid: false} // invalid segment value - invalid escaped sequence
}
decoded = append(decoded, string(r)...)
i += escapeSize + 1
default:
return Segment{encoded: path, isValid: false} // invalid segment value - invalid ascii value
}
copyFrom = i
}
if wrapped {
return Segment{encoded: path, isValid: false} // invalid wrapped segment value - missing closing bracket
}
decoded = append(decoded, path[copyFrom:]...)
return Segment{encoded: path, decoded: string(decoded), isValid: true} // valid identifier segment value - reaches the path end
}
func isUnescapedAsciiForIdentifier(x byte) bool {
return (x >= 0x20 && x <= 0x21) || // skip 0x22 " double quote
(x >= 0x23 && x <= 0x26) || // skip 0x27 ' single quote
(x >= 0x28 && x <= 0x2D) || // skip 0x2E . dot
(x >= 0x2F && x <= 0x5A) || // skip 0x5B [ opening square bracket; skip 0x5C \ backslash; skip 0x5D ] closing square bracket
(x >= 0x5E && x <= 0x7F)
}
func parseEscaped(value string) (rune, int, bool) {
if len(value) == 0 {
return 0, 0, false
}
switch value[0] {
case '"':
return rune('"'), 1, true
case '\'':
return rune('\''), 1, true
case '.':
return rune('.'), 1, true
case '[':
return rune('['), 1, true
case ']':
return rune(']'), 1, true
case '\\':
return rune('\\'), 1, true
case 'b':
return rune('\b'), 1, true
case 'f':
return rune('\f'), 1, true
case 'n':
return rune('\n'), 1, true
case 'r':
return rune('\r'), 1, true
case 't':
return rune('\t'), 1, true
case 'u':
r := parseHex4(value[1:])
return r, 4 + 1, r >= 0
case 'U':
r := parseHex8(value[1:])
return r, 8 + 1, r >= 0
}
return 0, 0, false // invalid escape sequence
}
func parseHex4(value string) rune {
if len(value) < 4 {
return -1
}
r := rune(0)
for range 4 {
b := value[0]
switch {
case b >= '0' && b <= '9':
r = r<<4 | rune(b-'0')
case b >= 'a' && b <= 'f':
r = r<<4 | rune(b-'a'+10)
case b >= 'A' && b <= 'F':
r = r<<4 | rune(b-'A'+10)
default:
return -1
}
value = value[1:]
}
if !utf8.ValidRune(r) {
r = unicode.ReplacementChar
}
return r
}
func parseHex8(value string) rune {
if len(value) < 8 {
return -1
}
r := rune(0)
for range 8 {
b := value[0]
switch {
case b >= '0' && b <= '9':
r = r<<4 | rune(b-'0')
case b >= 'a' && b <= 'f':
r = r<<4 | rune(b-'a'+10)
case b >= 'A' && b <= 'F':
r = r<<4 | rune(b-'A'+10)
default:
return -1
}
value = value[1:]
}
if !utf8.ValidRune(r) {
r = unicode.ReplacementChar
}
return r
}
func nextByte(value string) (byte, bool) {
if len(value) == 0 {
return 0, false
}
return value[0], true
}
type encodingPreference uint32
const (
preferWrapping encodingPreference = iota + 1
forceWrapping
)
func encodeSegmentWithPreferences(value string, pref encodingPreference) Segment {
switch pref {
case preferWrapping:
return encodeSegment(value, strings.ContainsRune(value, '.'))
case forceWrapping:
return encodeSegment(value, true)
default: // no preferences
return encodeSegment(value, false)
}
}
const hexToAscii = "0123456789abcdef"
func encodeSegment(value string, wrap bool) Segment {
var encoded []byte
var decoded []byte
if wrap {
encoded = append(encoded, '[')
}
copyFrom := 0
for i := 0; i < len(value); {
if value[i] >= utf8.RuneSelf {
r, size := utf8.DecodeRuneInString(string(value[i:]))
if r == utf8.RuneError && size == 1 {
encoded = append(encoded, value[copyFrom:i]...)
encoded = append(encoded, `\ufffd`...)
decoded = append(decoded, value[copyFrom:i]...)
decoded = append(decoded, "\ufffd"...)
i += size
copyFrom = i
continue
}
// Do not treat runes U+2028 (line separator) and U+2029 (paragraph separator) in any special way. See https://en.wikipedia.org/wiki/JSON#Safety for more details.
// It is expected that proto path will be embedded in a JSON string anyway so "encoding/json" package can handle that.
i += size
continue
}
if isUnescapedAsciiForIdentifier(value[i]) {
i++
continue
}
encoded = append(encoded, value[copyFrom:i]...)
decoded = append(decoded, value[copyFrom:i]...)
switch value[i] {
case '"':
encoded = append(encoded, '\\', '"')
case '\'':
encoded = append(encoded, '\\', '\'')
case '.':
if wrap {
encoded = append(encoded, '.')
} else {
encoded = append(encoded, '\\', '.')
}
case '[':
encoded = append(encoded, '\\', '[')
case ']':
encoded = append(encoded, '\\', ']')
case '\\':
encoded = append(encoded, '\\', '\\')
case '\b':
encoded = append(encoded, '\\', 'b')
case '\f':
encoded = append(encoded, '\\', 'f')
case '\n':
encoded = append(encoded, '\\', 'n')
case '\r':
encoded = append(encoded, '\\', 'r')
case '\t':
encoded = append(encoded, '\\', 't')
default:
encoded = append(encoded, '\\', 'u', '0', '0', hexToAscii[value[i]>>4], hexToAscii[value[i]&0x0F])
}
decoded = append(decoded, value[i])
i++
copyFrom = i
}
encoded = append(encoded, value[copyFrom:]...)
decoded = append(decoded, value[copyFrom:]...)
if wrap {
encoded = append(encoded, ']')
}
return Segment{encoded: string(encoded), decoded: string(decoded), isValid: true}
}