ferroni 2.8.0

Oniguruma regex in pure Rust, with a TextMate scanner, portable prepared Eager archives, and 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
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
//! Integration tests for the idiomatic Rust API.

use std::borrow::Cow;

use ferroni::api::{Regex, RegexBuilder};
use ferroni::error::RegexError;
use ferroni::oniguruma::{
    ONIGERR_INVALID_BACKREF, ONIGERR_UNDEFINED_GROUP_REFERENCE, OnigSyntaxType,
};
use ferroni::prelude::*;
use ferroni::regsyntax::{
    OnigSyntaxOniguruma, OnigSyntaxPerl_NG, OnigSyntaxPython, OnigSyntaxRuby,
};

// The documented defaults of the parser limits (ADR-013; the setters in
// `ferroni::regparse` restore them on zero). `regint` is crate-private.
const DEFAULT_PARSE_DEPTH_LIMIT: u32 = 256;
const DEFAULT_AST_NODE_LIMIT: u32 = 4096;

// === Regex::new ===

#[test]
fn recursive_capture_above_bitset_width() {
    let pattern = format!(r"{}(a\g<32>?b)", "()".repeat(31));
    let regex = Regex::new(&pattern).unwrap();
    assert_eq!(regex.find("aaabbb").unwrap().as_str(), "aaabbb");
    assert!(!regex.is_match("aaa"));
}

#[test]
fn simple_pattern() {
    let re = Regex::new(r"\d+").unwrap();
    let m = re.find("abc 123 def").unwrap();
    assert_eq!(m.as_str(), "123");
}

#[test]
fn unicode_pattern() {
    let re = Regex::new(r"\p{Hiragana}+").unwrap();
    let m = re.find("hello せかい world").unwrap();
    assert_eq!(m.as_str(), "せかい");
}

#[test]
fn no_match_returns_none() {
    let re = Regex::new(r"xyz").unwrap();
    assert!(re.find("abc").is_none());
}

#[test]
fn empty_pattern() {
    let re = Regex::new(r"").unwrap();
    let m = re.find("hello").unwrap();
    assert_eq!(m.start(), 0);
    assert_eq!(m.end(), 0);
    assert!(m.is_empty());
}

#[test]
fn invalid_pattern_syntax_error() {
    let err = Regex::new(r"(unclosed").unwrap_err();
    match err {
        RegexError::Syntax { code, .. } => assert!(code < 0),
        other => panic!("expected Syntax error, got {:?}", other),
    }
}

#[test]
fn invalid_pattern_empty_char_class() {
    let err = Regex::new(r"[]").unwrap_err();
    assert!(matches!(err, RegexError::Syntax { .. }));
}

#[test]
fn flat_patterns_exceeding_ast_depth_limit_fail_cleanly() {
    const HUGE_FLAT_PATTERN_ELEMENT_COUNT: usize = 50_000;

    let alternatives = format!("{}a", "a|".repeat(HUGE_FLAT_PATTERN_ELEMENT_COUNT - 1));
    let concatenation = ".".repeat(HUGE_FLAT_PATTERN_ELEMENT_COUNT);

    for pattern in [&alternatives, &concatenation] {
        let err = Regex::new(pattern).unwrap_err();
        assert_eq!(err, RegexError::ParseDepthLimitOver);
    }
}

#[test]
fn nested_wide_patterns_exceeding_ast_depth_limit_fail_cleanly() {
    const NESTED_GROUP_COUNT: usize = 20;
    const WIDE_ALTERNATIVE_COUNT: usize = 4_000;
    const LONG_CONCATENATION_COUNT: usize = 4_000;

    let mut pattern = ".".repeat(LONG_CONCATENATION_COUNT);
    for _ in 0..NESTED_GROUP_COUNT {
        pattern = format!("(?:{}{})", "a|".repeat(WIDE_ALTERNATIVE_COUNT), pattern);
    }

    let err = Regex::new(&pattern).unwrap_err();
    assert_eq!(err, RegexError::ParseDepthLimitOver);
}

/// Compiles `pattern` on a spawned thread with an explicit 2 MiB stack, the
/// default of `std::thread::spawn` and of Tokio and Rayon workers. Debug builds
/// use the most stack per level, so these tests are the harder case. A stack
/// overflow aborts the test binary instead of returning, so a test that gets a
/// result has shown that the limit fired first.
fn compile_on_two_mib_stack(pattern: String) -> Result<(), RegexError> {
    std::thread::Builder::new()
        .stack_size(2 * 1024 * 1024)
        .spawn(move || Regex::new(&pattern).map(|_| ()))
        .expect("spawn compile thread")
        .join()
        .expect("compile thread panicked")
}

#[test]
fn nested_groups_above_depth_limit_fail_cleanly_on_two_mib_stack() {
    // A group costs two parser units, so 128 groups reach depth 258.
    let groups = DEFAULT_PARSE_DEPTH_LIMIT as usize / 2;
    let pattern = "(".repeat(groups) + "a" + &")".repeat(groups);
    assert_eq!(
        compile_on_two_mib_stack(pattern),
        Err(RegexError::ParseDepthLimitOver)
    );
}

#[test]
fn nested_groups_at_depth_limit_compile_on_two_mib_stack() {
    // 127 groups reach depth 256, exactly the limit.
    let groups = DEFAULT_PARSE_DEPTH_LIMIT as usize / 2 - 1;
    let pattern = "(".repeat(groups) + "a" + &")".repeat(groups);
    assert_eq!(compile_on_two_mib_stack(pattern), Ok(()));
}

#[test]
fn nested_classes_above_depth_limit_fail_cleanly_on_two_mib_stack() {
    // A nested class costs one unit, on top of the two that the top-level
    // alternation and branch take, so 255 classes reach depth 257.
    let classes = DEFAULT_PARSE_DEPTH_LIMIT as usize - 1;
    let pattern = "[".repeat(classes) + "a" + &"]".repeat(classes);
    assert_eq!(
        compile_on_two_mib_stack(pattern),
        Err(RegexError::ParseDepthLimitOver)
    );
}

#[test]
fn nested_classes_at_depth_limit_compile_on_two_mib_stack() {
    // 254 classes reach depth 256, exactly the limit.
    let classes = DEFAULT_PARSE_DEPTH_LIMIT as usize - 2;
    let pattern = "[".repeat(classes) + "a" + &"]".repeat(classes);
    assert_eq!(compile_on_two_mib_stack(pattern), Ok(()));
}

#[test]
fn flat_alternation_above_ast_node_limit_fails_cleanly_on_two_mib_stack() {
    // One node per alternative: one more alternative than the budget allows.
    let pattern = (0..=DEFAULT_AST_NODE_LIMIT)
        .map(|i| format!("a{i}"))
        .collect::<Vec<_>>()
        .join("|");
    assert_eq!(
        compile_on_two_mib_stack(pattern),
        Err(RegexError::ParseDepthLimitOver)
    );
}

#[test]
fn long_literal_larger_than_ast_node_limit_is_supported() {
    // A literal run is one node, so its length is not bounded by the budget.
    let pattern = "a".repeat(DEFAULT_AST_NODE_LIMIT as usize * 2);
    let re = Regex::new(&pattern).unwrap();
    assert!(re.is_match(&pattern));
}

// === Regex::is_match ===

#[test]
fn is_match_true() {
    let re = Regex::new(r"world").unwrap();
    assert!(re.is_match("hello world"));
}

#[test]
fn is_match_false() {
    let re = Regex::new(r"world").unwrap();
    assert!(!re.is_match("hello earth"));
}

// === Regex::find ===

#[test]
fn find_start_end_range() {
    let re = Regex::new(r"bar").unwrap();
    let m = re.find("foobarbaz").unwrap();
    assert_eq!(m.start(), 3);
    assert_eq!(m.end(), 6);
    assert_eq!(m.range(), 3..6);
    assert_eq!(m.len(), 3);
    assert!(!m.is_empty());
}

#[test]
fn find_as_bytes() {
    let re = Regex::new(r"\w+").unwrap();
    let m = re.find("hello world").unwrap();
    assert_eq!(m.as_bytes(), b"hello");
}

#[test]
fn unset_backreference_does_not_reuse_prior_capture() {
    let prior = Regex::new(r"(abc)(def)").unwrap();
    assert!(prior.captures("abcdef").is_some());

    let re = Regex::new(r"(?:(foo)|bar)\1").unwrap();
    assert!(re.find("barbar").is_none());
}

// === Regex::captures ===

#[test]
fn captures_groups() {
    let re = Regex::new(r"(\w+)\s+(\w+)").unwrap();
    let caps = re.captures("hello world").unwrap();
    assert_eq!(caps.get(0).unwrap().as_str(), "hello world");
    assert_eq!(caps.get(1).unwrap().as_str(), "hello");
    assert_eq!(caps.get(2).unwrap().as_str(), "world");
    assert_eq!(caps.len(), 3); // group 0 + 2 captures
}

#[test]
fn captures_optional_group() {
    let re = Regex::new(r"(a)(b)?c").unwrap();
    let caps = re.captures("ac").unwrap();
    assert_eq!(caps.get(0).unwrap().as_str(), "ac");
    assert_eq!(caps.get(1).unwrap().as_str(), "a");
    assert!(caps.get(2).is_none()); // group 2 didn't participate
}

#[test]
fn captures_optional_group_is_not_reused_from_prior_regex() {
    let prior = Regex::new(r"(aaa)(bbb)(ccc)").unwrap();
    assert!(prior.captures("aaabbbccc").is_some());

    let re = Regex::new(r"(x)?z").unwrap();
    let caps = re.captures("z").unwrap();

    // A stale range here used to make safe Match::as_str() panic when slicing "z".
    assert!(caps.get(1).is_none());
}

#[test]
fn captures_named() {
    let re = Regex::new(r"(?<first>\w+)\s+(?<last>\w+)").unwrap();
    let caps = re.captures("John Doe").unwrap();
    assert_eq!(caps.name("first").unwrap().as_str(), "John");
    assert_eq!(caps.name("last").unwrap().as_str(), "Doe");
    assert!(caps.name("middle").is_none());
}

#[test]
fn captures_multiplex_name_returns_last_participating_group() {
    let re = Regex::new(r"(?<letter>a)(?<letter>b)").unwrap();
    let caps = re.captures("ab").unwrap();

    assert_eq!(caps.name("letter").unwrap().as_str(), "b");
}

#[test]
fn captures_multiplex_name_returns_earlier_participating_group() {
    let re = Regex::new(r"(?:(?<letter>a)|(?<letter>b))").unwrap();
    let caps = re.captures("a").unwrap();

    assert_eq!(caps.name("letter").unwrap().as_str(), "a");
}

#[test]
fn captures_multiplex_name_returns_later_participating_group() {
    let re = Regex::new(r"(?:(?<letter>a)|(?<letter>b))").unwrap();
    let caps = re.captures("b").unwrap();

    assert_eq!(caps.name("letter").unwrap().as_str(), "b");
}

#[test]
fn captures_multiplex_name_returns_none_when_no_group_participates() {
    let re = Regex::new(r"(?:(?<letter>a)|(?<letter>b))?").unwrap();
    let caps = re.captures("").unwrap();

    assert!(caps.name("letter").is_none());
}

#[test]
fn captures_name_returns_unique_named_group() {
    let re = Regex::new(r"(?<letter>a)").unwrap();
    let caps = re.captures("a").unwrap();

    assert_eq!(caps.name("letter").unwrap().as_str(), "a");
}

#[test]
fn captures_no_match() {
    let re = Regex::new(r"(\d+)").unwrap();
    assert!(re.captures("no digits").is_none());
}

#[test]
fn captures_len() {
    let re = Regex::new(r"(a)(b)(c)(d)").unwrap();
    assert_eq!(re.captures_len(), 4);
}

#[test]
fn captures_iter() {
    let re = Regex::new(r"(a)(b)").unwrap();
    let caps = re.captures("ab").unwrap();
    let items: Vec<_> = caps.iter().collect();
    assert_eq!(items.len(), 3);
    assert_eq!(items[0].unwrap().as_str(), "ab");
    assert_eq!(items[1].unwrap().as_str(), "a");
    assert_eq!(items[2].unwrap().as_str(), "b");
}

// === Regex::find_iter ===

#[test]
fn find_iter_multiple() {
    let re = Regex::new(r"\d+").unwrap();
    let results: Vec<&str> = re
        .find_iter("1 and 22 and 333")
        .map(|m| m.as_str())
        .collect();
    assert_eq!(results, vec!["1", "22", "333"]);
}

#[test]
fn find_iter_no_matches() {
    let re = Regex::new(r"\d+").unwrap();
    let results: Vec<_> = re.find_iter("no digits").collect();
    assert!(results.is_empty());
}

#[test]
fn find_iter_empty_pattern() {
    let re = Regex::new(r"").unwrap();
    let results: Vec<_> = re.find_iter("ab").collect();
    // Should find empty match at positions 0, 1, 2
    assert_eq!(results.len(), 3);
    for (i, m) in results.iter().enumerate() {
        assert_eq!(m.start(), i);
        assert!(m.is_empty());
    }
}

#[test]
fn find_iter_overlapping_region() {
    let re = Regex::new(r"\w+").unwrap();
    let results: Vec<&str> = re.find_iter("a bb ccc").map(|m| m.as_str()).collect();
    assert_eq!(results, vec!["a", "bb", "ccc"]);
}

// === RegexBuilder ===

#[test]
fn builder_case_insensitive() {
    let re = RegexBuilder::new(r"hello")
        .case_insensitive(true)
        .build()
        .unwrap();
    assert!(re.is_match("HELLO"));
    assert!(re.is_match("HeLlO"));
}

#[test]
fn builder_dot_matches_newline() {
    let re = Regex::builder(r"a.b")
        .dot_matches_newline(true)
        .build()
        .unwrap();
    assert!(re.is_match("a\nb"));

    let re2 = Regex::builder(r"a.b").build().unwrap();
    assert!(!re2.is_match("a\nb"));
}

#[test]
fn builder_extended_mode() {
    let re = Regex::builder(
        r"
        \d+   # digits
        \s+   # space
        \w+   # word
    ",
    )
    .extended(true)
    .build()
    .unwrap();
    assert!(re.is_match("42 hello"));
}

#[test]
fn builder_syntax() {
    use ferroni::regsyntax::OnigSyntaxPerl;
    let re = Regex::builder(r"\d+")
        .syntax(&OnigSyntaxPerl)
        .build()
        .unwrap();
    assert!(re.is_match("42"));
}

#[test]
fn prototype_ecmascript_negative_lookbehind_is_opt_in() {
    use ferroni::regsyntax::prototype_ecmascript_lookbehind_syntax;

    assert!(Regex::new(r"(?<!(a))b").is_err());
    assert!(Regex::new(r"(?<!a(?=b))b").is_err());

    let capture = Regex::builder(r"(?<!(a+|bc))d")
        .syntax(prototype_ecmascript_lookbehind_syntax())
        .build()
        .unwrap();
    assert!(capture.is_match("d"));
    assert!(!capture.is_match("ad"));
    assert!(!capture.is_match("bcd"));

    let positive = Regex::builder(r"(?<!a(?=b))b")
        .syntax(prototype_ecmascript_lookbehind_syntax())
        .build()
        .unwrap();
    assert!(positive.is_match("b"));
    assert!(!positive.is_match("ab"));

    let negative = Regex::builder(r"(?<!a(?!c))b")
        .syntax(prototype_ecmascript_lookbehind_syntax())
        .build()
        .unwrap();
    assert!(!negative.is_match("ab"));
    assert!(negative.is_match("acb"));
}

#[test]
fn prototype_ecmascript_unmatched_backreferences_are_empty() {
    use ferroni::regsyntax::prototype_ecmascript_lookbehind_syntax;

    for pattern in [r"(a)?b\1", r"(a)(b)?c\2", r"(a)(b)(c)?d\3"] {
        let re = Regex::builder(pattern)
            .syntax(prototype_ecmascript_lookbehind_syntax())
            .build()
            .unwrap();
        assert!(re.is_match(match pattern {
            r"(a)?b\1" => "b",
            r"(a)(b)?c\2" => "ac",
            _ => "abd",
        }));
    }

    let insensitive = Regex::builder(r"(a)(b)(c)?d\3")
        .syntax(prototype_ecmascript_lookbehind_syntax())
        .case_insensitive(true)
        .build()
        .unwrap();
    assert!(insensitive.is_match("ABD"));

    let named = Regex::builder(r"(?:(?<x>a)|(?<x>b))?c\k<x>")
        .syntax(prototype_ecmascript_lookbehind_syntax())
        .build()
        .unwrap();
    assert!(named.is_match("c"));

    let named_insensitive = Regex::builder(r"(?:(?<x>a)|(?<x>b))?c\k<x>")
        .syntax(prototype_ecmascript_lookbehind_syntax())
        .case_insensitive(true)
        .build()
        .unwrap();
    assert!(named_insensitive.is_match("C"));
}

#[test]
fn builder_chaining() {
    let re = Regex::builder(r"hello world")
        .case_insensitive(true)
        .dot_matches_newline(true)
        .extended(false)
        .build()
        .unwrap();
    assert!(re.is_match("HELLO WORLD"));
}

// === Byte API ===

#[test]
fn find_bytes() {
    let re = Regex::new_bytes(b"\\d+").unwrap();
    let m = re.find_bytes(b"abc 42 def").unwrap();
    assert_eq!(m.as_bytes(), b"42");
    assert_eq!(m.start(), 4);
}

#[test]
fn is_match_bytes() {
    let re = Regex::new_bytes(b"hello").unwrap();
    assert!(re.is_match_bytes(b"say hello"));
    assert!(!re.is_match_bytes(b"goodbye"));
}

#[test]
fn byte_matching_clamps_truncated_utf8_word_steps() {
    let cases: &[(&str, &[u8])] = &[
        (r"\w+", b"hello\xf0\x9f\x92"),
        (r"\b\w+\b", b"\xf0\x9f"),
        (r"\w\b", b"\xf0\x9f"),
        (r"\w+", b"abc\xc2"),
        (r"(?W)\W", b"\xf0"),
    ];

    for &(pattern, input) in cases {
        let re = Regex::new(pattern).unwrap();
        assert!(re.is_match_bytes(input), "pattern {pattern:?}");

        let matched = re.find_bytes(input).expect("matching input");
        assert_eq!(matched.range(), 0..input.len(), "pattern {pattern:?}");
        assert_eq!(matched.as_bytes(), input, "pattern {pattern:?}");
    }
}

#[test]
fn byte_matching_clamps_truncated_utf8_negated_class_steps() {
    let input = b"\xf0";
    let re = Regex::new_bytes(b"([^a]+)").unwrap();

    let matched = re.find_bytes(input).expect("matching input");
    assert_eq!(matched.range(), 0..input.len());
    assert_eq!(matched.as_bytes(), input);

    let captures = re.captures_bytes(input).expect("matching input");
    for index in 0..captures.len() {
        let capture = captures.get(index).expect("participating capture");
        assert_eq!(capture.range(), 0..input.len());
        assert_eq!(capture.as_bytes(), input);
    }
}

// === RegexError ===

#[test]
fn error_display() {
    let err = Regex::new(r"(").unwrap_err();
    let msg = format!("{}", err);
    assert!(!msg.is_empty());
}

#[test]
fn error_is_std_error() {
    let err: Box<dyn std::error::Error> = Box::new(Regex::new(r"[").unwrap_err());
    assert!(!err.to_string().is_empty());
}

#[test]
fn error_code() {
    let err = Regex::new(r"(").unwrap_err();
    assert!(err.code() < 0);
}

// Messages naming a group or property carry the offending name. Every
// expected string is what C's onig_error_code_to_str() prints for the same
// pattern and syntax, prefixed with "syntax error: ".

fn error_message(syntax: &'static OnigSyntaxType, pattern: &str) -> String {
    Regex::builder(pattern)
        .syntax(syntax)
        .build()
        .unwrap_err()
        .to_string()
}

#[test]
fn error_message_names_invalid_char_property() {
    let cases = [
        (r"\p{Nope}", "{Nope}"),
        (r"\p{^Nope}", "{Nope}"),
        (r"[\p{Nope}]", "{Nope}"),
        (r"\pQ", "{Q}"),
        (r"\p{}", "{}"),
        (r"\p{Ñope}", "{Ñope}"),
        // C cuts the name after 27 bytes.
        (
            r"\p{abcdefghijklmnopqrstuvwxyz0123456789}",
            "{abcdefghijklmnopqrstuvwxyz0...}",
        ),
    ];
    for (pattern, name) in cases {
        assert_eq!(
            Regex::new(pattern).unwrap_err().to_string(),
            format!("syntax error: invalid character property name {name}"),
            "{pattern}"
        );
    }
}

#[test]
fn error_message_names_undefined_name_reference() {
    let cases = [
        (&OnigSyntaxOniguruma, r"\k<nope>"),
        (&OnigSyntaxOniguruma, r"\g<nope>"),
        (&OnigSyntaxOniguruma, r"(?(<nope>)a|b)"),
        (&OnigSyntaxPerl_NG, r"(?&nope)"),
        (&OnigSyntaxPython, r"(?P=nope)"),
        (&OnigSyntaxPython, r"(?P>nope)"),
    ];
    for (syntax, pattern) in cases {
        assert_eq!(
            error_message(syntax, pattern),
            "syntax error: undefined name <nope> reference",
            "{pattern}"
        );
    }
}

#[test]
fn error_message_names_undefined_group_reference() {
    let cases = [
        (&OnigSyntaxOniguruma, r"\g<5>", "<5>"),
        (&OnigSyntaxOniguruma, r"\g<+1>", "<+1>"),
        (&OnigSyntaxOniguruma, r"\g<-2>", "<-2>"),
        (&OnigSyntaxPerl_NG, r"(?1)", "<1>"),
        (&OnigSyntaxPerl_NG, r"(?-2)", "<-2>"),
    ];
    for (syntax, pattern, name) in cases {
        assert_eq!(
            error_message(syntax, pattern),
            format!("syntax error: undefined group {name} reference"),
            "{pattern}"
        );
    }
}

#[test]
fn error_message_names_multiplex_defined_name() {
    // Oniguruma, Ruby and Perl_NG set ONIG_SYN_ALLOW_MULTIPLEX_DEFINITION_NAME;
    // Python is a syntax with named groups that does not.
    assert_eq!(
        error_message(&OnigSyntaxPython, r"(?P<a>x)(?P<a>y)"),
        "syntax error: multiplex defined name <a>"
    );
    // Callout tags are unique in every syntax.
    for pattern in [
        r"(*COUNT[AB]{X})(*COUNT[AB]{X})",
        r"(*COUNT[AB]{X})a(*MAX[AB]{2})",
    ] {
        assert_eq!(
            Regex::new(pattern).unwrap_err().to_string(),
            "syntax error: multiplex defined name <AB>",
            "{pattern}"
        );
    }
}

#[test]
fn error_message_names_multiplex_definition_name_call() {
    for syntax in [&OnigSyntaxOniguruma, &OnigSyntaxRuby] {
        assert_eq!(
            error_message(syntax, r"(?<a>x)(?<a>y)\g<a>"),
            "syntax error: multiplex definition name <a> call"
        );
    }
}

#[test]
fn error_message_names_invalid_group_name() {
    let cases = [
        (r"(?<1a>x)", "<1a>"),
        (r"\k<1a>", "<1a>"),
        (r"\k<+0>", "<+0>"),
        (r"\k<+>", "<+>"),
        (r"\k<-x>", "<-x>"),
        // A bad level runs the name to the end of the pattern, as in C.
        (r"\k<a+1x>", "<a+1x>>"),
        (r"\k<1+x>", "<1+x>>"),
    ];
    for (pattern, name) in cases {
        assert_eq!(
            Regex::new(pattern).unwrap_err().to_string(),
            format!("syntax error: invalid group name {name}"),
            "{pattern}"
        );
    }
}

#[test]
fn error_message_names_invalid_char_in_group_name() {
    let cases = [
        (r"(?<$a>x)", "<$a>"),
        (r"(?<$ab", "<$ab>"),
        (r"\k<a$b>", "<a$b>"),
        (r"\k<a$b", "<a$>"),
    ];
    for (pattern, name) in cases {
        assert_eq!(
            Regex::new(pattern).unwrap_err().to_string(),
            format!("syntax error: invalid char in group name {name}"),
            "{pattern}"
        );
    }
}

// Each case below is (syntax, pattern, what C's onig_new() reports): `None`
// when it compiles, else the onig_error_code_to_str() message.

fn assert_compiles_like_c(cases: &[(&'static OnigSyntaxType, &str, Option<&str>)]) {
    for &(syntax, pattern, expected) in cases {
        let got = Regex::builder(pattern)
            .syntax(syntax)
            .build()
            .err()
            .map(|e| e.to_string());
        let expected = expected.map(|m| format!("syntax error: {m}"));
        assert_eq!(got, expected, "{pattern}");
    }
}

/// C's fetch_name() only honors an error inside the name when the closing
/// delimiter is missing, and fetch_name_with_level() stops a name at `+`
/// or `-` to read a level.
#[test]
fn group_names_parse_like_c() {
    let onig = &OnigSyntaxOniguruma;
    assert_compiles_like_c(&[
        (onig, r"(?<a$b>x)", None),
        (onig, r"(?'a-b'x)", None),
        (&OnigSyntaxPython, r"(?P<a+1>x)", None),
        (onig, r"(?<ab", Some("invalid group name <a>")),
        (onig, r"(?<a$b)", Some("invalid group name <a$b>")),
        (onig, r"\k<a", Some("invalid group name <a>")),
        (onig, r"\k<a)", Some("invalid group name <a)>")),
        (onig, r"\k<a+>", Some("invalid group name <a+>>")),
        (onig, r"\k<1->", Some("invalid group name <1->>")),
        (onig, r"\k<$a>", Some("invalid char in group name <$a>")),
        (onig, r"\k<$a", Some("invalid char in group name <$>")),
        (onig, r"\g<ab", Some("invalid group name <a>")),
        (onig, r"\g<a)", Some("invalid group name <a>")),
        (onig, r"\g<a-b", Some("invalid group name <a->")),
        (onig, r"\g<1a>", Some("undefined name <1a> reference")),
        (onig, r"\g<a+1>", Some("undefined name <a+1> reference")),
        (onig, r"\g<a$b>", Some("undefined name <a$b> reference")),
        (
            &OnigSyntaxPerl_NG,
            r"(?&a-1)",
            Some("undefined name <a-1> reference"),
        ),
        (
            &OnigSyntaxPerl_NG,
            r"(?+0)",
            Some("invalid group name <+0>"),
        ),
        (
            onig,
            r"(x)\k<+2147483647>",
            Some("invalid backref number/name"),
        ),
    ]);
}

/// `(?P=name)` and `(?P>name)` are tokens that take no group number, as in
/// C's fetch_token().
#[test]
fn python_named_backref_and_call_parse_like_c() {
    let python = &OnigSyntaxPython;
    assert_compiles_like_c(&[
        (python, r"(?P=1)", Some("invalid backref number/name")),
        (python, r"(?P=a-b)", Some("invalid group name <a-b)>")),
        (python, r"(?P=1a)", Some("invalid group name <1a>")),
        (python, r"(?P>1a)", Some("undefined name <1a> reference")),
        (python, r"(?P>+0)", Some("invalid group name <+0>")),
        (python, r"(?P<a>x)(?P=a)", None),
        (python, r"(?P<a>x)(?P>a)", None),
    ]);
    let err = Regex::builder(r"(?P>1)")
        .syntax(python)
        .build()
        .unwrap_err();
    assert_eq!(err.code(), ONIGERR_UNDEFINED_GROUP_REFERENCE);
}

/// C checks backref bounds (check_backrefs) after rejecting numbered
/// backrefs next to named groups, and a backref may point forward.
#[test]
fn numbered_backrefs_are_checked_like_c() {
    let onig = &OnigSyntaxOniguruma;
    let not_allowed = Some("numbered backref/call is not allowed. (use name)");
    assert_compiles_like_c(&[
        (onig, r"(?<a>x)\k<2>", not_allowed),
        (onig, r"(?<a>x)\k<+1>", not_allowed),
        (onig, r"(?<a>x)(y)\k<3>", not_allowed),
        (&OnigSyntaxPerl_NG, r"(?<a>x)\k<2>", not_allowed),
        (onig, r"\k<2>", Some("invalid backref number/name")),
        (onig, r"(x)\k<2>", Some("invalid backref number/name")),
        (onig, r"\k<1>(x)", None),
    ]);
}

/// C's check_call_reference() rejects a call by number next to named groups.
#[test]
fn numbered_calls_are_checked_like_c() {
    let onig = &OnigSyntaxOniguruma;
    let not_allowed = Some("numbered backref/call is not allowed. (use name)");
    assert_compiles_like_c(&[
        (onig, r"(?<a>x)\g<1>", not_allowed),
        (onig, r"(?<a>x)\g<0>", not_allowed),
        (onig, r"(?<a>x)\g<2>", not_allowed),
        (onig, r"(?<a>x)\g<-1>", not_allowed),
        (&OnigSyntaxRuby, r"(?<a>x)\g<1>", not_allowed),
        (&OnigSyntaxPerl_NG, r"(?<a>x)(?1)", not_allowed),
        (&OnigSyntaxPerl_NG, r"(?<a>x)\g<1>", not_allowed),
        (onig, r"(x)\g<1>", None),
        (onig, r"(?<a>x)\g<a>", None),
    ]);
}

/// C's prs_bag() reads a condition reference with fetch_name_with_level(),
/// falls back to a pattern condition when a bare reference does not parse,
/// and leaves the group bounds to check_backrefs().
#[test]
fn conditions_parse_like_c() {
    let onig = &OnigSyntaxOniguruma;
    let not_allowed = Some("numbered backref/call is not allowed. (use name)");
    assert_compiles_like_c(&[
        (onig, r"(a)(?(1)a|b)", None),
        (onig, r"(a)(?(1+0)a|b)", None),
        (onig, r"(a)(?(<1>)a|b)", None),
        (onig, r"(a)(?(1))", None),
        (onig, r"(?(1a)a|b)", None),
        (onig, r"(?(-0)a|b)", None),
        (onig, r"(?(a)b)", None),
        (
            onig,
            r"(?(+0)a|b)",
            Some("target of repeat operator is not specified"),
        ),
        (onig, r"(?(1)a|b)", Some("invalid backref number/name")),
        (
            onig,
            r"(?(<nope>)a|b)",
            Some("undefined name <nope> reference"),
        ),
        (onig, r"(?(<1>a|b)", Some("end pattern in group")),
        (onig, r"(?(1", Some("end pattern in group")),
        (onig, r"(?(a", Some("end pattern in group")),
        (onig, r"(?(a))", Some("invalid if-else syntax")),
        (onig, r"(?<a>x)(?(<2>)a|b)", not_allowed),
        (onig, r"(?<a>x)(?(2)a|b)", not_allowed),
    ]);
}

/// C's fetch_interval() leaves the position at the `{` when the brace is an
/// ordinary character (ONIG_SYN_ALLOW_INVALID_INTERVAL), so what follows is
/// tokenized normally.
#[test]
fn invalid_interval_brace_is_a_literal_like_c() {
    let onig = &OnigSyntaxOniguruma;
    assert_compiles_like_c(&[
        (onig, r"{(", Some("end pattern with unmatched parenthesis")),
        (onig, r"{)", Some("unmatched close parenthesis")),
        (onig, r"{[", Some("premature end of char-class")),
        (onig, r"a{(?", Some("end pattern in group")),
        (
            onig,
            r"a{1,x}(",
            Some("end pattern with unmatched parenthesis"),
        ),
        (
            &OnigSyntaxRuby,
            r"{(",
            Some("end pattern with unmatched parenthesis"),
        ),
        (
            &OnigSyntaxPython,
            r"a{(",
            Some("end pattern with unmatched parenthesis"),
        ),
        (onig, r"a{1,x}", None),
        (onig, r"a{,2}", None),
        (onig, r"a{", None),
    ]);
    let re = Regex::new(r"a{1,x}(b)").unwrap();
    assert_eq!(re.find("xa{1,x}b").unwrap().as_str(), "a{1,x}b");
    let re = Regex::new(r"a{2}").unwrap();
    assert!(re.is_match("aa") && !re.is_match("a{2}"));
}

/// C's prs_bag() switches on the whole code point, so a non-ASCII character
/// whose low byte is `:`, `=`, `i`, ... is not a group option.
#[test]
fn non_ascii_after_qmark_is_not_an_option_like_c() {
    let onig = &OnigSyntaxOniguruma;
    let undefined = Some("undefined group option");
    assert_compiles_like_c(&[
        (onig, "(?\u{013A}a)", undefined), // low byte ':'
        (onig, "(?\u{013D}a)", undefined), // low byte '='
        (onig, "(?\u{0121}a)", undefined), // low byte '!'
        (onig, "(?\u{013E}a)", undefined), // low byte '>'
        (onig, "(?\u{0169})a", undefined), // low byte 'i'
        (onig, "(?i\u{0169})a", undefined),
        (onig, "(?i\u{0178})a", undefined),   // low byte 'x'
        (onig, "(?y{\u{0167}})a", undefined), // low byte 'g'
    ]);
}

/// C's fetch_token_cc() switches on the whole escaped code point, so an
/// escaped non-ASCII character in a class is that character, not the
/// escape its low byte spells (U+0177 is not `\w`, U+0164 not `\d`).
#[test]
fn escaped_non_ascii_in_char_class_is_literal_like_c() {
    let re = Regex::new("[\\\u{0177}]").unwrap();
    assert!(re.is_match("\u{0177}"));
    assert!(!re.is_match("a"));
    let re = Regex::new("[\\\u{0164}]").unwrap();
    assert!(re.is_match("\u{0164}"));
    assert!(!re.is_match("1"));
}

/// (*FAIL) takes a callout number as in C, and an unknown tag is
/// ONIGERR_INVALID_CALLOUT_TAG_NAME (onig_get_callout_num_by_tag()).
#[test]
fn callout_numbers_by_tag_match_c() {
    use ferroni::encodings::utf8::ONIG_ENCODING_UTF8;
    use ferroni::oniguruma::{ONIG_OPTION_NONE, ONIGERR_INVALID_CALLOUT_TAG_NAME};
    use ferroni::regcomp::onig_new;
    use ferroni::regexec::onig_get_callout_num_by_tag;

    for (pattern, tag, num) in [
        (r"(*FAIL[t])|x", "t", 1),
        (r"(*FAIL)(*COUNT[t]{X})|x", "t", 2),
        (r"(*COUNT[a]{X})(*FAIL[t])|x", "t", 2),
        (r"a(*FAIL[t])|(*MAX[u]{2})a", "u", 2),
        (r"(*FAIL)|a", "t", ONIGERR_INVALID_CALLOUT_TAG_NAME),
        (r"(*FAIL[t])|x", "u", ONIGERR_INVALID_CALLOUT_TAG_NAME),
    ] {
        let reg = onig_new(
            pattern.as_bytes(),
            ONIG_OPTION_NONE,
            &ONIG_ENCODING_UTF8,
            &OnigSyntaxOniguruma,
        )
        .unwrap();
        assert_eq!(
            onig_get_callout_num_by_tag(&reg, tag.as_bytes()),
            num,
            "{pattern}"
        );
    }
}

/// C scans a callout's argument list (prs_callout_args in skip mode) before
/// it looks up the callout name.
#[test]
fn callout_arguments_are_scanned_before_the_name_like_c() {
    let onig = &OnigSyntaxOniguruma;
    let bad = Some("invalid callout pattern");
    assert_compiles_like_c(&[
        (onig, r"(*kx{|&", bad),
        (onig, r"(*NOPE{1", bad),
        (onig, r"(*MAX{2", bad),
        (onig, r"(*NOPE{1})", Some("undefined callout name")),
        (onig, r"(*MAX{2})", None),
    ]);
}

/// C parses a condition's body with prs_alts(), which requires the closing
/// parenthesis even when the body has no `|`.
#[test]
fn condition_bodies_must_be_closed_like_c() {
    let onig = &OnigSyntaxOniguruma;
    let unclosed = Some("end pattern with unmatched parenthesis");
    assert_compiles_like_c(&[
        (onig, r"(?()1", unclosed),
        (onig, r"(?(a)b", unclosed),
        (onig, r"(a)(?(1)b", unclosed),
        (onig, r"(a)(?(1)b|c", unclosed),
        (onig, r"(?(a)(?:b|c))", None),
        (onig, r"(a)(?(1)b|c|d)", None),
    ]);
}

/// C's prs_callout_of_name() and prs_callout_of_contents() check the name
/// and tag characters, accept a tag on every callout, and count the
/// arguments before the closing parenthesis.
#[test]
fn callout_names_and_tags_parse_like_c() {
    let onig = &OnigSyntaxOniguruma;
    let bad_tag = Some("invalid callout tag name");
    assert_compiles_like_c(&[
        (onig, r"(*FAIL[a])", None),
        (onig, r"(*COUNT[_a]{X})", None),
        (onig, r"(?{foo}[a])", None),
        (onig, r"a|(*FAIL)", None),
        (onig, r"(*FAIL[1])", bad_tag),
        (onig, r"(*FAIL[])", bad_tag),
        (onig, r"(*FAIL[a$b])", bad_tag),
        (onig, r"(*MAX[1]{2})", bad_tag),
        (onig, r"(?{foo}[1])", bad_tag),
        (
            onig,
            r"(*FAIL[a])(*FAIL[a])",
            Some("multiplex defined name <a>"),
        ),
        (
            onig,
            r"(?{foo}[a])(?{bar}[a])",
            Some("multiplex defined name <a>"),
        ),
        (onig, r"(*FA-IL)", Some("invalid callout name")),
        (onig, r"(*NOPE)", Some("undefined callout name")),
        (onig, r"(*FAIL{1})", Some("invalid callout arg")),
        (onig, r"(*FAIL[a]", Some("end pattern in group")),
    ]);
}

/// C's prs_bag() only reads group options after an option letter; any
/// other character after `(?` is an undefined group option.
#[test]
fn group_options_start_like_c() {
    let onig = &OnigSyntaxOniguruma;
    let undefined = Some("undefined group option");
    assert_compiles_like_c(&[
        (onig, r"(?)", undefined),
        (onig, r"(?Q)", undefined),
        (&OnigSyntaxRuby, r"(?)", undefined),
        (&OnigSyntaxPerl_NG, r"(?)", undefined),
        (&OnigSyntaxRuby, r"(?W)a", undefined),
        (onig, r"(?i)a", None),
        (onig, r"(?-i)a", None),
        (onig, r"(?W)a", None),
        (onig, r"(?C)a", None),
        (onig, r"(?I)a", None),
    ]);
}

/// C's prs_bag() reports a pattern ending right after `(?<` like any other
/// unclosed group.
#[test]
fn pattern_ending_after_lt_is_reported_like_c() {
    let unclosed = Some("end pattern with unmatched parenthesis");
    assert_compiles_like_c(&[
        (&OnigSyntaxOniguruma, r"(?<", unclosed),
        (&OnigSyntaxOniguruma, r"(x)(?<", unclosed),
        (&OnigSyntaxOniguruma, r"(?<=", unclosed),
        (&OnigSyntaxPerl_NG, r"(?<", unclosed),
    ]);
}

/// Where C records no name, it prints an empty `<>`; the Rust message drops
/// the placeholder instead.
#[test]
fn error_message_without_recorded_name_drops_placeholder() {
    assert_compiles_like_c(&[
        (
            &OnigSyntaxPython,
            r"(?P>1)",
            Some("undefined group reference"),
        ),
        (&OnigSyntaxPerl_NG, r"(?1a)", Some("invalid group name")),
        (&OnigSyntaxPerl_NG, r"(?1+x)", Some("invalid group name")),
        // A recorded empty name is still shown.
        (
            &OnigSyntaxOniguruma,
            r"\p{}",
            Some("invalid character property name {}"),
        ),
    ]);
}

// === Prelude ===

#[test]
fn prelude_imports_work() {
    // This test verifies that the prelude re-exports are accessible.
    let re = Regex::new(r"(\w+)").unwrap();
    let caps: Captures = re.captures("hello").unwrap();
    let m: Match = caps.get(0).unwrap();
    assert_eq!(m.as_str(), "hello");
    let _: &RegexError = &Regex::new(r"(").unwrap_err();
}

// === as_raw escape hatch ===

#[test]
fn as_raw_access() {
    use ferroni::regexec::onig_number_of_captures;
    let re = Regex::new(r"(a)(b)(c)").unwrap();
    let raw = re.as_raw();
    assert_eq!(onig_number_of_captures(raw), 3);
}

// === Complex patterns ===

#[test]
fn alternation() {
    let re = Regex::new(r"cat|dog|bird").unwrap();
    assert_eq!(re.find("I have a dog").unwrap().as_str(), "dog");
}

#[test]
fn backreference() {
    let re = Regex::new(r"(\w+)\s+\1").unwrap();
    let m = re.find("hello hello world").unwrap();
    assert_eq!(m.as_str(), "hello hello");
}

#[test]
fn numbered_backreferences_validate_capture_group_bounds() {
    for pattern in [
        b"\\1".as_slice(),
        b"\\8",
        b"\\9",
        b"(a)\\2",
        b"\\k<2>",
        b"\\k<8>",
        b"\\k<9>",
        b"\\k<80>",
        b"(a)\\k<2>",
    ] {
        let err = Regex::new_bytes(pattern).unwrap_err();
        assert_eq!(err.code(), ONIGERR_INVALID_BACKREF, "pattern: {pattern:?}");
    }
}

#[test]
fn group_reference_numbers_reject_overflow_and_missing_terminators() {
    // As in C, a bare condition that is not a valid group reference is
    // parsed as a pattern instead, so an overflowing number compiles.
    for pattern in [
        br"(?(9999999999999)a|b)".as_slice(),
        br"(a)(?(1+9999999999999)a|b)".as_slice(),
    ] {
        assert!(Regex::new_bytes(pattern).is_ok(), "{pattern:?}");
    }
    for pattern in [
        br"(a)\k<a+999999999999999>".as_slice(),
        br"(a)\k<1".as_slice(),
        br"(a)\g<1".as_slice(),
        br"(a)\k'1".as_slice(),
    ] {
        assert!(Regex::new_bytes(pattern).is_err(), "{pattern:?}");
    }
}

#[test]
fn valid_numbered_backreferences_compile_and_match() {
    for (pattern, input) in [
        (b"(a)\\1".as_slice(), "aa"),
        (b"(a)\\k<1>".as_slice(), "aa"),
        (b"(a)(b)(c)(d)(e)(f)(g)(h)\\8".as_slice(), "abcdefghh"),
        (b"(a)(b)(c)(d)(e)(f)(g)(h)\\k<8>".as_slice(), "abcdefghh"),
    ] {
        let re = Regex::new_bytes(pattern).unwrap();
        assert!(re.is_match(input), "pattern: {pattern:?}, input: {input:?}");
    }
}

#[test]
fn lookahead() {
    let re = Regex::new(r"\d+(?= dollars)").unwrap();
    let m = re.find("I have 42 dollars").unwrap();
    assert_eq!(m.as_str(), "42");
}

#[test]
fn lookbehind() {
    let re = Regex::new(r"(?<=\$)\d+").unwrap();
    let m = re.find("price: $99").unwrap();
    assert_eq!(m.as_str(), "99");
}

#[test]
fn possessive_quantifier() {
    let re = Regex::new(r"a++b").unwrap();
    // "aab" should match since possessive consumes all 'a's, then finds 'b'
    assert!(re.is_match("aab"));
    // "aa" should not match since possessive consumes 'a's but no 'b'
    assert!(!re.is_match("aa"));
}

#[test]
fn date_extraction() {
    let re = Regex::new(r"(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})").unwrap();
    let caps = re.captures("Today is 2026-02-14.").unwrap();
    assert_eq!(caps.name("year").unwrap().as_str(), "2026");
    assert_eq!(caps.name("month").unwrap().as_str(), "02");
    assert_eq!(caps.name("day").unwrap().as_str(), "14");
    assert_eq!(caps.get(0).unwrap().as_str(), "2026-02-14");
}

#[test]
fn debug_impl() {
    let re = Regex::new(r"\d+").unwrap();
    let dbg = format!("{:?}", re);
    assert!(dbg.contains("Regex"));
}

#[test]
fn captures_debug_impl() {
    let re = Regex::new(r"(\d+)").unwrap();
    let caps = re.captures("42").unwrap();
    let dbg = format!("{:?}", caps);
    assert!(!dbg.is_empty());
}

// =========================================================================
// Coverage-targeted tests: Unicode case folding (unicode/mod.rs)
// =========================================================================

#[test]
fn case_insensitive_unicode_latin() {
    // Exercises apply_case_fold1 for non-ASCII Latin characters (ä/Ä, ö/Ö, etc.)
    let re = Regex::builder(r"straße")
        .case_insensitive(true)
        .build()
        .unwrap();
    assert!(re.is_match("STRASSE"));
    assert!(re.is_match("Straße"));
}

#[test]
fn case_insensitive_latin1_simple_fold_uses_multibyte_class() {
    let re = Regex::new(r"(?i)ö").unwrap();

    assert!(re.is_match("ö"));
    assert!(re.is_match("Ö"));
    assert!(!re.is_match_bytes(&[0xd6, 0x96])); // U+0596, not Ö
}

#[test]
fn case_insensitive_unicode_greek() {
    let re = Regex::builder(r"σ").case_insensitive(true).build().unwrap();
    assert!(re.is_match("Σ")); // capital sigma
    assert!(re.is_match("ς")); // final sigma
}

#[test]
fn case_insensitive_multi_char_fold() {
    // Exercises folds2/folds3 paths: ffi (U+FB03) folds to "ffi"
    let re = Regex::builder(r"ffi")
        .case_insensitive(true)
        .build()
        .unwrap();
    assert!(re.is_match("ffi"));
    assert!(re.is_match("FFI"));
}

#[test]
fn case_insensitive_char_class_unicode() {
    // Character class with case-insensitive Unicode
    let re = Regex::builder(r"[äöü]+")
        .case_insensitive(true)
        .build()
        .unwrap();
    assert!(re.is_match("ÄÖÜ"));
    assert!(re.is_match("äöü"));
}

// =========================================================================
// Coverage-targeted tests: Word boundary opcodes (regexec.rs WordStar etc.)
// =========================================================================

#[test]
fn word_star_non_ascii() {
    // Exercises WordStar non-ASCII path in match engine
    let re = Regex::new(r"\w*").unwrap();
    let m = re.find("café").unwrap();
    assert_eq!(m.as_str(), "café");
}

#[test]
fn word_star_peek_next() {
    // Exercises WordAsciiStarPeekNext opcode: \w* followed by specific char
    let re = Regex::new(r"\w*:").unwrap();
    let m = re.find("key: value").unwrap();
    assert_eq!(m.as_str(), "key:");
}

#[test]
fn word_boundary_unicode() {
    // Exercises Unicode word boundary code paths
    let re = Regex::new(r"\b\w+\b").unwrap();
    let results: Vec<&str> = re
        .find_iter("hello·world café")
        .map(|m| m.as_str())
        .collect();
    assert!(results.contains(&"hello"));
    assert!(results.contains(&"café"));
}

// =========================================================================
// Coverage-targeted tests: Subexpression calls (regcomp.rs tune_called_state)
// =========================================================================

#[test]
fn subexpression_call_in_quantifier() {
    // Exercises tune_called_state_call Quant branch
    let re = Regex::new(r"(?<digit>\d)(\g<digit>)+").unwrap();
    assert!(re.is_match("123"));
}

#[test]
fn subexpression_call_in_lookahead() {
    // Exercises tune_called_state_call Anchor/PREC_READ branch
    // \g<d> calls the pattern \d, not the captured value
    let re = Regex::new(r"(?<d>\d)(?=\g<d>)\d").unwrap();
    assert!(re.is_match("11"));
    assert!(re.is_match("12")); // \g<d> matches any digit
}

#[test]
fn subexpression_call_in_negative_lookahead() {
    // Exercises tune_called_state_call PREC_READ_NOT branch
    let re = Regex::new(r"(?<d>\d)(?!\g<d>)x").unwrap();
    assert!(re.is_match("1x")); // \g<d> matches digit but 'x' is not a digit
    assert!(!re.is_match("12")); // no 'x' after digit
}

#[test]
fn subexpression_call_in_lookbehind() {
    // Exercises tune_called_state_call LOOK_BEHIND branch
    let re = Regex::new(r"(?<d>\d)(?<=\g<d>)\w").unwrap();
    assert!(re.is_match("1a"));
}

#[test]
fn if_else_conditional() {
    // Exercises tune_called_state_call IfElse branch
    let re = Regex::new(r"^(a)?(?(1)b|c)$").unwrap();
    assert!(re.is_match("ab"));
    assert!(re.is_match("c"));
}

// =========================================================================
// Coverage-targeted tests: BackRef with nesting level (regexec.rs)
// =========================================================================

#[test]
fn backref_with_level() {
    // Exercises BackRefWithLevel opcode (backrefs in subexpression calls)
    // \k<b+0> is level-backref syntax from Oniguruma
    let re = Regex::new(r"\A(?<a>|.|(?:(?<b>.)\g<a>\k<b+0>))\z").unwrap();
    // Palindrome detector via recursive subexpression call with level-backref
    assert!(re.is_match("a"));
    assert!(re.is_match("aba"));
}

// =========================================================================
// Coverage-targeted tests: Anchors in search (regexec.rs sub_anchor paths)
// =========================================================================

#[test]
fn begin_line_anchor_multiline() {
    // Default: ^ matches start of string only
    let re = Regex::new(r"^\w+").unwrap();
    let results: Vec<&str> = re
        .find_iter("hello\nworld\nfoo")
        .map(|m| m.as_str())
        .collect();
    // Oniguruma: ^ is multiline by default
    assert!(!results.is_empty());
}

#[test]
fn end_line_anchor() {
    let re = Regex::new(r"\w+$").unwrap();
    let m = re.find("hello world").unwrap();
    assert_eq!(m.as_str(), "world");
}

#[test]
fn multiline_anchors() {
    let re = Regex::builder(r"^\w+$")
        .dot_matches_newline(false)
        .build()
        .unwrap();
    let m = re.find("hello").unwrap();
    assert_eq!(m.as_str(), "hello");
}

// `^` never matches at the very end of the subject, not even right after a
// trailing newline (C: BEGIN_LINE requires `!ON_STR_END(s)`). Expectations
// checked against C Oniguruma (ONIG_SYNTAX_ONIGURUMA, UTF-8).

#[test]
fn begin_line_does_not_match_at_end_after_trailing_newline() {
    use ferroni::encodings::utf8::ONIG_ENCODING_UTF8;
    use ferroni::oniguruma::{ONIG_MISMATCH, ONIG_OPTION_NONE, OnigRegion};
    use ferroni::regcomp::onig_new;
    use ferroni::regexec::{onig_match, onig_search};
    use ferroni::regsyntax::OnigSyntaxOniguruma;

    let compile = |pattern: &str| {
        onig_new(
            pattern.as_bytes(),
            ONIG_OPTION_NONE,
            &ONIG_ENCODING_UTF8,
            &OnigSyntaxOniguruma,
        )
        .unwrap()
    };

    // (pattern, subject, start, range, expected position)
    for (pattern, text, start, range, expected) in [
        ("^", "a\n", 2, 2, ONIG_MISMATCH),
        ("^", "a\n", 1, 2, ONIG_MISMATCH),
        ("^", "\n", 1, 1, ONIG_MISMATCH),
        ("^$", "a\n", 1, 2, ONIG_MISMATCH),
        ("(?m)^.*", "a\n", 2, 2, ONIG_MISMATCH),
        ("^", "a\nb\n", 3, 4, ONIG_MISMATCH),
        // Backward search still finds the start of the subject.
        ("^", "a\n", 2, 0, 0),
        // An empty line before the final newline is still a line.
        ("^", "a\n\n", 1, 3, 2),
        ("^$", "a\n\n", 1, 3, 2),
        // `$` keeps matching at the end after a trailing newline.
        ("$", "a\n", 2, 2, 2),
    ] {
        let reg = compile(pattern);
        let bytes = text.as_bytes();
        let (position, _) = onig_search(
            &reg,
            bytes,
            bytes.len(),
            start,
            range,
            Some(OnigRegion::new()),
            ONIG_OPTION_NONE,
        );
        assert_eq!(
            position, expected,
            "{pattern:?} on {text:?} start={start} range={range}"
        );
    }

    let reg = compile("^");
    let (length, _) = onig_match(&reg, b"a\n", 2, 2, None, ONIG_OPTION_NONE);
    assert_eq!(length, ONIG_MISMATCH);
    let (length, _) = onig_match(&reg, b"\n\n", 2, 1, None, ONIG_OPTION_NONE);
    assert_eq!(length, 0);
}

#[test]
fn find_iter_begin_line_skips_end_after_trailing_newline() {
    let starts = |pattern: &str, text: &str| -> Vec<usize> {
        Regex::new(pattern)
            .unwrap()
            .find_iter(text)
            .map(|m| m.start())
            .collect()
    };
    assert_eq!(starts(r"(?m)^$", "a\nb\n"), Vec::<usize>::new());
    assert_eq!(starts(r"^", "a\nb\n"), vec![0, 2]);
    assert_eq!(starts(r"^$", "a\n\n"), vec![2]);
    assert_eq!(starts(r"$", "a\nb\n"), vec![1, 3, 4]);
}

// =========================================================================
// Coverage-targeted tests: Callout patterns (regexec.rs callout paths)
// =========================================================================

#[test]
fn callout_max_count() {
    // Exercises CALLOUT_BUILTIN_MAX progress/retraction paths
    let re = Regex::new(r"(?:(*MAX{5})a)*").unwrap();
    let m = re.find("aaaaaa").unwrap();
    assert!(m.len() <= 6);
}

#[test]
fn callout_count() {
    // Exercises CALLOUT_BUILTIN_COUNT path
    let re = Regex::new(r"(?:(*COUNT)a)+").unwrap();
    assert!(re.is_match("aaa"));
}

// =========================================================================
// Coverage-targeted tests: Various parser paths (regparse.rs)
// =========================================================================

#[test]
fn possessive_repeat() {
    let re = Regex::new(r"[a-z]++").unwrap();
    assert!(re.is_match("abc"));
}

#[test]
fn named_group_multiple_alternation() {
    // Named groups in alternation
    let re = Regex::new(r"(?<x>a)|(?<x>b)").unwrap();
    assert!(re.is_match("a"));
    assert!(re.is_match("b"));
}

#[test]
fn unicode_property_negated() {
    let re = Regex::new(r"\P{Lu}+").unwrap();
    let m = re.find("ABCdef").unwrap();
    assert_eq!(m.as_str(), "def");
}

#[test]
fn extended_grapheme_cluster() {
    // Exercises EGCB path in unicode/mod.rs
    let re = Regex::new(r"\X").unwrap();
    // Flag emoji: regional indicators
    let m = re.find("🇩🇪x").unwrap();
    assert!(m.len() > 1); // Should match the whole flag as one cluster
}

#[test]
fn word_boundary_types() {
    // Exercises WB algorithm paths including Hebrew, Katakana, ExtendNumLet
    let re = Regex::new(r"\b\w+\b").unwrap();

    // Katakana
    assert!(re.is_match("カタカナ"));

    // Numbers
    let m = re.find("abc123def").unwrap();
    assert_eq!(m.as_str(), "abc123def");
}

#[test]
fn absent_expression() {
    // Exercises absent group compilation in regparse + regcomp
    let re = Regex::new(r"(?~abc)").unwrap();
    let m = re.find("xxabcyy").unwrap();
    assert_eq!(m.as_str(), "xx");
}

#[test]
fn nested_repeat_with_capture() {
    let re = Regex::new(r"((a){2,4}){1,3}").unwrap();
    let m = re.find("aaaa").unwrap();
    assert_eq!(m.as_str(), "aaaa");
}

#[test]
fn character_class_intersection() {
    // Exercises char class intersection parsing
    let re = Regex::new(r"[a-z&&[^aeiou]]+").unwrap();
    let m = re.find("abcde").unwrap();
    assert_eq!(m.as_str(), "bcd");
}

#[test]
fn hex_escape_unicode() {
    let re = Regex::new(r"\x{1F600}").unwrap();
    assert!(re.is_match("😀"));
}

#[test]
fn keep_marker() {
    // \K resets the match start
    let re = Regex::new(r"foo\Kbar").unwrap();
    let m = re.find("foobar").unwrap();
    assert_eq!(m.as_str(), "bar");
}

#[test]
fn atomic_group() {
    let re = Regex::new(r"(?>abc|ab)c").unwrap();
    // "abc" is consumed atomically, then "c" fails → no match for "abc"
    assert!(!re.is_match("abc"));
    assert!(re.is_match("abcc"));
}

#[test]
fn lazy_quantifier_in_capture() {
    let re = Regex::new(r"(a+?)(a+)").unwrap();
    let caps = re.captures("aaaa").unwrap();
    assert_eq!(caps.get(1).unwrap().as_str(), "a");
    assert_eq!(caps.get(2).unwrap().as_str(), "aaa");
}

#[test]
fn char_class_posix() {
    let re = Regex::new(r"[[:upper:]]+").unwrap();
    let m = re.find("abcDEF").unwrap();
    assert_eq!(m.as_str(), "DEF");
}

#[test]
fn non_greedy_repeat() {
    let re = Regex::new(r"<.+?>").unwrap();
    let m = re.find("<a><b>").unwrap();
    assert_eq!(m.as_str(), "<a>");
}

// =========================================================================
// Coverage-targeted: Parser paths (regparse.rs)
// =========================================================================

#[test]
fn quote_literal_syntax() {
    // Exercises \Q...\E literal quoting (TokenType::QuoteOpen) — only in Perl syntax
    use ferroni::regsyntax::OnigSyntaxPerl;
    let re = Regex::builder(r"\Q.+*\E")
        .syntax(&OnigSyntaxPerl)
        .build()
        .unwrap();
    assert!(re.is_match(".+*"));
    assert!(!re.is_match("abc"));
}

#[test]
fn char_class_dash_at_end() {
    // Exercises CC dash handling: [a-z-] → literal dash at end
    let re = Regex::new(r"[a-z-]+").unwrap();
    assert!(re.is_match("abc-"));
    let m = re.find("ABC-def").unwrap();
    assert_eq!(m.as_str(), "-def");
}

#[test]
fn char_class_range_and_dash() {
    // Exercises double-range handling: [0-9-a]
    let re = Regex::new(r"[0-9-a]+").unwrap();
    assert!(re.is_match("5-a"));
}

#[test]
fn named_backref_with_level() {
    // Exercises level syntax parsing in fetch_name: \k<name+1>. Both
    // occurrences of the called group capture one call level below the top,
    // so +1 finds them and +0 does not (checked against C Oniguruma).
    let re = Regex::new(r"(?<a>x)\g<a>\k<a+1>").unwrap();
    assert!(re.is_match("xxx"));
    let re = Regex::new(r"(?<a>x)\g<a>\k<a+0>").unwrap();
    assert!(!re.is_match("xxx"));
}

#[test]
fn numbered_backref_with_level() {
    // Exercises numeric level backref parsing: \k<1+1> and \k<1+0>
    let re = Regex::new(r"(x)\g<1>\k<1+1>").unwrap();
    assert!(re.is_match("xxx"));
    let re = Regex::new(r"(x)\g<1>\k<1+0>").unwrap();
    assert!(!re.is_match("xxx"));
}

#[test]
fn char_class_negated_intersection() {
    // Exercises negated char class with intersection
    let re = Regex::new(r"[^a-z&&[^m-z]]+").unwrap();
    // [^a-z&&[^m-z]] = not(a-z AND not(m-z)) = not(a-l) = anything except a-l
    assert!(re.is_match("M"));
}

#[test]
fn control_escape_sequences() {
    // Exercises control code syntax path
    let re = Regex::new(r"\ca").unwrap(); // \ca = control-A = 0x01
    assert!(re.is_match("\x01"));
}

#[test]
fn hex_escape_2digit() {
    let re = Regex::new(r"\x41").unwrap(); // 0x41 = 'A'
    assert!(re.is_match("A"));
}

#[test]
fn octal_escape() {
    let re = Regex::new(r"\101").unwrap(); // 0101 octal = 65 = 'A'
    assert!(re.is_match("A"));
}

#[test]
fn backref_with_negative_level() {
    // Exercises level syntax with negative sign: \k<name-1>
    // This tests the level_sign = -1 path
    let re = Regex::new(r"(?<a>(?<b>x)\g<a>|\w)\k<b-0>");
    // Either succeeds or fails at compile — either way exercises the parser
    if let Ok(re) = re {
        let _ = re.is_match("xyx");
    }
}

#[test]
fn unicode_named_property() {
    // Various Unicode property names to exercise property_name_to_ctype
    let re = Regex::new(r"\p{Katakana}+").unwrap();
    assert!(re.is_match("カタカナ"));

    let re = Regex::new(r"\p{Han}+").unwrap();
    assert!(re.is_match("漢字"));
}

#[test]
fn unicode_17_script_properties_and_case_folding() {
    for (property, character) in [
        (r"\p{Sidetic}", '\u{10940}'),
        (r"\p{Tolong_Siki}", '\u{11DB0}'),
        (r"\p{Beria_Erfe}", '\u{16EA0}'),
        (r"\p{Tai_Yo}", '\u{1E6C0}'),
    ] {
        let re = Regex::new(property).unwrap();
        assert!(re.is_match(&character.to_string()), "{property}");
    }

    let uppercase = Regex::new(&format!("(?i){}", '\u{16EA0}')).unwrap();
    assert!(uppercase.is_match("\u{16ebb}"));
}

#[test]
fn unicode_18_script_properties_and_case_folding() {
    // Seal, Jurchen and Proto_Cuneiform are new scripts in Unicode 18.0
    // (Scripts.txt).
    for (property, character) in [
        (r"\p{Seal}", '\u{3D000}'),
        (r"\p{Jurchen}", '\u{18E00}'),
        (r"\p{Proto_Cuneiform}", '\u{125A8}'),
    ] {
        let re = Regex::new(property).unwrap();
        assert!(re.is_match(&character.to_string()), "{property}");
    }

    // U+A7DD LATIN CAPITAL LETTER CLOSED OMEGA is new in 18.0 and folds to
    // U+0277.
    let folded = Regex::new(&format!("(?i){}", '\u{0277}')).unwrap();
    assert!(folded.is_match("\u{a7dd}"));

    // U+1DF95 LATIN SMALL LIGATURE LONG S WITH DESCENDER S is new in 18.0 and
    // full-folds to "ss", so "ss" matches it case-insensitively (ADR-015).
    let long_s = Regex::new("(?i)ss").unwrap();
    assert!(long_s.is_match("\u{1df95}"));
}

#[test]
fn unicode_incb_covers_every_section() {
    // DerivedCoreProperties.txt lists InCB as Linker, Consonant, and Extend
    // sections out of code point order. C Oniguruma drops ranges when it merges
    // them; Ferroni intentionally keeps all of them (ADR-015).
    let re = Regex::new(r"\p{InCB}").unwrap();
    for (section, character) in [
        ("Linker", '\u{094D}'),
        ("Consonant", '\u{0915}'),
        ("Extend", '\u{0300}'),
        ("Extend", '\u{200D}'),
        ("Extend", '\u{0DCA}'),
    ] {
        assert!(
            re.is_match(&character.to_string()),
            "{section} U+{:04X}",
            character as u32
        );
    }
    assert!(!re.is_match("a"));
}

#[test]
fn absent_clear_expression() {
    // Exercises absent group range stop: (?~|pattern|absent)
    let re = Regex::new(r"(?~|abc|[a-z]+)");
    if let Ok(re) = re {
        let _ = re.find("xyzabcdef");
    }
}

// =========================================================================
// Coverage-targeted: Compiler paths (regcomp.rs)
// =========================================================================

#[test]
fn alternation_many_branches() {
    // Exercises select_opt_map/trie path in regcomp
    let re = Regex::new(r"apple|banana|cherry|date|elderberry|fig|grape").unwrap();
    assert_eq!(re.find("I like banana").unwrap().as_str(), "banana");
}

#[test]
fn nested_quantifier() {
    // Exercises nested quantifier optimization in regcomp
    let re = Regex::new(r"(a{2,3}){2,4}").unwrap();
    assert!(re.is_match("aaaaaa"));
}

#[test]
fn complex_lookbehind() {
    // Exercises lookbehind compilation with variable-length content
    let re = Regex::new(r"(?<=ab|cd)\w+").unwrap();
    assert_eq!(re.find("cdHello").unwrap().as_str(), "Hello");
}

#[test]
fn case_insensitive_backref() {
    // Exercises BackRefIc opcode generation
    let re = Regex::builder(r"(\w+)\s+\1")
        .case_insensitive(true)
        .build()
        .unwrap();
    assert!(re.is_match("Hello HELLO"));
}

#[test]
fn many_capture_groups() {
    // Exercises capture group region handling with many groups
    let re = Regex::new(r"(a)(b)(c)(d)(e)(f)(g)(h)(i)(j)").unwrap();
    let caps = re.captures("abcdefghij").unwrap();
    assert_eq!(caps.len(), 11); // group 0 + 10 captures
    assert_eq!(caps.get(10).unwrap().as_str(), "j");
}

#[test]
fn forward_reference() {
    // Exercises forward backref in parser
    let re = Regex::new(r"(\2?(a))+").unwrap();
    assert!(re.is_match("aaa"));
}

// =========================================================================
// Coverage-targeted: Execution engine paths (regexec.rs)
// =========================================================================

#[test]
fn match_at_end_of_string() {
    let re = Regex::new(r"\w+\z").unwrap();
    let m = re.find("hello world").unwrap();
    assert_eq!(m.as_str(), "world");
}

#[test]
fn empty_match_progression() {
    // Tests empty match handling and advancement logic
    let re = Regex::new(r"(?:)+").unwrap();
    let results: Vec<_> = re.find_iter("ab").collect();
    assert!(!results.is_empty());
}

#[test]
fn long_alternation_literal() {
    // Exercises literal trie optimization in search
    let re = Regex::new(r"cat|car|cap|can|cam|cab").unwrap();
    assert_eq!(re.find("a cab ride").unwrap().as_str(), "cab");
}

#[test]
fn unicode_case_fold_in_char_class_range() {
    // Exercises get_case_fold_codes_by_str for multi-char sequences
    let re = Regex::builder(r"[a-ÿ]+")
        .case_insensitive(true)
        .build()
        .unwrap();
    assert!(re.is_match("Ä"));
    assert!(re.is_match("ß"));
}

#[test]
fn word_boundary_at_boundaries() {
    // Exercises edge cases in word boundary checking
    let re = Regex::new(r"\bword\b").unwrap();
    assert!(re.is_match("word"));
    assert!(re.is_match("a word b"));
    assert!(!re.is_match("sword"));
    assert!(!re.is_match("wordy"));
}

#[test]
fn dotall_mode() {
    let re = Regex::builder(r"a.b")
        .dot_matches_newline(true)
        .build()
        .unwrap();
    assert!(re.is_match("a\nb"));
    assert!(re.is_match("a\rb"));
}

#[test]
fn captures_iter_with_optional_groups() {
    let re = Regex::new(r"(?:(a)|(b))+").unwrap();
    let caps = re.captures("abba").unwrap();
    // Groups 1 and 2 alternately match
    assert!(caps.get(0).is_some());
}

// =========================================================================
// Coverage-targeted: Extended grapheme cluster / word break (unicode/mod.rs)
// =========================================================================

#[test]
fn egcb_flag_emoji_sequence() {
    // Regional indicators: 🇩🇪 = U+1F1E9 U+1F1EA
    let re = Regex::new(r"\X+").unwrap();
    let m = re.find("🇩🇪🇫🇷x").unwrap();
    // Each flag pair is one grapheme cluster
    assert!(m.len() > 4);
}

#[test]
fn egcb_zwj_sequence() {
    // ZWJ sequence: 👨‍👩‍👧 (man + ZWJ + woman + ZWJ + girl)
    let re = Regex::new(r"\X").unwrap();
    let m = re.find("👨\u{200D}👩\u{200D}👧x").unwrap();
    // Should match the whole ZWJ family as one grapheme cluster
    assert!(m.len() > 4);
}

#[test]
fn word_boundary_hebrew() {
    // Exercises HebrewLetter WB rules (WB7a, WB7b, WB7c)
    let re = Regex::new(r"\b\w+\b").unwrap();
    assert!(re.is_match("שלום")); // Hebrew "shalom"
}

#[test]
fn word_boundary_regional_indicators() {
    // Exercises WB15/WB16 RI x RI counting
    let re = Regex::new(r"\b.+\b").unwrap();
    let _ = re.find("🇩🇪🇫🇷"); // just exercise the path
}

#[test]
fn word_boundary_extend_num_let() {
    // Exercises WB13a/WB13b ExtendNumLet rules
    // Underscore (U+005F) is ExtendNumLet type
    let re = Regex::new(r"\b\w+\b").unwrap();
    let m = re.find("hello_world").unwrap();
    assert_eq!(m.as_str(), "hello_world"); // underscore connects words
}

// =========================================================================
// Coverage-targeted: Unicode multi-char case folds (unicode/mod.rs)
// =========================================================================

#[test]
fn case_insensitive_2char_fold() {
    // fi (U+FB01) folds to "fi" — exercises folds2 paths (apply_case_fold2, get_case_fold_codes folds2)
    let re = Regex::builder("fi").case_insensitive(true).build().unwrap();
    assert!(re.is_match("fi"));
    assert!(re.is_match("FI"));
}

#[test]
fn case_insensitive_3char_fold() {
    // ffi (U+FB03) folds to "ffi" — exercises folds3 paths
    let re = Regex::builder("ffi")
        .case_insensitive(true)
        .build()
        .unwrap();
    assert!(re.is_match("ffi"));
    assert!(re.is_match("FFI"));
}

#[test]
fn case_insensitive_eszett() {
    // ß folds to "ss" — another 2-char fold
    let re = Regex::builder("ss").case_insensitive(true).build().unwrap();
    assert!(re.is_match("ss"));
    assert!(re.is_match("SS"));
    assert!(re.is_match("ß"));
}

#[test]
fn case_insensitive_char_class_with_unicode_folds() {
    // Character class with case-insensitive: exercises get_case_fold_codes_by_str
    let re = Regex::builder("[ß]")
        .case_insensitive(true)
        .build()
        .unwrap();
    assert!(re.is_match("ß"));
    assert!(re.is_match("SS")); // ss should match ß case-insensitively
}

#[test]
fn case_insensitive_turkish_dotted_i() {
    // İ (U+0130) and ı (U+0131) — locale-specific fold entries
    let re = Regex::builder("i").case_insensitive(true).build().unwrap();
    assert!(re.is_match("I"));
    assert!(re.is_match("i"));
}

#[test]
fn case_insensitive_full_width() {
    // Full-width letters: A (U+FF21) should fold to a (U+FF41)
    let re = Regex::builder("\u{FF21}")
        .case_insensitive(true)
        .build()
        .unwrap();
    assert!(re.is_match("\u{FF21}"));
    assert!(re.is_match("\u{FF41}"));
}

// =========================================================================
// Coverage-targeted: EGCB and WB edge cases (unicode/mod.rs)
// =========================================================================

#[test]
fn egcb_regional_indicator_pairs() {
    // 🇩🇪🇫🇷🇯🇵 = 3 flag sequences, each 2 RI codepoints
    // Exercises BreakUndefRiRi counting
    let re = Regex::new(r"\X").unwrap();
    let input = "🇩🇪🇫🇷🇯🇵";
    let clusters: Vec<_> = re.find_iter(input).collect();
    // Should be 3 grapheme clusters (one per flag)
    assert_eq!(clusters.len(), 3);
}

#[test]
fn wb_midletter_between_letters() {
    // WB6/WB7: AHLetter x (MidLetter|MidNumLetQ) x AHLetter
    // · (U+00B7, MidLetter) between letters should NOT break
    let re = Regex::new(r"\b\w+\b").unwrap();
    // Test with apostrophe (U+2019, MidNumLetQ) in word
    let m = re.find("it's").unwrap();
    assert!(!m.as_str().is_empty());
}

#[test]
fn wb_numeric_sequences() {
    // WB8-WB11: Numeric x Numeric, Numeric x (MidNum|MidNumLetQ) x Numeric
    let re = Regex::new(r"\b[\w.]+\b").unwrap();
    assert!(re.is_match("3.14"));
}

#[test]
fn wb_katakana_extend_num_let() {
    // WB13: Katakana x Katakana + WB13a/b with ExtendNumLet
    let re = Regex::new(r"\b\w+\b").unwrap();
    let m = re.find("カタカナ_テスト").unwrap();
    // Katakana + underscore + Katakana should be one word
    assert_eq!(m.as_str(), "カタカナ_テスト");
}

// =========================================================================
// Coverage-targeted: Perl-style subexpression call syntax (regparse.rs)
// =========================================================================

#[test]
fn perl_named_call_syntax() {
    // (?&name) - named subroutine call (Perl_NG syntax)
    // Exercises ONIG_SYN_OP2_QMARK_PERL_SUBEXP_CALL parser path
    use ferroni::regsyntax::OnigSyntaxPerl_NG;
    let re = Regex::builder(r"(?<d>\d+)x(?&d)")
        .syntax(&OnigSyntaxPerl_NG)
        .build()
        .unwrap();
    assert!(re.is_match("123x456"));
    assert!(!re.is_match("123xabc"));
}

#[test]
fn perl_numbered_subexp_call() {
    // (?1) - absolute numbered call (Perl_NG syntax)
    use ferroni::regsyntax::OnigSyntaxPerl_NG;
    let re = Regex::builder(r"(\d+)-(?1)")
        .syntax(&OnigSyntaxPerl_NG)
        .build()
        .unwrap();
    assert!(re.is_match("123-456"));
}

#[test]
fn perl_relative_subexp_call() {
    // (?-1) - relative backward call (Perl_NG syntax)
    use ferroni::regsyntax::OnigSyntaxPerl_NG;
    let re = Regex::builder(r"(\d+)-(?-1)")
        .syntax(&OnigSyntaxPerl_NG)
        .build()
        .unwrap();
    assert!(re.is_match("42-99"));
}

// =========================================================================
// Coverage-targeted: Meta and control escape sequences (regparse.rs)
// =========================================================================

#[test]
fn ruby_meta_escape() {
    // \M-a sets the high bit: 'a' (0x61) | 0x80 = 0xe1
    // Using new_bytes since \M-a produces a non-UTF8 byte
    use ferroni::regsyntax::OnigSyntaxRuby;
    let re = Regex::builder(r"\M-a")
        .syntax(&OnigSyntaxRuby)
        .build()
        .unwrap();
    // In UTF-8, 0xe1 starts a 3-byte sequence. Test with raw bytes.
    assert!(re.is_match_bytes(&[0xc3, 0xa1])); // UTF-8 for U+00E1 (á)
}

#[test]
fn ruby_control_escape_c_bar() {
    // \C-a = control-A = 0x01
    use ferroni::regsyntax::OnigSyntaxRuby;
    let re = Regex::builder(r"\C-a")
        .syntax(&OnigSyntaxRuby)
        .build()
        .unwrap();
    assert!(re.is_match_bytes(&[0x01]));
}

// =========================================================================
// Coverage-targeted: Variable-length lookbehind (regcomp.rs)
// =========================================================================

#[test]
fn variable_length_lookbehind() {
    // (?<=a|ab) - lookbehind with variable-length alternatives
    let re = Regex::new(r"(?<=a|ab)x").unwrap();
    assert!(re.is_match("ax"));
    assert!(re.is_match("abx"));
    assert!(!re.is_match("bx"));
}

#[test]
fn variable_length_negative_lookbehind() {
    // (?<!ab|abc) - negative lookbehind with variable lengths
    let re = Regex::new(r"(?<!ab|abc)x").unwrap();
    assert!(re.is_match("cx"));
    assert!(!re.is_match("abx"));
}

// =========================================================================
// Coverage-targeted: Subroutine calls in compiler (regcomp.rs)
// =========================================================================

#[test]
fn subroutine_call_with_quantifier() {
    // (?<r>a+)\g<r> - subroutine call re-executes the pattern (not the captured text)
    let re = Regex::new(r"(?<r>a+)\g<r>").unwrap();
    assert!(re.is_match("aaa"));
    assert!(!re.is_match("b"));
}

#[test]
fn subroutine_call_in_if_else() {
    // Conditional pattern with subroutine call in then-branch
    // Exercises tune_called_state for if-else structures
    let re = Regex::new(r"(?<a>x)?(?(<a>)\g<a>|y)").unwrap();
    // When "x" is present, the conditional matches and \g<a> matches another "x"
    assert!(re.is_match("xx"));
    // When no "x", the else branch matches "y"
    assert!(re.is_match("y"));
}

#[test]
fn recursive_capture_group() {
    // Recursive pattern with captures - exercises MemEndPushRec
    let re = Regex::new(r"^(?<r>a(?:\g<r>)?b)$").unwrap();
    assert!(re.is_match("ab"));
    assert!(re.is_match("aabb"));
    assert!(re.is_match("aaabbb"));
    assert!(!re.is_match("aab"));
}

// =========================================================================
// Coverage-targeted: Backref with level syntax (regparse.rs + regcomp.rs)
// =========================================================================

#[test]
fn backref_with_level_named() {
    // \k<name+0> - backreference with explicit level in recursive context
    // In a recursive call, level 0 refers to the current recursion's capture
    let re = Regex::new(r"(?<a>[a-z])(?:\g<a>\k<a+0>)?").unwrap();
    assert!(re.is_match("a"));
    assert!(re.is_match("abc"));
}

// =========================================================================
// Coverage-targeted: WordAsciiStar opcode (regexec.rs)
// =========================================================================

#[test]
fn word_ascii_star_optimization() {
    // (?W)\w* triggers WordAsciiStar opcode (ASCII-only word matching)
    let re = Regex::new(r"(?W)\w*$").unwrap();
    assert!(re.is_match("hello"));
    assert!(re.is_match(""));
}

#[test]
fn word_ascii_star_peek_next_optimization() {
    // (?W)\w*x triggers WordAsciiStarPeekNext opcode
    let re = Regex::new(r"(?W)\w*x").unwrap();
    assert!(re.is_match("abcx"));
    assert!(re.is_match("x"));
    assert!(!re.is_match("abc"));
}

// =========================================================================
// Coverage-targeted: Scanner \G anchor (scanner.rs)
// =========================================================================

#[test]
fn scanner_g_anchor_pattern() {
    use ferroni::scanner::{Scanner, ScannerFindOptions};
    // A scanner with a \G anchor pattern exercises search_g_anchor_with_msa
    let mut scanner = Scanner::new(&[r"\G\w+", r"\s+"]).unwrap();
    let input = "hello world";
    let result = scanner.find_next_match(input, 0, ScannerFindOptions::NONE);
    assert!(result.is_some());
    let m = result.unwrap();
    assert_eq!(m.index, 0); // \G\w+ matches at start
    assert_eq!(m.captures()[0].start, 0);
}

// =========================================================================
// Coverage-targeted: Character class with range and dash (regparse.rs)
// =========================================================================

#[test]
fn char_class_dash_in_range_context() {
    // [!--] - dash at end of range (exercises CS_RANGE with dash)
    let re = Regex::new(r"[!--]").unwrap();
    assert!(re.is_match("!"));
    assert!(re.is_match("-"));
    assert!(re.is_match("#"));
    assert!(!re.is_match("0"));
}

#[test]
fn char_class_double_range() {
    // [0-9-a] - double range operator (exercises ALLOW_DOUBLE_RANGE_OP_IN_CC)
    let re = Regex::new(r"[0-9-a]").unwrap();
    assert!(re.is_match("5"));
    assert!(re.is_match("a"));
    assert!(re.is_match("-"));
}

// =========================================================================
// Coverage-targeted: Absent expression (regparse.rs + regcomp.rs)
// =========================================================================

#[test]
fn absent_expression_basic() {
    // (?~|pattern) - absent stopper expression
    let re = Regex::new(r"(?~abc)").unwrap();
    assert!(re.is_match("xyz"));
}

#[test]
fn absent_expression_with_range() {
    // (?~|...) - absent with explicit range
    let re = Regex::new(r"\w+(?~abc)\w+").unwrap();
    assert!(re.is_match("xyzdef"));
}

// =========================================================================
// Coverage-targeted: Conditional with DEFINE (regparse.rs)
// =========================================================================

#[test]
fn conditional_define_pattern() {
    // (?(DEFINE)(?<d>\d+)) - define-only group, not directly matched
    // Uses the defined pattern later via \g<d>
    let re = Regex::new(r"(?(DEFINE)(?<d>\d+))\g<d>-\g<d>").unwrap();
    assert!(re.is_match("123-456"));
    assert!(!re.is_match("abc-def"));
}

// =========================================================================
// Coverage-targeted: Named capture in anchors (regcomp.rs)
// =========================================================================

#[test]
fn named_capture_in_lookahead() {
    // Named capture inside lookahead exercises make_named_capture_number_map
    let re = Regex::new(r"(?=(?<word>\w+))\w+").unwrap();
    let caps = re.captures("hello").unwrap();
    assert_eq!(caps.get(1).unwrap().as_str(), "hello");
}

#[test]
fn named_capture_in_lookbehind() {
    // Named capture inside lookbehind
    let re = Regex::new(r"(?<=(?<prefix>ab))cd").unwrap();
    assert!(re.is_match("abcd"));
}

// =========================================================================
// Coverage-targeted: Infinite recursion check (regcomp.rs)
// =========================================================================

#[test]
fn subroutine_with_alternation_avoids_infinite_recursion() {
    // (?<r>a|b\g<r>c) - recursion in one branch only (valid, not infinite)
    let re = Regex::new(r"(?<r>a|b\g<r>c)").unwrap();
    assert!(re.is_match("a"));
    assert!(re.is_match("bac"));
    assert!(re.is_match("bbaccc")); // not valid - tests recursion depth
}

// =========================================================================
// Coverage-targeted: Literal trie optimization (regcomp.rs)
// =========================================================================

#[test]
fn many_literal_alternatives_trigger_trie() {
    // Many literal alternatives with common prefixes trigger literal trie optimization
    let re = Regex::new(r"abc|abd|abe|abf|abg|abh|abi|abj").unwrap();
    assert!(re.is_match("abc"));
    assert!(re.is_match("abj"));
    assert!(!re.is_match("abz"));
}

#[test]
fn literal_alternatives_preserve_ordered_backtracking() {
    let re = Regex::new(r"(?:foo|foobar|a1|a2|a3|a4|a5|a6)bar").unwrap();
    let matched = re.find("foobar").expect("shorter first branch matches");
    assert_eq!(matched.as_str(), "foobar");

    let ordered = Regex::new(r"foo|foobar|a1|a2|a3|a4|a5|a6").unwrap();
    assert_eq!(ordered.find("foobar").unwrap().as_str(), "foo");
}

#[test]
fn literal_alternatives_preserve_per_branch_case_options() {
    let re = Regex::new(r"(?i)foo|bar|baz|(?-i:QUX)").unwrap();
    assert!(!re.is_match("qux"));
    assert!(re.is_match("QUX"));
}

#[test]
fn case_insensitive_literal_alternatives() {
    // Case-insensitive alternations exercise literal path classification
    let re = Regex::builder("ABC|DEF|GHI|JKL")
        .case_insensitive(true)
        .build()
        .unwrap();
    assert!(re.is_match("abc"));
    assert!(re.is_match("jkl"));
}

// =========================================================================
// Coverage-targeted: API methods (api.rs)
// =========================================================================

#[test]
fn find_iter_bytes_method() {
    // Exercises find_iter_bytes code path
    let re = Regex::new_bytes(b"\\d+").unwrap();
    let matches: Vec<_> = re.find_iter_bytes(b"12 34 56").collect();
    assert_eq!(matches.len(), 3);
    assert_eq!(matches[0].as_bytes(), b"12");
    assert_eq!(matches[2].as_bytes(), b"56");
}

#[test]
fn builder_multi_line_anchors_default_matches_each_line() {
    let re = Regex::builder(r"^b$").build().unwrap();

    assert_eq!(re.find("a\nb\nc").unwrap().as_str(), "b");
}

#[test]
fn builder_multi_line_anchors_true_matches_each_line() {
    let re = Regex::builder(r"^b$")
        .multi_line_anchors(true)
        .build()
        .unwrap();

    assert_eq!(re.find("a\nb\nc").unwrap().as_str(), "b");
}

#[test]
fn builder_multi_line_anchors_false_matches_only_input_boundaries() {
    let re = Regex::builder(r"^b$")
        .multi_line_anchors(false)
        .build()
        .unwrap();

    assert!(re.find("a\nb\nc").is_none());
}

#[test]
fn builder_multi_line_anchors_toggles_without_affecting_other_options() {
    let re = Regex::builder(" ^ a . b $ ")
        .case_insensitive(true)
        .dot_matches_newline(true)
        .extended(true)
        .multi_line_anchors(false)
        .multi_line_anchors(true)
        .multi_line_anchors(true)
        .build()
        .unwrap();

    assert_eq!(re.find("before\nA\nB\nafter").unwrap().as_str(), "A\nB");
}

#[test]
fn builder_multi_line_anchors_overrides_syntax_defaults() {
    use ferroni::regsyntax::{
        OnigSyntaxJava, OnigSyntaxPerl, OnigSyntaxPosixBasic, OnigSyntaxPosixExtended,
    };

    for (name, syntax) in [
        ("Java", &OnigSyntaxJava),
        ("Perl", &OnigSyntaxPerl),
        ("POSIX basic", &OnigSyntaxPosixBasic),
        ("POSIX extended", &OnigSyntaxPosixExtended),
    ] {
        let default = Regex::builder(r"^b$").syntax(syntax).build().unwrap();
        assert!(default.find("a\nb\nc").is_none(), "{name} default");

        let enabled = Regex::builder(r"^b$")
            .syntax(syntax)
            .multi_line_anchors(true)
            .build()
            .unwrap();
        assert_eq!(
            enabled.find("a\nb\nc").unwrap().as_str(),
            "b",
            "{name} enabled"
        );

        let disabled = Regex::builder(r"^b$")
            .syntax(syntax)
            .multi_line_anchors(false)
            .build()
            .unwrap();
        assert!(disabled.find("a\nb\nc").is_none(), "{name} disabled");
    }
}

#[test]
fn builder_syntax_mode_matches_syntax_statics() {
    use ferroni::regsyntax::{
        OnigSyntaxASIS, OnigSyntaxEmacs, OnigSyntaxGnuRegex, OnigSyntaxGrep, OnigSyntaxJava,
        OnigSyntaxOniguruma, OnigSyntaxPerl, OnigSyntaxPerl_NG, OnigSyntaxPosixBasic,
        OnigSyntaxPosixExtended, OnigSyntaxPython, OnigSyntaxRuby,
    };

    let pairs: [(Syntax, &'static OnigSyntaxType); 12] = [
        (Syntax::Oniguruma, &OnigSyntaxOniguruma),
        (Syntax::Asis, &OnigSyntaxASIS),
        (Syntax::PosixBasic, &OnigSyntaxPosixBasic),
        (Syntax::PosixExtended, &OnigSyntaxPosixExtended),
        (Syntax::Emacs, &OnigSyntaxEmacs),
        (Syntax::Grep, &OnigSyntaxGrep),
        (Syntax::GnuRegex, &OnigSyntaxGnuRegex),
        (Syntax::Java, &OnigSyntaxJava),
        (Syntax::Perl, &OnigSyntaxPerl),
        (Syntax::PerlNg, &OnigSyntaxPerl_NG),
        (Syntax::Ruby, &OnigSyntaxRuby),
        (Syntax::Python, &OnigSyntaxPython),
    ];
    let patterns = [r"a.b", r"^b$", r"(a)(b)", r"a+b", r"(?<n>x)\w"];
    let texts = ["a.b", "axb", "a\nb\nc", "aab", "xy", "b"];

    for (syntax, statics) in pairs {
        for pattern in patterns {
            let typed = Regex::builder(pattern).syntax_mode(syntax).build();
            let raw = Regex::builder(pattern).syntax(statics).build();
            match (typed, raw) {
                (Ok(typed), Ok(raw)) => {
                    assert_eq!(
                        typed.captures_len(),
                        raw.captures_len(),
                        "{syntax:?} {pattern}"
                    );
                    for text in texts {
                        assert_eq!(
                            typed.find(text).map(|m| m.range()),
                            raw.find(text).map(|m| m.range()),
                            "{syntax:?} {pattern} on {text:?}"
                        );
                    }
                }
                (Err(typed), Err(raw)) => assert_eq!(typed, raw, "{syntax:?} {pattern}"),
                (typed, raw) => panic!(
                    "{syntax:?} {pattern}: typed ok={}, raw ok={}",
                    typed.is_ok(),
                    raw.is_ok()
                ),
            }
        }
    }
}

#[test]
fn builder_syntax_and_syntax_mode_last_call_wins() {
    use ferroni::regsyntax::OnigSyntaxRuby;

    let re = Regex::builder("a.b")
        .syntax(&OnigSyntaxRuby)
        .syntax_mode(Syntax::Asis)
        .build()
        .unwrap();
    assert!(re.is_match("a.b"));
    assert!(!re.is_match("axb"));

    let re = Regex::builder("a.b")
        .syntax_mode(Syntax::Asis)
        .syntax(&OnigSyntaxRuby)
        .build()
        .unwrap();
    assert!(re.is_match("axb"));
}

#[test]
fn syntax_default_and_scanner_alias() {
    // ScannerSyntax is an alias of Syntax, so one value serves both APIs.
    let scanner_default: ScannerSyntax = ScannerSyntax::default();
    assert_eq!(scanner_default, Syntax::Oniguruma);
    assert_eq!(Syntax::default(), Syntax::Oniguruma);

    let config = ferroni::scanner::ScannerConfig::default().syntax(Syntax::Asis);
    let mut scanner = ferroni::scanner::Scanner::with_config(&["a.b"], &config).unwrap();
    assert!(
        scanner
            .find_next_match("axb", 0, ScannerFindOptions::NONE)
            .is_none()
    );
    assert!(
        scanner
            .find_next_match("a.b", 0, ScannerFindOptions::NONE)
            .is_some()
    );
}

#[test]
fn scanner_config_setters_replace_one_field_each() {
    use ferroni::oniguruma::ONIG_OPTION_NONE;
    use ferroni::scanner::ScannerConfig;

    let config = ScannerConfig::default()
        .syntax(Syntax::Asis)
        .options(ONIG_OPTION_NONE);
    assert_eq!(config.syntax, Syntax::Asis);
    assert_eq!(config.options, ONIG_OPTION_NONE);

    let config = ScannerConfig::default().options(ONIG_OPTION_NONE);
    assert_eq!(config.syntax, Syntax::Oniguruma);
    assert_eq!(config.options, ONIG_OPTION_NONE);

    let mut scanner = ferroni::scanner::Scanner::with_config(&["a.b"], &config).unwrap();
    assert!(
        scanner
            .find_next_match("axb", 0, ScannerFindOptions::NONE)
            .is_some()
    );
}

#[test]
fn builder_capture_group_controls_unnamed_captures() {
    use ferroni::oniguruma::{ONIG_OPTION_CAPTURE_GROUP, ONIG_OPTION_DONT_CAPTURE_GROUP};

    let pattern = r"(?<word>\w+) (\d+)";

    // Oniguruma syntax: unnamed groups are not captured next to named ones by default.
    let default = Regex::builder(pattern).build().unwrap();
    assert_eq!(default.captures_len(), 1);
    let on = Regex::builder(pattern).capture_group(true).build().unwrap();
    assert_eq!(on.captures_len(), 2);
    let caps = on.captures("abc 42").unwrap();
    assert_eq!(caps.get(2).unwrap().as_str(), "42");
    let back = Regex::builder(pattern)
        .capture_group(true)
        .capture_group(false)
        .build()
        .unwrap();
    assert_eq!(back.captures_len(), 1);

    // capture_group(true) overrides an earlier DONT_CAPTURE_GROUP.
    let overridden = Regex::builder(pattern)
        .option(ONIG_OPTION_DONT_CAPTURE_GROUP)
        .capture_group(true)
        .build()
        .unwrap();
    assert_eq!(overridden.captures_len(), 2);
    let dont = Regex::builder(pattern)
        .option(ONIG_OPTION_DONT_CAPTURE_GROUP)
        .build()
        .unwrap();
    assert_eq!(dont.captures_len(), 1);

    // The raw flag and the typed method agree.
    let raw = Regex::builder(pattern)
        .option(ONIG_OPTION_CAPTURE_GROUP)
        .build()
        .unwrap();
    assert_eq!(raw.captures_len(), 2);

    // Python syntax captures unnamed groups even next to named ones, so false keeps them.
    let python = Regex::builder(r"(a)(?P<n>b)")
        .syntax(&OnigSyntaxPython)
        .capture_group(false)
        .build()
        .unwrap();
    assert_eq!(python.captures_len(), 2);
}

#[test]
fn builder_clear_option_removes_flag() {
    use ferroni::oniguruma::{ONIG_OPTION_EXTEND, ONIG_OPTION_IGNORECASE};

    let re = Regex::builder("hello")
        .option(ONIG_OPTION_IGNORECASE)
        .clear_option(ONIG_OPTION_IGNORECASE)
        .build()
        .unwrap();
    assert!(!re.is_match("HELLO"));

    // Clearing a flag that was never set changes nothing.
    let re = Regex::builder("a b")
        .clear_option(ONIG_OPTION_EXTEND)
        .build()
        .unwrap();
    assert!(re.is_match("a b"));
}

#[test]
fn builder_boolean_setters_false_clear_their_flags() {
    let insensitive = Regex::builder("hello")
        .case_insensitive(true)
        .case_insensitive(false)
        .build()
        .unwrap();
    assert!(!insensitive.is_match("HELLO"));

    let dot = Regex::builder("a.b")
        .dot_matches_newline(true)
        .dot_matches_newline(false)
        .build()
        .unwrap();
    assert!(!dot.is_match("a\nb"));

    let extended = Regex::builder("a b")
        .extended(true)
        .extended(false)
        .build()
        .unwrap();
    assert!(extended.is_match("a b"));
    assert!(!extended.is_match("ab"));
}

#[test]
fn builder_bytes_matches_regex_new_bytes() {
    let re = RegexBuilder::new_bytes(b"hello")
        .case_insensitive(true)
        .build()
        .unwrap();
    assert!(re.is_match("HeLLo"));

    let re = Regex::builder_bytes(br"\d+").build().unwrap();
    assert_eq!(re.find("abc 42").unwrap().as_str(), "42");

    // Invalid UTF-8 pattern bytes are accepted or rejected exactly as Regex::new_bytes does.
    let invalid: &[u8] = b"a\xff";
    let via_new = Regex::new_bytes(invalid).map(|_| ());
    let via_builder = RegexBuilder::new_bytes(invalid).build().map(|_| ());
    assert_eq!(via_new, via_builder);
}

#[test]
fn scanner_find_options_bitor_combines_flags() {
    let combined = ScannerFindOptions::NOT_BEGIN_STRING | ScannerFindOptions::NOT_END_STRING;
    assert_eq!(combined, ScannerFindOptions::from_bits(3));
    assert_eq!(
        ScannerFindOptions::NONE | ScannerFindOptions::NOT_BEGIN_POSITION,
        ScannerFindOptions::NOT_BEGIN_POSITION
    );

    let mut options = ScannerFindOptions::NONE;
    options |= ScannerFindOptions::NOT_BEGIN_POSITION;
    options |= ScannerFindOptions::NOT_END_STRING;
    assert_eq!(options, ScannerFindOptions::from_bits(6));

    let mut scanner = ferroni::scanner::Scanner::new(&[r"\Gab"]).unwrap();
    assert!(
        scanner
            .find_next_match("xab", 1, ScannerFindOptions::NOT_BEGIN_POSITION)
            .is_none()
    );
    assert!(
        scanner
            .find_next_match("xab", 1, ScannerFindOptions::NONE)
            .is_some()
    );
    let mut scanner = ferroni::scanner::Scanner::new(&[r"\Aab"]).unwrap();
    assert!(
        scanner
            .find_next_match("ab", 0, ScannerFindOptions::NOT_BEGIN_STRING)
            .is_none()
    );
}

#[test]
fn scanner_match_captures_is_the_capture_slice() {
    let mut scanner = ferroni::scanner::Scanner::new(&[r"(\d)(\d)"]).unwrap();
    let m = scanner
        .find_next_match("x42", 0, ScannerFindOptions::NONE)
        .unwrap();
    let spans: Vec<_> = m.captures().iter().map(|c| (c.start, c.end)).collect();
    assert_eq!(spans, [(1, 3), (1, 2), (2, 3)]);
    assert_eq!(m.captures().len(), 3);
}

#[test]
fn captures_len_and_is_empty() {
    // Exercises Captures::len() and is_empty()
    let re = Regex::new(r"(a)(b)(c)").unwrap();
    let caps = re.captures("abc").unwrap();
    assert_eq!(caps.len(), 4); // group 0 + 3 captures
    assert!(!caps.is_empty());
}

#[test]
fn captures_named_group_lookup() {
    // Exercises the named group lookup (captures_by_name path)
    let re = Regex::new(r"(?<year>\d{4})-(?<month>\d{2})").unwrap();
    let caps = re.captures("2024-03").unwrap();
    let year = caps.name("year");
    assert!(year.is_some());
    assert_eq!(year.unwrap().as_str(), "2024");
}

// =========================================================================
// Coverage-targeted: Octal escapes (regparse.rs)
// =========================================================================

#[test]
fn null_byte_octal_escape() {
    // \0 in regex matches null byte - exercises scan_octal_number path
    let re = Regex::new_bytes(b"\\0").unwrap();
    assert!(re.is_match_bytes(&[0x00]));
    assert!(!re.is_match_bytes(&[0x30])); // '0'
}

#[test]
fn ruby_u_hex_escape() {
    // \uXXXX hex escape in Ruby syntax - exercises ESC_U_HEX4 path
    use ferroni::regsyntax::OnigSyntaxRuby;
    let re = Regex::builder(r"\u0041")
        .syntax(&OnigSyntaxRuby)
        .build()
        .unwrap();
    assert!(re.is_match("A")); // U+0041 = 'A'
    assert!(!re.is_match("B"));
}

// =========================================================================
// Coverage-targeted: Non-ASCII escaped char in pattern (regparse.rs)
// =========================================================================

#[test]
fn escaped_non_ascii_literal() {
    // Backslash before a non-ASCII character treats it as literal
    // Exercises the non-ASCII escape fallback path
    let re = Regex::new(r"\ä").unwrap();
    assert!(re.is_match("ä"));
}

// =========================================================================
// Coverage-targeted: Subroutine in lookbehind check (regcomp.rs)
// =========================================================================

#[test]
fn subroutine_call_in_lookbehind_context() {
    // Subroutine call validated inside lookbehind - exercises check_called_node_in_look_behind
    let re = Regex::new(r"(?<d>ab)(?<=\g<d>)x");
    // This may or may not compile depending on lookbehind restrictions
    // Just exercise the validation path
    if let Ok(re) = re {
        let _ = re.is_match("abx");
    }
}

// =========================================================================
// Coverage-targeted: Recursion in alternation check (regcomp.rs)
// =========================================================================

#[test]
fn recursive_pattern_in_alternation() {
    // (?<r>a|b\g<r>c) exercises infinite_recursive_call_check with alternation
    let re = Regex::new(r"^(?<r>a|b\g<r>c)$").unwrap();
    assert!(re.is_match("a"));
    assert!(re.is_match("bac"));
    assert!(re.is_match("bbacc"));
}

// =========================================================================
// Coverage-targeted: Quantifier + subroutine state (regcomp.rs)
// =========================================================================

#[test]
fn subroutine_in_quantifier_state() {
    // Subroutine call inside quantifier exercises tune_called_state_call Quant branch
    let re = Regex::new(r"(?<r>\w)\g<r>{2,5}").unwrap();
    assert!(re.is_match("abcde"));
}

#[test]
fn subroutine_in_lookahead_state() {
    // Subroutine in lookahead exercises tune_called_state_call Anchor branch
    let re = Regex::new(r"(?<d>\d)(?=\g<d>)\d").unwrap();
    assert!(re.is_match("12"));
}

#[test]
fn subroutine_in_negative_lookbehind_state() {
    // Subroutine call in negative lookbehind exercises the state tuning path
    let re = Regex::new(r"(?<d>[a-z]{2})(?<!\g<d>)\d");
    if let Ok(re) = re {
        let _ = re.is_match("ab1");
    }
}

// =========================================================================
// Regression: positions must never leave the haystack (found by fuzzing)
// =========================================================================

#[test]
fn scanner_begin_position_anchor_at_end_of_text_does_not_panic() {
    use ferroni::scanner::{Scanner, ScannerFindOptions};
    // A begin-position anchor narrows the search range to `start + 1`. With
    // `start == text.len()` that range pointed one past the haystack and the
    // position loop subtracted past zero. A TextMate host reaches this
    // position on every line once it has consumed the last character.
    let mut scanner = Scanner::new(&[r"^\G"]).unwrap();
    assert!(
        scanner
            .find_next_match("h", 0, ScannerFindOptions::NONE)
            .is_some()
    );
    assert!(
        scanner
            .find_next_match("h", 1, ScannerFindOptions::NONE)
            .is_none()
    );

    // The same shape with a literal after the anchor exercises the skip
    // needle, whose slice also ran up to the overshooting range.
    let mut scanner = Scanner::new(&[r"^\Ga"]).unwrap();
    assert!(
        scanner
            .find_next_match("a", 1, ScannerFindOptions::NONE)
            .is_none()
    );
}

#[test]
fn search_over_truncated_multibyte_lead_stays_inside_haystack() {
    use ferroni::encodings::utf8::ONIG_ENCODING_UTF8;
    use ferroni::oniguruma::{ONIG_MISMATCH, ONIG_OPTION_NONE, OnigRegion};
    use ferroni::regcomp::onig_new;
    use ferroni::regexec::onig_search;
    use ferroni::regsyntax::OnigSyntaxOniguruma;

    // The byte API accepts arbitrary bytes. 0xE6 announces a three-byte
    // sequence, so stepping over it from position 1 used to land on 4 in a
    // three-byte haystack, and the next attempt indexed past the end.
    let reg = onig_new(
        b"$",
        ONIG_OPTION_NONE,
        &ONIG_ENCODING_UTF8,
        &OnigSyntaxOniguruma,
    )
    .unwrap();
    let haystack: &[u8] = &[0x00, 0xE6, 0xE6];
    for region in [None, Some(OnigRegion::new())] {
        let (position, _) = onig_search(
            &reg,
            haystack,
            haystack.len(),
            0,
            haystack.len(),
            region,
            ONIG_OPTION_NONE,
        );
        // The truncated character is consumed up to the logical end, where `$`
        // matches as it does for any other input.
        assert_eq!(position, haystack.len() as i32);
    }

    // A pattern that cannot match reports a mismatch rather than a panic.
    let reg = onig_new(
        b"x$",
        ONIG_OPTION_NONE,
        &ONIG_ENCODING_UTF8,
        &OnigSyntaxOniguruma,
    )
    .unwrap();
    let (position, _) = onig_search(
        &reg,
        haystack,
        haystack.len(),
        0,
        haystack.len(),
        None,
        ONIG_OPTION_NONE,
    );
    assert_eq!(position, ONIG_MISMATCH);
}

#[test]
fn optimizer_retry_over_truncated_multibyte_lead_stays_inside_haystack() {
    use ferroni::encodings::utf8::ONIG_ENCODING_UTF8;
    use ferroni::oniguruma::{ONIG_MISMATCH, ONIG_OPTION_NONE};
    use ferroni::regcomp::onig_new;
    use ferroni::regexec::onig_search;
    use ferroni::regsyntax::OnigSyntaxOniguruma;

    // `^\s$` is a byte-map search with a begin-line sub-anchor. The map
    // reports the 0xC8 lead byte as a candidate, the sub-anchor rejects it,
    // and the retry used to step two bytes past a two-byte haystack before
    // slicing it for the next map search.
    let reg = onig_new(
        br"^\s$",
        ONIG_OPTION_NONE,
        &ONIG_ENCODING_UTF8,
        &OnigSyntaxOniguruma,
    )
    .unwrap();
    let haystack: &[u8] = &[b'w', 0xC8];
    let (position, _) = onig_search(
        &reg,
        haystack,
        haystack.len(),
        0,
        haystack.len(),
        None,
        ONIG_OPTION_NONE,
    );
    assert_eq!(position, ONIG_MISMATCH);

    // The same retry over valid UTF-8 with a search range that ends inside
    // the haystack; the helpers must answer "not found" for a start past
    // the range instead of slicing it.
    let text = "w\u{3000}\u{3000}\n\u{3000}";
    let (position, _) = onig_search(
        &reg,
        text.as_bytes(),
        text.len(),
        0,
        2,
        None,
        ONIG_OPTION_NONE,
    );
    assert_eq!(position, ONIG_MISMATCH);
}

#[test]
fn stacked_possessive_quantifiers_compile_and_match() {
    use ferroni::encodings::utf8::ONIG_ENCODING_UTF8;
    use ferroni::oniguruma::{ONIG_OPTION_NONE, OnigRegion};
    use ferroni::regcomp::onig_new;
    use ferroni::regexec::onig_search;
    use ferroni::regsyntax::OnigSyntaxPerl_NG;

    // In a syntax where `++` is possessive, every further `+` wraps the
    // previous quantifier in an atomic group and quantifies it again. The
    // compiler used to emit the body twice per level for the mandatory
    // first pass, so 43 stacked `+` asked for gigabytes of bytecode. C jumps
    // into the loop body once the body exceeds the expansion limit.
    let mut pattern = b"un".to_vec();
    pattern.extend(std::iter::repeat_n(b'+', 43));
    pattern.extend_from_slice(b"ct?");

    // The bytecode size itself is asserted by a unit test in regcomp.rs; this
    // test pins the behavior of the compiled program.
    let reg = onig_new(
        &pattern,
        ONIG_OPTION_NONE,
        &ONIG_ENCODING_UTF8,
        &OnigSyntaxPerl_NG,
    )
    .unwrap();

    let haystack = b"xunnnct";
    let (position, region) = onig_search(
        &reg,
        haystack,
        haystack.len(),
        0,
        haystack.len(),
        Some(OnigRegion::new()),
        ONIG_OPTION_NONE,
    );
    assert_eq!(position, 1);
    assert_eq!(region.unwrap().end[0], 7);
}

#[test]
fn left_recursive_subexpression_call_is_rejected_at_compile_time() {
    use ferroni::scanner::Scanner;

    // A whole-pattern call at the head of an alternative re-enters the
    // pattern at the same position without end. C rejects it as never-ending
    // recursion when compiling; the port used to accept it and the matcher
    // then grew its stack until the process ran out of memory.
    assert!(Scanner::new(&["\\g<0>a|b|"]).is_err());
    assert!(Regex::new(r"\g<0>|a").is_err());

    // Recursion after consumed input stays valid.
    let re = Regex::new(r"a\g<0>|b").unwrap();
    assert_eq!(re.find("aab").unwrap().as_str(), "aab");
}

#[test]
fn scanner_reports_the_keep_adjusted_match() {
    use ferroni::scanner::{Scanner, ScannerFindOptions};

    // `\K` moves the match start to where it appears, so the whole match is
    // the region and not the position the winning attempt began at plus the
    // length that attempt reported. Without a capture group the scanner used
    // the position and the length, and reported the attempt's span instead.
    // Every expectation below is C Oniguruma's, taken from its own scanner
    // through the `ffi` feature.
    let spans = |patterns: &[&str], text: &str, position: usize| {
        Scanner::new(patterns)
            .unwrap()
            .find_next_match(text, position, ScannerFindOptions::NONE)
            .map(|m| (m.index, m.captures()[0].start, m.captures()[0].end))
    };

    assert_eq!(spans(&[r"a\Kb"], "xxabxx", 0), Some((0, 3, 4)));
    // `\K` at the end of the pattern keeps a zero-width match there.
    assert_eq!(spans(&[r"ab\K"], "xxabxx", 0), Some((0, 4, 4)));
    // A capture group already took the region path and stays as it was.
    assert_eq!(spans(&[r"a\K(b)"], "xxabxx", 0), Some((0, 3, 4)));
    // `\K` before anything consumed leaves the match start alone.
    assert_eq!(spans(&[r"\Kab"], "xxabxx", 0), Some((0, 2, 4)));
    // A set of several patterns reports the winner's own kept match.
    assert_eq!(spans(&[r"zz", r"a\Kb"], "xxabxx", 0), Some((1, 3, 4)));

    // Repeated calls from one position hand the scanner over to its cached
    // per-regex route after ROUTE_MIN_SAME_START_FOR_PROBE calls. Both routes
    // report the same match.
    let mut scanner = Scanner::new(&[r"a\Kb"]).unwrap();
    for _ in 0..32 {
        let m = scanner
            .find_next_match_with_id("xxabxx", 1, 0, ScannerFindOptions::NONE)
            .unwrap();
        assert_eq!((m.captures()[0].start, m.captures()[0].end), (3, 4));
    }
}

#[test]
fn scanner_keep_match_stays_on_character_boundaries() {
    use ferroni::scanner::{Scanner, ScannerFindOptions};

    // Minimized from the `scanner-api` fuzz target: the harness derives the
    // pattern `.*\K\u{FFFD}` and the text below from the 11 input bytes
    // `2e 2a 5c 4b b9 00 20 ff ff 61 ff`. `.*` consumes to the end of the
    // line and backtracks, so the winning attempt begins before the match
    // `\K` keeps. The scanner reported that attempt's span, 3..6, which ends
    // inside the trailing replacement character.
    let text = "\u{FFFD}a\u{FFFD}";
    let pattern = format!(".*\\K{}", '\u{FFFD}');
    let mut scanner = Scanner::new(&[pattern.as_str()]).unwrap();

    for position in (0..=text.len()).filter(|p| text.is_char_boundary(*p)) {
        let Some(m) = scanner.find_next_match(text, position, ScannerFindOptions::NONE) else {
            // Only a start past the last replacement character has no match.
            assert_eq!(position, text.len());
            continue;
        };
        let whole = &m.captures()[0];
        assert_eq!(
            (whole.start, whole.end),
            (4, 7),
            "match from position {position}"
        );
        assert!(text.is_char_boundary(whole.start) && text.is_char_boundary(whole.end));
    }
}

// A capture can read as start > end: group 1 of `((?=(a|ab))a?){2}` on "a"
// is 1..0 in C Oniguruma and in the region (the second pass restarts the
// group and fails before closing it). The high-level API reports such a
// capture as not participating instead of slicing.
#[test]
fn inverted_capture_is_not_participating() {
    let re = Regex::new(r"((?=(a|ab))a?){2}").unwrap();
    let caps = re.captures("a").unwrap();
    assert_eq!(caps.get(0).unwrap().range(), 0..0);
    assert!(caps.get(1).is_none());
    let group2 = caps.get(2).unwrap();
    assert_eq!(group2.range(), 1..1);
    assert_eq!(group2.as_str(), "");
    let listed: Vec<_> = caps.iter().map(|m| m.map(|m| m.range())).collect();
    assert_eq!(listed, vec![Some(0..0), None, Some(1..1)]);
    let _ = format!("{caps:?}");

    let named = Regex::new(r"(?<g>(?=(?<h>a|ab))a?){2}").unwrap();
    let caps = named.captures("a").unwrap();
    assert!(caps.name("g").is_none());
    assert_eq!(caps.name("h").unwrap().range(), 1..1);
}

// `((?=a\g<0>)|(?:\k<1>*?(?=a)()))*` on "aa" loops without consuming input:
// the capture-aware empty check never sees an empty iteration, so every cycle
// grows the backtracking stack and only the retry limit ends the search. C
// Oniguruma takes three backtracks per cycle and holds about 433k stack
// entries after 100k retries. Two of those backtracks happen where Ferroni
// guards the push (ADR-008); unless the guards count them like C, the same
// retry budget lets the stack grow three times as large, which with the
// default limit took the process past 4 GB. The stack limit keeps this test
// bounded either way.
#[test]
fn guarded_pushes_spend_the_retry_budget_like_c() {
    let re = Regex::new(r"((?=a\g<0>)|(?:\k<1>*?(?=a)()))*").unwrap();
    let options = || {
        SearchOptions::new()
            .retry_limit_in_match(100_000)
            .match_stack_limit(1_000_000)
    };
    let expected = RegexError::RetryLimitInMatchOver;
    assert_eq!(re.find_with("aa", options()).err(), Some(expected.clone()));
    assert_eq!(
        re.is_match_with("aa", options()).err(),
        Some(expected.clone())
    );
    assert_eq!(
        re.captures_with("aa", options()).err(),
        Some(expected.clone())
    );
    let mut matches = re.find_iter_with("aa", options());
    assert_eq!(matches.next().and_then(Result::err), Some(expected));
    assert!(matches.next().is_none());
}

// C Oniguruma stops a match once its retry count reaches the limit
// (`++counter >= limit`), not when it exceeds it. The smallest limits under
// which C finishes these searches without a limit error, for the retry
// limit in match and in search.
#[test]
fn retry_limits_stop_at_the_limit_like_c() {
    assert_c_retry_budgets(&[
        (r"x(a*)*y", "xaaaaaa", 65, 65),
        (r"(?:a|ab)*c", "ababababxc", 11, 41),
    ]);
}

// A guarded push whose main path forks (`a+|[ab]` below) skips several of
// C's backtracks when it jumps, and counts all of them. Budgets from C, as
// above.
#[test]
fn guarded_pushes_count_every_skipped_backtrack_like_c() {
    assert_c_retry_budgets(&[
        (r"(?:a+|[ab])*^", "aaaaa", 322, 322),
        (r"(?:(?=[ab])a+|a)*$", "aaa!", 48, 78),
    ]);
}

/// Each case is (pattern, subject, the smallest retry limit in match and in
/// search under which C Oniguruma finishes the search).
fn assert_c_retry_budgets(cases: &[(&str, &str, u64, u64)]) {
    for &(pattern, subject, in_match, in_search) in cases {
        let re = Regex::new(pattern).unwrap();
        let by_match =
            |limit| re.find_with(subject, SearchOptions::new().retry_limit_in_match(limit));
        assert!(by_match(in_match).is_ok(), "{pattern}");
        assert_eq!(
            by_match(in_match - 1).err(),
            Some(RegexError::RetryLimitInMatchOver),
            "{pattern}"
        );
        let by_search = |limit| {
            re.find_with(
                subject,
                SearchOptions::new()
                    .retry_limit_in_match(0)
                    .retry_limit_in_search(limit),
            )
        };
        assert!(by_search(in_search).is_ok(), "{pattern}");
        assert_eq!(
            by_search(in_search - 1).err(),
            Some(RegexError::RetryLimitInSearchOver),
            "{pattern}"
        );
    }
}

// === Regex: Clone, FromStr, Display, Debug, as_str ===

#[test]
fn clone_shares_the_compiled_program() {
    let re = Regex::new(r"(\d+)").unwrap();
    let copy = re.clone();
    assert!(std::ptr::eq(re.as_raw(), copy.as_raw()));
    drop(re);
    assert_eq!(copy.find("abc 42").unwrap().as_str(), "42");
    assert_eq!(&copy.captures("7").unwrap()[1], "7");
}

#[test]
fn from_str_compiles_or_reports_the_error() {
    let re: Regex = "a+".parse().unwrap();
    assert_eq!(re.as_str(), Some("a+"));
    assert!(matches!(
        "(".parse::<Regex>(),
        Err(RegexError::Syntax { .. })
    ));
}

#[test]
fn display_writes_the_pattern() {
    let re = Regex::new(r"\d{2}-(?<name>x)").unwrap();
    assert_eq!(re.to_string(), r"\d{2}-(?<name>x)");
}

#[test]
fn debug_shows_the_pattern() {
    let re = Regex::new("a+b").unwrap();
    assert_eq!(format!("{re:?}"), r#"Regex { pattern: "a+b", .. }"#);
}

#[test]
fn as_str_is_none_for_a_pattern_that_is_not_utf8() {
    // An encoded surrogate: Oniguruma accepts it, UTF-8 does not.
    let re = Regex::new_bytes(b"\xed\xa0\x80").unwrap();
    assert_eq!(re.as_str(), None);
    assert_eq!(re.as_bytes(), b"\xed\xa0\x80");
    assert!(re.to_string().contains('\u{FFFD}'));

    assert_eq!(Regex::new_bytes(b"ab").unwrap().as_str(), Some("ab"));
}

#[test]
fn regex_and_captures_are_unwind_safe() {
    fn assert_unwind_safe<T: std::panic::UnwindSafe + std::panic::RefUnwindSafe>() {}
    assert_unwind_safe::<Regex>();
    assert_unwind_safe::<Captures<'static>>();

    let re = Regex::new("b+").unwrap();
    let found = std::panic::catch_unwind(|| re.find("abb").map(|m| m.range()));
    assert_eq!(found.unwrap(), Some(1..3));
}

// === Captures borrows only the haystack ===

#[test]
fn captures_outlive_the_regex_they_came_from() {
    let caps = {
        let re = Regex::new(r"(?<word>\w+)!").unwrap();
        re.captures("hi there!")
    };
    assert_eq!(caps.unwrap().name("word").unwrap().as_str(), "there");
}

// === Captures indexing ===

#[test]
fn captures_index_by_number_and_name() {
    let re = Regex::new(r"(?<year>\d{4})-(?<month>\d{2})").unwrap();
    let caps = re.captures("on 2026-10").unwrap();
    assert_eq!(&caps[0], "2026-10");
    assert_eq!(&caps[1], "2026");
    assert_eq!(&caps[2], "10");
    assert_eq!(&caps["month"], "10");
}

#[test]
fn captures_index_by_name_takes_the_last_participating_group() {
    let re = Regex::new(r"(?<n>a)|(?<n>b)").unwrap();
    assert_eq!(&re.captures("b").unwrap()["n"], "b");
}

#[test]
#[should_panic(expected = "no group at index '3'")]
fn captures_index_panics_on_a_missing_group() {
    let re = Regex::new(r"(?<year>\d{4})").unwrap();
    let caps = re.captures("2026").unwrap();
    let _ = &caps[3];
}

#[test]
#[should_panic(expected = "no group at index '2'")]
fn captures_index_panics_on_a_group_that_did_not_participate() {
    let re = Regex::new(r"(a)|(b)").unwrap();
    let caps = re.captures("a").unwrap();
    let _ = &caps[2];
}

#[test]
#[should_panic(expected = "no group named 'day'")]
fn captures_index_panics_on_an_unknown_name() {
    let re = Regex::new(r"(?<year>\d{4})").unwrap();
    let caps = re.captures("2026").unwrap();
    let _ = &caps["day"];
}

#[test]
#[should_panic(expected = "not valid UTF-8")]
fn captures_index_panics_on_bytes_that_are_not_utf8() {
    let re = Regex::new(r"(?<all>.+)").unwrap();
    let caps = re.captures_bytes(b"\xff").unwrap();
    assert_eq!(caps.get(1).unwrap().as_bytes(), b"\xff");
    let _ = &caps[1];
}

// === Regex::capture_names ===

#[test]
fn capture_names_lists_one_entry_per_group() {
    let re = Regex::new(r"(?<year>\d{4})-(?<month>\d{2})").unwrap();
    let names = re.capture_names();
    assert_eq!(names.len(), 3);
    assert_eq!(
        names.collect::<Vec<_>>(),
        [None, Some("year"), Some("month")]
    );
}

#[test]
fn capture_names_repeats_a_shared_name_and_marks_unnamed_groups() {
    let shared = Regex::new(r"(?<n>a)|(?<n>b)").unwrap();
    assert_eq!(
        shared.capture_names().collect::<Vec<_>>(),
        [None, Some("n"), Some("n")]
    );

    let unnamed = Regex::new(r"(a)(b)").unwrap();
    assert_eq!(
        unnamed.capture_names().collect::<Vec<_>>(),
        [None, None, None]
    );

    let no_groups = Regex::new(r"a+").unwrap();
    assert_eq!(no_groups.capture_names().collect::<Vec<_>>(), [None]);
}

// === Regex::find_at and the start-offset family ===

#[test]
fn find_at_starts_the_search_at_the_offset() {
    let re = Regex::new(r"\w+").unwrap();
    let text = "hello world";
    assert_eq!(re.find_at(text, 6).unwrap().as_str(), "world");
    assert_eq!(re.find_at(text, 0).unwrap().as_str(), "hello");
    // A match can begin in the middle of a word.
    assert_eq!(re.find_at(text, 2).unwrap().range(), 2..5);
}

#[test]
fn find_at_offsets_are_relative_to_the_whole_text() {
    let re = Regex::new(r"\d+").unwrap();
    let text = "ab12cd345";
    assert_eq!(re.find_at(text, 3).unwrap().range(), 3..4);
    assert_eq!(re.find(&text[3..]).unwrap().range(), 0..1);
}

#[test]
fn find_at_look_behind_sees_the_text_before_start() {
    let re = Regex::new(r"(?<=a)b").unwrap();
    assert_eq!(re.find_at("ab", 1).unwrap().range(), 1..2);
    // The slice has no `a` in front of its `b`, so the look-behind fails.
    assert!(re.find(&"ab"[1..]).is_none());
}

#[test]
fn find_at_word_boundary_and_line_anchor_see_the_text_before_start() {
    let word = Regex::new(r"\bb").unwrap();
    // There is no word boundary between `a` and `b`.
    assert!(word.find_at("ab", 1).is_none());
    assert_eq!(word.find_at("a b", 2).unwrap().range(), 2..3);

    let line = Regex::new(r"^b").unwrap();
    assert!(line.find_at("ab", 1).is_none());
    assert_eq!(line.find_at("a\nb", 2).unwrap().range(), 2..3);
}

#[test]
fn find_at_g_anchor_matches_at_the_start_offset() {
    let re = Regex::new(r"\Gb").unwrap();
    assert_eq!(re.find_at("ab", 1).unwrap().range(), 1..2);
    assert!(re.find_at("ab", 0).is_none());

    // A tokenizer resumes at the end of each token.
    let token = Regex::new(r"\G\d+").unwrap();
    let text = "12ab34";
    assert_eq!(token.find_at(text, 0).unwrap().range(), 0..2);
    assert!(token.find_at(text, 2).is_none());
    assert_eq!(token.find_at(text, 4).unwrap().range(), 4..6);
}

#[test]
fn find_at_keep_moves_start_pattern_reports_the_kept_start() {
    // `\K` moves the match start, so this pattern searches through the region.
    let re = Regex::new(r"a\Kb").unwrap();
    assert_eq!(re.find_at("aab", 1).unwrap().range(), 2..3);
    assert_eq!(re.find_at("aab", 0).unwrap().range(), 2..3);
    assert!(re.find_at("aab", 2).is_none());
}

#[test]
fn find_at_matches_the_text_after_a_multibyte_start() {
    let re = Regex::new(r"\w").unwrap();
    let text = "é1é2";
    assert_eq!(re.find_at(text, 2).unwrap().range(), 2..3);
    assert_eq!(re.find_at(text, 3).unwrap().as_str(), "é");
    assert_eq!(re.find_at(text, 5).unwrap().as_str(), "2");
}

#[test]
fn find_at_start_at_the_end_of_the_text() {
    let empty = Regex::new(r"x*").unwrap();
    assert_eq!(empty.find_at("abc", 3).unwrap().range(), 3..3);
    assert_eq!(empty.find_bytes_at(b"abc", 3).unwrap().range(), 3..3);

    let letter = Regex::new(r"c").unwrap();
    assert!(letter.find_at("abc", 3).is_none());
    assert!(!letter.is_match_at("abc", 3));
}

#[test]
fn find_at_resumed_from_each_match_end_matches_find_iter() {
    let re = Regex::new(r"\w+").unwrap();
    let text = "one two  three";
    let mut resumed = Vec::new();
    let mut position = 0;
    while let Some(m) = re.find_at(text, position) {
        resumed.push(m.range());
        position = m.end();
    }
    let iterated: Vec<_> = re.find_iter(text).map(|m| m.range()).collect();
    assert_eq!(resumed, iterated);
    assert_eq!(resumed, [0..3, 4..7, 9..14]);
}

#[test]
#[should_panic(expected = "not a char boundary")]
fn find_at_panics_inside_a_character() {
    let re = Regex::new(r"\w").unwrap();
    let _ = re.find_at("é", 1);
}

#[test]
#[should_panic(expected = "past the end of the text")]
fn find_bytes_at_panics_past_the_end() {
    let re = Regex::new(r"a").unwrap();
    let _ = re.find_bytes_at(b"ab", 3);
}

#[test]
fn start_checks_panic_for_every_at_method() {
    fn panics(f: impl FnOnce() + std::panic::UnwindSafe) -> bool {
        std::panic::catch_unwind(f).is_err()
    }

    let re = Regex::new(r"a").unwrap();
    let options = SearchOptions::new;
    let text = "éa"; // byte 1 is inside `é`, and byte 4 is past the end
    let bytes = text.as_bytes();

    assert!(panics(|| {
        let _ = re.find_at(text, 1);
    }));
    assert!(panics(|| {
        let _ = re.find_at_with(text, 1, options());
    }));
    assert!(panics(|| {
        let _ = re.is_match_at(text, 1);
    }));
    assert!(panics(|| {
        let _ = re.is_match_at_with(text, 1, options());
    }));
    assert!(panics(|| {
        let _ = re.captures_at(text, 1);
    }));
    assert!(panics(|| {
        let _ = re.captures_at_with(text, 1, options());
    }));

    assert!(panics(|| {
        let _ = re.find_bytes_at(bytes, 4);
    }));
    assert!(panics(|| {
        let _ = re.find_bytes_at_with(bytes, 4, options());
    }));
    assert!(panics(|| {
        let _ = re.is_match_bytes_at(bytes, 4);
    }));
    assert!(panics(|| {
        let _ = re.is_match_bytes_at_with(bytes, 4, options());
    }));
    assert!(panics(|| {
        let _ = re.captures_bytes_at(bytes, 4);
    }));
    assert!(panics(|| {
        let _ = re.captures_bytes_at_with(bytes, 4, options());
    }));
}

#[test]
fn find_bytes_at_accepts_a_start_inside_a_character() {
    // The bytes API searches bytes, so a start between the bytes of `é` is
    // allowed.
    let re = Regex::new(r"x").unwrap();
    assert_eq!(re.find_bytes_at("é x".as_bytes(), 1).unwrap().range(), 3..4);
    assert_eq!(re.find_bytes_at(b"\xff\x78", 1).unwrap().range(), 1..2);
}

#[test]
fn find_bytes_at_matches_find_at_on_valid_text() {
    let re = Regex::new(r"(?<=a)b").unwrap();
    let text = "xab";
    assert_eq!(
        re.find_bytes_at(text.as_bytes(), 2).map(|m| m.range()),
        re.find_at(text, 2).map(|m| m.range())
    );
}

#[test]
fn is_match_at_checks_from_the_start_offset() {
    let re = Regex::new(r"(?<=a)b").unwrap();
    assert!(re.is_match_at("ab", 1));
    // A match at or after the offset counts, so offset 0 also finds the `b`.
    assert!(re.is_match_at("ab", 0));
    assert!(!re.is_match_at("ab", 2));
    assert!(!re.is_match(&"ab"[1..]));
    assert!(re.is_match_bytes_at(b"ab", 1));
    assert!(!re.is_match_bytes_at(b"ab", 2));
}

#[test]
fn captures_at_reports_groups_relative_to_the_whole_text() {
    let re = Regex::new(r"(?<=a)(b)(c)").unwrap();
    let caps = re.captures_at("abc", 1).unwrap();
    assert_eq!(caps.get(0).unwrap().range(), 1..3);
    assert_eq!(caps.get(1).unwrap().range(), 1..2);
    assert_eq!(caps.get(2).unwrap().range(), 2..3);
    assert!(re.captures_at("abc", 2).is_none());

    let named = Regex::new(r"(?<=a)(?<mid>b)c").unwrap();
    assert_eq!(
        named
            .captures_at("abc", 1)
            .unwrap()
            .name("mid")
            .unwrap()
            .range(),
        1..2
    );
    assert!(re.captures_bytes_at(b"abc", 1).is_some());
}

#[test]
fn at_with_variants_match_their_plain_counterparts() {
    let re = Regex::new(r"(?<=a)(b)(c)").unwrap();
    let text = "abc";
    let options = SearchOptions::new();

    assert_eq!(
        re.find_at_with(text, 1, options).unwrap().unwrap().range(),
        1..3
    );
    assert!(re.find_at_with(text, 2, options).unwrap().is_none());
    assert_eq!(
        re.find_bytes_at_with(text.as_bytes(), 1, options)
            .unwrap()
            .unwrap()
            .range(),
        1..3
    );
    assert!(re.is_match_at_with(text, 1, options).unwrap());
    assert!(!re.is_match_at_with(text, 3, options).unwrap());
    assert!(
        re.is_match_bytes_at_with(text.as_bytes(), 1, options)
            .unwrap()
    );

    let caps = re.captures_at_with(text, 1, options).unwrap().unwrap();
    assert_eq!(caps.get(1).unwrap().as_str(), "b");
    assert!(
        re.captures_bytes_at_with(text.as_bytes(), 1, options)
            .unwrap()
            .is_some()
    );
}

#[test]
fn at_with_variants_report_limit_errors() {
    // The pattern from `guarded_pushes_spend_the_retry_budget_like_c`, started
    // inside the text.
    let re = Regex::new(r"((?=a\g<0>)|(?:\k<1>*?(?=a)()))*").unwrap();
    let options = || {
        SearchOptions::new()
            .retry_limit_in_match(100_000)
            .match_stack_limit(1_000_000)
    };
    let expected = Some(RegexError::RetryLimitInMatchOver);
    assert_eq!(re.find_at_with("xaa", 1, options()).err(), expected);
    assert_eq!(re.is_match_at_with("xaa", 1, options()).err(), expected);
    assert_eq!(re.captures_at_with("xaa", 1, options()).err(), expected);
    assert_eq!(re.find_bytes_at_with(b"xaa", 1, options()).err(), expected);
}

#[test]
fn captures_iter_yields_the_find_iter_matches() {
    let cases: &[(&str, &str)] = &[
        (r"a*", "ab"),
        (r"", "ab"),
        (r"\b", "ab cd"),
        (r"(?<=a)", "aab"),
        (r"\G\w", "abc def"),
        (r"x*", "éxé"),
        (r"\w*", "héllo wörld"),
        (r"\d+|\s*", "12 ab 345"),
        (r"a\Kb", "aab ab"),
        (r"(?<n>\d)", "a1b2"),
        (r"^", "a\n\nb"),
        (r"$", "ab\ncd"),
        (r"(?<=é)", "éé"),
    ];
    for &(pattern, text) in cases {
        let re = Regex::new(pattern).unwrap();
        let found: Vec<_> = re.find_iter(text).map(|m| m.range()).collect();

        let captured: Vec<_> = re
            .captures_iter(text)
            .map(|caps| caps.get(0).unwrap().range())
            .collect();
        assert_eq!(captured, found, "captures_iter: {pattern:?} on {text:?}");

        let bytes: Vec<_> = re
            .captures_iter_bytes(text.as_bytes())
            .map(|caps| caps.get(0).unwrap().range())
            .collect();
        assert_eq!(bytes, found, "captures_iter_bytes: {pattern:?} on {text:?}");

        let with: Vec<_> = re
            .captures_iter_with(text, SearchOptions::new())
            .map(|caps| caps.unwrap().get(0).unwrap().range())
            .collect();
        assert_eq!(with, found, "captures_iter_with: {pattern:?} on {text:?}");
    }
}

#[test]
fn captures_iter_empty_matches_follow_find_iter_rule() {
    // `a*` over "ab": the empty match at 1 is yielded, and the one at 2 too.
    let re = Regex::new(r"a*").unwrap();
    let ranges: Vec<_> = re
        .captures_iter("ab")
        .map(|caps| caps.get(0).unwrap().range())
        .collect();
    assert_eq!(ranges, [0..1, 1..1, 2..2]);
}

#[test]
fn captures_iter_yields_every_group_of_each_match() {
    let re = Regex::new(r"(?<year>\d{4})-(?<month>\d{2})").unwrap();
    let dates: Vec<(&str, &str)> = re
        .captures_iter("2025-01, then 2026-10")
        .map(|caps| {
            (
                caps.name("year").unwrap().as_str(),
                caps.name("month").unwrap().as_str(),
            )
        })
        .collect();
    assert_eq!(dates, [("2025", "01"), ("2026", "10")]);

    let caps: Vec<_> = re.captures_iter("2025-01").collect();
    assert_eq!(caps[0].len(), 3);
    assert_eq!(caps[0].get(2).unwrap().range(), 5..7);
}

#[test]
fn captures_iter_with_yields_a_limit_error_once() {
    let re = Regex::new(r"((?=a\g<0>)|(?:\k<1>*?(?=a)()))*").unwrap();
    let options = SearchOptions::new()
        .retry_limit_in_match(100_000)
        .match_stack_limit(1_000_000);
    let mut matches = re.captures_iter_with("aa", options);
    assert_eq!(
        matches.next().and_then(Result::err),
        Some(RegexError::RetryLimitInMatchOver)
    );
    assert!(matches.next().is_none());
}

#[test]
fn captures_iterators_are_fused() {
    fn assert_fused<I: std::iter::FusedIterator>(_: &I) {}

    let re = Regex::new(r"a").unwrap();
    let mut iter = re.captures_iter_with("a", SearchOptions::new());
    assert_fused(&iter);
    assert!(iter.next().is_some());
    assert!(iter.next().is_none());
    assert!(iter.next().is_none());
}

#[test]
fn captures_from_iter_outlive_the_iterator_and_the_regex() {
    let text = "a1 b2";
    let caps: Vec<_> = Regex::new(r"(?<d>\d)")
        .unwrap()
        .captures_iter(text)
        .collect();
    assert_eq!(caps.len(), 2);
    assert_eq!(caps[1].name("d").unwrap().as_str(), "2");
}

// === Captures::expand ===

fn expanded(re: &Regex, text: &str, template: &str) -> String {
    let caps = re.captures(text).expect("the pattern matches the text");
    let mut out = String::new();
    caps.expand(template, &mut out);
    out
}

#[test]
fn expand_numbered_reference() {
    let re = Regex::new(r"(\w+)-(\d+)").unwrap();
    assert_eq!(expanded(&re, "item-42", "$2 $1"), "42 item");
    assert_eq!(expanded(&re, "item-42", "$0"), "item-42");
}

#[test]
fn expand_braced_numbered_reference_ends_the_name() {
    let re = Regex::new(r"(\w+)").unwrap();
    assert_eq!(expanded(&re, "ab", "${1}x"), "abx");
    assert_eq!(expanded(&re, "ab", "$1x"), "");
}

#[test]
fn expand_leading_zeros_name_the_same_group() {
    let re = Regex::new(r"(a)(b)").unwrap();
    assert_eq!(expanded(&re, "ab", "$01$002"), "ab");
}

#[test]
fn expand_named_reference_takes_longest_name_run() {
    let re = Regex::new(r"(?<year>\d{4})-(?<month>\d{2})").unwrap();
    assert_eq!(expanded(&re, "2026-10", "$year-$month"), "2026-10");
    assert_eq!(expanded(&re, "2026-10", "$yearx"), "");
    assert_eq!(expanded(&re, "2026-10", "$year_"), "");
    assert_eq!(expanded(&re, "2026-10", "${year}x"), "2026x");
    assert_eq!(expanded(&re, "2026-10", "${month}"), "10");
}

#[test]
fn expand_dollar_dollar_is_a_literal_dollar() {
    let re = Regex::new(r"(a)").unwrap();
    assert_eq!(expanded(&re, "a", "$$"), "$");
    assert_eq!(expanded(&re, "a", "$$1"), "$1");
    assert_eq!(expanded(&re, "a", "$$$1"), "$a");
    assert_eq!(expanded(&re, "a", "$$${1}"), "$a");
    assert_eq!(expanded(&re, "a", "cost: 5$$"), "cost: 5$");
}

#[test]
fn expand_missing_group_is_empty() {
    let re = Regex::new(r"(?<n>\d)").unwrap();
    assert_eq!(expanded(&re, "7", "[$9]"), "[]");
    assert_eq!(expanded(&re, "7", "[${9}]"), "[]");
    assert_eq!(expanded(&re, "7", "[$missing]"), "[]");
    assert_eq!(expanded(&re, "7", "[${missing}]"), "[]");
    assert_eq!(expanded(&re, "7", "[$n]"), "[7]");
}

#[test]
fn expand_non_participating_group_is_empty() {
    let re = Regex::new(r"(a)?b").unwrap();
    assert_eq!(expanded(&re, "b", "[$1]"), "[]");
    assert_eq!(expanded(&re, "ab", "[$1]"), "[a]");
}

#[test]
fn expand_name_resolves_like_captures_name() {
    // Two groups share the name `n`. `Captures::name` picks the last one that
    // participated, and `$n` must pick the same group.
    let re = Regex::new(r"(?<n>a)|(?<n>b)").unwrap();
    assert_eq!(expanded(&re, "a", "$n"), "a");
    assert_eq!(expanded(&re, "b", "$n"), "b");
    let caps = re.captures("b").unwrap();
    assert_eq!(expanded(&re, "b", "$n"), caps.name("n").unwrap().as_str());
}

#[test]
fn expand_invalid_reference_is_literal() {
    let re = Regex::new(r"(a)").unwrap();
    assert_eq!(expanded(&re, "a", "cost $"), "cost $");
    assert_eq!(expanded(&re, "a", "$-$."), "$-$.");
    assert_eq!(expanded(&re, "a", "$ $"), "$ $");
    assert_eq!(expanded(&re, "a", "${"), "${");
    assert_eq!(expanded(&re, "a", "${1"), "${1");
    assert_eq!(expanded(&re, "a", "${ x"), "${ x");
    assert_eq!(expanded(&re, "a", "é$"), "é$");
}

#[test]
fn expand_braced_reference_runs_to_the_closing_brace() {
    let re = Regex::new(r"(a)").unwrap();
    // The text between the braces is looked up as a name or a number. No group
    // has these names, so they expand to nothing, and the braces are consumed.
    assert_eq!(expanded(&re, "a", "[${}]"), "[]");
    assert_eq!(expanded(&re, "a", "[${a-b}]"), "[]");
    assert_eq!(expanded(&re, "a", "[${1 }]"), "[]");
    assert_eq!(expanded(&re, "a", "${}x"), "x");
    assert_eq!(expanded(&re, "a", "${a}b}"), "b}");
    // A `${` whose first `}` comes later swallows the text in between.
    assert_eq!(expanded(&re, "a", "${1 $1}"), "");
    // A number written with a sign or leading zeros still names that group.
    assert_eq!(expanded(&re, "a", "${+1}"), "a");
    assert_eq!(expanded(&re, "a", "${001}"), "a");
}

#[test]
fn expand_keeps_non_ascii_literal_text() {
    let re = Regex::new(r"(?<w>\w+)").unwrap();
    assert_eq!(expanded(&re, "ü", "→ $w ✓"), "→ ü ✓");
}

#[test]
fn expand_unnamed_groups_are_not_captured_next_to_named_groups() {
    let re = Regex::new(r"(?<y>\d+)-(\d+)").unwrap();
    assert_eq!(re.captures_len(), 1);
    assert_eq!(expanded(&re, "2026-10", "$y/$2"), "2026/");
}

#[test]
fn expand_unnamed_groups_with_capture_group_option() {
    let re = Regex::builder(r"(?<y>\d+)-(\d+)")
        .capture_group(true)
        .build()
        .unwrap();
    assert_eq!(re.captures_len(), 2);
    assert_eq!(expanded(&re, "2026-10", "$y/$2"), "2026/10");
}

#[test]
fn expand_bytes_copies_non_utf8_groups() {
    let re = Regex::new(r"(?<word>\S+)").unwrap();
    let caps = re.captures_bytes(b"\xff\xfe end").unwrap();
    let mut out = Vec::new();
    caps.expand_bytes(b"[$word]$$", &mut out);
    assert_eq!(out, b"[\xff\xfe]$");
}

// === Regex::replace ===

#[test]
fn replace_changes_only_the_first_match() {
    let re = Regex::new(r"\d+").unwrap();
    assert_eq!(re.replace("a1 b22 c333", "#"), "a# b22 c333");
}

#[test]
fn replace_all_changes_every_match() {
    let re = Regex::new(r"\d+").unwrap();
    assert_eq!(re.replace_all("a1 b22 c333", "#"), "a# b# c#");
}

#[test]
fn replacen_limits_the_number_of_replacements() {
    let re = Regex::new(r"\d").unwrap();
    assert_eq!(re.replacen("1 2 3", 2, "x"), "x x 3");
    assert_eq!(re.replacen("1 2 3", 1, "x"), "x 2 3");
    assert_eq!(re.replacen("1 2 3", 5, "x"), "x x x");
}

#[test]
fn replacen_zero_replaces_all_matches() {
    let re = Regex::new(r"\d").unwrap();
    assert_eq!(re.replacen("1 2 3", 0, "x"), "x x x");
}

#[test]
fn replace_without_a_match_borrows_the_text() {
    let re = Regex::new(r"\d").unwrap();
    let text = "no digits here";
    let out = re.replace_all(text, "#");
    assert!(matches!(out, Cow::Borrowed(_)));
    assert_eq!(out, text);
    assert!(matches!(re.replace(text, "#"), Cow::Borrowed(_)));
}

#[test]
fn replace_with_a_match_owns_the_result() {
    let re = Regex::new(r"\d").unwrap();
    let out = re.replace_all("a1", "#");
    assert!(matches!(out, Cow::Owned(_)));
    assert_eq!(out, "a#");
}

#[test]
fn replace_all_treats_empty_match_after_nonempty_as_a_match() {
    // `a*` matches `a` at 0..1, then empty matches at 1 and at 2. Ferroni's
    // find_iter reports all three, so each one is replaced.
    let re = Regex::new(r"a*").unwrap();
    assert_eq!(re.replace_all("ab", "-"), "--b-");
    assert_eq!(re.replace_all("aab", "-"), "--b-");
}

#[test]
fn replace_matches_follow_find_iter_for_empty_patterns() {
    let re = Regex::new(r"").unwrap();
    assert_eq!(re.replace_all("ab", "-"), "-a-b-");
    assert_eq!(re.replacen("ab", 2, "-"), "-a-b");
}

#[test]
fn replace_all_at_text_end_and_empty_text() {
    let re = Regex::new(r"$").unwrap();
    assert_eq!(re.replace_all("ab", "!"), "ab!");
    assert_eq!(re.replace_all("", "!"), "!");
}

#[test]
fn replace_all_expands_named_references() {
    let re = Regex::new(r"(?<year>\d{4})-(?<month>\d{2})").unwrap();
    assert_eq!(
        re.replace_all("2025-01 and 2026-10", "$month/$year"),
        "01/2025 and 10/2026"
    );
    assert_eq!(re.replace_all("2025-01", "${month}.${year}"), "01.2025");
}

#[test]
fn replace_all_expands_dollar_escape() {
    let re = Regex::new(r"\d+").unwrap();
    assert_eq!(re.replace_all("a1 b2", "$$$0"), "a$1 b$2");
}

#[test]
fn replace_all_accepts_every_string_replacer() {
    let re = Regex::new(r"\d").unwrap();
    let owned = String::from("#");
    assert_eq!(re.replace_all("1x2", "#"), "#x#");
    assert_eq!(re.replace_all("1x2", owned.clone()), "#x#");
    assert_eq!(re.replace_all("1x2", &owned), "#x#");
    assert_eq!(re.replace_all("1x2", Cow::Borrowed("#")), "#x#");
    assert_eq!(re.replace_all("1x2", Cow::<str>::Owned(owned)), "#x#");
}

#[test]
fn replace_all_with_a_closure_sees_the_captures() {
    let re = Regex::new(r"(?<k>\w+)=(?<v>\d+)").unwrap();
    let out = re.replace_all("a=1, b=22", |caps: &Captures| {
        format!("{}:{}", &caps["v"], &caps["k"])
    });
    assert_eq!(out, "1:a, 22:b");
}

#[test]
fn replace_all_with_a_closure_returning_str() {
    let re = Regex::new(r"\d").unwrap();
    let out = re.replace_all("1-2", |_: &Captures| "#");
    assert_eq!(out, "#-#");
}

#[test]
fn replace_all_with_no_expansion_inserts_dollars_literally() {
    let re = Regex::new(r"(\d)").unwrap();
    assert_eq!(re.replace_all("1 2", NoExpand("$1")), "$1 $1");
    assert_eq!(re.replace_all("1 2", NoExpand("")), " ");
}

#[test]
fn replace_all_with_by_ref_keeps_the_replacer() {
    let re = Regex::new(r"\d").unwrap();
    let mut count = 0;
    let mut counting = |_: &Captures| {
        count += 1;
        count.to_string()
    };
    assert_eq!(re.replace_all("1 2", counting.by_ref()), "1 2");
    assert_eq!(count, 2);
}

#[test]
fn replace_bytes_handles_text_that_is_not_utf8() {
    let re = Regex::new(r"(?<d>\d+)").unwrap();
    let text: &[u8] = b"\xff 12 \xfe 3";
    let out = re.replace_all_bytes(text, &b"<$d>"[..]);
    assert_eq!(out, &b"\xff <12> \xfe <3>"[..]);
    assert!(matches!(out, Cow::Owned(_)));
}

#[test]
fn replace_bytes_borrows_without_a_match() {
    let re = Regex::new(r"\d").unwrap();
    let text: &[u8] = b"\xff\xfe";
    let out = re.replace_bytes(text, &b"#"[..]);
    assert!(matches!(out, Cow::Borrowed(_)));
    assert_eq!(out, text);
}

#[test]
fn replacen_bytes_with_owned_and_no_expansion_replacers() {
    let re = Regex::new(r"(?<d>\d)").unwrap();
    let owned: Vec<u8> = b"<$d>".to_vec();
    assert_eq!(re.replace_all_bytes(b"4", owned.clone()), &b"<4>"[..]);
    assert_eq!(re.replace_all_bytes(b"4", &owned), &b"<4>"[..]);
    assert_eq!(
        re.replace_all_bytes(b"4", Cow::Borrowed(&b"<$d>"[..])),
        &b"<4>"[..]
    );
    assert_eq!(re.replace_all_bytes(b"4", NoExpandBytes(b"$d")), &b"$d"[..]);
}

#[test]
fn replace_all_bytes_closure_gets_bytes() {
    let re = Regex::new(r"\d").unwrap();
    let out = re.replace_all_bytes(b"1a2", |caps: &Captures| {
        let digit = caps[0].parse::<u32>().unwrap();
        (digit * 10).to_string().into_bytes()
    });
    assert_eq!(out, &b"10a20"[..]);
}

// === Regex::split ===

#[test]
fn split_yields_the_pieces_between_matches() {
    let re = Regex::new(r"\s*,\s*").unwrap();
    let parts: Vec<&str> = re.split("a , b,c").collect();
    assert_eq!(parts, ["a", "b", "c"]);
}

#[test]
fn split_keeps_empty_pieces_at_the_ends() {
    let re = Regex::new(r",").unwrap();
    assert_eq!(re.split(",x,").collect::<Vec<_>>(), ["", "x", ""]);
    assert_eq!(re.split(",,").collect::<Vec<_>>(), ["", "", ""]);
}

#[test]
fn split_without_a_match_yields_the_whole_text() {
    let re = Regex::new(r",").unwrap();
    assert_eq!(re.split("abc").collect::<Vec<_>>(), ["abc"]);
    assert_eq!(re.split("").collect::<Vec<_>>(), [""]);
}

#[test]
fn split_empty_pattern_splits_between_characters() {
    let re = Regex::new(r"").unwrap();
    assert_eq!(re.split("ab").collect::<Vec<_>>(), ["", "a", "b", ""]);
    assert_eq!(re.split("").collect::<Vec<_>>(), ["", ""]);
}

#[test]
fn split_follows_empty_matches_after_nonempty_ones() {
    // The matches are 0..1, 1..1 and 2..2, so the pieces are the text before
    // 0, between 0 and 1, between 1 and 2, and after 2. The regex crate
    // yields only ["", "b", ""] here.
    let re = Regex::new(r"a*").unwrap();
    assert_eq!(re.split("ab").collect::<Vec<_>>(), ["", "", "b", ""]);
}

#[test]
fn split_at_the_end_of_the_text_leaves_an_empty_last_piece() {
    let re = Regex::new(r"$").unwrap();
    assert_eq!(re.split("ab").collect::<Vec<_>>(), ["ab", ""]);
}

#[test]
fn split_pieces_and_matches_rebuild_the_text() {
    let re = Regex::new(r"x*").unwrap();
    let text = "axxbxc";
    let pieces: Vec<&str> = re.split(text).collect();
    let matches: Vec<&str> = re.find_iter(text).map(|m| m.as_str()).collect();
    assert_eq!(pieces.len(), matches.len() + 1);

    let mut rebuilt = String::from(pieces[0]);
    for (piece, matched) in pieces[1..].iter().zip(&matches) {
        rebuilt.push_str(matched);
        rebuilt.push_str(piece);
    }
    assert_eq!(rebuilt, text);
}

#[test]
fn split_non_ascii_pieces() {
    let re = Regex::new(r",").unwrap();
    assert_eq!(re.split("é,ü,日本").collect::<Vec<_>>(), ["é", "ü", "日本"]);
}

#[test]
fn split_iterators_are_fused() {
    fn assert_fused<I: std::iter::FusedIterator>(_: &I) {}

    let re = Regex::new(r",").unwrap();
    let mut pieces = re.split("a");
    assert_fused(&pieces);
    assert_eq!(pieces.next(), Some("a"));
    assert_eq!(pieces.next(), None);
    assert_eq!(pieces.next(), None);

    let mut limited = re.splitn("a", 2);
    assert_fused(&limited);
    assert_eq!(limited.next(), Some("a"));
    assert_eq!(limited.next(), None);
    assert_eq!(limited.next(), None);
}

// === Regex::splitn ===

#[test]
fn splitn_last_piece_is_the_unsplit_rest() {
    let re = Regex::new(r",").unwrap();
    assert_eq!(re.splitn("a,b,c", 2).collect::<Vec<_>>(), ["a", "b,c"]);
    assert_eq!(re.splitn("a,b,c", 3).collect::<Vec<_>>(), ["a", "b", "c"]);
}

#[test]
fn splitn_limit_one_yields_the_whole_text() {
    let re = Regex::new(r",").unwrap();
    assert_eq!(re.splitn("a,b,c", 1).collect::<Vec<_>>(), ["a,b,c"]);
    assert_eq!(re.splitn("", 1).collect::<Vec<_>>(), [""]);
}

#[test]
fn splitn_limit_zero_yields_nothing() {
    let re = Regex::new(r",").unwrap();
    assert_eq!(re.splitn("a,b,c", 0).count(), 0);
}

#[test]
fn splitn_limit_above_the_piece_count_yields_all_pieces() {
    let re = Regex::new(r",").unwrap();
    assert_eq!(re.splitn("a,b", 10).collect::<Vec<_>>(), ["a", "b"]);
    assert_eq!(re.splitn("a", 10).collect::<Vec<_>>(), ["a"]);
}

#[test]
fn splitn_counts_empty_matches_as_separators() {
    let re = Regex::new(r"a*").unwrap();
    assert_eq!(re.splitn("ab", 2).collect::<Vec<_>>(), ["", "b"]);
    assert_eq!(re.splitn("ab", 3).collect::<Vec<_>>(), ["", "", "b"]);
}

// === Regex::split_bytes ===

#[test]
fn split_bytes_handles_text_that_is_not_utf8() {
    let re = Regex::new(r",").unwrap();
    let text: &[u8] = b"\xff,\xfe,";
    let parts: Vec<&[u8]> = re.split_bytes(text).collect();
    assert_eq!(parts, [&b"\xff"[..], &b"\xfe"[..], &b""[..]]);
    let parts: Vec<&[u8]> = re.splitn_bytes(text, 2).collect();
    assert_eq!(parts, [&b"\xff"[..], &b"\xfe,"[..]]);
}