-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecoder_structural.go
More file actions
784 lines (742 loc) · 23.3 KB
/
Copy pathdecoder_structural.go
File metadata and controls
784 lines (742 loc) · 23.3 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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
package vibejson
import (
"strings"
"sync"
"unsafe"
"github.com/thesyncim/vibejson/x/byteview"
simdkernels "github.com/thesyncim/vibejson/x/kernels"
)
const (
structuralFieldSlow uint8 = 0
structuralFieldMatched uint8 = 1
structuralFieldEnd uint8 = 2
structuralArrayValue uint8 = 1
structuralArrayEnd uint8 = 2
decoderStructuralTapeRetentionBytes = 2 << 20
decoderStructuralTapeRetentionPositions = decoderStructuralTapeRetentionBytes / 4
decoderStructuralMinBytes = 4096
decoderStructuralWindowShift = 11
decoderStructuralWindowCount = decoderStructuralTapeRetentionBytes >> decoderStructuralWindowShift
decoderStructuralWindowWords = decoderStructuralWindowCount / 32
)
// decoderStructuralTape is the typed decoder's On-Demand-style cursor. Stage
// 1 builds it once, including closing quotes, and the decoder only advances a
// monotonically increasing index. The backing slice is reused across calls.
type decoderStructuralTape struct {
positions []uint32
index int
bad bool
nonASCII bool
escaped bool
// Each word stores 32 escape bits in its low half and the corresponding
// 32 non-ASCII bits in its high half. The inline coverage matches the tape
// retention budget; larger inputs conservatively use exact local scans.
stringWindows [decoderStructuralWindowWords]uint64
}
var decoderStatePool sync.Pool
// structuralBytes is an unchecked, typed view over decoder input. Callers
// prove an index is in range before at and that eight bytes remain before
// uint64LEAt. The view keeps repeated unsafe conversions out of hot paths.
type structuralBytes struct {
base *byte
}
func structuralBytesOf(src []byte) structuralBytes {
return structuralBytes{base: unsafe.SliceData(src)}
}
func (src structuralBytes) at(index int) byte {
return *(*byte)(unsafe.Add(unsafe.Pointer(src.base), uintptr(index)))
}
func (src structuralBytes) uint64LEAt(index int) uint64 {
return loadUint64LE(unsafe.Add(unsafe.Pointer(src.base), index))
}
// structuralPositions is the corresponding unchecked view over the stage-1
// tape. Its typed base remains visible to the garbage collector, and callers
// establish bounds before indexing it.
type structuralPositions struct {
base *uint32
}
func structuralPositionsOf(positions []uint32) structuralPositions {
return structuralPositions{base: unsafe.SliceData(positions)}
}
func (positions structuralPositions) at(index int) uint32 {
return *(*uint32)(unsafe.Add(unsafe.Pointer(positions.base), uintptr(index)*4))
}
func takeDecoderState() *decoderState {
state, _ := decoderStatePool.Get().(*decoderState)
if state == nil {
state = new(decoderState)
}
return state
}
// decoderStructuralWorthwhile applies the document-side routing contract for
// the forward structural producer. The caller separately checks that its
// compiled shape has a structural executor.
func decoderStructuralWorthwhile(src []byte) bool {
return len(src) >= decoderStructuralMinBytes && uint64(len(src)) <= uint64(^uint32(0))
}
func acquireDecoderState(src []byte) *decoderState {
state := takeDecoderState()
state.structuralActive = true
state.structural.build(src)
return state
}
func releaseDecoderState(state *decoderState) {
state.resetOperationState()
state.structuralActive = false
state.structural.resetForPool()
decoderStatePool.Put(state)
}
// resetForPool keeps ordinary structural tapes reusable while preventing one
// exceptional document from setting the global pool's permanent high-water
// memory. uint32 has a fixed four-byte width, so the byte budget is exact.
func (t *decoderStructuralTape) resetForPool() {
t.index = 0
t.bad = false
t.nonASCII = false
t.escaped = false
clear(t.stringWindows[:])
if cap(t.positions) > decoderStructuralTapeRetentionPositions {
t.positions = nil
return
}
t.positions = t.positions[:0]
}
func (t *decoderStructuralTape) build(src []byte) {
t.index = 0
t.bad = false
t.positions = t.positions[:0]
clear(t.stringWindows[:])
if cap(t.positions) < len(src)/8 {
t.positions = make([]uint32, 0, len(src)/4)
}
n := len(src)
base := sliceBase(src)
fullBlocks := n / 64
var stream simdkernels.Stage1IndexStream
for chunk := 0; chunk < fullBlocks; chunk += simdkernels.Stage1ChunkBlocks {
count := fullBlocks - chunk
if count > simdkernels.Stage1ChunkBlocks {
count = simdkernels.Stage1ChunkBlocks
}
start := len(t.positions)
need := start + count*64 + 64
if cap(t.positions) < need {
capacity := cap(t.positions) * 2
if capacity < need {
capacity = need
}
grown := make([]uint32, start, capacity)
copy(grown, t.positions)
t.positions = grown
}
t.positions = t.positions[:need]
var meta simdkernels.Stage1CursorMeta
written := simdkernels.Stage1CursorBlocksMeta(
(*byte)(unsafe.Add(base, chunk*64)), count, uint32(chunk*64), &stream, t.positions[start:need], &meta,
)
t.positions = t.positions[:start+written]
t.markStringWindow(chunk*64, meta)
}
if fullBlocks*64 != n {
var tail [64]byte
for i := range tail {
tail[i] = ' '
}
copy(tail[:], src[fullBlocks*64:])
start := len(t.positions)
need := start + 128
if cap(t.positions) < need {
capacity := cap(t.positions) * 2
if capacity < need {
capacity = need
}
grown := make([]uint32, start, capacity)
copy(grown, t.positions)
t.positions = grown
}
t.positions = t.positions[:need]
var meta simdkernels.Stage1CursorMeta
written := simdkernels.Stage1CursorBlocksMeta(
&tail[0], 1, uint32(fullBlocks*64), &stream, t.positions[start:need], &meta,
)
t.positions = t.positions[:start+written]
t.markStringWindow(fullBlocks*64, meta)
}
t.bad = stream.Bad
t.nonASCII = stream.NonASCII
t.escaped = stream.Escaped
}
func (t *decoderStructuralTape) markStringWindow(base int, meta simdkernels.Stage1CursorMeta) {
window := base >> decoderStructuralWindowShift
if uint(window) >= decoderStructuralWindowCount {
return
}
bit := uint64(1) << uint(window&31)
if meta.Escaped {
t.stringWindows[window>>5] |= bit
}
if meta.NonASCII {
t.stringWindows[window>>5] |= bit << 32
}
}
func (t *decoderStructuralTape) stringRangeDirty(start, end int, nonASCII bool) bool {
if start >= end {
return false
}
first := start >> decoderStructuralWindowShift
last := (end - 1) >> decoderStructuralWindowShift
if uint(first) >= decoderStructuralWindowCount || uint(last) >= decoderStructuralWindowCount {
return true
}
shift := uint(0)
if nonASCII {
shift = 32
}
for window := first; window <= last; window++ {
if t.stringWindows[window>>5]&(uint64(1)<<uint(window&31)<<shift) != 0 {
return true
}
}
return false
}
// position returns the first tape position at or after target. Callers only
// enter on JSON whitespace. Any non-whitespace byte in the gap is itself a
// structural or scalar-start token, while scalar continuations never enter.
func (t *decoderStructuralTape) position(target, end int) int {
i := t.index
positions := t.positions
for i < len(positions) && int(positions[i]) < target {
i++
}
t.index = i
if i == len(positions) {
return end
}
return int(positions[i])
}
func (t *decoderStructuralTape) stringEnd(src []byte, start int) (end int, ok bool) {
i := t.index
for i < len(t.positions) && int(t.positions[i]) < start {
i++
}
if i+1 >= len(t.positions) || int(t.positions[i]) != start || src[start] != '"' {
return 0, false
}
entry := t.positions[i+1]
end = int(entry)
if end >= len(src) || src[end] != '"' {
return 0, false
}
t.index = i + 1
return end, true
}
func (t *decoderStructuralTape) seekFrom(index, target, end int) (next, position int, ok bool) {
for index < len(t.positions) && int(t.positions[index]) < target {
index++
}
if index == len(t.positions) {
return index, end, false
}
return index, int(t.positions[index]), true
}
// structuralColonGap validates the punctuation omitted from the compact tape.
// Stage 1 has already rejected non-JSON control bytes, so bytes at or below a
// space outside strings are legal JSON whitespace here.
func structuralColonGap(src structuralBytes, n, closePosition, valuePosition int) bool {
i := closePosition + 1
for i < valuePosition && uint(i) < uint(n) && src.at(i) <= ' ' {
i++
}
if i >= valuePosition || uint(i) >= uint(n) || src.at(i) != ':' {
return false
}
i++
for i < valuePosition && uint(i) < uint(n) && src.at(i) <= ' ' {
i++
}
return i == valuePosition
}
// structuralPackedColonTail is used after the packed key word has already
// proved the quote and colon. The whole-shape path accepts only the two
// dominant layouts; uncommon whitespace misses transactionally and is
// validated by the generic cursor.
func structuralPackedColonTail(src structuralBytes, closePosition, valuePosition int) bool {
gap := valuePosition - closePosition
return gap == 2 || gap == 3 && src.at(closePosition+2) <= ' '
}
// structuralFirstValueGapOK verifies the one gap where the colon-elided stream
// has no preceding value to protect it. The next tape entry normally follows
// the opener directly or after whitespace; a colon is the only structural byte
// Stage 1 can hide there. Structural container decoders establish this once so
// all of their specialized executors share the same grammar invariant.
func (c *decoderCursor) structuralFirstValueGapOK() bool {
tape := &c.state.structural
index := tape.index + 1
if uint(index) >= uint(len(tape.positions)) {
return true
}
src := structuralBytesOf(c.src)
end := int(tape.positions[index])
for start := c.i; start < end; start++ {
if src.at(start) == ':' {
return false
}
}
return true
}
// syncStructuralValue restores the forward-cursor invariant after a value was
// consumed by a generic decoder. Exact structural decoders already leave the
// tape on their final token; generic maps, hooks, and skipped subtrees may not.
func (c *decoderCursor) syncStructuralValue() {
tape := &c.state.structural
positions := tape.positions
index := tape.index
end := c.i
for index+1 < len(positions) && int(positions[index+1]) < end {
index++
}
tape.index = index
}
// matchObjectFieldStructural is the expected-order ASCII path. The structural
// decoder maintains tape.index on the final token of the preceding value, so
// a member is a fixed sequence of increments with no byte/tape rescan.
func (c *decoderCursor) matchObjectFieldStructural(first bool, expected *typedField) uint8 {
if expected == nil || expected.keyMask == 0 {
return structuralFieldSlow
}
tape := &c.state.structural
positions := tape.positions
index := tape.index + 1
if uint(index) >= uint(len(positions)) {
return structuralFieldSlow
}
src := c.src
n := len(src)
bytes := structuralBytesOf(src)
if !first && uint(c.i) < uint(n) {
tail := bytes.at(c.i)
if tail > ' ' && tail != ',' && tail != '}' {
return structuralFieldSlow
}
}
entries := structuralPositionsOf(positions)
position := int(entries.at(index))
if !first {
switch bytes.at(position) {
case '}':
tape.index = index
c.i = position + 1
c.depth--
return structuralFieldEnd
case ',':
index++
default:
return structuralFieldSlow
}
} else if bytes.at(position) == '}' {
tape.index = index
c.i = position + 1
c.depth--
return structuralFieldEnd
}
valueIndex := index + 2
if uint(valueIndex) >= uint(len(positions)) {
return structuralFieldSlow
}
openPosition := int(entries.at(index))
closeEntry := entries.at(index + 1)
closePosition := int(closeEntry)
valuePosition := int(entries.at(valueIndex))
if bytes.at(openPosition) != '"' || bytes.at(closePosition) != '"' ||
!structuralColonGap(bytes, n, closePosition, valuePosition) {
return structuralFieldSlow
}
keyStart := openPosition + 1
keyLen := closePosition - keyStart
if keyLen != int(expected.keyLen) || keyStart+8 > n {
return structuralFieldSlow
}
mask := expected.keyMask
if expected.keyLen <= 6 {
mask >>= 8
}
diff := (bytes.uint64LEAt(keyStart) ^ expected.key) & mask
if diff != 0 || keyLen > 8 && !matchStringAt(src, keyStart+8, expected.name[8:]) {
return structuralFieldSlow
}
tape.index = valueIndex
c.i = valuePosition
return structuralFieldMatched
}
func (c *decoderCursor) matchFirstObjectFieldStructuralExpected(expected *typedField) uint8 {
tape := &c.state.structural
index := tape.index + 1
positions := tape.positions
valueIndex := index + 2
if uint(valueIndex) >= uint(len(positions)) {
return structuralFieldSlow
}
entries := structuralPositionsOf(positions)
openPosition := int(entries.at(index))
src := c.src
bytes := structuralBytesOf(src)
if bytes.at(openPosition) == '}' {
tape.index = index
c.i = openPosition + 1
c.depth--
return structuralFieldEnd
}
closeEntry := entries.at(index + 1)
closePosition := int(closeEntry)
valuePosition := int(entries.at(valueIndex))
keyStart := openPosition + 1
if keyStart+8 > len(src) || closePosition-keyStart != int(expected.keyLen) ||
bytes.at(openPosition) != '"' || bytes.at(closePosition) != '"' ||
!structuralColonGap(bytes, len(src), closePosition, valuePosition) {
return structuralFieldSlow
}
mask := expected.keyMask
if expected.keyLen <= 6 {
mask >>= 8
}
if (bytes.uint64LEAt(keyStart)^expected.key)&mask != 0 {
return structuralFieldSlow
}
tape.index = valueIndex
c.i = valuePosition
return structuralFieldMatched
}
func (c *decoderCursor) matchNextObjectFieldStructuralExpected(expected *typedField) uint8 {
tape := &c.state.structural
index := tape.index + 1
positions := tape.positions
if uint(index) >= uint(len(positions)) {
return structuralFieldSlow
}
src := c.src
bytes := structuralBytesOf(src)
if uint(c.i) < uint(len(src)) {
tail := bytes.at(c.i)
if tail > ' ' && tail != ',' && tail != '}' {
return structuralFieldSlow
}
}
entries := structuralPositionsOf(positions)
delimiterPosition := int(entries.at(index))
switch bytes.at(delimiterPosition) {
case '}':
tape.index = index
c.i = delimiterPosition + 1
c.depth--
return structuralFieldEnd
case ',':
index++
default:
return structuralFieldSlow
}
valueIndex := index + 2
if uint(valueIndex) >= uint(len(positions)) {
return structuralFieldSlow
}
openPosition := int(entries.at(index))
closeEntry := entries.at(index + 1)
closePosition := int(closeEntry)
valuePosition := int(entries.at(valueIndex))
keyStart := openPosition + 1
if keyStart+8 > len(src) || closePosition-keyStart != int(expected.keyLen) ||
bytes.at(openPosition) != '"' || bytes.at(closePosition) != '"' ||
!structuralColonGap(bytes, len(src), closePosition, valuePosition) {
return structuralFieldSlow
}
mask := expected.keyMask
if expected.keyLen <= 6 {
mask >>= 8
}
if (bytes.uint64LEAt(keyStart)^expected.key)&mask != 0 {
return structuralFieldSlow
}
tape.index = valueIndex
c.i = valuePosition
return structuralFieldMatched
}
// matchFirstObjectFieldStructuralShape is the positive-only first transition
// for a compiled whole-object superinstruction. Packed plans up to six bytes
// compare name, quote, and colon in one load; seven-byte names add one colon
// check. Empty objects and uncommon formatting fail transactionally.
func (c *decoderCursor) matchFirstObjectFieldStructuralShape(expected *typedField) bool {
tape := &c.state.structural
positions := tape.positions
index := tape.index + 1
valueIndex := index + 2
if uint(valueIndex) >= uint(len(positions)) {
return false
}
src := c.src
n := len(src)
bytes := structuralBytesOf(src)
entries := structuralPositionsOf(positions)
openPosition := int(entries.at(index))
valuePosition := int(entries.at(valueIndex))
keyStart := openPosition + 1
if keyStart+8 > n || bytes.at(openPosition) != '"' ||
(bytes.uint64LEAt(keyStart)^expected.key)&expected.keyMask != 0 ||
expected.keyLen == 7 && (keyStart+8 >= n || bytes.at(keyStart+8) != ':') ||
!structuralPackedColonTail(bytes, keyStart+int(expected.keyLen), valuePosition) {
return false
}
tape.index = valueIndex
c.i = valuePosition
return true
}
// matchNextObjectFieldStructuralShape is the positive-only next transition
// used by compiled whole-object superinstructions. The shape requires another
// field in compile order, so early object ends and uncommon key forms fall
// into the general decoder without paying for their branches on the hot path.
func (c *decoderCursor) matchNextObjectFieldStructuralShape(expected *typedField) bool {
tape := &c.state.structural
positions := tape.positions
index := tape.index + 1
valueIndex := index + 3
if uint(valueIndex) >= uint(len(positions)) {
return false
}
src := c.src
n := len(src)
bytes := structuralBytesOf(src)
if uint(c.i) >= uint(n) || bytes.at(c.i) != ',' {
return false
}
entries := structuralPositionsOf(positions)
if entries.at(index) != uint32(c.i) {
return false
}
openPosition := int(entries.at(index + 1))
valuePosition := int(entries.at(valueIndex))
keyStart := openPosition + 1
if keyStart+8 > n || bytes.at(openPosition) != '"' {
return false
}
if (bytes.uint64LEAt(keyStart)^expected.key)&expected.keyMask != 0 ||
expected.keyLen == 7 && (keyStart+8 >= n || bytes.at(keyStart+8) != ':') ||
!structuralPackedColonTail(bytes, keyStart+int(expected.keyLen), valuePosition) {
return false
}
tape.index = valueIndex
c.i = valuePosition
return true
}
func (c *decoderCursor) finishObjectStructural(first bool) bool {
tape := &c.state.structural
index := tape.index + 1
if uint(index) >= uint(len(tape.positions)) {
return false
}
src := c.src
bytes := structuralBytesOf(src)
if !first && uint(c.i) < uint(len(src)) {
tail := bytes.at(c.i)
if tail > ' ' && tail != '}' {
return false
}
}
position := int(tape.positions[index])
if bytes.at(position) != '}' {
return false
}
tape.index = index
c.i = position + 1
c.depth--
return true
}
// nextObjectFieldStructural fuses the On-Demand object sequence into one
// forward tape operation: delimiter, key open/close, and value start. The
// omitted colon is validated from the raw gap.
// Escaped keys fall back transactionally without moving either cursor.
func (c *decoderCursor) nextObjectFieldStructural(first bool, expected *typedField) (key string, matched, ok, handled bool, err error) {
if c.state == nil || !c.state.structuralActive || c.state.structural.bad {
return "", false, false, false, nil
}
tape := &c.state.structural
if tape.escaped {
return "", false, false, false, nil
}
src := c.src
n := len(src)
bytes := structuralBytesOf(src)
if !first && uint(c.i) < uint(n) {
tail := src[c.i]
if tail > ' ' && tail != ',' && tail != '}' {
return "", false, false, true, c.err(c.i, "expected comma or closing brace in object")
}
}
index, position, exists := tape.seekFrom(tape.index, c.i, n)
if !exists {
return "", false, false, true, c.err(position, "unterminated object")
}
if first {
if src[position] == '}' {
tape.index = index
c.i = position + 1
c.depth--
return "", false, false, true, nil
}
} else {
switch src[position] {
case '}':
tape.index = index
c.i = position + 1
c.depth--
return "", false, false, true, nil
case ',':
index++
if index >= len(tape.positions) {
return "", false, false, true, c.err(position, "unterminated object")
}
position = int(tape.positions[index])
default:
return "", false, false, true, c.err(position, "expected comma or closing brace in object")
}
}
if src[position] != '"' {
return "", false, false, true, c.err(position, "expected object key string")
}
if index+1 >= len(tape.positions) {
return "", false, false, true, c.err(position, "unterminated string")
}
closeEntry := tape.positions[index+1]
closePosition := int(closeEntry)
if closePosition >= n || src[closePosition] != '"' {
return "", false, false, true, c.err(position, "unterminated string")
}
keyStart := position + 1
if tape.nonASCII && !validUTF8Fast(src[keyStart:closePosition]) {
return "", false, false, true, c.err(keyStart, "invalid UTF-8 in string")
}
valueIndex := index + 2
valuePosition := n
if valueIndex < len(tape.positions) {
valuePosition = int(tape.positions[valueIndex])
}
if !structuralColonGap(bytes, n, closePosition, valuePosition) {
return "", false, false, true, c.err(closePosition+1, "expected colon after object key")
}
keyLen := closePosition - keyStart
if expected != nil {
if keyLen == int(expected.keyLen) && expected.keyMask != 0 && keyStart+8 <= n {
mask := ^uint64(0)
if keyLen < 7 {
mask >>= uint(7-keyLen) * 8
}
diff := (bytes.uint64LEAt(keyStart) ^ expected.key) & mask
matched = diff == 0 && (keyLen <= 8 || matchStringAt(src, keyStart+8, expected.name[8:]))
}
if !matched && c.flags&decoderCaseSensitive == 0 {
actual := byteview.String(src[keyStart:closePosition])
matched = strings.EqualFold(actual, expected.name)
}
}
if !matched {
key = byteview.String(src[keyStart:closePosition])
}
tape.index = valueIndex
c.i = valuePosition
return key, matched, true, true, nil
}
func (c *decoderCursor) nextArrayElementStructural(first bool) (more, handled bool, err error) {
if c.state == nil || !c.state.structuralActive || c.state.structural.bad {
return false, false, nil
}
tape := &c.state.structural
src := c.src
n := len(src)
if !first && uint(c.i) < uint(n) {
tail := src[c.i]
if tail > ' ' && tail != ',' && tail != ']' {
return false, true, c.err(c.i, "expected comma or closing bracket in array")
}
}
index, position, exists := tape.seekFrom(tape.index, c.i, n)
if !exists {
return false, true, c.err(position, "unterminated array")
}
if src[position] == ']' {
tape.index = index
c.i = position + 1
c.depth--
return false, true, nil
}
if !first {
if src[position] != ',' {
return false, true, c.err(position, "expected comma or closing bracket in array")
}
index++
if index >= len(tape.positions) {
return false, true, c.err(position, "unterminated array")
}
position = int(tape.positions[index])
}
tape.index = index
c.i = position
return true, true, nil
}
// nextArrayElementExact advances from the final token of the preceding value.
// It moves neither cursor on a miss, leaving the seek-based parser to retain
// full malformed-input diagnostics for uncommon operations.
func (c *decoderCursor) nextArrayElementExact(first bool) uint8 {
tape := &c.state.structural
positions := tape.positions
index := tape.index + 1
if uint(index) >= uint(len(positions)) {
return structuralFieldSlow
}
src := c.src
n := len(src)
bytes := structuralBytesOf(src)
if !first && uint(c.i) < uint(n) {
tail := bytes.at(c.i)
if tail > ' ' && tail != ',' && tail != ']' {
return structuralFieldSlow
}
}
position := int(positions[index])
if uint(position) >= uint(n) {
return structuralFieldSlow
}
if bytes.at(position) == ']' {
tape.index = index
c.i = position + 1
c.depth--
return structuralArrayEnd
}
if !first {
if bytes.at(position) != ',' {
return structuralFieldSlow
}
index++
if uint(index) >= uint(len(positions)) {
return structuralFieldSlow
}
position = int(positions[index])
if uint(position) >= uint(n) || bytes.at(position) == ']' {
return structuralFieldSlow
}
}
tape.index = index
c.i = position
return structuralArrayValue
}
func (c *decoderCursor) skipSpaceAt(i int) int {
if c.state == nil || !c.state.structuralActive {
return skipSpaceIndent(c.src, i)
}
if uint(i) >= uint(len(c.src)) || c.src[i] > ' ' {
return i
}
return c.state.structural.position(i, len(c.src))
}
func (c *decoderCursor) structuralStringEnd(start int) (end int, ok bool) {
if c.state == nil || !c.state.structuralActive || c.state.structural.bad {
return 0, false
}
return c.state.structural.stringEnd(c.src, start)
}