utopia-php-std 0.1.0

PHP engine semantics (the value model, juggling and comparison, string, number, JSON, serialize, igbinary, sorting, filter and URL functions) for crates that must behave exactly like PHP.
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
//! `yaml_parse()` (ext-yaml 2.3 over libyaml 0.2.5) with the default ini
//! settings: timestamps, `!!binary` and `!php/object` stay strings.
//!
//! Documents are split into events by libyaml itself (`unsafe-libyaml`, the
//! C library translated to Rust), so exactly the same inputs are accepted and
//! refused; the events are turned into PHP values as ext-yaml's `parse.c`
//! and `detect.c` do: YAML 1.1 booleans (`yes`, `off`, `y`), integers in
//! binary, octal (`0755`), hexadecimal, sexagesimal (`1:20`) and with `_` or
//! `,` separators, floats whose exponent has a sign, merge keys (`<<`), and
//! keys converted like array offsets.
//!
//! PHP: anchors are PHP references. An alias of a node that is still being
//! built (a recursive structure) has no value here: it is refused as an
//! unknown alias.
//!
//! Nesting deeper than [`MAX_DEPTH`] refuses the document, as the other
//! codecs in this crate do. ext-yaml has no limit, and walking an unbounded
//! document recurses until the stack overflows, which aborts the process.

use std::collections::HashMap;
use std::mem::MaybeUninit;

use crate::format::{dval_to_lval, str_to_float, str_to_int};
use crate::{Array, ArrayKey, Str, Value};

const NULL_TAG: &[u8] = b"tag:yaml.org,2002:null";
const BOOL_TAG: &[u8] = b"tag:yaml.org,2002:bool";
const INT_TAG: &[u8] = b"tag:yaml.org,2002:int";
const FLOAT_TAG: &[u8] = b"tag:yaml.org,2002:float";
const TIMESTAMP_TAG: &[u8] = b"tag:yaml.org,2002:timestamp";
const MERGE_TAG: &[u8] = b"tag:yaml.org,2002:merge";
const NONSPECIFIC_TAG: &[u8] = b"!";

/// The deepest nesting of mappings and sequences [`parse`] reads. A deeper
/// document is refused (`None`) instead of recursing until the stack
/// overflows. Real configuration files stay far below it. At this depth a
/// release build needs under 768 KB of stack, well inside a 2 MB coroutine.
pub const MAX_DEPTH: usize = 512;

/// A parser event, copied out of libyaml.
#[derive(Debug, Clone, PartialEq)]
enum Event {
    StreamStart,
    StreamEnd,
    DocumentStart,
    DocumentEnd,
    Alias(Vec<u8>),
    Scalar(Scalar),
    SequenceStart(Option<Vec<u8>>),
    SequenceEnd,
    MappingStart(Option<Vec<u8>>),
    MappingEnd,
    /// No event: what libyaml returns after an error or the end of the stream.
    Nothing,
}

#[derive(Debug, Clone, PartialEq)]
struct Scalar {
    anchor: Option<Vec<u8>>,
    tag: Option<Vec<u8>>,
    value: Vec<u8>,
    plain_implicit: bool,
    quoted_implicit: bool,
    /// Plain (or any) style: not quoted, literal or folded.
    plain: bool,
}

impl Scalar {
    fn tag_is(&self, tag: &[u8]) -> bool {
        self.tag.as_deref() == Some(tag)
    }

    fn not_implicit_and_tag_is(&self, tag: &[u8]) -> bool {
        !self.quoted_implicit && !self.plain_implicit && self.tag_is(tag)
    }

    fn not_quoted_or_tag_is(&self, tag: &[u8]) -> bool {
        self.plain && (self.plain_implicit || self.tag_is(tag))
    }
}

/// libyaml's parser over one input.
struct Parser<'a> {
    parser: Box<MaybeUninit<unsafe_libyaml::yaml_parser_t>>,
    _input: &'a [u8],
}

/// A C string of libyaml, copied.
///
/// # Safety
///
/// `p` is null or points at a NUL-terminated string.
unsafe fn c_bytes(p: *const u8) -> Option<Vec<u8>> {
    if p.is_null() {
        return None;
    }
    // SAFETY: the caller guarantees a NUL-terminated string.
    let s = unsafe { std::ffi::CStr::from_ptr(p.cast()) };
    Some(s.to_bytes().to_vec())
}

impl<'a> Parser<'a> {
    fn new(input: &'a [u8]) -> Option<Self> {
        let mut parser = Box::new(MaybeUninit::<unsafe_libyaml::yaml_parser_t>::uninit());
        // SAFETY: `yaml_parser_initialize` initialises the parser it is given;
        // the input outlives the parser (`_input` borrows it).
        unsafe {
            if unsafe_libyaml::yaml_parser_initialize(parser.as_mut_ptr()).fail {
                return None;
            }
            unsafe_libyaml::yaml_parser_set_input_string(parser.as_mut_ptr(), input.as_ptr(), input.len() as _);
        }
        Some(Self { parser, _input: input })
    }

    /// The next event, or `None` on a reader, scanner or parser error.
    fn next(&mut self) -> Option<Event> {
        let mut raw = MaybeUninit::<unsafe_libyaml::yaml_event_t>::uninit();
        // SAFETY: the parser was initialised in `new`; on success the event is
        // initialised, read according to its type, then deleted once.
        unsafe {
            if unsafe_libyaml::yaml_parser_parse(self.parser.as_mut_ptr(), raw.as_mut_ptr()).fail {
                return None;
            }
            let event = raw.as_mut_ptr();
            let converted = match (*event).type_ {
                unsafe_libyaml::YAML_STREAM_START_EVENT => Event::StreamStart,
                unsafe_libyaml::YAML_STREAM_END_EVENT => Event::StreamEnd,
                unsafe_libyaml::YAML_DOCUMENT_START_EVENT => Event::DocumentStart,
                unsafe_libyaml::YAML_DOCUMENT_END_EVENT => Event::DocumentEnd,
                unsafe_libyaml::YAML_ALIAS_EVENT => {
                    Event::Alias(c_bytes((*event).data.alias.anchor).unwrap_or_default())
                }
                unsafe_libyaml::YAML_SCALAR_EVENT => {
                    let s = (*event).data.scalar;
                    let value = if s.value.is_null() {
                        Vec::new()
                    } else {
                        std::slice::from_raw_parts(s.value, s.length as usize).to_vec()
                    };
                    Event::Scalar(Scalar {
                        anchor: c_bytes(s.anchor),
                        tag: c_bytes(s.tag),
                        value,
                        plain_implicit: s.plain_implicit,
                        quoted_implicit: s.quoted_implicit,
                        plain: matches!(
                            s.style,
                            unsafe_libyaml::YAML_PLAIN_SCALAR_STYLE | unsafe_libyaml::YAML_ANY_SCALAR_STYLE
                        ),
                    })
                }
                unsafe_libyaml::YAML_SEQUENCE_START_EVENT => {
                    Event::SequenceStart(c_bytes((*event).data.sequence_start.anchor))
                }
                unsafe_libyaml::YAML_SEQUENCE_END_EVENT => Event::SequenceEnd,
                unsafe_libyaml::YAML_MAPPING_START_EVENT => {
                    Event::MappingStart(c_bytes((*event).data.mapping_start.anchor))
                }
                unsafe_libyaml::YAML_MAPPING_END_EVENT => Event::MappingEnd,
                _ => Event::Nothing,
            };
            unsafe_libyaml::yaml_event_delete(event);
            Some(converted)
        }
    }
}

impl Drop for Parser<'_> {
    fn drop(&mut self) {
        // SAFETY: the parser was initialised in `new` and is deleted once.
        unsafe { unsafe_libyaml::yaml_parser_delete(self.parser.as_mut_ptr()) }
    }
}

/// A node as ext-yaml builds it: its value, whether PHP holds it as a
/// reference (it has an anchor or is an alias), and which of its direct
/// children are references (a merge key merges only those).
struct Node {
    value: Value,
    is_ref: bool,
    child_refs: Vec<ArrayKey>,
}

impl Node {
    fn plain(value: Value) -> Self {
        Node { value, is_ref: false, child_refs: Vec::new() }
    }
}

struct State<'a> {
    parser: Parser<'a>,
    /// The last event read; `None` after an error.
    event: Option<Event>,
    aliases: HashMap<Vec<u8>, Value>,
    /// Mappings and sequences currently open.
    depth: usize,
    /// Set when a node would nest deeper than [`MAX_DEPTH`]. A mapping
    /// returns what it has so far when a value fails, so the refusal is
    /// recorded here and checked once the document is read.
    too_deep: bool,
}

impl State<'_> {
    fn next(&mut self) -> bool {
        self.event = self.parser.next();
        self.event.is_some()
    }

    /// `get_next_element()`: the next node, `None` at a halting event or
    /// on failure.
    fn element(&mut self) -> Option<Node> {
        if !self.next() {
            return None;
        }
        match self.event.clone()? {
            Event::DocumentEnd | Event::MappingEnd | Event::SequenceEnd | Event::StreamEnd => None,
            Event::DocumentStart => self.document(),
            Event::MappingStart(anchor) => self.nested(|state| state.mapping(anchor)),
            Event::SequenceStart(anchor) => self.nested(|state| state.sequence(anchor)),
            Event::Scalar(scalar) => {
                let value = eval_scalar(&scalar);
                if let Some(anchor) = scalar.anchor {
                    self.aliases.insert(anchor, value.clone());
                    return Some(Node { value, is_ref: true, child_refs: Vec::new() });
                }
                Some(Node::plain(value))
            }
            Event::Alias(anchor) => {
                let value = self.aliases.get(&anchor)?.clone();
                Some(Node { value, is_ref: true, child_refs: Vec::new() })
            }
            Event::StreamStart | Event::Nothing => None,
        }
    }

    /// Reads a mapping or sequence one level deeper, or refuses it past
    /// [`MAX_DEPTH`].
    fn nested(&mut self, read: impl FnOnce(&mut Self) -> Option<Node>) -> Option<Node> {
        if self.depth >= MAX_DEPTH {
            self.too_deep = true;
            return None;
        }
        self.depth += 1;
        let node = read(self);
        self.depth -= 1;
        node
    }

    fn document(&mut self) -> Option<Node> {
        self.aliases.clear();
        self.depth = 0;
        self.too_deep = false;
        let node = self.element();
        if self.too_deep {
            return None;
        }
        // The end of the document must follow; an error reading it is
        // ignored, as ext-yaml does.
        if self.next() && self.event != Some(Event::DocumentEnd) {
            return None;
        }
        node
    }

    fn mapping(&mut self, anchor: Option<Vec<u8>>) -> Option<Node> {
        let mut array = Array::default();
        let mut refs: Vec<ArrayKey> = Vec::new();
        while let Some(key) = self.element() {
            let key_event = self.event.clone();
            // ext-yaml 2.3 returns what it has so far when a value fails
            // (an unknown alias, an error), without reading the mapping's end.
            let Some(value) = self.element() else {
                return Some(self.anchored(anchor, Value::Array(array), refs));
            };
            let merge_key = matches!(&key_event, Some(Event::Scalar(s)) if s.not_quoted_or_tag_is(MERGE_TAG))
                && matches!(&key.value, Value::Str(s) if s.as_bytes() == b"<<");
            if merge_key && matches!(value.value, Value::Array(_)) {
                let Value::Array(source) = &value.value else { unreachable!() };
                if matches!(self.event, Some(Event::Alias(_))) {
                    merge(&mut array, source);
                } else {
                    for (k, v) in source.iter() {
                        if let Value::Array(inner) = v
                            && value.child_refs.iter().any(|r| r.as_ref() == k)
                        {
                            merge(&mut array, inner);
                        }
                    }
                }
                continue;
            }
            let offset = match &key.value {
                Value::Float(f) => ArrayKey::Int(dval_to_lval(*f)),
                Value::Bool(b) => ArrayKey::Int(i64::from(*b)),
                Value::Int(i) => ArrayKey::Int(*i),
                Value::Null => ArrayKey::Str(Str::default()),
                Value::Str(s) => ArrayKey::normalize(s.clone()),
                // Illegal offset type: the pair is dropped (PHP warns).
                _ => continue,
            };
            refs.retain(|r| *r != offset);
            if value.is_ref {
                refs.push(offset.clone());
            }
            array.set(offset, value.value);
        }
        if self.event != Some(Event::MappingEnd) {
            return None;
        }
        Some(self.anchored(anchor, Value::Array(array), refs))
    }

    fn sequence(&mut self, anchor: Option<Vec<u8>>) -> Option<Node> {
        let mut array = Array::default();
        let mut refs = Vec::new();
        while let Some(node) = self.element() {
            if node.is_ref {
                refs.push(ArrayKey::Int(array.len() as i64));
            }
            let _ = array.push(node.value);
        }
        if self.event != Some(Event::SequenceEnd) {
            return None;
        }
        Some(self.anchored(anchor, Value::Array(array), refs))
    }

    fn anchored(&mut self, anchor: Option<Vec<u8>>, value: Value, child_refs: Vec<ArrayKey>) -> Node {
        match anchor {
            Some(anchor) => {
                self.aliases.insert(anchor, value.clone());
                Node { value, is_ref: true, child_refs }
            }
            None => Node { value, is_ref: false, child_refs },
        }
    }
}

/// `zend_hash_merge($target, $source, overwrite: false)`: adds the entries
/// whose key `target` lacks.
fn merge(target: &mut Array, source: &Array) {
    for (k, v) in source.iter() {
        let key = k.to_owned();
        if !target.contains_key(&key) {
            target.set(key, v.clone());
        }
    }
}

/// `yaml_parse($input)`: the first document of `input` as a PHP value, or
/// `None` where PHP returns `false` (and warns): a malformed document or an
/// unknown alias, or nesting deeper than [`MAX_DEPTH`]. An input without a
/// document is `null`.
///
/// PHP: `yaml_parse($input)` with `yaml.decode_timestamp`, `decode_binary`
/// and `decode_php` off (their defaults). Only the first document is read:
/// errors after it are not reported.
///
/// ```
/// use php_std::{Value, yaml};
///
/// let Some(Value::Array(config)) = yaml::parse(b"enabled: yes\nmode: 0755\nratio: 1.5\n") else { panic!() };
/// assert_eq!(config.get_str("enabled"), Some(&Value::Bool(true)));
/// assert_eq!(config.get_str("mode"), Some(&Value::Int(493)));
/// assert_eq!(config.get_str("ratio"), Some(&Value::Float(1.5)));
/// assert_eq!(yaml::parse(b"a: *missing"), None);
/// ```
///
/// ```php
/// $config = yaml_parse("enabled: yes\nmode: 0755\nratio: 1.5\n");
/// var_dump($config['enabled'], $config['mode'], $config['ratio']); // bool(true) int(493) float(1.5)
/// var_dump(@yaml_parse('a: *missing')); // bool(false)
/// ```
pub fn parse(input: &[u8]) -> Option<Value> {
    let parser = Parser::new(input)?;
    let mut state = State { parser, event: None, aliases: HashMap::new(), depth: 0, too_deep: false };
    loop {
        if !state.next() {
            return None;
        }
        match state.event {
            Some(Event::StreamEnd) => return Some(Value::Null),
            Some(Event::DocumentStart) => return state.document().map(|n| n.value),
            Some(Event::Nothing) | None => return None,
            _ => {}
        }
    }
}

/// `eval_scalar()`: a scalar's PHP value.
fn eval_scalar(s: &Scalar) -> Value {
    let value = &s.value;
    let string = || Value::Str(Str::copy_from(value));
    if s.tag_is(NONSPECIFIC_TAG) || s.quoted_implicit {
        return string();
    }
    if is_null(s) {
        return Value::Null;
    }
    if let Some(b) = is_bool(s) {
        return Value::Bool(b);
    }
    if s.plain_implicit || s.tag_is(INT_TAG) || s.tag_is(FLOAT_TAG) {
        match numeric(value) {
            Some(Number::Int(i)) if !s.plain_implicit && s.tag_is(FLOAT_TAG) => return Value::Float(i as f64),
            Some(Number::Float(f)) if !s.plain_implicit && s.tag_is(INT_TAG) => {
                return Value::Int(dval_to_lval(f));
            }
            Some(Number::Int(i)) => return Value::Int(i),
            Some(Number::Float(f)) => return Value::Float(f),
            None if s.not_implicit_and_tag_is(FLOAT_TAG) => return Value::Float(str_to_float(value)),
            None if s.not_implicit_and_tag_is(INT_TAG) => return Value::Int(str_to_int(value)),
            None => {}
        }
    }
    // Timestamps, binary and PHP objects stay strings with the default ini.
    let _ = s.not_implicit_and_tag_is(TIMESTAMP_TAG);
    string()
}

/// `scalar_is_null()`.
fn is_null(s: &Scalar) -> bool {
    if s.quoted_implicit {
        return false;
    }
    if s.plain_implicit {
        let v = s.value.as_slice();
        return v.is_empty() || v == b"~" || v == b"NULL" || v == b"Null" || v == b"null";
    }
    s.tag_is(NULL_TAG)
}

/// `scalar_is_bool()`: `Some(bool)`, or `None` when the scalar is not one.
fn is_bool(s: &Scalar) -> Option<bool> {
    let v = s.value.as_slice();
    if s.not_quoted_or_tag_is(BOOL_TAG) {
        const TRUE: &[&[u8]] = &[b"Y", b"y", b"YES", b"Yes", b"yes", b"TRUE", b"True", b"true", b"ON", b"On", b"on"];
        const FALSE: &[&[u8]] =
            &[b"N", b"n", b"NO", b"No", b"no", b"FALSE", b"False", b"false", b"OFF", b"Off", b"off"];
        if TRUE.contains(&v) {
            return Some(true);
        }
        if FALSE.contains(&v) {
            return Some(false);
        }
        return None;
    }
    if s.not_implicit_and_tag_is(BOOL_TAG) {
        return Some(!(v.is_empty() || v == b"0"));
    }
    None
}

enum Number {
    Int(i64),
    Float(f64),
}

/// C's `strtol(s, NULL, base)` as musl implements it: optional whitespace,
/// sign and (base 16) `0x` prefix; saturates on overflow.
fn strtol(s: &[u8], base: u32) -> i64 {
    let mut i = 0;
    while i < s.len() && (s[i] == b' ' || (b'\t'..=b'\r').contains(&s[i])) {
        i += 1;
    }
    let negative = s.get(i) == Some(&b'-');
    if matches!(s.get(i), Some(b'-' | b'+')) {
        i += 1;
    }
    if base == 16 && s.get(i) == Some(&b'0') && matches!(s.get(i + 1), Some(b'x' | b'X')) {
        i += 2;
    }
    let mut n: i128 = 0;
    while let Some(d) = s.get(i).and_then(|&c| (c as char).to_digit(base)) {
        n = (n * i128::from(base) + i128::from(d)).min(i128::from(u64::MAX) + 1);
        i += 1;
    }
    let n = if negative { -n } else { n };
    n.clamp(i128::from(i64::MIN), i128::from(i64::MAX)) as i64
}

/// C's `atol` as musl implements it: optional whitespace and sign, then
/// digits accumulated with wrapping.
fn atol(s: &[u8]) -> i64 {
    let mut i = 0;
    while i < s.len() && (s[i] == b' ' || (b'\t'..=b'\r').contains(&s[i])) {
        i += 1;
    }
    let negative = s.get(i) == Some(&b'-');
    if matches!(s.get(i), Some(b'-' | b'+')) {
        i += 1;
    }
    let mut n: i64 = 0;
    while let Some(&c) = s.get(i).filter(|c| c.is_ascii_digit()) {
        n = n.wrapping_mul(10).wrapping_sub(i64::from(c - b'0'));
        i += 1;
    }
    if negative { n } else { n.wrapping_neg() }
}

/// `zend_strtod` of the leading decimal number.
fn strtod(s: &[u8]) -> f64 {
    str_to_float(s)
}

fn sexagesimal_l(buf: &[u8]) -> i64 {
    let mut n: i64 = 0;
    for part in buf.split(|c| !c.is_ascii_digit()).filter(|p| !p.is_empty()) {
        n = n.wrapping_mul(60).wrapping_add(strtol(part, 10));
    }
    n
}

fn sexagesimal_d(buf: &[u8]) -> f64 {
    let mut d = 0.0;
    let mut i = 0;
    loop {
        while i < buf.len() && buf[i] != b'.' && !buf[i].is_ascii_digit() {
            i += 1;
        }
        if i >= buf.len() || buf[i] == b'.' {
            return d;
        }
        d = d * 60.0 + strtod(&buf[i..]);
        while i < buf.len() && buf[i].is_ascii_digit() {
            i += 1;
        }
    }
}

/// `scalar_is_numeric()`: the number a plain scalar spells, if any.
fn numeric(input: &[u8]) -> Option<Number> {
    let mut v = input;
    while let Some((&last, rest)) = v.split_last() {
        if last == b' ' || last == b'\t' {
            v = rest;
        } else {
            break;
        }
    }
    while let Some((&first, rest)) = v.split_first() {
        if first == b' ' || first == b'\t' {
            v = rest;
        } else {
            break;
        }
    }
    if v.is_empty() || v == b"." {
        return None;
    }
    if v == b".NAN" || v == b".NaN" || v == b".nan" {
        return Some(Number::Float(f64::NAN));
    }
    let negative = v[0] == b'-';
    if v[0] == b'+' || v[0] == b'-' {
        v = &v[1..];
    }
    if v.is_empty() {
        return None;
    }
    if v == b".INF" || v == b".Inf" || v == b".inf" {
        return Some(Number::Float(if negative { f64::NEG_INFINITY } else { f64::INFINITY }));
    }
    // Bytes past the end read as the NUL terminator, as in C.
    let at = |i: usize| v.get(i).copied().unwrap_or(0);
    let end = v.len();
    let mut buf: Vec<u8> = Vec::with_capacity(end + 3);
    if negative {
        buf.push(b'-');
    }
    let mut i = 0;

    #[derive(PartialEq)]
    enum Kind {
        Binary,
        Octal,
        Hex,
        Decimal,
        SexaInt,
        SexaFloat,
        Float,
    }

    let kind;
    // Where to continue: sexagesimal or float checks.
    let mut goto_sexa = false;
    let mut goto_float = false;
    if at(0) == b'0' {
        buf.push(b'0');
        i = 1;
        if i == end {
            return Some(Number::Int(0));
        }
        match at(i) {
            b'b' => {
                buf.push(b'b');
                i += 1;
                if i == end {
                    return None;
                }
                while i < end && (at(i) == b'_' || at(i) == b'0') {
                    i += 1;
                }
                if i == end {
                    return Some(Number::Int(0));
                }
                while i < end {
                    match at(i) {
                        b'_' => i += 1,
                        c @ (b'0' | b'1') => {
                            buf.push(c);
                            i += 1;
                        }
                        _ => return None,
                    }
                }
                kind = Kind::Binary;
            }
            b'x' => {
                buf.push(b'x');
                i += 1;
                if i == end {
                    return None;
                }
                while i < end && (at(i) == b'_' || at(i) == b'0') {
                    i += 1;
                }
                if i == end {
                    return Some(Number::Int(0));
                }
                while i < end {
                    match at(i) {
                        b'_' => i += 1,
                        c if c.is_ascii_hexdigit() => {
                            buf.push(c);
                            i += 1;
                        }
                        _ => return None,
                    }
                }
                kind = Kind::Hex;
            }
            c if c == b'_' || (b'0'..=b'7').contains(&c) => {
                while i < end {
                    match at(i) {
                        b'_' => i += 1,
                        c @ b'0'..=b'7' => {
                            buf.push(c);
                            i += 1;
                        }
                        _ => return None,
                    }
                }
                kind = Kind::Octal;
            }
            b'.' => {
                goto_float = true;
                kind = Kind::Float;
            }
            b':' => {
                goto_sexa = true;
                kind = Kind::SexaInt;
            }
            _ => return None,
        }
    } else if (b'1'..=b'9').contains(&at(0)) {
        buf.push(at(0));
        i = 1;
        let mut k = Kind::Decimal;
        while i < end {
            match at(i) {
                b'_' | b',' => i += 1,
                c if c.is_ascii_digit() => {
                    buf.push(c);
                    i += 1;
                }
                b':' => {
                    goto_sexa = true;
                    k = Kind::SexaInt;
                    break;
                }
                b'.' => {
                    goto_float = true;
                    k = Kind::Float;
                    break;
                }
                _ => return None,
            }
        }
        kind = k;
    } else if at(0) == b':' {
        goto_sexa = true;
        kind = Kind::SexaInt;
    } else if at(0) == b'.' {
        buf.push(b'0');
        goto_float = true;
        kind = Kind::Float;
    } else {
        return None;
    }

    let mut kind = kind;
    if goto_sexa {
        let mut float = false;
        while i < end {
            if at(i) == b'.' {
                float = true;
                break;
            }
            if at(i) != b':' {
                return None;
            }
            buf.push(at(i));
            i += 1;
            if at(i + 1) == b':' || at(i + 1) == b'.' || i + 1 == end {
                if at(i).is_ascii_digit() {
                    buf.push(at(i));
                    i += 1;
                } else {
                    return None;
                }
            } else if (b'0'..=b'5').contains(&at(i)) && at(i + 1).is_ascii_digit() {
                buf.push(at(i));
                buf.push(at(i + 1));
                i += 2;
            } else {
                return None;
            }
        }
        if float || at(i) == b'.' {
            kind = Kind::SexaFloat;
            goto_float = true;
        } else if i == end {
            kind = Kind::SexaInt;
        } else {
            return None;
        }
    }
    if goto_float {
        buf.push(at(i));
        i += 1;
        if kind == Kind::SexaFloat {
            let mut e = end;
            while i < e && (v[e - 1] == b'_' || v[e - 1] == b'0') {
                e -= 1;
            }
            if i == e {
                buf.push(b'0');
            }
            while i < e {
                match at(i) {
                    b'_' => i += 1,
                    c if c.is_ascii_digit() => {
                        buf.push(c);
                        i += 1;
                    }
                    _ => return None,
                }
            }
        } else {
            while i < end {
                match at(i) {
                    b'_' => i += 1,
                    c if c.is_ascii_digit() => {
                        buf.push(c);
                        i += 1;
                    }
                    b'E' | b'e' => {
                        buf.push(at(i));
                        i += 1;
                        if i == end || (at(i) != b'+' && at(i) != b'-') {
                            return None;
                        }
                        buf.push(at(i));
                        i += 1;
                        if i == end || !at(i).is_ascii_digit() || (at(i) == b'0' && i + 1 == end) {
                            return None;
                        }
                        buf.push(at(i));
                        i += 1;
                        while i < end {
                            if at(i).is_ascii_digit() {
                                buf.push(at(i));
                                i += 1;
                            } else {
                                return None;
                            }
                        }
                    }
                    _ => return None,
                }
            }
            kind = Kind::Float;
        }
    }

    Some(match kind {
        Kind::Binary => {
            let digits = &buf[if negative { 3 } else { 2 }..];
            let n = strtol(digits, 2);
            Number::Int(if negative { n.wrapping_neg() } else { n })
        }
        Kind::Octal => Number::Int(strtol(&buf, 8)),
        Kind::Hex => Number::Int(strtol(&buf, 16)),
        Kind::SexaInt => {
            let n = sexagesimal_l(&buf);
            Number::Int(if negative { n.wrapping_neg() } else { n })
        }
        Kind::SexaFloat => {
            let d = sexagesimal_d(&buf);
            Number::Float(if negative { -d } else { d })
        }
        Kind::Decimal => Number::Int(atol(&buf)),
        Kind::Float => Number::Float(strtod(&buf)),
    })
}

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

    fn int(s: &str) -> Option<i64> {
        match numeric(s.as_bytes()) {
            Some(Number::Int(i)) => Some(i),
            _ => None,
        }
    }

    #[test]
    fn numbers() {
        assert_eq!(int("99999999999999999999"), Some(7766279631452241919));
        assert_eq!(int("0x7FFFFFFFFFFFFFFFFF"), Some(i64::MAX));
        assert_eq!(int("1,000"), Some(1000));
        assert_eq!(int("1:20:30"), Some(4830));
        assert_eq!(int("-0b11"), Some(-3));
        assert_eq!(int("0o17"), None);
        assert_eq!(int("08"), None);
        assert!(matches!(numeric(b"190:20:30.15"), Some(Number::Float(f)) if (f - 685230.15).abs() < 1e-9));
        assert!(numeric(b"1e3").is_none());
    }

    /// The stack a coroutine gets, which reading up to [`MAX_DEPTH`] must fit.
    const STACK_BUDGET: usize = 2 * 1024 * 1024;

    fn nested_sequences(depth: usize) -> Vec<u8> {
        [vec![b'['; depth], vec![b']'; depth]].concat()
    }

    #[test]
    fn nesting_up_to_the_limit_is_read() {
        let Some(Value::Array(mut array)) = parse(&nested_sequences(MAX_DEPTH)) else { panic!() };
        for _ in 1..MAX_DEPTH {
            let Some(Value::Array(inner)) = array.get(&ArrayKey::Int(0)).cloned() else { panic!() };
            array = inner;
        }
        assert_eq!(array.len(), 0);

        // Mappings take the deepest frames. The limit has to fit the 2 MB a
        // coroutine gets: a release build needs under 768 KB at this depth and
        // a debug build, which the tests run, under 1.5 MB.
        let flow = [b"{a: ".repeat(MAX_DEPTH), b"1".to_vec(), b"}".repeat(MAX_DEPTH)].concat();
        let handle = std::thread::Builder::new().stack_size(STACK_BUDGET).spawn(move || parse(&flow)).unwrap();
        assert!(matches!(handle.join().unwrap(), Some(Value::Array(_))));
    }

    #[test]
    fn nesting_past_the_limit_is_refused() {
        assert_eq!(parse(&nested_sequences(MAX_DEPTH + 1)), None);
        let mappings = [b"a: ".repeat(MAX_DEPTH + 1), b"1".to_vec()].concat();
        assert_eq!(parse(&mappings), None);
        // A failure inside a mapping value used to come back as the part
        // read so far: the refusal covers the whole document.
        let flow = [b"{a: ".repeat(MAX_DEPTH + 1), b"1".to_vec(), b"}".repeat(MAX_DEPTH + 1)].concat();
        assert_eq!(parse(&flow), None);
    }

    #[test]
    fn a_document_far_past_the_limit_does_not_overflow_the_stack() {
        // About 24 KB of brackets overflowed the stack before the limit.
        let handle =
            std::thread::Builder::new().stack_size(STACK_BUDGET).spawn(|| parse(&nested_sequences(100_000))).unwrap();
        assert_eq!(handle.join().unwrap(), None);
    }
}