rusty_expressions 0.2.0

Oniguruma remade in pure Rust: named groups, look-around, backreferences, subexp calls, absent expressions, callouts, per-regex encodings (UTF-8/16/32, Shift_JIS, Big5, EUC-*, GB18030, ISO-8859-*) and Perl/Python/Java/POSIX/GNU/Emacs/grep syntax dialects. Match-equivalent to Oniguruma 6.9.10 and ~3x faster than libonig. no_std + alloc with default-features = false, runs on wasm32, no C toolchain.
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
//! rusty_expressions tests: unit + harvested Oniguruma vectors.
//!
//! The `compat` test drives the C ABI, which is unsafe by construction.
#![cfg_attr(feature = "compat", allow(unsafe_code))]

use std::fs;
use std::path::PathBuf;

use rusty_expressions::{
    find_all_str, is_match_str, Encoding, ErrorKind, MatchParam, Options, Regex, Syntax,
};

#[test]
fn compile_search_basic() {
    let re = Regex::new("ca+t", Options::NONE, Encoding::UTF8, Syntax::ONIGURUMA).unwrap();
    assert!(re.is_match(b"caaat").unwrap());
    let m = re.search(b"one cat two").unwrap().expect("match");
    assert_eq!(m.range(), 4..7);
}

#[test]
fn stress_plus_and_literal_spans() {
    let plus = Regex::new_str("a+", Options::NONE, Syntax::ONIGURUMA).unwrap();
    let hay = "a".repeat(80) + "b";
    let m = plus.search(hay.as_bytes()).unwrap().unwrap();
    assert_eq!(m.range(), 0..80);
    let lit = Regex::new_str("needle", Options::NONE, Syntax::ONIGURUMA).unwrap();
    let hay = format!("{}needle{}", "x".repeat(400), "y".repeat(400));
    let m = lit.search(hay.as_bytes()).unwrap().unwrap();
    assert_eq!(m.range(), 400..406);
}

#[test]
fn numbered_captures() {
    let re = Regex::new("(ca)+t", Options::NONE, Encoding::UTF8, Syntax::ONIGURUMA).unwrap();
    let m = re.search(b"cacat").unwrap().unwrap();
    assert_eq!(m.range(), 0..5);
    assert_eq!(m.get(1), Some(2..4));
}

#[test]
fn retry_limit_errors() {
    let re = Regex::new("(a+)+b", Options::NONE, Encoding::UTF8, Syntax::ONIGURUMA).unwrap();
    let mut p = MatchParam::default();
    p.retry_limit_in_match = 8;
    let r = re.search_param(b"aaaaaaaaac", &p);
    assert!(r.is_err(), "hostile match must hit retry limit, got {r:?}");
}

#[test]
fn ops_are_callable() {
    assert!(is_match_str("ca+t", "caaat").unwrap());
    let hits = find_all_str("ca+t", "one cat two caaat").unwrap();
    assert_eq!(hits.len(), 2);
    assert_eq!(hits[0], 4..7);
}

#[test]
fn utf8_multibyte() {
    let re = Regex::new_str("a.b", Options::NONE, Syntax::ONIGURUMA).unwrap();
    let hay = "a\u{00e9}b";
    let m = re.search(hay.as_bytes()).unwrap().unwrap();
    assert_eq!(m.range(), 0..hay.len());
}

#[test]
fn lookaround_and_named() {
    let re = Regex::new_str(r"(?<=foo)bar", Options::NONE, Syntax::ONIGURUMA).unwrap();
    assert!(re.is_match(b"foobar").unwrap());
    assert!(!re.is_match(b"bar").unwrap());
    let re = Regex::new_str(r"(?<n>ca+)t", Options::NONE, Syntax::ONIGURUMA).unwrap();
    let m = re.search(b"caaat").unwrap().unwrap();
    assert_eq!(m.name("n"), Some(0..4));
}

#[test]
fn possessive_and_atomic() {
    let re = Regex::new_str(r"a*+a", Options::NONE, Syntax::ONIGURUMA).unwrap();
    assert!(!re.is_match(b"aaa").unwrap());
    let re = Regex::new_str(r"(?>a*)a", Options::NONE, Syntax::ONIGURUMA).unwrap();
    assert!(!re.is_match(b"aaa").unwrap());
}

#[test]
fn backref_and_call() {
    let re = Regex::new_str(r"(cat|dog)\1", Options::NONE, Syntax::ONIGURUMA).unwrap();
    assert!(re.is_match(b"catcat").unwrap());
    assert!(!re.is_match(b"catdog").unwrap());
}

#[test]
fn scan_and_regset() {
    let re = Regex::new_str(r"\w+", Options::NONE, Syntax::ONIGURUMA).unwrap();
    let hits = rusty_expressions::scan(&re, b"a bb ccc", &MatchParam::default()).unwrap();
    assert_eq!(hits.len(), 3);
    let a = Regex::new_str("cat", Options::NONE, Syntax::ONIGURUMA).unwrap();
    let b = Regex::new_str("dog", Options::NONE, Syntax::ONIGURUMA).unwrap();
    let set = rusty_expressions::RegSet::new(vec![a, b]).unwrap();
    let (i, r) = set.search(b"a dog", &MatchParam::default()).unwrap().unwrap();
    assert_eq!(i, 1);
    assert_eq!(r.range(), 2..5);
}

#[test]
fn encodings_utf16_and_latin1() {
    let re = Regex::new(b"a.b", Options::NONE, Encoding::ASCII, Syntax::ONIGURUMA).unwrap();
    assert!(re.is_match(b"axb").unwrap());
    let mut buf = [0u8; 8];
    buf[0] = 0;
    buf[1] = b'a';
    buf[2] = 0;
    buf[3] = b'x';
    buf[4] = 0;
    buf[5] = b'b';
    let re = Regex::new(
        &[0, b'a', 0, b'.', 0, b'b'],
        Options::NONE,
        Encoding::UTF16_BE,
        Syntax::ONIGURUMA,
    )
    .unwrap();
    assert!(re.is_match(&buf[..6]).unwrap());
}

#[test]
fn syntax_python_named() {
    let re = Regex::new_str("(?P<n>foo)", Options::NONE, Syntax::python()).unwrap();
    let m = re.search(b"foo").unwrap().unwrap();
    assert_eq!(m.name("n"), Some(0..3));
}

#[test]
fn callout_skip_and_user_prop() {
    // (*SKIP) succeeds going forward; it only redirects where the SEARCH
    // resumes if the whole attempt at this start fails. libonig matches 0..2.
    let re = Regex::new_str(r"a(*SKIP)b", Options::NONE, Syntax::ONIGURUMA).unwrap();
    assert_eq!(re.search(b"ab").unwrap().unwrap().range(), 0..2);
    // On failure the retry happens AT the skip point, not past it.
    let re = Regex::new_str(r"[ab](*SKIP)c", Options::NONE, Syntax::ONIGURUMA).unwrap();
    assert_eq!(re.search(b"abc").unwrap().unwrap().range(), 1..3);
    // A sibling alternative at the same start is still tried.
    let re = Regex::new_str(r"(?:a(*SKIP)x|ab)", Options::NONE, Syntax::ONIGURUMA).unwrap();
    assert_eq!(re.search(b"ab").unwrap().unwrap().range(), 0..2);
    // (*FAIL) / (*MISMATCH) are built in and fail where they stand.
    let re = Regex::new_str(r"a(*FAIL)b", Options::NONE, Syntax::ONIGURUMA).unwrap();
    assert!(!re.is_match(b"ab").unwrap());
    let re = Regex::new_str(r"(?:a(*MISMATCH)|b)", Options::NONE, Syntax::ONIGURUMA).unwrap();
    assert_eq!(re.search(b"ab").unwrap().unwrap().range(), 1..2);
    let mut param = MatchParam::default();
    param.named_callout = Some(|ctx| {
        if ctx.name == "FAIL" {
            rusty_expressions::CalloutResult::Fail
        } else {
            rusty_expressions::CalloutResult::Success
        }
    });
    let re = Regex::new_str(r"a(*FAIL)b|ab", Options::NONE, Syntax::ONIGURUMA).unwrap();
    let m = re.search_param(b"ab", &param).unwrap();
    assert_eq!(m.unwrap().range(), 0..2);
    let mut re = Regex::new_str(r"\p{Vowel}", Options::NONE, Syntax::ONIGURUMA).unwrap();
    re.define_user_property(rusty_expressions::UserProperty {
        name: String::from("Vowel"),
        ranges: vec![(b'a' as u32, b'a' as u32), (b'e' as u32, b'e' as u32)],
    });
    assert!(re.is_match(b"a").unwrap());
    assert!(!re.is_match(b"b").unwrap());
}

#[test]
fn expressions_demo_op() {
    let hay = "one cat two caaat";
    assert!(is_match_str("ca+t", hay).unwrap());
    let hits = find_all_str("ca+t", hay).unwrap();
    let formatted = rusty_expressions::format_matches(hay, &hits);
    assert!(formatted.contains("4..7 cat"));
    assert!(formatted.contains("12..17 caaat"));
}

#[test]
fn phase2_oniguruma_utf8() {
    let re = |p: &str| Regex::new_str(p, Options::NONE, Syntax::ONIGURUMA).unwrap();
    assert_eq!(re("foo(?=bar)").search(b"foobar").unwrap().unwrap().range(), 0..3);
    assert!(re("foo(?=bar)").search(b"foo").unwrap().is_none());
    assert_eq!(re("foo(?!bar)").search(b"foox").unwrap().unwrap().range(), 0..3);
    assert_eq!(re("(?<=foo)bar").search(b"foobar").unwrap().unwrap().range(), 3..6);
    assert!(re("(?<!foo)bar").search(b"xbar").unwrap().is_some());
    assert!(!re("(?<!foo)bar").is_match(b"foobar").unwrap());
    assert!(!re("a*+a").is_match(b"aaa").unwrap());
    assert!(!re("(?>a*)a").is_match(b"aaa").unwrap());
    let m = re("(?<n>ca+)t").search(b"caaat").unwrap().unwrap();
    assert_eq!(m.name("n"), Some(0..4));
    assert!(re(r"(?<n>cat)\k<n>").is_match(b"catcat").unwrap());
    assert!(!re(r"(?<n>cat)\k<n>").is_match(b"catdog").unwrap());
    assert!(re(r"(a)\g<1>").is_match(b"aa").unwrap());
    assert_eq!(re("(?~abc)").search(b"xxabc").unwrap().unwrap().range(), 0..2);
    assert_eq!(re("(a)?(?(1)a|b)").search(b"aa").unwrap().unwrap().range(), 0..2);
    assert_eq!(re("(a)?(?(1)a|b)").search(b"b").unwrap().unwrap().range(), 0..1);
    assert!(re(r"\p{L}+").is_match(b"Ab").unwrap());
    assert!(re(r"\p{Lu}").is_match(b"A").unwrap());
    assert!(!re(r"\p{Lu}").is_match(b"a").unwrap());
    assert!(re(r"\p{Latin}+").is_match("cafe".as_bytes()).unwrap());
    assert!(re(r"[[:digit:]]+").is_match(b"42").unwrap());
    let re_i = Regex::new_str("foo", Options::IGNORECASE, Syntax::ONIGURUMA).unwrap();
    assert!(re_i.is_match(b"FOO").unwrap());
    let re_i = Regex::new_str("(?i)foo", Options::NONE, Syntax::ONIGURUMA).unwrap();
    assert!(re_i.is_match(b"FoO").unwrap());
    assert!(re(r"a\Kb").search(b"ab").unwrap().unwrap().range() == (1..2));
    assert!(re(r"\d+\D+\d+").is_match(b"12x34").unwrap());
    assert_eq!(re(r"\R").search(b"a\r\nb").unwrap().unwrap().range(), 1..3);
    assert!(re(r"\h+").is_match(b"aF").unwrap());
    assert!(!re(r"\h+").is_match(b"G").unwrap());
    assert!(re(r"\X").is_match("e\u{0301}".as_bytes()).unwrap());
    let re_l = Regex::new_str("a+|aa", Options::FIND_LONGEST, Syntax::ONIGURUMA).unwrap();
    assert_eq!(re_l.search(b"aaa").unwrap().unwrap().range(), 0..3);
    let re_w = Regex::new_str("cat", Options::MATCH_WHOLE_STRING, Syntax::ONIGURUMA).unwrap();
    assert!(re_w.search(b"cat").unwrap().is_some());
    assert!(re_w.search(b"xcat").unwrap().is_none());
    assert!(re(r"a{2,4}").is_match(b"aaa").unwrap());
    assert_eq!(re("xa{3}?").search(b"x").unwrap().unwrap().range(), 0..1);
    assert!(re(r"\Gcat").find_at(b"xcat", 1).unwrap().is_some());
    assert!(re(r"\Gcat").search(b"xcat").unwrap().is_none());
    assert!(re(r"\O").is_match(b"\n").unwrap());
    assert!(!re(r"\N").is_match(b"\n").unwrap());
    let re_ne = Regex::new_str("a*", Options::FIND_NOT_EMPTY, Syntax::ONIGURUMA).unwrap();
    assert!(re_ne.search(b"bbb").unwrap().is_none());
    let re_dc = Regex::new_str("(a)", Options::DONT_CAPTURE_GROUP, Syntax::ONIGURUMA).unwrap();
    assert_eq!(re_dc.capture_count(), 1);
}

#[test]
fn phase3_dialects_encodings_scan_set() {
    let asis = Regex::new_str(r"\d", Options::NONE, Syntax::ASIS).unwrap();
    assert!(asis.is_match(br"\d").unwrap());
    assert!(!asis.is_match(b"4").unwrap());
    let py = Regex::new_str("(?P<n>foo)", Options::NONE, Syntax::python()).unwrap();
    assert_eq!(py.search(b"foo").unwrap().unwrap().name("n"), Some(0..3));
    let perl = Regex::new_str(r"(?i)FOO", Options::NONE, Syntax::perl()).unwrap();
    assert!(perl.is_match(b"foo").unwrap());
    let java = Regex::new_str(r"\u0041", Options::NONE, Syntax::java()).unwrap();
    assert!(java.is_match(b"A").unwrap());
    let posix = Regex::new_str("a+", Options::NONE, Syntax::posix_extended()).unwrap();
    assert!(posix.is_match(b"aaa").unwrap());
    let gnu = Regex::new_str(r"\<cat\>", Options::NONE, Syntax::gnu_regex()).unwrap();
    assert!(gnu.is_match(b"a cat b").unwrap());
    let emacs = Regex::new_str(r"foo\|bar", Options::NONE, Syntax::emacs()).unwrap();
    assert!(emacs.is_match(b"bar").unwrap());
    let grep = Regex::new_str(r"foo\|bar", Options::NONE, Syntax::grep()).unwrap();
    assert!(grep.is_match(b"bar").unwrap());
    let perl_ng = Regex::new_str("(?<n>foo)", Options::NONE, Syntax::perl_ng()).unwrap();
    assert_eq!(perl_ng.search(b"foo").unwrap().unwrap().name("n"), Some(0..3));
    let posix_b = Regex::new_str(r"a\{1,3\}", Options::NONE, Syntax::posix_basic()).unwrap();
    assert!(posix_b.is_match(b"aa").unwrap());
    let sql = Regex::new_str("a%c", Options::NONE, rusty_expressions::sql_syntax()).unwrap();
    assert!(sql.is_match(b"axxxc").unwrap());
    let iso = Regex::new(b"a.b", Options::NONE, Encoding::ISO_8859_1, Syntax::ONIGURUMA).unwrap();
    assert!(iso.is_match(&[b'a', 0xe9, b'b']).unwrap());
    let u32be = Regex::new(
        &[0, 0, 0, b'a', 0, 0, 0, b'.', 0, 0, 0, b'b'],
        Options::NONE,
        Encoding::UTF32_BE,
        Syntax::ONIGURUMA,
    )
    .unwrap();
    assert!(u32be
        .is_match(&[0, 0, 0, b'a', 0, 0, 0, b'x', 0, 0, 0, b'b'])
        .unwrap());
    let sjis = Regex::new(b"ab", Options::NONE, Encoding::SJIS, Syntax::ONIGURUMA).unwrap();
    assert!(sjis.is_match(b"ab").unwrap());
    assert_eq!(Encoding::EUC_JP.name(), "EUC-JP");
    assert_eq!(Encoding::GB18030.name(), "GB18030");
    assert_eq!(Encoding::BIG5.name(), "Big5");
    assert_eq!(Encoding::KOI8_R.name(), "KOI8-R");
    assert_eq!(Encoding::CP1251.name(), "CP1251");
    let mut syn = Syntax::ONIGURUMA;
    syn.set_meta_char(1, b'_' as u32);
    assert_eq!(syn.meta_anychar, b'_' as u32);
}

type Vector = (String, String, Option<(usize, usize)>, Vec<Option<(usize, usize)>>);

fn load_vectors(name: &str) -> Vec<Vector> {
    let path = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
        .join("tests/data/oniguruma")
        .join(name);
    let raw = fs::read_to_string(&path).unwrap_or_else(|_| panic!("missing {name}"));
    parse_json_vectors(&raw)
}

fn load_phase0() -> Vec<Vector> {
    load_vectors("phase0.json")
}

fn parse_json_vectors(raw: &str) -> Vec<Vector> {
    let mut out = Vec::new();
    for obj in json_objects(raw) {
        let id_pat = field(&obj, "pattern");
        let hay = field(&obj, "hay");
        if id_pat.is_empty() {
            continue;
        }
        if obj.contains("\"mismatch\": true") {
            out.push((id_pat, hay, None, Vec::new()));
        } else {
            let start = num_field(&obj, "start").unwrap_or(0);
            let end = num_field(&obj, "end").unwrap_or(0);
            out.push((id_pat, hay, Some((start, end)), captures_field(&obj)));
        }
    }
    out
}

fn json_objects(raw: &str) -> Vec<String> {
    let mut out = Vec::new();
    let bytes = raw.as_bytes();
    let mut i = 0;
    while i < bytes.len() {
        if bytes[i] != b'{' {
            i += 1;
            continue;
        }
        let start = i;
        let mut depth = 0;
        let mut in_str = false;
        let mut esc = false;
        while i < bytes.len() {
            let b = bytes[i];
            if in_str {
                if esc {
                    esc = false;
                } else if b == b'\\' {
                    esc = true;
                } else if b == b'"' {
                    in_str = false;
                }
            } else if b == b'"' {
                in_str = true;
            } else if b == b'{' {
                depth += 1;
            } else if b == b'}' {
                depth -= 1;
                if depth == 0 {
                    out.push(raw[start..=i].to_string());
                    i += 1;
                    break;
                }
            }
            i += 1;
        }
    }
    out
}

fn field(obj: &str, key: &str) -> String {
    let needle = format!("\"{key}\": \"");
    if let Some(i) = obj.find(&needle) {
        let s = &obj[i + needle.len()..];
        let mut out = String::new();
        let mut chars = s.chars();
        while let Some(c) = chars.next() {
            if c == '\\' {
                if let Some(n) = chars.next() {
                    match n {
                        'n' => out.push('\n'),
                        't' => out.push('\t'),
                        'r' => out.push('\r'),
                        '\\' => out.push('\\'),
                        '"' => out.push('"'),
                        // \uXXXX: without this an escaped codepoint silently
                        // decayed to the literal text "uXXXX".
                        'u' => {
                            let hex: String = (0..4).filter_map(|_| chars.next()).collect();
                            match u32::from_str_radix(&hex, 16).ok().and_then(char::from_u32) {
                                Some(ch) => out.push(ch),
                                None => panic!("bad unicode escape in fixture: {hex:?}"),
                            }
                        }
                        other => out.push(other),
                    }
                }
            } else if c == '"' {
                break;
            } else {
                out.push(c);
            }
        }
        return out;
    }
    String::new()
}

/// Parse `"captures": [[a, b], null, ...]` into group 1.. ranges.
fn captures_field(obj: &str) -> Vec<Option<(usize, usize)>> {
    let needle = "\"captures\": [";
    let Some(i) = obj.find(needle) else {
        return Vec::new();
    };
    let rest = &obj[i + needle.len()..];
    let end = match rest.find(']') {
        Some(_) => {
            // find the matching close of the outer array
            let bytes = rest.as_bytes();
            let mut depth = 1i32;
            let mut k = 0usize;
            while k < bytes.len() {
                match bytes[k] {
                    b'[' => depth += 1,
                    b']' => {
                        depth -= 1;
                        if depth == 0 {
                            break;
                        }
                    }
                    _ => {}
                }
                k += 1;
            }
            k
        }
        None => return Vec::new(),
    };
    let body = &rest[..end];
    let mut out = Vec::new();
    let mut i = 0usize;
    let b = body.as_bytes();
    while i < b.len() {
        match b[i] {
            b'[' => {
                let close = match body[i..].find(']') {
                    Some(c) => i + c,
                    None => break,
                };
                let nums: Vec<usize> = body[i + 1..close]
                    .split(',')
                    .filter_map(|t| t.trim().parse::<usize>().ok())
                    .collect();
                out.push(if nums.len() == 2 {
                    Some((nums[0], nums[1]))
                } else {
                    None
                });
                i = close + 1;
            }
            b'n' => {
                out.push(None);
                i += 4;
            }
            _ => i += 1,
        }
    }
    out
}

fn num_field(obj: &str, key: &str) -> Option<usize> {
    let needle = format!("\"{key}\": ");
    let i = obj.find(&needle)?;
    let s = &obj[i + needle.len()..];
    s.chars()
        .take_while(|c| c.is_ascii_digit())
        .collect::<String>()
        .parse()
        .ok()
}

#[test]
fn harvested_phase0_vectors() {
    let vecs = load_phase0();
    assert!(!vecs.is_empty(), "expected harvested vectors");
    for (pat, hay, expect, caps) in vecs {
        check_vector("phase0.json", &pat, &hay, expect, &caps);
    }
}

/// Compare one harvested vector, captures included.
///
/// Checking only `range()` here is what let a `\g<>` capture bug sit green in
/// this suite while the side-by-side bench reported it.
fn check_vector(
    name: &str,
    pat: &str,
    hay: &str,
    expect: Option<(usize, usize)>,
    caps: &[Option<(usize, usize)>],
) {
    let re = Regex::new(pat.as_bytes(), Options::NONE, Encoding::UTF8, Syntax::ONIGURUMA)
        .unwrap_or_else(|e| panic!("{name} compile {pat:?}: {e}"));
    let got = re
        .search(hay.as_bytes())
        .unwrap_or_else(|e| panic!("{name} search {pat:?}: {e}"));
    match (expect, got) {
        (None, None) => {}
        (Some((s, e)), Some(r)) => {
            assert_eq!(r.range(), s..e, "{name} pattern {pat:?} hay {hay:?}");
            for (k, want) in caps.iter().enumerate() {
                let group = k + 1;
                let have = r.get(group).map(|g| (g.start, g.end));
                assert_eq!(
                    have, *want,
                    "{name} pattern {pat:?} hay {hay:?} group {group}"
                );
            }
        }
        (expect, got) => {
            panic!("{name} pattern {pat:?} hay {hay:?} expect {expect:?} got {got:?}")
        }
    }
}

fn assert_harvested(name: &str) {
    let vecs = load_vectors(name);
    assert!(!vecs.is_empty(), "expected harvested vectors in {name}");
    for (pat, hay, expect, caps) in vecs {
        check_vector(name, &pat, &hay, expect, &caps);
    }
}

#[test]
fn harvested_phase2_vectors() {
    assert_harvested("phase2.json");
}

#[test]
fn harvested_phase3_vectors() {
    assert_harvested("phase3.json");
}

#[test]
fn capture_history_tree() {
    let re = Regex::new_str("(?@(ab|c))+", Options::NONE, Syntax::ONIGURUMA).unwrap();
    let m = re.search(b"abcab").unwrap().expect("match");
    let tree = m.history.as_ref().expect("capture history");
    assert_eq!(tree.group, 0);
    assert_eq!(tree.range, 0..5);
    let mut groups = Vec::new();
    m.traverse_history(|n, d| groups.push((n.group, n.range.clone(), d)));
    assert!(groups.iter().any(|(g, r, d)| *g == 1 && *r == (0..2) && *d == 1));
    assert!(groups.iter().any(|(g, _, d)| *g == 1 && *d >= 1));
    assert!(groups.iter().filter(|(g, _, _)| *g == 1).count() >= 2);
}

#[test]
fn left_recursive_g_is_compile_error() {
    let err = Regex::new_str(r"(?<a>\g<a>|x)", Options::NONE, Syntax::ONIGURUMA).unwrap_err();
    assert_eq!(err.kind, ErrorKind::NeverEndingRecursion);
    let err = Regex::new_str(r"(\g<1>)", Options::NONE, Syntax::ONIGURUMA).unwrap_err();
    assert_eq!(err.kind, ErrorKind::NeverEndingRecursion);
    assert!(Regex::new_str(r"(a)\g<1>", Options::NONE, Syntax::ONIGURUMA).is_ok());
    assert!(Regex::new_str(r"(?<a>x\g<a>)", Options::NONE, Syntax::ONIGURUMA).is_ok());
}

#[test]
fn unicode_16_unassigned_is_cn_not_assigned() {
    let assigned = Regex::new_str(r"^\p{Assigned}$", Options::NONE, Syntax::ONIGURUMA).unwrap();
    let cn = Regex::new_str(r"^\p{Cn}$", Options::NONE, Syntax::ONIGURUMA).unwrap();
    let lu = Regex::new_str(r"^\p{Lu}$", Options::NONE, Syntax::ONIGURUMA).unwrap();
    let unassigned = "\u{0378}";
    assert!(!assigned.is_match(unassigned.as_bytes()).unwrap());
    assert!(cn.is_match(unassigned.as_bytes()).unwrap());
    assert!(assigned.is_match(b"A").unwrap());
    assert!(lu.is_match(b"A").unwrap());
    assert!(!lu.is_match(unassigned.as_bytes()).unwrap());
}

#[test]
fn text_segment_word_mode() {
    let grapheme = Regex::new_str(r"\X", Options::NONE, Syntax::ONIGURUMA).unwrap();
    assert_eq!(grapheme.search(b"hello").unwrap().unwrap().range(), 0..1);
    let word = Regex::new(
        r"\X",
        Options::TEXT_SEGMENT_WORD,
        Encoding::UTF8,
        Syntax::ONIGURUMA,
    )
    .unwrap();
    assert_eq!(word.search(b"hello world").unwrap().unwrap().range(), 0..5);
    let bound = Regex::new(
        r"\y",
        Options::TEXT_SEGMENT_WORD,
        Encoding::UTF8,
        Syntax::ONIGURUMA,
    )
    .unwrap();
    assert_eq!(bound.search(b"hello").unwrap().unwrap().range(), 0..0);
}

#[test]
fn count_callout_persists_on_match_param() {
    let re = Regex::new_str("a(*COUNT)", Options::NONE, Syntax::ONIGURUMA).unwrap();
    let p = MatchParam::default();
    assert!(re.search_param(b"a", &p).unwrap().is_some());
    assert!(re.search_param(b"a", &p).unwrap().is_some());
    assert_eq!(p.count.get(), 2);
    assert!(re.search_param(b"b", &p).unwrap().is_none());
    assert_eq!(p.count.get(), 2);
}

#[test]
fn east_asian_unicode_round_trip() {
    let mut buf = [0u8; 8];
    let sjis_a = [0x82, 0xa0];
    assert_eq!(Encoding::SJIS.mbc_to_code(&sjis_a).unwrap(), 0x3042);
    let n = Encoding::SJIS.code_to_mbc(0x3042, &mut buf).unwrap();
    assert_eq!(&buf[..n], &sjis_a);
    let euc_a = [0xa4, 0xa2];
    assert_eq!(Encoding::EUC_JP.mbc_to_code(&euc_a).unwrap(), 0x3042);
    let n = Encoding::EUC_JP.code_to_mbc(0x3042, &mut buf).unwrap();
    assert_eq!(&buf[..n], &euc_a);
    let big5_zhong = [0xa4, 0xa4];
    assert_eq!(Encoding::BIG5.mbc_to_code(&big5_zhong).unwrap(), 0x4e2d);
    let n = Encoding::BIG5.code_to_mbc(0x4e2d, &mut buf).unwrap();
    assert_eq!(&buf[..n], &big5_zhong);
    let gb_zhong = [0xd6, 0xd0];
    assert_eq!(Encoding::GB18030.mbc_to_code(&gb_zhong).unwrap(), 0x4e2d);
    let n = Encoding::GB18030.code_to_mbc(0x4e2d, &mut buf).unwrap();
    assert_eq!(&buf[..n], &gb_zhong);
    let re = Regex::new(&sjis_a, Options::NONE, Encoding::SJIS, Syntax::ONIGURUMA).unwrap();
    assert!(re.is_match(&sjis_a).unwrap());
}

#[cfg(feature = "compat")]
#[test]
fn onig_c_abi_new_search() {
    use std::ptr;
    use rusty_expressions::compat::*;
    let pat = b"ca+t";
    let mut reg = ptr::null_mut();
    let rc = unsafe {
        onig_new(
            &mut reg,
            pat.as_ptr(),
            pat.as_ptr().add(pat.len()),
            0,
            ONIG_ENCODING_UTF8,
            ONIG_SYNTAX_ONIGURUMA,
            ptr::null_mut(),
        )
    };
    assert_eq!(rc, ONIG_NORMAL);
    let hay = b"one cat two";
    let region = unsafe { onig_region_new() };
    let pos = unsafe {
        onig_search(
            reg,
            hay.as_ptr(),
            hay.as_ptr().add(hay.len()),
            hay.as_ptr(),
            hay.as_ptr().add(hay.len()),
            region,
            0,
        )
    };
    assert_eq!(pos, 4);
    unsafe {
        assert!(!(*region).beg.is_null());
        assert_eq!(*(*region).beg, 4);
        assert_eq!(*(*region).end, 7);
        onig_region_free(region, 1);
        onig_free(reg);
    }
}

#[test]
fn deferred_gaps_are_named() {
    let path = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
        .join("tests/data/oniguruma/deferred.txt");
    let raw = fs::read_to_string(&path).expect("deferred.txt");
    let lines: Vec<&str> = raw
        .lines()
        .map(str::trim)
        .filter(|l| !l.is_empty() && !l.starts_with('#'))
        .collect();
    assert!(
        lines.is_empty(),
        "named Oniguruma gaps must be closed; leftover {lines:?}"
    );
}

// ---------------------------------------------------------------------------
// Oracle-swap regressions
//
// Each of these was a difference against live libonig found by
// `tools/onig-bench --example oracle_audit`, which took the audit from 189
// differences to 12. They are pinned here so the fix survives without a C
// library to compare against: the audit only runs where libonig builds, these
// run everywhere.
// ---------------------------------------------------------------------------

/// POSIX BRE spells a group `\(...\)`; the bare parens are literals.
///
/// `ESC_LPAREN_SUBEXP` was in the flag tables but nothing in the parser ever
/// read it, so `\(a\)` compiled to the literal text `(a)`.
#[test]
fn bre_escaped_parens_are_groups() {
    for syn in [Syntax::posix_basic(), Syntax::grep(), Syntax::emacs()] {
        let re = Regex::new_str(r"\(a\)", Options::NONE, syn).unwrap();
        let m = re.search(b"ab").unwrap().expect("group matches 'a'");
        assert_eq!(m.range(), 0..1);
        assert_eq!(m.get(1), Some(0..1));
    }
    // ...and a bare paren stays a literal in the same dialect.
    let re = Regex::new_str("(a)", Options::NONE, Syntax::posix_basic()).unwrap();
    assert_eq!(re.search(b"(a)").unwrap().unwrap().range(), 0..3);
}

/// Where `?` is not the non-greedy marker it is a second quantifier.
///
/// POSIX ERE and Emacs read `a*?` as `(a*)?`. We used to leave the `?`
/// unconsumed, so it fell through to `parse_atom` and became a literal `?` --
/// making `a*?b` match only haystacks that contained one.
#[test]
fn stacked_quantifier_where_qmark_is_not_lazy() {
    for syn in [Syntax::posix_extended(), Syntax::gnu_regex(), Syntax::emacs()] {
        let re = Regex::new_str("a*?b", Options::NONE, syn).unwrap();
        assert_eq!(re.search(b"aab").unwrap().expect("matches").range(), 0..3);
        assert_eq!(re.search(b"ab").unwrap().expect("matches").range(), 0..2);
    }
    // Perl-family dialects still read it as a lazy star, not a stack.
    let re = Regex::new_str("a*?b", Options::NONE, Syntax::perl()).unwrap();
    assert_eq!(re.search(b"aab").unwrap().unwrap().range(), 0..3);
}

/// An undecodable UTF-8 byte is one character, and matches no character class.
///
/// Oniguruma does not refuse malformed input: `mbc_enc_len` reports 1 and
/// `USE_INVALID_CODE_SCHEME` lifts the byte above every real codepoint. We
/// used to error, returning no match where libonig returns one -- and then,
/// after reporting the raw byte instead, called 0xA0 whitespace.
#[test]
fn invalid_utf8_byte_is_one_character() {
    let dot = Regex::new_str(".", Options::NONE, Syntax::ONIGURUMA).unwrap();
    let m = dot.search(&[0x82, 0xa0, 0x61]).unwrap().expect("matches");
    assert_eq!(m.range(), 0..1);

    let two = Regex::new_str("..", Options::NONE, Syntax::ONIGURUMA).unwrap();
    assert_eq!(two.search(&[0x82, 0xa0, 0x61]).unwrap().unwrap().range(), 0..2);

    // 0xA0 is NOT whitespace: it never decoded to U+00A0 in the first place.
    let sp = Regex::new_str(r"\s+", Options::NONE, Syntax::ONIGURUMA).unwrap();
    assert_eq!(sp.search(&[0x82, 0xa0, 0x61]).unwrap(), None);
}

/// In a CJK encoding a non-ASCII character is a word character only if it is
/// genuinely multi-byte.
///
/// Shift_JIS spells halfwidth katakana in ONE byte, and libonig does not call
/// those word characters. Asking Unicode about the transcoded codepoint said
/// otherwise, so `\w+` swallowed them.
#[test]
fn sjis_single_byte_katakana_is_not_a_word_char() {
    let re = Regex::new(r"\w+", Options::NONE, Encoding::SJIS, Syntax::ONIGURUMA).unwrap();
    // 0xB0 and 0xA1 are single-byte katakana; only the 'A' is a word char.
    let m = re.search(&[0xb0, 0xa1, 0x41, 0xb0, 0xa2]).unwrap().expect("matches");
    assert_eq!(m.range(), 2..3);
    // A real two-byte character IS a word character.
    let m = re.search(&[0xe4, 0xb8, 0xad, 0x61]).unwrap().expect("matches");
    assert_eq!(m.range(), 0..2);
}

/// A prefilter must not report a match starting inside a character.
///
/// ASCII bytes appear as TRAIL bytes in the CJK encodings, so a byte scan for
/// `a` finds the 0x61 in the Big5 character `AD 61` -- which is not the letter
/// `a`. The filters are gated on `Encoding::self_sync` for exactly this.
#[test]
fn cjk_prefilters_do_not_match_inside_a_character() {
    let re = Regex::new(b"a", Options::NONE, Encoding::BIG5, Syntax::ONIGURUMA).unwrap();
    assert_eq!(re.search(&[0xe4, 0xb8, 0xad, 0x61]).unwrap(), None);
    // The same bytes in a single-byte encoding really do end in an 'a'.
    let re = Regex::new(b"a", Options::NONE, Encoding::ISO_8859_1, Syntax::ONIGURUMA).unwrap();
    assert_eq!(re.search(&[0xe4, 0xb8, 0xad, 0x61]).unwrap().unwrap().range(), 3..4);
}

/// FIND_LONGEST takes the longest match at a start, not the first one found.
///
/// `Inst::Match` used to unwind immediately, so `a|aa` reported the first
/// alternative and the option only ever compared across start positions.
#[test]
fn find_longest_picks_the_longest_alternative() {
    let re = Regex::new_str("a|aa", Options::FIND_LONGEST, Syntax::ONIGURUMA).unwrap();
    assert_eq!(re.search(b"aaa").unwrap().expect("matches").range(), 0..2);
    // Without the option it is leftmost-first, as before.
    let re = Regex::new_str("a|aa", Options::NONE, Syntax::ONIGURUMA).unwrap();
    assert_eq!(re.search(b"aaa").unwrap().unwrap().range(), 0..1);
}

/// `find_at` past the end of the haystack is an error, not a match there.
///
/// Found by `tools/onig-bench --example fuzz_compat` driving the C ABI with
/// offsets a careless caller can compute: `onig_match` handed the offset
/// straight through, and the engine cheerfully reported an empty match at
/// offset 4 of a two-byte haystack. `search_range_param` had always checked;
/// this path had not.
#[test]
fn find_at_rejects_positions_past_the_haystack() {
    let re = Regex::new_str("", Options::NONE, Syntax::ONIGURUMA).unwrap();
    let hay = b"ab";
    // The last legal position is the end itself.
    assert_eq!(re.find_at(hay, 2).unwrap().expect("empty match").range(), 2..2);
    for at in [3usize, 4, 999] {
        let e = re.find_at(hay, at).expect_err("must reject");
        assert_eq!(e.kind, ErrorKind::InvalidArgument);
    }
    // A real pattern takes the same path.
    let re = Regex::new_str("a", Options::NONE, Syntax::ONIGURUMA).unwrap();
    assert!(re.find_at(hay, 99).is_err());
}