rehuman 0.2.0

Unicode-safe text cleaning & typographic normalization for Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
//! rehuman — Unicode‑safe text cleaning & typographic normalization.

use deunicode::deunicode_char;
use icu_properties::{props, CodePointSetData, CodePointSetDataBorrowed};
use serde::Serialize;
use std::borrow::Cow;
use std::fmt;
#[cfg(feature = "unorm")]
use unicode_normalization::UnicodeNormalization;
use unicode_segmentation::UnicodeSegmentation;

mod generated;
mod sets;
use generated::{DASH_MAP, GREEK_MAP, QUOTE_MAP, SPACE_MAP};
use sets::{is_common_symbol_or_punctuation, is_control_char, is_latin_script};
pub use sets::{is_emoji, is_extended_keyboard_char, is_hidden_char, is_keyboard_ascii};

const FRACTION_SLASH: char = '\u{2044}';
const HORIZONTAL_ELLIPSIS: char = '\u{2026}';
const MIDLINE_HORIZONTAL_ELLIPSIS: char = '\u{22EF}';

/// Unicode normalization modes.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum UnicodeNormalizationMode {
    None,
    NFD,
    NFC,
    NFKD,
    NFKC,
}

/// Line ending styles.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LineEndingStyle {
    Lf,   // \n
    Crlf, // \r\n
    Cr,   // \r
}

/// Policy for emoji handling when `keyboard_only` is enabled.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum EmojiPolicy {
    Keep,
    Drop,
}

/// Policy for handling non-ASCII graphemes in `keyboard_only` mode.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NonAsciiPolicy {
    /// Remove non-ASCII graphemes when they are not explicitly normalized elsewhere.
    Drop,
    /// Keep only compatibility-decomposed ASCII output.
    Fold,
    /// Fold first, then apply transliteration fallbacks before dropping.
    Transliterate,
}

/// Detailed statistics about cleaning operations.
#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize)]
pub struct CleaningStats {
    pub hidden_chars_removed: u64,
    pub trailing_whitespace_removed: u64,
    pub spaces_normalized: u64,
    pub whitespace_collapsed: u64,
    pub dashes_normalized: u64,
    pub quotes_normalized: u64,
    pub other_normalized: u64,
    pub control_chars_removed: u64,
    pub line_endings_normalized: u64,
    pub unicode_normalized: u64,
    pub non_keyboard_removed: u64,
    pub non_keyboard_transliterated: u64,
    pub emojis_dropped: u64,
    #[cfg(feature = "security")]
    pub bidi_controls_removed: u64,
}

/// Result of a text cleaning operation.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct CleaningResult<'a> {
    pub text: Cow<'a, str>,
    pub changes_made: u64,
    pub stats: CleaningStats,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
/// Errors produced by fallible cleaning APIs.
pub enum CleaningError {
    /// A Unicode normalization mode was requested without the `unorm` feature.
    NormalizationUnavailable { requested: UnicodeNormalizationMode },
}

impl fmt::Display for CleaningError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            CleaningError::NormalizationUnavailable { requested } => write!(
                f,
                "Unicode normalization {:?} requested but the 'unorm' feature is disabled",
                requested
            ),
        }
    }
}

impl std::error::Error for CleaningError {}

impl CleaningStats {
    /// Merge another stats snapshot into this one.
    ///
    /// # Arguments
    /// - `other`: Additional counters to accumulate into `self`.
    #[cfg(feature = "stats")]
    pub fn accumulate(&mut self, other: &CleaningStats) {
        self.hidden_chars_removed = self
            .hidden_chars_removed
            .saturating_add(other.hidden_chars_removed);
        self.trailing_whitespace_removed = self
            .trailing_whitespace_removed
            .saturating_add(other.trailing_whitespace_removed);
        self.spaces_normalized = self
            .spaces_normalized
            .saturating_add(other.spaces_normalized);
        self.whitespace_collapsed = self
            .whitespace_collapsed
            .saturating_add(other.whitespace_collapsed);
        self.dashes_normalized = self
            .dashes_normalized
            .saturating_add(other.dashes_normalized);
        self.quotes_normalized = self
            .quotes_normalized
            .saturating_add(other.quotes_normalized);
        self.other_normalized = self.other_normalized.saturating_add(other.other_normalized);
        self.control_chars_removed = self
            .control_chars_removed
            .saturating_add(other.control_chars_removed);
        self.line_endings_normalized = self
            .line_endings_normalized
            .saturating_add(other.line_endings_normalized);
        self.unicode_normalized = self
            .unicode_normalized
            .saturating_add(other.unicode_normalized);
        self.non_keyboard_removed = self
            .non_keyboard_removed
            .saturating_add(other.non_keyboard_removed);
        self.non_keyboard_transliterated = self
            .non_keyboard_transliterated
            .saturating_add(other.non_keyboard_transliterated);
        self.emojis_dropped = self.emojis_dropped.saturating_add(other.emojis_dropped);
        #[cfg(feature = "security")]
        {
            self.bidi_controls_removed = self
                .bidi_controls_removed
                .saturating_add(other.bidi_controls_removed);
        }
    }

    #[cfg(not(feature = "stats"))]
    #[inline]
    /// No-op stats accumulation when the `stats` feature is disabled.
    ///
    /// # Arguments
    /// - `_`: Ignored stats payload.
    pub fn accumulate(&mut self, _: &CleaningStats) {
        // No-op when stats are disabled.
    }
}

#[cfg(feature = "stats")]
macro_rules! record_stat {
    ($stats:expr, $field:ident, $amount:expr) => {{
        $stats.$field = $stats.$field.saturating_add($amount);
    }};
}

#[cfg(not(feature = "stats"))]
macro_rules! record_stat {
    ($stats:expr, $field:ident, $amount:expr) => {{
        let _ = &$stats;
        let _ = stringify!($field);
        let _ = &$amount;
    }};
}

macro_rules! record_change {
    ($changes:expr, $stats:expr, $field:ident) => {{
        record_change!($changes, $stats, $field, 1u64);
    }};
    ($changes:expr, $stats:expr, $field:ident, $amount:expr) => {{
        let amount = ($amount) as u64;
        $changes = $changes.saturating_add(amount);
        record_stat!($stats, $field, amount);
    }};
}

/// Configuration for cleaning.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CleaningOptions {
    pub remove_hidden: bool,
    pub remove_trailing_whitespace: bool,
    pub normalize_spaces: bool,
    pub normalize_dashes: bool,
    pub normalize_quotes: bool,
    pub normalize_other: bool, // ellipsis (… -> ...), etc.
    pub keyboard_only: bool,
    pub extended_keyboard: bool, // curated non-ASCII allowlist in keyboard mode
    pub emoji_policy: EmojiPolicy, // effective only if keyboard_only = true
    pub non_ascii_policy: NonAsciiPolicy, // effective only if keyboard_only = true
    pub preserve_joiners: bool,  // keep ZWJ/ZWNJ when remove_hidden is enabled
    pub remove_control_chars: bool, // remove Cc excluding \n, \r, \t
    pub collapse_whitespace: bool,
    pub normalize_line_endings: Option<LineEndingStyle>,
    pub unicode_normalization: UnicodeNormalizationMode,
    #[cfg_attr(not(feature = "security"), doc(hidden))]
    pub strip_bidi_controls: bool,
}

#[derive(Debug, Clone)]
/// Builder for [`CleaningOptions`].
pub struct CleaningOptionsBuilder {
    options: CleaningOptions,
}

impl Default for CleaningOptions {
    fn default() -> Self {
        Self {
            remove_hidden: true,
            remove_trailing_whitespace: true,
            normalize_spaces: true,
            normalize_dashes: true,
            normalize_quotes: true,
            normalize_other: true,
            keyboard_only: true,
            extended_keyboard: false,
            emoji_policy: EmojiPolicy::Drop,
            non_ascii_policy: NonAsciiPolicy::Transliterate,
            preserve_joiners: false,
            remove_control_chars: true,
            collapse_whitespace: false,
            normalize_line_endings: None,
            unicode_normalization: UnicodeNormalizationMode::None,
            strip_bidi_controls: false,
        }
    }
}

impl CleaningOptions {
    /// Start a new [`CleaningOptionsBuilder`] with default values.
    ///
    /// # Returns
    /// A builder initialized from [`CleaningOptions::default`].
    pub fn builder() -> CleaningOptionsBuilder {
        CleaningOptionsBuilder {
            options: CleaningOptions::default(),
        }
    }

    /// Minimal preset: only removes hidden/invisible chars.
    ///
    /// # Returns
    /// A conservative preset that performs minimal transformations.
    pub fn minimal() -> Self {
        Self {
            remove_trailing_whitespace: false,
            normalize_spaces: false,
            normalize_dashes: false,
            normalize_quotes: false,
            normalize_other: false,
            keyboard_only: false,
            remove_control_chars: false,
            ..Self::default()
        }
    }

    /// Balanced preset for day-to-day text.
    ///
    /// # Returns
    /// A general-purpose preset for normal prose cleanup.
    pub fn balanced() -> Self {
        Self {
            keyboard_only: false,
            unicode_normalization: UnicodeNormalizationMode::NFC,
            ..Self::default()
        }
    }

    /// Humanize preset for AI/LLM-ish text.
    ///
    /// # Returns
    /// A preset tuned for typographic normalization and whitespace cleanup.
    pub fn humanize() -> Self {
        Self {
            keyboard_only: false,
            unicode_normalization: UnicodeNormalizationMode::NFKC,
            collapse_whitespace: true,
            ..Self::default()
        }
    }

    /// Aggressive preset: maximum cleanup.
    ///
    /// # Returns
    /// A strict preset that targets keyboard-safe output.
    pub fn aggressive() -> Self {
        Self {
            collapse_whitespace: true,
            normalize_line_endings: Some(LineEndingStyle::Lf),
            unicode_normalization: UnicodeNormalizationMode::NFKC,
            strip_bidi_controls: true,
            ..Self::default()
        }
    }

    /// Code-safe preset for docs/source-like content.
    ///
    /// # Returns
    /// A preset that preserves Unicode glyphs (box-drawing, emoji, joiners)
    /// and ellipsis-like punctuation while still removing hidden/control
    /// noise and normalizing typographic quotes and dashes to ASCII.
    pub fn code_safe() -> Self {
        Self {
            normalize_other: false,
            keyboard_only: false,
            emoji_policy: EmojiPolicy::Keep,
            preserve_joiners: true,
            ..Self::default()
        }
    }
}

impl CleaningOptionsBuilder {
    /// Set `remove_hidden`.
    ///
    /// # Arguments
    /// - `value`: Whether to remove default-ignorable code points.
    ///
    /// # Returns
    /// Updated builder.
    pub fn remove_hidden(mut self, value: bool) -> Self {
        self.options.remove_hidden = value;
        self
    }

    /// Set `remove_trailing_whitespace`.
    ///
    /// # Arguments
    /// - `value`: Whether trailing spaces/tabs are trimmed per line.
    ///
    /// # Returns
    /// Updated builder.
    pub fn remove_trailing_whitespace(mut self, value: bool) -> Self {
        self.options.remove_trailing_whitespace = value;
        self
    }

    /// Set `normalize_spaces`.
    ///
    /// # Arguments
    /// - `value`: Whether Unicode spaces normalize to ASCII space.
    ///
    /// # Returns
    /// Updated builder.
    pub fn normalize_spaces(mut self, value: bool) -> Self {
        self.options.normalize_spaces = value;
        self
    }

    /// Set `normalize_dashes`.
    ///
    /// # Arguments
    /// - `value`: Whether Unicode dash variants normalize to `-`.
    ///
    /// # Returns
    /// Updated builder.
    pub fn normalize_dashes(mut self, value: bool) -> Self {
        self.options.normalize_dashes = value;
        self
    }

    /// Set `normalize_quotes`.
    ///
    /// # Arguments
    /// - `value`: Whether curly/Unicode quotes normalize to ASCII quotes.
    ///
    /// # Returns
    /// Updated builder.
    pub fn normalize_quotes(mut self, value: bool) -> Self {
        self.options.normalize_quotes = value;
        self
    }

    /// Set `normalize_other`.
    ///
    /// # Arguments
    /// - `value`: Whether miscellaneous replacements (for example ellipsis)
    ///   are applied.
    ///
    /// # Returns
    /// Updated builder.
    pub fn normalize_other(mut self, value: bool) -> Self {
        self.options.normalize_other = value;
        self
    }

    /// Set `keyboard_only`.
    ///
    /// # Arguments
    /// - `value`: Whether output is restricted to keyboard-safe characters.
    ///
    /// # Returns
    /// Updated builder.
    pub fn keyboard_only(mut self, value: bool) -> Self {
        self.options.keyboard_only = value;
        self
    }

    /// Set `extended_keyboard`.
    ///
    /// # Arguments
    /// - `value`: Whether curated non-ASCII keyboard characters are allowed.
    ///
    /// # Returns
    /// Updated builder.
    pub fn extended_keyboard(mut self, value: bool) -> Self {
        self.options.extended_keyboard = value;
        self
    }

    /// Set `emoji_policy`.
    ///
    /// # Arguments
    /// - `policy`: Emoji handling policy when `keyboard_only` is enabled.
    ///
    /// # Returns
    /// Updated builder.
    pub fn emoji_policy(mut self, policy: EmojiPolicy) -> Self {
        self.options.emoji_policy = policy;
        self
    }

    /// Set `non_ascii_policy`.
    ///
    /// # Arguments
    /// - `policy`: Non-ASCII handling strategy in keyboard-only mode.
    ///
    /// # Returns
    /// Updated builder.
    pub fn non_ascii_policy(mut self, policy: NonAsciiPolicy) -> Self {
        self.options.non_ascii_policy = policy;
        self
    }

    /// Set `preserve_joiners`.
    ///
    /// # Arguments
    /// - `value`: Whether ZWJ/ZWNJ are preserved when `remove_hidden` is enabled.
    ///
    /// # Returns
    /// Updated builder.
    pub fn preserve_joiners(mut self, value: bool) -> Self {
        self.options.preserve_joiners = value;
        self
    }

    /// Set `remove_control_chars`.
    ///
    /// # Arguments
    /// - `value`: Whether control characters are removed.
    ///
    /// # Returns
    /// Updated builder.
    pub fn remove_control_chars(mut self, value: bool) -> Self {
        self.options.remove_control_chars = value;
        self
    }

    /// Set `collapse_whitespace`.
    ///
    /// # Arguments
    /// - `value`: Whether consecutive whitespace runs collapse to single spaces.
    ///
    /// # Returns
    /// Updated builder.
    pub fn collapse_whitespace(mut self, value: bool) -> Self {
        self.options.collapse_whitespace = value;
        self
    }

    /// Set `normalize_line_endings`.
    ///
    /// # Arguments
    /// - `value`: Optional target line-ending style.
    ///
    /// # Returns
    /// Updated builder.
    pub fn normalize_line_endings(mut self, value: Option<LineEndingStyle>) -> Self {
        self.options.normalize_line_endings = value;
        self
    }

    /// Set `unicode_normalization`.
    ///
    /// # Arguments
    /// - `mode`: Unicode normalization mode to apply before cleaning.
    ///
    /// # Returns
    /// Updated builder.
    pub fn unicode_normalization(mut self, mode: UnicodeNormalizationMode) -> Self {
        self.options.unicode_normalization = mode;
        self
    }

    #[cfg_attr(not(feature = "security"), doc(hidden))]
    /// Set `strip_bidi_controls`.
    ///
    /// # Arguments
    /// - `value`: Whether bidirectional controls are removed.
    ///
    /// # Returns
    /// Updated builder.
    pub fn strip_bidi_controls(mut self, value: bool) -> Self {
        self.options.strip_bidi_controls = value;
        self
    }

    /// Build an immutable [`CleaningOptions`] value.
    ///
    /// # Returns
    /// Finalized options struct.
    pub fn build(self) -> CleaningOptions {
        self.options
    }
}

/// Main cleaner.
pub struct TextCleaner {
    options: CleaningOptions,
}

impl TextCleaner {
    /// Create a cleaner from explicit options.
    ///
    /// # Arguments
    /// - `options`: Cleaning behavior configuration.
    ///
    /// # Returns
    /// A reusable [`TextCleaner`].
    pub fn new(options: CleaningOptions) -> Self {
        Self { options }
    }

    /// Borrow the cleaner options.
    ///
    /// # Returns
    /// Immutable reference to configured [`CleaningOptions`].
    pub fn options(&self) -> &CleaningOptions {
        &self.options
    }

    /// Clean text and panic on unavailable normalization features.
    ///
    /// # Arguments
    /// - `text`: Input text to normalize.
    ///
    /// # Returns
    /// Cleaned output and stats.
    ///
    /// # Errors
    /// This infallible wrapper does not return errors; use
    /// [`TextCleaner::try_clean`] for error handling.
    ///
    /// # Panics
    /// Panics when a normalization mode requires the `unorm` feature but it is
    /// not enabled.
    pub fn clean<'a>(&self, text: &'a str) -> CleaningResult<'a> {
        self.try_clean(text).unwrap_or_else(|err| {
            panic!(
                "clean() failed: {err}. Enable the 'unorm' feature or call try_clean() to handle the error"
            )
        })
    }

    /// Clean text into a caller-provided buffer and panic on unavailable
    /// normalization features.
    ///
    /// # Arguments
    /// - `text`: Input text to normalize.
    /// - `out`: Output buffer to reuse.
    ///
    /// # Returns
    /// A result borrowing from `out`.
    ///
    /// # Errors
    /// This infallible wrapper does not return errors; use
    /// [`TextCleaner::try_clean_into`] for error handling.
    ///
    /// # Panics
    /// Panics when a normalization mode requires the `unorm` feature but it is
    /// not enabled.
    pub fn clean_into<'output>(
        &self,
        text: &str,
        out: &'output mut String,
    ) -> CleaningResult<'output> {
        self.try_clean_into(text, out).unwrap_or_else(|err| {
            panic!(
                "clean_into() failed: {err}. Enable the 'unorm' feature or call try_clean_into() to handle the error"
            )
        })
    }

    /// Fallible variant of [`TextCleaner::clean`].
    ///
    /// # Arguments
    /// - `text`: Input text to normalize.
    ///
    /// # Returns
    /// Cleaned output and stats.
    ///
    /// # Errors
    /// Returns [`CleaningError::NormalizationUnavailable`] when normalization
    /// was requested without the `unorm` feature.
    pub fn try_clean<'a>(&self, text: &'a str) -> Result<CleaningResult<'a>, CleaningError> {
        self.try_clean_with_context(text, false)
    }

    /// Fallible variant of [`TextCleaner::clean_into`].
    ///
    /// # Arguments
    /// - `text`: Input text to normalize.
    /// - `out`: Output buffer to reuse.
    ///
    /// # Returns
    /// A result borrowing from `out`.
    ///
    /// # Errors
    /// Returns [`CleaningError::NormalizationUnavailable`] when normalization
    /// was requested without the `unorm` feature.
    pub fn try_clean_into<'output>(
        &self,
        text: &str,
        out: &'output mut String,
    ) -> Result<CleaningResult<'output>, CleaningError> {
        self.try_clean_into_with_context(text, out, false)
    }

    /// Clean text while preserving context about previously emitted output.
    ///
    /// # Arguments
    /// - `text`: Input chunk to clean.
    /// - `has_prior_output`: Whether earlier chunks already emitted output.
    ///
    /// # Returns
    /// Cleaned output and stats.
    ///
    /// # Errors
    /// Returns [`CleaningError::NormalizationUnavailable`] when normalization
    /// was requested without the `unorm` feature.
    pub fn try_clean_with_context<'a>(
        &self,
        text: &'a str,
        has_prior_output: bool,
    ) -> Result<CleaningResult<'a>, CleaningError> {
        let Some((working, renormalized)) = self.prepare_input(text)? else {
            return Ok(CleaningResult {
                text: Cow::Borrowed(text),
                changes_made: 0,
                stats: CleaningStats::default(),
            });
        };

        let mut buffer = String::with_capacity(working.len());
        let (changes, stats) =
            self.clean_into_internal(working, &mut buffer, has_prior_output, renormalized);
        Ok(CleaningResult {
            text: Cow::Owned(buffer),
            changes_made: changes,
            stats,
        })
    }

    /// Buffer-reusing context-aware cleaner.
    ///
    /// # Arguments
    /// - `text`: Input chunk to clean.
    /// - `out`: Output buffer to reuse.
    /// - `has_prior_output`: Whether earlier chunks already emitted output.
    ///
    /// # Returns
    /// A result borrowing from `out`.
    ///
    /// # Errors
    /// Returns [`CleaningError::NormalizationUnavailable`] when normalization
    /// was requested without the `unorm` feature.
    pub fn try_clean_into_with_context<'output>(
        &self,
        text: &str,
        out: &'output mut String,
        has_prior_output: bool,
    ) -> Result<CleaningResult<'output>, CleaningError> {
        out.clear();

        let Some((working, renormalized)) = self.prepare_input(text)? else {
            out.push_str(text);
            return Ok(CleaningResult {
                text: Cow::Borrowed(out.as_str()),
                changes_made: 0,
                stats: CleaningStats::default(),
            });
        };

        let (changes, stats) =
            self.clean_into_internal(working, out, has_prior_output, renormalized);
        Ok(CleaningResult {
            text: Cow::Borrowed(out.as_str()),
            changes_made: changes,
            stats,
        })
    }

    fn clean_into_internal(
        &self,
        working_input: Cow<'_, str>,
        out: &mut String,
        has_prior_output: bool,
        renormalized: bool,
    ) -> (u64, CleaningStats) {
        // Without the `stats` feature, record_stat! never mutates the struct.
        #[cfg_attr(not(feature = "stats"), allow(unused_mut))]
        let mut stats = CleaningStats::default();
        let mut changes = 0u64;

        if renormalized {
            record_change!(changes, stats, unicode_normalized);
        }

        let mut working = working_input;

        let mut line_ending_conversions = LineEndingCounts::default();
        if self.options.normalize_line_endings.is_some() {
            let (lf, counts) = to_lf(working.as_ref());
            line_ending_conversions = counts;
            working = Cow::Owned(lf);
        }

        out.clear();
        out.reserve(working.len());

        // Buffered run of ' '/'\t' chars kept verbatim so a flush without
        // collapse reproduces the input bytes (tabs must survive cleaning).
        let mut pending_ws = String::new();
        let mut cap_next_whitespace = false;
        let mut drop_leading_whitespace = false;
        let mut emitted_anything = has_prior_output;
        let trim = self.options.remove_trailing_whitespace;
        let collapse = self.options.collapse_whitespace;

        let mut emoji_classifier: Option<EmojiClassifier> = None;
        let mut cluster_buffer = String::new();
        #[cfg(feature = "security")]
        let bidi_controls: Option<CodePointSetDataBorrowed<'static>> =
            if self.options.strip_bidi_controls {
                Some(CodePointSetData::new::<props::BidiControl>())
            } else {
                None
            };

        for grapheme in UnicodeSegmentation::graphemes(working.as_ref(), true) {
            if grapheme.is_empty() {
                continue;
            }

            if is_newline_grapheme(grapheme) {
                finish_pending_whitespace_before_break(
                    out,
                    &mut pending_ws,
                    &mut cap_next_whitespace,
                    trim,
                    collapse,
                    &mut changes,
                    &mut stats,
                );
                out.push_str(grapheme);
                emitted_anything = true;
                drop_leading_whitespace = false;
                continue;
            }

            // U+2028/U+2029 LINE/PARAGRAPH SEPARATOR: when line-ending
            // normalization is on, `to_lf` already folded them to `\n` above.
            // Otherwise fold them here as part of space normalization — mirroring
            // the newline branch so surrounding whitespace is trimmed identically —
            // instead of passing them through (or dropping them in keyboard mode).
            if self.options.normalize_spaces
                && self.options.normalize_line_endings.is_none()
                && matches!(grapheme, "\u{2028}" | "\u{2029}")
            {
                finish_pending_whitespace_before_break(
                    out,
                    &mut pending_ws,
                    &mut cap_next_whitespace,
                    trim,
                    collapse,
                    &mut changes,
                    &mut stats,
                );
                out.push('\n');
                record_change!(changes, stats, spaces_normalized);
                emitted_anything = true;
                drop_leading_whitespace = false;
                continue;
            }

            let mut emoji_cluster_cache: Option<bool> = None;
            let mut ensure_emoji_cluster = |classifier: &mut Option<EmojiClassifier>| -> bool {
                if let Some(value) = emoji_cluster_cache {
                    return value;
                }
                if grapheme.is_ascii() {
                    emoji_cluster_cache = Some(false);
                    return false;
                }
                if !self.options.keyboard_only && !self.options.remove_hidden {
                    emoji_cluster_cache = Some(false);
                    return false;
                }
                let classifier = classifier.get_or_insert_with(EmojiClassifier::new);
                let value = classify_emoji_cluster(grapheme, classifier).is_rendered;
                emoji_cluster_cache = Some(value);
                value
            };

            cluster_buffer.clear();
            cluster_buffer.reserve(grapheme.len());
            let mut emitted_directly = false;
            // Per-grapheme tally of interior default-ignorable chars (VS16/ZWJ) we
            // remove. Committed to `hidden_chars_removed` only if the cluster is
            // emitted; discarded if the whole cluster is dropped as an emoji, so a
            // dropped emoji is billed once (emojis_dropped) rather than twice.
            let mut cluster_hidden_removed = 0u64;

            for mut c in grapheme.chars() {
                #[cfg(feature = "security")]
                if let Some(set) = bidi_controls {
                    if set.contains(c) {
                        record_change!(changes, stats, bidi_controls_removed);
                        continue;
                    }
                }

                if self.options.remove_hidden && is_hidden_char(c) {
                    let keep_hidden = (self.options.preserve_joiners && is_joiner(c))
                        || ((!self.options.keyboard_only
                            || matches!(self.options.emoji_policy, EmojiPolicy::Keep))
                            && ensure_emoji_cluster(&mut emoji_classifier));
                    if keep_hidden {
                        cluster_buffer.push(c);
                    } else {
                        record_change!(changes, stats, hidden_chars_removed);
                        cluster_hidden_removed = cluster_hidden_removed.saturating_add(1);
                    }
                    continue;
                }

                if self.options.remove_control_chars && is_disallowed_control(c) {
                    record_change!(changes, stats, control_chars_removed);
                    continue;
                }

                if self.options.normalize_spaces {
                    if let Some(&mapped) = SPACE_MAP.get(&c) {
                        record_change!(changes, stats, spaces_normalized);
                        c = mapped;
                    }
                }

                if self.options.normalize_dashes {
                    if let Some(mapped) = map_dash(c) {
                        if mapped != c {
                            record_change!(changes, stats, dashes_normalized);
                        }
                        c = mapped;
                    }
                }

                if self.options.normalize_quotes {
                    if let Some(mapped) = map_quote(c) {
                        if mapped != c {
                            record_change!(changes, stats, quotes_normalized);
                        }
                        c = mapped;
                    }
                }

                if self.options.normalize_other {
                    match c {
                        FRACTION_SLASH => {
                            c = '/';
                            record_change!(changes, stats, other_normalized);
                        }
                        HORIZONTAL_ELLIPSIS | MIDLINE_HORIZONTAL_ELLIPSIS => {
                            flush_or_drop_pending_whitespace(
                                out,
                                &mut pending_ws,
                                collapse,
                                drop_leading_whitespace,
                                emitted_anything,
                                &mut changes,
                                &mut stats,
                            );
                            out.push_str("...");
                            emitted_anything = true;
                            drop_leading_whitespace = false;
                            record_change!(changes, stats, other_normalized);
                            emitted_directly = true;
                            break;
                        }
                        _ => {}
                    }
                }

                cluster_buffer.push(c);
            }

            if emitted_directly {
                continue;
            }

            if cluster_buffer.is_empty() {
                continue;
            }

            if cluster_buffer.chars().all(|ch| matches!(ch, ' ' | '\t')) {
                if cap_next_whitespace {
                    pending_ws.clear();
                    pending_ws.push(' ');
                    cap_next_whitespace = false;
                } else {
                    pending_ws.push_str(&cluster_buffer);
                }
                continue;
            }

            if self.options.keyboard_only {
                let is_emoji_cluster = ensure_emoji_cluster(&mut emoji_classifier);
                if is_emoji_cluster && matches!(self.options.emoji_policy, EmojiPolicy::Keep) {
                    flush_or_drop_pending_whitespace(
                        out,
                        &mut pending_ws,
                        collapse,
                        drop_leading_whitespace,
                        emitted_anything,
                        &mut changes,
                        &mut stats,
                    );
                    out.push_str(&cluster_buffer);
                    emitted_anything = true;
                    drop_leading_whitespace = false;
                    continue;
                }

                if let Some(rewrite) = rewrite_cluster_to_keyboard_ascii(
                    &cluster_buffer,
                    self.options.non_ascii_policy,
                    self.options.extended_keyboard,
                ) {
                    if rewrite.non_ascii_transliterated > 0 {
                        record_change!(
                            changes,
                            stats,
                            non_keyboard_transliterated,
                            rewrite.non_ascii_transliterated
                        );
                    }
                    if rewrite.non_ascii_removed > 0 {
                        record_change!(
                            changes,
                            stats,
                            non_keyboard_removed,
                            rewrite.non_ascii_removed
                        );
                    }
                    cluster_buffer = rewrite.text;

                    flush_or_drop_pending_whitespace(
                        out,
                        &mut pending_ws,
                        collapse,
                        drop_leading_whitespace,
                        emitted_anything,
                        &mut changes,
                        &mut stats,
                    );
                    out.push_str(&cluster_buffer);
                    emitted_anything = true;
                    drop_leading_whitespace = false;
                } else if is_emoji_cluster {
                    // The emoji cluster is dropped as a unit; its interior VS16/ZWJ
                    // removals are part of that single drop, not separate hidden
                    // removals. Roll them back so changes_made bills the cluster once.
                    if cluster_hidden_removed > 0 {
                        changes = changes.saturating_sub(cluster_hidden_removed);
                        #[cfg(feature = "stats")]
                        {
                            stats.hidden_chars_removed = stats
                                .hidden_chars_removed
                                .saturating_sub(cluster_hidden_removed);
                        }
                    }
                    record_change!(changes, stats, emojis_dropped);
                    cluster_buffer.clear();
                    cap_next_whitespace = true;
                    drop_leading_whitespace = pending_ws.is_empty() && !emitted_anything;
                    if !pending_ws.is_empty() {
                        pending_ws.clear();
                        pending_ws.push(' ');
                    }
                } else {
                    let removed = cluster_buffer
                        .chars()
                        .filter(|c| !is_keyboard_allowed(*c, self.options.extended_keyboard))
                        .count();
                    if removed > 0 {
                        record_change!(changes, stats, non_keyboard_removed, removed);
                    }
                    cluster_buffer.clear();
                    cap_next_whitespace = true;
                    drop_leading_whitespace = pending_ws.is_empty() && !emitted_anything;
                    if !pending_ws.is_empty() {
                        pending_ws.clear();
                        pending_ws.push(' ');
                    }
                }
            } else {
                flush_or_drop_pending_whitespace(
                    out,
                    &mut pending_ws,
                    collapse,
                    drop_leading_whitespace,
                    emitted_anything,
                    &mut changes,
                    &mut stats,
                );
                out.push_str(&cluster_buffer);
                emitted_anything = true;
                drop_leading_whitespace = false;
            }
        }

        if trim {
            if !pending_ws.is_empty() {
                record_change!(
                    changes,
                    stats,
                    trailing_whitespace_removed,
                    pending_ws.chars().count()
                );
            }
        } else {
            flush_or_drop_pending_whitespace(
                out,
                &mut pending_ws,
                collapse,
                drop_leading_whitespace,
                emitted_anything,
                &mut changes,
                &mut stats,
            );
        }

        match self.options.normalize_line_endings {
            Some(LineEndingStyle::Lf) => {
                let total = line_ending_conversions.total();
                if total > 0 {
                    record_change!(changes, stats, line_endings_normalized, total);
                }
            }
            Some(style @ (LineEndingStyle::Crlf | LineEndingStyle::Cr)) => {
                let restamp_changes = restamp_line_endings_mut(style, out);
                let baseline = match style {
                    LineEndingStyle::Crlf => line_ending_conversions.crlf,
                    LineEndingStyle::Cr => line_ending_conversions.cr,
                    LineEndingStyle::Lf => unreachable!(),
                };
                let net = restamp_changes.saturating_sub(baseline);
                if net > 0 {
                    record_change!(changes, stats, line_endings_normalized, net);
                }
            }
            None => {}
        }

        (changes, stats)
    }

    /// Prepare input for the cleaning loop.
    ///
    /// Returns `None` when the fast path applies, otherwise the (possibly
    /// normalized) working text plus whether Unicode normalization rewrote
    /// it — the caller must count that rewrite as a change.
    fn prepare_input<'a>(
        &self,
        text: &'a str,
    ) -> Result<Option<(Cow<'a, str>, bool)>, CleaningError> {
        if text.is_empty() || self.can_use_ascii_fast_path(text) {
            Ok(None)
        } else {
            let working = self.normalize_input(text)?;
            // Non-`None` modes always return an owned string; it counts as a
            // rewrite only when it differs from the input.
            let renormalized = match &working {
                Cow::Borrowed(_) => false,
                Cow::Owned(owned) => owned != text,
            };
            Ok(Some((working, renormalized)))
        }
    }

    fn normalize_input<'a>(&self, text: &'a str) -> Result<Cow<'a, str>, CleaningError> {
        match self.options.unicode_normalization {
            UnicodeNormalizationMode::None => Ok(Cow::Borrowed(text)),
            #[cfg(feature = "unorm")]
            mode => {
                let mut normalized = String::with_capacity(text.len());
                let mut segment_start = 0;

                for (index, c) in text.char_indices() {
                    if self.has_source_sensitive_mapping(c) {
                        append_normalized_segment(
                            mode,
                            &text[segment_start..index],
                            &mut normalized,
                        );
                        normalized.push(c);
                        segment_start = index + c.len_utf8();
                    }
                }
                append_normalized_segment(mode, &text[segment_start..], &mut normalized);
                Ok(Cow::Owned(normalized))
            }
            #[cfg(not(feature = "unorm"))]
            mode => Err(CleaningError::NormalizationUnavailable { requested: mode }),
        }
    }

    /// Return whether the original code point must survive whole-input
    /// normalization so the later keyboard rewrite can apply its explicit
    /// semantic mapping. Normalizing these characters first can erase meaning
    /// (`≠` -> `=` + overlay) or make source unit signs indistinguishable from
    /// Greek letters (`Ω`/`µ` -> `Ω`/`μ`).
    #[cfg(feature = "unorm")]
    fn has_source_sensitive_mapping(&self, c: char) -> bool {
        if !self.options.keyboard_only || is_keyboard_allowed(c, self.options.extended_keyboard) {
            return false;
        }

        match self.options.non_ascii_policy {
            NonAsciiPolicy::Drop => false,
            NonAsciiPolicy::Fold => compat_override(c).is_some(),
            NonAsciiPolicy::Transliterate => {
                compat_override(c).is_some() || explicit_transliteration(c).is_some()
            }
        }
    }

    fn can_use_ascii_fast_path(&self, text: &str) -> bool {
        // Tabs need no guard here: the full pipeline preserves buffered
        // whitespace verbatim when neither trimming nor collapsing, so ASCII
        // input with tabs round-trips identically on both paths.
        text.is_ascii()
            && !self.options.remove_trailing_whitespace
            && !self.options.collapse_whitespace
            && self.options.normalize_line_endings.is_none()
            && !self.options.remove_control_chars
            && matches!(
                self.options.unicode_normalization,
                UnicodeNormalizationMode::None
            )
            // keyboard_only strips ASCII control chars (except \n \r \t) regardless
            // of remove_control_chars; the fast path must not silently keep them.
            && (!self.options.keyboard_only || text.bytes().all(is_fast_path_safe_ascii_byte))
    }
}

#[cfg(feature = "unorm")]
fn append_normalized_segment(mode: UnicodeNormalizationMode, segment: &str, out: &mut String) {
    match mode {
        UnicodeNormalizationMode::None => out.push_str(segment),
        UnicodeNormalizationMode::NFD => out.extend(segment.nfd()),
        UnicodeNormalizationMode::NFC => out.extend(segment.nfc()),
        UnicodeNormalizationMode::NFKD => out.extend(segment.nfkd()),
        UnicodeNormalizationMode::NFKC => out.extend(segment.nfkc()),
    }
}

#[derive(Debug, Clone)]
/// Summary of cumulative streaming cleanup work.
pub struct StreamSummary {
    /// Aggregated counters over all emitted chunks.
    pub stats: CleaningStats,
    /// Total transformations across all emitted chunks.
    pub changes_made: u64,
}

/// Potential flush boundaries: LF plus U+2028 LINE SEPARATOR and U+2029
/// PARAGRAPH SEPARATOR. The Unicode separators are boundaries only when the
/// configured cleaner folds them to `\n`; otherwise only LF is flushable.
const STREAM_FLUSH_BOUNDARIES: [char; 3] = ['\n', '\u{2028}', '\u{2029}'];

/// Incremental cleaner that processes text in line-delimited chunks.
pub struct StreamCleaner {
    cleaner: TextCleaner,
    buffer: String,
    total_stats: CleaningStats,
    total_changes: u64,
    has_emitted_output: bool,
}

impl StreamCleaner {
    /// Construct a streaming cleaner from options.
    ///
    /// # Arguments
    /// - `options`: Cleaning behavior configuration.
    ///
    /// # Returns
    /// A new [`StreamCleaner`].
    pub fn new(options: CleaningOptions) -> Self {
        Self {
            cleaner: TextCleaner::new(options),
            buffer: String::new(),
            total_stats: CleaningStats::default(),
            total_changes: 0,
            has_emitted_output: false,
        }
    }

    /// Construct a streaming cleaner from an existing [`TextCleaner`].
    ///
    /// # Arguments
    /// - `cleaner`: Preconfigured text cleaner.
    ///
    /// # Returns
    /// A new [`StreamCleaner`].
    pub fn from_cleaner(cleaner: TextCleaner) -> Self {
        Self {
            cleaner,
            buffer: String::new(),
            total_stats: CleaningStats::default(),
            total_changes: 0,
            has_emitted_output: false,
        }
    }

    /// Feed one input chunk and emit cleaned output only after a line boundary
    /// is available. LF is always a boundary; U+2028 LINE SEPARATOR and U+2029
    /// PARAGRAPH SEPARATOR are boundaries when the configured options normalize
    /// them to newlines.
    ///
    /// # Arguments
    /// - `chunk`: Incoming text data.
    /// - `out`: Reusable output buffer.
    ///
    /// # Returns
    /// `Some(CleaningResult)` when at least one complete line was processed,
    /// otherwise `None`.
    ///
    /// # Panics
    /// Panics when normalization is requested but the `unorm` feature is disabled.
    pub fn feed<'out>(
        &mut self,
        chunk: &str,
        out: &'out mut String,
    ) -> Option<CleaningResult<'out>> {
        out.clear();
        if chunk.is_empty() {
            return None;
        }
        self.buffer.push_str(chunk);
        let unicode_separators_are_line_breaks = self.cleaner.options().normalize_spaces
            || self.cleaner.options().normalize_line_endings.is_some();
        let last_boundary = if unicode_separators_are_line_breaks {
            self.buffer.rfind(STREAM_FLUSH_BOUNDARIES)
        } else {
            self.buffer.rfind('\n')
        }?;
        let boundary_len = self.buffer[last_boundary..]
            .chars()
            .next()
            .expect("rfind returned the start of a char")
            .len_utf8();
        let flush_end = last_boundary + boundary_len;
        let to_process = self.buffer[..flush_end].to_owned();
        self.buffer.drain(..flush_end);
        Some(self.process_owned_chunk(to_process, out))
    }

    /// Flush remaining buffered text at end-of-stream.
    ///
    /// # Arguments
    /// - `out`: Reusable output buffer.
    ///
    /// # Returns
    /// Final cleaned chunk when buffered content remains, otherwise `None`.
    pub fn finish<'out>(&mut self, out: &'out mut String) -> Option<CleaningResult<'out>> {
        out.clear();
        if self.buffer.is_empty() {
            return None;
        }
        let remainder = std::mem::take(&mut self.buffer);
        Some(self.process_owned_chunk(remainder, out))
    }

    /// Return cumulative stream statistics.
    ///
    /// # Returns
    /// Aggregate counters and total change count.
    pub fn summary(&self) -> StreamSummary {
        StreamSummary {
            stats: self.total_stats.clone(),
            changes_made: self.total_changes,
        }
    }

    fn process_owned_chunk<'out>(
        &mut self,
        chunk: String,
        out: &'out mut String,
    ) -> CleaningResult<'out> {
        let result = self
            .cleaner
            .try_clean_into_with_context(&chunk, out, self.has_emitted_output)
            .unwrap_or_else(|err| {
                panic!(
                    "StreamCleaner::feed failed: {err}. Enable the 'unorm' feature or use try_clean"
                )
            });
        let emitted = result.text.as_ref();

        self.total_stats.accumulate(&result.stats);
        self.total_changes = self.total_changes.saturating_add(result.changes_made);
        if !emitted.is_empty() {
            self.has_emitted_output = true;
        }

        result
    }
}

#[derive(Clone, Copy)]
struct EmojiClusterContext {
    is_rendered: bool,
}

struct EmojiClassifier {
    emoji: CodePointSetDataBorrowed<'static>,
    emoji_presentation: CodePointSetDataBorrowed<'static>,
    extended_pictographic: CodePointSetDataBorrowed<'static>,
}

impl EmojiClassifier {
    fn new() -> Self {
        Self {
            emoji: CodePointSetData::new::<props::Emoji>(),
            emoji_presentation: CodePointSetData::new::<props::EmojiPresentation>(),
            extended_pictographic: CodePointSetData::new::<props::ExtendedPictographic>(),
        }
    }
}

fn classify_emoji_cluster(grapheme: &str, classifier: &EmojiClassifier) -> EmojiClusterContext {
    let mut has_emoji_presentation = false;
    let mut has_extended_pictographic = false;
    let mut has_emoji = false;
    let mut has_vs16 = false;
    let mut has_zwj = false;
    let mut has_keycap = false;

    for c in grapheme.chars() {
        if classifier.emoji_presentation.contains(c) {
            has_emoji_presentation = true;
        }
        if classifier.extended_pictographic.contains(c) {
            has_extended_pictographic = true;
        }
        if classifier.emoji.contains(c) {
            has_emoji = true;
        }
        match c {
            '\u{FE0F}' => has_vs16 = true,   // Variation Selector-16
            '\u{200D}' => has_zwj = true,    // Zero Width Joiner
            '\u{20E3}' => has_keycap = true, // Combining Enclosing Keycap
            _ => {}
        }
    }

    let is_rendered = has_emoji_presentation
        || has_extended_pictographic
        || (has_emoji && (has_vs16 || has_zwj || has_keycap));

    EmojiClusterContext { is_rendered }
}

fn flush_pending_whitespace(
    out: &mut String,
    pending: &str,
    collapse: bool,
    changes: &mut u64,
    stats: &mut CleaningStats,
) {
    if pending.is_empty() {
        return;
    }
    if collapse {
        // Anything other than a lone space is a rewrite: count the characters
        // removed by the collapse, or 1 when a single tab canonicalizes to a
        // space. ishuman and --exit-code rely on this being non-zero whenever
        // the output differs from the input.
        if pending != " " {
            let removed = (pending.chars().count() as u64).saturating_sub(1).max(1);
            record_change!(*changes, stats, whitespace_collapsed, removed);
        }
        out.push(' ');
    } else {
        out.push_str(pending);
    }
}

#[allow(clippy::too_many_arguments)]
fn flush_or_drop_pending_whitespace(
    out: &mut String,
    pending: &mut String,
    collapse: bool,
    drop_leading: bool,
    emitted_anything: bool,
    changes: &mut u64,
    stats: &mut CleaningStats,
) {
    if pending.is_empty() {
        return;
    }
    if !drop_leading || emitted_anything {
        flush_pending_whitespace(out, pending, collapse, changes, stats);
    }
    pending.clear();
}

#[allow(clippy::too_many_arguments)]
fn finish_pending_whitespace_before_break(
    out: &mut String,
    pending: &mut String,
    cap_next: &mut bool,
    trim: bool,
    collapse: bool,
    changes: &mut u64,
    stats: &mut CleaningStats,
) {
    if trim {
        if !pending.is_empty() {
            record_change!(
                *changes,
                stats,
                trailing_whitespace_removed,
                pending.chars().count()
            );
            pending.clear();
        }
    } else {
        flush_pending_whitespace(out, pending, collapse, changes, stats);
        pending.clear();
    }
    // A line break ends any dropped-cluster whitespace-merge context: leading
    // whitespace on the next line is indentation, not run continuation. This
    // must reset even with nothing pending, or batch mode would cap the next
    // line's indent while streaming (fresh state per line) preserves it.
    *cap_next = false;
}

fn is_disallowed_control(c: char) -> bool {
    is_control_char(c) && !matches!(c, '\n' | '\r' | '\t')
}

fn is_newline_grapheme(g: &str) -> bool {
    matches!(g, "\n" | "\r" | "\r\n")
}

fn is_joiner(c: char) -> bool {
    matches!(c, '\u{200C}' | '\u{200D}')
}

fn is_keyboard_allowed(c: char, extended_keyboard: bool) -> bool {
    is_keyboard_ascii(c) || (extended_keyboard && is_extended_keyboard_char(c))
}

/// ASCII byte that `keyboard_only` leaves unchanged, so an all-ASCII input is
/// safe to return verbatim via the fast path: printable ASCII plus the three
/// retained whitespace controls. Excludes C0 controls and DEL (0x7F).
fn is_fast_path_safe_ascii_byte(b: u8) -> bool {
    matches!(b, b'\n' | b'\r' | b'\t') || (0x20..0x7F).contains(&b)
}

#[derive(Debug, Default, Clone, Copy)]
struct LineEndingCounts {
    crlf: u64,
    cr: u64,
    unicode: u64,
}

impl LineEndingCounts {
    fn total(&self) -> u64 {
        self.crlf + self.cr + self.unicode
    }
}

// ----------------- helpers -----------------

fn to_lf(s: &str) -> (String, LineEndingCounts) {
    // Convert CRLF, CR, NEL (U+0085), LS (U+2028) and PS (U+2029) to LF and track conversions.
    let mut out = String::with_capacity(s.len());
    let mut counts = LineEndingCounts::default();
    let mut it = s.chars().peekable();
    while let Some(c) = it.next() {
        if c == '\r' {
            if matches!(it.peek(), Some('\n')) {
                it.next(); // consume LF
                counts.crlf = counts.crlf.saturating_add(1);
            } else {
                counts.cr = counts.cr.saturating_add(1);
            }
            out.push('\n');
        } else if matches!(c, '\u{0085}' | '\u{2028}' | '\u{2029}') {
            out.push('\n');
            counts.unicode = counts.unicode.saturating_add(1);
        } else {
            out.push(c);
        }
    }
    (out, counts)
}

fn restamp_line_endings_mut(style: LineEndingStyle, text: &mut String) -> u64 {
    let replacement = match style {
        LineEndingStyle::Lf => return 0,
        LineEndingStyle::Crlf => "\r\n",
        LineEndingStyle::Cr => "\r",
    };
    let lf_count = text.as_bytes().iter().filter(|&&b| b == b'\n').count() as u64;
    if lf_count > 0 {
        *text = text.replace('\n', replacement);
    }
    lf_count
}

fn map_dash(c: char) -> Option<char> {
    DASH_MAP.get(&c).copied()
}

fn map_quote(c: char) -> Option<char> {
    QUOTE_MAP.get(&c).copied()
}

#[derive(Debug)]
struct KeyboardAsciiRewrite {
    text: String,
    non_ascii_transliterated: u64,
    non_ascii_removed: u64,
}

fn rewrite_cluster_to_keyboard_ascii(
    cluster: &str,
    policy: NonAsciiPolicy,
    extended_keyboard: bool,
) -> Option<KeyboardAsciiRewrite> {
    let mut out = String::with_capacity(cluster.len());
    let mut non_ascii_transliterated = 0u64;
    let mut non_ascii_removed = 0u64;

    for c in cluster.chars() {
        if is_keyboard_allowed(c, extended_keyboard) {
            out.push(c);
            continue;
        }

        // U+0338 COMBINING LONG SOLIDUS OVERLAY negates the operator it sits
        // on. Decomposed input ("=" + U+0338, the canonical decomposition of
        // `≠`) never reaches compat_override — the ASCII base was already
        // emitted above — so stripping the overlay like any other mark would
        // silently invert the comparison. Negate the emitted tail instead.
        // Drop mode keeps plain mark-stripping, mirroring how it reduces
        // decomposed diacritics to their base letters.
        if c == '\u{0338}'
            && !matches!(policy, NonAsciiPolicy::Drop)
            && negate_relational_tail(&mut out)
        {
            non_ascii_transliterated = non_ascii_transliterated.saturating_add(1);
            continue;
        }

        // Precedence, most-specific first:
        //   1. compat_override  — meaning-preserving ASCII for chars whose NFKD
        //      would silently invert sense (`≠` -> `=`). Runs in BOTH Fold and
        //      Transliterate, *before* NFKD, so the negation is never lost.
        //   2. NFKD compatibility fold — `½`->`1/2`, `™`->`TM`, fullwidth, etc.
        //   3. curated symbol map + scoped deunicode — Transliterate only.
        //   4. drop.
        let mapped = match policy {
            NonAsciiPolicy::Drop => false,
            NonAsciiPolicy::Fold => {
                append_mapping(compat_override(c), &mut out, extended_keyboard)
                    || append_folded_non_ascii(c, &mut out, extended_keyboard)
            }
            NonAsciiPolicy::Transliterate => {
                append_mapping(compat_override(c), &mut out, extended_keyboard)
                    || append_folded_non_ascii(c, &mut out, extended_keyboard)
                    || append_transliterated_non_ascii(c, &mut out, extended_keyboard)
            }
        };

        if mapped {
            non_ascii_transliterated = non_ascii_transliterated.saturating_add(1);
        } else {
            non_ascii_removed = non_ascii_removed.saturating_add(1);
        }
    }

    if out.is_empty() {
        None
    } else {
        Some(KeyboardAsciiRewrite {
            text: out,
            non_ascii_transliterated,
            non_ascii_removed,
        })
    }
}

#[cfg(feature = "unorm")]
fn append_folded_non_ascii(c: char, out: &mut String, extended_keyboard: bool) -> bool {
    let mut added = false;
    let source_is_space = c.is_whitespace();
    for decomposed in c.to_string().nfkd() {
        // Spacing-modifier diacritics (´ ¨ ¯ ¸ ˜ …) decompose to <space + combining
        // mark>. Emitting that leading space turns the glyph into whitespace, which
        // both mistranslates it and breaks idempotence (the space later escapes
        // trailing-whitespace trimming). Suppress decomposition spaces unless the
        // source character was itself whitespace (e.g. NBSP -> space is correct).
        if decomposed == ' ' && !source_is_space {
            continue;
        }
        if is_keyboard_allowed(decomposed, extended_keyboard) {
            out.push(decomposed);
            added = true;
        } else if let Some(mapped) = map_compatibility_ascii(decomposed) {
            out.push(mapped);
            added = true;
        }
    }
    added
}

#[cfg(not(feature = "unorm"))]
fn append_folded_non_ascii(c: char, out: &mut String, _: bool) -> bool {
    if let Some(mapped) = map_compatibility_ascii(c) {
        out.push(mapped);
        true
    } else {
        false
    }
}

fn append_transliterated_non_ascii(c: char, out: &mut String, extended_keyboard: bool) -> bool {
    // Curated, high-quality ASCII first: Latin letters that must be spelled out
    // (ß -> ss), the symbol glyphs LLMs emit constantly (-> for arrows, etc.),
    // and Greek letters used as math symbols (lambda, Delta, ...).
    if let Some(mapping) = explicit_transliteration(c) {
        return append_mapping(Some(mapping), out, extended_keyboard);
    }

    // Long-tail fallback via deunicode, scoped by Unicode properties: Latin
    // script, or script-neutral symbols/punctuation. The scope is deliberate:
    // deunicode romanizes scripts (世 -> "Shi "), which we do NOT want, so
    // letters of concrete scripts are never passed to it and continue to drop.
    // Emoji-property chars are excluded for the same reason: deunicode expands
    // them to English names (✂ -> "scissors"), which is worse than dropping them
    // with the rest of the emoji. Curated entries above still win for the emoji
    // marks we do want (© ® ✓ …). The output is trimmed because deunicode pads
    // some mappings with spaces that would escape trailing-whitespace trimming.
    if is_latin_script(c) || (is_common_symbol_or_punctuation(c) && !is_emoji(c)) {
        if let Some(mapped) = deunicode_char(c) {
            return append_mapping(Some(mapped.trim()), out, extended_keyboard);
        }
    }
    false
}

fn explicit_transliteration(c: char) -> Option<&'static str> {
    transliteration_override(c)
        .or_else(|| symbol_translit(c))
        .or_else(|| greek_translit(c))
}

/// Apply an optional ASCII mapping, returning whether anything was emitted.
/// Every char is funneled through [`append_ascii_mapping`], which discards any
/// non-keyboard output, so a table entry can never break the keyboard-only ASCII
/// invariant even if it is wrong.
fn append_mapping(mapping: Option<&str>, out: &mut String, extended_keyboard: bool) -> bool {
    match mapping {
        Some(text) => {
            let before = out.len();
            append_ascii_mapping(text, out, extended_keyboard);
            out.len() > before
        }
        None => false,
    }
}

/// Greek letters spell out to their English names under `Transliterate`. Greek
/// is the one deliberate exception to the "letter scripts drop" rule: LLM
/// output overwhelmingly uses Greek letters as math/stats symbols ("lambda =
/// 0.5", "Delta x"), where silent deletion destroys meaning. The cost is that
/// actual Greek-language prose becomes concatenated letter names; that trade
/// was made consciously. Other scripts (CJK, Cyrillic, Arabic, ...) still drop.
///
/// The table is generated in `build.rs` from the 24 base letter names: every
/// Greek-script character whose NFKD form reduces to a base letter (accented,
/// polytonic, final/lunate sigma, math symbol variants) maps to that name.
fn greek_translit(c: char) -> Option<&'static str> {
    GREEK_MAP.get(&c).copied()
}

/// Meaning-preserving ASCII for characters whose NFKD decomposition would
/// otherwise *invert* their sense: negated relational operators decompose to the
/// un-negated base plus U+0338 COMBINING LONG SOLIDUS OVERLAY, the overlay is then
/// dropped, and `≠` becomes `=`. Applied before NFKD in every fold/translit mode.
/// Only the three operators whose base is itself ASCII can actually invert today;
/// they are the mandatory members. See `negated_operators_are_not_inverted`.
fn compat_override(c: char) -> Option<&'static str> {
    Some(match c {
        '\u{2260}' => "!=", // ≠ NOT EQUAL TO     (NFKD -> "=")
        '\u{226E}' => "!<", // ≮ NOT LESS-THAN    (NFKD -> "<")
        '\u{226F}' => "!>", // ≯ NOT GREATER-THAN (NFKD -> ">")
        _ => return None,
    })
}

/// Negate the relational tail already emitted into `out`, for a U+0338
/// COMBINING LONG SOLIDUS OVERLAY following its base (decomposed `≠`/`≮`/`≯`
/// and friends). The maximal trailing run of `=`/`<`/`>` must exactly match a
/// known operator so the result agrees with the precomposed mappings: `=` +
/// overlay -> `!=` like `≠`, `<=` (emitted for `≤`) -> `!<=` like `≰`, `===`
/// (emitted for `≡`) -> `!==` like `≢`. A run preceded by `-` is an arrow
/// shaft (`<->`), not a relation, and is left alone. Returns whether the tail
/// was rewritten; on false the overlay falls through to normal mark handling.
fn negate_relational_tail(out: &mut String) -> bool {
    const NEGATIONS: &[(&str, &str)] = &[
        ("===", "!=="),
        ("<=", "!<="),
        (">=", "!>="),
        ("=", "!="),
        ("<", "!<"),
        (">", "!>"),
    ];
    let Some(run_start) = out
        .char_indices()
        .rev()
        .take_while(|&(_, c)| matches!(c, '=' | '<' | '>'))
        .last()
        .map(|(index, _)| index)
    else {
        return false;
    };
    if out[..run_start].ends_with('-') {
        return false;
    }
    let Some(&(_, negated)) = NEGATIONS.iter().find(|&&(run, _)| run == &out[run_start..]) else {
        return false;
    };
    out.truncate(run_start);
    out.push_str(negated);
    true
}

/// Curated symbol -> ASCII overrides for glyphs where the automatic layers get
/// it wrong. This table is deliberately minimal; most symbols are handled
/// without it. Do NOT add an entry unless BOTH automatic layers fail:
///
/// - NFKD runs first (`™`->`TM`, `½`->`1/2`) — an entry it covers is dead code;
/// - the `deunicode` fallback runs after — an entry returning the same string
///   deunicode already gives is redundant (verify with `deunicode_char`).
///
/// Legitimate reasons to be here: deunicode is lossy for the char (`→`->`-`,
/// `≔`->`=`, `£`->`PS`), or the char is Emoji-classified so the fallback never
/// sees it (`©`, `✅`) yet its meaning is worth keeping.
fn symbol_translit(c: char) -> Option<&'static str> {
    Some(match c {
        // --- Arrows: deunicode collapses direction/shaft (`→`->`-`, `⇒`->`=`).
        //     Double arrows use `==>` (not `=>`) so they never collide with the
        //     `<=`/`>=` output of the relational operators. ---
        '\u{2192}' => "->",
        '\u{2190}' => "<-",
        '\u{2194}' => "<->",
        '\u{2191}' => "^",
        '\u{2193}' => "v",
        '\u{2195}' => "^v",
        '\u{21D2}' => "==>",
        '\u{21D0}' => "<==",
        '\u{21D4}' => "<==>",
        '\u{21A6}' => "|->",
        '\u{21B5}' => "<-'",
        '\u{23CE}' => "<-'",
        '\u{27F6}' => "-->",
        '\u{27F5}' => "<--",
        '\u{27F7}' => "<-->",
        '\u{27F9}' => "==>",
        '\u{27F8}' => "<==",
        '\u{27FA}' => "<==>",
        '\u{2794}' => "->",
        '\u{2799}' => "->",
        '\u{279C}' => "->",
        '\u{27A4}' => "->",
        '\u{21CC}' => "<=>", // chemical equilibrium (deunicode gives bare "=")
        '\u{21CB}' => "<=>",
        // --- Math operators deunicode flattens to a bare sign or single letter ---
        '\u{2254}' => ":=", // deunicode drops the colon ("=")
        '\u{2255}' => "=:",
        '\u{2218}' => "o",   // function composition (deunicode gives "*")
        '\u{22EE}' => "...", // vertical ellipsis (deunicode gives "|")
        '\u{2243}' => "~=",
        '\u{2248}' => "~=",
        '\u{2261}' => "===",
        '\u{00B1}' => "+/-", // deunicode gives "+-"
        '\u{2213}' => "-/+",
        '\u{2211}' => "sum", // deunicode gives "S"
        '\u{220F}' => "prod",
        '\u{222B}' => "int",
        '\u{2207}' => "grad",
        '\u{2206}' => "delta",
        '\u{2205}' => "{}",
        '\u{2208}' => "in",
        '\u{220B}' => "ni",
        '\u{2227}' => "and",
        '\u{2228}' => "or",
        '\u{2200}' => "forall",
        '\u{2203}' => "exists",
        // --- Negated operators: deunicode strips the negation entirely ---
        '\u{2270}' => "!<=",
        '\u{2271}' => "!>=",
        '\u{2209}' => "!in",
        '\u{220C}' => "!ni",
        '\u{2224}' => "!|",
        '\u{2226}' => "!||",
        '\u{2241}' => "!~",
        '\u{2244}' => "!~=",
        '\u{2249}' => "!~~",
        '\u{2262}' => "!==",
        // --- Bullets read as list markers, not asterisks ---
        '\u{2022}' => "-",
        '\u{2023}' => "-",
        '\u{2043}' => "-",
        '\u{2027}' => "-",
        '\u{25E6}' => "o",
        '\u{25CB}' => "o",
        // --- Latin-1 currency/marks where deunicode is wrong ---
        '\u{00A2}' => "c",    // deunicode: "C/"
        '\u{00A3}' => "GBP",  // deunicode: "PS"
        '\u{00A5}' => "JPY",  // deunicode: "Y="
        '\u{00A7}' => "S",    // deunicode: "SS"
        '\u{2030}' => "0/00", // per mille (deunicode gives "%0")
        // --- Shapes/checks where deunicode is lossy or the char is emoji ---
        '\u{25A1}' => "[ ]",
        '\u{25B6}' => ">", // emoji-classified, so the fallback never sees it
        '\u{25C0}' => "<", // emoji-classified
        '\u{25BC}' => "v", // lowercase for symmetry with the ↓/▲ family
        '\u{25C6}' => "<>",
        '\u{25C7}' => "<>",
        '\u{2713}' => "[x]", // deunicode: "OK"
        '\u{2714}' => "[x]", // deunicode: "checkmark"
        '\u{2717}' => "x",   // ballot x: "wrong/failed", not "unchecked"
        '\u{2718}' => "x",
        '\u{2611}' => "[x]", // emoji-classified
        '\u{25AA}' => "-",   // small squares used as list bullets (emoji-classified)
        '\u{25AB}' => "-",
        // --- Meaning-bearing emoji marks: Emoji-classified, so they would drop
        //     under EmojiPolicy::Drop, but they carry pass/fail/alert semantics
        //     LLMs rely on. Curated entries win over the emoji drop; pictorial
        //     emoji still drop. EmojiPolicy::Keep still keeps these as-is. ---
        '\u{2705}' => "[x]", // white heavy check mark
        '\u{274C}' => "x",   // cross mark: "failed", so not "[ ]" (= "not done")
        '\u{274E}' => "x",   // negative squared cross mark
        '\u{2716}' => "x",   // heavy multiplication x
        '\u{26A0}' => "[!]", // warning sign
        '\u{2757}' => "!",
        '\u{2755}' => "!",
        '\u{2753}' => "?",
        '\u{2754}' => "?",
        '\u{2B50}' => "*", // star ratings
        // --- Letterlike marks: emoji-classified or better than the fallback ---
        '\u{00A9}' => "(c)", // emoji-classified
        '\u{00AE}' => "(r)", // emoji-classified
        '\u{00B5}' => "u",   // micro sign is GC=Ll, so no gate reaches it
        '\u{2126}' => "ohm", // else GREEK_MAP would give "Omega"
        // --- Spacing-modifier diacritics where the fallback misreads them ---
        '\u{02DA}' => "deg", // ˚ RING ABOVE, used as a degree sign (deunicode: "@")
        '\u{02BB}' => "'",   // ʻ okina (deunicode gives a backtick, a markdown hazard)
        // --- Technical / keyboard keys (deunicode gives "#", "*", "<", ...) ---
        '\u{2318}' => "Cmd",
        '\u{2325}' => "Opt",
        '\u{2303}' => "Ctrl",
        '\u{21E7}' => "Shift",
        '\u{2387}' => "Alt",
        '\u{238B}' => "Esc",
        '\u{232B}' => "Bksp",
        '\u{2326}' => "Del",
        '\u{23CF}' => "Eject", // emoji-classified, so the fallback never sees it
        _ => return None,
    })
}

fn append_ascii_mapping(mapped: &str, out: &mut String, extended_keyboard: bool) {
    for c in mapped.chars() {
        if is_keyboard_allowed(c, extended_keyboard) {
            out.push(c);
        } else if let Some(compat) = map_compatibility_ascii(c) {
            out.push(compat);
        }
    }
}

fn transliteration_override(c: char) -> Option<&'static str> {
    match c {
        'ß' => Some("ss"),
        '' => Some("SS"),
        _ => None,
    }
}

fn map_compatibility_ascii(c: char) -> Option<char> {
    match c {
        FRACTION_SLASH => Some('/'),
        _ => None,
    }
}

/// Convenience: clean with default options.
///
/// # Arguments
/// - `text`: Input text to clean.
///
/// # Returns
/// Cleaned output and statistics.
///
/// The default preset emits keyboard-safe output. Non-ASCII text is
/// normalized/folded and transliterated when possible (for example
/// `"Café"` -> `"Cafe"`, `"Straße"` -> `"Strasse"`), while characters
/// with no feasible ASCII mapping are removed.
///
/// # Errors
/// This infallible wrapper does not return errors; construct a
/// [`TextCleaner`] and call [`TextCleaner::try_clean`] for fallible behavior.
///
/// # Panics
/// Panics when normalization is requested but the `unorm` feature is disabled.
pub fn clean(text: &str) -> CleaningResult<'_> {
    TextCleaner::new(CleaningOptions::default()).clean(text)
}

/// Convenience: clean with the humanize preset.
///
/// # Arguments
/// - `text`: Input text to clean.
///
/// # Returns
/// Cleaned output and statistics.
///
/// # Errors
/// This infallible wrapper does not return errors; construct a
/// [`TextCleaner`] and call [`TextCleaner::try_clean`] for error handling.
///
/// # Panics
/// Panics when normalization is requested but the `unorm` feature is disabled.
pub fn humanize(text: &str) -> CleaningResult<'_> {
    TextCleaner::new(CleaningOptions::humanize()).clean(text)
}

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

    #[test]
    fn removes_hidden() {
        let c = TextCleaner::new(CleaningOptions {
            remove_hidden: true,
            ..CleaningOptions::minimal()
        });
        let out = c.clean("Hello\u{200B}World");
        assert_eq!(out.text, "HelloWorld");
        #[cfg(feature = "stats")]
        assert_eq!(out.stats.hidden_chars_removed, 1);
    }

    #[test]
    fn removes_mongolian_vowel_separator() {
        let c = TextCleaner::new(CleaningOptions::default());
        let out = c.clean("Hello\u{180E}World");
        assert_eq!(out.text, "HelloWorld");
        #[cfg(feature = "stats")]
        assert!(out.stats.hidden_chars_removed >= 1);
    }

    #[test]
    fn normalizes_spaces_and_dashes_quotes_and_ellipsis() {
        let c = TextCleaner::new(CleaningOptions::default());
        let out = c.clean("\u{201C}Hi\u{201D}\u{00A0}\u{2014} ok…");
        assert_eq!(out.text, "\"Hi\" - ok...");
        #[cfg(feature = "stats")]
        {
            assert!(out.stats.spaces_normalized >= 1);
            assert!(out.stats.dashes_normalized >= 1);
            assert!(out.stats.quotes_normalized >= 2);
            assert!(out.stats.other_normalized >= 1);
        }
    }

    #[test]
    fn trims_trailing_ws() {
        let c = TextCleaner::new(CleaningOptions {
            remove_trailing_whitespace: true,
            ..CleaningOptions::minimal()
        });
        let out = c.clean("a  \n b\t\t\n");
        assert_eq!(out.text, "a\n b\n");
        #[cfg(feature = "stats")]
        assert!(out.stats.trailing_whitespace_removed >= 3);
    }

    #[test]
    fn collapses_ws() {
        let c = TextCleaner::new(CleaningOptions {
            collapse_whitespace: true,
            ..CleaningOptions::minimal()
        });
        let out = c.clean("a    b\t\tc");
        assert_eq!(out.text, "a b c");
        assert!(out.changes_made > 0);
        #[cfg(feature = "stats")]
        assert_eq!(out.stats.whitespace_collapsed, 4); // 3 spaces + 1 tab removed
    }

    #[test]
    fn keyboard_only_with_emoji_policy() {
        let c = TextCleaner::new(CleaningOptions {
            keyboard_only: true,
            emoji_policy: EmojiPolicy::Keep,
            ..CleaningOptions::minimal()
        });
        let out = c.clean("Hello😀世界");
        assert_eq!(out.text, "Hello😀");
        #[cfg(feature = "stats")]
        assert!(out.stats.non_keyboard_removed >= 2);
    }

    #[test]
    fn normalize_eol_crlf_to_lf_and_back() {
        let c = TextCleaner::new(CleaningOptions {
            normalize_line_endings: Some(LineEndingStyle::Lf),
            ..CleaningOptions::minimal()
        });
        let out = c.clean("a\r\nb\rc\u{0085}");
        assert_eq!(out.text, "a\nb\nc\n");
        #[cfg(feature = "stats")]
        assert!(out.stats.line_endings_normalized >= 3);
    }

    #[test]
    fn normalizes_unicode_line_separators() {
        let mut options = CleaningOptions::minimal();
        options.normalize_line_endings = Some(LineEndingStyle::Lf);
        let c = TextCleaner::new(options);
        let out = c.clean("a\u{2028}b\u{2029}c");
        assert_eq!(out.text, "a\nb\nc");
        #[cfg(feature = "stats")]
        assert_eq!(out.stats.line_endings_normalized, 2);
    }

    #[test]
    fn restamping_counts_changes() {
        let mut options = CleaningOptions::minimal();
        options.normalize_line_endings = Some(LineEndingStyle::Crlf);
        let c = TextCleaner::new(options);
        let out = c.clean("a\nb\n");
        assert_eq!(out.text, "a\r\nb\r\n");
        #[cfg(feature = "stats")]
        assert_eq!(out.stats.line_endings_normalized, 2);
    }

    #[test]
    fn default_cleaning_matches_keyboard_equivalent() {
        let out = clean("“Hello—world…”\u{00A0}😀");
        assert_eq!(out.text, "\"Hello-world...\"");
        #[cfg(feature = "stats")]
        {
            assert_eq!(out.stats.quotes_normalized, 2);
            assert_eq!(out.stats.dashes_normalized, 1);
            assert_eq!(out.stats.other_normalized, 1);
            assert_eq!(out.stats.spaces_normalized, 1);
            assert_eq!(out.stats.emojis_dropped, 1);
        }
        assert_eq!(out.changes_made, 7);
    }

    #[test]
    fn keyboard_only_drops_non_ascii_and_emoji() {
        let cleaner = TextCleaner::new(CleaningOptions {
            keyboard_only: true,
            ..CleaningOptions::default()
        });
        let out = cleaner.clean("Ascii😀世界");
        assert_eq!(out.text, "Ascii");
        #[cfg(feature = "stats")]
        {
            assert_eq!(out.stats.emojis_dropped, 1);
            assert!(out.stats.non_keyboard_removed >= 2);
        }
    }

    #[test]
    fn keyboard_only_folds_latin_diacritics_to_ascii() {
        let cleaner = TextCleaner::new(CleaningOptions::default());
        let out = cleaner.clean("Caf\u{00E9} d\u{00E9}j\u{00E0} vu");
        assert_eq!(out.text, "Cafe deja vu");
        #[cfg(feature = "stats")]
        assert!(out.stats.non_keyboard_transliterated >= 3);
    }

    #[test]
    fn transliterates_non_decomposing_latin_letters() {
        let cleaner = TextCleaner::new(CleaningOptions::default());
        let out = cleaner.clean("Stra\u{00DF}e \u{00C6}sir \u{00F8}l \u{0153}uvre");
        assert_eq!(out.text, "Strasse AEsir ol oeuvre");
        #[cfg(feature = "stats")]
        assert!(out.stats.non_keyboard_transliterated >= 4);
    }

    #[test]
    fn non_ascii_policy_modes_control_behavior() {
        let drop = TextCleaner::new(
            CleaningOptions::builder()
                .non_ascii_policy(NonAsciiPolicy::Drop)
                .build(),
        )
        .clean("Stra\u{00DF}e \u{00BD} \u{2122}");
        assert_eq!(drop.text, "Strae");

        // ½ -> "1/2" and ™ -> "TM" come from NFKD, which needs `unorm`.
        #[cfg(feature = "unorm")]
        {
            let fold = TextCleaner::new(
                CleaningOptions::builder()
                    .non_ascii_policy(NonAsciiPolicy::Fold)
                    .build(),
            )
            .clean("Stra\u{00DF}e \u{00BD} \u{2122}");
            assert_eq!(fold.text, "Strae 1/2 TM");

            let transliterate = TextCleaner::new(
                CleaningOptions::builder()
                    .non_ascii_policy(NonAsciiPolicy::Transliterate)
                    .build(),
            )
            .clean("Stra\u{00DF}e \u{00BD} \u{2122}");
            assert_eq!(transliterate.text, "Strasse 1/2 TM");
            #[cfg(feature = "stats")]
            assert!(transliterate.stats.non_keyboard_transliterated >= 3);
        }
    }

    #[test]
    fn extended_keyboard_allowlist_can_preserve_curated_symbols() {
        let default = TextCleaner::new(
            CleaningOptions::builder()
                .non_ascii_policy(NonAsciiPolicy::Drop)
                .build(),
        )
        .clean("€ and ™");
        assert_eq!(default.text, "and");

        let extended = TextCleaner::new(
            CleaningOptions::builder()
                .extended_keyboard(true)
                .non_ascii_policy(NonAsciiPolicy::Drop)
                .build(),
        )
        .clean("€ and ™");
        assert_eq!(extended.text, "€ and");
    }

    #[test]
    fn preserve_joiners_toggle_controls_zwj_zwnj_retention() {
        let text = "می\u{200C}خواهم";
        let default =
            TextCleaner::new(CleaningOptions::builder().keyboard_only(false).build()).clean(text);
        assert!(!default.text.contains('\u{200C}'));

        let preserved = TextCleaner::new(
            CleaningOptions::builder()
                .keyboard_only(false)
                .preserve_joiners(true)
                .build(),
        )
        .clean(text);
        assert!(preserved.text.contains('\u{200C}'));
    }

    #[test]
    fn ts_whitespace_scenarios() {
        let input = "Hello\u{200B}\u{00A0}World!  ";

        let cleaner = TextCleaner::new(CleaningOptions::default());
        let out = cleaner.clean(input);
        assert_eq!(out.text, "Hello World!");
        assert_eq!(out.changes_made, 4);

        let cleaner = TextCleaner::new(CleaningOptions {
            remove_trailing_whitespace: false,
            ..CleaningOptions::default()
        });
        let out = cleaner.clean(input);
        assert_eq!(out.text, "Hello World!  ");
        assert_eq!(out.changes_made, 2);

        let cleaner = TextCleaner::new(CleaningOptions {
            remove_hidden: false,
            keyboard_only: false,
            ..CleaningOptions::default()
        });
        let out = cleaner.clean(input);
        assert_eq!(out.text, "Hello\u{200B} World!");
        assert_eq!(out.changes_made, 3);

        let cleaner = TextCleaner::new(CleaningOptions {
            normalize_spaces: false,
            keyboard_only: false,
            ..CleaningOptions::default()
        });
        let out = cleaner.clean(input);
        assert_eq!(out.text, "Hello\u{00A0}World!");
        assert_eq!(out.changes_made, 3);
    }

    #[cfg(feature = "security")]
    #[test]
    fn strips_bidi_controls_when_enabled() {
        let options = CleaningOptions {
            strip_bidi_controls: true,
            ..CleaningOptions::default()
        };
        let cleaner = TextCleaner::new(options);
        let out = cleaner.clean("\u{202E}ab\u{202C}c");
        assert_eq!(out.text, "abc");
        assert!(out.changes_made >= 2);
        #[cfg(feature = "stats")]
        {
            assert!(out.stats.bidi_controls_removed >= 2);
        }
    }

    #[test]
    fn ts_dashes_case() {
        let cleaner = TextCleaner::new(CleaningOptions::default());
        let out = cleaner.clean("I — super — man – 💪");
        assert_eq!(out.text, "I - super - man -");
        #[cfg(feature = "stats")]
        {
            assert_eq!(out.stats.dashes_normalized, 3);
            assert_eq!(out.stats.emojis_dropped, 1);
        }
        assert_eq!(out.changes_made, 5);
    }

    #[test]
    fn ts_quotes_case() {
        let cleaner = TextCleaner::new(CleaningOptions::default());
        let out = cleaner.clean("Angular “quote” «marks» looks„ like Christmas «« tree");
        assert_eq!(
            out.text,
            "Angular \"quote\" \"marks\" looks\" like Christmas \"\" tree"
        );
        #[cfg(feature = "stats")]
        assert_eq!(out.stats.quotes_normalized, 7);
        assert_eq!(out.changes_made, 7);
    }

    #[test]
    fn maps_additional_quotes_and_primes() {
        let cleaner = TextCleaner::new(CleaningOptions::default());
        let out = cleaner.clean("‹left› ‟double‟ ′prime′ ″double″");
        assert_eq!(out.text, "'left' \"double\" 'prime' \"double\"");
        #[cfg(feature = "stats")]
        assert!(out.stats.quotes_normalized >= 6);
    }

    #[test]
    fn fraction_slash_maps_to_ascii() {
        let cleaner = TextCleaner::new(CleaningOptions::default());
        let out = cleaner.clean("1\u{2044}2");
        assert_eq!(out.text, "1/2");
        #[cfg(feature = "stats")]
        assert_eq!(out.stats.other_normalized, 1);
    }

    #[test]
    fn keeps_variation_selector_for_emoji() {
        let cleaner = TextCleaner::new(CleaningOptions {
            keyboard_only: false,
            ..CleaningOptions::default()
        });
        let out = cleaner.clean("👍\u{FE0F}");
        assert_eq!(out.text, "👍\u{FE0F}");
        assert_eq!(out.stats.hidden_chars_removed, 0);
    }

    #[test]
    fn drops_emoji_sequence_when_policy_drop() {
        let cleaner = TextCleaner::new(CleaningOptions {
            keyboard_only: true,
            emoji_policy: EmojiPolicy::Drop,
            ..CleaningOptions::default()
        });
        let out = cleaner.clean("👍\u{FE0F}");
        assert_eq!(out.text, "");
        #[cfg(feature = "stats")]
        assert_eq!(out.stats.emojis_dropped, 1);
    }

    // `preset_fields_match_contract` below asserts each preset as a delta
    // against `CleaningOptions::default()`, so it can't catch drift in
    // `default()` itself. This test pins every default field absolutely,
    // with no `..` spread — together the two tests cover each preset's
    // full resolved value.
    #[test]
    fn default_options_match_contract() {
        assert_eq!(
            CleaningOptions::default(),
            CleaningOptions {
                remove_hidden: true,
                remove_trailing_whitespace: true,
                normalize_spaces: true,
                normalize_dashes: true,
                normalize_quotes: true,
                normalize_other: true,
                keyboard_only: true,
                extended_keyboard: false,
                emoji_policy: EmojiPolicy::Drop,
                non_ascii_policy: NonAsciiPolicy::Transliterate,
                preserve_joiners: false,
                remove_control_chars: true,
                collapse_whitespace: false,
                normalize_line_endings: None,
                unicode_normalization: UnicodeNormalizationMode::None,
                strip_bidi_controls: false,
            }
        );
    }

    #[test]
    fn preset_fields_match_contract() {
        assert_eq!(
            CleaningOptions::minimal(),
            CleaningOptions {
                remove_trailing_whitespace: false,
                normalize_spaces: false,
                normalize_dashes: false,
                normalize_quotes: false,
                normalize_other: false,
                keyboard_only: false,
                remove_control_chars: false,
                ..CleaningOptions::default()
            }
        );
        assert_eq!(
            CleaningOptions::balanced(),
            CleaningOptions {
                keyboard_only: false,
                unicode_normalization: UnicodeNormalizationMode::NFC,
                ..CleaningOptions::default()
            }
        );
        assert_eq!(
            CleaningOptions::humanize(),
            CleaningOptions {
                keyboard_only: false,
                collapse_whitespace: true,
                unicode_normalization: UnicodeNormalizationMode::NFKC,
                ..CleaningOptions::default()
            }
        );
        assert_eq!(
            CleaningOptions::aggressive(),
            CleaningOptions {
                collapse_whitespace: true,
                normalize_line_endings: Some(LineEndingStyle::Lf),
                unicode_normalization: UnicodeNormalizationMode::NFKC,
                strip_bidi_controls: true,
                ..CleaningOptions::default()
            }
        );
        assert_eq!(
            CleaningOptions::code_safe(),
            CleaningOptions {
                normalize_other: false,
                keyboard_only: false,
                emoji_policy: EmojiPolicy::Keep,
                preserve_joiners: true,
                ..CleaningOptions::default()
            }
        );
    }

    #[test]
    fn code_safe_normalizes_quotes_and_dashes_but_keeps_glyphs() {
        let c = TextCleaner::new(CleaningOptions::code_safe());
        // Typographic quotes/dashes are LLM signatures and must normalize.
        assert_eq!(
            c.clean("\u{201C}quoted\u{201D} \u{2014} text").text,
            "\"quoted\" - text"
        );
        // Semantic glyphs, ellipses, and emoji stay untouched.
        let preserved = "\u{251C}\u{2500}\u{2500} src/ \u{2026} \u{1F600}";
        assert_eq!(c.clean(preserved).text, preserved);
    }

    // ---- F2: negation must never be inverted ----

    #[test]
    fn negated_operators_are_not_inverted() {
        let c = TextCleaner::new(CleaningOptions::default());
        // The three operators whose ASCII base survives NFKD and would otherwise
        // flip; output must carry the negation, never the bare base operator.
        assert_eq!(c.clean("a \u{2260} b").text, "a != b"); // was "a = b"
        assert_eq!(c.clean("a \u{226E} b").text, "a !< b"); // was "a < b"
        assert_eq!(c.clean("a \u{226F} b").text, "a !> b"); // was "a > b"
        assert_ne!(c.clean("\u{2260}").text, "=");
    }

    #[test]
    fn fold_mode_also_preserves_negation() {
        let fold = TextCleaner::new(
            CleaningOptions::builder()
                .non_ascii_policy(NonAsciiPolicy::Fold)
                .build(),
        );
        // compat_override runs before NFKD in Fold too, so ≠ stays "!=", not "=".
        assert_eq!(fold.clean("a \u{2260} b").text, "a != b");
    }

    #[test]
    fn decomposed_negated_operators_are_not_inverted() {
        // ASCII base + U+0338 is the canonical decomposition of ≠/≮/≯. The
        // base is keyboard-allowed and already emitted when the overlay is
        // seen, so it must negate the emitted tail, not strip like other marks.
        let c = TextCleaner::new(CleaningOptions::default());
        assert_eq!(c.clean("a =\u{0338} b").text, "a != b"); // was "a = b"
        assert_eq!(c.clean("a <\u{0338} b").text, "a !< b");
        assert_eq!(c.clean("a >\u{0338} b").text, "a !> b");
        #[cfg(feature = "stats")]
        assert!(c.clean("a =\u{0338} b").stats.non_keyboard_transliterated >= 1);

        // Tails emitted by earlier mappings negate to the same ASCII as the
        // precomposed forms: ≤ + overlay is decomposed ≰ ("!<="), ≡ + overlay
        // is decomposed ≢ ("!==").
        assert_eq!(c.clean("a \u{2264}\u{0338} b").text, "a !<= b");
        assert_eq!(c.clean("a \u{2261}\u{0338} b").text, "a !== b");

        // Fold preserves negation too, matching the precomposed compat_override.
        let fold = TextCleaner::new(
            CleaningOptions::builder()
                .non_ascii_policy(NonAsciiPolicy::Fold)
                .build(),
        );
        assert_eq!(fold.clean("a =\u{0338} b").text, "a != b");

        // Drop keeps plain mark-stripping by design (the same rule that
        // reduces decomposed diacritics to their base letters).
        let drop = TextCleaner::new(
            CleaningOptions::builder()
                .non_ascii_policy(NonAsciiPolicy::Drop)
                .build(),
        );
        assert_eq!(drop.clean("a =\u{0338} b").text, "a = b");

        // Non-relational tails are untouched; the overlay strips as usual.
        assert_eq!(c.clean("b\u{0338}").text, "b");
        assert_eq!(c.clean("a \u{2194}\u{0338} b").text, "a <-> b"); // arrow shaft, not a relation
    }

    #[cfg(feature = "unorm")]
    #[test]
    fn source_sensitive_mappings_precede_unicode_normalization() {
        let nfd = TextCleaner::new(
            CleaningOptions::builder()
                .unicode_normalization(UnicodeNormalizationMode::NFD)
                .non_ascii_policy(NonAsciiPolicy::Fold)
                .build(),
        );
        assert_eq!(nfd.clean("a \u{2260} b").text, "a != b");

        let aggressive = TextCleaner::new(CleaningOptions::aggressive());
        let out = aggressive.clean("\u{2126} \u{00B5} \u{03A9} \u{03BC}");
        assert_eq!(out.text, "ohm u Omega mu");
        #[cfg(feature = "stats")]
        assert_eq!(out.stats.non_keyboard_transliterated, 4);
    }

    // ---- F3: symbols transliterate instead of being deleted ----

    #[test]
    fn arrows_transliterate_to_ascii() {
        let c = TextCleaner::new(CleaningOptions::default());
        assert_eq!(c.clean("a \u{2192} b").text, "a -> b"); //        assert_eq!(c.clean("a \u{2190} b").text, "a <- b"); //        assert_eq!(c.clean("a \u{2194} b").text, "a <-> b"); //        assert_eq!(c.clean("a \u{21D2} b").text, "a ==> b"); //        assert_eq!(c.clean("a \u{27F6} b").text, "a --> b"); // ⟶ (long)
    }

    #[test]
    fn math_and_relational_operators_transliterate() {
        let c = TextCleaner::new(CleaningOptions::default());
        assert_eq!(c.clean("a \u{2264} b").text, "a <= b"); //        assert_eq!(c.clean("a \u{2265} b").text, "a >= b"); //        assert_eq!(c.clean("a \u{2248} b").text, "a ~= b"); //        assert_eq!(c.clean("5 \u{00B1} 1").text, "5 +/- 1"); // ±
    }

    #[test]
    fn bullets_geometric_and_marks_transliterate() {
        let c = TextCleaner::new(CleaningOptions::default());
        assert_eq!(c.clean("\u{2022} item").text, "- item"); //        assert_eq!(c.clean("\u{2605} star").text, "* star"); //        assert_eq!(c.clean("\u{00A9} 2026").text, "(c) 2026"); // © (was dropped as "emoji")
        assert_eq!(c.clean("Acme\u{00AE}").text, "Acme(r)"); // ®
    }

    #[test]
    fn long_tail_symbols_use_deunicode_fallback() {
        // Box-drawing is not in the curated table; it reaches deunicode via the
        // widened symbol gate (─ -> "-", │ -> "|", ┌ -> "+").
        let c = TextCleaner::new(CleaningOptions::default());
        assert_eq!(c.clean("\u{2500}\u{2502}\u{250C}").text, "-|+");
    }

    #[test]
    fn scripts_still_drop_and_are_not_romanized() {
        // The symbol gate must not enable deunicode for letter scripts. Greek is
        // the one deliberate exception (spelled out; see greek_letters tests).
        let c = TextCleaner::new(CleaningOptions::default());
        assert_eq!(c.clean("ok \u{4E16}\u{754C}").text, "ok"); // 世界 dropped, not "Shi Jie"
        assert_eq!(c.clean("ok \u{0430}\u{0431}").text, "ok"); // Cyrillic dropped
        assert_eq!(c.clean("ok \u{0645}\u{0631}").text, "ok"); // Arabic dropped
    }

    // ---- Greek letters spell out to names (math-symbol usage) ----

    #[test]
    fn greek_letters_spell_out_to_names() {
        let c = TextCleaner::new(CleaningOptions::default());
        assert_eq!(c.clean("\u{03BB} = 0.5").text, "lambda = 0.5");
        assert_eq!(
            c.clean("\u{0394}x \u{2264} \u{03B5}").text,
            "Deltax <= epsilon"
        );
        assert_eq!(c.clean("\u{03C0} \u{2248} 3.14").text, "pi ~= 3.14");
        assert_eq!(c.clean("\u{03A3} over \u{03C3}").text, "Sigma over sigma");
        assert_eq!(c.clean("\u{03D5}").text, "phi"); // math phi symbol variant
        assert_eq!(c.clean("\u{03AD}").text, "epsilon"); // monotonic accented
        assert_eq!(c.clean("\u{1D6FC} = 1").text, "alpha = 1"); // math italic alpha
    }

    // ---- Meaning-bearing emoji marks transliterate; pictures still drop ----

    #[test]
    fn semantic_emoji_marks_transliterate() {
        let c = TextCleaner::new(CleaningOptions::default());
        assert_eq!(c.clean("\u{2705} tests pass").text, "[x] tests pass");
        // Cross marks mean "failed", not "unchecked": `x`, never `[ ]`.
        assert_eq!(c.clean("\u{274C} build fails").text, "x build fails");
        assert_eq!(c.clean("\u{2717} wrong").text, "x wrong");
        assert_eq!(c.clean("\u{26A0}\u{FE0F} careful").text, "[!] careful"); // with VS16
        assert_eq!(c.clean("\u{2B50}\u{2B50}\u{2B50}").text, "***");
        assert_eq!(c.clean("done\u{2757}").text, "done!");
        // Pictorial emoji still drop.
        assert_eq!(c.clean("ok \u{1F44D}\u{2702}").text, "ok");
    }

    // ---- Latin-1 punctuation / currency (below the block gates) ----

    #[test]
    fn latin1_punctuation_and_currency_transliterate() {
        let c = TextCleaner::new(CleaningOptions::default());
        assert_eq!(c.clean("50\u{00A2}").text, "50c");
        assert_eq!(c.clean("\u{00A3}5").text, "GBP5");
        assert_eq!(c.clean("\u{00A5}100").text, "JPY100");
        assert_eq!(c.clean("a\u{00A6}b").text, "a|b");
        assert_eq!(c.clean("\u{00A7} 230").text, "S 230");
        assert_eq!(c.clean("\u{00B6} 4").text, "P 4");
    }

    // ---- Math extras where deunicode alone was wrong or absent ----

    #[test]
    fn math_extras_transliterate() {
        let c = TextCleaner::new(CleaningOptions::default());
        assert_eq!(c.clean("x \u{2254} 5").text, "x := 5"); // ≔ (deunicode: "=")
        assert_eq!(c.clean("A \u{21CC} B").text, "A <=> B"); // ⇌ (deunicode: "=")
        assert_eq!(c.clean("f \u{2218} g").text, "f o g"); // ∘ (deunicode: "*")
        assert_eq!(c.clean("a \u{226A} b").text, "a << b"); // ≪ via gate fallback
        assert_eq!(c.clean("\u{27E8}x, y\u{27E9}").text, "<x, y>"); // ⟨⟩ via gate
        assert_eq!(c.clean("5\u{2030}").text, "50/00"); // per mille
    }

    // ---- Phonetic Latin and modifier apostrophes ----

    #[test]
    fn phonetic_latin_and_modifier_apostrophes() {
        let c = TextCleaner::new(CleaningOptions::default());
        assert_eq!(c.clean("don\u{02BC}t").text, "don't"); // modifier apostrophe
        assert_eq!(c.clean("Hawai\u{02BB}i").text, "Hawai'i"); // okina, not a backtick
        assert_eq!(c.clean("37\u{02DA}C").text, "37degC"); // spacing ring as degree
        assert_eq!(c.clean("\u{0259}").text, "@"); // IPA schwa, SAMPA-style
    }

    // ---- F1: ASCII fast path must respect keyboard_only ----

    #[test]
    fn fast_path_respects_keyboard_only() {
        // minimal() leaves trim/collapse/control off, which is what makes the
        // all-ASCII fast path reachable; keyboard_only must still strip C0/DEL.
        let c = TextCleaner::new(CleaningOptions {
            keyboard_only: true,
            ..CleaningOptions::minimal()
        });
        let out = c.clean("a\u{0001}b\u{007F}c");
        assert_eq!(out.text, "abc");
    }

    #[test]
    fn ascii_fast_path_borrows_input() {
        let cleaner = TextCleaner::new(CleaningOptions::minimal());
        let output = cleaner.clean("plain ASCII");
        assert!(matches!(output.text, Cow::Borrowed("plain ASCII")));
        // Tabs are fast-path safe: the full pipeline preserves them verbatim.
        let output = cleaner.clean("a\tb");
        assert!(matches!(output.text, Cow::Borrowed("a\tb")));
    }

    // ---- F8: every output rewrite must be counted (ishuman contract) ----

    #[test]
    fn retained_tabs_survive_cleaning_verbatim() {
        // Tabs are keyboard characters and no option claims the right to
        // rewrite them; silently spacing them out broke changes_made == 0
        // detection and mangled tab-indented source under code_safe.
        for options in [
            CleaningOptions::default(),
            CleaningOptions::minimal(),
            CleaningOptions::code_safe(),
        ] {
            let c = TextCleaner::new(options);
            let out = c.clean("a\tb");
            assert_eq!(out.text, "a\tb");
            assert_eq!(out.changes_made, 0);
        }
        let code_safe = TextCleaner::new(CleaningOptions::code_safe());
        assert_eq!(code_safe.clean("\tindent").text, "\tindent");
    }

    #[test]
    fn collapse_rewrites_are_counted() {
        let c = TextCleaner::new(CleaningOptions {
            collapse_whitespace: true,
            ..CleaningOptions::minimal()
        });

        let out = c.clean("a  b");
        assert_eq!(out.text, "a b");
        assert_eq!(out.changes_made, 1);
        #[cfg(feature = "stats")]
        assert_eq!(out.stats.whitespace_collapsed, 1);

        // A lone tab collapses to a space: zero chars removed, still a rewrite.
        let out = c.clean("a\tb");
        assert_eq!(out.text, "a b");
        assert_eq!(out.changes_made, 1);

        // A lone space is already collapsed; nothing may be counted.
        let out = c.clean("a b");
        assert_eq!(out.text, "a b");
        assert_eq!(out.changes_made, 0);
    }

    #[cfg(feature = "unorm")]
    #[test]
    fn unicode_normalization_rewrites_are_counted() {
        let c = TextCleaner::new(CleaningOptions {
            keyboard_only: false,
            unicode_normalization: UnicodeNormalizationMode::NFC,
            ..CleaningOptions::default()
        });

        // e + U+0301 composes under NFC: a real rewrite, must be counted.
        let out = c.clean("cafe\u{0301}");
        assert_eq!(out.text, "caf\u{00E9}");
        assert!(out.changes_made > 0);
        #[cfg(feature = "stats")]
        assert_eq!(out.stats.unicode_normalized, 1);

        // Already-NFC input is not a normalization change.
        let out = c.clean("caf\u{00E9}");
        assert_eq!(out.text, "caf\u{00E9}");
        assert_eq!(out.changes_made, 0);
    }

    // ---- F4: a dropped emoji is billed once, not also as hidden removals ----

    #[test]
    fn dropped_emoji_not_double_counted() {
        let c = TextCleaner::new(CleaningOptions {
            keyboard_only: true,
            emoji_policy: EmojiPolicy::Drop,
            ..CleaningOptions::default()
        });
        let out = c.clean("\u{1F44D}\u{FE0F}"); // 👍 + VS16
        assert_eq!(out.text, "");
        assert_eq!(out.changes_made, 1);
        #[cfg(feature = "stats")]
        {
            assert_eq!(out.stats.emojis_dropped, 1);
            assert_eq!(out.stats.hidden_chars_removed, 0); // VS16 not separately billed
        }

        let family = c.clean("\u{1F468}\u{200D}\u{1F469}\u{200D}\u{1F467}"); // ZWJ family
        assert_eq!(family.text, "");
        assert_eq!(family.changes_made, 1);
        #[cfg(feature = "stats")]
        {
            assert_eq!(family.stats.emojis_dropped, 1);
            assert_eq!(family.stats.hidden_chars_removed, 0); // two interior ZWJ not billed
        }
    }

    // ---- F7: U+2028/U+2029 fold to \n without normalize_line_endings ----

    #[test]
    fn line_separators_fold_to_newline_without_line_ending_option() {
        // Default preset (keyboard_only, normalize_line_endings = None).
        let out = clean("a\u{2028}b\u{2029}c");
        assert_eq!(out.text, "a\nb\nc");
        #[cfg(feature = "stats")]
        assert_eq!(out.stats.spaces_normalized, 2);

        // Non-keyboard mode: previously passed through unchanged.
        let c = TextCleaner::new(CleaningOptions::builder().keyboard_only(false).build());
        assert_eq!(c.clean("a\u{2028}b").text, "a\nb");

        // Trailing whitespace before the separator trims like a real newline.
        assert_eq!(clean("a \u{2028}b").text, "a\nb");

        // humanize collapses whitespace but keeps the folded newline. (Its
        // preset requests NFKC, so it needs `unorm` to run at all.)
        #[cfg(feature = "unorm")]
        assert_eq!(humanize("x\u{2028}y").text, "x\ny");
    }

    #[test]
    fn stream_cleaner_flushes_on_unicode_line_separators() {
        // Since batch cleaning treats U+2028/U+2029 as line breaks, streaming
        // must flush on them too: a stream delimited only by these separators
        // (no LF byte anywhere) has to emit per line, not buffer to finish().
        for sep in ['\u{2028}', '\u{2029}'] {
            let mut stream = StreamCleaner::new(CleaningOptions::default());
            let mut out = String::new();
            let input = format!("alpha{sep}beta");
            let flushed = stream
                .feed(&input, &mut out)
                .unwrap_or_else(|| panic!("U+{:04X} must be a flush boundary", sep as u32));
            assert_eq!(flushed.text, "alpha\n");
            let mut tail = String::new();
            let finished = stream.finish(&mut tail).expect("buffered remainder");
            assert_eq!(finished.text, "beta");
        }
    }

    #[test]
    fn stream_cleaner_matches_batch_for_fast_path_options() {
        for sep in ['\u{2028}', '\u{2029}'] {
            for normalize_spaces in [false, true] {
                let options = CleaningOptions {
                    normalize_spaces,
                    ..CleaningOptions::minimal()
                };
                let input = format!("a{sep}\t");
                let baseline = TextCleaner::new(options.clone()).clean(&input);
                let mut stream = StreamCleaner::new(options);
                let mut chunk_output = String::new();
                let mut streamed = String::new();

                let flushed = stream.feed(&input, &mut chunk_output);
                assert_eq!(
                    flushed.is_some(),
                    normalize_spaces,
                    "U+{:04X} boundary behavior must follow normalize_spaces",
                    sep as u32
                );
                if let Some(result) = flushed {
                    streamed.push_str(result.text.as_ref());
                }
                if let Some(result) = stream.finish(&mut chunk_output) {
                    streamed.push_str(result.text.as_ref());
                }

                let summary = stream.summary();
                assert_eq!(streamed, baseline.text);
                assert_eq!(summary.stats, baseline.stats);
                assert_eq!(summary.changes_made, baseline.changes_made);
            }
        }
    }

    // ---- idempotence: clean(clean(x)) == clean(x) on representative input ----

    #[test]
    fn cleaning_is_idempotent_on_symbol_samples() {
        let c = TextCleaner::new(CleaningOptions::default());
        for s in [
            "a \u{2192} b \u{2264} c",
            "\u{2260} \u{00A9} \u{2605} \u{2022}",
            "caf\u{00E9} \u{2014} \u{00BD}",
            "\u{1F44D}\u{FE0F} done",
            "\u{00B8}\u{00B4}\u{00A8}", // spacing diacritics: the F8 regression
            "\u{03BB} \u{03A3} \u{2705} \u{274C} \u{00A3} \u{00A7}",
            "\u{27E8}x\u{27E9} don\u{02BC}t \u{0259}",
        ] {
            let once = c.clean(s).text.into_owned();
            let twice = c.clean(&once).text.into_owned();
            assert_eq!(once, twice, "not idempotent for {s:?}");
        }
    }
}