chrono-node-rs 0.1.0

Behaviour-matched Rust port of chrono-node: natural-language date parsing, checked against chrono-node's own output
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
//! JavaScript regular expressions on top of `fancy-regex`.
//!
//! Port patterns **verbatim from the TypeScript source** (the text between `/.../` or inside
//! `new RegExp("...")` after JS string unescaping) and pass the JS flags. `JsRegex` translates
//! the parts where JS and Rust differ:
//!
//! | JS (no `u` flag) | Rust default | Translation |
//! |---|---|---|
//! | `\d`, `\w`, `\b` are ASCII-only | Unicode-aware | explicit ASCII classes; `\b` as lookarounds |
//! | `\s` is a fixed set incl. U+FEFF | Unicode `White_Space` | the JS set, listed explicitly |
//! | `.` excludes `\n \r U+2028 U+2029` | excludes `\n` only | explicit class |
//! | `\–` (escaped non-ASCII) is a literal | error | the literal character |
//! | `[`, `&&`, `--`, `~~` in a class are literals | class syntax | escaped |
//! | lookahead/lookbehind, backreferences | unsupported in `regex` | `fancy-regex` handles them |
//!
//! | `i` compares `Canonicalize(ch)`: the single-unit uppercase, never non-ASCII → ASCII, BMP only | Unicode simple case folding (U+017F LONG S ~ `s`, U+212A KELVIN SIGN ~ `k`, ...) | no `(?i)`: each literal becomes its JS-equivalence set, each class is closed under it |
//!
//! With `i`, backreferences are rejected (JS compares them canonicalized; no ported pattern uses one).
//!
//! **Unicode mode (`u`)**, as far as ported patterns need it (`[^\p{L}\p{N}\p{M}_]` in Spanish):
//! `\p{..}`/`\P{..}` are Unicode property classes and `\u{..}` a code point (both passed to Rust),
//! and `iu` compares by Unicode **simple case folding** (ECMA-262 `Canonicalize` with `rer.[[Unicode]]`),
//! which is exactly Rust's `(?i)`. That includes `\w`/`\b` matching U+017F and U+212A, as JS `iu`
//! does. `v` (unicode sets) is not supported.

use std::collections::HashMap;
use std::sync::{LazyLock, RwLock};

use fancy_regex::Regex;

/// JS `Canonicalize(ch)` for a non-unicode, case-insensitive regex (ECMA-262 §22.2.2.7.3): the
/// uppercase of the single UTF-16 code unit if it is one code unit, unless that maps a non-ASCII
/// character onto ASCII. Astral characters (surrogate pairs in JS) never fold.
fn js_canonicalize(c: char) -> char {
    if c as u32 > 0xFFFF {
        return c;
    }
    let mut up = c.to_uppercase();
    match (up.next(), up.next()) {
        (Some(u), None) if (u as u32) <= 0xFFFF && !((c as u32) >= 128 && (u as u32) < 128) => u,
        _ => c,
    }
}

/// The BMP characters that share a `js_canonicalize` value with at least one other, by that value.
static JS_CASE_GROUPS: LazyLock<HashMap<char, Vec<char>>> = LazyLock::new(|| {
    let mut by: HashMap<char, Vec<char>> = HashMap::new();
    for cp in 0..=0xFFFFu32 {
        if let Some(c) = char::from_u32(cp) {
            by.entry(js_canonicalize(c)).or_default().push(c);
        }
    }
    by.retain(|_, v| v.len() > 1);
    by
});

/// The characters a JS `/i` (non-unicode) regex treats as equal to `c`, including `c`.
fn js_case_equivalents(c: char) -> Vec<char> {
    JS_CASE_GROUPS
        .get(&js_canonicalize(c))
        .cloned()
        .unwrap_or_else(|| vec![c])
}

fn class_char(c: char) -> String {
    format!(r"\x{{{:X}}}", c as u32)
}

const JS_WHITESPACE: &str =
    r"\t\n\x0B\x0C\r \x{A0}\x{1680}\x{2000}-\x{200A}\x{2028}\x{2029}\x{202F}\x{205F}\x{3000}\x{FEFF}";
const WORD: &str = "A-Za-z0-9_";
const NON_WORD_IN_CLASS: &str = r"\x{0}-\x{2F}\x{3A}-\x{40}\x{5B}-\x{5E}\x{60}\x{7B}-\x{10FFFF}";
/// `\W` inside a class under `iu`: `(?i)` closes a class under case folding, and U+017F / U+212A
/// fold to `s` / `k`, so they must be left out (JS `iu` counts them as word characters).
const NON_WORD_IN_CLASS_IU: &str =
    r"\x{0}-\x{2F}\x{3A}-\x{40}\x{5B}-\x{5E}\x{60}\x{7B}-\x{17E}\x{180}-\x{2129}\x{212B}-\x{10FFFF}";
const NON_DIGIT_IN_CLASS: &str = r"\x{0}-\x{2F}\x{3A}-\x{10FFFF}";
const WORD_BOUNDARY: &str = r"(?:(?<=[A-Za-z0-9_])(?![A-Za-z0-9_])|(?<![A-Za-z0-9_])(?=[A-Za-z0-9_]))";
const NOT_WORD_BOUNDARY: &str = r"(?:(?<=[A-Za-z0-9_])(?=[A-Za-z0-9_])|(?<![A-Za-z0-9_])(?![A-Za-z0-9_]))";
const META: &str = r"\.+*?()|[]{}^$#&-~";

const COMPILED_CAP: usize = 4096;
/// source → flags → compiled. Nested so a hit borrows `&str` keys (no allocation), under a shared lock.
type CompiledCache = HashMap<String, HashMap<String, JsRegex>>;
static COMPILED: LazyLock<RwLock<CompiledCache>> = LazyLock::new(|| RwLock::new(HashMap::new()));

fn cached(source: &str, flags: &str) -> Option<JsRegex> {
    let cache = COMPILED.read().unwrap_or_else(|e| e.into_inner());
    cache.get(source).and_then(|by_flags| by_flags.get(flags)).cloned()
}

/// A compiled JS regular expression.
#[derive(Clone, Debug)]
pub struct JsRegex {
    source: String,
    flags: String,
    re: Regex,
}

/// The result of `RegExp.prototype.exec`: `index` is a **byte** offset into the searched text,
/// and `groups[i]` is `match[i]` (`None` = `undefined`, a group that didn't participate).
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct RegexMatch {
    pub index: usize,
    pub groups: Vec<Option<String>>,
}

impl RegexMatch {
    /// `match[i]`, or `None` if undefined or out of range.
    pub fn get(&self, i: usize) -> Option<&str> {
        self.groups.get(i).and_then(|g| g.as_deref())
    }

    /// `match[0]`.
    pub fn text(&self) -> &str {
        self.get(0).unwrap_or("")
    }

    /// `match.length`.
    pub fn len(&self) -> usize {
        self.groups.len()
    }

    pub fn is_empty(&self) -> bool {
        self.groups.is_empty()
    }
}

impl JsRegex {
    /// Compiles a JS pattern. Panics on an invalid pattern: patterns are constants, and every one
    /// is compiled by the unit tests of the module that owns it.
    pub fn new(source: &str, flags: &str) -> JsRegex {
        JsRegex::try_new(source, flags).unwrap_or_else(|e| panic!("invalid JS regex /{source}/{flags}: {e}"))
    }

    /// Compiled once per `(source, flags)` in the process: upstream builds `new RegExp(...)` inside
    /// methods called on every parse (`innerPattern()`), and a ported step that does the same must not
    /// recompile a Unicode case-insensitive pattern each time (ru/uk ran ~30x slower than other locales).
    pub fn try_new(source: &str, flags: &str) -> Result<JsRegex, String> {
        if let Some(hit) = cached(source, flags) {
            return Ok(hit);
        }
        let translated = translate(source, flags)?;
        let re = Regex::new(&translated).map_err(|e| format!("{e} (translated: {translated})"))?;
        let compiled = JsRegex {
            source: source.to_string(),
            flags: flags.to_string(),
            re,
        };
        let mut cache = COMPILED.write().unwrap_or_else(|e| e.into_inner());
        // Patterns come from the ported code (finite); the cap only guards against unbounded growth.
        let entries: usize = cache.values().map(HashMap::len).sum();
        if entries < COMPILED_CAP {
            cache
                .entry(source.to_string())
                .or_default()
                .insert(flags.to_string(), compiled.clone());
        }
        Ok(compiled)
    }

    /// `regex.source`: the original JS pattern text.
    pub fn source(&self) -> &str {
        &self.source
    }

    /// `regex.flags`.
    pub fn flags(&self) -> &str {
        &self.flags
    }

    /// `regex.exec(text)` for a non-global regex: the first match, searching from the start.
    /// A backtracking-limit error counts as "no match".
    pub fn exec(&self, text: &str) -> Option<RegexMatch> {
        let caps = self.re.captures(text).ok()??;
        let index = caps.get(0)?.start();
        let groups = (0..caps.len())
            .map(|i| caps.get(i).map(|m| m.as_str().to_string()))
            .collect();
        Some(RegexMatch { index, groups })
    }

    /// `regex.test(text)`, or the truthiness of `text.match(regex)`.
    pub fn is_match(&self, text: &str) -> bool {
        self.re.is_match(text).unwrap_or(false)
    }
}

/// A lazily compiled `&'static JsRegex`: `js_regex!(r"(\d+)\s*days?", "i")`.
#[macro_export]
macro_rules! js_regex {
    ($source:expr) => {
        $crate::js_regex!($source, "")
    };
    ($source:expr, $flags:expr) => {{
        static RE: std::sync::LazyLock<$crate::core::regex::JsRegex> =
            std::sync::LazyLock::new(|| $crate::core::regex::JsRegex::new($source, $flags));
        &*RE
    }};
}

fn push_literal(out: &mut String, c: char) {
    if META.contains(c) {
        out.push('\\');
    }
    out.push(c);
}

fn hex_digits(chars: &[char], start: usize, count: usize) -> Option<String> {
    let s: String = chars.get(start..start + count)?.iter().collect();
    s.chars().all(|c| c.is_ascii_hexdigit()).then_some(s)
}

/// Length of a `{n}`, `{n,}` or `{n,m}` quantifier starting at `chars[i] == '{'`, if valid.
fn quantifier_len(chars: &[char], i: usize) -> Option<usize> {
    let mut j = i + 1;
    let digits_start = j;
    while j < chars.len() && chars[j].is_ascii_digit() {
        j += 1;
    }
    if j == digits_start {
        return None;
    }
    if chars.get(j) == Some(&',') {
        j += 1;
        while j < chars.len() && chars[j].is_ascii_digit() {
            j += 1;
        }
    }
    (chars.get(j) == Some(&'}')).then_some(j - i + 1)
}

/// A class atom: its code-point ranges, whether it is a single character, and its source length.
type ClassAtom = (Vec<(u32, u32)>, bool, usize);

/// One class atom at `chars[i]` (after `[`): its code-point ranges and its length, or None at `]`.
/// Ranges for class escapes (`\d`, `\W`, ...) are returned with `single = false` (no `a-b` with them).
fn class_atom(chars: &[char], i: usize) -> Result<Option<ClassAtom>, String> {
    let Some(&c) = chars.get(i) else {
        return Err("unterminated character class".into());
    };
    if c == ']' {
        return Ok(None);
    }
    if c != '\\' {
        return Ok(Some((vec![(c as u32, c as u32)], true, 1)));
    }
    let Some(&e) = chars.get(i + 1) else {
        return Err("trailing backslash".into());
    };
    let one = |v: u32, len: usize| Ok(Some((vec![(v, v)], true, len)));
    let set = |r: Vec<(u32, u32)>| Ok(Some((r, false, 2)));
    let word = vec![(0x30, 0x39), (0x41, 0x5A), (0x5F, 0x5F), (0x61, 0x7A)];
    match e {
        'd' => set(vec![(0x30, 0x39)]),
        'D' => set(vec![(0, 0x2F), (0x3A, 0x10FFFF)]),
        'w' => set(word),
        'W' => set(vec![
            (0, 0x2F),
            (0x3A, 0x40),
            (0x5B, 0x5E),
            (0x60, 0x60),
            (0x7B, 0x10FFFF),
        ]),
        's' => set(vec![
            (0x09, 0x0D),
            (0x20, 0x20),
            (0xA0, 0xA0),
            (0x1680, 0x1680),
            (0x2000, 0x200A),
            (0x2028, 0x2029),
            (0x202F, 0x202F),
            (0x205F, 0x205F),
            (0x3000, 0x3000),
            (0xFEFF, 0xFEFF),
        ]),
        'b' => one(8, 2),
        'n' => one(10, 2),
        'r' => one(13, 2),
        't' => one(9, 2),
        'f' => one(12, 2),
        'v' => one(11, 2),
        'u' => match hex_digits(chars, i + 2, 4) {
            Some(h) => one(u32::from_str_radix(&h, 16).unwrap(), 6),
            None => one('u' as u32, 2),
        },
        'x' => match hex_digits(chars, i + 2, 2) {
            Some(h) => one(u32::from_str_radix(&h, 16).unwrap(), 4),
            None => one('x' as u32, 2),
        },
        '0' if !chars.get(i + 2).is_some_and(|d| d.is_ascii_digit()) => one(0, 2),
        other => one(other as u32, 2),
    }
}

/// Whether `out` ends with an unescaped `-` (an even number of backslashes before it).
fn ends_with_bare_dash(out: &str) -> bool {
    let Some(rest) = out.strip_suffix('-') else {
        return false;
    };
    rest.chars().rev().take_while(|&c| c == '\\').count() % 2 == 0
}

/// For a case-insensitive class starting at `chars[start]` (its first member): the characters JS
/// treats as equal to a member but the class doesn't list, as class text to append before `]`.
fn class_case_closure(chars: &[char], start: usize) -> Result<String, String> {
    let mut ranges: Vec<(u32, u32)> = Vec::new();
    let mut i = start;
    while let Some((r, single, len)) = class_atom(chars, i)? {
        i += len;
        // `a-b`: a range between two single characters (not at the end of the class).
        if single && chars.get(i) == Some(&'-') && chars.get(i + 1).is_some_and(|&n| n != ']') {
            if let Some((r2, true, len2)) = class_atom(chars, i + 1)? {
                ranges.push((r[0].0, r2[0].1));
                i += 1 + len2;
                continue;
            }
        }
        ranges.extend(r);
    }
    let has = |c: char| ranges.iter().any(|&(a, b)| (a..=b).contains(&(c as u32)));
    let mut extra: Vec<char> = Vec::new();
    for group in JS_CASE_GROUPS.values() {
        if group.iter().any(|&c| has(c)) {
            extra.extend(group.iter().copied().filter(|&c| !has(c)));
        }
    }
    extra.sort_unstable();
    Ok(extra.into_iter().map(class_char).collect())
}

/// A literal character outside a class: with `i`, the set of its JS equivalents.
fn push_ci_literal(out: &mut String, c: char, ci: bool) {
    let eq = if ci { js_case_equivalents(c) } else { vec![c] };
    if eq.len() > 1 {
        out.push('[');
        for e in eq {
            out.push_str(&class_char(e));
        }
        out.push(']');
    } else {
        push_literal(out, c);
    }
}

/// Translates JS regex source (non-unicode mode) to `fancy-regex` syntax.
pub fn translate(source: &str, flags: &str) -> Result<String, String> {
    let mut out = String::new();
    let mut inline = String::new();
    let unicode = flags.contains('u');
    // Without `u`, `i` is applied by expansion (JS canonicalize), not by Rust's Unicode case
    // folding. With `u`, JS folds by simple case folding: Rust's `(?i)`.
    let ci = flags.contains('i') && !unicode;
    let unicode_ci = flags.contains('i') && unicode;
    for f in flags.chars() {
        match f {
            'i' if unicode => inline.push('i'),
            'i' | 'u' => {}
            'm' | 's' => inline.push(f),
            'g' | 'y' | 'd' => return Err(format!("flag '{f}' changes exec() state; not supported")),
            'v' => return Err(format!("flag '{f}' (unicode sets) is not supported")),
            _ => return Err(format!("unknown flag '{f}'")),
        }
    }
    if !inline.is_empty() {
        out.push_str(&format!("(?{inline})"));
    }

    let chars: Vec<char> = source.chars().collect();
    let n = chars.len();
    let mut in_class = false;
    let mut class_extra = String::new();
    let mut i = 0;
    while i < n {
        let c = chars[i];
        if c == '\\' {
            i += 1;
            let Some(&e) = chars.get(i) else {
                return Err("trailing backslash".into());
            };
            match e {
                'd' => out.push_str(if in_class { "0-9" } else { "[0-9]" }),
                'D' => {
                    if in_class {
                        out.push_str(NON_DIGIT_IN_CLASS)
                    } else {
                        out.push_str("[^0-9]")
                    }
                }
                'w' => {
                    if in_class {
                        out.push_str(WORD)
                    } else {
                        out.push_str(&format!("[{WORD}]"))
                    }
                }
                'W' => {
                    if in_class {
                        out.push_str(if unicode_ci {
                            NON_WORD_IN_CLASS_IU
                        } else {
                            NON_WORD_IN_CLASS
                        })
                    } else {
                        out.push_str(&format!("[^{WORD}]"))
                    }
                }
                's' => {
                    if in_class {
                        out.push_str(JS_WHITESPACE)
                    } else {
                        out.push_str(&format!("[{JS_WHITESPACE}]"))
                    }
                }
                'S' => {
                    if in_class {
                        return Err(r"\S inside a character class is not supported".into());
                    }
                    out.push_str(&format!("[^{JS_WHITESPACE}]"));
                }
                'b' => out.push_str(if in_class { r"\x{8}" } else { WORD_BOUNDARY }),
                'B' => {
                    if in_class {
                        return Err(r"\B inside a character class".into());
                    }
                    out.push_str(NOT_WORD_BOUNDARY);
                }
                'n' => out.push_str(r"\n"),
                'r' => out.push_str(r"\r"),
                't' => out.push_str(r"\t"),
                'f' => out.push_str(r"\x{C}"),
                'v' => out.push_str(r"\x{B}"),
                'u' if unicode && chars.get(i + 1) == Some(&'{') => {
                    let close = chars[i + 1..]
                        .iter()
                        .position(|&c| c == '}')
                        .ok_or("unterminated \\u{")?;
                    let h: String = chars[i + 2..i + 1 + close].iter().collect();
                    if h.is_empty() || !h.chars().all(|c| c.is_ascii_hexdigit()) {
                        return Err(format!(r"invalid \u{{{h}}}"));
                    }
                    out.push_str(&format!(r"\x{{{h}}}"));
                    i += close + 1;
                }
                'p' | 'P' if unicode && chars.get(i + 1) == Some(&'{') => {
                    let close = chars[i + 1..]
                        .iter()
                        .position(|&c| c == '}')
                        .ok_or("unterminated \\p{")?;
                    let name: String = chars[i + 2..i + 1 + close].iter().collect();
                    if !name.chars().all(|c| c.is_ascii_alphanumeric() || c == '_' || c == '=') {
                        return Err(format!(r"invalid \{e}{{{name}}}"));
                    }
                    out.push_str(&format!(r"\{e}{{{name}}}"));
                    i += close + 1;
                }
                'u' => match hex_digits(&chars, i + 1, 4) {
                    Some(h) => {
                        let ch = char::from_u32(u32::from_str_radix(&h, 16).unwrap());
                        match ch {
                            Some(ch) if ci && !in_class => push_ci_literal(&mut out, ch, ci),
                            _ => out.push_str(&format!(r"\x{{{h}}}")),
                        }
                        i += 4;
                    }
                    None => push_ci_literal(&mut out, 'u', ci && !in_class),
                },
                'x' => match hex_digits(&chars, i + 1, 2) {
                    Some(h) => {
                        let ch = char::from_u32(u32::from_str_radix(&h, 16).unwrap());
                        match ch {
                            Some(ch) if ci && !in_class => push_ci_literal(&mut out, ch, ci),
                            _ => out.push_str(&format!(r"\x{{{h}}}")),
                        }
                        i += 2;
                    }
                    None => push_ci_literal(&mut out, 'x', ci && !in_class),
                },
                '0' if !chars.get(i + 1).is_some_and(|d| d.is_ascii_digit()) => out.push_str(r"\x{0}"),
                '1'..='9' if !in_class => {
                    if ci {
                        return Err("backreferences with the `i` flag are not supported".into());
                    }
                    out.push('\\');
                    out.push(e);
                    while chars.get(i + 1).is_some_and(|d| d.is_ascii_digit()) {
                        i += 1;
                        out.push(chars[i]);
                    }
                }
                'c' | 'k' | 'p' | 'P' => return Err(format!(r"\{e} is not supported")),
                // Identity escapes: `\-`, `\.`, `\/`, `\–`, `\:` ... are the literal character.
                other => push_ci_literal(&mut out, other, ci && !in_class),
            }
            i += 1;
            continue;
        }

        if in_class {
            match c {
                ']' => {
                    in_class = false;
                    if !class_extra.is_empty() && ends_with_bare_dash(&out) {
                        // JS reads a final `-` as a literal; the appended members would make it a range.
                        out.pop();
                        out.push_str(r"\-");
                    }
                    out.push_str(&std::mem::take(&mut class_extra));
                    out.push(']');
                }
                '[' | '&' | '~' => {
                    out.push('\\');
                    out.push(c);
                }
                '-' if chars.get(i + 1) == Some(&'-') => out.push_str(r"\-"),
                _ => out.push(c),
            }
            i += 1;
            continue;
        }

        match c {
            '[' => {
                if chars.get(i + 1) == Some(&']') || (chars.get(i + 1) == Some(&'^') && chars.get(i + 2) == Some(&']'))
                {
                    return Err("empty character classes ([] or [^]) are not supported".into());
                }
                in_class = true;
                out.push('[');
                if chars.get(i + 1) == Some(&'^') {
                    out.push('^');
                    i += 1;
                }
                if ci {
                    class_extra = class_case_closure(&chars, i + 1)?;
                }
            }
            '.' => out.push_str(r"[^\n\r\x{2028}\x{2029}]"),
            '(' if chars.get(i + 1) == Some(&'?')
                && chars.get(i + 2) == Some(&'<')
                && !matches!(chars.get(i + 3), Some('=') | Some('!')) =>
            {
                out.push_str("(?P<");
                i += 3;
                // The group name is copied verbatim: it isn't matched text, so `i` mustn't expand it.
                while let Some(&n) = chars.get(i) {
                    out.push(n);
                    i += 1;
                    if n == '>' {
                        break;
                    }
                }
                continue;
            }
            '{' => match quantifier_len(&chars, i) {
                Some(len) => {
                    out.extend(&chars[i..i + len]);
                    i += len;
                    continue;
                }
                None => out.push_str(r"\{"),
            },
            '}' => out.push_str(r"\}"),
            // Syntax characters (`( ) | * + ? ^ $`) have no case equivalents, so they pass unchanged.
            _ if ci && js_case_equivalents(c).len() > 1 => push_ci_literal(&mut out, c, true),
            _ => out.push(c),
        }
        i += 1;
    }
    if in_class {
        return Err("unterminated character class".into());
    }
    Ok(out)
}

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

    #[test]
    fn compiled_once_per_source_and_flags() {
        let src = r"(?:с|со)?\s*(сегодня|завтра)(?=[^\p{L}\p{N}_]|$)";
        let a = JsRegex::new(src, "iu");
        // Cached unless the (global, capped) cache was already full when it was compiled.
        let full = COMPILED
            .read()
            .unwrap_or_else(|e| e.into_inner())
            .values()
            .map(HashMap::len)
            .sum::<usize>()
            >= COMPILED_CAP;
        assert!(full || cached(src, "iu").is_some());
        let b = JsRegex::new(src, "iu");
        assert_eq!((a.source(), a.flags()), (b.source(), b.flags()));
        assert_eq!(
            b.exec("с Завтра").map(|m| m.groups),
            Some(vec![Some("с Завтра".into()), Some("Завтра".into())])
        );
        // Other flags are a different regex.
        assert!(JsRegex::new(src, "u").exec("с Завтра").is_none());
        assert!(JsRegex::try_new("(", "").is_err());
    }

    fn m(src: &str, flags: &str, text: &str) -> Option<(usize, Vec<Option<String>>)> {
        JsRegex::new(src, flags).exec(text).map(|m| (m.index, m.groups))
    }

    #[test]
    fn ascii_classes_like_js() {
        assert!(JsRegex::new(r"^\w+$", "").is_match("abc_09"));
        assert!(!JsRegex::new(r"^\w+$", "").is_match("café"));
        assert!(!JsRegex::new(r"^\d$", "").is_match("٣")); // Arabic-Indic digit
        assert!(JsRegex::new(r"^\W$", "").is_match("é"));
        assert!(JsRegex::new(r"^[\W]$", "").is_match("é"));
        assert!(!JsRegex::new(r"^[\W]$", "").is_match("a"));
        assert!(JsRegex::new(r"^[\D]$", "").is_match("x"));
    }

    #[test]
    fn whitespace_and_dot_like_js() {
        assert!(JsRegex::new(r"^\s$", "").is_match("\u{FEFF}"));
        assert!(JsRegex::new(r"^\s$", "").is_match("\u{3000}"));
        assert!(!JsRegex::new(r"^\s$", "").is_match("\u{85}")); // NEL is not JS whitespace
        assert!(!JsRegex::new(r"^.$", "").is_match("\r"));
        assert!(!JsRegex::new(r"^.$", "").is_match("\u{2028}"));
        assert!(JsRegex::new(r"^.$", "").is_match("é"));
    }

    #[test]
    fn word_boundary_is_ascii() {
        // "é" is not a JS word character, so there is a boundary between "caf" and "é".
        assert_eq!(m(r"caf\b", "", "café"), Some((0, vec![Some("caf".into())])));
        assert_eq!(m(r"\bnow\b", "i", "right NOW!"), Some((6, vec![Some("NOW".into())])));
        assert!(JsRegex::new(r"a\Bb", "").is_match("ab"));
    }

    #[test]
    fn lookaround_and_groups() {
        // The ENCasualDateParser pattern, with lookahead and a left-boundary group.
        let re = JsRegex::new(r"(\W|^)(now|today|tonight|last\s*night)(?=\W|$)", "i");
        let mm = re.exec("see you Tonight.").unwrap();
        assert_eq!(mm.index, 7);
        assert_eq!(
            mm.groups,
            vec![Some(" Tonight".into()), Some(" ".into()), Some("Tonight".into())]
        );
        assert!(re.exec("nowhere").is_none());
        // Undefined optional groups are None.
        let mm = JsRegex::new(r"(a)(b)?", "").exec("a").unwrap();
        assert_eq!(mm.get(2), None);
        assert_eq!(mm.len(), 3);
        assert!(JsRegex::new(r"(?<!x)y", "").is_match("ay"));
    }

    #[test]
    fn escapes_and_literals() {
        assert!(JsRegex::new(r"^\–\〜\:$", "").is_match("–〜:"));
        assert!(JsRegex::new(r"^\/\-\.$", "").is_match("/-."));
        assert!(JsRegex::new(r"^[\-\–~&]+$", "").is_match("-–~&"));
        assert!(JsRegex::new(r"^\u00e9\x41$", "").is_match("éA"));
        assert!(JsRegex::new(r"^a{2,3}$", "").is_match("aaa"));
        assert!(JsRegex::new(r"^{x}$", "").is_match("{x}"));
        assert!(JsRegex::new(r"^(?<word>ab)\1$", "").is_match("abab"));
        assert!(JsRegex::new(r"^[\[\]]+$", "").is_match("[]"));
    }

    #[test]
    fn rejects_unsupported() {
        assert!(JsRegex::try_new("a", "g").is_err());
        assert!(JsRegex::try_new("a", "v").is_err());
        assert!(JsRegex::try_new("[]", "").is_err());
    }

    #[test]
    fn case_insensitive_flag() {
        assert!(JsRegex::new(r"^TODAY$", "i").is_match("today"));
        assert!(!JsRegex::new(r"^TODAY$", "").is_match("today"));
    }

    /// JS `/i` without `u` folds by `Canonicalize` (single-unit uppercase, never non-ASCII onto
    /// ASCII), not by Unicode case folding. Found by the reviewer on #136 (the ` EſT` example).
    #[test]
    fn case_insensitive_is_js_canonicalize() {
        let abbr = JsRegex::new(r"^\s*([A-Z]{2,4})(?:\W|$)", "i");
        assert_eq!(
            abbr.exec(" EsT").and_then(|m| m.get(1).map(str::to_string)),
            Some("EsT".into())
        );
        assert!(!abbr.is_match(" E\u{17F}T"), "U+017F LONG S is not `s` in JS");
        let k = JsRegex::new("k", "i");
        assert!(k.is_match("K") && k.is_match("k"));
        assert!(!k.is_match("\u{212A}"), "U+212A KELVIN SIGN is not `k` in JS");
        assert!(!JsRegex::new("[a-z]", "i").is_match("\u{212A}"));
        assert!(!JsRegex::new("[a-z]", "i").is_match("\u{17F}"));
        // Real case pairs still fold, inside and outside classes.
        assert!(JsRegex::new("é", "i").is_match("É") && JsRegex::new("[à-ÿ]", "i").is_match("É"));
        let sigma = JsRegex::new("σ", "i");
        assert!(sigma.is_match("Σ") && sigma.is_match("ς"));
        // Full mappings don't count: ß is not SS.
        assert!(!JsRegex::new("^ß$", "i").is_match("SS") && !JsRegex::new("^s$", "i").is_match("ß"));
        // Negated classes are closed before negation.
        let not_lower = JsRegex::new("^[^a-z]$", "i");
        assert!(!not_lower.is_match("A") && not_lower.is_match("\u{17F}") && not_lower.is_match("\u{212A}"));
        // Escapes fold like literals: \x41 is `A`; \u017F stays itself.
        assert!(JsRegex::new(r"^\x41$", "i").is_match("a"));
        assert!(!JsRegex::new(r"^\u017F$", "i").is_match("s"));
        // Astral characters are surrogate pairs in JS: they never fold without `u`.
        assert!(!JsRegex::new("\u{10400}", "i").is_match("\u{10428}"));
        // Without `i`, nothing changes.
        assert!(!JsRegex::new("k", "").is_match("K"));
    }

    #[test]
    fn case_insensitive_class_keeps_a_final_dash_literal() {
        // Appended case equivalents must not turn a final `-` into a range (`[k-\x{4B}]`, `[A-\x{61}]`).
        let k = JsRegex::new("^[k-]$", "i");
        assert!(k.is_match("K") && k.is_match("-"));
        let a = JsRegex::new("^[A-]$", "i");
        assert!(a.is_match("a") && a.is_match("-") && !a.is_match("_"));
        let x = JsRegex::new("^[x-]$", "i");
        assert!(x.is_match("X"));
        let lead = JsRegex::new("^[-a]$", "i");
        assert!(lead.is_match("A") && !lead.is_match("_"));
        assert!(JsRegex::new(r"^[a\-]$", "i").is_match("A"));
        let not_a = JsRegex::new("^[^A-]$", "i");
        assert!(!not_a.is_match("-") && !not_a.is_match("a") && not_a.is_match("_") && not_a.is_match("["));
    }

    #[test]
    fn case_insensitive_group_names_are_not_expanded() {
        let re = JsRegex::new("(?<ab>.)(?<AB>.)", "i");
        assert!(re.is_match("xy"));
        assert!(JsRegex::new(r"^(?<word>ab)$", "i").is_match("AB"));
    }

    #[test]
    fn case_insensitive_backreferences_are_rejected() {
        assert!(JsRegex::try_new(r"(a)\1", "i").is_err());
        assert!(JsRegex::try_new(r"(a)\1", "").is_ok());
    }

    #[test]
    fn js_canonicalize_rules() {
        assert_eq!(js_canonicalize('a'), 'A');
        assert_eq!(js_canonicalize('\u{17F}'), '\u{17F}', "non-ASCII never maps onto ASCII");
        assert_eq!(
            js_canonicalize('\u{131}'),
            '\u{131}',
            "dotless i: its uppercase I is ASCII"
        );
        assert_eq!(js_canonicalize('ß'), 'ß', "uppercase is two units (SS)");
        assert_eq!(js_canonicalize('ς'), 'Σ');
    }

    #[test]
    fn unicode_mode_like_js() {
        // Expected values from Node (indexes here are bytes; JS reports UTF-16 units).
        // ESCasualDateParser's look-behind check, `/(?:^|[^\p{L}\p{N}\p{M}_])(?:a|en)\s+la\s*$/iu`.
        let src = r"(?:^|[^\p{L}\p{N}\p{M}_])(?:a|en)\s+la\s*$";
        let at = |t: &str| JsRegex::new(src, "iu").exec(t).map(|m| (m.index, m.text().to_string()));
        assert_eq!(at("Nos vemos a la "), Some((9, " a la ".into())));
        assert_eq!(at("EN  LA"), Some((0, "EN  LA".into())));
        assert_eq!(at("é en la"), Some((2, " en la".into())));
        assert_eq!(at("a la"), Some((0, "a la".into())));
        for t in ["casa la ", "ña la ", "1a la ", "_en la"] {
            assert_eq!(at(t), None, "{t:?}");
        }
        // `iu` folds by simple case folding: U+017F and U+212A are word characters (not with `i` alone).
        for (src, flags, text, hit) in [
            (r"^\w$", "iu", "ſ", true),
            (r"^\w$", "iu", "\u{212A}", true),
            (r"^\w$", "iu", "é", false),
            (r"^\w$", "i", "ſ", false),
            (r"^\w$", "i", "\u{212A}", false),
            (r"^k$", "iu", "\u{212A}", true),
            (r"^ß$", "iu", "ẞ", true),
            (r"^ß$", "iu", "SS", false),
            (r"^[^\p{L}]$", "u", "é", false),
            (r"^[^\p{L}]$", "u", "1", true),
            (r"^\p{M}$", "u", "\u{301}", true),
            (r"^\P{L}+$", "u", "12 !", true),
            (r"^\P{L}+$", "u", "a1", false),
        ] {
            assert_eq!(
                JsRegex::new(src, flags).is_match(text),
                hit,
                "/{src}/{flags} on {text:?}"
            );
        }
        // `\W` and `\w` in and outside a class under `iu` (Copilot review of #208): s S k K ſ K a é !
        let probe = ["s", "S", "k", "K", "ſ", "\u{212A}", "a", "é", "!"];
        for (src, flags, want) in [
            (r"^[\W]$", "iu", "000000011"),
            (r"^\W$", "iu", "000000011"),
            (r"^[\W]$", "i", "000011011"),
            (r"^[\w]$", "iu", "111111100"),
            (r"^[^\w]$", "iu", "000000011"),
            (r"^[^\W]$", "iu", "111111100"),
        ] {
            let re = JsRegex::new(src, flags);
            let got: String = probe.iter().map(|t| if re.is_match(t) { '1' } else { '0' }).collect();
            assert_eq!(got, want, "/{src}/{flags}");
        }
        assert!(JsRegex::new(r"a\Bſ", "iu").is_match("aſ"));
        assert!(!JsRegex::new(r"a\bſ", "iu").is_match("aſ"));
        assert!(JsRegex::new(r"k\B\u212A", "iu").is_match("k\u{212A}"));
        assert!(JsRegex::new(r"\bſ", "iu").is_match(" ſ"));
        assert_eq!(JsRegex::new(r"\u{1F600}", "u").exec("x😀").map(|m| m.index), Some(1));
        // Without `u`, `\p` stays unsupported (JS would read it as a literal `p`).
        assert!(JsRegex::try_new(r"\p{L}", "").is_err());
    }
}