jsonx-lib 0.5.2

Strict RFC 8259 JSON: a borrowing parser, a pull reader, and a deterministic writer, with budgets checked before allocation and no recursion. No dependencies, no_std.
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
//! The strict tokenizer: one string, number, or literal token at a byte
//! offset, with the exact offset of the first byte that breaks it.
//!
//! Every function here works on the raw bytes of a `&str` and reports the
//! first byte at which the input stops being the beginning of a valid token,
//! so an error points at the mistake rather than at the token that holds it.
//! Running off the end of the input is always [`ErrorKind::UnexpectedEnd`] at
//! the input's length: the text so far could still have been valid.

use alloc::string::String;

use crate::{
    error::{Error, ErrorKind},
    scan,
};

/// A scanned string token.
#[derive(Clone, Copy, Debug)]
pub(crate) struct StrToken {
    /// The byte just past the closing quote.
    pub(crate) end: usize,
    /// Whether the body holds an escape sequence (and so must be decoded).
    pub(crate) escaped: bool,
}

#[inline]
const fn err<T>(kind: ErrorKind, offset: usize) -> Result<T, Error> {
    Err(Error::new(kind, offset))
}

/// Where a string scan stopped, so it can continue when more input arrives:
/// every byte before `next` has been checked, and `next` is not inside an
/// escape sequence.
#[derive(Clone, Copy, Debug)]
pub(crate) struct StrScan {
    /// The first byte not yet checked.
    pub(crate) next: usize,
    /// Whether an escape sequence was seen before `next`.
    pub(crate) escaped: bool,
}

impl StrScan {
    /// A scan of the string whose opening quote is at `quote`, from its
    /// first body byte.
    #[inline]
    pub(crate) const fn start(quote: usize) -> Self {
        Self {
            next: quote + 1,
            escaped: false,
        }
    }
}

/// Scans the string whose opening quote is at `quote`, validating every escape
/// and refusing a body longer than `max` source bytes.
///
/// Nothing is decoded or allocated here: a string with escapes is validated in
/// this pass and decoded by [`decode`] in a second, so the decoder can size its
/// buffer exactly, and only after the length budget has passed.
#[inline]
pub(crate) fn scan_string(bytes: &[u8], quote: usize, max: usize) -> Result<StrToken, Error> {
    resume_string(bytes, quote, &mut StrScan::start(quote), max)
}

/// [`scan_string`] from where `scan` stopped. When the input runs out
/// ([`ErrorKind::UnexpectedEnd`]), `scan` records how far the string was
/// checked, so a later call over a longer input picks up there: a string fed
/// in pieces is scanned once, plus at most the eleven bytes of an escape cut
/// in half.
#[inline(always)]
pub(crate) fn resume_string(
    bytes: &[u8],
    quote: usize,
    scan: &mut StrScan,
    max: usize,
) -> Result<StrToken, Error> {
    let len = bytes.len();
    let body = quote + 1;
    // The closing quote may sit at `body + max` at the latest; nothing past
    // that needs looking at before the budget refuses the string.
    let cap = body.saturating_add(max).saturating_add(1).min(len);
    let mut i = scan.next;
    loop {
        i = scan::find_read_special(bytes, i, cap);
        scan.next = i;
        let byte = if i < cap { bytes.get(i).copied() } else { None };
        let Some(b) = byte else {
            return if i - body > max {
                err(ErrorKind::StringTooLong, quote)
            } else {
                err(ErrorKind::UnexpectedEnd, len)
            };
        };
        match b {
            b'"' => {
                return Ok(StrToken {
                    end: i + 1,
                    escaped: scan.escaped,
                });
            }
            b'\\' => {
                i = scan_escape(bytes, i)?;
                scan.next = i;
                scan.escaped = true;
                if i - body > max {
                    return err(ErrorKind::StringTooLong, quote);
                }
            }
            // `find_read_special` stops only at a quote, a backslash, or a
            // control character.
            _ => return err(ErrorKind::ControlCharacter, i),
        }
    }
}

/// Whether `b` may follow a backslash as a one-character escape.
#[inline]
const fn is_short_escape(b: u8) -> bool {
    matches!(b, b'"' | b'\\' | b'/' | b'b' | b'f' | b'n' | b'r' | b't')
}

/// Validates the escape whose backslash is at `at` and returns the offset just
/// past it. A high surrogate must be followed at once by a `\u` escape naming
/// a low one; the pair is one escape here.
fn scan_escape(bytes: &[u8], at: usize) -> Result<usize, Error> {
    let len = bytes.len();
    match bytes.get(at + 1) {
        None => err(ErrorKind::UnexpectedEnd, len),
        Some(&b) if is_short_escape(b) => Ok(at + 2),
        Some(b'u') => {
            let unit = hex4(bytes, at + 2)?;
            match unit {
                0xD800..=0xDBFF => {
                    let next = at + 6;
                    match bytes.get(next) {
                        None => return err(ErrorKind::UnexpectedEnd, len),
                        Some(b'\\') => {}
                        Some(_) => return err(ErrorKind::LoneSurrogate, at),
                    }
                    match bytes.get(next + 1) {
                        None => err(ErrorKind::UnexpectedEnd, len),
                        Some(b'u') => {
                            let low = hex4(bytes, next + 2)?;
                            if (0xDC00..=0xDFFF).contains(&low) {
                                Ok(next + 6)
                            } else {
                                err(ErrorKind::LoneSurrogate, at)
                            }
                        }
                        // A well-formed escape that is not the low half.
                        Some(&b) if is_short_escape(b) => err(ErrorKind::LoneSurrogate, at),
                        Some(_) => err(ErrorKind::InvalidEscape, next + 1),
                    }
                }
                0xDC00..=0xDFFF => err(ErrorKind::LoneSurrogate, at),
                _ => Ok(at + 6),
            }
        }
        Some(_) => err(ErrorKind::InvalidEscape, at + 1),
    }
}

/// Reads the four hex digits of a `\u` escape starting at `at`.
fn hex4(bytes: &[u8], at: usize) -> Result<u32, Error> {
    let mut value = 0;
    for k in 0..4 {
        let Some(&b) = bytes.get(at + k) else {
            return err(ErrorKind::UnexpectedEnd, bytes.len());
        };
        let Some(digit) = scan::hex_value(b) else {
            return err(ErrorKind::InvalidUnicodeEscape, at + k);
        };
        value = (value << 4) | digit;
    }
    Ok(value)
}

/// Reads four hex digits already validated by [`scan_string`].
fn hex4_valid(bytes: &[u8], at: usize) -> u32 {
    let mut value = 0;
    for k in 0..4 {
        let digit = bytes
            .get(at + k)
            .and_then(|&b| scan::hex_value(b))
            .unwrap_or(0);
        value = (value << 4) | digit;
    }
    value
}

/// Decodes the body of a string that [`scan_string`] accepted.
///
/// The output never needs more room than the body: every escape is at least as
/// long as the UTF-8 it stands for (`\n` is two bytes for one, `\uXXXX` six
/// for at most three, a surrogate pair twelve for four), so the one allocation
/// is sized exactly up front and never grows.
pub(crate) fn decode(body: &str) -> String {
    let bytes = body.as_bytes();
    let mut out = String::with_capacity(body.len());
    let mut from = 0;
    loop {
        // In a validated body the only byte `find_read_special` can stop at is
        // a backslash: quotes and control characters cannot appear unescaped.
        let at = scan::find_read_special(bytes, from, bytes.len());
        out.push_str(body.get(from..at).unwrap_or(""));
        let Some(&kind) = bytes.get(at + 1) else {
            return out;
        };
        let (ch, width) = match kind {
            b'b' => ('\u{8}', 2),
            b'f' => ('\u{c}', 2),
            b'n' => ('\n', 2),
            b'r' => ('\r', 2),
            b't' => ('\t', 2),
            b'u' => {
                let unit = hex4_valid(bytes, at + 2);
                if (0xD800..=0xDBFF).contains(&unit) {
                    let low = hex4_valid(bytes, at + 8);
                    let code =
                        0x10000 + ((unit - 0xD800) << 10) + (low.wrapping_sub(0xDC00) & 0x3FF);
                    (
                        char::from_u32(code).unwrap_or(char::REPLACEMENT_CHARACTER),
                        12,
                    )
                } else {
                    (
                        char::from_u32(unit).unwrap_or(char::REPLACEMENT_CHARACTER),
                        6,
                    )
                }
            }
            // `"`, `\`, and `/` stand for themselves.
            other => (char::from(other), 2),
        };
        out.push(ch);
        from = at + width;
    }
}

/// Where a number scan stopped: the first byte not yet looked at, and the
/// grammar state reached before it.
#[derive(Clone, Copy, Debug)]
pub(crate) struct NumScan {
    /// The first byte not yet checked.
    next: usize,
    state: NumState,
}

impl NumScan {
    /// A scan of the number whose first byte is at `start`.
    #[inline]
    pub(crate) const fn start(start: usize) -> Self {
        Self {
            next: start,
            state: NumState::Start,
        }
    }

    /// Moves the scan's position `by` bytes towards the start of the input.
    #[inline]
    pub(crate) const fn rebase(&mut self, by: usize) {
        self.next -= by;
    }
}

/// The states of the RFC 8259 number grammar,
/// `-? (0 | [1-9][0-9]*) (. [0-9]+)? ([eE] [+-]? [0-9]+)?`.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum NumState {
    /// Nothing read yet.
    Start,
    /// After `-`: a digit must follow.
    Minus,
    /// A leading `0`: a whole number, but no digit may follow it.
    Zero,
    /// In the integer digits.
    Int,
    /// After `.`: a digit must follow.
    Dot,
    /// In the fraction digits.
    Frac,
    /// After `e` or `E`: a sign or a digit must follow.
    Exp,
    /// After the exponent's sign: a digit must follow.
    ExpSign,
    /// In the exponent digits.
    ExpDigits,
}

impl NumState {
    /// Whether the text so far is a whole number.
    #[inline]
    const fn is_complete(self) -> bool {
        matches!(self, Self::Zero | Self::Int | Self::Frac | Self::ExpDigits)
    }
}

/// Scans the number starting at `start` (a `-` or a digit) and returns the
/// offset just past it. A number longer than `max` bytes is refused as
/// [`ErrorKind::NumberTooLong`] at `start`, whatever follows it.
#[cfg(test)]
pub(crate) fn scan_number(bytes: &[u8], start: usize, max: usize) -> Result<usize, Error> {
    resume_number(bytes, start, &mut NumScan::start(start), true, max)
}

/// [`scan_number`] from where `scan` stopped. With `complete`, the end of
/// `bytes` is the end of the input, so a number that reaches it ends there.
/// Without it, a number that reaches the end may still go on: the scan stops
/// with [`ErrorKind::UnexpectedEnd`] and `scan` set to continue, so a number
/// fed in pieces is scanned once.
pub(crate) fn resume_number(
    bytes: &[u8],
    start: usize,
    scan: &mut NumScan,
    complete: bool,
    max: usize,
) -> Result<usize, Error> {
    match number_text(bytes, scan, complete) {
        Ok(end) if end - start > max => err(ErrorKind::NumberTooLong, start),
        Err(e) if e.offset() - start > max => err(ErrorKind::NumberTooLong, start),
        other => other,
    }
}

/// The end of the integer starting at `start` (`-?(0|[1-9][0-9]*)`), when
/// the byte after it ends the number: the common case, decided without the
/// grammar's state machine. `None` for anything else (a fraction, an
/// exponent, a leading zero followed by a digit, the end of the text, or no
/// number at all), which [`resume_number`] then reads, with its errors.
#[inline(always)]
pub(crate) fn quick_integer(bytes: &[u8], start: usize) -> Option<usize> {
    let mut i = start;
    if bytes.get(i) == Some(&b'-') {
        i += 1;
    }
    match bytes.get(i)? {
        b'0' => i += 1,
        b'1'..=b'9' => {
            i += 1;
            while bytes.get(i).is_some_and(u8::is_ascii_digit) {
                i += 1;
            }
        }
        _ => return None,
    }
    match bytes.get(i)? {
        b'.' | b'e' | b'E' | b'0'..=b'9' => None,
        _ => Some(i),
    }
}

/// The number grammar as a resumable state machine; see [`NumState`].
fn number_text(bytes: &[u8], scan: &mut NumScan, complete: bool) -> Result<usize, Error> {
    let mut i = scan.next;
    let mut state = scan.state;
    loop {
        let Some(&b) = bytes.get(i) else {
            scan.next = i;
            scan.state = state;
            return if complete && state.is_complete() {
                Ok(i)
            } else {
                err(ErrorKind::UnexpectedEnd, bytes.len())
            };
        };
        state = match (state, b) {
            (NumState::Start, b'-') => NumState::Minus,
            (NumState::Start | NumState::Minus, b'0') => NumState::Zero,
            (NumState::Start | NumState::Minus, b'1'..=b'9') => NumState::Int,
            // A leading zero stands alone: `01` and `-00` are not numbers.
            (NumState::Zero, b'0'..=b'9') => return err(ErrorKind::InvalidNumber, i),
            (NumState::Int | NumState::Frac | NumState::ExpDigits, b'0'..=b'9') => {
                // The common case: a run of digits.
                i += 1;
                while bytes.get(i).is_some_and(u8::is_ascii_digit) {
                    i += 1;
                }
                continue;
            }
            (NumState::Zero | NumState::Int, b'.') => NumState::Dot,
            (NumState::Dot, b'0'..=b'9') => NumState::Frac,
            (NumState::Zero | NumState::Int | NumState::Frac, b'e' | b'E') => NumState::Exp,
            (NumState::Exp, b'+' | b'-') => NumState::ExpSign,
            (NumState::Exp | NumState::ExpSign, b'0'..=b'9') => NumState::ExpDigits,
            // The number stops where the grammar does; the caller judges
            // what follows.
            (complete_so_far, _) if complete_so_far.is_complete() => return Ok(i),
            _ => return err(ErrorKind::InvalidNumber, i),
        };
        i += 1;
    }
}

/// Matches the rest of the literal `word` (`true`, `false`, or `null`), whose
/// first byte is at `at` and already known to match.
pub(crate) fn scan_literal(bytes: &[u8], at: usize, word: &[u8]) -> Result<usize, Error> {
    for (k, &expected) in word.iter().enumerate().skip(1) {
        match bytes.get(at + k) {
            None => return err(ErrorKind::UnexpectedEnd, bytes.len()),
            Some(&b) if b == expected => {}
            Some(_) => return err(ErrorKind::InvalidLiteral, at + k),
        }
    }
    Ok(at + word.len())
}

/// Whether `text` is exactly one JSON number, with nothing before or after.
pub(crate) fn is_number(text: &str) -> bool {
    let bytes = text.as_bytes();
    matches!(bytes.first(), Some(b'-' | b'0'..=b'9'))
        && number_text(bytes, &mut NumScan::start(0), true).is_ok_and(|end| end == bytes.len())
}

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

    fn string(src: &str) -> Result<StrToken, Error> {
        scan_string(src.as_bytes(), 0, usize::MAX)
    }

    fn kind_at(r: Result<impl core::fmt::Debug, Error>) -> (ErrorKind, usize) {
        let e = r.err().map(|e| (e.kind(), e.offset()));
        e.unwrap_or((ErrorKind::UnexpectedEnd, usize::MAX))
    }

    #[test]
    fn test_quick_integer_agrees_with_the_grammar_where_it_answers() {
        let cases = [
            "0,",
            "-0]",
            "7 ",
            "42}",
            "-12345678901234567890,",
            "01,",
            "-01 ",
            "1.5,",
            "1e3 ",
            "2E1]",
            "-,",
            "- 1",
            "0",
            "123",
            "-",
            "x",
            "9.",
            "0x1,",
            "00",
        ];
        for case in cases {
            let bytes = case.as_bytes();
            if let Some(end) = quick_integer(bytes, 0) {
                assert_eq!(scan_number(bytes, 0, usize::MAX).ok(), Some(end), "{case}");
            }
        }
        assert_eq!(quick_integer(b"42,", 0), Some(2));
        assert_eq!(quick_integer(b"[-7]", 1), Some(3));
        assert_eq!(quick_integer(b"42", 0), None); // the end of the text decides
        assert_eq!(quick_integer(b"01,", 0), None);
        assert_eq!(quick_integer(b"1.5,", 0), None);
    }

    #[test]
    fn test_scan_string_plain_and_escaped() {
        let t = string(r#""abc" tail"#).ok();
        assert_eq!(t.map(|t| (t.end, t.escaped)), Some((5, false)));
        let t = string(r#""a\nb""#).ok();
        assert_eq!(t.map(|t| (t.end, t.escaped)), Some((6, true)));
        let t = string(r#""\ud83d\ude00""#).ok();
        assert_eq!(t.map(|t| t.end), Some(14));
    }

    #[test]
    fn test_scan_string_errors_point_at_the_offending_byte() {
        assert_eq!(kind_at(string("\"abc")), (ErrorKind::UnexpectedEnd, 4));
        assert_eq!(
            kind_at(string("\"a\u{1}\"")),
            (ErrorKind::ControlCharacter, 2)
        );
        assert_eq!(kind_at(string(r#""\x""#)), (ErrorKind::InvalidEscape, 2));
        assert_eq!(
            kind_at(string(r#""\u12g4""#)),
            (ErrorKind::InvalidUnicodeEscape, 5)
        );
        assert_eq!(kind_at(string(r#""\u12"#)), (ErrorKind::UnexpectedEnd, 5));
        assert_eq!(
            kind_at(string(r#""a\udc00""#)),
            (ErrorKind::LoneSurrogate, 2)
        );
        assert_eq!(
            kind_at(string(r#""\ud800""#)),
            (ErrorKind::LoneSurrogate, 1)
        );
        assert_eq!(
            kind_at(string(r#""\ud800\n""#)),
            (ErrorKind::LoneSurrogate, 1)
        );
        assert_eq!(
            kind_at(string(r#""\ud800\u0041""#)),
            (ErrorKind::LoneSurrogate, 1)
        );
        assert_eq!(
            kind_at(string(r#""\ud800\q""#)),
            (ErrorKind::InvalidEscape, 8)
        );
        assert_eq!(
            kind_at(string(r#""\ud800\"#)),
            (ErrorKind::UnexpectedEnd, 8)
        );
        assert_eq!(kind_at(string(r#""\ud800"#)), (ErrorKind::UnexpectedEnd, 7));
    }

    #[test]
    fn test_scan_string_budget_boundaries() {
        let b = br#""abcd""#;
        assert!(scan_string(b, 0, 4).is_ok());
        assert_eq!(kind_at(scan_string(b, 0, 3)), (ErrorKind::StringTooLong, 0));
        assert_eq!(
            kind_at(scan_string(br#""ab\n""#, 0, 3)),
            (ErrorKind::StringTooLong, 0)
        );
        assert!(scan_string(br#""ab\n""#, 0, 4).is_ok());
        assert!(scan_string(br#""""#, 0, 0).is_ok());
        // Cut short inside the budget: the input simply ended.
        assert_eq!(
            kind_at(scan_string(b"\"ab", 0, 4)),
            (ErrorKind::UnexpectedEnd, 3)
        );
    }

    #[test]
    fn test_decode_every_escape() {
        assert_eq!(
            decode(r#"a\"b\\c\/d\be\ff\ng\rh\ti"#),
            "a\"b\\c/d\u{8}e\u{c}f\ng\rh\ti"
        );
        assert_eq!(decode(r"\u0041\u00e9\u20ac"), "A\u{e9}\u{20ac}");
        assert_eq!(decode(r"x\ud83d\ude00y"), "x\u{1f600}y");
        assert_eq!(decode("plain"), "plain");
    }

    #[test]
    fn test_scan_number_grammar() {
        let num = |s: &str| scan_number(s.as_bytes(), 0, usize::MAX);
        for ok in [
            "0", "-0", "1", "-12", "1.5", "0.0", "1e5", "1E+5", "1e-05", "-0.5e10",
        ] {
            assert_eq!(num(ok).ok(), Some(ok.len()), "{ok}");
        }
        assert_eq!(kind_at(num("01")), (ErrorKind::InvalidNumber, 1));
        assert_eq!(kind_at(num("-01")), (ErrorKind::InvalidNumber, 2));
        assert_eq!(kind_at(num("-")), (ErrorKind::UnexpectedEnd, 1));
        assert_eq!(kind_at(num("-a")), (ErrorKind::InvalidNumber, 1));
        assert_eq!(kind_at(num("1.")), (ErrorKind::UnexpectedEnd, 2));
        assert_eq!(kind_at(num("1.e")), (ErrorKind::InvalidNumber, 2));
        assert_eq!(kind_at(num("1e")), (ErrorKind::UnexpectedEnd, 2));
        assert_eq!(kind_at(num("1e+")), (ErrorKind::UnexpectedEnd, 3));
        assert_eq!(kind_at(num("1e+x")), (ErrorKind::InvalidNumber, 3));
        // The number stops where the grammar does; the caller judges what follows.
        assert_eq!(num("1.5.2").ok(), Some(3));
        assert_eq!(num("12abc").ok(), Some(2));
    }

    #[test]
    fn test_scan_number_budget() {
        assert_eq!(scan_number(b"-12", 0, 3).ok(), Some(3));
        assert_eq!(
            kind_at(scan_number(b"-123", 0, 3)),
            (ErrorKind::NumberTooLong, 0)
        );
        // Too long wins over a later syntax error.
        assert_eq!(
            kind_at(scan_number(b"1234.x", 0, 3)),
            (ErrorKind::NumberTooLong, 0)
        );
        assert_eq!(
            kind_at(scan_number(b"12.x", 0, 3)),
            (ErrorKind::InvalidNumber, 3)
        );
    }

    #[test]
    fn test_scan_literal() {
        assert_eq!(scan_literal(b"true", 0, b"true").ok(), Some(4));
        assert_eq!(
            kind_at(scan_literal(b"trux", 0, b"true")),
            (ErrorKind::InvalidLiteral, 3)
        );
        assert_eq!(
            kind_at(scan_literal(b"nul", 0, b"null")),
            (ErrorKind::UnexpectedEnd, 3)
        );
    }

    #[test]
    fn test_resume_string_at_every_split_matches_one_scan() {
        let src = br#""ab\n\u00e9\ud83d\ude00cd" tail"#;
        let whole = scan_string(src, 0, usize::MAX)
            .ok()
            .map(|t| (t.end, t.escaped));
        assert_eq!(whole, Some((26, true)));
        for cut in 1..src.len() {
            let mut scan = StrScan::start(0);
            let head = src.get(..cut).unwrap_or_default();
            match resume_string(head, 0, &mut scan, usize::MAX) {
                Ok(t) => assert_eq!(Some((t.end, t.escaped)), whole),
                Err(e) => {
                    assert_eq!((e.kind(), e.offset()), (ErrorKind::UnexpectedEnd, cut));
                    // The scan never stops inside an escape, and never past the cut.
                    assert!(scan.next <= cut);
                    assert_ne!(src.get(scan.next.saturating_sub(1)), Some(&b'\\'));
                    let rest = resume_string(src, 0, &mut scan, usize::MAX);
                    assert_eq!(rest.ok().map(|t| (t.end, t.escaped)), whole, "cut {cut}");
                }
            }
        }
    }

    #[test]
    fn test_resume_string_keeps_budget_and_errors() {
        let mut scan = StrScan::start(0);
        assert!(resume_string(b"\"abc", 0, &mut scan, 3).is_err());
        assert_eq!(
            kind_at(resume_string(b"\"abcd\"", 0, &mut scan, 3)),
            (ErrorKind::StringTooLong, 0)
        );
        let mut scan = StrScan::start(0);
        assert!(resume_string(b"\"a\\", 0, &mut scan, 9).is_err());
        assert_eq!(
            kind_at(resume_string(b"\"a\\q\"", 0, &mut scan, 9)),
            (ErrorKind::InvalidEscape, 3)
        );
    }

    #[test]
    fn test_resume_number_at_every_split_matches_one_scan() {
        for text in ["-12.5e+30,", "0,", "-0.0e0]", "7 ", "123456789}", "1E-2,"] {
            let src = text.as_bytes();
            let whole = scan_number(src, 0, usize::MAX).ok();
            for cut in 1..src.len() {
                let mut scan = NumScan::start(0);
                let head = src.get(..cut).unwrap_or_default();
                match resume_number(head, 0, &mut scan, false, usize::MAX) {
                    Ok(end) => assert_eq!(Some(end), whole, "{text} cut {cut}"),
                    Err(e) => {
                        assert_eq!((e.kind(), e.offset()), (ErrorKind::UnexpectedEnd, cut));
                        let rest = resume_number(src, 0, &mut scan, false, usize::MAX);
                        assert_eq!(rest.ok(), whole, "{text} cut {cut}");
                    }
                }
            }
            // Complete input: a number at the very end ends there.
            let bare = src.get(..src.len() - 1).unwrap_or_default();
            assert_eq!(scan_number(bare, 0, usize::MAX).ok(), whole);
        }
        // Partial input: an invalid byte is still refused at once.
        let mut scan = NumScan::start(0);
        assert_eq!(
            kind_at(resume_number(b"01", 0, &mut scan, false, 9)),
            (ErrorKind::InvalidNumber, 1)
        );
        // And the budget is enforced on the part seen so far.
        let mut scan = NumScan::start(0);
        assert_eq!(
            kind_at(resume_number(b"1234", 0, &mut scan, false, 3)),
            (ErrorKind::NumberTooLong, 0)
        );
        let mut scan = NumScan::start(4);
        scan.rebase(4);
        assert_eq!(resume_number(b"5 ", 0, &mut scan, false, 3).ok(), Some(1));
    }

    #[test]
    fn test_is_number() {
        assert!(is_number("-1.5e3"));
        assert!(!is_number(""));
        assert!(!is_number("1 "));
        assert!(!is_number("+1"));
        assert!(!is_number("01"));
    }
}