@@ -3,7 +3,6 @@ package json
3
3
import (
4
4
"errors"
5
5
"io"
6
- "strings"
7
6
8
7
"gno.land/p/demo/ufmt"
9
8
)
@@ -112,28 +111,6 @@ func (b *buffer) skip(bs byte) error {
112
111
return io.EOF
113
112
}
114
113
115
- // skipAny moves the index until it encounters one of the given set of bytes.
116
- func (b *buffer) skipAny(endTokens map[byte]bool) error {
117
- for b.index < b.length {
118
- if _, exists := endTokens[b.data[b.index]]; exists {
119
- return nil
120
- }
121
-
122
- b.index++
123
- }
124
-
125
- // build error message
126
- var tokens []string
127
- for token := range endTokens {
128
- tokens = append(tokens, string(token))
129
- }
130
-
131
- return ufmt.Errorf(
132
- "EOF reached before encountering one of the expected tokens: %s",
133
- strings.Join(tokens, ", "),
134
- )
135
- }
136
-
137
114
// skipAndReturnIndex moves the buffer index forward by one and returns the new index.
138
115
func (b *buffer) skipAndReturnIndex() (int, error) {
139
116
err := b.step()
@@ -165,7 +142,7 @@ func (b *buffer) skipUntil(endTokens map[byte]bool) (int, error) {
165
142
166
143
// significantTokens is a map where the keys are the significant characters in a JSON path.
167
144
// The values in the map are all true, which allows us to use the map as a set for quick lookups.
168
- var significantTokens = map[byte ]bool{
145
+ var significantTokens = [256 ]bool{
169
146
dot: true, // access properties of an object
170
147
dollarSign: true, // root object
171
148
atSign: true, // current object
@@ -174,7 +151,7 @@ var significantTokens = map[byte]bool{
174
151
}
175
152
176
153
// filterTokens stores the filter expression tokens.
177
- var filterTokens = map[byte ]bool{
154
+ var filterTokens = [256 ]bool{
178
155
aesterisk: true, // wildcard
179
156
andSign: true,
180
157
orSign: true,
@@ -186,7 +163,7 @@ func (b *buffer) skipToNextSignificantToken() {
186
163
for b.index < b.length {
187
164
current := b.data[b.index]
188
165
189
- if _, ok := significantTokens[current]; ok {
166
+ if significantTokens[current] {
190
167
break
191
168
}
192
169
@@ -205,7 +182,7 @@ func (b *buffer) backslash() bool {
205
182
206
183
count := 0
207
184
for i := b.index - 1; ; i-- {
208
- if i >= b.length || b.data[i] != backSlash {
185
+ if b.data[i] != backSlash {
209
186
break
210
187
}
211
188
@@ -220,7 +197,7 @@ func (b *buffer) backslash() bool {
220
197
}
221
198
222
199
// numIndex holds a map of valid numeric characters
223
- var numIndex = map[byte ]bool{
200
+ var numIndex = [256 ]bool{
224
201
'0': true,
225
202
'1': true,
226
203
'2': true,
@@ -255,11 +232,11 @@ func (b *buffer) pathToken() error {
255
232
}
256
233
257
234
if err := b.skip(c); err != nil {
258
- return errors.New("unmatched quote in path")
235
+ return errUnmatchedQuotePath
259
236
}
260
237
261
238
if b.index >= b.length {
262
- return errors.New("unmatched quote in path")
239
+ return errUnmatchedQuotePath
263
240
}
264
241
265
242
case c == bracketOpen || c == parenOpen:
@@ -269,7 +246,7 @@ func (b *buffer) pathToken() error {
269
246
case c == bracketClose || c == parenClose:
270
247
inToken = true
271
248
if len(stack) == 0 || (c == bracketClose && stack[len(stack)-1] != bracketOpen) || (c == parenClose && stack[len(stack)-1] != parenOpen) {
272
- return errors.New("mismatched bracket or parenthesis")
249
+ return errUnmatchedParenthesis
273
250
}
274
251
275
252
stack = stack[:len(stack)-1]
@@ -284,7 +261,7 @@ func (b *buffer) pathToken() error {
284
261
inToken = true
285
262
inNumber = true
286
263
} else if !inToken {
287
- return errors.New("unexpected operator at start of token")
264
+ return errInvalidToken
288
265
}
289
266
290
267
default:
@@ -300,7 +277,7 @@ func (b *buffer) pathToken() error {
300
277
301
278
end:
302
279
if len(stack) != 0 {
303
- return errors.New("unclosed bracket or parenthesis at end of path")
280
+ return errUnmatchedParenthesis
304
281
}
305
282
306
283
if first == b.index {
@@ -315,15 +292,15 @@ end:
315
292
}
316
293
317
294
func pathStateContainsValidPathToken(c byte) bool {
318
- if _, ok := significantTokens[c]; ok {
295
+ if significantTokens[c] {
319
296
return true
320
297
}
321
298
322
- if _, ok := filterTokens[c]; ok {
299
+ if filterTokens[c] {
323
300
return true
324
301
}
325
302
326
- if _, ok := numIndex[c]; ok {
303
+ if numIndex[c] {
327
304
return true
328
305
}
329
306
@@ -342,7 +319,7 @@ func (b *buffer) numeric(token bool) error {
342
319
for ; b.index < b.length; b.index++ {
343
320
b.class = b.getClasses(doubleQuote)
344
321
if b.class == __ {
345
- return errors.New("invalid token found while parsing path")
322
+ return errInvalidToken
346
323
}
347
324
348
325
b.state = StateTransitionTable[b.last][b.class]
@@ -351,7 +328,7 @@ func (b *buffer) numeric(token bool) error {
351
328
break
352
329
}
353
330
354
- return errors.New("invalid token found while parsing path")
331
+ return errInvalidToken
355
332
}
356
333
357
334
if b.state < __ {
@@ -366,7 +343,7 @@ func (b *buffer) numeric(token bool) error {
366
343
}
367
344
368
345
if b.last != ZE && b.last != IN && b.last != FR && b.last != E3 {
369
- return errors.New("invalid token found while parsing path")
346
+ return errInvalidToken
370
347
}
371
348
372
349
return nil
@@ -407,12 +384,12 @@ func (b *buffer) string(search byte, token bool) error {
407
384
b.class = b.getClasses(search)
408
385
409
386
if b.class == __ {
410
- return errors.New("invalid token found while parsing path")
387
+ return errInvalidToken
411
388
}
412
389
413
390
b.state = StateTransitionTable[b.last][b.class]
414
391
if b.state == __ {
415
- return errors.New("invalid token found while parsing path")
392
+ return errInvalidToken
416
393
}
417
394
418
395
if b.state < __ {
@@ -431,11 +408,11 @@ func (b *buffer) word(bs []byte) error {
431
408
max := len(bs)
432
409
index := 0
433
410
434
- for ; b.index < b.length; b.index++ {
411
+ for ; b.index < b.length && index < max ; b.index++ {
435
412
c = b.data[b.index]
436
413
437
414
if c != bs[index] {
438
- return errors.New("invalid token found while parsing path")
415
+ return errInvalidToken
439
416
}
440
417
441
418
index++
@@ -445,7 +422,7 @@ func (b *buffer) word(bs []byte) error {
445
422
}
446
423
447
424
if index != max {
448
- return errors.New("invalid token found while parsing path")
425
+ return errInvalidToken
449
426
}
450
427
451
428
return nil
0 commit comments