rusty_rtos_json-core 0.2.0

The pure no_std core of rusty_rtos_json: types, traits and algorithms with no CPU, no allocator and no operating system. forbid(unsafe).
Documentation
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
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
//! `JSON_Validate`: coreJSON's strict ECMA-404 validator, over a byte slice.
//!
//! A transcription of `core_json.c` v3.3.1 (`cffa492`), which is a ladder of
//! `skip*` scanners each advancing a cursor. The shape is kept because the
//! shape IS the specification: coreJSON's acceptances and rejections fall out
//! of exactly where each scanner gives up, and a "tidier" parser would be a
//! different parser.
//!
//! Zero allocation, no recursion, `no_std`, `forbid(unsafe)`. Nesting is
//! bounded by an explicit stack of [`MAX_DEPTH`], as the C bounds it.
//!
//! # Measured before it was written
//!
//! coreJSON scores **100 %** on JSONTestSuite: 95 of 95 `y_` files accepted,
//! 188 of 188 `n_` files rejected. So "agree with the C" and "pass the suite"
//! are the same target here, which was worth establishing rather than
//! assuming — if the C had failed the suite anywhere, those two goals would
//! have pulled apart and one of them would have had to give.
//!
//! The 35 `i_` files, where the standard leaves the answer to the
//! implementation, are the interesting ones: coreJSON accepts 10 and rejects
//! 25. Being *a* JSON parser does not determine those; being *coreJSON* does,
//! and they are where a transcription drifts first.

/// `JSON_MAX_DEPTH`. The C's default, and the reason there is no recursion.
pub const MAX_DEPTH: usize = 32;

/// `JSONStatus_t`, for the subset [`validate`] can answer with.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Validity {
    /// `JSONSuccess`: valid and complete.
    Valid,
    /// `JSONIllegalDocument`: invalid or malformed.
    Illegal,
    /// `JSONMaxDepthExceeded`.
    MaxDepthExceeded,
    /// `JSONPartial`: valid so far, but the buffer ends mid-document.
    Partial,
    /// `JSONBadParameter`: an empty buffer.
    BadParameter,
}

// ---- the character classes, which are the C's macros ---------------------

/// `isspace_`, and it is JSON's whitespace rather than C's: space, tab,
/// newline and carriage return only. A vertical tab or form feed is NOT
/// whitespace to ECMA-404, and `isspace()` from `<ctype.h>` would say it was.
///
/// # Four comparisons, and they beat both clever forms
///
/// This looks like the textbook place for a bitmask or a lookup table, and
/// it is not. The first comparison is the MOST COMMON byte, so a document
/// full of whitespace answers in one instruction; a form without a branch
/// has no common case to be fast for. Measured, both instruments:
///
/// | form | `json-ir` | `search-ir` |
/// |---|---:|---:|
/// | these four comparisons | baseline | baseline |
/// | a 64-bit mask, `c < 64 && (SPACE >> c) & 1` | **+1.42%** | -0.77% |
/// | a generated `[bool; 256]`, one load | **+2.77%** | -0.32% |
///
/// The mask's split sign is the short-circuit stating itself: better where
/// whitespace is rare, worse where it is common. Contrast [`hex_to_int`],
/// where a table DID win -- three range checks with no dominant first arm
/// have no short-circuit to lose.
const fn is_space(c: u8) -> bool {
    c == b' ' || c == b'\t' || c == b'\n' || c == b'\r'
}

/// `isascii_`.
const fn is_ascii(c: u8) -> bool {
    c <= 0x7F
}

/// `iscntrl_`: an ASCII control character, which a string may not contain
/// unescaped.
const fn is_cntrl(c: u8) -> bool {
    is_ascii(c) && c < b' '
}

/// `isdigit_`.
const fn is_digit(c: u8) -> bool {
    c.is_ascii_digit()
}

///
/// `[` is 0x5B and `{` is 0x7B, one bit apart, so `(c | 0x20) == b'{'` is
/// exactly this test in one fewer operation. It measured **+1.22% on
/// `json-ir`** and +0.08% on `search-ir` -- worse on both. LLVM already
/// has the pair and picks its own encoding; folding the bit by hand only
/// took the choice away. Recorded so it is not re-derived.
const fn is_open_bracket(c: u8) -> bool {
    c == b'{' || c == b'['
}

const fn is_close_bracket(c: u8) -> bool {
    c == b'}' || c == b']'
}

/// `isMatchingBracket_`.
///
/// Both pairs are two apart in ASCII -- `[` `]` are 0x5B 0x5D and `{` `}`
/// are 0x7B 0x7D -- so the distance answers first and the opener is checked
/// only when it matches. That second test is what keeps this a predicate
/// about brackets rather than about any two bytes two apart.
const fn is_matching_bracket(open: u8, close: u8) -> bool {
    open.wrapping_add(2) == close && is_open_bracket(open)
}

/// The byte at `i`, or `None` past the end. Every scanner reads through this,
/// so "past the end" is a value rather than a panic.
///
/// # This is defence in depth, and it was measured
///
/// Replacing the body with `Some(buf[i])` -- a panic on any read past the end
/// -- leaves all eleven tests passing, including 20,000 fuzz documents, every
/// truncation of six valid documents, every single-byte corruption of a
/// seventh, and all 318 corpus files. So no caller reads out of bounds today:
/// every scanner establishes `i < max` first.
///
/// The `Option` stays anyway, because that property holds only while every
/// caller is right, and a scanner edited later would lose it silently. The two
/// places where the bound is genuinely load-bearing are
/// [`skip_literal`]'s slice and the depth stack in [`validate`]; poisoning
/// either one panics immediately.
pub(crate) fn at(buf: &[u8], i: usize) -> Option<u8> {
    buf.get(i).copied()
}

// ---- the scanners --------------------------------------------------------

/// `skipSpace`.
pub(crate) fn skip_space(buf: &[u8], start: &mut usize, max: usize) {
    let mut i = *start;
    while i < max {
        match at(buf, i) {
            Some(c) if is_space(c) => i = i.saturating_add(1),
            _ => break,
        }
    }
    *start = i;
}

/// `countHighBits`.
const fn count_high_bits(c: u8) -> usize {
    let mut n = c;
    let mut i = 0usize;
    while (n & 0x80) != 0 {
        i = i.saturating_add(1);
        n = (n & 0x7F) << 1;
    }
    i
}

/// `shortestUTF8`: is this the shortest encoding of this value, and is the
/// value a legal scalar?
///
/// Rejecting an over-long encoding is a security property, not a nicety: the
/// classic exploit is smuggling an ASCII character past a filter as a
/// two-byte sequence.
const fn shortest_utf8(length: usize, value: u32) -> bool {
    let (min, max) = match length {
        2 => (1u32 << 7, (1u32 << 11) - 1),
        3 => (1u32 << 11, (1u32 << 16) - 1),
        _ => (1u32 << 16, 0x10_FFFF),
    };
    value >= min && value <= max && (value < 0xD800 || value > 0xDFFF)
}

/// `skipUTF8MultiByte`.
///
/// Out of line on purpose. Inlined, its leading-bit count and its
/// shortest-form table live inside [`skip_string`]'s frame and are paid for
/// by every string, including the ones that are entirely ASCII. See the note
/// on [`skip_escape`], which has to leave with it.
#[inline(never)]
fn skip_utf8_multibyte(buf: &[u8], start: &mut usize, max: usize) -> bool {
    let mut i = *start;
    let Some(first) = at(buf, i) else {
        return false;
    };
    // `> 0xC1` rejects the two over-long lead bytes; `< 0xF5` rejects
    // anything above U+10FFFF.
    if !(first > 0xC1 && first < 0xF5) {
        return false;
    }

    let bit_count = count_high_bits(first);
    let shift = 7usize.saturating_sub(bit_count);
    let mut value = u32::from(first) & ((1u32 << shift).saturating_sub(1));

    // The bit count is one greater than the number of continuation bytes.
    let mut j = bit_count.saturating_sub(1);
    while j > 0 {
        i = i.saturating_add(1);
        if i >= max {
            break;
        }
        let Some(c) = at(buf, i) else { break };
        // Continuation bytes must match 10xxxxxx.
        if (c & 0xC0) != 0x80 {
            break;
        }
        value = (value << 6) | u32::from(c & 0x3F);
        j = j.saturating_sub(1);
    }

    if j == 0 && shortest_utf8(bit_count, value) {
        *start = i.saturating_add(1);
        return true;
    }
    false
}

/// `skipUTF8`.
fn skip_utf8(buf: &[u8], start: &mut usize, max: usize) -> bool {
    if *start >= max {
        return false;
    }
    match at(buf, *start) {
        Some(c) if is_ascii(c) => {
            *start = start.saturating_add(1);
            true
        }
        Some(_) => skip_utf8_multibyte(buf, start, max),
        None => false,
    }
}

/// `NOT_A_HEX_CHAR`.
const NOT_A_HEX_CHAR: u8 = 0x10;

/// The rule `hexToInt` encodes, kept as the table's GENERATOR and its oracle.
///
/// Each arm's own range is what makes its arithmetic total: the subtraction
/// cannot go below zero and the sum cannot pass 15. The saturating forms say
/// so to the compiler as well as to the reader.
const fn hex_value(c: u8) -> u8 {
    match c {
        b'a'..=b'f' => c.saturating_sub(b'a').saturating_add(10),
        b'A'..=b'F' => c.saturating_sub(b'A').saturating_add(10),
        b'0'..=b'9' => c.saturating_sub(b'0'),
        _ => NOT_A_HEX_CHAR,
    }
}

/// Every byte's hex value, or [`NOT_A_HEX_CHAR`].
///
/// **Generated from [`hex_value`]**, so the table and the rule it encodes
/// cannot drift apart -- a hand-typed table is the classic way to ship a
/// parser that accepts one byte it should not.
const fn build_hex() -> [u8; 256] {
    let mut table = [NOT_A_HEX_CHAR; 256];
    let mut c = 0usize;
    while c < 256 {
        // Indexing a fixed 256-entry array by a counter the loop condition
        // has just proven below 256. `get_mut` is not const here, and a
        // const initialiser cannot panic at run time in any case.
        #[allow(clippy::indexing_slicing)]
        {
            table[c] = hex_value(c as u8);
        }
        c = c.saturating_add(1);
    }
    table
}

/// See [`build_hex`].
static HEX: [u8; 256] = build_hex();

/// `hexToInt`, as one load.
///
/// The three range arms this replaces cost a comparison pair each, and a
/// `\uXXXX` pays them four times -- eight across a surrogate pair. `json-ir`
/// put **7.6%** of its whole total in `skip_hex_escape`.
///
/// That share is why fifteen earlier wins in this file went straight past it:
/// they were all measured on `search-ir`, whose documents carry no hex escape
/// at all, so the function was never once hot in the instrument being read.
#[inline]
fn hex_to_int(c: u8) -> u8 {
    match HEX.get(c as usize) {
        Some(value) => *value,
        // `u8 as usize` is below 256 and the table is 256 long, so this arm
        // is unreachable. Answering "not a hex char" rather than panicking is
        // what keeps this crate's no-panic property a property.
        None => NOT_A_HEX_CHAR,
    }
}

/// `skipOneHexEscape`: `\uXXXX`.
fn skip_one_hex_escape(buf: &[u8], start: &mut usize, max: usize, out: &mut u16) -> bool {
    let mut i = *start;
    // `HEX_ESCAPE_LENGTH` is 6: the backslash, the `u`, and four digits.
    let end = i.saturating_add(6);
    let mut value = 0u16;

    // `end < max` and NOT `<=`, which is the C's. It means the escape must be
    // followed by at least one more byte -- and it always is, because a string
    // still needs its closing quote.
    if end > i
        && end < max
        && at(buf, i) == Some(b'\\')
        && at(buf, i.saturating_add(1)) == Some(b'u')
    {
        // Four digits, unrolled, and validated with ONE test.
        //
        // `NOT_A_HEX_CHAR` is `0x10` and every real value is `0x0..=0xF`, so
        // bit four is set exactly when a digit is not hex. OR the four
        // together and that one bit answers for all of them -- where the
        // byte-at-a-time loop carried a bound, a branch per digit and an
        // `Option` per digit.
        //
        // A byte past the end reads as `0`, which is NUL, which is not a hex
        // digit -- so an out-of-range escape fails through the same test
        // rather than needing its own. `end < max` above means it cannot
        // happen anyway.
        i = i.saturating_add(2);
        let n0 = hex_to_int(at(buf, i).unwrap_or(0));
        let n1 = hex_to_int(at(buf, i.wrapping_add(1)).unwrap_or(0));
        let n2 = hex_to_int(at(buf, i.wrapping_add(2)).unwrap_or(0));
        let n3 = hex_to_int(at(buf, i.wrapping_add(3)).unwrap_or(0));
        if (n0 | n1 | n2 | n3) & NOT_A_HEX_CHAR == 0 {
            value = (u16::from(n0) << 12)
                | (u16::from(n1) << 8)
                | (u16::from(n2) << 4)
                | u16::from(n3);
            i = end;
        }
    }

    if i == end {
        *out = value;
        *start = i;
        return true;
    }
    false
}

const fn is_high_surrogate(x: u16) -> bool {
    x >= 0xD800 && x <= 0xDBFF
}

const fn is_low_surrogate(x: u16) -> bool {
    x >= 0xDC00 && x <= 0xDFFF
}

/// `skipHexEscape`: one escape, or a surrogate PAIR.
///
/// A high surrogate must be followed by a low one, and a lone low surrogate is
/// refused outright. That is stricter than "any four hex digits" and it is
/// where several `i_` files in JSONTestSuite land.
fn skip_hex_escape(buf: &[u8], start: &mut usize, max: usize) -> bool {
    let mut i = *start;
    let mut value = 0u16;
    let mut ret = false;

    if skip_one_hex_escape(buf, &mut i, max, &mut value) {
        if is_high_surrogate(value) {
            if skip_one_hex_escape(buf, &mut i, max, &mut value) && is_low_surrogate(value) {
                ret = true;
            }
        } else if is_low_surrogate(value) {
            // A premature low surrogate: refused.
        } else {
            ret = true;
        }
    }

    if ret {
        *start = i;
    }
    ret
}

/// `skipEscape`.
///
/// Out of line on purpose, and this is where most of it is: the `u` arm's
/// four hex digits and surrogate pairing needed callee-saved registers that
/// [`skip_string`] then had to push and pop on every call, escape or no
/// escape.
///
/// It only pays if [`skip_utf8_multibyte`] goes with it -- either one left
/// inline still claims the registers, so outlining this one alone takes half
/// as much off `search-ir` and puts nearly three times as much back on
/// `json-ir`. Keeping the two-byte escapes here and outlining only the rest
/// is worse than both, for the same reason: the match stays.
#[inline(never)]
fn skip_escape(buf: &[u8], start: &mut usize, max: usize) -> bool {
    let mut i = *start;
    let mut ret = false;

    // `i < max - 1`: an escape needs at least one byte after the backslash.
    if i < max.saturating_sub(1) && at(buf, i) == Some(b'\\') {
        match at(buf, i.saturating_add(1)) {
            Some(b'u') => ret = skip_hex_escape(buf, &mut i, max),
            Some(b'"' | b'\\' | b'/' | b'b' | b'f' | b'n' | b'r' | b't') => {
                i = i.saturating_add(2);
                ret = true;
            }
            _ => {}
        }
    }

    if ret {
        *start = i;
    }
    ret
}

/// `skipString`.
pub(crate) fn skip_string(buf: &[u8], start: &mut usize, max: usize) -> bool {
    let mut i = *start;
    let mut ret = false;
    // `max` is a promise about the document; the buffer is the promise about
    // memory. Clamping one to the other says so once, and every read below is
    // then provably inside the slice -- where before each one carried its own
    // bounds check. Past the buffer the old loop stopped on a `None` read;
    // this one stops on the bound, at the same index.
    let max = max.min(buf.len());

    if i < max && at(buf, i) == Some(b'"') {
        i = i.saturating_add(1);
        while i < max {
            match at(buf, i) {
                Some(b'"') => {
                    ret = true;
                    i = i.saturating_add(1);
                    break;
                }
                Some(b'\\') => {
                    if !skip_escape(buf, &mut i, max) {
                        break;
                    }
                }
                // An unescaped control character is not allowed.
                Some(c) if is_cntrl(c) => break,
                Some(_) => {
                    if !skip_utf8(buf, &mut i, max) {
                        break;
                    }
                }
                None => break,
            }
        }
    }

    if ret {
        *start = i;
    }
    ret
}

/// `skipLiteral`.
fn skip_literal(buf: &[u8], start: &mut usize, max: usize, literal: &[u8]) -> bool {
    let from = *start;
    if from >= max || literal.len() > max.saturating_sub(from) {
        return false;
    }
    let end = from.saturating_add(literal.len());
    let Some(slice) = buf.get(from..end) else {
        return false;
    };
    if slice == literal {
        *start = end;
        return true;
    }
    false
}

/// The largest value an array index may reach: `MAX_INDEX_VALUE`, which the
/// C's header defines as `0x7FFFFFF7`, or 2^31 - 9.
pub(crate) const MAX_INDEX_VALUE: i32 = 0x7FFF_FFF7;

/// `MAX_FACTOR`: the largest accumulator that can still take another digit.
const MAX_FACTOR: i32 = MAX_INDEX_VALUE / 10;

/// `skipDigits`, including the `outValue` half.
///
/// [`validate`] never asks for the value, but `multiSearch` reads an array
/// index with it, and the overflow behaviour is load-bearing there: once the
/// accumulator passes `MAX_FACTOR` the C latches it to **-1** and stops
/// accumulating, and a negative index is what makes `[99999999999999999999]`
/// a `BadParameter` rather than a wrapped-around lookup.
///
/// Returns `None` when there were no digits at all (the C's `false`), and
/// otherwise the accumulated value, which may be -1.
pub(crate) fn skip_digits_value(buf: &[u8], start: &mut usize, max: usize) -> Option<i32> {
    let from = *start;
    let mut i = from;
    let mut value: i32 = 0;

    while i < max {
        let Some(c) = at(buf, i) else { break };
        if !is_digit(c) {
            break;
        }

        if value > -1 {
            let n = i32::from(hex_to_int(c));
            value = if value <= MAX_FACTOR {
                value.saturating_mul(10).saturating_add(n)
            } else {
                -1
            };
        }

        i = i.saturating_add(1);
    }

    if i > from {
        *start = i;
        return Some(value);
    }
    None
}

/// `skipDigits` with `outValue == NULL`. The C skips only the accumulation,
/// never the scan, so this is the same walk with the answer thrown away.
fn skip_digits(buf: &[u8], start: &mut usize, max: usize) -> bool {
    skip_digits_value(buf, start, max).is_some()
}

/// `skipDecimals`. Note it advances `start` only when digits FOLLOW the dot,
/// so `1.` leaves the cursor on the dot and the document fails later.
fn skip_decimals(buf: &[u8], start: &mut usize, max: usize) {
    let mut i = *start;
    if i < max && at(buf, i) == Some(b'.') {
        i = i.saturating_add(1);
        if skip_digits(buf, &mut i, max) {
            *start = i;
        }
    }
}

/// `skipExponent`. Same shape as [`skip_decimals`]: a bare `e` does not
/// advance, so `1e` fails.
fn skip_exponent(buf: &[u8], start: &mut usize, max: usize) {
    let mut i = *start;
    if i < max && matches!(at(buf, i), Some(b'e' | b'E')) {
        i = i.saturating_add(1);
        if i < max && matches!(at(buf, i), Some(b'-' | b'+')) {
            i = i.saturating_add(1);
        }
        if skip_digits(buf, &mut i, max) {
            *start = i;
        }
    }
}

/// `skipNumber`.
///
/// In line on purpose. It reaches the scalar dispatch already knowing the
/// first byte is `-` or a digit, and out of line it re-read that byte behind
/// a call and a frame, 90,900 times. `skipDecimals`, `skipExponent` and the
/// digit walk all fold into it, so the frame it was paying for is the whole
/// number scanner's.
#[inline(always)]
fn skip_number(buf: &[u8], start: &mut usize, max: usize) -> bool {
    // Clamped once: past the buffer every read answers `None`, which fails
    // the comparison it feeds anyway -- and `skipDecimals` and `skipExponent`
    // fold into here, so one clamp covers all five of them.
    let max = max.min(buf.len());
    let mut i = *start;
    let mut ret = false;

    if i < max && at(buf, i) == Some(b'-') {
        i = i.saturating_add(1);
    }

    if i < max {
        // JSON disallows superfluous leading zeroes, so a leading zero is a
        // whole integer part on its own.
        if at(buf, i) == Some(b'0') {
            ret = true;
            i = i.saturating_add(1);
        } else {
            ret = skip_digits(buf, &mut i, max);
        }
    }

    if ret {
        skip_decimals(buf, &mut i, max);
        skip_exponent(buf, &mut i, max);
        *start = i;
    }
    ret
}

/// `skipAnyScalar`, out of line. The callers that want it in line call
/// [`any_scalar`] instead.
pub(crate) fn skip_any_scalar(buf: &[u8], start: &mut usize, max: usize) -> bool {
    any_scalar(buf, start, max)
}

/// The body of [`skip_any_scalar`], for the call sites that want it in line.
///
/// # Why this is split at all
///
/// One function has one body, so an inlining decision on it is made once and
/// then serves every caller -- and here the callers do not agree. Forcing it
/// in line everywhere costs search-ir 3,972,988 while taking 1,105,466 off
/// json-ir, and leaving it out of line everywhere is the reverse. Neither
/// instrument is wrong; they call it from different places.
///
/// Splitting the body from the symbol hands the choice back to each site.
/// The two below -- the array and object scalar walks, which is the whole of
/// what `validate` does -- take it in line. `next_value` in the query walk
/// keeps the call, and that is where the 3,972,988 was. Both instruments
/// then win: search-ir -669,013 and json-ir -1,117,915.
#[inline(always)]
fn any_scalar(buf: &[u8], start: &mut usize, max: usize) -> bool {
    // One dispatch on the first byte instead of up to three failed parses.
    // JSON is unambiguous at the first character, so at most one of these can
    // succeed: `skip_string` needs `"`, each literal needs its own initial,
    // and `skip_number` needs `-` or a digit. The chain this replaces
    // tried the string, then all three literals, then the number -- so every
    // number paid for a failed string parse and three slice comparisons
    // before it began.
    //
    // `skipAnyLiteral` is folded in rather than called. It was the same
    // dispatch one level down -- it read this byte again and matched it into
    // the same three arms -- and the byte that says "this is a literal" is
    // the byte that says which literal it is.
    //
    // Byte-identical by construction: each of the three writes `*start` only
    // on success, so a parse that cannot match leaves nothing behind, and not
    // running it is invisible to everything downstream.
    //
    // And no `*start >= max` guard: all three refuse that themselves --
    // `skip_string` tests `i < max`, `skip_literal` opens with `from >= max`,
    // `skip_number` guards both branches -- so a guard here is a second test
    // of something already tested.
    match at(buf, *start) {
        Some(b'"') => skip_string(buf, start, max),
        Some(b't') => skip_literal(buf, start, max, b"true"),
        Some(b'f') => skip_literal(buf, start, max, b"false"),
        Some(b'n') => skip_literal(buf, start, max, b"null"),
        Some(b'-' | b'0'..=b'9') => skip_number(buf, start, max),
        _ => false,
    }
}

/// `skipSpaceAndComma`: true only when a comma is followed by more content.
///
/// A comma before a closing bracket answers false, which is what makes a
/// trailing comma illegal.
pub(crate) fn skip_space_and_comma(buf: &[u8], start: &mut usize, max: usize) -> bool {
    skip_space(buf, start, max);
    let mut i = *start;
    if i < max && at(buf, i) == Some(b',') {
        i = i.saturating_add(1);
        skip_space(buf, &mut i, max);
        if i < max && !at(buf, i).is_some_and(is_close_bracket) {
            *start = i;
            return true;
        }
    }
    false
}

/// `skipArrayScalars`.
///
/// In line on purpose, and with [`skip_object_scalars`]: `skip_scalars` is
/// the only caller of either, it has already skipped the space and read the
/// byte that chose between them, and out of line each one re-established
/// that behind a call and a frame. Worth -994,800 on search-ir on its own
/// and -2,432,600 on json-ir.
#[inline(always)]
fn skip_array_scalars(buf: &[u8], start: &mut usize, max: usize) -> bool {
    // Clamped once: past the buffer every read answers `None`, which
    // is what the loop below breaks on anyway.
    let max = max.min(buf.len());
    let mut i = *start;
    let mut ret = true;

    while i < max {
        // An element that opens a collection is not a scalar, so the scalar
        // attempt could only answer false and leave `i` where it is -- which
        // is the same state this break leaves. `skipCollection`'s stack is
        // what handles it, exactly as it does when the parse fails.
        if at(buf, i).is_some_and(is_open_bracket) {
            break;
        }
        if !any_scalar(buf, &mut i, max) {
            break;
        }
        if !skip_space_and_comma(buf, &mut i, max) {
            // After a scalar there must be either a comma with more content,
            // or the closing bracket.
            if i >= max || at(buf, i) != Some(b']') {
                ret = false;
            }
            break;
        }
    }

    *start = i;
    ret
}

/// `skipObjectScalars`.
///
/// In line on purpose; see [`skip_array_scalars`]. Worth -2,550,612 on
/// search-ir on its own and -598,770 on json-ir.
#[inline(always)]
fn skip_object_scalars(buf: &[u8], start: &mut usize, max: usize) -> bool {
    // Clamped once: past the buffer every read answers `None`, which
    // is what the loop below breaks on anyway.
    let max = max.min(buf.len());
    let mut i = *start;
    let mut ret = true;

    while i < max {
        if !skip_string(buf, &mut i, max) {
            ret = false;
            break;
        }
        skip_space(buf, &mut i, max);
        if i < max && at(buf, i) != Some(b':') {
            ret = false;
            break;
        }
        i = i.saturating_add(1);
        skip_space(buf, &mut i, max);

        // A nested collection: hand back to `skipCollection`.
        if i < max && at(buf, i).is_some_and(is_open_bracket) {
            *start = i;
            break;
        }
        if !any_scalar(buf, &mut i, max) {
            ret = false;
            break;
        }

        let comma = skip_space_and_comma(buf, &mut i, max);
        *start = i;
        if !comma {
            break;
        }
    }

    ret
}

/// `skipScalars`.
///
/// In line on purpose. `skip_collection` is its only caller, from two sites,
/// and left out of line it was paying six callee-saved pushes, six pops and
/// a call on every one of them to decide whether a collection has contents.
/// It is a big function and LLVM declined on size; the frame is the reason
/// to overrule that.
///
/// The same attribute on `skip_any_scalar` costs 3,972,988 on search-ir
/// while taking 1,105,466 off json-ir, and on `skip_collection` it costs
/// both. Neither is here.
#[inline(always)]
fn skip_scalars(buf: &[u8], start: &mut usize, max: usize, mode: u8) -> bool {
    // Clamped once: the guard below already says `i < max`, and clamping is
    // what makes that also say the read is inside the buffer. Past the buffer
    // the old `else` answered true; the clamped guard answers true at the
    // same index, by the line above it.
    let max = max.min(buf.len());
    skip_space(buf, start, max);
    let i = *start;
    if i >= max {
        return true;
    }
    let Some(c) = at(buf, i) else { return true };

    if mode == b'[' {
        if c != b']' {
            return skip_array_scalars(buf, start, max);
        }
    } else if c != b'}' {
        return skip_object_scalars(buf, start, max);
    }
    true
}

/// `skipCollection`: the explicit stack that replaces recursion.
pub(crate) fn skip_collection(buf: &[u8], start: &mut usize, max: usize) -> Validity {
    // Clamped once: past the buffer every read answers `None`, which
    // is what the loop below breaks on anyway.
    let max = max.min(buf.len());
    let mut ret = Validity::Partial;
    let mut stack = [0u8; MAX_DEPTH];
    // `int16_t depth = -1`, as a count so there is no negative index: zero
    // is the C's -1, and level `d` is count `d + 1`. Carrying it as an
    // `Option` instead meant testing a discriminant before every use of it.
    let mut depth = 0usize;
    let mut i = *start;

    while i < max {
        let Some(c) = at(buf, i) else { break };
        i = i.saturating_add(1);

        match c {
            b'{' | b'[' => {
                // The count is the index of the slot about to be filled.
                let next = depth;
                if next >= MAX_DEPTH {
                    ret = Validity::MaxDepthExceeded;
                } else {
                    depth = next.saturating_add(1);
                    if let Some(slot) = stack.get_mut(next) {
                        *slot = c;
                    }
                    if !skip_scalars(buf, &mut i, max, c) {
                        ret = Validity::Illegal;
                    }
                }
            }
            b'}' | b']' => {
                let d = depth.saturating_sub(1);
                let open = stack.get(d).copied().unwrap_or(0);
                // The nested case: close one level and carry on.
                if depth > 1 && d < MAX_DEPTH && is_matching_bracket(open, c) {
                    let outer = d.saturating_sub(1);
                    depth = outer.saturating_add(1);
                    let mode = stack.get(outer).copied().unwrap_or(0);
                    if skip_space_and_comma(buf, &mut i, max) {
                        if !skip_scalars(buf, &mut i, max, mode) {
                            ret = Validity::Illegal;
                        }
                    } else if i < max && !at(buf, i).is_some_and(|n| is_matching_bracket(mode, n)) {
                        // After closing a nested collection with no comma, the
                        // only legal next byte is the outer collection's own
                        // close.
                        ret = Validity::Illegal;
                    }
                } else {
                    ret = if depth == 1 && is_matching_bracket(open, c) {
                        Validity::Valid
                    } else {
                        Validity::Illegal
                    };
                }
            }
            _ => ret = Validity::Illegal,
        }

        if ret != Validity::Partial {
            break;
        }
    }

    if ret == Validity::Valid {
        *start = i;
    }
    ret
}

/// `JSON_Validate`: is this a complete, valid JSON document?
///
/// Strict ECMA-404, as coreJSON is strict: no trailing commas, no leading
/// zeroes, no unescaped control characters in strings, no over-long UTF-8, no
/// lone surrogates, and nesting no deeper than [`MAX_DEPTH`].
///
/// A **scalar at the top level is valid** — `42` and `"hi"` are documents —
/// which is ECMA-404 and not the older RFC 4627 rule that a document must be
/// an object or an array. The C makes that switchable with
/// `JSON_VALIDATE_COLLECTIONS_ONLY`; this follows the default.
#[must_use]
pub fn validate(buf: &[u8]) -> Validity {
    let max = buf.len();
    if max == 0 {
        // `JSONBadParameter` for `max == 0`. There is no null pointer to
        // report here, which is the one status this port cannot produce.
        return Validity::BadParameter;
    }

    let mut i = 0usize;
    skip_space(buf, &mut i, max);

    // A document that opens a collection cannot be a scalar, so the scalar
    // attempt is one the dispatch could only answer false to. The compiler
    // cannot know that; the grammar does.
    let mut ret = if at(buf, i).is_some_and(is_open_bracket) {
        skip_collection(buf, &mut i, max)
    } else if skip_any_scalar(buf, &mut i, max) {
        Validity::Valid
    } else {
        skip_collection(buf, &mut i, max)
    };

    if ret == Validity::Valid && i < max {
        skip_space(buf, &mut i, max);
        if i != max {
            ret = Validity::Illegal;
        }
    }

    ret
}

/// Whether [`validate`] accepts this document.
///
/// The convenience the differential and the test suite both want: coreJSON
/// distinguishes four kinds of "no", and a corpus only asks "yes or no".
#[must_use]
pub fn is_valid(buf: &[u8]) -> bool {
    validate(buf) == Validity::Valid
}

#[cfg(test)]
mod tests {
    use super::*;

    /// The table IS the rule, for every byte there is.
    ///
    /// [`HEX`] is generated from [`hex_value`] in a `const` block, so this
    /// cannot fail while that stays true -- which is the point. It fails the
    /// moment somebody replaces the generator with a hand-typed table, and a
    /// hand-typed table accepting one byte it should not is the classic way
    /// to ship a parser that disagrees with its reference.
    #[test]
    fn the_hex_table_is_the_rule_for_every_one_of_the_256_bytes() {
        for c in 0..=u8::MAX {
            assert_eq!(
                hex_to_int(c),
                hex_value(c),
                "the table and the rule disagree about {c:#04x}"
            );
        }
    }

    /// Exactly 22 bytes are hex digits, and nothing else is.
    ///
    /// Stated as a count as well as a rule, because a count is the cheapest
    /// thing to get wrong by one.
    #[test]
    fn exactly_twenty_two_bytes_are_hex_digits() {
        let hex = (0..=u8::MAX).filter(|c| hex_to_int(*c) != NOT_A_HEX_CHAR);
        assert_eq!(hex.count(), 22, "10 digits plus a-f plus A-F");
    }

    /// **A gate gap this closes.** A non-hex byte inside a `\u` escape must be
    /// refused.
    ///
    /// JSONTestSuite has no file that distinguishes this: deliberately
    /// poisoning `hex_value` to accept `g` left all 318 files agreeing with
    /// coreJSON and every other test in the crate passing. The corpus is a
    /// conformance corpus, not a proof that every byte class is separated.
    #[test]
    fn a_non_hex_byte_in_an_escape_is_refused() {
        // Every position in the four digits, so none of them is unchecked.
        for bad in [
            br#"{"a":"\ug000"}"#.as_slice(),
            br#"{"a":"\u0g00"}"#.as_slice(),
            br#"{"a":"\u00g0"}"#.as_slice(),
            br#"{"a":"\u000g"}"#.as_slice(),
        ] {
            assert!(
                !is_valid(bad),
                "a non-hex digit was accepted: {}",
                core::str::from_utf8(bad).unwrap_or("<not utf8>")
            );
        }
        // And the same shape WITH four real digits is accepted, so the test
        // above is rejecting the digit and not the surrounding document.
        assert!(is_valid(br#"{"a":"\u0041"}"#.as_slice()));
    }
}