forbidden-strings 0.1.4

Out-of-band scanner for forbidden literal strings and regex patterns. Gitignore-aware, fast, dependency-light: built for CI deny-listing of leaked credentials and banned tokens.
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
// What:     `use resharp::Regex;` imports the resharp regex type.
//           Resharp's `Regex` holds a `Mutex<RegexInner>` for lazy DFA
//           growth, so calling `is_match`/`find_all` on a SHARED Regex
//           from multiple threads serializes through that lock. Each
//           rule gets its own Regex, so per-rule parallelism still
//           works (different mutexes).
// Why:      We use resharp only for the (smaller) regex bucket --
//           literals go through AC. The combined-over-regex-bucket
//           Regex acts as a fast "any regex rule might match?" gate.
// TS map:   `import { Regex } from "resharp";`.
//
// In TS you'd write (pseudocode):
// ```ts
// import { Regex } from "resharp";
// ```
use resharp::Regex;

// What:     `use std::panic::{catch_unwind, AssertUnwindSafe};` brings
//           the panic-recovery primitives into scope.
//           - `catch_unwind(closure)` runs the closure on the current
//             thread. If the closure panics, the panic is INTERCEPTED
//             instead of propagating: the call returns
//             `Err(Box<dyn Any + Send>)` carrying the panic payload.
//             Successful returns are wrapped in `Ok(value)`. The
//             intercepted panic does NOT cause the process to abort or
//             the thread to die -- execution continues after the call.
//           - `AssertUnwindSafe(value)` is a transparent wrapper that
//             asserts to the compiler "I have personally verified this
//             value's invariants survive a panic crossing my
//             closure". Required because Rust's `UnwindSafe` is an
//             AUTO-TRAIT (the compiler derives it structurally): it is
//             NOT implemented for `&T where T: !RefUnwindSafe`, and
//             `&resharp::Regex` is NOT `RefUnwindSafe` because
//             `Regex` holds a `Mutex<RegexInner>` (the lazy-DFA cache
//             from `resharp`'s lazy-determinisation strategy).
//             `AssertUnwindSafe` is sound for our usage because:
//             (a) every `Regex` instance is owned by exactly one
//                 `CompiledRegex` and lives the entire scanner run, so
//                 there is no shared interior state for a panic to
//                 corrupt across calls;
//             (b) a poisoned `Mutex` after a caught panic returns
//                 `PoisonError` on the next lock attempt, which
//                 resharp converts into one of its own `Error`
//                 variants -- our `.map_err(|_| ())` already swallows
//                 those into `Err(())`, exactly the same shape callers
//                 already handle (synthetic "engine error" hit in
//                 `scan.rs`); the failure stays inside the engine
//                 boundary;
//             (c) we never look at the panic payload, so payload-
//                 specific UnwindSafe concerns do not apply.
//             Siblings: `RefUnwindSafe` (same idea, for shared
//             references); `panic::resume_unwind` (re-throws a caught
//             payload -- we never want this here because we are the
//             top of the engine boundary, not a transparent passthrough).
// Why:      Resharp 0.5.x through 0.6.x panics on a handful of fuzzer-
//           discovered rule shapes -- one during `Regex::new` (algebra
//           overflow at `resharp-algebra/src/lib.rs:2470`), one during
//           `find_all` (engine "unexpected end" assertion at
//           `resharp/src/engine.rs:1020`, behind a `debug_assert!`
//           that fires in test profile but is compiled out of release;
//           release silently returns corrupted matches instead).
//           Both crashes are inside upstream code we do not own.
//           `Result::map_err` cannot catch panics; only `catch_unwind`
//           can. Without this wrapper an upstream panic propagates
//           through our process, libFuzzer records a crash, and
//           (more importantly) a production scanner run on the same
//           rule + content pair aborts the process instead of
//           degrading gracefully to "skip this
//           rule on this file". The scanner is a CI gate: an aborted
//           run silently passes the gate.
// TS map:   `try { ... } catch (e) { ... }` -- TS exceptions are
//           always caught structurally; Rust panics require an
//           explicit unwind barrier.
//
// In TS you'd write (pseudocode):
// ```ts
// // No equivalent. Rust requires catch_unwind + AssertUnwindSafe to
// // intercept panics across a closure boundary.
// ```
use std::panic::{catch_unwind, AssertUnwindSafe};

// What:     `use regex::bytes::Regex as PlainRegex;` imports the
//           standard `regex` crate's byte-mode regex type under an
//           alias to disambiguate from `resharp::Regex`. The `regex`
//           crate is Rust's mainline regex engine (Russ Cox-style
//           NFA + lazy DFA + Teddy literal accel); its compile path
//           is roughly 100x faster than resharp on patterns that
//           don't use set-algebra (`A&B`, `~(A)`). Resharp's
//           strength is set-algebra and bounded-state guarantees --
//           its compile cost is the price of admitting set
//           operations as first-class. For rules without set-algebra
//           (the overwhelming majority of our secret-detection
//           corpus -- 257 of 259 rules in the betterleaks example),
//           `regex` produces an equivalent matcher in a fraction of
//           the time.
// Why:      Phase 1 (regex compile) was the dominant remaining cost
//           at 2.0s of 2.96s total wall. Switching the 257
//           non-set-algebra rules to `regex` drops Phase 1 to
//           tens of milliseconds, putting total wall well under 1s
//           on the current corpus and providing the 5x growth
//           headroom the user asked for.
// TS map:   No equivalent crate exists in TS; closest is the
//           built-in `RegExp` which is engineered for pattern-search
//           rather than streaming bulk-text scan.
//
// In TS you'd write (pseudocode):
// ```ts
// // No 1:1; pretend `import { Regex as PlainRegex } from "regex-bytes";`
// ```
use regex::bytes::Regex as PlainRegex;

// What:     `pub enum CompiledRegex { Resharp(Regex), Plain(PlainRegex) }`
//           is the unified compiled-regex container. Each rule's
//           source is classified at load time (set-algebra vs not)
//           and routed to the appropriate engine. Both engines
//           satisfy the same `find_all`/`is_match` contract via
//           inherent methods on this enum.
// Why:      A single dispatch point keeps `scan.rs` engine-agnostic
//           on the hot path. Without this, `RegexRule.re` would have
//           to be `Box<dyn Trait>` -- which adds vtable indirection
//           per call AND prevents inlining. Static dispatch via
//           `match` lets LLVM specialize each branch.
// TS map:   `type CompiledRegex = { kind: "resharp"; re: Regex } | { kind: "plain"; re: PlainRegex };`.
//
// In TS you'd write (pseudocode):
// ```ts
// type CompiledRegex =
//   | { kind: "resharp"; re: Regex }
//   | { kind: "plain"; re: PlainRegex };
// ```
//
// Clippy lint suppressed: `Resharp` carries a 3.3 KiB inner DFA struct,
// while `Plain` is 32 bytes. Boxing the Resharp arm would add a heap
// indirection on every `find_all`/`is_match` (the hot path), regressing
// scan throughput. The size asymmetry is acceptable -- a few hundred
// `RegexRule` values is a one-time per-process cost.
#[allow(clippy::large_enum_variant)]
pub enum CompiledRegex {
    Resharp(Regex),
    Plain(PlainRegex),
}

// What:     `pub struct ScanMatch { pub start: usize, pub end: usize }`
//           is the engine-agnostic match record. Field-shape is
//           identical to `resharp::Match` so `scan.rs` code reading
//           `m.start`/`m.end` works unchanged whether the source
//           engine is resharp or regex. The fields are byte offsets
//           into the scanned content; `start` is inclusive, `end`
//           exclusive (half-open range).
// Why:      We can't expose `resharp::Match` directly when the match
//           originated from `regex` because regex's match type
//           (`regex::bytes::Match`) is a separate library type with
//           method-style accessors `.start()`/`.end()`. Translating
//           to a common record at the dispatch boundary keeps
//           call-sites uniform.
// TS map:   `type ScanMatch = { start: number; end: number };`.
//
// In TS you'd write (pseudocode):
// ```ts
// type ScanMatch = { start: number; end: number };
// ```
#[derive(Debug, Clone, Copy)]
pub struct ScanMatch {
    pub start: usize,
    pub end: usize,
}

impl CompiledRegex {
    // What:     `pub fn find_all(&self, content: &[u8]) -> Result<Vec<ScanMatch>, ()>`
    //           returns every non-overlapping match in `content` as
    //           a Vec of ScanMatch. The empty Vec means clean (no
    //           matches). The `Result::Err(())` arm covers engine-
    //           specific errors that callers don't need to
    //           distinguish (resharp can return `Error::TooLarge`
    //           on pathological inputs; we treat any error as
    //           "skip this rule on this file" rather than crash).
    // Why:      Single dispatch point for the violation-path
    //           `find_all` call from `scan.rs`. The `Result<_, ()>`
    //           shape lets callers use `if let Ok(matches) = ...`
    //           without unwrapping engine-specific error types.
    // TS map:   `findAll(content: Uint8Array): ScanMatch[]` (TS would
    //           throw on engine error rather than return Result).
    //
    // In TS you'd write (pseudocode):
    // ```ts
    // findAll(content: Uint8Array): ScanMatch[] {
    //   if (this.kind === "resharp") return this.re.findAll(content);
    //   return Array.from(this.re.findIter(content), (m) => ({ start: m.start, end: m.end }));
    // }
    // ```
    pub fn find_all(&self, content: &[u8]) -> Result<Vec<ScanMatch>, ()> {
        match self {
            // What:     `catch_unwind(AssertUnwindSafe(|| re.find_all(content)))`.
            //           - The outer `catch_unwind` runs the inner
            //             closure with an unwind barrier; any panic
            //             inside (resharp 0.5.x through 0.6.x has a
            //             `debug_assert!` in `scan_fwd_all` at
            //             `engine.rs:1020` that fires on
            //             `(?:lookahead&lookbehind)` shapes -- only
            //             when `debug_assertions` is on; in release
            //             that path returns corrupted matches instead)
            //             is intercepted and returned as the outer `Err`
            //             arm of a `Result<_, Box<dyn Any + Send>>`.
            //           - `AssertUnwindSafe(...)` wraps the closure so
            //             the compiler accepts that we have manually
            //             verified the captured `&Regex` (which is
            //             NOT `RefUnwindSafe` because it owns a
            //             `Mutex<RegexInner>` for lazy DFA growth) is
            //             safe to use across the panic boundary in
            //             our usage -- see the import-site comment
            //             for the full soundness argument.
            //           - The double-nested `match` flattens the
            //             two-level `Result<Result<_, _>, _>` into a
            //             single `Result<_, ()>`: an outer `Err`
            //             (caught panic), an inner `Err` (resharp's
            //             own `Error`), and the inner `Ok` are all
            //             distinct paths but the engine boundary
            //             erases the difference -- callers already
            //             treat `Err(())` as "engine refused; emit
            //             synthetic hit".
            // Why:      Defense in depth against upstream panics. A
            //           resharp panic without this wrapper would
            //           abort the whole scanner process; with it the
            //           panic stays inside the engine boundary,
            //           degrades the rule on this file only, and lets
            //           the rest of the scan proceed.
            // TS map:   `try { return { ok: true, value: this.re.findAll(content) }; } catch { return { ok: false }; }`.
            //
            // In TS you'd write (pseudocode):
            // ```ts
            // try {
            //   return { ok: true, value: this.re.findAll(content).map(m => ({ start: m.start, end: m.end })) };
            // } catch { return { ok: false }; }
            // ```
            CompiledRegex::Resharp(re) => {
                let caught = catch_unwind(AssertUnwindSafe(|| re.find_all(content)));
                match caught {
                    Ok(Ok(ms)) => Ok(ms
                        .into_iter()
                        .map(|m| ScanMatch { start: m.start, end: m.end })
                        .collect()),
                    Ok(Err(_)) => Err(()),
                    Err(_) => Err(()),
                }
            }
            CompiledRegex::Plain(re) => Ok(re
                .find_iter(content)
                .map(|m| ScanMatch { start: m.start(), end: m.end() })
                .collect()),
        }
    }

    // What:     `pub fn is_match(&self, content: &[u8]) -> Result<bool, ()>`
    //           is the short-circuit "any match anywhere" check. Used by
    //           the Combined residual shard's gate. Returns `Ok(true)` on
    //           a match, `Ok(false)` on a clean miss, and `Err(())` when
    //           the engine itself refuses to evaluate (resharp can return
    //           `Error::TooLarge` on pathological inputs).
    // Why:      Closes BUG 7: pre-fix the function folded engine errors
    //           into `false` via `unwrap_or(false)`, indistinguishable
    //           from a real "no match". For a secret-scanning tool that
    //           failure mode is fail-open -- a file the engine could not
    //           evaluate exits with zero hits, silently passing CI. Post-
    //           fix callers see the `Err` and emit a synthetic hit (or
    //           fall back to per-member evaluation when this is the gate
    //           of a multi-rule shard).
    // TS map:   `isMatch(content: Uint8Array): Result<boolean>`. TS has
    //           no Result; the equivalent would throw on engine error.
    //
    // In TS you'd write (pseudocode):
    // ```ts
    // isMatch(content: Uint8Array): { ok: true; value: boolean } | { ok: false } {
    //   if (this.kind === "resharp") {
    //     try { return { ok: true, value: this.re.isMatch(content) }; }
    //     catch { return { ok: false }; }
    //   }
    //   return { ok: true, value: this.re.isMatch(content) };
    // }
    // ```
    pub fn is_match(&self, content: &[u8]) -> Result<bool, ()> {
        match self {
            // What:     Same `catch_unwind(AssertUnwindSafe(...))`
            //           shape as `find_all`. `is_match` reuses the
            //           same resharp engine internals (lazy DFA),
            //           so the same `scan_fwd_*` panic surface is
            //           reachable from here -- on the gate path of
            //           a Combined residual shard, an `is_match` call
            //           against a content slice that trips resharp's
            //           assertion would abort the whole scanner
            //           process pre-fix. Post-fix it returns
            //           `Err(())`, and `scan.rs`'s existing
            //           `Err(()) => fall back to per-member find_all`
            //           branch handles the rest.
            // Why:      Symmetry with `find_all` -- any caller-
            //           visible engine surface must be unwind-safe
            //           or upstream panics escape the boundary.
            // TS map:   `try { return { ok: true, value: this.re.isMatch(content) }; } catch { return { ok: false }; }`.
            //
            // In TS you'd write (pseudocode):
            // ```ts
            // try { return { ok: true, value: this.re.isMatch(content) }; }
            // catch { return { ok: false }; }
            // ```
            CompiledRegex::Resharp(re) => {
                let caught = catch_unwind(AssertUnwindSafe(|| re.is_match(content)));
                match caught {
                    Ok(Ok(v)) => Ok(v),
                    Ok(Err(_)) => Err(()),
                    Err(_) => Err(()),
                }
            }
            CompiledRegex::Plain(re) => Ok(re.is_match(content)),
        }
    }
}

// What:     `fn requires_resharp(src: &str) -> bool` returns `true` when
//           `src` contains any feature the `regex` crate cannot parse
//           OR would parse with semantics different from resharp's.
//           Three feature families trigger true:
//           1. Set-algebra operators: unescaped `&` or `~(` outside a
//              character class (resharp's intersection / complement).
//           2. Lookaround groups: `(?=`, `(?!`, `(?<=`, `(?<!`. The
//              `regex` crate rejects these with "look-around, including
//              look-ahead and look-behind, is not supported"; resharp
//              accepts them.
//           3. Bare `_` outside a character class. Resharp treats `_`
//              as a universal wildcard (matches any single character),
//              while the `regex` crate treats it as a literal underscore.
//              Routing a rule like `pre_post` to the `regex` crate
//              would silently change its meaning -- the rule author
//              wrote a wildcard pattern, the matcher searched for a
//              literal seven-byte string. Escaped (`\_`) and class-
//              internal `_` ([_], [A-Z_]) stay literal in both engines
//              and do not trigger this branch.
//           Conservative: any of the above triggers true, even if the
//           resharp parser would have accepted a sequence the regex
//           crate also accepts (no false-positive cost beyond using the
//           slower engine).
// Why:      We need to dispatch each rule to its engine at compile time.
//           This shallow string scan avoids invoking either engine's
//           parser; the actual parse happens once via the chosen
//           engine. Regex character classes can contain `&` and parens
//           as literal bytes (e.g. `[&a-z]`, `[()]`) without those
//           characters carrying their group/algebra meaning, so we
//           track class membership and skip class interiors. Named
//           captures `(?<name>` / `(?P<name>` and non-capturing groups
//           `(?:` must NOT trigger -- the regex crate handles them --
//           so the lookbehind discriminator is "the byte after `(?<`
//           is `=` or `!`", not "the regex contains `(?<`".
// TS map:   `function requiresResharp(src: string): boolean`.
//
// In TS you'd write (pseudocode):
// ```ts
// function requiresResharp(src: string): boolean {
//   // walk bytes, skip \X escapes, track class membership,
//   // return true on outside-class `&`, `~(`, or any of
//   // `(?=`, `(?!`, `(?<=`, `(?<!`.
// }
// ```
pub fn requires_resharp(src: &str) -> bool {
    let bytes = src.as_bytes();
    let mut i = 0usize;
    let mut in_class = false;
    while i < bytes.len() {
        let c = bytes[i];
        if c == b'\\' {
            i += 2;
            continue;
        }
        if !in_class && c == b'[' {
            in_class = true;
            i += 1;
            continue;
        }
        if in_class && c == b']' {
            in_class = false;
            i += 1;
            continue;
        }
        if !in_class {
            if c == b'&' {
                return true;
            }
            if c == b'_' {
                return true;
            }
            if c == b'~' && i + 1 < bytes.len() && bytes[i + 1] == b'(' {
                return true;
            }
            // Lookaround detection. Shape: `(?` followed by `=`/`!` is
            // a lookahead; `(?<` followed by `=`/`!` is a lookbehind.
            // Other `(?...` forms (`(?:`, `(?P<`, `(?<name>`, `(?#...)`,
            // inline flags `(?i)`) are NOT lookarounds and the regex
            // crate handles them, so they must not trigger.
            if c == b'(' && i + 2 < bytes.len() && bytes[i + 1] == b'?' {
                let after = bytes[i + 2];
                if after == b'=' || after == b'!' {
                    return true;
                }
                if after == b'<'
                    && i + 3 < bytes.len()
                    && (bytes[i + 3] == b'=' || bytes[i + 3] == b'!')
                {
                    return true;
                }
            }
        }
        i += 1;
    }
    false
}

// What:     `const TROUBLESHOOT_REF: &str = "...";` is a compile-time
//           constant pointing readers from a runtime error message to
//           the long-form troubleshooting doc. `&str` here is a
//           reference into the binary's read-only string table -- no
//           allocation, no per-call cost.
// Why:      Centralise the doc reference so renaming or moving the
//           file updates one site, not five. Every message returned by
//           `lookaround_in_complement` ends with this constant.
// TS map:   `const TROUBLESHOOT_REF = "...";`.
//
// In TS you'd write (pseudocode):
// ```ts
// const TROUBLESHOOT_REF = "See TROUBLESHOOTING.resharp.md for workarounds.";
// ```
const TROUBLESHOOT_REF: &str = "See TROUBLESHOOTING.resharp.md for workarounds.";

// What:     `pub fn lookaround_in_complement(src: &str) -> Option<String>`
//           returns `Some(reason)` when `src` contains a `~(...)` whose
//           body holds an atom that resharp 0.5.x through 0.6.x cannot handle, and
//           `None` otherwise. The detected atoms are:
//             - `\b` (rewritten to a lookaround pair, then refused by
//               the reverse pass at `resharp-algebra/src/lib.rs:2234`)
//             - `\B` (parser falls through to the generic assertion
//               handler at `resharp-parser/src/lib.rs:1419-1424` and
//               rejects at parse time)
//             - unescaped `^` or `$` (rewritten to lookaround in
//               default-multiline mode at
//               `resharp-parser/src/lib.rs:1425-1441`, then refused)
//             - user-explicit lookaround `(?=`, `(?!`, `(?<=`, `(?<!`
//               (refused by the same reverse-pass arm)
//           The function tracks paren depth via a stack of "is this
//           open paren a complement-open" flags so we can recognise
//           when the matching close exits the complement. Character
//           class interiors `[...]` are skipped because inside a class
//           those bytes are literal, not the structural metacharacters.
// Why:      Catch the failure shape before the rule reaches
//           `resharp::Regex::new`, so the user gets an actionable
//           message that names the surface trigger ("complement body
//           contains \b") instead of resharp's opaque rendering
//           ("unsupported lookaround pattern" or
//           "UnsupportedResharpRegex"), which the user must reverse-
//           engineer back to their own input.
// TS map:   `function lookaroundInComplement(src: string): string | null`.
//
// In TS you'd write (pseudocode):
// ```ts
// function lookaroundInComplement(src: string): string | null {
//   // walk bytes; for each position, track:
//   //   inClass: are we inside `[...]`?
//   //   parenStack: bool[] -- true means the open paren was `~(`
//   // inside a complement (any `true` in the stack) and outside a class,
//   // reject \b, \B, ^, $, (?=, (?!, (?<=, (?<!.
// }
// ```
pub fn lookaround_in_complement(src: &str) -> Option<String> {
    let bytes = src.as_bytes();
    let mut i = 0usize;
    let mut in_class = false;
    // What:     `let mut paren_stack: Vec<bool> = Vec::new();`. A growable
    //           vector of `bool`. Each entry records the kind of an
    //           unclosed open paren -- `true` if the opener was `~(`,
    //           `false` for any other `(` (including non-capturing
    //           `(?:`, named `(?P<...>`, inline flags `(?i)`). On `)`
    //           we pop the top; tracking complement-ness depth-aware
    //           lets nested constructs like `~(.*(?:foo).*)` correctly
    //           identify the `~(` as the complement while the inner
    //           `(?:foo)` close does not exit the complement.
    // Why:      Without per-open kind tracking we cannot tell whether
    //           a `)` closes a complement or a regular group, so we
    //           cannot bound the complement body.
    // TS map:   `const parenStack: boolean[] = [];`.
    //
    // In TS you'd write (pseudocode):
    // ```ts
    // const parenStack: boolean[] = [];
    // ```
    let mut paren_stack: Vec<bool> = Vec::new();
    while i < bytes.len() {
        let c = bytes[i];
        // What:     `if c == b'\\' { ... }` handles regex escape
        //           sequences. The trigger atoms `\b` and `\B` ARE
        //           escape sequences, so we check the escapee byte
        //           BEFORE skipping past the pair. Outside a complement
        //           or inside a class, `\b` is literal-ish and we just
        //           skip the two bytes.
        // Why:      The trigger is the escape sequence itself, not the
        //           backslash. Treating `\\` as "skip 2" would let us
        //           miss `\b` and `\B` entirely.
        // TS map:   `if (c === 0x5c) { ... }` (0x5c = `\`).
        //
        // In TS you'd write (pseudocode):
        // ```ts
        // if (c === '\\'.charCodeAt(0)) {
        //   if (inComplement && !inClass && i + 1 < bytes.length) {
        //     const e = bytes[i + 1];
        //     if (e === 'b') return msgWordBoundary();
        //     if (e === 'B') return msgNotWordBoundary();
        //   }
        //   i += 2; continue;
        // }
        // ```
        if c == b'\\' {
            let in_complement = !in_class && paren_stack.iter().any(|&k| k);
            if in_complement && i + 1 < bytes.len() {
                match bytes[i + 1] {
                    b'b' => {
                        return Some(format!(
                            "complement body contains \\b; resharp 0.5.x through 0.6.x rewrites it to an internal lookaround which the reverse pass refuses. Replace with \\W (consumes a char on each side) or literal whitespace, or move the boundary check outside the complement. {}",
                            TROUBLESHOOT_REF
                        ));
                    }
                    b'B' => {
                        return Some(format!(
                            "complement body contains \\B; resharp 0.5.x through 0.6.x rejects it at parse time when its neighbours are unclassifiable. Restructure the rule to avoid \\B inside the complement. {}",
                            TROUBLESHOOT_REF
                        ));
                    }
                    _ => {}
                }
            }
            i += 2;
            continue;
        }
        if !in_class && c == b'[' {
            in_class = true;
            i += 1;
            continue;
        }
        if in_class && c == b']' {
            in_class = false;
            i += 1;
            continue;
        }
        if !in_class {
            // What:     `let in_complement = paren_stack.iter().any(|&k| k);`
            //           returns `true` when ANY entry in the paren
            //           stack is a complement-open. Equivalent to
            //           "we are nested inside at least one `~(`".
            //           `.iter()` borrows the vec; `.any(closure)`
            //           short-circuits on the first match.
            // Why:      A `^` inside a regular group nested inside a
            //           complement (`~(foo(.|\n)*^bar)`) is still
            //           "inside the complement" for resharp's purposes;
            //           the rewrite happens regardless of intermediate
            //           non-complement parens.
            // TS map:   `const inComplement = parenStack.some(Boolean);`.
            //
            // In TS you'd write (pseudocode):
            // ```ts
            // const inComplement = parenStack.some(Boolean);
            // ```
            let in_complement = paren_stack.iter().any(|&k| k);
            if in_complement {
                if c == b'^' {
                    return Some(format!(
                        "complement body contains ^; resharp 0.5.x through 0.6.x rewrites it to a lookbehind in default-multiline mode, which the reverse pass refuses. Use \\A for whole-content start-anchor semantics, or move the anchor outside the complement. {}",
                        TROUBLESHOOT_REF
                    ));
                }
                if c == b'$' {
                    return Some(format!(
                        "complement body contains $; resharp 0.5.x through 0.6.x rewrites it to a lookahead in default-multiline mode, which the reverse pass refuses. Use \\z for whole-content end-anchor semantics, or move the anchor outside the complement. {}",
                        TROUBLESHOOT_REF
                    ));
                }
                if c == b'(' && i + 2 < bytes.len() && bytes[i + 1] == b'?' {
                    let after = bytes[i + 2];
                    if after == b'=' || after == b'!' {
                        return Some(format!(
                            "complement body contains a lookahead (?{}; the reverse pass refuses complement-of-lookaround. Lift the lookaround outside the complement. {}",
                            after as char, TROUBLESHOOT_REF
                        ));
                    }
                    if after == b'<'
                        && i + 3 < bytes.len()
                        && (bytes[i + 3] == b'=' || bytes[i + 3] == b'!')
                    {
                        return Some(format!(
                            "complement body contains a lookbehind (?<{}; the reverse pass refuses complement-of-lookaround. Lift the lookaround outside the complement. {}",
                            bytes[i + 3] as char, TROUBLESHOOT_REF
                        ));
                    }
                }
            }
            // What:     Push/pop the paren stack. Order matters: detect
            //           `~(` BEFORE the bare-`(` arm, otherwise the `~`
            //           and `(` would be pushed independently and we
            //           would miscount.
            // Why:      Maintain accurate complement-depth tracking
            //           across nested groups.
            // TS map:   The same push/pop pattern in JS.
            //
            // In TS you'd write (pseudocode):
            // ```ts
            // if (c === '~' && bytes[i+1] === '(') { parenStack.push(true); i += 2; continue; }
            // if (c === '(') { parenStack.push(false); i += 1; continue; }
            // if (c === ')') { parenStack.pop(); i += 1; continue; }
            // ```
            if c == b'~' && i + 1 < bytes.len() && bytes[i + 1] == b'(' {
                paren_stack.push(true);
                i += 2;
                continue;
            }
            if c == b'(' {
                paren_stack.push(false);
                i += 1;
                continue;
            }
            if c == b')' {
                paren_stack.pop();
                i += 1;
                continue;
            }
        }
        i += 1;
    }
    None
}

// What:     `pub fn intersection_with_lookbehind(src: &str) -> Option<String>`
//           detects rule shapes that match resharp 0.5.x through 0.6.x's
//           lookahead-vs-lookbehind intersection debug_assert at
//           `resharp/src/engine.rs:1020` (`unexpected end 0 > N`,
//           where N varies by content length: 56 in 0.5.3, 1 in 0.6.0,
//           the assertion lives behind `debug_assert!` so release
//           returns corrupted matches silently instead). The minimal
//           reproducer is
//           `(?:(?=a)&(?<=_))` driven against a content slice of
//           at least 64 bytes; the panic fires inside
//           `scan_fwd_all` during the runtime forward scan, not
//           at compile. The detector reports the shape at
//           compile time so callers get an actionable error
//           BEFORE the rule reaches the scan path.
// Why:      Catch-and-convert via `catch_unwind` in
//           `CompiledRegex::find_all` already keeps the scanner
//           process alive; this pre-validator gives the rule
//           author a clean message ("intersection involving a
//           lookbehind") instead of a generic engine-error
//           synthetic hit. Bisection (see TROUBLESHOOTING.resharp.md)
//           narrowed the trigger to "intersection (`&` outside
//           class) where at least one operand contains a
//           lookbehind `(?<=` or `(?<!`". The two-lookahead
//           variant (`(?:(?=a)&(?=b))`) does not panic; it
//           returns `Algebra(UnsupportedPattern)` -- only the
//           lookbehind path hits the assertion. Detection is
//           conservative: any presence of intersection + any
//           lookbehind anywhere outside a class triggers; we
//           accept rare false positives on contrived shapes
//           (rule authors get a friendlier "rewrite without
//           intersecting a lookbehind" message instead of the
//           opaque assertion) in exchange for not having to walk
//           operand boundaries.
// TS map:   `function intersectionWithLookbehind(src: string): string | null`.
//
// In TS you'd write (pseudocode):
// ```ts
// function intersectionWithLookbehind(src: string): string | null {
//   // walk bytes outside character classes; set hasIntersection on
//   // bare `&`; set hasLookbehind on `(?<=` or `(?<!`. Return
//   // a reason when both are seen, null otherwise.
// }
// ```
pub fn intersection_with_lookbehind(src: &str) -> Option<String> {
    // What:     Single-pass walker: track `in_class` membership;
    //           on bare `&` outside class set `has_intersection`;
    //           on `(?=` / `(?!` / `(?<=` / `(?<!` outside class
    //           set `has_lookaround` and record the kind seen.
    //           Return early as soon as both are seen.
    // Why:      The original detector covered `&` + lookbehind only
    //           (the panic the user's HANDOVER bisected to).
    //           Subsequent fuzzer findings show resharp 0.5.x through
    //           0.6.x also panics on `&` + lookahead shapes via the
    //           same `engine.rs:1020` assertion. Widening keeps the
    //           detection symmetric across lookaround direction.
    //           Avoid the cost of a second pass; rule sources are
    //           short and a single linear scan is plenty.
    // TS map:   Same shape; one for-loop with two booleans.
    let bytes = src.as_bytes();
    let mut i = 0usize;
    let mut in_class = false;
    let mut has_intersection = false;
    let mut has_lookaround = false;
    // What:     `lookaround_kind: &str` records which lookaround
    //           direction triggered the flag, used in the error
    //           message so the author can find the offending
    //           assertion / lookbehind quickly. "lookbehind" or
    //           "lookahead" -- whichever was seen first.
    // Why:      Diagnostic clarity. Without it the message is
    //           generic and the rule author has to scan the source
    //           to find which lookaround direction caused the
    //           rejection.
    // TS map:   `let lookaroundKind = "";`.
    let mut lookaround_kind: &'static str = "";
    while i < bytes.len() {
        let c = bytes[i];
        // What:     Escape sequence `\X` -- skip two bytes, do
        //           not interpret the second byte as a structural
        //           character.
        // Why:      `\&` and `\(` inside the source are literal
        //           bytes the regex parser treats as the actual
        //           character, not as the metacharacter.
        // TS map:   `if (c === 0x5c) { i += 2; continue; }`.
        if c == b'\\' {
            i += 2;
            continue;
        }
        if !in_class && c == b'[' {
            in_class = true;
            i += 1;
            continue;
        }
        if in_class && c == b']' {
            in_class = false;
            i += 1;
            continue;
        }
        if !in_class {
            if c == b'&' {
                has_intersection = true;
            }
            // What:     `(?=` / `(?!` / `(?<=` / `(?<!` lookaround
            //           detection. The `(?` prefix can start many
            //           constructs:
            //             - `(?=...)` positive lookahead
            //             - `(?!...)` negative lookahead
            //             - `(?<=...)` positive lookbehind
            //             - `(?<!...)` negative lookbehind
            //             - `(?<name>...)` named capture (NOT a lookaround)
            //             - `(?:...)`, `(?i)`, `(?P<name>` etc. (also not)
            //           The discriminator is the byte after `(?`:
            //             - `=` / `!` means lookahead
            //             - `<` followed by `=` / `!` means lookbehind
            //             - any other shape is not a lookaround
            // Why:      Catch both directions of lookaround. The
            //           debug_assert at `engine.rs:1020` fires for
            //           intersection involving any lookaround, not
            //           just lookbehind specifically.
            // TS map:   `if (c === '(' && b[i+1] === '?' && (b[i+2] === '=' || b[i+2] === '!' || (b[i+2] === '<' && (b[i+3] === '=' || b[i+3] === '!'))))`.
            if c == b'(' && i + 2 < bytes.len() && bytes[i + 1] == b'?' {
                let after = bytes[i + 2];
                if after == b'=' || after == b'!' {
                    has_lookaround = true;
                    if lookaround_kind.is_empty() {
                        lookaround_kind = "lookahead";
                    }
                } else if after == b'<'
                    && i + 3 < bytes.len()
                    && (bytes[i + 3] == b'=' || bytes[i + 3] == b'!')
                {
                    has_lookaround = true;
                    if lookaround_kind.is_empty() {
                        lookaround_kind = "lookbehind";
                    }
                }
            }
            if has_intersection && has_lookaround {
                return Some(format!(
                    "intersection (`&`) involving a {} triggers a known resharp 0.5.x through 0.6.x debug_assert in `scan_fwd_all` (`engine.rs:1020`); behind a `debug_assert!` so release silently returns wrong matches. Rewrite the rule to lift the {} outside the intersection (e.g. anchor it as a prefix), or replace it with an explicit consume of the relevant byte. {}",
                    lookaround_kind, lookaround_kind, TROUBLESHOOT_REF
                ));
            }
        }
        i += 1;
    }
    None
}

// What:     `pub fn lookaround_in_alternation_with_sibling(src: &str) -> Option<String>`
//           detects rule shapes that compile through resharp's
//           parser but trigger the `engine.rs:1020` `debug_assert!`
//           (`unexpected end 0 > N`) at scan time. The minimal
//           reproducer bisected from
//           `fuzz/artifacts/fuzz_extract_gate_soundness/crash-8cba104f0805ccb567513aff895398a4f652200c`
//           is `(a|(?![_]))(?!a)` -- a capturing alternation whose
//           branches include a negative lookahead with a single-char
//           class body, followed by ANOTHER negative lookahead. The
//           pattern compiles (because alternation provides a
//           non-lookaround branch the algebra can simplify against)
//           but scanning panics during the forward DFA pass.
//
//           Variants confirmed to trigger the same panic:
//             - `(a|(?![_]))(?![a-e-u-vaaa])` (original artifact)
//             - `(?:a|(?![_]))(?!a)` (non-capturing first group)
//             - `((?![_])|a)(?!a)` (lookaround as first alt branch)
//             - `(a|(?![X]))(?!a)` for X in `_`, `0`, `.`, `-`, `|`, `^a`
//
//           Variants that do NOT trigger:
//             - `(a|(?!a))(?!a)` (bare atom in first lookahead, not class)
//             - `(a|(?![ab]))(?!a)` (class with two chars)
//             - `(?!a)(a|(?!a))` (lookaround before alternation, not after)
//             - `(?!a)b(?!c)` (atom between two lookaheads, no alt)
// Why:      `catch_unwind` in `CompiledRegex::find_all` already
//           converts the upstream panic into `Err(())` so production
//           scanning degrades gracefully. But libFuzzer-sys's panic
//           hook calls `abort()` before `catch_unwind` can intercept
//           in the fuzz harness, so the fuzz target sees a crash on
//           every iteration that hits this shape. The pre-validator
//           rejects the shape at compile time, surfacing an
//           actionable error and skipping the input so the fuzzer
//           can continue exploring the (?u)-Unicode space the
//           soundness-by-revert phase 11 verification needs.
//
//           Detection algorithm (single-pass byte walker):
//             - Maintain a stack of `(has_alternation, has_lookaround)`
//               flags, one entry per open paren. Each `(` pushes
//               `(false, false)`; each `)` pops the top entry.
//             - On `|` outside class, set top entry's
//               `has_alternation = true` (the alternation belongs
//               to the innermost group).
//             - On `(?=` / `(?!` / `(?<=` / `(?<!`, count
//               `total_lookarounds += 1` AND mark the CURRENT top
//               of the stack (the parent group containing this
//               lookaround) `has_lookaround = true` -- this models
//               "this group's body contains a lookaround". Push
//               `(false, false)` for the lookaround's own group
//               (its body is irrelevant for our pattern).
//             - On `)` pop: if the popped frame has BOTH
//               `has_alternation && has_lookaround` AND
//               `total_lookarounds >= 2` (there is another
//               lookaround outside this group), return Some(reason).
//           Conservative: false positives are acceptable. The
//           panicking shape is virtually never authored
//           intentionally; rule authors writing intersection-of-
//           lookaround patterns are rare, and the alternative is
//           a fuzz crash on every iteration.
// TS map:   `function lookaroundInAlternationWithSibling(src: string): string | null`.
//
// In TS you'd write (pseudocode):
// ```ts
// function lookaroundInAlternationWithSibling(src: string): string | null {
//   // walk bytes; maintain paren stack of (hasAlt, hasLookaround);
//   // track totalLookarounds. On `)` pop, check the combined
//   // pattern; return reason when matched.
// }
// ```
pub fn lookaround_in_alternation_with_sibling(src: &str) -> Option<String> {
    let bytes = src.as_bytes();
    let mut i = 0usize;
    let mut in_class = false;
    // What:     `paren_stack: Vec<(bool, bool)>` per-open-paren flags.
    //           Each entry `(has_alternation, has_lookaround)`
    //           records two facts about the open group's body so
    //           far: "did we see a `|` at this depth" and "does
    //           this group's body contain at least one lookaround".
    // Why:      The panicking shape has alt+lookaround inside one
    //           group; we need per-depth tracking because flat
    //           counters would confuse "alternation in group A,
    //           lookaround in group B" with the real pattern
    //           "alternation AND lookaround both in group A".
    // TS map:   `const parenStack: Array<[boolean, boolean]> = [];`.
    let mut paren_stack: Vec<(bool, bool)> = Vec::new();
    // What:     `total_lookarounds: usize` counts every lookaround
    //           opening in the source, regardless of depth or
    //           position. Used in the final-check to know whether
    //           the alt+la group had a sibling lookaround anywhere
    //           else in the source.
    // Why:      The panicking shape always has TWO or more
    //           lookarounds in the source; one alone (even inside
    //           alternation) doesn't trigger.
    // TS map:   `let totalLookarounds = 0;`.
    let mut total_lookarounds: usize = 0;
    // What:     `found_alt_la_group: bool` is set when ANY closed
    //           group's body had both alternation and at least one
    //           lookaround. Sticky -- once set, stays set.
    // Why:      The sibling lookaround may appear AFTER the
    //           alt+la group closes (e.g. `(a|(?![_]))(?!a)`); we
    //           can't fire at close time because we don't yet
    //           know about future siblings. Tracking the flag and
    //           checking at the end of the walk handles both
    //           "sibling before" and "sibling after" symmetrically.
    // TS map:   `let foundAltLaGroup = false;`.
    let mut found_alt_la_group = false;
    while i < bytes.len() {
        let c = bytes[i];
        if c == b'\\' {
            i += 2;
            continue;
        }
        if !in_class && c == b'[' {
            in_class = true;
            i += 1;
            continue;
        }
        if in_class && c == b']' {
            in_class = false;
            i += 1;
            continue;
        }
        if in_class {
            i += 1;
            continue;
        }
        // What:     Alternation `|` outside class. Marks the
        //           innermost open group as containing alternation.
        // Why:      Belongs to the innermost group; need per-depth
        //           tracking.
        if c == b'|' {
            if let Some(top) = paren_stack.last_mut() {
                top.0 = true;
            }
            i += 1;
            continue;
        }
        // What:     Group open `(`. Two cases:
        //           - Lookaround open `(?=`/`(?!`/`(?<=`/`(?<!`:
        //             count it, mark CURRENT top-of-stack as
        //             containing a lookaround, then push a fresh
        //             frame for the lookaround's own group body.
        //           - Other `(...)` (capturing, non-capturing, named,
        //             flags, comment): push a fresh frame.
        // Why:      The lookaround's PARENT group is the one
        //           containing it; the lookaround's own body is
        //           irrelevant for the pattern we're matching.
        if c == b'(' {
            let is_lookaround = i + 2 < bytes.len()
                && bytes[i + 1] == b'?'
                && (matches!(bytes[i + 2], b'=' | b'!')
                    || (bytes[i + 2] == b'<'
                        && i + 3 < bytes.len()
                        && matches!(bytes[i + 3], b'=' | b'!')));
            if is_lookaround {
                total_lookarounds += 1;
                if let Some(top) = paren_stack.last_mut() {
                    top.1 = true;
                }
            }
            paren_stack.push((false, false));
            i += 1;
            continue;
        }
        // What:     Group close `)`. Pop the top frame. If the
        //           popped frame had BOTH alternation AND at
        //           least one lookaround in its body, set the
        //           sticky `found_alt_la_group` flag. Also bubble
        //           the popped frame's has_lookaround up to the
        //           parent (a group contains a lookaround if any
        //           nested group did).
        // Why:      We defer the final fire-decision to end of
        //           walk because the sibling lookaround may appear
        //           AFTER the alt+la group closes. The bubble
        //           preserves the per-depth invariant: an outer
        //           group's body has a lookaround iff a nested
        //           subgroup body did.
        if c == b')' {
            let popped = paren_stack.pop().unwrap_or((false, false));
            if popped.0 && popped.1 {
                found_alt_la_group = true;
            }
            if popped.1 {
                if let Some(parent) = paren_stack.last_mut() {
                    parent.1 = true;
                }
            }
            i += 1;
            continue;
        }
        i += 1;
    }
    // What:     Final check: fire when any closed group had both
    //           alternation AND a lookaround in its body. The
    //           `total_lookarounds` counter is retained for debugging
    //           but is no longer required to gate the fire decision.
    // Why:      The original detector required `total_lookarounds >= 2`
    //           on the theory that the shape always has a sibling
    //           lookaround. Bisection of
    //           `crash-c3c364eb3a03114a52015721c02cba0bf20eb496` (rendered
    //           as `(?:        4qüVk|o\w|\s(?![_]))23o:aaaaaaaaaaaaaaa`)
    //           showed that a SINGLE lookaround inside an alternation
    //           followed by literal content can also trip
    //           `engine.rs:1020` at find-all time -- the compile
    //           succeeds, the panic fires during `scan_fwd_all`. The
    //           threshold of 2 was an over-narrow heuristic from the
    //           first crash shape; widening to "any alt+la group"
    //           accepts a small false-positive rate in exchange for
    //           defense against the broader panic class.
    let _ = total_lookarounds;
    if found_alt_la_group {
        return Some(format!(
            "alternation containing a lookaround triggers a known resharp 0.5.x through 0.6.x debug_assert in `scan_fwd_all` (`engine.rs:1020`; `unexpected end 0 > N`). Minimal reproducers: `(a|(?![_]))(?!a)` and `(?:literal|other|x(?![_]))trailing`. The shape compiles through the resharp parser because the non-lookaround alt branch lets the algebra simplify, but the forward DFA scan trips the assertion when the input drives the engine into the lookaround branch. Rewrite to remove the alternation, lift the lookaround outside, replace with an explicit byte consume, or split the rule into two separate patterns. {}",
            TROUBLESHOOT_REF
        ));
    }
    None
}

// What:     `pub fn intersection_with_word_end_alternation(src: &str) -> Option<String>`
//           detects rule shapes that match resharp 0.5.x through 0.6.x's
//           algebra arithmetic-overflow panic at
//           `resharp-algebra/src/lib.rs:2470`
//           (`attempt to add with overflow` inside
//           `attempt_rw_concat_2`; the `overflow-checks = true`
//           profile setting in our Cargo.toml is load-bearing for
//           this panic to fire in release). The minimum bisected
//           reproducer is `(?:\w|$)(?:(?![1g]\_X)& a)`: an
//           alternation containing both `\w` and the end-anchor
//           `$` concatenated with an intersection whose operand
//           contains a negative lookahead enclosing a character
//           class followed by additional literal bytes. The
//           overflow happens during DFA derivative construction,
//           reached from `Regex::new`.
// Why:      Bisection (see TROUBLESHOOTING.resharp.md) showed
//           the trigger is robust to the specific lookahead
//           class contents and the surrounding scoped-flag wrap.
//           The cheapest stable signal is "intersection (`&`
//           outside class) co-occurring with both `\w`
//           shorthand and `$` end-anchor (outside class) in the
//           same rule source". Real secret-detection rules
//           rarely combine all three -- they are either pure
//           literal-prefix patterns or simple character classes
//           -- so the false-positive rate is low. The catch_unwind
//           wrap in `compile_rule_src` is the load-bearing
//           safety net; this pre-validator turns the panic into
//           an actionable message for the common shape.
// TS map:   `function intersectionWithWordEndAlternation(src: string): string | null`.
//
// In TS you'd write (pseudocode):
// ```ts
// function intersectionWithWordEndAlternation(src: string): string | null {
//   // walk bytes outside character classes; flag `&` outside class,
//   // flag `\w`, flag `$`. Return reason when all three are present.
// }
// ```
pub fn intersection_with_word_end_alternation(src: &str) -> Option<String> {
    let bytes = src.as_bytes();
    let mut i = 0usize;
    let mut in_class = false;
    let mut has_intersection = false;
    let mut has_word_shorthand = false;
    let mut has_end_anchor = false;
    while i < bytes.len() {
        let c = bytes[i];
        if c == b'\\' && i + 1 < bytes.len() {
            // What:     Detect `\w` (and `\W`) as the word
            //           shorthand. Inside a character class the
            //           same escape compiles to the byte-set
            //           definition rather than the alternation
            //           shape; the panic correlate appears only
            //           outside a class, so we gate on
            //           `!in_class`.
            // Why:      Match the shape we bisected to a panic.
            // TS map:   `if (b[i+1] === 'w' || b[i+1] === 'W')`.
            if !in_class && (bytes[i + 1] == b'w' || bytes[i + 1] == b'W') {
                has_word_shorthand = true;
            }
            i += 2;
            continue;
        }
        if !in_class && c == b'[' {
            in_class = true;
            i += 1;
            continue;
        }
        if in_class && c == b']' {
            in_class = false;
            i += 1;
            continue;
        }
        if !in_class {
            if c == b'&' {
                has_intersection = true;
            }
            if c == b'$' {
                has_end_anchor = true;
            }
            if has_intersection && has_word_shorthand && has_end_anchor {
                return Some(format!(
                    "intersection (`&`) co-occurring with `\\w` shorthand and `$` end-anchor triggers a known resharp 0.5.x through 0.6.x arithmetic-overflow panic in `attempt_rw_concat_2` (`resharp-algebra/src/lib.rs:2470`). Rewrite the rule to avoid this combination -- typically by replacing `\\w` with an explicit character class (`[A-Za-z0-9_]`) or by lifting the end-anchor outside the intersection. {}",
                    TROUBLESHOOT_REF
                ));
            }
        }
        i += 1;
    }
    None
}

// What:     `pub fn stacked_quantifier(src: &str) -> Option<String>`
//           detects two regex quantifier suffixes appearing back-to-back
//           without an atom or group between them: `a**`, `\D{5,11}{5,11}`,
//           `(?:a){2}{3}`, `a*?+`, `a*??`. Returns `Some(reason)` when the
//           shape is found and `None` otherwise.
// Why:      Stacked quantifiers expand multiplicatively in NFA-based
//           regex engines. The `regex` crate parses `a{5,11}{5,11}` as
//           "5-11 reps of (5-11 reps of `a`)" -- 25-121 reps -- and
//           five-deep stacking like the fuzz-discovered slow-unit
//           `\D{5,11}{5,11}{5,11}{5,11}{5,11}\D*****aa` reaches 5^5
//           through 11^5 (~161,051 reps). With `(?u)` widening `\D`
//           to the full non-decimal-digit Unicode class, NFA size
//           exceeds the crate's 256 MB limit and `RegexBuilder::build`
//           wall-clocks at 1.4-1.5 seconds before erroring with
//           `CompiledTooBig`. The plain path tries `unicode(false)`
//           first then `unicode(true)`, so the total compile call
//           takes ~2.9s -- well past libFuzzer's `report_slow_units`
//           threshold of 10s once ASAN overhead and the fuzz-corpus
//           replay loop are accounted for. The resharp parser rejects
//           stacked quantifiers in microseconds with
//           `UnsupportedResharpRegex`, but the slow-unit shape lacks
//           any `requires_resharp` trigger (no `&`, `_`, `~(`,
//           lookaround), so it never reaches resharp.
//
//           Stacked quantifiers are virtually never a legitimate
//           authored pattern: the regex crate accepts them by treating
//           the outer as a quantifier on the inner-quantified atom,
//           but no realistic secret-detection rule needs this shape.
//           Rejecting at the pre-validator level is conservative and
//           keeps the fuzz target moving past inputs that would
//           otherwise burn the entire budget on one compile attempt.
//
//           Detection runs as a single-pass byte walker over `src`
//           and skips:
//             - escape pairs `\X` (so `\{` is a literal `{`, not a
//               quantifier-brace start);
//             - character class interiors `[...]` (where the
//               metacharacters are literal bytes);
//             - the `?` byte immediately after `(` (so `(?:`, `(?i)`,
//               `(?<=`, `(?P<name>` are not counted as a `?`
//               quantifier).
//           A quantifier is recognised as one of `*`, `+`, `?`, or
//           `{` followed by an ASCII digit (so a literal `{abc}` is
//           NOT treated as a quantifier). After a primary quantifier,
//           a single trailing `?` (lazy) or `+` (possessive) modifier
//           is consumed as part of the same quantifier; a second
//           quantifier byte after that is the stacked case we flag.
//
//           Place the call BEFORE `requires_resharp` in
//           `compile_rule_src` so the rejection reads as "this
//           pattern is structurally bad", not "the plain path
//           specifically dislikes it". Either engine would either
//           reject the shape outright (resharp) or wall-clock on it
//           (regex crate); the structural rejection is the honest
//           framing.
// TS map:   `function stackedQuantifier(src: string): string | null`.
//
// In TS you'd write (pseudocode):
// ```ts
// function stackedQuantifier(src: string): string | null {
//   // walk bytes; track in_class and just_consumed_quant;
//   // skip \X escapes and class interiors and `(?` group prefixes;
//   // when a quantifier start is seen and just_consumed_quant is true,
//   // return Some(reason); otherwise consume the quantifier and any
//   // single trailing `?`/`+` modifier, set just_consumed_quant=true.
// }
// ```
pub fn stacked_quantifier(src: &str) -> Option<String> {
    let bytes = src.as_bytes();
    let mut i = 0usize;
    let mut in_class = false;
    // What:     `just_consumed_quant` tracks whether the previous byte
    //           span consumed a full quantifier (`*`/`+`/`?`/`{N,M}`
    //           plus optional `?`/`+` modifier). The next quantifier
    //           start while this is `true` is the stacked case.
    //           Resets to `false` on any non-quantifier byte: a literal
    //           byte starts a fresh atom; `(`, `|`, `)`, anchors all
    //           re-anchor the parse state to "atom may follow".
    // Why:      The stacked-quantifier failure is exactly two
    //           quantifier suffixes back-to-back; the state-machine
    //           pinpoints that adjacency without parsing the regex.
    // TS map:   `let justConsumedQuant = false;`.
    let mut just_consumed_quant = false;
    while i < bytes.len() {
        let c = bytes[i];
        // What:     `if c == b'\\' { i += 2; ... }` skips the escape
        //           pair. `\{`, `\}`, `\*`, `\+`, `\?` are all literal
        //           bytes, NOT quantifier starts. Resetting
        //           `just_consumed_quant = false` is correct: the
        //           escape pair represents an atom (a single
        //           literal/shorthand), so a quantifier may follow it
        //           but the escape itself is not a quantifier.
        // Why:      Without this skip, `\{` would be mis-detected as a
        //           bounded-quantifier start and the algorithm would
        //           walk past the `}` of a literal byte sequence.
        // TS map:   `if (c === 0x5c) { i += 2; justConsumedQuant = false; continue; }`.
        if c == b'\\' {
            i += 2;
            just_consumed_quant = false;
            continue;
        }
        // What:     Character class entry / exit. Inside a class,
        //           `*`/`+`/`?`/`{` are literal bytes; we don't
        //           reason about quantifiers there.
        // Why:      `[*+?{]` is a five-byte literal class, not five
        //           stacked quantifiers.
        // TS map:   `if (!inClass && c === 0x5b) { inClass = true; ... }`.
        if !in_class && c == b'[' {
            in_class = true;
            just_consumed_quant = false;
            i += 1;
            continue;
        }
        if in_class {
            if c == b']' {
                in_class = false;
            }
            i += 1;
            continue;
        }
        // What:     `(?` group prefix. The `?` is part of the group
        //           syntax (non-capturing `(?:`, inline flags `(?i)`,
        //           lookarounds `(?=`/`(?!`/`(?<=`/`(?<!`, named
        //           captures `(?P<name>`/`(?<name>`, comments
        //           `(?#...)`). Skipping the two bytes prevents the
        //           `?` from being misread as a `?` quantifier on the
        //           preceding atom -- which it is not, because the
        //           preceding atom is `(`, which starts a new
        //           sub-expression rather than ending one.
        // Why:      Without the skip, `(?:a)*` would parse as
        //           "open-paren, lazy-quantifier-on-open-paren, ..."
        //           which is structurally wrong and would falsely flag
        //           any `(?:...){...}` shape as stacked.
        // TS map:   `if (c === 0x28 && b[i+1] === 0x3f) { i += 2; ... }`.
        if c == b'(' && i + 1 < bytes.len() && bytes[i + 1] == b'?' {
            i += 2;
            just_consumed_quant = false;
            continue;
        }
        // What:     Recognise a quantifier start. `{` is only a
        //           quantifier start if followed by an ASCII digit;
        //           otherwise it is a literal `{` (e.g. `{abc}` is
        //           treated as the four literal bytes `{abc}` by the
        //           regex crate when the contents do not parse as a
        //           repetition spec).
        // Why:      The fuzz generator never emits literal `{` outside
        //           a class, but real rules sometimes do (e.g. JSON-
        //           shaped literals like `{"key":`). Requiring the
        //           digit lookahead keeps the algorithm well-defined
        //           on real inputs.
        // TS map:   `const isQuant = c === 0x2a || c === 0x2b || c === 0x3f || (c === 0x7b && isDigit(b[i+1]));`.
        let is_quant_start = matches!(c, b'*' | b'+' | b'?')
            || (c == b'{'
                && i + 1 < bytes.len()
                && bytes[i + 1].is_ascii_digit());
        if is_quant_start {
            if just_consumed_quant {
                return Some(format!(
                    "stacked quantifier at byte offset {}: `{}` follows another quantifier suffix. Stacked quantifiers (e.g. `a**`, `\\D{{5,11}}{{5,11}}`, `(?:a){{2}}{{3}}`) cause NFA size explosion in the `regex` crate (compile reaches the 256 MB DFA limit before erroring) and are rejected outright by resharp's parser. Group the inner quantified atom and apply at most one quantifier per level, or rewrite to a single bounded-or-unbounded repetition.",
                    i, c as char
                ));
            }
            // What:     Consume the quantifier. For `{N}` / `{N,}` /
            //           `{N,M}` we walk to the matching `}`; for the
            //           single-byte `*`/`+`/`?` we just advance one.
            //           Then absorb an optional lazy `?` or possessive
            //           `+` modifier so `a*?` is NOT counted as two
            //           stacked quantifiers (it is one lazy `*`).
            // Why:      The lazy/possessive modifier is part of the
            //           same quantifier in regex syntax. Treating it
            //           as a separate quantifier would false-positive
            //           every lazy quantifier in the corpus.
            // TS map:   `if (c === 0x7b) { while (i < len && b[i] !== 0x7d) i++; if (i < len) i++; } else { i++; }`.
            if c == b'{' {
                while i < bytes.len() && bytes[i] != b'}' {
                    i += 1;
                }
                if i < bytes.len() {
                    i += 1;
                }
            } else {
                i += 1;
            }
            // What:     Optional `?` lazy modifier suffix. The regex
            //           crate does NOT support possessive `+`
            //           modifier (e.g. `a*+`, `a++`); the trailing
            //           `+` always represents a fresh quantifier on
            //           the previously-quantified atom and is
            //           therefore stacked.
            // Why:      `a*?` is a complete lazy quantifier; the `?`
            //           is part of it. A SECOND `?` (so `a*??`) is
            //           the stacked case, caught on the next
            //           iteration. `a*+` is treated as `*` followed
            //           by stacked `+`, the same way the regex crate
            //           parses it.
            if i < bytes.len() && bytes[i] == b'?' {
                i += 1;
            }
            just_consumed_quant = true;
            continue;
        }
        // What:     Any other byte (atom, anchor, `(`, `)`, `|`, ...)
        //           resets `just_consumed_quant` to `false`. A
        //           quantifier may follow this byte but the byte
        //           itself is not a quantifier suffix.
        // Why:      Re-anchor the state machine to "atom may now be
        //           quantified". `(a)*` is fine: `(` starts a group,
        //           `)` closes it (group becomes an atom), `*`
        //           quantifies the group; only ONE quantifier follows
        //           the group close.
        i += 1;
        just_consumed_quant = false;
    }
    None
}

// What:     `pub fn complement_intersection_quantified_group(src: &str) -> Option<String>`
//           detects rule shapes that cause resharp's algebra
//           simplification to hang for tens of seconds or
//           indefinitely. The name is historical -- the original
//           bisect targeted `<prefix>~(\w)&(?:aaa)*`, but a second
//           fuzz timeout
//           (`timeout-0815a95346bfa16ae0c6454162d9af0b8c05779c`,
//           shape `(?i) ###(?:\s&üü)(?:####)+#@...`) showed the
//           hang also fires WITHOUT a `~(` complement -- bare
//           intersection (`&`) co-occurring with a quantified
//           group (`)*`/`)+`/etc.) is sufficient. The detector now
//           checks only those two co-occurrence triggers; the
//           complement check is omitted. The original bisect
//           reproducer from
//           `timeout-00179d433e26fbcc3bedf2b7b38b6ce1ff9e6438` was
//           `abc~(\w)&(?:aaa)*`. The compile call to resharp's
//           `Regex::new` does not terminate within libFuzzer's
//           10s per-input timeout.
//
//           Shapes confirmed to hang (>5s each via probe bisection):
//             - `abc~(\w)&(?:aaa)*`
//             - `xyz~(\w)&(?:aaaaaaaaaaaaa)*`
//             - `[_]ñe-XM1[^42v]~(\w)&(?:aaaaaaaaaaaaa)*` (original artifact)
//             - `(?:[^a]~(\w)&(?:aaaaaaaaaaaaa)*)` (with negated-class prefix)
//
//           Shapes that compile in milliseconds:
//             - `~(\w)&(?:a)*` (no prefix)
//             - `abc~(\w)&def` (no quantified group)
//             - `(?:abc~(\w)&(?:aaa)*)` (wrapped in single outer group)
//             - `x~(\w)&(?:a)*` (1-char prefix and 1-char quantified body)
//
//           Root cause traced via gdb-attach to a hung probe plus
//           reading the cloned resharp source: the hang is in
//           `resharp::prefix::calc_prefix_sets_inner` at
//           `resharp-engine/src/prefix.rs:27`. The function walks a
//           chain of regex derivatives in a `loop { ... }` searching
//           for a fixed prefix, but its `redundant` set only ever
//           holds the original `start` node and `NodeId::BOT` --
//           freshly visited nodes are never added back. For the
//           trigger shape `<prefix>~(\w)&(?:aaa)*`, the derivative
//           chain produces a sequence of unique single-target
//           nodes indefinitely (each `der` step rotates the
//           star+intersection state but never lands back on `start`
//           or `BOT`), so the loop runs without bound. The shape
//           `(?:<prefix>~(\w)&(?:aaa)*)` wrapped in a group skips
//           the slow path because the outer non-capturing group
//           changes the simplified node form that enters
//           `select_prefix`, making the derivative chain visit
//           `BOT` early. Filed upstream: see TROUBLESHOOTING.resharp.md
//           for the issue link.
//
//           The pre-validator uses a coarse structural heuristic:
//           "source contains a complement (`~(`), intersection
//           (`&`), AND a quantified group (`)*`, `)+`, `)?`,
//           `){...}`) outside character classes". This is
//           conservative: it would flag the OK cases above too.
//           That trade-off is acceptable because:
//             1. The production rule corpus contains zero rules
//                with both `&` and `~(` (intersection requires
//                resharp set-algebra, which is virtually never
//                authored by humans; the only `&` in the example
//                betterleaks config is inside character classes or
//                as `&amp;` HTML-escape literals).
//             2. The fuzz target only authors these shapes
//                accidentally via the structured generator; it
//                never needs them to discover the real bugs.
//             3. The cost of a false positive is "skip this rule
//                and continue"; the cost of a missed hang is
//                "libFuzzer reports a timeout and halts the run",
//                which blocks progress entirely.
// Why:      catch_unwind protects against panics but not against
//           non-termination. resharp does not expose a compile
//           timeout, and we can't safely cancel a running compile
//           from outside. The pre-validator is the only safe way
//           to skip the slow shapes without modifying resharp.
//           Upstream fix would be adding `redundant.insert(node);`
//           on each loop iteration in `calc_prefix_sets_inner`
//           so the visited-set actually accumulates across the walk.
// TS map:   `function complementIntersectionQuantifiedGroup(src: string): string | null`.
//
// In TS you'd write (pseudocode):
// ```ts
// function complementIntersectionQuantifiedGroup(src: string): string | null {
//   // walk bytes outside character classes; set hasComplement on `~(`,
//   // set hasIntersection on `&`, set hasQuantifiedGroup on `)` followed
//   // by `*`/`+`/`?`/`{N`. Return reason when all three are present.
// }
// ```
pub fn complement_intersection_quantified_group(src: &str) -> Option<String> {
    let bytes = src.as_bytes();
    let mut i = 0usize;
    let mut in_class = false;
    let mut has_intersection = false;
    let mut has_quantified_group = false;
    while i < bytes.len() {
        let c = bytes[i];
        if c == b'\\' {
            i += 2;
            continue;
        }
        if !in_class && c == b'[' {
            in_class = true;
            i += 1;
            continue;
        }
        if in_class && c == b']' {
            in_class = false;
            i += 1;
            continue;
        }
        if !in_class {
            if c == b'&' {
                has_intersection = true;
            }
            // What:     `)` followed by `*`/`+`/`?`/`{N` is a
            //           quantified group close. Same recognition
            //           rule used by `nested_grouped_quantifier`.
            // Why:      The trigger shape needs the quantified
            //           group; bare `)` not followed by a
            //           quantifier doesn't reproduce the hang.
            if c == b')' && i + 1 < bytes.len() {
                let next = bytes[i + 1];
                if matches!(next, b'*' | b'+' | b'?')
                    || (next == b'{'
                        && i + 2 < bytes.len()
                        && bytes[i + 2].is_ascii_digit())
                {
                    has_quantified_group = true;
                }
            }
            if has_intersection && has_quantified_group {
                return Some(format!(
                    "intersection (`&`) co-occurring with a quantified group (`(...)`*/+/?/{{N}}) triggers a known resharp 0.5.x through 0.6.x prefix-loop non-termination during `Regex::new` (see TROUBLESHOOTING.resharp.md Bug E -- `calc_prefix_sets_inner` lacks a visited-set update on each iteration). The compile does not terminate within libFuzzer's per-input timeout. Reproducers: `abc~(\\w)&(?:aaa)*` and `(?i) ###(?:\\s&üü)(?:####)+...`. Rewrite the rule to inline the quantified group's body into the intersection operand, or remove one of the two operators. {}",
                    TROUBLESHOOT_REF
                ));
            }
        }
        i += 1;
    }
    None
}

// What:     `pub fn nested_lookahead_in_quantified_group(src: &str) -> Option<String>`
//           detects rule shapes that trigger a `attempt to add with
//           overflow` panic at `resharp-algebra/src/lib.rs:2470`
//           during `Regex::new`. The panicked addition is
//           `tail_rel + la_rel` where both operands are `u32`; the
//           inner `tail_rel` saturates to `u32::MAX` on the previous
//           lookahead-chain step, and the next add overflows under
//           debug assertions. cargo-fuzz keeps debug-assertions on
//           even in `--release`, so the fuzz target observes the
//           panic that production (built with debug-assertions OFF)
//           would silently wrap to 0.
//
//           Shapes confirmed to PANIC (bisected via probe binaries
//           in `/tmp/probe-slow-unit/src/bin/bisect_f*.rs`):
//             - `(?:(?:(?!\?)){1,5}){2,4}` (base case)
//             - `(?:(?:(?!\?)){1,5}){2,2}` (outer min=max=2)
//             - `(?:(?!\?){1,2}){3}` (single wrap, outer min=3)
//             - `(?:(?:(?:(?!\?)){1,5}){1,3}){2,4}` (triple nest)
//             - `(?:(?:(?:(?!\?)){1,5}){2,3}){1,4}` (middle min=2)
//             - `(?:(?=a)(?:(?!\?)){1,5}){2,4}` (sibling lookahead)
//             - positive lookahead `(?=...)` also panics
//
//           Shapes that compile OK (no panic):
//             - `(?:(?:(?!\?)){1,5}){1,5}` (outer min=1)
//             - `(?:(?!\?)){2,4}` (single wrap, no inner quant)
//             - `(?:a(?:(?!\?)){1,5}){2,4}` (literal sibling content)
//             - `(?:(?:(?!\?)){1,5}a){2,4}` (literal trailing)
//             - `(?:(?:(?!\?)){1,5}|a){2,4}` (alternation sibling)
//             - lookbehinds `(?<=...)`/`(?<!...)` -- resharp returns
//               UnsupportedPattern before reaching the overflow path
//
//           Detection criterion (single-pass byte walker with a
//           paren-frame stack):
//             - At each `)`, parse the following quantifier (if any)
//               and read its minimum repetition count. For `{n,m}` /
//               `{n}` use `n`; for `*` use 0; for `+` use 1; for `?`
//               use 0.
//             - Maintain a Frame per open group tracking:
//               (a) is_lookahead_self -- this group opened with `(?!`
//                   or `(?=`;
//               (b) has_lookahead_subtree -- a descendant frame was a
//                   lookahead (propagated up on close);
//               (c) has_inner_quant_group -- a child group was
//                   quantified (propagated up on close);
//               (d) has_non_group_atom -- a literal / escape / class /
//                   anchor byte appeared AT THIS DEPTH;
//               (e) has_alternation -- a `|` appeared at this depth.
//             - On close, fire if ALL hold:
//                 quantifier present AND min >= 2 AND
//                 (is_lookahead_self OR has_lookahead_subtree) AND
//                 has_inner_quant_group AND
//                 !has_non_group_atom AND !has_alternation.
//             - The `!has_non_group_atom` and `!has_alternation`
//               clauses prevent false positives on the literal-sibling
//               and alternation shapes that resharp handles fine: a
//               concrete sibling breaks the lookahead derivative
//               chain so the `rel` never saturates.
// Why:      Without this pre-validator the panic propagates through
//           `catch_unwind` (libfuzzer's panic hook calls `abort`
//           before our catch_unwind can intercept), libFuzzer
//           records a deadly-signal crash, and the fuzz run halts
//           before reaching the soundness-by-revert phase 11. The
//           fuzz target ran into two such crashes in the worktree:
//           `crash-06d9dd9fa1abfeec72a8154c09434b237dfc7f38` and
//           `crash-df95fcd52de76d952ee3db291f59434ece2c0b81`. Both
//           decode to a quantified group containing a lookahead
//           inside another quantified group, with the outer
//           quantifier's min >= 2. Filed upstream: see
//           TROUBLESHOOTING.resharp.md Bug F for the issue link.
// TS map:   `function nestedLookaheadInQuantifiedGroup(src: string): string | null`.
//
// In TS you'd write (pseudocode):
// ```ts
// function nestedLookaheadInQuantifiedGroup(src: string): string | null {
//   // walk bytes; maintain a stack of frames per open paren;
//   // mark is_lookahead_self when the open is `(?!` / `(?=`;
//   // on close, parse the trailing quantifier's min;
//   // fire if quant min >= 2 and the frame had a lookahead in subtree
//   // and a quantified child group and no atoms / alternations at depth.
// }
// ```
pub fn nested_lookahead_in_quantified_group(src: &str) -> Option<String> {
    let bytes = src.as_bytes();
    let mut i = 0usize;
    let mut in_class = false;
    // What:     `struct Frame { ... }`. One per open group; pushed at
    //           `(`, popped at `)`. Tracks the structural facts needed
    //           to decide whether the group's quantifier completes a
    //           Bug F shape.
    // Why:      Bug F requires a SPECIFIC nested structure -- a
    //           single-element body that is itself a quantified group
    //           containing a lookahead. Per-frame flags let us reject
    //           shapes that look superficially similar but have
    //           sibling literals / alternations that break the chain.
    #[derive(Default, Clone)]
    struct Frame {
        is_lookahead_self: bool,
        has_lookahead_subtree: bool,
        has_inner_quant_group: bool,
        has_non_group_atom: bool,
        has_alternation: bool,
    }
    let mut stack: Vec<Frame> = Vec::new();
    while i < bytes.len() {
        let c = bytes[i];
        // What:     Escape `\X`: skip both bytes; the escape is one atom
        //           AT THIS DEPTH so the parent frame records "has
        //           non-group atom" -- this rules out the sibling-
        //           literal false positive `(?:\a(?:(?!\?)){1,5}){2,4}`.
        // Why:      `\)` would otherwise be misread as a close;
        //           `\(` would be misread as an open; either would
        //           corrupt the frame stack.
        if c == b'\\' {
            if let Some(top) = stack.last_mut() {
                top.has_non_group_atom = true;
            }
            i += 2;
            continue;
        }
        // What:     `[`: enter character class. The class is an atom at
        //           this depth.
        // Why:      `[)]` shouldn't pop a frame; bytes inside `[...]`
        //           are literal.
        if !in_class && c == b'[' {
            if let Some(top) = stack.last_mut() {
                top.has_non_group_atom = true;
            }
            in_class = true;
            i += 1;
            continue;
        }
        if in_class {
            if c == b']' {
                in_class = false;
            }
            i += 1;
            continue;
        }
        // What:     `(`: open a new frame. Detect `(?!` or `(?=` lookahead
        //           openers (lookbehind `(?<!` / `(?<=` are also detected
        //           but do not set is_lookahead_self -- they don't reach
        //           the overflow path; resharp returns UnsupportedPattern
        //           on lookbehind plus a quantifier). The group prefix
        //           bytes `?...` are skipped here so the body walk
        //           doesn't misread `?` as a quantifier or `:` / `=` /
        //           `!` as alternation / atom.
        if c == b'(' {
            let is_lookahead = i + 2 < bytes.len()
                && bytes[i + 1] == b'?'
                && (bytes[i + 2] == b'!' || bytes[i + 2] == b'=');
            let frame = Frame {
                is_lookahead_self: is_lookahead,
                ..Frame::default()
            };
            stack.push(frame);
            i += 1;
            if i < bytes.len() && bytes[i] == b'?' {
                i += 1;
                if i < bytes.len()
                    && matches!(bytes[i], b':' | b'!' | b'=' | b'<' | b'P' | b'i' | b'x' | b'u' | b'-' | b's' | b'm' | b'a' | b'R')
                {
                    // What:     Consume the group prefix character (e.g.
                    //           `:` in `(?:`, `!` in `(?!`, `=` in
                    //           `(?=`, `<` in `(?<!` / `(?<=` / `(?<name>`,
                    //           flag chars in `(?i)`/`(?x:`/etc., `-`
                    //           in `(?-i:`).
                    // Why:      Without this skip the body walk would
                    //           interpret these bytes as literals,
                    //           incorrectly setting has_non_group_atom.
                    i += 1;
                    // What:     Lookbehind `(?<=` / `(?<!` needs one
                    //           more byte consumed.
                    if bytes[i - 1] == b'<'
                        && i < bytes.len()
                        && (bytes[i] == b'=' || bytes[i] == b'!')
                    {
                        i += 1;
                    }
                }
            }
            continue;
        }
        // What:     `)`: close the current frame. Parse the trailing
        //           quantifier (if any); decide whether to fire; then
        //           propagate transient flags to the parent.
        // Why:      The fire check sees the COMPLETE subtree state for
        //           this group, before propagation collapses it into
        //           the parent.
        if c == b')' {
            i += 1;
            let frame = stack.pop().unwrap_or_default();
            let mut quant_min: u32 = 0;
            let mut is_quantified = false;
            if i < bytes.len() {
                match bytes[i] {
                    b'*' => {
                        quant_min = 0;
                        is_quantified = true;
                        i += 1;
                    }
                    b'+' => {
                        quant_min = 1;
                        is_quantified = true;
                        i += 1;
                    }
                    b'?' => {
                        quant_min = 0;
                        is_quantified = true;
                        i += 1;
                    }
                    b'{' => {
                        let mut j = i + 1;
                        let num_start = j;
                        while j < bytes.len() && bytes[j].is_ascii_digit() {
                            j += 1;
                        }
                        if j > num_start {
                            let num_str = std::str::from_utf8(&bytes[num_start..j]).unwrap_or("0");
                            quant_min = num_str.parse().unwrap_or(0);
                            while j < bytes.len() && bytes[j] != b'}' {
                                j += 1;
                            }
                            if j < bytes.len() {
                                is_quantified = true;
                                j += 1;
                            }
                        }
                        i = j;
                    }
                    _ => {}
                }
                if is_quantified && i < bytes.len() && bytes[i] == b'?' {
                    i += 1;
                }
            }

            let has_la_in_subtree = frame.is_lookahead_self || frame.has_lookahead_subtree;

            if is_quantified
                && quant_min >= 2
                && has_la_in_subtree
                && frame.has_inner_quant_group
                && !frame.has_non_group_atom
                && !frame.has_alternation
            {
                return Some(format!(
                    "nested lookahead inside a quantified group with outer quantifier min >= 2 (e.g. `(?:(?:(?!X)){{1,5}}){{2,4}}`) triggers a known resharp 0.5.x through 0.6.x algebra-overflow panic during `Regex::new` (see TROUBLESHOOTING.resharp.md Bug F -- `attempt to add with overflow` at `resharp-algebra/src/lib.rs:2470`; the lookahead-chain `rel` length saturates to u32::MAX and the next add overflows under debug assertions). Production builds without debug-assertions silently wrap to 0 (likely producing wrong matches). Reproducers: `(?:(?:(?!\\?)){{1,5}}){{2,4}}` and `(?:(?!\\?){{1,2}}){{3}}`. Either lower the outer quantifier's min below 2, drop the inner quantifier wrap, or insert a literal / alt sibling alongside the lookahead-bearing inner group. {}",
                    TROUBLESHOOT_REF
                ));
            }

            if let Some(parent) = stack.last_mut() {
                if has_la_in_subtree {
                    parent.has_lookahead_subtree = true;
                }
                if is_quantified || frame.has_inner_quant_group {
                    parent.has_inner_quant_group = true;
                }
            }
            continue;
        }
        // What:     `|` at this depth: top-level alternation. Marks the
        //           frame so the fire condition can rule out sibling-
        //           alt false positives like
        //           `(?:(?:(?!\?)){1,5}|a){2,4}`.
        if c == b'|' {
            if let Some(top) = stack.last_mut() {
                top.has_alternation = true;
            }
            i += 1;
            continue;
        }
        // What:     Any other byte (literal, anchor, dot, etc.) is an
        //           atom at the current depth. The flag rules out the
        //           sibling-literal false positives.
        if let Some(top) = stack.last_mut() {
            top.has_non_group_atom = true;
        }
        i += 1;
    }
    None
}

// What:     `pub fn quantified_lookahead_with_sibling_content(src: &str) -> Option<String>`
//           detects a second Bug F shape that the narrower
//           `nested_lookahead_in_quantified_group` misses: a
//           variable-bound-quantified lookahead-bearing group
//           followed by exactly 1 or 2 trailing atoms at parent
//           depth. Bisected from a fuzz crash where
//           `crash-a219859099426658d70e90bc97f560b85f2cf256` decoded
//           to `... (?:(?!ñññAtsöéaañ)){4,12}~(ññM aaaaaaaa)aaaaaa`
//           and minimised to `(?:(?!abc)){4,12}a`.
//
//           PANIC shapes (probes in
//           `/tmp/probe-slow-unit/src/bin/bisect_f{7,8}.rs`):
//             - `(?:(?!abc)){4,12}a` (variable quant + 1 trailing atom)
//             - `(?:(?!abc)){4,12}aa` (variable quant + 2 trailing atoms)
//             - `(?:(?!abc)){4,12}\?` (escape trail counts as 1 atom)
//             - `(?:(?!abc)){4,12}(?:d)` (group trail counts as 1 atom)
//             - `(?:(?!abc)){4,12}[d]` (class trail counts as 1 atom)
//             - `(?:(?!abc)){2,3}a` / `{4,5}a` / `{1,4}a` (any variable bound)
//             - `(?!abc){4,12}a` (bare lookahead, no `(?:)` wrap)
//             - `a(?:(?!abc)){4,12}a` (leading content does not save it)
//
//           OK shapes (validator does NOT fire -- they compile cleanly
//           upstream):
//             - `(?:(?!abc)){4,12}` alone (no trailing)
//             - `a(?:(?!abc)){4,12}` (leading only, no trailing)
//             - `(?:(?!abc)){4,12}aaa` and longer (3+ trailing atoms
//               break the upstream chain -- the derivative builder
//               consumes them before reaching the saturating add)
//             - `(?:(?!abc)){2}a` / `{3}a` / `{4}a` (EXACT quant `{n}`
//               -- no variable bound, no overflow accumulation)
//             - `(?:(?!abc)){4,12}\d{3,5}` (the 3+ trailing rule
//               applies if the count >= 3; trailing groups count as
//               1 atom each regardless of inner repetition)
//
//           Detection rule (precise, derived from bisect data, no
//           accepted false positives):
//             - On `)` close, parse the trailing quantifier and
//               record whether the bound is VARIABLE (`{m,n}` with m
//               < n, including `*`/`+`/`?` which are `{0,inf}` /
//               `{1,inf}` / `{0,1}`). EXACT `{n}` quants do not arm.
//             - If the closing frame had a lookahead in its subtree
//               AND the quantifier is variable, set the parent's
//               `pending_trailing_count` to 0 (armed, counting).
//             - Each subsequent atom at parent depth (literal byte,
//               escape `\X`, char class `[...]`, group `(...)`,
//               anchor) increments `pending_trailing_count`.
//             - Fire when the count is 1 or 2 AND we reach a
//               structural boundary that confirms the trailing
//               window is closed (end-of-regex, `|`, or the parent
//               `)`).
//             - If the count reaches 3 before any boundary, disarm
//               (the upstream chain breaks at 3+ atoms).
//             - `|` and the parent close also disarm if we hadn't
//               fired yet, OR fire if the trailing count is 1-2 at
//               that boundary.
//
//           This precise rule has no accepted false positives:
//           rejecting only shapes that actually panic upstream.
//           Earlier broader widening (which also rejected
//           `(?:(?!X)){n}<atom>` exact-quant and
//           `(?:(?!X)){m,n}aaa` long-trail shapes) was tightened
//           after the user flagged that production rules might
//           legitimately use those shapes. See
//           HANDOVER.forbidden-strings-fuzzing.md.
// Why:      `nested_lookahead_in_quantified_group` only catches the
//           DOUBLE-quantified nesting (`(?:(?:(?!X)){m,n}){p,q}`). The
//           SINGLE-quantified-with-1-or-2-trailing shape reaches the
//           same overflow path in `resharp-algebra/src/lib.rs:2470`
//           but through a different upstream code path -- the
//           trailing content feeds into the lookahead-chain
//           derivative without an intermediate `Quant` wrap, and the
//           `tail_rel` saturates identically. Without this
//           pre-validator the fuzz target catches the panic via
//           `catch_unwind` only on lucky builds; libfuzzer's panic
//           hook calls `abort` first on most, halting the run.
// TS map:   `function quantifiedLookaheadWithSiblingContent(src: string): string | null`.
//
// In TS you'd write (pseudocode):
// ```ts
// function quantifiedLookaheadWithSiblingContent(src: string): string | null {
//   // walk bytes; per-frame `pendingTrailingCount` -1 = unarmed,
//   // 0+ = counting; arm on close of a quantified-LA group iff the
//   // quant is variable-bound; increment per atom at parent depth;
//   // fire on boundary (eof / `|` / parent `)`) if count is 1 or 2;
//   // disarm if count reaches 3.
// }
// ```
pub fn quantified_lookahead_with_sibling_content(src: &str) -> Option<String> {
    let bytes = src.as_bytes();
    let mut i = 0usize;
    let mut in_class = false;
    // What:     `struct Frame { ... }`. One per open group; pushed at
    //           `(`, popped at `)`. The top entry represents the
    //           implicit top-level frame so trailing content at the
    //           regex root also fires.
    // Why:      `pending_trailing_count` is the per-frame "armed and
    //           counting" cursor. `-1` means unarmed; `0+` means
    //           counting trailing atoms after a quantified-LA close.
    //           Tracking it per-frame supports nested usage like
    //           `((?:(?!X)){4,12}a)` (the outer frame counts the
    //           trailing `a` if the parent itself is armed).
    #[derive(Default, Clone)]
    struct Frame {
        is_lookahead_self: bool,
        has_lookahead_subtree: bool,
        pending_trailing_count: i32,
    }
    let mut stack: Vec<Frame> = vec![Frame {
        is_lookahead_self: false,
        has_lookahead_subtree: false,
        pending_trailing_count: -1,
    }];
    // What:     `fire_reason()` returns the diagnostic string. Wrapped
    //           in a helper because three call sites (escape, class,
    //           group-open, alternation-boundary, eof-boundary,
    //           parent-close-boundary, literal-byte) all emit the
    //           same payload.
    // Why:      Keep the rejection message centralised so a future
    //           tweak doesn't drift across call sites.
    fn fire_reason() -> String {
        format!(
            "quantified lookahead-bearing group with variable bound `{{m,n}}` and 1-2 trailing atoms at parent depth (e.g. `(?:(?!X)){{m,n}}<atom>` or `(?:(?!X)){{m,n}}<atom><atom>`) triggers a resharp 0.5.x through 0.6.x `attempt to add with overflow` panic at `resharp-algebra/src/lib.rs:2470` (see TROUBLESHOOTING.resharp.md Bug F). The lookahead-chain `rel` saturates to u32::MAX and the next add overflows under debug assertions; production builds without debug-assertions silently wrap to 0 (likely producing wrong matches). Reproducer: `(?:(?!abc)){{4,12}}a`. Workarounds: (a) extend the trailing content to 3 or more atoms (`...{{4,12}}aaa`), (b) use an EXACT quantifier `(?:(?!X)){{n}}<atom>` (single-bound), or (c) split the regex. {}",
            TROUBLESHOOT_REF
        )
    }
    while i < bytes.len() {
        let c = bytes[i];
        // What:     Inside a character class: consume literally until
        //           `]`. The class as a whole was already counted as
        //           one atom when `[` was seen.
        if in_class {
            if c == b']' {
                in_class = false;
            }
            i += 1;
            continue;
        }
        // What:     `\X`: escape. One atom at the current depth.
        if c == b'\\' {
            if let Some(top) = stack.last_mut() {
                if top.pending_trailing_count >= 0 {
                    top.pending_trailing_count += 1;
                    if top.pending_trailing_count >= 3 {
                        top.pending_trailing_count = -1;
                    }
                }
            }
            i += 2;
            continue;
        }
        // What:     `[`: character class open. One atom; the class
        //           body is consumed in the `in_class` branch.
        if c == b'[' {
            if let Some(top) = stack.last_mut() {
                if top.pending_trailing_count >= 0 {
                    top.pending_trailing_count += 1;
                    if top.pending_trailing_count >= 3 {
                        top.pending_trailing_count = -1;
                    }
                }
            }
            in_class = true;
            i += 1;
            continue;
        }
        // What:     `(`: group open. Counts as one atom at parent
        //           depth (regardless of what's inside; the group is
        //           atomic from the trailing-count perspective).
        if c == b'(' {
            if let Some(top) = stack.last_mut() {
                if top.pending_trailing_count >= 0 {
                    top.pending_trailing_count += 1;
                    if top.pending_trailing_count >= 3 {
                        top.pending_trailing_count = -1;
                    }
                }
            }
            let is_lookahead = i + 2 < bytes.len()
                && bytes[i + 1] == b'?'
                && (bytes[i + 2] == b'!' || bytes[i + 2] == b'=');
            stack.push(Frame {
                is_lookahead_self: is_lookahead,
                has_lookahead_subtree: false,
                pending_trailing_count: -1,
            });
            i += 1;
            if i < bytes.len() && bytes[i] == b'?' {
                i += 1;
                if i < bytes.len()
                    && matches!(bytes[i], b':' | b'!' | b'=' | b'<' | b'P' | b'i' | b'x' | b'u' | b'-' | b's' | b'm' | b'a' | b'R')
                {
                    i += 1;
                    if bytes[i - 1] == b'<'
                        && i < bytes.len()
                        && (bytes[i] == b'=' || bytes[i] == b'!')
                    {
                        i += 1;
                    }
                }
            }
            continue;
        }
        // What:     `)`: group close. Parse the trailing quantifier;
        //           classify it as exact `{n}`, variable `{m,n}`,
        //           `*`/`+`/`?`, or unquantified. If the closing
        //           frame had a lookahead in its subtree AND the
        //           quantifier is variable (i.e. unbounded or bound
        //           with m < n), arm the parent's
        //           `pending_trailing_count` to 0. Before doing so,
        //           check the parent's pre-existing pending count: a
        //           close at parent depth is also a structural
        //           boundary that fires if 1-2 atoms had accumulated.
        if c == b')' {
            i += 1;
            let frame = stack.pop().unwrap_or_default();
            // What:     `(is_quantified, is_variable_bound)`. The
            //           tuple captures both whether ANY quantifier
            //           follows (so we know how to arm the parent)
            //           and whether the bound is variable
            //           (only variable bounds trigger Bug F).
            let mut is_quantified = false;
            let mut is_variable_bound = false;
            if i < bytes.len() {
                match bytes[i] {
                    b'*' | b'+' | b'?' => {
                        is_quantified = true;
                        // `*` = `{0,inf}`, `+` = `{1,inf}`, `?` =
                        // `{0,1}` -- all variable bounds.
                        is_variable_bound = true;
                        i += 1;
                    }
                    b'{' => {
                        let mut j = i + 1;
                        let min_start = j;
                        while j < bytes.len() && bytes[j].is_ascii_digit() {
                            j += 1;
                        }
                        let min_end = j;
                        let min_val: u32 = if min_end > min_start {
                            std::str::from_utf8(&bytes[min_start..min_end])
                                .unwrap_or("0")
                                .parse()
                                .unwrap_or(0)
                        } else {
                            0
                        };
                        let mut max_val: u32 = min_val;
                        let mut saw_comma = false;
                        if j < bytes.len() && bytes[j] == b',' {
                            saw_comma = true;
                            j += 1;
                            let max_start = j;
                            while j < bytes.len() && bytes[j].is_ascii_digit() {
                                j += 1;
                            }
                            if j > max_start {
                                max_val = std::str::from_utf8(&bytes[max_start..j])
                                    .unwrap_or("0")
                                    .parse()
                                    .unwrap_or(min_val);
                            } else {
                                // `{m,}` = open upper bound = variable.
                                max_val = u32::MAX;
                            }
                        }
                        if j < bytes.len() && bytes[j] == b'}' {
                            is_quantified = true;
                            // Variable iff `{m,...}` with m < max OR `{m,}`.
                            is_variable_bound = saw_comma && (max_val > min_val);
                            j += 1;
                        }
                        i = j;
                    }
                    _ => {}
                }
                if is_quantified && i < bytes.len() && bytes[i] == b'?' {
                    i += 1;
                }
            }
            let has_la_in_subtree = frame.is_lookahead_self || frame.has_lookahead_subtree;
            // What:     A close at parent depth is a structural
            //           boundary. If the parent was already armed and
            //           accumulated 1-2 trailing atoms (this group
            //           being one of them, already counted via `(`),
            //           fire now.
            // Why:      `(?:(?!abc)){4,12}(?:d)` -- the `(?:d)` open
            //           already incremented the count to 1; the
            //           matching close is the boundary at which we
            //           can confirm the trailing window. But here we
            //           want to keep counting -- the close is at
            //           parent depth and only ends THIS sibling
            //           group's contribution, not the trailing
            //           window. So no firing at close-only.
            if let Some(parent) = stack.last_mut() {
                if has_la_in_subtree {
                    parent.has_lookahead_subtree = true;
                }
                if is_quantified && is_variable_bound && has_la_in_subtree {
                    // What:     Arm the parent. Set count to 0 (zero
                    //           trailing atoms seen yet).
                    parent.pending_trailing_count = 0;
                }
            }
            continue;
        }
        // What:     `|`: alternation. Structural boundary for the
        //           current frame. If we have a pending count of 1-2,
        //           fire; otherwise disarm.
        if c == b'|' {
            if let Some(top) = stack.last_mut() {
                if (1..=2).contains(&top.pending_trailing_count) {
                    return Some(fire_reason());
                }
                top.pending_trailing_count = -1;
            }
            i += 1;
            continue;
        }
        // What:     Stray `*`/`+`/`?`/`{` outside the close-quantifier
        //           handler: malformed input, treat as no-op (not a
        //           trailing atom). Skip without incrementing.
        if matches!(c, b'*' | b'+' | b'?' | b'{') {
            i += 1;
            continue;
        }
        // What:     Any other byte: literal, anchor, dot, etc. One
        //           atom at the current depth.
        if let Some(top) = stack.last_mut() {
            if top.pending_trailing_count >= 0 {
                top.pending_trailing_count += 1;
                if top.pending_trailing_count >= 3 {
                    top.pending_trailing_count = -1;
                }
            }
        }
        i += 1;
    }
    // What:     End-of-regex boundary. Check the top-level (and any
    //           remaining open frames) for pending 1-2 counts.
    // Why:      `(?:(?!abc)){4,12}a` at the top level -- the
    //           top-level frame was armed by the close, the trailing
    //           `a` brought count to 1, and there's no further
    //           boundary; eof is the boundary that fires.
    for frame in stack.iter().rev() {
        if (1..=2).contains(&frame.pending_trailing_count) {
            return Some(fire_reason());
        }
    }
    None
}

// What:     `pub fn nested_grouped_quantifier(src: &str) -> Option<String>`
//           detects regex source containing four or more consecutive
//           `)`+quantifier adjacencies. The shape the fuzz target
//           actually emits looks like
//           `(?:(?:(?:(?:(?:\d){5,11}){5,11}){5,11}){5,11}){5,11}` --
//           five `(?:` opens, an inner atom, then five `){5,11}`
//           close+quantifier pairs back-to-back. Sibling shape
//           `(?:(?:(?:(?:(?:\d)*)*)*)*)*` is the same with `*`
//           instead of `{5,11}`. The `Node::Quant` renderer at
//           `fuzz/src/generators.rs:1292` ALWAYS wraps a quantified
//           atom in `(?:...)`, so the fuzz generator can never emit
//           the bare-stacked shape `stacked_quantifier` was written
//           for -- the grouped form is the actual blowup vector.
// Why:      Each `{N,M}` (or `*`/`+`/`?`) on a group multiplies the
//           inner NFA's state count by up to M (or by a large but
//           bounded factor for `*`/`+`). Five-deep multiplication
//           reaches `11^5 = 161051` repetitions of the inner atom;
//           combined with `(?u)` widening `\d` to the full Unicode
//           decimal-digit class (~700 codepoint sub-states), the
//           regex crate's NFA construction exceeds the 256 MB
//           DFA size limit and takes ~3 s of wall to error with
//           `CompiledTooBig`. Under ASAN in the fuzz target, the
//           single iteration crosses libFuzzer's `report_slow_units`
//           threshold of 10 s and halts the run before the
//           interesting (?u)-Unicode shapes for the e49d8694
//           case-fold soundness bug can be discovered. Rejecting
//           depth-4 chains at the pre-validator level rejects the
//           shape in microseconds with a clear message, keeping
//           the fuzz target moving.
//
//           Threshold: depth 4 (= chain of four `){quant}` pairs).
//           Empirically depth 3 still compiles in milliseconds even
//           under Unicode `\D`/`\d`; depth 4 starts to noticeably
//           bloat NFA construction; depth 5+ reliably wall-clocks.
//           Catching at 4 is conservative enough to spare any
//           plausibly-authored rule (real secret-detection rules
//           seldom nest beyond 2 levels of quantified groups) while
//           covering both halves of the slow-unit source (chain=5
//           for each half; flags as soon as chain reaches 4).
//
//           Detection runs as a single-pass byte walker over `src`:
//             - Escape pairs `\X` and class interiors `[...]` are
//               skipped (escapes are atoms; class bytes are literal
//               and quantifiers don't apply structurally there).
//             - Any `(` resets the chain (a new group opening breaks
//               a `)`+quant chain). The optional `?` after `(` for
//               `(?...)` group prefixes (non-capturing `(?:`, flag
//               groups `(?i)`, lookarounds `(?=`/`(?!`/`(?<=`/`(?<!`,
//               named captures `(?P<...>`/`(?<...>`, comments `(?#...)`)
//               is skipped so it isn't misread as a `?` quantifier.
//             - On `)`, peek the next byte: if it is a quantifier
//               start (`*`/`+`/`?`/`{` followed by a digit), consume
//               the quantifier and an optional `?` lazy modifier;
//               increment the chain counter. If the chain reaches
//               THRESHOLD, return Some(reason).
//             - On `)` with no quantifier following, reset the chain
//               (a close that is not quantified breaks the pattern).
//             - Any other byte (atom, anchor, `|`, literal) resets
//               the chain.
//
//           Place the call alongside `stacked_quantifier` in
//           `compile_rule_src` -- both are structural pre-validators
//           that apply regardless of engine routing, and both
//           rejection messages read as "this source shape is
//           structurally bad" rather than "the plain path
//           specifically dislikes it". Either engine would either
//           reject the shape outright or wall-clock on it.
// TS map:   `function nestedGroupedQuantifier(src: string): string | null`.
//
// In TS you'd write (pseudocode):
// ```ts
// function nestedGroupedQuantifier(src: string): string | null {
//   // walk bytes; skip \X escapes and class interiors;
//   // on `(` reset chain (and skip optional `?` after);
//   // on `)` peek next byte: if quantifier start, consume it
//   // (plus optional lazy `?`) and chain++; if chain >= 4 return reason;
//   // otherwise reset chain. Any other byte resets chain.
// }
// ```
pub fn nested_grouped_quantifier(src: &str) -> Option<String> {
    let bytes = src.as_bytes();
    let mut i = 0usize;
    let mut in_class = false;
    // What:     `chain: usize` counts consecutive `){quant}` adjacencies.
    //           Resets to 0 on anything that interrupts the pattern
    //           (a new `(`, an atom byte, an alternation, an escape,
    //           a class boundary, or a `)` not followed by a quantifier).
    // Why:      The fuzz blowup shape is exactly a chain of these; the
    //           single integer captures the state without paren-depth
    //           tracking. We don't need to know HOW deep the groups
    //           nest, only that there are CHAIN consecutive
    //           close+quantifier pairs back-to-back.
    // TS map:   `let chain = 0;`.
    let mut chain: usize = 0;
    // What:     `const THRESHOLD: usize = 4;` is the flag-at chain
    //           length. Empirically 3 still compiles in milliseconds
    //           even on Unicode classes; 4 is the inflection point
    //           where NFA size starts to bloat noticeably; 5 is the
    //           slow-unit shape. Catching at 4 gives one level of
    //           safety margin under Unicode widening.
    // Why:      The cutoff is conservative enough to spare plausibly-
    //           authored rules (real secret-detection patterns rarely
    //           nest beyond 2 quantifier levels). Tune downward only
    //           if production rules trip it.
    // TS map:   `const THRESHOLD = 4;`.
    const THRESHOLD: usize = 4;
    while i < bytes.len() {
        let c = bytes[i];
        // What:     Escape pair `\X` is two literal bytes representing
        //           one atom. Skip both bytes and reset chain (the atom
        //           breaks any pending chain of `){quant}` pairs --
        //           e.g. `){5,11}\d){5,11}` is not a chain).
        // Why:      Without skipping, `\)` would be mis-detected as a
        //           group close, and `\{` would be mis-detected as a
        //           quantifier start.
        // TS map:   `if (c === 0x5c) { i += 2; chain = 0; continue; }`.
        if c == b'\\' {
            i += 2;
            chain = 0;
            continue;
        }
        // What:     Character class entry / exit. Inside `[...]` the
        //           metacharacters `(`, `)`, `*`, `+`, `?`, `{`, `}`
        //           are literal bytes; we skip the entire class body.
        //           The class itself is an atom so entry resets chain.
        // Why:      `[)]*` is a one-byte class then a quantifier on
        //           that atom, NOT a `)`+quantifier chain link.
        // TS map:   `if (!inClass && c === 0x5b) { inClass = true; chain = 0; ... }`.
        if !in_class && c == b'[' {
            in_class = true;
            chain = 0;
            i += 1;
            continue;
        }
        if in_class {
            if c == b']' {
                in_class = false;
            }
            i += 1;
            continue;
        }
        // What:     Group open `(`. Opens a fresh group; any pending
        //           `){quant}` chain is broken (the chain demands
        //           BACK-TO-BACK close+quant pairs with nothing
        //           between, and a new open is "something between").
        //           Skip an optional `?` after `(` so the `?` in
        //           `(?:`, `(?i)`, `(?P<name>`, `(?<name>`, `(?=`,
        //           `(?!`, `(?<=`, `(?<!`, `(?#...)` is not later
        //           misread as a `?` quantifier.
        // Why:      Without the `?` skip, `(?:a)*` would peek `?`
        //           after the open and may misinterpret subsequent
        //           parsing. The skip keeps the walker structurally
        //           agnostic to group flavor.
        // TS map:   `if (c === 0x28) { chain = 0; i += 1; if (b[i] === 0x3f) i += 1; continue; }`.
        if c == b'(' {
            chain = 0;
            i += 1;
            if i < bytes.len() && bytes[i] == b'?' {
                i += 1;
            }
            continue;
        }
        // What:     Group close `)`. Peek the next byte; if it is a
        //           quantifier start, consume the quantifier (`*`/`+`/
        //           `?` is one byte; `{N}`/`{N,}`/`{N,M}` runs to the
        //           matching `}`) plus an optional `?` lazy modifier,
        //           and increment chain. The `+` possessive is NOT a
        //           modifier in the regex crate -- treat it as a
        //           fresh stacked quantifier on the next iteration
        //           (which will hit "any other byte" and reset chain).
        //           If chain reaches THRESHOLD, return the reason.
        //           If the close is not followed by a quantifier
        //           (close that re-enters a parent expression, etc.),
        //           reset chain -- the pattern is broken.
        // Why:      This is the single state transition that detects
        //           the blowup shape. Every chain increment requires a
        //           `)` IMMEDIATELY followed by a `*`/`+`/`?`/`{N`;
        //           any deviation resets the chain.
        // TS map:   `if (c === 0x29) { i += 1; const isQuant = ...; if (isQuant) { ...consume...; chain += 1; if (chain >= THRESHOLD) return reason; } else { chain = 0; } continue; }`.
        if c == b')' {
            i += 1;
            let is_quant = i < bytes.len()
                && (matches!(bytes[i], b'*' | b'+' | b'?')
                    || (bytes[i] == b'{'
                        && i + 1 < bytes.len()
                        && bytes[i + 1].is_ascii_digit()));
            if is_quant {
                if bytes[i] == b'{' {
                    while i < bytes.len() && bytes[i] != b'}' {
                        i += 1;
                    }
                    if i < bytes.len() {
                        i += 1;
                    }
                } else {
                    i += 1;
                }
                if i < bytes.len() && bytes[i] == b'?' {
                    i += 1;
                }
                chain += 1;
                if chain >= THRESHOLD {
                    return Some(format!(
                        "nested grouped quantifier at byte offset {}: chain of {} consecutive `)`+quantifier pairs detected. Deeply nested quantified groups (e.g. `(?:(?:(?:(?:a){{5,11}}){{5,11}}){{5,11}}){{5,11}}`) expand multiplicatively in NFA construction; depth 4+ reaches the `regex` crate's 256 MB size limit during compile and takes seconds to error. Flatten the nesting or apply at most a few quantifier levels per group.",
                        i, chain
                    ));
                }
            } else {
                chain = 0;
            }
            continue;
        }
        // What:     Any other byte (atom, anchor, `|`, literal) breaks
        //           the chain.
        // Why:      Chain requires BACK-TO-BACK close+quant; an atom
        //           between two `){quant}` pairs means they are not
        //           chained.
        chain = 0;
        i += 1;
    }
    None
}

// What:     `pub fn nested_quantifier_after_wildcard(src: &str) -> Option<String>`
//           detects rule shapes where a bare `_` wildcard (the
//           scanner's `_` triad atom, expanded to wildcard during
//           resharp dispatch) is immediately followed by three or
//           more consecutive `)`+quantifier adjacencies. The shape
//           the fuzz target emits is `(?:(?:(?:(?:_){5,6}){5,12})+`
//           and similar, decoded from artifacts
//           `slow-unit-8c4172d7d381b5c64c5aba568217c38c5ce94945`
//           (compile 409ms + scan 1.16s) and
//           `slow-unit-709cb39b5255ddf0721c435159191d03aa0498ea`
//           (compile 4.33s).
// Why:      Each `){N,M}` on a group multiplies the inner atom's NFA
//           state count by up to M. `_` is the largest atom in the
//           dispatch surface (matches every byte / Unicode code
//           point), so depth-3 nesting on `_` produces hundreds of
//           thousands of effective inner repetitions and resharp's
//           NFA construction takes from hundreds of milliseconds to
//           several seconds. libFuzzer's `report_slow_units` flag
//           records these as slow-unit artifacts; the fuzz target
//           burns budget re-running them. The existing
//           `nested_grouped_quantifier` detector (threshold 4)
//           passes these depth-3 shapes through to resharp because
//           depth-3 with literal innermost atom (e.g. `(?:(?:(?:a)*)*)*`)
//           does compile in milliseconds. Distinguishing on the
//           innermost atom kind without rewriting the existing
//           walker is cleanest as a separate, targeted pre-validator
//           that fires only when the chain follows `_` directly.
//
//           Detection criterion (single-pass byte walker):
//             - Walk `src`, skipping escape pairs `\X` and class
//               interiors `[...]` (the `_` inside a class is a
//               literal underscore byte, not the wildcard triad).
//             - At every bare `_` byte outside a class, count the
//               immediately-following chain of `)`+quantifier pairs.
//             - If the chain length reaches 3, return Some(reason).
//
//           Threshold: chain >= 3 (one less than
//           `nested_grouped_quantifier`'s threshold of 4). Empirical
//           timings (probed against resharp 0.6.0 with
//           overflow-checks=on): depth-3 on `_` ranges 400ms-4.3s;
//           depth-2 on `_` (chain 2) compiles in <50ms. Catching at
//           chain=3 is the precise inflection point and keeps the
//           false-positive risk at zero against the production
//           ruleset (max chain in `forbidden-strings.local.txt` is
//           1; max chain in any production rule is 1).
//
//           Place the call in `compile_rule_src`'s resharp branch:
//           bare `_` outside a class is a `requires_resharp` trigger,
//           so the validator only fires for rules that would route
//           to resharp anyway. The plain-regex path never sees the
//           `_` wildcard triad and the validator would be dead code
//           on that branch.
// TS map:   `function nestedQuantifierAfterWildcard(src: string): string | null`.
//
// In TS you'd write (pseudocode):
// ```ts
// function nestedQuantifierAfterWildcard(src: string): string | null {
//   // walk bytes; skip \X escapes and [class] bodies;
//   // at every bare `_` outside a class, count consecutive `)`+quant
//   // pairs that follow; if >= 3, return reason.
// }
// ```
pub fn nested_quantifier_after_wildcard(src: &str) -> Option<String> {
    let bytes = src.as_bytes();
    let mut i = 0usize;
    let mut in_class = false;
    while i < bytes.len() {
        let c = bytes[i];
        // What:     Escape pair `\X` is two bytes representing one atom.
        // Why:      `\_` is an escaped underscore literal, not the
        //           wildcard triad; `\(` / `\)` would corrupt the
        //           class-tracking state.
        // TS map:   `if (c === 0x5c) { i += 2; continue; }`.
        if c == b'\\' {
            i += 2;
            continue;
        }
        // What:     Character class entry / exit. Inside `[...]`, `_`
        //           is a literal underscore byte, NOT the wildcard
        //           triad.
        // Why:      `[_]` matches the single byte `_`; only bare `_`
        //           outside a class expands to wildcard in this
        //           scanner's dispatch.
        // TS map:   `if (!inClass && c === 0x5b) { inClass = true; i++; continue; }`.
        if !in_class && c == b'[' {
            in_class = true;
            i += 1;
            continue;
        }
        if in_class {
            if c == b']' {
                in_class = false;
            }
            i += 1;
            continue;
        }
        // What:     At every bare `_` outside a class, count the
        //           immediately-following chain of `)`+quantifier
        //           pairs. If >= 3, return reason.
        // Why:      The 8c41 / 709c slow-unit shape is exactly this:
        //           a `_` followed by three or more close+quantifier
        //           adjacencies.
        // TS map:   `if (c === 0x5f) { const chain = countCloseQuantChain(bytes, i+1); if (chain >= 3) return reason; }`.
        if c == b'_' {
            let chain = count_close_quant_chain_after(bytes, i + 1);
            if chain >= 3 {
                return Some(format!(
                    "nested quantifier on bare wildcard `_` at byte offset {}: chain of {} consecutive `)`+quantifier pairs immediately follows the `_`. The `_` triad expands to a wildcard atom (matches every char); nesting quantifiers depth 3+ on a wildcard explodes resharp's NFA construction, taking hundreds of milliseconds to several seconds to compile. Flatten the nesting or replace `_` with a more restrictive atom.",
                    i, chain
                ));
            }
        }
        i += 1;
    }
    None
}

// What:     `pub fn nested_chain_in_lookaround_body(src: &str) -> Option<String>`
//           detects rule shapes containing a chain of three or more
//           consecutive `)`+quantifier adjacencies anywhere INSIDE
//           an open lookaround body (`(?!...)`, `(?=...)`,
//           `(?<!...)`, `(?<=...)`). Decoded from artifact
//           `slow-unit-4eabfd5c52969dcc20c2170cd30947eccf8ae62f`
//           which compiles in 1.9s before resharp returns
//           `Algebra(UnsupportedPattern)`.
// Why:      Even with a literal innermost atom (`nested_quantifier_after_wildcard`
//           does not fire), resharp's algebra simplifier struggles
//           to canonicalise the derivative of a chain-3 quantifier
//           nest that sits inside an open lookaround context. The
//           lookaround forces the simplifier to walk derivative
//           shapes per-prefix per-suffix, multiplying the work
//           proportionally to the chain's NFA size. The compile
//           wall-clocks past libFuzzer's slow-unit threshold even
//           though the eventual outcome is a clean `Err`.
//
//           Detection criterion (single-pass byte walker with a
//           lookaround-depth stack):
//             - Walk `src`, skipping escape pairs `\X` and class
//               interiors `[...]`.
//             - On `(`, push a frame: `true` if the opener is
//               `(?!` / `(?=` / `(?<!` / `(?<=`, else `false`.
//             - On `)`, pop the frame.
//             - Maintain a running `chain` counter of consecutive
//               `)`+quantifier pairs; reset on `(`, alternation `|`,
//               escape, class entry, or any other literal byte.
//             - Increment chain only when `)` is immediately followed
//               by a quantifier (`*`/`+`/`?`/`{N`). Consume the
//               quantifier and an optional lazy `?`.
//             - If chain >= 3 AND any frame currently on the stack
//               is a lookaround, return Some(reason).
//
//           The lookaround-stack is intentionally counter-style
//           (any lookaround anywhere up the stack triggers) rather
//           than nearest-lookaround: 4eab nests the chain inside
//           the OUTER `(?!...)` lookahead with an INNER `(?!...)`
//           in between; both are open during the chain. A nearest-
//           lookaround check would miss the case where the chain
//           sits two levels deep inside the outer lookaround.
//
//           Place the call in `compile_rule_src`'s resharp branch:
//           lookarounds are `requires_resharp` triggers, so the
//           validator only fires for rules that would route to
//           resharp anyway.
// TS map:   `function nestedChainInLookaroundBody(src: string): string | null`.
//
// In TS you'd write (pseudocode):
// ```ts
// function nestedChainInLookaroundBody(src: string): string | null {
//   // walk bytes; maintain a stack of bool (true=lookaround frame);
//   // skip \X escapes and [class] bodies;
//   // on `(` push frame (true if `(?!`/`(?=`/`(?<!`/`(?<=`);
//   // on `)` pop frame, then if next is quantifier, consume and
//   //   chain++; if chain >= 3 and any open frame is lookaround,
//   //   return reason;
//   // any other byte resets chain.
// }
// ```
pub fn nested_chain_in_lookaround_body(src: &str) -> Option<String> {
    let bytes = src.as_bytes();
    let mut i = 0usize;
    let mut in_class = false;
    let mut chain: usize = 0;
    // What:     `stack: Vec<bool>`. One entry per open group; `true`
    //           if the opener is a lookaround (`(?!`/`(?=`/`(?<!`/
    //           `(?<=`), `false` otherwise (`(`, `(?:`, `(?P<...>`,
    //           `(?i)`, etc.).
    // Why:      Knowing whether ANY currently-open frame is a
    //           lookaround is enough to gate the chain trigger.
    //           Storing the booleans in order lets us pop on `)`
    //           and check the stack with one pass.
    let mut stack: Vec<bool> = Vec::new();
    while i < bytes.len() {
        let c = bytes[i];
        // What:     Escape pair `\X`. Two bytes, one atom; breaks any
        //           pending close+quant chain.
        // Why:      `\)` would mis-pop a frame; `\(` would push a
        //           bogus frame.
        // TS map:   `if (c === 0x5c) { i += 2; chain = 0; continue; }`.
        if c == b'\\' {
            i += 2;
            chain = 0;
            continue;
        }
        // What:     Character class entry / exit. Inside `[...]`,
        //           parens / quantifiers / `|` are literal bytes.
        // Why:      `[)]` shouldn't pop a frame; `[*]` shouldn't be
        //           read as a quantifier.
        // TS map:   `if (!inClass && c === 0x5b) { inClass = true; chain = 0; i++; continue; }`.
        if !in_class && c == b'[' {
            in_class = true;
            chain = 0;
            i += 1;
            continue;
        }
        if in_class {
            if c == b']' {
                in_class = false;
            }
            i += 1;
            continue;
        }
        // What:     `(`: detect lookaround opener, push frame, reset
        //           chain. Skip the `?` after `(` so the body walk
        //           doesn't misread the group prefix.
        // Why:      Lookaround openers are `(?!` / `(?=` / `(?<!` /
        //           `(?<=`. Detecting at open time means the inner
        //           body walk can check `stack.iter().any(|&b| b)`
        //           in O(depth) without re-parsing.
        // TS map:   `if (c === 0x28) { stack.push(isLookaroundOpen(bytes, i)); chain = 0; i++; if (bytes[i] === 0x3f) i++; continue; }`.
        if c == b'(' {
            let is_lookaround = is_lookaround_opener(bytes, i);
            stack.push(is_lookaround);
            chain = 0;
            i += 1;
            if i < bytes.len() && bytes[i] == b'?' {
                i += 1;
            }
            continue;
        }
        // What:     `)`: pop the frame. Check the following byte for
        //           a quantifier; if present, consume it (plus lazy
        //           `?`) and increment chain. If chain >= 3 AND any
        //           STILL-OPEN frame is a lookaround, return reason.
        // Why:      Popping before the chain check is critical: the
        //           lookaround that wraps the chain must remain on
        //           the stack at trigger time. The wrap is OUTER, so
        //           it stays open until its own matching `)`.
        // TS map:   `if (c === 0x29) { stack.pop(); ...quant consume...; chain++; if (chain >= 3 && stack.some(b => b)) return reason; }`.
        if c == b')' {
            stack.pop();
            i += 1;
            let is_quant = i < bytes.len()
                && (matches!(bytes[i], b'*' | b'+' | b'?')
                    || (bytes[i] == b'{'
                        && i + 1 < bytes.len()
                        && bytes[i + 1].is_ascii_digit()));
            if is_quant {
                if bytes[i] == b'{' {
                    while i < bytes.len() && bytes[i] != b'}' {
                        i += 1;
                    }
                    if i < bytes.len() {
                        i += 1;
                    }
                } else {
                    i += 1;
                }
                if i < bytes.len() && bytes[i] == b'?' {
                    i += 1;
                }
                chain += 1;
                if chain >= 3 && stack.iter().any(|&b| b) {
                    return Some(format!(
                        "nested grouped quantifier inside lookaround body at byte offset {}: chain of {} consecutive `)`+quantifier pairs while an open lookaround frame remains higher up the stack. Resharp's algebra simplifier walks derivative shapes per-prefix per-suffix inside lookarounds, multiplying the chain's NFA cost by the lookaround context size; the compile wall-clocks past libFuzzer's slow-unit threshold even when the eventual outcome is `Err(UnsupportedPattern)`. Flatten the nesting or move the chain outside the lookaround body.",
                        i, chain
                    ));
                }
            } else {
                chain = 0;
            }
            continue;
        }
        // What:     Any other byte (atom, anchor, `|`, literal) breaks
        //           the chain.
        // Why:      Chain requires BACK-TO-BACK close+quant.
        chain = 0;
        i += 1;
    }
    None
}

// What:     `fn count_close_quant_chain_after(bytes: &[u8], start: usize) -> usize`
//           returns the number of consecutive `)`+quantifier pairs
//           starting at `start`. Used by `nested_quantifier_after_wildcard`
//           to count the chain after a `_`.
// Why:      Sharing the chain-counting logic via a helper avoids
//           duplicating the quantifier-parsing rules across the two
//           detectors.
// TS map:   `function countCloseQuantChainAfter(bytes: Uint8Array, start: number): number`.
fn count_close_quant_chain_after(bytes: &[u8], start: usize) -> usize {
    let mut i = start;
    let mut chain = 0usize;
    while i < bytes.len() && bytes[i] == b')' {
        i += 1;
        let is_quant = i < bytes.len()
            && (matches!(bytes[i], b'*' | b'+' | b'?')
                || (bytes[i] == b'{'
                    && i + 1 < bytes.len()
                    && bytes[i + 1].is_ascii_digit()));
        if !is_quant {
            break;
        }
        if bytes[i] == b'{' {
            while i < bytes.len() && bytes[i] != b'}' {
                i += 1;
            }
            if i < bytes.len() {
                i += 1;
            }
        } else {
            i += 1;
        }
        if i < bytes.len() && bytes[i] == b'?' {
            i += 1;
        }
        chain += 1;
    }
    chain
}

// What:     `pub fn nested_complement(src: &str) -> Option<String>`
//           detects rule shapes containing one resharp complement
//           `~(...)` whose body contains another `~(...)` complement
//           (back-to-back `~(~(...))` or transparent-group-separated
//           `~((?:~(...)))`). Decoded from artifact
//           `timeout-95f5e661c596e4b5a12e9841cda2e3ba242ecf7a` after
//           the bias commit's generator now produces such shapes.
// Why:      Resharp's algebra simplifier computes a complement via
//           DFA derivative; computing the complement of a complement
//           does not short-circuit to identity in 0.6.x and instead
//           walks both derivative chains. Probed compile time:
//             - `~(~(quantified_ws_chain))` -- 916 ms
//             - `~((?:~(quantified_ws_chain)))` -- 913 ms
//             - `~(quantified_ws_chain)` (single) -- 1.84 ms
//           Under cargo-fuzz's ASAN build the 900 ms compile
//           amplifies past libFuzzer's 10 s per-input timeout (the
//           timeout artifact reproduced in 31 s through the fuzz
//           binary). Source-shape rejection avoids the wall-clock
//           burn.
//
//           Detection criterion (single-pass byte walker with a
//           per-paren is-complement-frame stack):
//             - Walk `src`, skipping escape pairs `\X` and class
//               interiors `[...]`.
//             - On `~(`, FIRST check whether any open frame is a
//               complement; if so, return Some(reason). Then push
//               a "complement" frame onto the stack.
//             - On `(` (without preceding `~`), push a "non-
//               complement" frame; skip the `?` after `(` so the
//               body walk doesn't misread group prefixes.
//             - On `)`, pop the top frame.
//
//           The check-before-push order is critical: it ensures the
//           OUTER complement is still on the stack when the INNER
//           complement is detected. Reversing the order would miss
//           the back-to-back case (the outer's frame would be
//           pushed, then immediately the inner's, with no
//           opportunity to detect that the inner is INSIDE the
//           outer's body).
//
//           Sibling complements (`~(...)&~(...)` shape used by the
//           production rule
//           `RELEASE_TAG_[a-f0-9]{32}&~(RELEASE_TAG_(00){16})&~(...)`)
//           do NOT trigger this detector: the first complement
//           closes (popping its frame) before the second opens, so
//           the second `~(` sees a stack with no complement frames.
//
//           Place the call in `compile_rule_src`'s resharp branch:
//           `~(` is itself a `requires_resharp` trigger, so the
//           validator only fires for rules that would route to
//           resharp anyway.
// TS map:   `function nestedComplement(src: string): string | null`.
//
// In TS you'd write (pseudocode):
// ```ts
// function nestedComplement(src: string): string | null {
//   // walk bytes; maintain stack of bool (true=complement frame);
//   // skip \X escapes and [class] bodies;
//   // on `~(`: check `stack.some(b => b)` first (return if true);
//   //   then push true; skip 2 bytes;
//   // on `(`: push false; skip 1 byte; skip `?` after `(`;
//   // on `)`: pop.
// }
// ```
pub fn nested_complement(src: &str) -> Option<String> {
    let bytes = src.as_bytes();
    let mut i = 0usize;
    let mut in_class = false;
    let mut stack: Vec<bool> = Vec::new();
    while i < bytes.len() {
        let c = bytes[i];
        if c == b'\\' {
            i += 2;
            continue;
        }
        if !in_class && c == b'[' {
            in_class = true;
            i += 1;
            continue;
        }
        if in_class {
            if c == b']' {
                in_class = false;
            }
            i += 1;
            continue;
        }
        // What:     `~(` opens a complement. Check first whether we
        //           are inside another complement; if so, fire.
        //           Otherwise push a "complement" frame and skip
        //           past `~(`.
        // Why:      Resharp's complement-of-complement evaluation
        //           walks both derivative chains and takes
        //           hundreds of milliseconds; under ASAN that
        //           amplifies past libFuzzer's timeout.
        // TS map:   `if (c === 0x7e && bytes[i+1] === 0x28) { if (stack.some(b => b)) return reason; stack.push(true); i += 2; continue; }`.
        if c == b'~' && bytes.get(i + 1).copied() == Some(b'(') {
            if stack.iter().any(|&b| b) {
                return Some(format!(
                    "nested complement `~(~(...))` at byte offset {}: a complement appears inside another complement's body. Resharp's algebra simplifier computes complements via DFA derivative; complement-of-complement walks both derivative chains without identity short-circuit, taking hundreds of milliseconds to compile. Under ASAN the cost amplifies past libFuzzer's 10s per-input timeout. Flatten the nesting (one complement at a time, joined via `&` if combining), or eliminate the outer complement.",
                    i
                ));
            }
            stack.push(true);
            i += 2;
            continue;
        }
        if c == b'(' {
            stack.push(false);
            i += 1;
            if i < bytes.len() && bytes[i] == b'?' {
                i += 1;
            }
            continue;
        }
        if c == b')' {
            stack.pop();
            i += 1;
            continue;
        }
        i += 1;
    }
    None
}

// What:     `fn is_lookaround_opener(bytes: &[u8], i: usize) -> bool`
//           returns true if `bytes[i..]` starts with `(?!`/`(?=`/
//           `(?<!`/`(?<=`.
// Why:      Detect lookaround openers without committing to a full
//           regex parser. Used by `nested_chain_in_lookaround_body`.
// TS map:   `function isLookaroundOpener(bytes: Uint8Array, i: number): boolean`.
fn is_lookaround_opener(bytes: &[u8], i: usize) -> bool {
    if bytes.get(i).copied() != Some(b'(') {
        return false;
    }
    if bytes.get(i + 1).copied() != Some(b'?') {
        return false;
    }
    match bytes.get(i + 2).copied() {
        Some(b'!') | Some(b'=') => true,
        Some(b'<') => matches!(bytes.get(i + 3).copied(), Some(b'!') | Some(b'=')),
        _ => false,
    }
}