json_scanner 0.1.0

A simple JSON parser that reports positions
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
#![allow(clippy::unusual_byte_groupings)]
//! A simple JSON parser that reports positions.
//!
//! This is a pull-based, streaming parser. That means you create a
//! [`Parser`], and then repeatedly call [`Parser::next_event`] to
//! request the next [`Event`].
//!
//! # Example
//!
//! ```rust
//! use json_scanner::{Parser, SpannedEvent, Event};
//!
//! let mut parser = Parser::new(br#"{ "foo": "bar" }"#);
//! assert_eq!(
//!   parser.next_event().unwrap().unwrap(),
//!   SpannedEvent {
//!     start: 0,
//!     end: 1,
//!     event: Event::BeginObject,
//!   }
//! );
//!
//! assert_eq!(
//!   parser.next_event().unwrap().unwrap(),
//!   SpannedEvent {
//!     start: 2,
//!     end: 7,
//!     event: Event::String("foo".into()),
//!   }
//! );
//!
//! assert_eq!(
//!   parser.next_event().unwrap().unwrap(),
//!   SpannedEvent {
//!     start: 9,
//!     end: 14,
//!     event: Event::String("bar".into()),
//!   }
//! );
//!
//! assert_eq!(
//!   parser.next_event().unwrap().unwrap(),
//!   SpannedEvent {
//!     start: 15,
//!     end: 16,
//!     event: Event::EndObject,
//!   }
//! );
//! ```

use memchr::memchr;
use std::borrow::Cow;

/// All the different kinds of errors we can encounter when parsing JSON.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum ErrorKind {
    InvalidUtf8,
    NumberWithLeadingZero,
    UnterminatedString,
    UnescapedControl(u8),
    UnmatchedSurrogate,
    InvalidEscape(u8),
    InvalidHex(u8),
    ExpectedComma,
    ExpectedDigit,
    ExpectedFalse,
    ExpectedTrue,
    ExpectedNull,
    ExpectedNumber,
    ExpectedExponentStart,
    ExpectedValue,
    ExpectedString,
    ExpectedEos,
    ExpectedColon,
    ExpectedFirstObjectEntry,
    ExpectedNextObjectEntry,
    ExpectedFirstArrayEntry,
    ExpectedNextArrayEntry,
}

impl std::error::Error for ErrorKind {}

impl std::fmt::Display for ErrorKind {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let s = match self {
            ErrorKind::InvalidUtf8 => "invalid UTF-8 in string",
            ErrorKind::NumberWithLeadingZero => "numbers with a leading zero",
            ErrorKind::UnterminatedString => "unterminated string",
            ErrorKind::UnmatchedSurrogate => "unmatched UTF-16 surrogate",
            ErrorKind::ExpectedComma => "expected a comma (,)",
            ErrorKind::ExpectedDigit => "expected a digit",
            ErrorKind::ExpectedFalse => "expected `false`",
            ErrorKind::ExpectedTrue => "expected `true`",
            ErrorKind::ExpectedNull => "expected `null`",
            ErrorKind::ExpectedNumber => "expected a number",
            ErrorKind::ExpectedExponentStart => "expected an exponent",
            ErrorKind::ExpectedValue => {
                "expected an object, array, string, boolean, number, or null"
            }
            ErrorKind::ExpectedString => "expected a string",
            ErrorKind::ExpectedEos => "expected the end",
            ErrorKind::ExpectedColon => "expected a colon (:)",
            ErrorKind::ExpectedFirstObjectEntry => "expected a string or `}`",
            ErrorKind::ExpectedNextObjectEntry => "expected a string or `}`",
            ErrorKind::ExpectedFirstArrayEntry => "expected a value or ]",
            ErrorKind::ExpectedNextArrayEntry => "expected a value or ]",
            ErrorKind::InvalidHex(_) => "expected a hex digit",
            ErrorKind::UnescapedControl(c) => {
                return write!(f, "unescaped control character {c:x} in string");
            }
            ErrorKind::InvalidEscape(c) => {
                return write!(f, "invalid escape character {c:x} in string");
            }
        };
        s.fmt(f)
    }
}

/// A parse error and its location.
#[derive(Copy, Clone, Debug)]
pub struct ParseError {
    /// The byte offset in the input that caused the error.
    pub byte_offset: usize,
    /// The kind of error.
    pub kind: ErrorKind,
}

impl std::fmt::Display for ParseError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "parse error at byte {}: {}", self.byte_offset, self.kind)
    }
}

impl std::error::Error for ParseError {}

enum Container {
    Object,
    Array,
}

enum State {
    /// The parser has not yet consumed any input. The next non-whitespace
    /// character should be the beginning of a value.
    Init,
    /// We just started a new object. The next non-whitespace character
    /// should be `}` or the start of a key (i.e., a string).
    ObjectStart,
    /// We just read an object key. The next non-whitespace character
    /// should be `:`, followed by a value.
    ObjectAfterKey,
    /// We just read an object key/value pair. The next non-whitespace character
    /// should be `}`, or `,` followed by a value.
    ObjectAfterValue,
    /// We just started an array. The next non-whitespace character
    /// should be `]` or the beginning of a value.
    ArrayStart,
    /// We just read an array element. The next non-whitespace character
    /// should `]`, or `,` followed by a value.
    ArrayAfterValue,
    /// We're finished. If there are any more non-whitespace characters,
    /// it's an error.
    Done,
}

/// A simple JSON parser that delivers event streams in "pull" mode.
pub struct Parser<'input> {
    /// The current offset in the input, for reporting spans and errors.
    offset: usize,
    /// The remaining input left to process.
    ///
    /// The next byte to process is `input[0]`: we have advanced past `offset`
    /// bytes already.
    input: &'input [u8],
    /// The current state of the parser.
    state: State,
    /// The stack of "old" containers. The current container isn't
    /// on this stack, because it can be deduced from `state`.
    ///
    /// For example, consider the input
    /// ```json
    ///  { "foo": [1, 2] }`
    /// ^0 ^1      ^2
    /// ```
    ///
    /// At position `^0`, the state will be `Init` and the stack
    /// will be empty. At position `^1`, the state will be `ObjectStart`
    /// and the stack will be empty. At position `^2`, the state will
    /// be `ArrayStart` and the stack will have an object on it.
    container_stack: Vec<Container>,
}

/// The various different things that a JSON parser can encounter.
#[derive(Debug, PartialEq, Eq)]
pub enum Event<'input> {
    /// The parser has entered an object (i.e., it has encountered a `{`).
    ///
    /// The next event will either be `EndObject`, or else a `String` containing
    /// an object key.
    BeginObject,
    /// The parser has left an object (i.e., it has encountered a `}`).
    EndObject,
    /// The parser has entered an array (i.e., it has encountered a `[`).
    BeginArray,
    /// The parser has left an array (i.e., it has encountered a `]`).
    EndArray,
    /// The parser has encountered a number. The number has not been
    /// parsed.
    Number(&'input str),
    /// The parser has encountered a string.
    String(Cow<'input, str>),
    /// The parser has encountered a boolean.
    Boolean(bool),
    /// The parser has encountered a JSON `null` literal.
    Null,
}

/// A JSON parsing event emitted by [`Parser::next_event`], consisting of an
/// [`Event`] and its source location.
#[derive(Debug, PartialEq, Eq)]
pub struct SpannedEvent<'input> {
    pub start: usize,
    pub end: usize,
    pub event: Event<'input>,
}

/// Does JSON consider `c` an ASCII control character?
///
/// This is different from Rust's `u8::is_ascii_control`.
///
/// https://datatracker.ietf.org/doc/html/rfc8259#section-7
fn is_ascii_control(c: u8) -> bool {
    c <= 0x1F
}

/// See https://www.unicode.org/versions/Unicode17.0.0/core-spec/chapter-3/#G2630
fn is_high_surrogate(c: u16) -> bool {
    (0xD800..=0xDBFF).contains(&c)
}

/// See https://www.unicode.org/versions/Unicode17.0.0/core-spec/chapter-3/#G2630
fn is_low_surrogate(c: u16) -> bool {
    (0xDC00..=0xDFFF).contains(&c)
}

impl<'input> Parser<'input> {
    /// Create a new JSON parser for parsing `input`.
    pub fn new(input: &'input [u8]) -> Self {
        Parser {
            input,
            offset: 0,
            container_stack: Vec::new(),
            state: State::Init,
        }
    }

    /// Read the next event from the input.
    ///
    /// An `Err` return value indicates that parsing has failed. In that case, the
    /// parser should not be used anymore. A return value of `Ok(None)` indicates that
    /// there is nothing more to parse.
    pub fn next_event(&mut self) -> Result<Option<SpannedEvent<'input>>, ParseError> {
        self.skip_whitespace();

        let ev = match self.state {
            State::Init => self.expect_value(State::Done)?,
            State::ObjectStart => {
                let c = self.peek(ErrorKind::ExpectedFirstObjectEntry)?;
                if c == b'}' {
                    self.pop_container(Event::EndObject)
                } else {
                    let s = self.expect_string()?;
                    self.state = State::ObjectAfterKey;
                    s
                }
            }
            State::ObjectAfterKey => {
                self.expect_byte(b':', self.err(ErrorKind::ExpectedColon))?;
                self.skip_whitespace();
                self.expect_value(State::ObjectAfterValue)?
            }
            State::ObjectAfterValue => {
                let c = self.peek(ErrorKind::ExpectedNextObjectEntry)?;
                if c == b'}' {
                    self.pop_container(Event::EndObject)
                } else {
                    self.expect_byte(b',', self.err(ErrorKind::ExpectedComma))?;
                    self.skip_whitespace();
                    let k = self.expect_string()?;
                    self.state = State::ObjectAfterKey;
                    k
                }
            }
            State::ArrayStart => {
                let c = self.peek(ErrorKind::ExpectedFirstArrayEntry)?;
                if c == b']' {
                    self.pop_container(Event::EndArray)
                } else {
                    self.expect_value(State::ArrayAfterValue)?
                }
            }
            State::ArrayAfterValue => {
                let c = self.peek(ErrorKind::ExpectedNextArrayEntry)?;
                if c == b']' {
                    self.pop_container(Event::EndArray)
                } else {
                    self.expect_byte(b',', self.err(ErrorKind::ExpectedComma))?;
                    self.expect_value(State::ArrayAfterValue)?
                }
            }
            State::Done => {
                if self.peek(ErrorKind::ExpectedEos).is_ok() {
                    return Err(self.err(ErrorKind::ExpectedEos));
                } else {
                    return Ok(None);
                }
            }
        };

        Ok(Some(ev))
    }

    /// Handle the end of a container.
    ///
    /// The current character of input should be either `]` or `}`. We advance
    /// past that character, while updating the parser state and container stack.
    fn pop_container(&mut self, event: Event<'input>) -> SpannedEvent<'input> {
        #[cfg(debug_assertions)]
        {
            let c = self.peek(ErrorKind::ExpectedEos).unwrap();
            assert!(c == b'}' || c == b']');
        }
        self.state = match self.container_stack.pop() {
            Some(Container::Object) => State::ObjectAfterValue,
            Some(Container::Array) => State::ArrayAfterValue,
            None => State::Done,
        };
        self.advance(1);
        SpannedEvent {
            start: self.offset - 1,
            end: self.offset,
            event,
        }
    }

    /// Consume some literal bytes from the input, erroring if we don't get them.
    fn expect_literal(&mut self, literal: &[u8], error: ErrorKind) -> Result<(), ParseError> {
        if self.input.starts_with(literal) {
            self.advance(literal.len());
            Ok(())
        } else {
            Err(self.err(error))
        }
    }

    /// Consume a string and return it.
    fn expect_string(&mut self) -> Result<SpannedEvent<'input>, ParseError> {
        let start = self.offset;
        self.expect_byte(b'"', self.err(ErrorKind::ExpectedString))?;
        let s = self.parse_str_tail()?;
        let ev = SpannedEvent {
            start,
            end: self.offset,
            event: Event::String(s),
        };
        Ok(ev)
    }

    /// If we're in a container, push it onto the container stack.
    fn remember_container(&mut self) {
        match self.state {
            State::Init | State::Done => {}
            State::ObjectStart | State::ObjectAfterKey | State::ObjectAfterValue => {
                self.container_stack.push(Container::Object)
            }
            State::ArrayStart | State::ArrayAfterValue => {
                self.container_stack.push(Container::Array)
            }
        }
    }

    /// We expect that the next thing in the input is a JSON value.
    ///
    /// If it's a simple value, consume it and return it. If it's a container,
    /// consume just the opening character.
    fn expect_value(&mut self, next_state: State) -> Result<SpannedEvent<'input>, ParseError> {
        self.skip_whitespace();
        let c = self.peek(ErrorKind::ExpectedValue)?;

        let (ev, state) = match c {
            b'{' => {
                let ev = SpannedEvent {
                    start: self.offset,
                    end: self.offset + 1,
                    event: Event::BeginObject,
                };
                self.remember_container();
                self.advance(1);
                (ev, State::ObjectStart)
            }
            b'[' => {
                let ev = SpannedEvent {
                    start: self.offset,
                    end: self.offset + 1,
                    event: Event::BeginArray,
                };
                self.remember_container();
                self.advance(1);
                (ev, State::ArrayStart)
            }
            b'f' => {
                let ev = SpannedEvent {
                    start: self.offset,
                    end: self.offset + b"false".len(),
                    event: Event::Boolean(false),
                };
                self.expect_literal(b"false", ErrorKind::ExpectedFalse)?;
                (ev, next_state)
            }
            b't' => {
                let ev = SpannedEvent {
                    start: self.offset,
                    end: self.offset + b"true".len(),
                    event: Event::Boolean(true),
                };
                self.expect_literal(b"true", ErrorKind::ExpectedTrue)?;
                (ev, next_state)
            }
            b'n' => {
                let ev = SpannedEvent {
                    start: self.offset,
                    end: self.offset + b"null".len(),
                    event: Event::Null,
                };
                self.expect_literal(b"null", ErrorKind::ExpectedNull)?;
                (ev, next_state)
            }
            b'"' => (self.expect_string()?, next_state),
            b'-' | b'0'..=b'9' => {
                let start = self.offset;
                let n = self.consume_number()?;
                let ev = SpannedEvent {
                    start,
                    end: self.offset,
                    event: Event::Number(n),
                };
                (ev, next_state)
            }
            _ => {
                self.advance(1);
                return Err(self.err(ErrorKind::ExpectedValue));
            }
        };

        self.state = state;
        Ok(ev)
    }

    fn skip_whitespace(&mut self) {
        let not_ws = |c: u8| !(c == b' ' || c == b'\t' || c == b'\n' || c == b'\r');
        match self.input.iter().copied().position(not_ws) {
            Some(offset) => {
                self.advance(offset);
            }
            None => {
                self.advance(self.input.len());
            }
        }
    }

    /// Return the next input byte, or error with `expecting` if there are no more bytes.
    fn peek(&self, expecting: ErrorKind) -> Result<u8, ParseError> {
        self.input
            .first()
            .copied()
            .ok_or_else(|| self.err(expecting))
    }

    fn err(&self, kind: ErrorKind) -> ParseError {
        ParseError {
            // Typically, we consume -- as opposed to peeking at -- a character
            // of input before we detect that it caused an error. Therefore,
            // the most common error location is one byte ago.
            byte_offset: self.offset.saturating_sub(1),
            kind,
        }
    }

    fn advance(&mut self, count: usize) {
        self.offset += count;
        self.input = &self.input[count..];
    }

    /// Consume and return the next input byte, or error with `expecting` if there are
    /// no more bytes.
    fn next_byte(&mut self, expecting: ErrorKind) -> Result<u8, ParseError> {
        let b = self.peek(expecting)?;
        self.advance(1);
        Ok(b)
    }

    /// Consume the next byte, raising an error if it isn't `b`.
    fn expect_byte(&mut self, b: u8, err: ParseError) -> Result<(), ParseError> {
        if self.next_byte(err.kind).is_ok_and(|x| x == b) {
            Ok(())
        } else {
            Err(err)
        }
    }

    /// Consume the next byte of input, raising an error if it isn't a hex digit.
    ///
    /// Returns the numeric value of the hex digit, as a number between 0 and
    /// 15 inclusive.
    fn next_hex(&mut self, eos_error: ParseError) -> Result<u8, ParseError> {
        let c = self.next_byte(eos_error.kind).map_err(|_| eos_error)?;
        match c {
            b'0'..=b'9' => Ok(c - b'0'),
            b'a'..=b'f' => Ok(c - b'a' + 10),
            b'A'..=b'F' => Ok(c - b'A' + 10),
            _ => Err(self.err(ErrorKind::InvalidHex(c))),
        }
    }

    /// Read four hex keys from the input, and return the `u16` value that they specify.
    fn four_hex(&mut self, eos_error: ParseError) -> Result<u16, ParseError> {
        let mut code = 0;
        code += (self.next_hex(eos_error)? as u16) << 12;
        code += (self.next_hex(eos_error)? as u16) << 8;
        code += (self.next_hex(eos_error)? as u16) << 4;
        code += self.next_hex(eos_error)? as u16;
        Ok(code)
    }

    /// Parse a JSON string, not including its opening quotation mark, substituting any escape sequences.
    ///
    /// This is supposed to be correct, but not particularly optimized. There's
    /// a fast path for strings with no escapes, and this is the fallback.
    fn parse_str_tail_slow(&mut self, capacity: usize) -> Result<Cow<'input, str>, ParseError> {
        let mut s = Vec::with_capacity(capacity);
        let string_start_offset = self.offset.saturating_sub(1);
        let unterminated = ParseError {
            byte_offset: string_start_offset,
            kind: ErrorKind::UnterminatedString,
        };

        loop {
            let b = self
                .next_byte(ErrorKind::UnterminatedString)
                .map_err(|_| unterminated)?;

            if b == b'\\' {
                let b = self
                    .next_byte(ErrorKind::UnterminatedString)
                    .map_err(|_| unterminated)?;

                match b {
                    b'"' | b'\\' | b'/' => s.push(b),
                    b'n' => s.push(b'\n'),
                    b'r' => s.push(b'\r'),
                    b't' => s.push(b'\t'),
                    b'b' => s.push(0x08),
                    b'f' => s.push(0x0c),
                    b'u' => {
                        let hi = self.four_hex(unterminated)?;

                        let c = match char::from_u32(hi as u32) {
                            Some(c) => c,
                            None => {
                                let err = ParseError {
                                    byte_offset: self.offset,
                                    kind: ErrorKind::UnmatchedSurrogate,
                                };
                                self.expect_byte(b'\\', err)?;
                                self.expect_byte(b'u', err)?;

                                let lo = self.four_hex(err)?;

                                if !(is_high_surrogate(hi) && is_low_surrogate(lo)) {
                                    return Err(err);
                                }

                                let w = (hi & 0b000000_1111_000000) >> 6;
                                let x_hi = (hi & 0b000000_0000_111111) << 10;
                                let x_lo = lo & 0b000000_1111_111111;
                                let x = (x_hi | x_lo) as u32;
                                let code = (((w + 1) as u32) << 16) | x;
                                char::from_u32(code).unwrap()
                            }
                        };
                        let mut buf = [0u8; 4];
                        c.encode_utf8(&mut buf);
                        s.extend_from_slice(&buf[..c.len_utf8()]);
                    }
                    _ => {
                        return Err(self.err(ErrorKind::InvalidEscape(b)));
                    }
                };
            } else if b == b'"' {
                let s = String::from_utf8(s).map_err(|e| ParseError {
                    byte_offset: string_start_offset + 1 + e.utf8_error().valid_up_to(),
                    kind: ErrorKind::InvalidUtf8,
                })?;
                return Ok(s.into());
            } else if b <= 0x1F {
                // We don't check b.is_ascii_control(), because JSON has different rules.
                return Err(self.err(ErrorKind::UnescapedControl(b)));
            } else {
                s.push(b);
            }
        }
    }

    /// Parse a JSON string, not including its opening quotation mark.
    fn parse_str_tail(&mut self) -> Result<Cow<'input, str>, ParseError> {
        let Some(close) = memchr(b'"', self.input) else {
            return Err(self.err(ErrorKind::UnterminatedString));
        };

        if close == 0 {
            self.advance(1);
            Ok(Cow::Borrowed(""))
        } else if memchr(b'\\', &self.input[..close]).is_none() {
            if let Some(idx) = self.input[..close]
                .iter()
                .copied()
                .position(is_ascii_control)
            {
                return Err(ParseError {
                    byte_offset: self.offset + idx,
                    kind: ErrorKind::UnescapedControl(self.input[idx]),
                });
            }
            let s = std::str::from_utf8(&self.input[..close]).map_err(|e| ParseError {
                byte_offset: self.offset + e.valid_up_to(),
                kind: ErrorKind::InvalidUtf8,
            })?;
            self.advance(close + 1);
            Ok(Cow::Borrowed(s))
        } else {
            self.parse_str_tail_slow(close)
        }
    }

    /// Consume a JSON number without parsing it.
    ///
    /// This advances the input past the number and then returns the unparsed number.
    fn consume_number(&mut self) -> Result<&'input str, ParseError> {
        let mut number_state = NumberParser {
            input: self.input,
            offset: 0,
        };
        let n = number_state.consume().map_err(|mut e| {
            e.byte_offset += self.offset;
            e
        })?;
        self.advance(n.len());
        // unwrap: a JSON number contains only ASCII characters. So if we haven't
        // errored yet, it's valid UTF-8.
        Ok(str::from_utf8(n).unwrap())
    }
}

/// A parser for JSON numbers.
struct NumberParser<'input> {
    /// The input data we're parsing. Unlike the input data
    /// in [`Parser`], this one does not advance: the next byte
    /// to be read is at `self.input[self.offset]`.
    input: &'input [u8],
    /// Our current offset in `input`.
    offset: usize,
}

impl<'input> NumberParser<'input> {
    fn err(&self, kind: ErrorKind) -> ParseError {
        ParseError {
            byte_offset: self.offset.saturating_sub(1),
            kind,
        }
    }

    /// Advance the input past all digits.
    fn skip_digits(&mut self) {
        match self.input[self.offset..]
            .iter()
            .position(|c| !c.is_ascii_digit())
        {
            Some(offset) => self.offset += offset,
            None => self.offset = self.input.len(),
        }
    }

    /// Advance the input past all digits, raising an error if there
    /// aren't any.
    fn skip_digits_at_least_one(&mut self) -> Result<(), ParseError> {
        let prev_offset = self.offset;
        self.skip_digits();
        if self.offset == prev_offset {
            Err(ParseError {
                byte_offset: self.offset,
                kind: ErrorKind::ExpectedDigit,
            })
        } else {
            Ok(())
        }
    }

    /// Returns the part of the input that was consumed so far.
    fn number_so_far(&self) -> &'input [u8] {
        &self.input[..self.offset]
    }

    /// Returns the next byte if there is one.
    fn next_byte(&mut self) -> Option<u8> {
        if self.offset < self.input.len() {
            let c = self.input[self.offset];
            self.offset += 1;
            Some(c)
        } else {
            None
        }
    }

    /// Consumes and returns the next byte, or errors with `expecting` if there are no more.
    fn next_byte_or(&mut self, expecting: ErrorKind) -> Result<u8, ParseError> {
        if self.offset < self.input.len() {
            let c = self.input[self.offset];
            self.offset += 1;
            Ok(c)
        } else {
            Err(ParseError {
                byte_offset: self.input.len(),
                kind: expecting,
            })
        }
    }

    /// Try to parse the beginning of the input as a number.
    ///
    /// If successful, returns the part of the input that could
    /// be recognized as a number.
    fn consume(&mut self) -> Result<&'input [u8], ParseError> {
        let mut c = self.next_byte_or(ErrorKind::ExpectedNumber)?;
        if c == b'-' {
            c = self.next_byte_or(ErrorKind::ExpectedDigit)?;
        } else if !c.is_ascii_digit() {
            return Err(self.err(ErrorKind::ExpectedNumber));
        }

        if c == b'0' {
            let Some(next) = self.next_byte() else {
                return Ok(self.number_so_far());
            };
            c = next;
            if c.is_ascii_digit() {
                return Err(self.err(ErrorKind::NumberWithLeadingZero));
            }
        } else if (b'1'..=b'9').contains(&c) {
            self.skip_digits();
            let Some(next) = self.next_byte() else {
                return Ok(self.number_so_far());
            };
            c = next;
        } else {
            return Err(self.err(ErrorKind::ExpectedDigit));
        }

        if c == b'.' {
            self.skip_digits_at_least_one()?;
            let Some(next) = self.next_byte() else {
                return Ok(self.number_so_far());
            };
            c = next;
        }

        if c == b'e' || c == b'E' {
            c = self.next_byte_or(ErrorKind::ExpectedExponentStart)?;
            if c == b'+' || c == b'-' {
                c = self.next_byte_or(ErrorKind::ExpectedDigit)?;
            }
            if !c.is_ascii_digit() {
                return Err(self.err(ErrorKind::ExpectedDigit));
            }
            self.skip_digits();

            // All the other branches consume one byte past the end of the
            // number, so make sure to match that.
            if self.next_byte().is_none() {
                return Ok(self.number_so_far());
            };
        }
        debug_assert!(self.offset > 0);
        Ok(&self.input[..self.offset - 1])
    }
}

#[cfg(test)]
mod tests {
    use crate::{ErrorKind, Event, NumberParser, Parser};

    fn parse_all(input: &[u8]) -> Vec<Event<'_>> {
        let mut parser = Parser::new(input);
        let mut ret = Vec::new();
        while let Some(ev) = parser.next_event().unwrap() {
            ret.push(ev.event);
        }
        ret
    }

    #[test]
    fn string() {
        let successes = [
            (r#""foo""#, "foo", 5),
            (r#""foo" trailing"#, "foo", 5),
            (r#""foo\n""#, "foo\n", 7),
            (r#""foo\n\n""#, "foo\n\n", 9),
            (r#""foo\u000a""#, "foo\n", 11),
            (r#""\uD834\uDD1E""#, "𝄞", 14),
        ];

        let failures: [(&[u8], _, _); _] = [
            (br#""foo"#, ErrorKind::UnterminatedString, 0),
            (br#""foo\"#, ErrorKind::UnterminatedString, 0),
            (br#""foo\u000""#, ErrorKind::InvalidHex(b'"'), 9),
            (br#""foo\x""#, ErrorKind::InvalidEscape(b'x'), 5),
            (b"\"foo\n\"", ErrorKind::UnescapedControl(b'\n'), 4),
            (b"\"foo\xff\xff\"", ErrorKind::InvalidUtf8, 4),
        ];

        for (input, expected, position) in successes {
            dbg!(input);
            let mut parser = Parser::new(input.as_bytes());
            parser.advance(1);

            let result = parser.parse_str_tail().unwrap();
            assert_eq!(&result, expected);
            assert_eq!(parser.offset, position);

            let mut parser = Parser::new(input.as_bytes());
            parser.advance(1);
            let result = parser.parse_str_tail_slow(0).unwrap();
            assert_eq!(&result, expected);
            assert_eq!(parser.offset, position);
        }

        for (input, expected, position) in failures {
            dbg!(input);

            let mut parser = Parser::new(input);
            parser.advance(1);
            let result = parser.parse_str_tail().unwrap_err();
            assert_eq!(result.kind, expected);
            assert_eq!(result.byte_offset, position);

            let mut parser = Parser::new(input);
            parser.advance(1);
            let result = parser.parse_str_tail_slow(0).unwrap_err();
            assert_eq!(result.kind, expected);
            assert_eq!(result.byte_offset, position);
        }
    }

    #[test]
    fn number() {
        let successes = [
            ("0", "0"),
            ("0.1", "0.1"),
            ("0,", "0"),
            ("-1", "-1"),
            ("1e-3", "1e-3"),
            ("1e-3,", "1e-3"),
            ("1e3", "1e3"),
            ("1e33", "1e33"),
            ("1.0e33", "1.0e33"),
        ];

        let failures = [
            ("01", ErrorKind::NumberWithLeadingZero, 1),
            ("+1", ErrorKind::ExpectedNumber, 0),
            ("--1", ErrorKind::ExpectedDigit, 1),
            ("0.", ErrorKind::ExpectedDigit, 2),
            ("0.,", ErrorKind::ExpectedDigit, 2),
            ("1e-", ErrorKind::ExpectedDigit, 3),
        ];

        for (input, expected) in successes {
            dbg!(input);
            let mut np = NumberParser {
                input: input.as_bytes(),
                offset: 0,
            };
            let n = np.consume().unwrap();
            let n = str::from_utf8(n).unwrap();

            assert_eq!(n, expected);
        }

        for (input, expected, position) in failures {
            dbg!(input);
            let mut np = NumberParser {
                input: input.as_bytes(),
                offset: 0,
            };
            let e = np.consume().unwrap_err();

            assert_eq!(e.kind, expected);
            assert_eq!(e.byte_offset, position);
        }
    }

    #[test]
    fn basic() {
        let successes: [(&str, &[Event<'_>]); _] = [
            ("[]", &[Event::BeginArray, Event::EndArray]),
            ("[] ", &[Event::BeginArray, Event::EndArray]),
            (" [ ] ", &[Event::BeginArray, Event::EndArray]),
            (
                " [ 1, 2 ] ",
                &[
                    Event::BeginArray,
                    Event::Number("1"),
                    Event::Number("2"),
                    Event::EndArray,
                ],
            ),
            (
                r#" [ 1, { "foo": "bar" } ] "#,
                &[
                    Event::BeginArray,
                    Event::Number("1"),
                    Event::BeginObject,
                    Event::String("foo".into()),
                    Event::String("bar".into()),
                    Event::EndObject,
                    Event::EndArray,
                ],
            ),
        ];

        for (input, expected) in successes {
            dbg!(input);

            let result = parse_all(input.as_bytes());
            assert_eq!(&result, expected);
        }
    }
}