-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecoder_cursor.go
More file actions
862 lines (808 loc) · 22.9 KB
/
Copy pathdecoder_cursor.go
File metadata and controls
862 lines (808 loc) · 22.9 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
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
package vibejson
//go:generate go run ./internal/cmd/codegen decoder-cursor
import (
"encoding/binary"
"math/bits"
"strconv"
"unsafe"
"github.com/thesyncim/vibejson/x/byteview"
)
// signedInteger is the set of integer types accepted by decoderCursor.Int.
type signedInteger interface {
~int | ~int8 | ~int16 | ~int32 | ~int64
}
// unsignedInteger is the set of integer types accepted by decoderCursor.Uint.
type unsignedInteger interface {
~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr
}
// floatValue is the set of floating-point types accepted by decoderCursor.Float.
type floatValue interface {
~float32 | ~float64
}
// decoderFlags carries the per-decode switches. All but one mirror
// DecoderOptions; decoderExpectedSlow latches after the first semantic-order
// miss of the packed-key matcher. Formatting whitespace is handled by the
// packed path and does not trip the latch.
type decoderFlags uint8
const (
decoderZeroCopy decoderFlags = 1 << iota
decoderDisallowUnknown
decoderCaseSensitive
decoderReplace
decoderExpectedSlow
decoderUseNumber
decoderReplaceWideDestination
)
// decoderCursor is the concrete, interface-free parser used by compiled typed
// decoders. Its generic scalar methods are specialized by destination width.
type decoderCursor struct {
src []byte
i int
maxDepth int32
depth int32
flags decoderFlags
// floatLong is the sticky element-shape hint for fused float array
// loops: while set, elements skip the short-form probe that uniformly
// long values (geographic coordinates) always fail.
floatLong bool
// strings is the current append-only owned-string block.
strings *decoderStringBlock
// state carries uncommon per-decode storage behind one pointer. The
// destination range detects pointers into sibling value storage.
state *decoderState
}
// decoderState carries uncommon structural and Replace-mode state between
// parser round trips. Owned string blocks live directly on decoderCursor:
// unlike pooled operation metadata, destination strings must remain alive and
// immutable after Decode returns.
type decoderState struct {
structural decoderStructuralTape
structuralActive bool
operation *decoderOperationState
replaceDestination unsafe.Pointer
replaceSpan uint32
}
// newDecoderCursor starts decoding src with opts.
func newDecoderCursor(src []byte, opts DecoderOptions) decoderCursor {
var flags decoderFlags
if opts.ZeroCopy {
flags |= decoderZeroCopy
}
if opts.DisallowUnknownFields {
flags |= decoderDisallowUnknown
}
if opts.CaseSensitive {
flags |= decoderCaseSensitive
}
if opts.Replace {
flags |= decoderReplace
}
if opts.UseNumber {
flags |= decoderUseNumber
}
return decoderCursor{
src: src,
maxDepth: int32(opts.MaxDepth),
flags: flags,
}
}
// Finish verifies that exactly one complete JSON value was consumed.
func (c *decoderCursor) Finish() error {
if c.i == len(c.src) {
return nil
}
return c.finishSlow()
}
//go:noinline
func (c *decoderCursor) finishSlow() error {
c.skipSpace()
if c.i != len(c.src) {
return c.err(c.i, "unexpected data after top-level value")
}
return nil
}
// TryNull consumes null and reports true, or leaves a non-null value untouched.
func (c *decoderCursor) TryNull() (bool, error) {
i := c.i
if i < len(c.src) && c.src[i] > ' ' && c.src[i] != 'n' {
return false, nil
}
return c.tryNullSlow()
}
// notNullFast reports that the next byte proves a non-null value with no
// leading whitespace, letting callers skip the TryNull call entirely on the
// common present-value path. TryNull itself cannot fit the inlining budget
// because of its mandatory slow-path call.
func (c *decoderCursor) notNullFast() bool {
i := c.i
return i < len(c.src) && c.src[i] > ' ' && c.src[i] != 'n'
}
//go:noinline
func (c *decoderCursor) tryNullSlow() (bool, error) {
c.skipSpace()
if c.i >= len(c.src) || c.src[c.i] != 'n' {
return false, nil
}
if !literalNullAt(c.src, c.i) {
return false, c.err(c.i, "invalid literal")
}
c.i += 4
return true, nil
}
// BeginObject consumes an opening object delimiter.
func (c *decoderCursor) BeginObject(typeName string) error {
i := c.i
if i < len(c.src) && c.src[i] == '{' && c.depth < c.maxDepth {
c.depth++
c.i = i + 1
return nil
}
return c.beginObjectSlow(typeName)
}
//go:noinline
func (c *decoderCursor) beginObjectSlow(typeName string) error {
c.skipSpace()
if c.i >= len(c.src) || c.src[c.i] != '{' {
return c.expected(typeName, "object")
}
if c.depth >= c.maxDepth {
return c.err(c.i, "maximum nesting depth exceeded")
}
c.depth++
c.i++
return nil
}
// NextObjectField returns the next decoded key. first must be true only for
// the first call after BeginObject. The key aliases the source (or the
// string arena when escaped) — callers that retain it own the aliasing
// rules of the current decode mode.
func (c *decoderCursor) NextObjectField(first bool) (key string, ok bool, err error) {
i := c.i
if uint(i) < uint(len(c.src)) && c.src[i] <= ' ' {
// Pretty-printed documents lead every member and closing brace with a
// newline-and-indent run. Consuming it here keeps such documents on
// the packed key match below; without this, every member of an
// indented object detours through the slow parser. Whitespace
// consumption never needs rollback, so the position commits at once.
i = c.skipSpaceAt(i)
c.i = i
}
if i >= len(c.src) {
return c.nextObjectFieldSlow(first)
}
if first {
if c.src[i] == '}' {
c.i = i + 1
c.depth--
return "", false, nil
}
if c.src[i] != '"' {
return c.nextObjectFieldSlow(first)
}
} else {
switch c.src[i] {
case '}':
c.i = i + 1
c.depth--
return "", false, nil
case ',':
i++
if uint(i) < uint(len(c.src)) && c.src[i] <= ' ' {
// The gap between comma and key stays local: on the rare
// fallthrough the slow parser re-reads from the committed
// pre-comma position and owns every error offset.
i = c.skipSpaceAt(i)
}
if i >= len(c.src) || c.src[i] != '"' {
return c.nextObjectFieldSlow(first)
}
default:
return c.nextObjectFieldSlow(first)
}
}
keyStart := i + 1
keyEnd := keyStart
if keyStart+8 <= len(c.src) {
mask := stringSpecialMask(binary.LittleEndian.Uint64(c.src[keyStart:]))
if mask == 0 {
return c.nextObjectFieldSlow(first)
}
keyEnd += bits.TrailingZeros64(mask) / 8
} else {
keyEnd = scanStringSpecial(c.src, keyStart)
}
// One 16-bit load checks the closing quote and colon together; the
// length guard covers both bytes.
if keyEnd+2 > len(c.src) ||
loadUint16LE(unsafe.Add(sliceBase(c.src), keyEnd)) != quoteColonLE {
return c.nextObjectFieldSlow(first)
}
key = byteview.String(c.src[keyStart:keyEnd])
c.i = keyEnd + 2
if i := c.i; i < len(c.src) && c.src[i] <= ' ' {
// A pretty-printer writes exactly one space between colon and value;
// that shape advances without a call. Wider or structural gaps take
// the shared skipper. (An inlineable helper does not fit: the
// skipSpace call alone costs 57 of the 80-node budget.) The manual
// advance is safe under an active structural tape — position lookups
// are monotonic and realign lazily on the next query.
if c.src[i] == ' ' && uint(i+1) < uint(len(c.src)) && c.src[i+1] > ' ' {
c.i = i + 1
} else {
c.skipSpace()
}
}
return key, true, nil
}
//go:noinline
func (c *decoderCursor) nextObjectFieldSlow(first bool) (key string, ok bool, err error) {
c.skipSpace()
if c.i >= len(c.src) {
return "", false, c.err(c.i, "unterminated object")
}
if !first {
switch c.src[c.i] {
case '}':
c.i++
c.depth--
return "", false, nil
case ',':
c.i++
default:
return "", false, c.err(c.i, "expected comma or closing brace in object")
}
} else if c.src[c.i] == '}' {
c.i++
c.depth--
return "", false, nil
}
c.skipSpace()
if c.i >= len(c.src) || c.src[c.i] != '"' {
return "", false, c.err(c.i, "expected object key string")
}
keyStart := c.i + 1
if keyStart+8 <= len(c.src) {
mask := stringSpecialMask(binary.LittleEndian.Uint64(c.src[keyStart:]))
keyEnd := keyStart + bits.TrailingZeros64(mask)/8
if mask != 0 && c.src[keyEnd] == '"' {
key = byteview.String(c.src[keyStart:keyEnd])
c.i = keyEnd + 1
} else {
key, err = c.typedKey()
}
} else {
key, err = c.typedKey()
}
if err != nil {
return "", false, err
}
c.skipSpace()
if c.i >= len(c.src) || c.src[c.i] != ':' {
return "", false, c.err(c.i, "expected colon after object key")
}
c.i++
c.skipSpace()
return key, true, nil
}
// BeginArray consumes an opening array delimiter.
func (c *decoderCursor) BeginArray(typeName string) error {
i := c.i
if i < len(c.src) && c.src[i] == '[' && c.depth < c.maxDepth {
c.depth++
c.i = i + 1
return nil
}
return c.beginArraySlow(typeName)
}
//go:noinline
func (c *decoderCursor) beginArraySlow(typeName string) error {
c.skipSpace()
if c.i >= len(c.src) || c.src[c.i] != '[' {
return c.expected(typeName, "array")
}
if c.depth >= c.maxDepth {
return c.err(c.i, "maximum nesting depth exceeded")
}
c.depth++
c.i++
return nil
}
// NextArrayElement reports whether another value is available. first must be
// true only for the first call after BeginArray.
func (c *decoderCursor) NextArrayElement(first bool) (bool, error) {
i := c.i
if uint(i) < uint(len(c.src)) && c.src[i] <= ' ' {
// Indented arrays open with a newline before the first element and
// close with one before the bracket; consuming the run here keeps
// both transitions off the slow path, as in NextObjectField.
i = c.skipSpaceAt(i)
c.i = i
}
if i >= len(c.src) {
return c.nextArrayElementSlow(first)
}
if first {
if c.src[i] == ']' {
c.i = i + 1
c.depth--
return false, nil
}
if c.src[i] > ' ' {
return true, nil
}
return c.nextArrayElementSlow(first)
}
switch c.src[i] {
case ']':
c.i = i + 1
c.depth--
return false, nil
case ',':
c.i = i + 1
if c.i < len(c.src) && c.src[c.i] <= ' ' {
c.i = c.skipSpaceAt(c.i)
}
return true, nil
}
return c.nextArrayElementSlow(first)
}
//go:noinline
func (c *decoderCursor) nextArrayElementSlow(first bool) (bool, error) {
c.skipSpace()
if c.i >= len(c.src) {
return false, c.err(c.i, "unterminated array")
}
if !first {
switch c.src[c.i] {
case ']':
c.i++
c.depth--
return false, nil
case ',':
c.i++
default:
return false, c.err(c.i, "expected comma or closing bracket in array")
}
} else if c.src[c.i] == ']' {
c.i++
c.depth--
return false, nil
}
c.skipSpace()
return true, nil
}
// Skip validates and consumes the next value without materializing it.
func (c *decoderCursor) Skip() error {
p := c.slowParser()
err := p.skipTypedValue(int(c.depth))
c.i = p.i
return err
}
// Unknown skips key unless unknown fields are disallowed.
func (c *decoderCursor) Unknown(typeName, key string) error {
if c.flags&decoderDisallowUnknown != 0 {
return &DecodeError{Offset: c.i, TypeName: typeName, Reason: "unknown field " + strconv.Quote(key)}
}
return c.Skip()
}
// CaseSensitive reports whether folded field matching is disabled.
func (c *decoderCursor) CaseSensitive() bool {
return c.flags&decoderCaseSensitive != 0
}
func (c *decoderCursor) stringStructural(dst *string) error {
i := c.i
if i >= len(c.src) || c.src[i] != '"' {
return c.stringStructuralSlow(dst)
}
tape := &c.state.structural
index := tape.index
positions := tape.positions
if uint(index+1) >= uint(len(positions)) || int(positions[index]) != i {
return c.stringStructuralSlow(dst)
}
entry := positions[index+1]
end := int(entry)
if end >= len(c.src) || c.src[end] != '"' {
return c.stringStructuralSlow(dst)
}
tape.index = index + 1
start := i + 1
if !tape.nonASCII && !tape.escaped || c.structuralStringLocallyDirect(start, end) {
if c.flags&decoderZeroCopy != 0 {
*dst = byteview.String(c.src[start:end])
} else if c.reuseOwnedString(*dst, start, end) {
// Keep the already-owned identical value.
} else {
*dst = c.ownedString(start, end)
}
c.i = end + 1
return nil
}
return c.stringStructuralExactSlow(dst, start, end)
}
// structuralStringLocallyDirect proves that one structural string can use the
// source-backed path after another string made the document-wide facts dirty.
// It is only called after the all-clean branch misses.
func (c *decoderCursor) structuralStringLocallyDirect(start, end int) bool {
if uint(start) > uint(end) || uint(end) > uint(len(c.src)) {
return false
}
span := c.src[start:end]
tape := &c.state.structural
if tape.escaped && tape.stringRangeDirty(start, end, false) && scanStringSyntax(span, 0) != len(span) {
return false
}
return !tape.nonASCII || !tape.stringRangeDirty(start, end, true) || validUTF8Fast(span)
}
//go:noinline
func (c *decoderCursor) stringStructuralExactSlow(dst *string, start, end int) error {
tape := &c.state.structural
if !tape.escaped && (!tape.nonASCII || validUTF8Fast(c.src[start:end])) ||
c.structuralStringLocallyDirect(start, end) {
if !c.reuseOwnedString(*dst, start, end) {
*dst = c.ownedString(start, end)
}
c.i = end + 1
return nil
}
_, decodedCapacity := rawJSONStringLayoutHint(c.src, start)
text, err := c.parseOwnedString(end, decodedCapacity)
if err != nil {
return err
}
*dst = text
return nil
}
// parseOwnedString decodes the string at c.i into the cursor's owned block.
// end is a proven or conservative raw closing-quote position and decodedCapacity
// bounds the decoded bytes. The reservation proves parser appends cannot move.
func (c *decoderCursor) parseOwnedString(end, decodedCapacity int) (string, error) {
start := c.i + 1
if end < start || end > len(c.src) {
end = len(c.src)
}
if decodedCapacity < 0 || decodedCapacity > end-start {
decodedCapacity = end - start
}
c.ensureStringArenaCapacity(decodedCapacity + stringArenaHeadroom)
block := c.strings
p := c.slowParser()
p.strings = block.bytes()[:block.used:block.capacity]
text, err := p.parseString()
c.i = p.i
data := block.bytes()
if unsafe.SliceData(p.strings) == unsafe.SliceData(data) {
block.used = len(p.strings)
}
if err != nil {
return "", err
}
return text, nil
}
// rawJSONStringLayoutHint locates the next unescaped quote and returns a safe
// decoded-size bound without validating syntax. parseString remains
// authoritative; this pass only sizes owned escaped output.
func rawJSONStringLayoutHint(src []byte, i int) (end, decodedCapacity int) {
for i < len(src) {
switch src[i] {
case '"':
return i, decodedCapacity
case '\\':
i++
if i >= len(src) {
return len(src), decodedCapacity
}
if src[i] == 'u' {
if i > len(src)-5 {
return len(src), decodedCapacity
}
decodedCapacity += 3
i += 5
continue
}
decodedCapacity++
i++
continue
}
decodedCapacity++
i++
}
return len(src), decodedCapacity
}
//go:noinline
func (c *decoderCursor) stringStructuralSlow(dst *string) error {
if i := c.i; i < len(c.src) && c.src[i] == '"' {
c.structuralStringEnd(i)
}
return c.String(dst)
}
func shortTypedFloatAt(base unsafe.Pointer, n, start int) (value float64, end int, ok bool) {
if uint(start) >= uint(n) {
return 0, start, false
}
negative := false
i := start
if fastByteAt(base, i) == '-' {
negative = true
i++
}
if i >= n || !IsDigit(fastByteAt(base, i)) {
return 0, start, false
}
value = float64(fastByteAt(base, i) - '0')
i++
if typedNumberEnd(base, n, i) {
if negative {
value = -value
}
return value, i, true
}
if i >= n {
return 0, start, false
}
switch fastByteAt(base, i) {
case '.':
i++
if i >= n || !IsDigit(fastByteAt(base, i)) {
return 0, start, false
}
value += float64(fastByteAt(base, i)-'0') / 10
i++
case 'e', 'E':
i++
exponentNegative := false
if i < n && (fastByteAt(base, i) == '+' || fastByteAt(base, i) == '-') {
exponentNegative = fastByteAt(base, i) == '-'
i++
}
if i >= n || !IsDigit(fastByteAt(base, i)) {
return 0, start, false
}
exponent := int(fastByteAt(base, i) - '0')
if exponentNegative {
value /= anyPow10[exponent]
} else {
value *= anyPow10[exponent]
}
i++
default:
return 0, start, false
}
if !typedNumberEnd(base, n, i) {
return 0, start, false
}
if negative {
value = -value
}
return value, i, true
}
func typedNumberEnd(base unsafe.Pointer, n, i int) bool {
if i == n {
return true
}
if uint(i) >= uint(n) {
return false
}
switch fastByteAt(base, i) {
case ',', ']', '}', ' ', '\n', '\r', '\t':
return true
default:
return false
}
}
// ownedString copies one source span into a compact result-owned string block.
// Blocks are append-only and never recycled across Decode calls, so strings
// already returned to the destination remain immutable for their full lifetime.
func (c *decoderCursor) ownedString(start, end int) string {
if start == end {
return ""
}
if c.flags&decoderZeroCopy != 0 {
return byteview.String(c.src[start:end])
}
c.ensureStringArenaCapacity(end - start)
block := c.strings
offset := block.used
copy(block.bytes()[offset:], c.src[start:end])
block.used += end - start
return OwnedBytesString(block.bytes()[offset:block.used])
}
// reuseOwnedString reports whether existing already contains the source span
// and is independently owned. The source-range check prevents a caller from
// seeding dst with a string view into this decode's mutable input and having
// that alias survive an owned Decode.
func (c *decoderCursor) reuseOwnedString(existing string, start, end int) bool {
if c.flags&decoderZeroCopy != 0 || len(existing) != end-start {
return false
}
if len(existing) == 0 {
return true
}
source := uintptr(unsafe.Pointer(unsafe.SliceData(c.src)))
data := uintptr(unsafe.Pointer(unsafe.StringData(existing)))
if data >= source && data-source <= uintptr(len(c.src)-len(existing)) {
return false
}
return existing == byteview.String(c.src[start:end])
}
// ownedText copies text only when it aliases the decode source. Escaped text
// already lives in a result-owned arena block and can be retained as-is.
func (c *decoderCursor) ownedText(text string) string {
if len(text) == 0 || c.flags&decoderZeroCopy != 0 {
return text
}
source := uintptr(unsafe.Pointer(unsafe.SliceData(c.src)))
data := uintptr(unsafe.Pointer(unsafe.StringData(text)))
if data < source || data-source > uintptr(len(c.src)-len(text)) {
return text
}
return c.ownedString(int(data-source), int(data-source)+len(text))
}
func (c *decoderCursor) skipSpace() {
if c.state != nil && c.state.structuralActive {
if uint(c.i) >= uint(len(c.src)) || !IsJSONWhitespace(c.src[c.i]) {
return
}
c.i = c.state.structural.position(c.i, len(c.src))
return
}
c.i = SkipSpace(c.src, c.i)
}
func (c *decoderCursor) err(offset int, reason string) error {
return syntaxError(c.src, offset, reason)
}
func (c *decoderCursor) slowParser() parser {
return parser{src: c.src, i: c.i, maxDepth: int(c.maxDepth), zeroCopy: true}
}
// prepareOwnedParser lends the cursor's current result-owned block to a
// dynamic parser when the next value can retain text. The block is
// pessimistically sealed while parsing: if the parser outgrows it, strings
// already returned from the old block must never be overwritten.
// finishOwnedParser restores the exact used prefix when no relocation occurred.
func (c *decoderCursor) prepareOwnedParser(p *parser, useNumber bool) *decoderStringBlock {
if p.zeroCopy || !anyValueMayRetainText(p.src, p.i, useNumber) || anyValueIsEmpty(p.src, p.i) {
return nil
}
c.ensureStringArenaCapacity(stringArenaHeadroom)
block := c.strings
data := block.bytes()
p.strings = data[:block.used:block.capacity]
block.used = block.capacity
return block
}
func anyValueMayRetainText(src []byte, i int, useNumber bool) bool {
if uint(i) >= uint(len(src)) {
return false
}
switch src[i] {
case '"', '[', '{':
return true
default:
return useNumber && (src[i] == '-' || IsDigit(src[i]))
}
}
func anyValueIsEmpty(src []byte, i int) bool {
if uint(i) >= uint(len(src)) {
return false
}
close := byte(0)
switch src[i] {
case '"':
close = '"'
case '[':
close = ']'
case '{':
close = '}'
default:
return false
}
i++
if close != '"' {
i = SkipSpace(src, i)
}
return i < len(src) && src[i] == close
}
func (c *decoderCursor) finishOwnedParser(p *parser, block *decoderStringBlock) {
if block == nil {
return
}
data := block.bytes()
if unsafe.SliceData(p.strings) == unsafe.SliceData(data) {
block.used = len(p.strings)
}
}
func (c *decoderCursor) typedKey() (string, error) {
start := c.i + 1
special := scanStringSpecial(c.src, start)
if special < len(c.src) {
switch c.src[special] {
case '"':
c.i = special + 1
return byteview.String(c.src[start:special]), nil
case '\\':
end, decodedCapacity := rawJSONStringLayoutHint(c.src, start)
return c.parseOwnedString(end, decodedCapacity)
}
}
p := c.slowParser()
key, err := p.typedKey()
c.i = p.i
return key, err
}
// stringArenaSeed sizes the first arena block; stringArenaHeadroom is the
// free space below which the parser starts a fresh block of twice the size
// instead of letting append copy retained content. Real documents carry
// little escaped content, so the arena starts small; escape-dense documents
// pay a handful of block allocations but never re-copy a written byte.
const (
stringArenaSeed = 2048
stringArenaHeadroom = 2048
)
func (c *decoderCursor) ensureStringArenaCapacity(need int) {
if c.strings == nil {
capacity := stringArenaSeed
if capacity > len(c.src) {
capacity = len(c.src) + 1
}
if capacity < need {
capacity = need
}
c.strings = newDecoderStringBlock(capacity)
return
}
if c.strings.capacity-c.strings.used >= need {
return
}
const ownedStringBlockCapacity = 16 << 10
capacity := ownedStringBlockCapacity
if minimum := need + stringArenaHeadroom; capacity < minimum {
capacity = minimum
}
c.strings = newDecoderStringBlock(capacity)
}
func (c *decoderCursor) expected(typeName, jsonType string) error {
return &DecodeError{Offset: c.i, TypeName: typeName, Reason: "expected " + jsonType}
}
// stringToken consumes a JSON string and returns its contents: unescaped
// strings alias the source, escaped strings alias a transient buffer that
// callers must not retain.
func (c *decoderCursor) stringToken() ([]byte, error) {
i := c.i
if i >= len(c.src) || c.src[i] != '"' {
return nil, &DecodeError{Offset: i, Reason: "expected string"}
}
start := i + 1
end := scanStringSpecial(c.src, start)
if end < len(c.src) && c.src[end] == '"' {
c.i = end + 1
return c.src[start:end], nil
}
p := c.slowParser()
text, err := p.parseString()
c.i = p.i
if err != nil {
return nil, err
}
return byteview.Bytes(text), nil
}
func (p *parser) typedKey() (string, error) {
start := p.i + 1
end := scanStringSpecial(p.src, start)
if end < len(p.src) && p.src[end] == '"' {
p.i = end + 1
return byteview.String(p.src[start:end]), nil
}
zeroCopy := p.zeroCopy
p.zeroCopy = true
key, err := p.parseString()
p.zeroCopy = zeroCopy
return key, err
}
func (p *parser) skipTypedValue(depth int) error {
value := validator{src: p.src, i: p.i, maxDepth: p.maxDepth}
if err := value.parseValue(depth); err != nil {
return err
}
p.i = value.i
return nil
}