kataan 0.0.8

A high-performance JavaScript engine written in pure Rust. Library, C FFI, and CLI.
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
//! Regex engine tests.

use super::Regex;
use alloc::format;
use alloc::string::ToString;

fn re(pattern: &str, flags: &str) -> Regex {
    Regex::new(pattern, flags).expect("compile ok")
}

#[test]
fn literals_and_anchors() {
    assert!(re("abc", "").is_match("xxabcyy"));
    assert!(!re("abc", "").is_match("ab c"));
    assert!(re("^abc$", "").is_match("abc"));
    assert!(!re("^abc$", "").is_match("abcd"));
    assert_eq!(re("abc", "").find_from("xxabc", 0), Some((2, 5)));
}

#[test]
fn unicode_and_hex_escapes() {
    // `\uHHHH` and `\xHH` resolve to the code point.
    assert!(re(r"A", "").is_match("A"));
    assert!(re(r"\x41", "").is_match("A"));
    assert!(re(r"σ", "").is_match("\u{03c3}"));
    assert!(!re(r"A", "").is_match("B"));
    // `\uHHHH` 4-digit form (the pattern contains a backslash-u escape).
    assert!(re("\\u0041", "").is_match("A"));
    assert!(re("\\u03c3", "").is_match("\u{03c3}"));
    assert!(!re("\\u0041", "").is_match("B"));
    // `\u{…}` code-point form (supplementary planes).
    assert!(re(r"\u{1F600}", "").is_match("\u{1F600}"));
    // Inside a character class.
    assert!(re(r"[A-Z]+", "").is_match("HELLO"));
    assert!(re(r"[\x61\x62]", "").is_match("b"));
    // `\t` via `\x09`.
    assert!(re(r"\x09", "").is_match("a\tb"));
    // Without the `u` flag, an ill-formed `\u` is a valid AnnexB IdentityEscape:
    // `\u00` matches the literal `u00` (`\u` → `u`, then `00`).
    assert!(re(r"\u00", "").is_match("u00"));
    // With the `u` flag it IS a compile error (strict unicode mode).
    assert!(Regex::new(r"\u00", "u").is_err());
}

#[test]
fn dot_and_classes() {
    assert!(re("a.c", "").is_match("axc"));
    assert!(!re("a.c", "").is_match("a\nc")); // `.` excludes newline
    assert!(re("a.c", "s").is_match("a\nc")); // dotall
    assert!(re("[abc]+", "").is_match("cab"));
    assert!(re("[a-z]+", "").is_match("hello"));
    assert!(!re("[a-z]+", "").is_match("123"));
    assert!(re("[^0-9]", "").is_match("a"));
    assert!(!re("[^0-9]", "").is_match("5"));
    assert!(re(r"\d{3}", "").is_match("a123b"));
    assert!(re(r"\w+", "").is_match("foo_bar"));
    assert!(re(r"\s", "").is_match("a b"));
}

#[test]
fn quantifiers() {
    assert!(re("a*", "").is_match(""));
    assert!(re("ab+c", "").is_match("abbbc"));
    assert!(!re("ab+c", "").is_match("ac"));
    assert!(re("colou?r", "").is_match("color"));
    assert!(re("colou?r", "").is_match("colour"));
    assert!(re("a{2,4}", "").is_match("aaa"));
    assert!(!re("^a{2,4}$", "").is_match("a"));
    assert!(!re("^a{2,4}$", "").is_match("aaaaa"));
    // Greedy vs lazy.
    assert_eq!(
        re("a.*b", "").captures_from("axxbxxb", 0).unwrap().whole(),
        (0, 7)
    );
    assert_eq!(
        re("a.*?b", "").captures_from("axxbxxb", 0).unwrap().whole(),
        (0, 4)
    );
}

#[test]
fn groups_and_alternation() {
    assert!(re("cat|dog", "").is_match("hotdog"));
    assert!(!re("cat|dog", "").is_match("fish"));
    let caps = re(r"(\d+)-(\d+)", "")
        .captures_from("x 12-34 y", 0)
        .unwrap();
    assert_eq!(caps.group(1), Some((2, 4)));
    assert_eq!(caps.group(2), Some((5, 7)));
    // Non-capturing group still groups for quantification.
    assert!(re("(?:ab)+", "").is_match("ababab"));
    assert_eq!(re("(?:ab)+", "").group_count(), 0);
    assert_eq!(re("(a)(b)", "").group_count(), 2);
}

#[test]
fn word_boundaries() {
    assert!(re(r"\bword\b", "").is_match("a word here"));
    assert!(!re(r"\bword\b", "").is_match("wordy"));
    assert!(re(r"\Bord", "").is_match("word"));
}

#[test]
fn case_insensitive() {
    assert!(re("hello", "i").is_match("HELLO"));
    assert!(re("[a-z]+", "i").is_match("ABC"));
    assert!(!re("hello", "").is_match("HELLO"));
}

#[test]
fn multiline() {
    assert!(re("^bar", "m").is_match("foo\nbar"));
    assert!(!re("^bar", "").is_match("foo\nbar"));
}

#[test]
fn replace() {
    assert_eq!(re("o", "g").replace("foo boo", "0"), "f00 b00");
    assert_eq!(re("o", "").replace("foo", "0"), "f0o"); // first only
    assert_eq!(
        re(r"(\w+)@(\w+)", "").replace("user@host", "$2.$1"),
        "host.user"
    );
    assert_eq!(re(r"\d+", "g").replace("a1b22c333", "#"), "a#b#c#");
}

#[test]
fn lookaround_backref_named() {
    // Lookahead / negative lookahead.
    assert_eq!(re("foo(?=bar)", "").find_from("foobar", 0), Some((0, 3)));
    assert!(!re("foo(?=bar)", "").is_match("foobaz"));
    assert!(re("foo(?!bar)", "").is_match("foobaz"));
    // Lookbehind / negative lookbehind.
    assert_eq!(re("(?<=\\$)\\d+", "").find_from("$100", 0), Some((1, 4)));
    assert!(re("(?<!\\$)\\d+", "").is_match("100"));
    // Backreference.
    assert!(re("(ab)\\1", "").is_match("abab"));
    assert!(!re("(ab)\\1", "").is_match("abcd"));
    // Named group exposes its index.
    let r = re("(?<year>\\d{4})", "");
    assert_eq!(r.group_names(), &[(1, alloc::string::String::from("year"))]);
}

#[test]
fn unicode_property_escapes() {
    // Property escapes require the `u` flag. The `&str`/`&[char]` adapters always
    // run the scalar (unicode-mode) program, so these match as property escapes.
    assert!(re("\\p{L}", "u").is_match("a"));
    assert!(!re("\\p{L}", "u").is_match("5"));
    assert!(re("\\p{L}", "u").is_match("Ω")); // Unicode-aware, not just ASCII
    assert!(re("^\\p{N}+$", "u").is_match("123"));
    assert!(re("\\p{Lu}", "u").is_match("A"));
    assert!(!re("\\p{Lu}", "u").is_match("a"));
    assert!(re("\\P{L}", "u").is_match("5")); // negated
    assert!(re("^[\\p{L}\\p{N}]+$", "u").is_match("abc123")); // in a class

    // `Property=Value` forms: General_Category, Script, Script_Extensions, with
    // canonical names, aliases (`gc`/`sc`/`scx`), and ISO 15924 short codes.
    assert!(re("^\\p{General_Category=Letter}$", "u").is_match("a"));
    assert!(re("^\\p{gc=L}$", "u").is_match("a"));
    assert!(re("^\\p{Script=Greek}+$", "u").is_match("αβ"));
    assert!(re("^\\p{sc=Latn}$", "u").is_match("a"));
    assert!(!re("^\\p{Script=Greek}$", "u").is_match("a"));
    assert!(re("^\\p{Script_Extensions=Latin}$", "u").is_match("a"));
    // Synthetic GC values: `LC`/`Cased_Letter` and POSIX-style aliases.
    assert!(re("^\\p{LC}$", "u").is_match("A"));
    assert!(re("^\\p{gc=digit}$", "u").is_match("7"));
    // Closed-form binary properties.
    assert!(re("^\\p{ASCII}$", "u").is_match("a"));
    assert!(!re("^\\p{ASCII}$", "u").is_match("é"));
    assert!(re("^\\p{ASCII_Hex_Digit}+$", "u").is_match("0aF"));
    assert!(re("^\\p{White_Space}$", "u").is_match(" "));
    assert!(re("^\\p{Alphabetic}$", "u").is_match("a"));

    // --- Validation (→ SyntaxError at compile) ---
    // Unknown property / value names must be rejected.
    assert!(Regex::new("\\p{Nonsense}", "u").is_err());
    assert!(Regex::new("\\p{Script=Nonsense}", "u").is_err());
    // A non-binary property used as a lone name must be rejected.
    assert!(Regex::new("\\p{General_Category}", "u").is_err());
    assert!(Regex::new("\\p{Script}", "u").is_err());
    // A binary property with an explicit value must be rejected.
    assert!(Regex::new("\\p{ASCII=Invalid}", "u").is_err());
    assert!(Regex::new("\\p{Alphabetic=Yes}", "u").is_err());
    // Malformed grammar.
    assert!(Regex::new("\\p{}", "u").is_err());
    assert!(Regex::new("\\p{=}", "u").is_err());
    assert!(Regex::new("\\p{=L}", "u").is_err());
    assert!(Regex::new("\\p{^L}", "u").is_err());
    assert!(Regex::new("\\p{ L }", "u").is_err()); // no whitespace folding
    assert!(Regex::new("\\pL", "u").is_err()); // braces required under `u`
    assert!(Regex::new("\\p", "u").is_err());
    assert!(Regex::new("\\p{", "u").is_err());
    // Valid binary property names with no local data still parse (no SyntaxError).
    assert!(Regex::new("\\p{Emoji}", "u").is_ok());
    assert!(Regex::new("\\p{ID_Start}", "u").is_ok());

    // Without the `u` flag, `\p`/`\P` are an Annex B IdentityEscape: the literal
    // `p`/`P`, so any "property name" text is just literal characters and never a
    // SyntaxError. (Compiled in non-unicode mode via the native program.)
    assert!(Regex::new("\\p{Nonsense}", "").is_ok());
    assert!(Regex::new("\\pL", "").is_ok());
}

#[test]
fn sticky_flag() {
    // Sticky must match at exactly the start position.
    assert!(re("\\d", "y").find_from("1a", 0).is_some());
    assert!(re("\\d", "y").find_from("a1", 0).is_none());
    // Non-sticky scans forward.
    assert!(re("\\d", "").find_from("a1", 0).is_some());
    // Sticky from a later start matches only there.
    assert_eq!(re("\\d", "y").find_from("a1", 1), Some((1, 2)));
    assert!(re("abc", "y").find_from("xabc", 0).is_none());
}

#[test]
fn errors() {
    assert!(Regex::new("(unterminated", "").is_err());
    assert!(Regex::new("[abc", "").is_err());
    assert!(Regex::new("a", "z").is_err()); // unknown flag
    assert!(Regex::new("*abc", "").is_err()); // nothing to repeat
}

#[test]
fn redos_catastrophic_terminates() {
    let subject: alloc::string::String = "a".repeat(40) + "!";
    assert!(!re("(a+)+$", "").is_match(&subject));
}

#[test]
fn redos_linear_depth_terminates() {
    let subject: alloc::string::String = "a".repeat(200_000);
    assert!(re("a*", "").is_match(&subject));
    assert_eq!(
        re("a+", "").captures_from(&subject, 0).unwrap().whole().1,
        200_000
    );
}

#[test]
fn redos_zero_width_terminates() {
    assert!(re("()*", "").is_match("abc"));
    assert!(re("(a*)*", "").is_match("aaa"));
    assert!(re("(a*)*", "").is_match(""));
    assert!(re("(|a)*", "").is_match("aa"));
}

#[test]
fn compile_blowup_rejected() {
    assert!(Regex::new("a{99999999999}", "").is_err());
    assert!(Regex::new("a{5,2}", "").is_err());
    assert!(Regex::new("(a{1000}){1000}", "").is_err());
    assert!(Regex::new("a{100}", "").is_ok());
    assert!(Regex::new("a{2,4}", "").is_ok());
}

// --- UTF-16 code-unit API (`*_in_u16`) ---

/// Encodes a `&str` to UTF-16 code units for the u16 entry points.
fn u16s(s: &str) -> alloc::vec::Vec<u16> {
    s.encode_utf16().collect()
}

#[test]
fn u16_dot_non_unicode_matches_one_code_unit() {
    // "😀" is U+1F600 → two code units. Without `u`, `.` matches one code unit,
    // so the first match is length 1 and there are two matches over the string.
    let units = u16s("😀");
    assert_eq!(units.len(), 2);
    let r = re(".", "");
    let m1 = r.find_in_u16(&units, 0).unwrap();
    assert_eq!(m1, (0, 1));
    let m2 = r.find_in_u16(&units, 1).unwrap();
    assert_eq!(m2, (1, 2));
    assert!(r.find_in_u16(&units, 2).is_none());
}

#[test]
fn u16_dot_unicode_matches_astral_as_one() {
    // With `u`, `.` matches the whole astral character (a surrogate pair) as one
    // code point, but the reported span is in code-unit indices (0..2).
    let units = u16s("😀");
    let r = re(".", "u");
    let m = r.find_in_u16(&units, 0).unwrap();
    assert_eq!(m, (0, 2));
    // Only one match: after consuming both units we're at the end.
    assert!(r.find_in_u16(&units, 2).is_none());
}

#[test]
fn u16_lone_surrogate_matches() {
    // A lone high surrogate (0xD83D) is a matchable code unit in both modes.
    let units: alloc::vec::Vec<u16> = alloc::vec![0xD83D];
    assert_eq!(re(".", "").find_in_u16(&units, 0), Some((0, 1)));
    assert_eq!(re(".", "u").find_in_u16(&units, 0), Some((0, 1)));
    // A class can match the specific lone surrogate via a `\u` escape.
    let r = re(r"\uD83D", "");
    assert_eq!(r.find_in_u16(&units, 0), Some((0, 1)));
}

#[test]
fn u16_unicode_escape_astral_in_u_mode() {
    // `\u{1F600}` in `u` mode matches the astral character as one code point.
    let units = u16s("😀");
    let r = re(r"\u{1F600}", "u");
    assert_eq!(r.find_in_u16(&units, 0), Some((0, 2)));
    // The non-u engine matches it via the surrogate-pair code units too.
    let r2 = re(r"\u{1F600}", "");
    assert_eq!(r2.find_in_u16(&units, 0), Some((0, 2)));
}

#[test]
fn u16_capture_indices_are_code_unit_based() {
    // "x😀y" → units: x(1) hi(1) lo(1) y(1) = indices 0,1,2,3.
    let units = u16s("x😀y");
    assert_eq!(units.len(), 4);
    // Capture the astral char in u mode; its span is code units 1..3.
    let r = re(r"x(.)y", "u");
    let caps = r.captures_in_u16(&units, 0).unwrap();
    assert_eq!(caps.whole(), (0, 4));
    assert_eq!(caps.group(1), Some((1, 3)));
}

#[test]
fn u16_astral_quantifier_unicode() {
    // `.+` in u mode over two astral chars consumes 4 code units.
    let units = u16s("😀😁");
    assert_eq!(units.len(), 4);
    assert_eq!(re(".+", "u").find_in_u16(&units, 0), Some((0, 4)));
    // Astral class range works in u mode.
    let r = re(r"[\u{1F600}-\u{1F610}]+", "u");
    assert_eq!(r.find_in_u16(&units, 0), Some((0, 4)));
}

#[test]
fn u16_backtracking_bomb_terminates() {
    // The step budget still bounds a catastrophic pattern over the u16 API.
    let subject: alloc::string::String = "a".repeat(40) + "!";
    let units = u16s(&subject);
    assert!(re("(a+)+$", "").find_in_u16(&units, 0).is_none());
}

#[test]
fn parser_deep_nesting_rejected() {
    let pat: alloc::string::String = "(".repeat(100_000) + "a" + &")".repeat(100_000);
    assert!(Regex::new(&pat, "").is_err());
    let ok: alloc::string::String = "(".repeat(50) + "a" + &")".repeat(50);
    assert!(Regex::new(&ok, "").is_ok());
}

#[test]
fn lazy_scalar_prog_reused_is_consistent() {
    // RE-P2: the scalar adapter program is built lazily on first `&str` use; a
    // single compiled `Regex` reused across many adapter calls must keep
    // returning identical results (the `OnceCell` is filled once, not rebuilt).
    let r = re(r"(\d+)", "");
    for _ in 0..5 {
        assert_eq!(r.captures_from("a12b", 0).unwrap().whole(), (1, 3));
        assert_eq!(r.find_from("xx99", 0), Some((2, 4)));
        assert!(r.is_match("z7"));
    }

    // The native u16 path (what the interpreter uses) and the scalar adapter path
    // agree for the same reused regex, and stay consistent over repeated calls.
    let g = re(r"(\d+)", "g");
    for _ in 0..3 {
        let units = u16s("a1b22c333");
        let mut pos = 0;
        let mut found = alloc::vec::Vec::new();
        while let Some((s, e)) = g.find_in_u16(&units, pos) {
            found.push((s, e));
            pos = if e > s { e } else { e + 1 };
        }
        assert_eq!(found, alloc::vec![(1, 2), (3, 5), (6, 9)]);
    }

    // Astral scalar atomicity through the lazily-built scalar program: an astral
    // char is one atom for `.` on the `&str` adapter path.
    let dot = re(".", "");
    assert_eq!(dot.find_from("😀x", 0), Some((0, 1)));
}

#[test]
fn positive_lookahead_captures_propagate() {
    // A positive lookahead contributes the groups its sub-match captured.
    let units = u16s("123");
    let caps = re(r"(?=(\d+))", "").captures_in_u16(&units, 0).unwrap();
    assert_eq!(caps.whole(), (0, 0)); // zero-width
    assert_eq!(caps.group(1), Some((0, 3))); // captured "123"

    // The captured group is usable by a later backreference.
    let units = u16s("abcabc");
    assert!(re(r"(?=(\w{3}))\1", "").find_in_u16(&units, 0).is_some());

    // A negative lookahead contributes nothing.
    let units = u16s("ac");
    let caps = re(r"a(?!(b))", "").captures_in_u16(&units, 0).unwrap();
    assert_eq!(caps.group(1), None);
}

#[test]
fn positive_lookbehind_captures_propagate() {
    // A positive lookbehind reports the groups of the matched substring.
    let units = u16s("foobar");
    let caps = re(r"(?<=(o)(o))bar", "")
        .captures_in_u16(&units, 0)
        .unwrap();
    assert_eq!(caps.group(1), Some((1, 2)));
    assert_eq!(caps.group(2), Some((2, 3)));

    // A negative lookbehind contributes nothing.
    let units = u16s("za");
    let caps = re(r"(?<!(x))a", "").captures_in_u16(&units, 0).unwrap();
    assert_eq!(caps.group(1), None);
}

#[test]
fn lookbehind_reverse_captures() {
    // The lookbehind body matches right-to-left (ECMAScript direction -1), so a
    // quantified group captures its rightmost/leftmost iteration per the spec.
    // Cases mirror test262 built-ins/RegExp/lookBehind/captures.js.
    let grp = |pat: &str, subj: &str, g: usize| -> Option<alloc::string::String> {
        let units = u16s(subj);
        let caps = re(pat, "").captures_in_u16(&units, 0)?;
        caps.group(g)
            .map(|(s, e)| alloc::string::String::from_utf16(&units[s..e]).unwrap())
    };

    // `(?<=(\w){3})def` over "abcdef": the group's last (reverse) iteration is "a".
    assert_eq!(grp(r"(?<=(\w){3})def", "abcdef", 1).as_deref(), Some("a"));
    // Nested groups: `(?<=(\w(\w)))def` → g1 "bc", g2 "c".
    assert_eq!(grp(r"(?<=(\w(\w)))def", "abcdef", 1).as_deref(), Some("bc"));
    assert_eq!(grp(r"(?<=(\w(\w)))def", "abcdef", 2).as_deref(), Some("c"));
    // `(?<=(\w{2}))def` → "bc".
    assert_eq!(grp(r"(?<=(\w{2}))def", "abcdef", 1).as_deref(), Some("bc"));
    // Greedy loop binds from the right: `(?<=(b+))c` over "abbbbbbc".
    assert_eq!(grp(r"(?<=(b+))c", "abbbbbbc", 1).as_deref(), Some("bbbbbb"));
    assert_eq!(grp(r"(?<=(b\d+))c", "ab1234c", 1).as_deref(), Some("b1234"));
    // Backreference inside the lookbehind (reverse order): `(?<=\1(\w))d` /i.
    let units = u16s("abcCd");
    let caps = re(r"(?<=\1(\w))d", "i").captures_in_u16(&units, 0).unwrap();
    assert_eq!(
        caps.group(1)
            .map(|(s, e)| alloc::string::String::from_utf16(&units[s..e]).unwrap()),
        Some(alloc::string::String::from("C"))
    );
    // Reverse evaluation order: `(?<=(\w+)\1)c` over "ababc" → g1 "abab".
    assert_eq!(grp(r"(?<=(\w+)\1)c", "ababc", 1).as_deref(), Some("abab"));

    // Fixed-length lookbehind still works unchanged.
    assert!(
        re(r"(?<=abc)def", "")
            .captures_in_u16(&u16s("abcdef"), 0)
            .is_some()
    );
    // Lookahead nested inside a lookbehind resets to forward matching.
    assert!(
        re(r"(?<=a(?=b))b", "")
            .captures_in_u16(&u16s("ab"), 0)
            .is_some()
    );
}

#[test]
fn group_name_validation() {
    // Valid identifier-name groups compile.
    assert!(Regex::new(r"(?<a>x)", "").is_ok());
    assert!(Regex::new(r"(?<$_>x)", "").is_ok());
    assert!(Regex::new(r"(?<A>x)", "").is_ok());
    // Invalid group names are a Syntax Error.
    assert!(Regex::new(r"(?<1a>x)", "").is_err()); // digit start
    assert!(Regex::new(r"(?<a b>x)", "").is_err()); // space
    assert!(Regex::new(r"(?<>x)", "").is_err()); // empty
}

#[test]
fn duplicate_group_names() {
    // Same alternative → Syntax Error.
    assert!(Regex::new(r"(?<a>x)(?<a>y)", "").is_err());
    // Different (mutually exclusive) alternatives → allowed (ES2025).
    assert!(Regex::new(r"(?<a>x)|(?<a>y)", "").is_ok());
    assert!(Regex::new(r"(?:(?<a>x)|(?<a>y))", "").is_ok());
    // Nested in the same alternative → Syntax Error.
    assert!(Regex::new(r"(?<a>(?<a>y))", "").is_err());
}

#[test]
fn named_backreference_validation() {
    // A reference to a declared name is fine (even forward).
    assert!(Regex::new(r"(?<a>x)\k<a>", "").is_ok());
    assert!(Regex::new(r"\k<a>(?<a>x)", "").is_ok());
    // A reference to an undefined name (when named groups exist) is an error.
    assert!(Regex::new(r"(?<a>x)\k<b>", "").is_err());
    // In `u` mode any `\k<…>` requires a matching group.
    assert!(Regex::new(r"\k<a>", "u").is_err());
    // Annex B: with no named groups (non-`u`), `\k<a>` is the literal `k<a>`.
    let r = re(r"\k<a>", "");
    assert!(r.is_match("k<a>"));
    assert!(!r.is_match("xyz"));
}

#[test]
fn unicode_mode_strict_syntax() {
    // Out-of-range / legacy numeric escapes are errors under `u`.
    assert!(Regex::new(r"\1", "u").is_err());
    assert!(Regex::new(r"\8", "u").is_err());
    assert!(Regex::new(r"(a)\1", "u").is_ok());
    // Invalid identity escapes are errors under `u`, fine under Annex B.
    assert!(Regex::new(r"\M", "u").is_err());
    assert!(re(r"\M", "").is_match("M"));
    // Lone `{`, `}`, `]` are errors under `u`, literal under Annex B.
    assert!(Regex::new(r"{", "u").is_err());
    assert!(Regex::new(r"}", "u").is_err());
    assert!(Regex::new(r"]", "u").is_err());
    assert!(re(r"}", "").is_match("}"));
    // Control and character escapes remain valid under `u`.
    assert!(Regex::new(r"\cA", "u").is_ok());
    assert!(Regex::new(r"\n\t\r\f\v\0", "u").is_ok());
    // SyntaxCharacter identity escapes are valid under `u`.
    assert!(Regex::new(r"\.\*\+\?\(\)\[\]\{\}\|\^\$\\\/", "u").is_ok());
}

#[test]
fn quantifier_on_assertion() {
    // A lookbehind is never quantifiable (both modes).
    assert!(Regex::new(r"(?<=a)?b", "").is_err());
    assert!(Regex::new(r"(?<=a)?b", "u").is_err());
    assert!(Regex::new(r"(?<=a){2}b", "u").is_err());
    // A lookahead is quantifiable under Annex B (non-`u`) but not under `u`.
    assert!(Regex::new(r"(?=a)?b", "").is_ok());
    assert!(Regex::new(r"(?=a)*b", "").is_ok());
    assert!(Regex::new(r"(?=a)?b", "u").is_err());
    // `^`, `$`, `\b` are unquantifiable under `u`.
    assert!(Regex::new(r"^?a", "u").is_err());
    assert!(Regex::new(r"\b?a", "u").is_err());
    // A quantified group around an assertion is fine.
    assert!(Regex::new(r"(?:^)+", "u").is_ok());
}

#[test]
fn inline_modifier_groups() {
    // Add/remove i/m/s scoped to the group.
    assert!(re("(?i:A)", "").is_match("a"));
    assert!(!re("(?-i:A)", "i").is_match("a"));
    assert!(re("(?m:^a$)", "").is_match("x\na\ny"));
    assert!(re("(?s:.)", "").is_match("\n"));
    // `i` applies only inside the scope.
    assert!(re("(?i:a)b", "").is_match("Ab"));
    assert!(!re("(?i:a)b", "").is_match("AB"));
    // Nested modifiers: inner remove overrides outer add.
    assert!(!re("(?i:(?-i:a))", "").is_match("A"));
    assert!(re("(?i:(?-i:a))", "").is_match("a"));
    // Valid combined / single-sided forms stay accepted.
    assert!(Regex::new("(?i-m:a)", "").is_ok());
    assert!(Regex::new("(?-i:a)", "").is_ok());
    assert!(Regex::new("(?ims:a)", "").is_ok());
    // RemoveFlags after `-` may be empty: add-only with a trailing dash is valid.
    assert!(Regex::new("(?i-:a)", "").is_ok());
    // Syntax errors — each must be a *hard* error (not an `unsupported` deferral),
    // so that a regex *literal* is rejected at parse phase. `is_unsupported()` must
    // be false for every one.
    for src in [
        "(?-:a)",     // both sets empty
        "(?ii:a)",    // duplicate within add
        "(?i-mm:a)",  // duplicate within remove
        "(?ims-m:a)", // `m` in both add and remove
        "(?i-i:a)",   // single flag in both
        "(?d:a)",     // invalid flag (g/y/u/d/v/uppercase/etc.)
        "(?g:a)",
        "(?u:a)",
        "(?y:a)",
        "(?I:a)",
        "(?Q:a)",
        "(?1:a)",
        "(?i)", // modifier flags with no `:Disjunction`
        "(?ms-i)",
        "(?-s)",
        "(?i-)", // no colon
    ] {
        match Regex::new(src, "") {
            Ok(_) => panic!("{src} should be a SyntaxError"),
            Err(e) => assert!(!e.is_unsupported(), "{src} should be a hard SyntaxError"),
        }
    }
}

#[test]
fn ignore_case_unicode_word_carveout() {
    // Under `iu`, U+017F (ſ) and U+212A (K) count as word chars.
    assert!(re(r"\w", "iu").is_match("\u{017F}"));
    assert!(re(r"\w", "iu").is_match("\u{212A}"));
    // Without both flags, they do not.
    assert!(!re(r"\w", "u").is_match("\u{017F}"));
}

#[test]
fn case_insensitive_property() {
    // `\p{Lu}` matches lowercase under `i`; `\P{Lu}` matches uppercase under `i`.
    assert!(re(r"\p{Lu}", "iu").is_match("a"));
    assert!(re(r"\P{Lu}", "iu").is_match("A"));
    assert!(!re(r"\p{Lu}", "u").is_match("a"));
}

#[test]
fn v_flag_set_operations() {
    // Intersection, difference, union, nested classes.
    assert!(re("^[[0-9]&&[0-9]]+$", "v").is_match("123"));
    assert!(!re("^[[0-9]&&[a-z]]+$", "v").is_match("1"));
    assert!(re(r"^[\d--[0-5]]+$", "v").is_match("789"));
    assert!(!re(r"^[\d--[0-5]]+$", "v").is_match("3"));
    assert!(re(r"^[\p{ASCII}&&\p{L}]+$", "v").is_match("abc"));
    assert!(!re(r"^[\p{ASCII}&&\p{L}]+$", "v").is_match("a1"));
}

#[test]
fn v_flag_string_literals() {
    // `\q{…}` string alternatives, longest-match preferred.
    assert!(re(r"^[\q{abc|de}]+$", "v").is_match("abcde"));
    assert!(!re(r"^[\q{abc|de}]+$", "v").is_match("abccd"));
    assert!(re(r"^[[0-9]\q{ab}]+$", "v").is_match("ab9"));
}

#[test]
fn group_name_surrogate_pairs_non_unicode() {
    // In a non-`u` regex a group name may use `\u` surrogate pairs, `\u{…}` code
    // point escapes, or literal astral characters (all name U+1D453 `𝑓` …).
    // Previously a `\u`-surrogate-pair name was rejected as an invalid code point.
    assert!(Regex::new(r"(?<𝑓>fox)", "").is_ok());
    assert!(Regex::new(r"(?<\u{1d453}>fox)", "").is_ok());
    assert!(Regex::new("(?<\u{1d453}>fox)", "").is_ok());
    // Named backreference to an astral-named group.
    assert!(re(r"(?<𝑓>dog)(.*?)(\k<𝑓>)", "").is_match("dog eat dog"));
    // A lone `\u` surrogate that does not pair is still an invalid name.
    assert!(Regex::new(r"(?<\ud835x>fox)", "").is_err());
}

#[test]
fn v_flag_property_of_strings_keycap() {
    // `\p{Emoji_Keycap_Sequence}` matches the twelve `<base>U+FE0F U+20E3`
    // keycap strings (multi-code-point), and nothing else.
    let kc = "0\u{FE0F}\u{20E3}"; // 0️⃣
    let hashkc = "#\u{FE0F}\u{20E3}"; // #️⃣
    assert!(re(r"^\p{Emoji_Keycap_Sequence}$", "v").is_match(kc));
    assert!(re(r"^\p{Emoji_Keycap_Sequence}$", "v").is_match(hashkc));
    assert!(!re(r"^\p{Emoji_Keycap_Sequence}$", "v").is_match("0"));
    // As a class operand, combined with a string literal via union.
    let re1 = re(r"^[\p{Emoji_Keycap_Sequence}\q{0|2}]+$", "v");
    assert!(re1.is_match(kc));
    assert!(re1.is_match("0"));
    let mut combo = alloc::string::String::new();
    combo.push_str(kc);
    combo.push_str(hashkc);
    combo.push('0');
    assert!(re1.is_match(&combo));
    // Difference: `[0-9]` minus the keycap strings still matches bare digits
    // (a bare `0` is not a keycap sequence) but never a keycap sequence.
    let d = re(r"^[[0-9]--\p{Emoji_Keycap_Sequence}]+$", "v");
    assert!(d.is_match("019"));
    assert!(!d.is_match(kc));
    // Intersection with a string literal set keeps only the shared strings.
    let i = re(r"^[\p{Emoji_Keycap_Sequence}&&\q{0️⃣|zz}]+$", "v");
    assert!(i.is_match(kc));
    assert!(!i.is_match(hashkc));
}

#[test]
fn v_flag_property_of_strings_errors() {
    // A property of strings may not be negated…
    assert!(Regex::new(r"\P{Emoji_Keycap_Sequence}", "v").is_err());
    // …nor appear inside a negated class…
    assert!(Regex::new(r"[^\p{Emoji_Keycap_Sequence}]", "v").is_err());
    // …including the ones backed by the generated emoji tables.
    assert!(Regex::new(r"[^\p{RGI_Emoji}]", "v").is_err());
    assert!(Regex::new(r"\P{RGI_Emoji}", "v").is_err());
    // A well-formed, un-negated one is accepted.
    assert!(Regex::new(r"[\p{RGI_Emoji}]", "v").is_ok());
}

#[test]
fn v_flag_syntax_errors() {
    assert!(Regex::new(r"[^\q{ab}]", "v").is_err()); // negated class with string
    assert!(Regex::new("[a~~b]", "v").is_err()); // reserved double punctuator
    assert!(Regex::new("[a&&]", "v").is_err()); // trailing operator
    assert!(Regex::new("[(]", "v").is_err()); // unescaped reserved char
    assert!(Regex::new(r"[\(]", "v").is_ok()); // escaped is fine
}

#[test]
fn annexb_control_escape_fallback() {
    // Main pattern: `\c` not followed by an ASCII letter is a literal `\` then
    // `c` in non-`u` mode (Annex B), so `\c0` matches the text "\c0", not U+0010.
    assert!(re(r"\c0", "").is_match(r"\c0"));
    assert_eq!(re(r"\c0", "").find_from("\u{0f}\u{10}\u{11}", 0), None);
    // Followed by a non-ASCII letter (Cyrillic А): same literal fallback.
    assert!(re(r"\cА", "").is_match(r"\cА"));
    // Inside a class the invalid `\c` contributes BOTH `\` and `c` as members.
    let c = re(r"[\c ]", "");
    assert!(c.is_match("\\"));
    assert!(c.is_match("c"));
    // Inside a class, Annex B ClassControlLetter admits DecimalDigit and `_`:
    // `\c0` = 0x30 % 32 = 0x10, `\c_` = 0x5F % 32 = 0x1F.
    assert_eq!(
        re(r"[\c0]", "").find_from("\u{0f}\u{10}\u{11}", 0),
        Some((1, 2))
    );
    assert_eq!(
        re(r"[\c_]", "").find_from("\u{1e}\u{1f}\u{20}", 0),
        Some((1, 2))
    );
    // Letters still work in both modes.
    assert!(re(r"\cA", "").is_match("\u{1}"));
    assert!(re(r"\cA", "u").is_match("\u{1}"));
}

#[test]
fn annexb_legacy_octal_zero_prefixed() {
    // `\0` followed by octal digits is a LegacyOctalEscapeSequence (non-`u`).
    assert_eq!(re(r"\00", "").find_from("\u{0}", 0), Some((0, 1)));
    assert_eq!(re(r"\07", "").find_from("\u{7}", 0), Some((0, 1)));
    assert_eq!(re(r"\007", "").find_from("\u{7}", 0), Some((0, 1)));
    // At most three octal digits total: `\0111` = \x09 then literal '1'.
    assert!(re(r"\0111", "").is_match("\u{9}1"));
    // A bare `\0` (no following digit) is still NUL.
    assert_eq!(re(r"\0", "").find_from("\u{0}", 0), Some((0, 1)));
    // Under `u`, `\0` followed by a digit is a Syntax Error.
    assert!(Regex::new(r"\00", "u").is_err());
}

#[test]
fn annexb_class_range_shorthand_endpoint() {
    // Non-`u`: a `-` whose high endpoint is a class-escape shorthand does NOT
    // form a range (CharacterRangeOrUnion): `[%-\d]` = { '%', '-', \d }.
    assert_eq!(
        re(r"[%-\d]+", "").find_from("&%0123456789-&", 0),
        Some((1, 13))
    );
    // `[--\d]` = { '-', \d } (no '.' match).
    assert_eq!(
        re(r"[--\d]+", "").find_from(".-0123456789-.", 0),
        Some((1, 13))
    );
    // Under `u` the same pattern is a Syntax Error.
    assert!(Regex::new(r"[%-\d]", "u").is_err());
}

#[test]
fn v_flag_bare_dash_is_error() {
    // In `v` mode `-` is a ClassSetSyntaxCharacter and must be escaped: a bare
    // `[-]` is an early Syntax Error, while `[\-]` is a literal dash.
    assert!(Regex::new("[-]", "v").is_err());
    assert!(Regex::new(r"[\-]", "v").is_ok());
    // A genuine range still parses.
    assert!(Regex::new("[a-z]", "v").is_ok());
}

#[test]
fn whitespace_class_escape_is_not_unicode_white_space() {
    // `\s` is *WhiteSpace* ∪ *LineTerminator*, not the Unicode `White_Space`
    // property: U+0085 (NEL) has `White_Space` but is NOT `\s`, and U+FEFF
    // (ZWNBSP) is `\s` but does not have `White_Space`.
    assert!(!re(r"\s", "").is_match("\u{0085}"));
    assert!(re(r"\S", "").is_match("\u{0085}"));
    assert!(re(r"\s", "").is_match("\u{FEFF}"));
    assert!(!re(r"\S", "").is_match("\u{FEFF}"));
    for c in [
        '\t', '\n', '\u{b}', '\u{c}', '\r', ' ', '\u{a0}', '\u{1680}',
    ] {
        assert!(re(r"^\s$", "").is_match(&c.to_string()), "{c:?} is \\s");
    }
    for c in ['\u{2028}', '\u{2029}', '\u{202f}', '\u{205f}', '\u{3000}'] {
        assert!(re(r"^\s$", "").is_match(&c.to_string()), "{c:?} is \\s");
    }
}

#[test]
fn unicode_property_tables_match_intl_version() {
    // The generated tables in `props_data` and the `intl` crate's own tables
    // must be the same UCD version, or `\p{…}` would answer from two different
    // Unicode releases depending on which property was asked for.
    assert_eq!(
        super::props_data::UNICODE_VERSION,
        intl::unicode::UNICODE_VERSION,
    );
}

#[test]
fn unicode_property_escapes_table_backed() {
    // Spot-checks across the properties served by the generated UCD tables —
    // one member and one non-member each, taken from the property definitions.
    let cases: &[(&str, char, char)] = &[
        ("ID_Start", 'ª', '_'),
        ("ID_Continue", '_', '-'),
        ("Case_Ignorable", '\'', 'a'),
        ("Changes_When_NFKC_Casefolded", 'A', 'a'),
        ("Deprecated", 'ʼn', 'a'),
        ("Emoji", '#', 'a'),
        ("Emoji_Component", '#', 'a'),
        ("Emoji_Modifier", '\u{1F3FB}', 'a'),
        ("Emoji_Modifier_Base", '\u{261D}', 'a'),
        ("Emoji_Presentation", '\u{231A}', '#'),
        ("Extended_Pictographic", '\u{00A9}', 'a'),
        ("Extender", '·', 'a'),
        ("Grapheme_Base", 'Ό', '\u{0301}'),
        ("Grapheme_Extend", '\u{05BF}', 'a'),
        ("IDS_Binary_Operator", '\u{2FF0}', 'a'),
        ("IDS_Trinary_Operator", '\u{2FF2}', 'a'),
        ("Ideographic", '\u{16FE4}', 'a'),
        ("Logical_Order_Exception", '\u{0E40}', 'a'),
        ("Pattern_Syntax", '`', 'a'),
        ("Pattern_White_Space", '\u{200E}', 'a'),
        ("Radical", '\u{2E80}', 'a'),
        ("Sentence_Terminal", '!', 'a'),
        ("Soft_Dotted", 'į', 'a'),
        ("Terminal_Punctuation", '!', 'a'),
        ("Unified_Ideograph", '\u{FA11}', 'a'),
    ];
    for &(name, yes, no) in cases {
        let r = re(&format!(r"^\p{{{name}}}$"), "u");
        assert!(
            r.is_match(&yes.to_string()),
            "\\p{{{name}}} should match {yes:?}"
        );
        assert!(
            !r.is_match(&no.to_string()),
            "\\p{{{name}}} should not match {no:?}"
        );
    }
}

#[test]
fn emoji_property_of_strings_tables() {
    // `RGI_Emoji` is the union of the six component sequence properties, and a
    // `v`-mode class matches the whole sequence (longest-first).
    assert_eq!(super::props_emoji::EMOJI_VERSION, (17, 0));
    assert!(re(r"^\p{RGI_Emoji}$", "v").is_match("\u{1F1E8}\u{1F1F6}"));
    assert!(re(r"^\p{RGI_Emoji}$", "v").is_match("\u{1F426}\u{200D}\u{2B1B}"));
    assert!(re(r"^\p{Basic_Emoji}$", "v").is_match("\u{231A}"));
    assert!(!re(r"^\p{Basic_Emoji}$", "v").is_match("a"));
    assert!(re(r"^\p{RGI_Emoji_Flag_Sequence}$", "v").is_match("\u{1F1E8}\u{1F1F6}"));
    assert!(re(r"^\p{Emoji_Keycap_Sequence}$", "v").is_match("#\u{FE0F}\u{20E3}"));
    assert!(re(r"^\p{RGI_Emoji_Modifier_Sequence}$", "v").is_match("\u{261D}\u{1F3FB}"));
    assert!(
        re(r"^\p{RGI_Emoji_Tag_Sequence}$", "v")
            .is_match("\u{1F3F4}\u{E0067}\u{E0062}\u{E0065}\u{E006E}\u{E0067}\u{E007F}")
    );
    // A property of strings may not be negated, nor used outside `v` mode.
    assert!(Regex::new(r"\P{RGI_Emoji}", "v").is_err());
    assert!(Regex::new(r"\p{RGI_Emoji}", "u").is_err());
}