awkrs 0.5.5

Awk implementation in Rust with broad CLI compatibility, parallel records, and experimental Cranelift JIT
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
use std::borrow::Cow;
use std::cell::Cell;
use std::cmp::Ordering;
use std::collections::HashMap;

/// Fast hash map for awk variables and arrays. Uses FxHash (no DoS resistance,
/// but ~2× faster than SipHash for short string keys typical in awk programs).
pub type AwkMap<K, V> = rustc_hash::FxHashMap<K, V>;
use socket2::{Domain, Socket, Type};
use std::fs::{File, OpenOptions};
use std::io::{BufRead, BufReader, BufWriter, Read, Write};
use std::net::{Ipv4Addr, Ipv6Addr, SocketAddr, TcpStream, ToSocketAddrs, UdpSocket};
use std::path::Path;
use std::process::{Child, ChildStdin, ChildStdout, Command, ExitStatus, Stdio};
use std::sync::{Arc, Mutex};
use std::time::Duration;

use crate::awkstr::AwkStr;
use crate::bignum::value_to_mpfr;
use crate::bytecode::CompiledProgram;
use crate::error::{Error, Result};
use gettext::Catalog;
use rug::float::Round;
use rug::ops::Pow as _;
use rug::Float;

thread_local! {
    static NON_DECIMAL_PARSE: Cell<bool> = const { Cell::new(false) };
}

/// Set how string→number coercion parses literals (gawk `--non-decimal-data` / `-n`).
pub fn set_numeric_parse_mode(enabled: bool) {
    NON_DECIMAL_PARSE.with(|c| c.set(enabled));
}

/// Whether [`parse_number`] uses hex/octal rules like gawk `strtonum`.
#[inline]
pub fn numeric_parse_mode() -> bool {
    NON_DECIMAL_PARSE.with(|c| c.get())
}
use memchr::memmem;
use regex::bytes::Regex as BytesRegex;

/// Initial capacity for stdout batching (`print` accumulates here until flush).
/// Large END blocks (e.g. `for (k in a) print …`) grow this heavily; starting larger
/// avoids repeated `Vec` reallocations without a hard upper bound on output size.
const DEFAULT_PRINT_BUF_CAPACITY: usize = 512 * 1024;

pub(crate) type SharedInputReader = Arc<Mutex<BufReader<Box<dyn Read + Send>>>>;

/// Default precision for [`Value::Mpfr`] when `-M` / `--bignum` is enabled (MPFR bits).
pub const MPFR_PREC: u32 = 256;

/// POSIX / gawk: string ordering via `strcoll` on Unix (used by `for-in` value sorts and comparisons).
pub fn awk_locale_str_cmp(a: &str, b: &str) -> Ordering {
    #[cfg(unix)]
    {
        use std::ffi::CString;
        match (CString::new(a), CString::new(b)) {
            (Ok(ca), Ok(cb)) => unsafe {
                let r = libc::strcoll(ca.as_ptr(), cb.as_ptr());
                r.cmp(&0)
            },
            _ => a.cmp(b),
        }
    }
    #[cfg(not(unix))]
    {
        a.cmp(b)
    }
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) enum SortedInMode {
    /// `Unsorted` variant.
    Unsorted,
    /// `IndStrAsc` variant.
    IndStrAsc,
    /// `IndStrDesc` variant.
    IndStrDesc,
    /// `IndNumAsc` variant.
    IndNumAsc,
    /// `IndNumDesc` variant.
    IndNumDesc,
    /// `ValStrAsc` variant.
    ValStrAsc,
    /// `ValStrDesc` variant.
    ValStrDesc,
    /// `ValNumAsc` variant.
    ValNumAsc,
    /// `ValNumDesc` variant.
    ValNumDesc,
    /// `ValTypeAsc` variant.
    ValTypeAsc,
    /// `ValTypeDesc` variant.
    ValTypeDesc,
    /// gawk: `PROCINFO["sorted_in"] = "cmp"` — user function `(i1, i2)` returns &lt;0 / 0 / &gt;0 (index sort).
    CustomFn(String),
}

fn is_sorted_in_user_fn_name(s: &str) -> bool {
    let mut chars = s.chars();
    let Some(c) = chars.next() else {
        return false;
    };
    if !(c.is_ascii_alphabetic() || c == '_') {
        return false;
    }
    chars.all(|c| c.is_ascii_alphanumeric() || c == '_')
}

fn parse_sorted_in_at_token(t: &str) -> Option<SortedInMode> {
    match t {
        "@unsorted" => Some(SortedInMode::Unsorted),
        "@ind_str_asc" => Some(SortedInMode::IndStrAsc),
        "@ind_str_desc" => Some(SortedInMode::IndStrDesc),
        "@ind_num_asc" => Some(SortedInMode::IndNumAsc),
        "@ind_num_desc" => Some(SortedInMode::IndNumDesc),
        "@val_str_asc" => Some(SortedInMode::ValStrAsc),
        "@val_str_desc" => Some(SortedInMode::ValStrDesc),
        "@val_num_asc" => Some(SortedInMode::ValNumAsc),
        "@val_num_desc" => Some(SortedInMode::ValNumDesc),
        "@val_type_asc" => Some(SortedInMode::ValTypeAsc),
        "@val_type_desc" => Some(SortedInMode::ValTypeDesc),
        _ => None,
    }
}

pub(crate) fn sorted_in_mode(rt: &Runtime) -> SortedInMode {
    if rt.posix {
        return SortedInMode::Unsorted;
    }
    match rt.get_global_var("PROCINFO") {
        Some(Value::Array(m)) => {
            let Some(v) = m.get("sorted_in") else {
                return SortedInMode::Unsorted;
            };
            let s = v.as_str();
            let t = s.trim();
            if t.is_empty() {
                return SortedInMode::Unsorted;
            }
            if t.starts_with('@') {
                if let Some(mode) = parse_sorted_in_at_token(t) {
                    return mode;
                }
                if !rt.sorted_in_warned.get() {
                    rt.sorted_in_warned.set(true);
                    eprintln!(
                        "awkrs: PROCINFO[\"sorted_in\"]={s:?}: unknown @… token (expected @ind_* / @val_* / @unsorted)"
                    );
                }
                return SortedInMode::Unsorted;
            }
            if is_sorted_in_user_fn_name(t) {
                return SortedInMode::CustomFn(t.to_string());
            }
            SortedInMode::Unsorted
        }
        _ => SortedInMode::Unsorted,
    }
}

#[inline]
fn val_type_rank(v: &Value) -> u8 {
    match v {
        Value::Uninit => 0,
        Value::Num(_) | Value::Mpfr(_) => 1,
        Value::Str(_) | Value::StrLit(_) | Value::Regexp(_) => 2,
        Value::Array(_) => 3,
    }
}

pub(crate) fn sort_for_in_keys(keys: &mut [AwkStr], arr: &AwkArray, mode: SortedInMode) {
    use SortedInMode::*;
    match mode {
        Unsorted => {}
        CustomFn(_) => {}
        IndStrAsc => keys.sort(),
        IndStrDesc => keys.sort_by(|a, b| b.cmp(a)),
        IndNumAsc => keys.sort_by(|a, b| {
            parse_number(&a.to_str_lossy())
                .partial_cmp(&parse_number(&b.to_str_lossy()))
                .unwrap_or(Ordering::Equal)
        }),
        IndNumDesc => keys.sort_by(|a, b| {
            parse_number(&b.to_str_lossy())
                .partial_cmp(&parse_number(&a.to_str_lossy()))
                .unwrap_or(Ordering::Equal)
        }),
        ValStrAsc => keys.sort_by(|ka, kb| {
            let sa = arr.get_bytes(ka).map(|v| v.as_str()).unwrap_or_default();
            let sb = arr.get_bytes(kb).map(|v| v.as_str()).unwrap_or_default();
            awk_locale_str_cmp(&sa, &sb)
        }),
        ValStrDesc => keys.sort_by(|ka, kb| {
            let sa = arr.get_bytes(ka).map(|v| v.as_str()).unwrap_or_default();
            let sb = arr.get_bytes(kb).map(|v| v.as_str()).unwrap_or_default();
            awk_locale_str_cmp(&sb, &sa)
        }),
        ValNumAsc => keys.sort_by(|ka, kb| {
            let na = arr.get_bytes(ka).map(|v| v.as_number()).unwrap_or(0.0);
            let nb = arr.get_bytes(kb).map(|v| v.as_number()).unwrap_or(0.0);
            na.partial_cmp(&nb).unwrap_or(Ordering::Equal)
        }),
        ValNumDesc => keys.sort_by(|ka, kb| {
            let na = arr.get_bytes(ka).map(|v| v.as_number()).unwrap_or(0.0);
            let nb = arr.get_bytes(kb).map(|v| v.as_number()).unwrap_or(0.0);
            nb.partial_cmp(&na).unwrap_or(Ordering::Equal)
        }),
        ValTypeAsc => keys.sort_by(|ka, kb| {
            let va = arr.get_bytes(ka);
            let vb = arr.get_bytes(kb);
            let ra = va.map(val_type_rank).unwrap_or(0);
            let rb = vb.map(val_type_rank).unwrap_or(0);
            ra.cmp(&rb).then_with(|| {
                let sa = va.map(|v| v.as_str()).unwrap_or_default();
                let sb = vb.map(|v| v.as_str()).unwrap_or_default();
                awk_locale_str_cmp(&sa, &sb)
            })
        }),
        ValTypeDesc => keys.sort_by(|ka, kb| {
            let va = arr.get_bytes(ka);
            let vb = arr.get_bytes(kb);
            let ra = va.map(val_type_rank).unwrap_or(0);
            let rb = vb.map(val_type_rank).unwrap_or(0);
            rb.cmp(&ra).then_with(|| {
                let sa = va.map(|v| v.as_str()).unwrap_or_default();
                let sb = vb.map(|v| v.as_str()).unwrap_or_default();
                awk_locale_str_cmp(&sb, &sa)
            })
        }),
    }
}

#[cfg(unix)]
fn wait_fd_read_timeout(fd: std::os::unix::io::RawFd, timeout_ms: i32) -> crate::error::Result<()> {
    if timeout_ms <= 0 {
        return Ok(());
    }
    let mut fds = libc::pollfd {
        fd,
        events: libc::POLLIN,
        revents: 0,
    };
    let rc = unsafe { libc::poll(&mut fds, 1, timeout_ms) };
    if rc < 0 {
        return Err(crate::error::Error::Io(std::io::Error::last_os_error()));
    }
    if rc == 0 {
        return Err(crate::error::Error::Io(std::io::Error::new(
            std::io::ErrorKind::TimedOut,
            "read timeout (PROCINFO[\"READ_TIMEOUT\"])",
        )));
    }
    Ok(())
}

/// Convert a [`Value`] to a [`Float`] for MPFR arithmetic (`-M`).
/// Coerce to MPFR for `-M` (see [`crate::bignum::value_to_mpfr`]).
#[inline]
pub fn value_to_float(v: &Value, prec: u32, round: Round) -> Float {
    value_to_mpfr(v, prec, round)
}

/// Binary `+=` / `-=` / … for compound assignment; uses MPFR when `use_mpfr` is true.
pub fn awk_binop_values(
    op: crate::ast::BinOp,
    old: &Value,
    rhs: &Value,
    use_mpfr: bool,
    rt: &Runtime,
) -> crate::error::Result<Value> {
    use crate::ast::BinOp;
    use crate::error::Error;
    old.reject_if_array_scalar()?;
    rhs.reject_if_array_scalar()?;
    if !use_mpfr {
        let a = old.as_number();
        let b = rhs.as_number();
        let n = match op {
            BinOp::Add => a + b,
            BinOp::Sub => a - b,
            BinOp::Mul => a * b,
            BinOp::Div => {
                if b == 0.0 {
                    return Err(Error::Runtime("division by zero attempted".into()));
                }
                a / b
            }
            BinOp::Mod => {
                if b == 0.0 {
                    return Err(Error::Runtime("division by zero attempted in `%'".into()));
                }
                a % b
            }
            BinOp::Pow => a.powf(b),
            _ => return Err(Error::Runtime("invalid compound assignment op".into())),
        };
        return Ok(Value::Num(n));
    }
    let prec = rt.mpfr_prec_bits();
    let round = rt.mpfr_round();
    let a = value_to_mpfr(old, prec, round);
    let b = value_to_mpfr(rhs, prec, round);
    let r = match op {
        BinOp::Add => Float::with_val_round(prec, &a + &b, round).0,
        BinOp::Sub => Float::with_val_round(prec, &a - &b, round).0,
        BinOp::Mul => Float::with_val_round(prec, &a * &b, round).0,
        BinOp::Div => {
            if b.is_zero() {
                return Err(Error::Runtime("division by zero attempted".into()));
            }
            Float::with_val_round(prec, &a / &b, round).0
        }
        BinOp::Mod => {
            if b.is_zero() {
                return Err(Error::Runtime("division by zero attempted in `%'".into()));
            }
            Float::with_val_round(prec, &a % &b, round).0
        }
        BinOp::Pow => Float::with_val_round(prec, a.pow(&b), round).0,
        _ => return Err(Error::Runtime("invalid compound assignment op".into())),
    };
    Ok(Value::Mpfr(r))
}

/// Does this redirection target name the program's own standard output?
///
/// `print "x" > "/dev/stdout"` must land in the same byte stream as a plain
/// `print`, interleaved in program order. Opening `/dev/stdout` as an ordinary
/// file gives it a second, independent buffer, and awkrs then emitted
/// `A`/`B`/`C` from `print "A"; print "B" > "/dev/stdout"; print "C"` as
/// `A C B` — the separate buffer drained at exit, after the main one. gawk,
/// mawk and one-true-awk all print `A B C`. Recognising the name and writing
/// through the ordinary print buffer makes the ordering fall out for free
/// instead of depending on flush timing.
fn is_program_stdout(path: &str) -> bool {
    path == "/dev/stdout"
}

/// The file `getline < path` should actually open.
///
/// POSIX gives the operand `-` the meaning "standard input", and gawk, mawk and
/// one-true-awk all honour it for `getline < "-"` as well as for a file
/// operand: all three read stdin for `while ((getline l < "-") > 0)`. awkrs
/// opened a file literally named `-`, which does not exist, so the read
/// returned -1 and the loop never ran. `/dev/stdin` is the same stream and is
/// already handled correctly, so `-` is redirected onto it.
///
/// Only the *input* side is remapped. On the output side the references
/// disagree — gawk writes `print > "-"` to stdout while mawk and one-true-awk
/// create a file named `-` — so there is no single behavior to match and awkrs
/// keeps the mawk / one-true-awk reading.
fn getline_open_path(p: &Path) -> &Path {
    if p.as_os_str() == "-" {
        return Path::new("/dev/stdin");
    }
    p
}

/// The number awk reports for a finished child: `system()` and `close()` on a
/// pipe both answer with this.
///
/// A child that exited normally reports its exit code. A child killed by a
/// signal has no exit code at all, and `ExitStatus::code()` answers `None` for
/// it — reporting that as `-1` loses which signal fired and collides with the
/// "could not wait" answer. gawk, mawk and one-true-awk all report `256 + signo`
/// instead, so `system("kill -TERM $$")` is 271 in every one of them; awkrs
/// answered -1 from `system()` while already answering 271 from `close()`,
/// because the encoding lived in `close_handle` and nowhere else. It lives here
/// now, and both callers go through it.
pub fn awk_process_status(status: std::io::Result<ExitStatus>) -> f64 {
    let Ok(status) = status else { return -1.0 };
    if let Some(code) = status.code() {
        return code as f64;
    }
    #[cfg(unix)]
    {
        use std::os::unix::process::ExitStatusExt;
        if let Some(sig) = status.signal() {
            return (256 + sig) as f64;
        }
    }
    -1.0
}

/// Parse gawk-style `/inet/tcp/lport/host/rport` (local port `0` = ephemeral client).
pub fn parse_inet_tcp(path: &str) -> Option<(u16, String, u16)> {
    parse_inet_l4(path, "/inet/tcp/")
}

/// Parse gawk-style `/inet/udp/lport/host/rport`.
pub fn parse_inet_udp(path: &str) -> Option<(u16, String, u16)> {
    parse_inet_l4(path, "/inet/udp/")
}

fn parse_inet_l4(path: &str, prefix: &str) -> Option<(u16, String, u16)> {
    let rest = path.strip_prefix(prefix)?;
    let mut it = rest.split('/');
    let lport = it.next()?.parse().ok()?;
    let host = it.next()?.to_string();
    let rport = it.next()?.parse().ok()?;
    if it.next().is_some() {
        return None;
    }
    Some((lport, host, rport))
}

fn tcp_connect_with_local_port(host: &str, lport: u16, rport: u16) -> Result<TcpStream> {
    let mut addrs = format!("{host}:{rport}")
        .to_socket_addrs()
        .map_err(|e| Error::Runtime(format!("inet resolve `{host}`: {e}")))?;
    let addr = addrs
        .next()
        .ok_or_else(|| Error::Runtime(format!("inet: no address for `{host}:{rport}`")))?;
    let domain = match addr {
        SocketAddr::V4(_) => Domain::IPV4,
        SocketAddr::V6(_) => Domain::IPV6,
    };
    let socket = Socket::new(domain, Type::STREAM, None)
        .map_err(|e| Error::Runtime(format!("inet socket: {e}")))?;
    let bind_addr = match addr {
        SocketAddr::V4(_) => SocketAddr::from((Ipv4Addr::UNSPECIFIED, lport)),
        SocketAddr::V6(_) => SocketAddr::from((Ipv6Addr::UNSPECIFIED, lport)),
    };
    socket
        .bind(&bind_addr.into())
        .map_err(|e| Error::Runtime(format!("inet bind local port {lport}: {e}")))?;
    socket.set_nonblocking(false).ok();
    socket
        .connect(&addr.into())
        .map_err(|e| Error::Runtime(format!("inet connect `{host}:{rport}`: {e}")))?;
    Ok(socket.into())
}

/// Open two-way pipe to `sh -c` (gawk-style `|&` / `<&`).
pub struct CoprocHandle {
    /// `child` field.
    pub child: Child,
    /// `stdin` field.
    pub stdin: BufWriter<ChildStdin>,
    /// `stdout` field.
    pub stdout: BufReader<ChildStdout>,
}
/// `Value` — see variants for the choices.
#[derive(Debug, Clone)]
pub enum Value {
    /// Never assigned (missing global, missing function argument, or fresh slot).
    /// String/number contexts treat this like `""` / `0` (same as gawk *untyped*).
    Uninit,
    /// Dynamic string (fields, concat, I/O, etc.) — may be a POSIX *numeric string* in comparisons.
    Str(AwkStr),
    /// String literal from program text (`"..."`) — not a numeric string for relational ops (POSIX).
    StrLit(AwkStr),
    /// gawk: `@/regex/` regexp constant — distinct from [`Value::Str`] for `typeof` and typed `~`.
    Regexp(AwkStr),
    /// `Num` variant.
    Num(f64),
    /// GNU MPFR arbitrary-precision float (`-M` / `--bignum`).
    Mpfr(Float),
    /// `Array` variant.
    Array(AwkArray),
}

/// Default **`-M`** number→string when no [`Runtime`] is available (POSIX default **CONVFMT** **`%.6g`**).
///
/// Uses each MPFR `Float`'s own precision (`Float::prec()`) for `sprintf` MPFR mode so values
/// allocated at high `PROCINFO["prec"]` are not rounded down through a hardcoded bit count during
/// formatting (round mode defaults to nearest when `Runtime` is not in scope).
#[inline]
fn mpfr_value_default_display(f: &Float) -> String {
    let prec = f.prec();
    crate::format::awk_sprintf_with_decimal(
        "%.6g",
        &[Value::Mpfr(f.clone())],
        '.',
        Some(','),
        Some((prec, Round::Nearest)),
    )
    .unwrap_or_else(|_| crate::bignum::mpfr_string_trim_trailing_zeros(f.to_string()))
}

/// Longest leading substring of `s` that `f64::from_str` accepts (POSIX awk string→number prefix rule).
/// Parseability is not monotonic in prefix length (e.g. `1` ok, `1e` err, `1e2` ok), so we scan downward.
///
/// gawk parity: Rust's `f64::from_str` accepts bare `"inf"`, `"nan"`, `"infinity"`, `"Infinity"`, etc.
/// gawk does NOT — bare special names coerce to 0. The only special-name forms gawk accepts are
/// sign-prefixed three-letter `inf` / `nan` (case-insensitive), AND only when not followed by more
/// alphanumeric characters (so `"+infzzz"` and `"+infinity"` both coerce to 0, not `+inf`).
#[inline]
pub(crate) fn longest_f64_prefix(s: &str) -> Option<&str> {
    if s.is_empty() {
        return None;
    }
    // f64 numeric prefixes are entirely ASCII (digits, signs, dot, e/E, inf, nan).
    // Limit the byte-index iteration to the ASCII prefix so `&s[..end]` never
    // lands inside a multi-byte UTF-8 character. (Without this, a string like
    // "ÿ" panicked: "end byte index 1 is not a char boundary; it is inside 'ÿ'".)
    let ascii_len = s.as_bytes().iter().take_while(|&&b| b.is_ascii()).count();
    if ascii_len == 0 {
        return None;
    }
    for end in (1..=ascii_len).rev() {
        let p = &s[..end];
        if !awk_numeric_prefix_acceptable(p, end < s.len() && next_byte_is_alnum(s, end)) {
            continue;
        }
        if p.parse::<f64>().is_ok() {
            return Some(p);
        }
    }
    None
}

#[inline]
fn next_byte_is_alnum(s: &str, end: usize) -> bool {
    s.as_bytes()
        .get(end)
        .map(|c| c.is_ascii_alphanumeric())
        .unwrap_or(false)
}

/// gawk numeric coercion rule: accept ordinary decimal/float prefixes (must contain a digit), plus
/// signed three-letter `inf` / `nan` (case-insensitive). The special-name form is rejected when
/// followed by an alphanumeric byte in the source (gawk requires the keyword to stand alone, so
/// `"+infzzz"` and `"+infinity"` both coerce to 0).
#[inline]
fn awk_numeric_prefix_acceptable(p: &str, has_trailing_alnum: bool) -> bool {
    if p.bytes().any(|c| c.is_ascii_digit()) {
        return true;
    }
    if has_trailing_alnum {
        return false;
    }
    let b = p.as_bytes();
    if b.len() != 4 {
        return false;
    }
    if !matches!(b[0], b'+' | b'-') {
        return false;
    }
    let tail = &p[1..];
    tail.eq_ignore_ascii_case("inf") || tail.eq_ignore_ascii_case("nan")
}

impl Value {
    /// gawk-style fatal: whole arrays cannot be coerced to a string in scalar contexts (`print a`, concat, etc.).
    #[inline]
    pub fn reject_if_array_scalar(&self) -> Result<()> {
        if matches!(self, Value::Array(_)) {
            return Err(Error::Runtime(
                "attempt to use an array in a scalar context".into(),
            ));
        }
        Ok(())
    }
    /// `as_str` — see implementation for the contract.
    pub fn as_str(&self) -> String {
        match self {
            Value::Uninit => String::new(),
            Value::Str(s) | Value::StrLit(s) | Value::Regexp(s) => s.to_lossy_string(),
            Value::Num(n) => format_number(*n),
            Value::Mpfr(f) => mpfr_value_default_display(f),
            Value::Array(_) => String::new(),
        }
    }

    /// For `&str` APIs (e.g. `gsub`) without allocating when the value is already `Str`.
    #[inline]
    pub fn as_str_cow(&self) -> Cow<'_, str> {
        match self {
            Value::Uninit => Cow::Borrowed(""),
            Value::Str(s) | Value::StrLit(s) | Value::Regexp(s) => s.to_str_lossy(),
            Value::Num(n) => Cow::Owned(format_number(*n)),
            Value::Mpfr(f) => Cow::Owned(mpfr_value_default_display(f)),
            Value::Array(_) => Cow::Borrowed(""),
        }
    }

    /// Borrow the inner byte string without cloning. `None` for Num/Array.
    #[inline]
    #[allow(dead_code)]
    pub fn str_ref(&self) -> Option<&AwkStr> {
        match self {
            Value::Str(s) | Value::StrLit(s) | Value::Regexp(s) => Some(s),
            _ => None,
        }
    }

    /// The value's **bytes**, borrowing when it already holds them.
    ///
    /// This is the byte-exact counterpart of [`Self::as_str_cow`], and the one
    /// any path the awk program can observe should use: `as_str`/`as_str_cow`
    /// render through `U+FFFD` and cannot round-trip a byte that is not part of
    /// valid UTF-8. A number renders through `CONVFMT` exactly as before — that
    /// text is always ASCII, so the two agree for every numeric value.
    #[inline]
    pub fn as_bytes_cow(&self) -> Cow<'_, [u8]> {
        match self {
            Value::Uninit | Value::Array(_) => Cow::Borrowed(b""),
            Value::Str(s) | Value::StrLit(s) | Value::Regexp(s) => Cow::Borrowed(s.as_bytes()),
            Value::Num(n) => Cow::Owned(format_number(*n).into_bytes()),
            Value::Mpfr(f) => Cow::Owned(mpfr_value_default_display(f).into_bytes()),
        }
    }

    /// Write the string representation directly into a byte buffer — zero allocation
    /// for the Str case, one `write!` for Num.
    pub fn write_to(&self, buf: &mut Vec<u8>) {
        match self {
            Value::Uninit => {}
            Value::Str(s) | Value::StrLit(s) => buf.extend_from_slice(s.as_bytes()),
            Value::Regexp(s) => buf.extend_from_slice(s.as_bytes()),
            Value::Num(n) => {
                use std::io::Write;
                let n = *n;
                if n.is_finite() && n.fract() == 0.0 {
                    let _ = write!(buf, "{:.0}", n);
                } else {
                    let _ = write!(buf, "{n}");
                }
            }
            Value::Mpfr(f) => buf.extend_from_slice(mpfr_value_default_display(f).as_bytes()),
            Value::Array(_) => {}
        }
    }
    /// `as_number` — see implementation for the contract.
    pub fn as_number(&self) -> f64 {
        match self {
            Value::Uninit => 0.0,
            Value::Num(n) => *n,
            Value::Str(s) | Value::StrLit(s) => parse_number(&s.to_str_lossy()),
            Value::Regexp(s) => parse_number(&s.to_str_lossy()),
            Value::Mpfr(f) => f.to_f64(),
            Value::Array(_) => 0.0,
        }
    }
    /// `truthy` — see implementation for the contract.
    pub fn truthy(&self) -> bool {
        match self {
            Value::Uninit => false,
            Value::Num(n) => *n != 0.0,
            // POSIX: string LITERALS (Value::StrLit, from source) are truthy
            // iff non-empty — "0", "false", " " are all truthy strings.
            Value::StrLit(s) => !s.is_empty(),
            // `Value::Str` comes from input (fields, -v, getline) and may be a
            // "numeric string": if it parses cleanly as a number, use numeric
            // truthiness; otherwise non-empty.
            Value::Str(s) => {
                if s.is_empty() {
                    false
                } else if let Ok(n) = s.to_str_lossy().parse::<f64>() {
                    n != 0.0
                } else {
                    true
                }
            }
            Value::Regexp(s) => !s.is_empty(),
            Value::Mpfr(f) => !f.is_zero(),
            Value::Array(a) => !a.is_empty(),
        }
    }

    /// Boolean tests in `if` / `while` / `for` / `?:` — whole arrays are a fatal error (gawk).
    pub fn truthy_cond(&self) -> crate::error::Result<bool> {
        self.reject_if_array_scalar()?;
        Ok(match self {
            Value::Uninit => false,
            Value::Num(n) => *n != 0.0,
            // Same rule as `truthy()` — see comments there.
            Value::StrLit(s) => !s.is_empty(),
            Value::Str(s) => {
                if s.is_empty() {
                    false
                } else if let Ok(n) = s.to_str_lossy().parse::<f64>() {
                    n != 0.0
                } else {
                    true
                }
            }
            Value::Regexp(s) => !s.is_empty(),
            Value::Mpfr(f) => !f.is_zero(),
            Value::Array(_) => unreachable!(),
        })
    }

    /// Take ownership of the inner String, converting numbers to string form.
    /// Avoids clone when the Value is already a Str variant.
    #[inline]
    pub fn into_string(self) -> String {
        match self {
            Value::Uninit => String::new(),
            Value::Str(s) | Value::StrLit(s) | Value::Regexp(s) => s.to_lossy_string(),
            Value::Num(n) => format_number(n),
            Value::Mpfr(f) => mpfr_value_default_display(&f),
            Value::Array(_) => String::new(),
        }
    }

    /// Append this value's string representation to an existing String.
    /// Avoids intermediate allocation compared to `format!("{a}{b}")`.
    #[allow(dead_code)] // tested but no production call sites currently
    #[inline]
    pub fn append_to_string(&self, buf: &mut String) {
        match self {
            Value::Uninit => {}
            Value::Str(s) | Value::StrLit(s) => buf.push_str(&s.to_str_lossy()),
            Value::Regexp(s) => buf.push_str(&s.to_str_lossy()),
            Value::Num(n) => {
                use std::fmt::Write;
                let n = *n;
                if n.is_finite() && n.fract() == 0.0 {
                    let _ = write!(buf, "{:.0}", n);
                } else {
                    let _ = write!(buf, "{n}");
                }
            }
            Value::Mpfr(f) => buf.push_str(&mpfr_value_default_display(f)),
            Value::Array(_) => {}
        }
    }

    /// POSIX-style: true if the value is numeric (including string that looks like number).
    pub fn is_numeric_str(&self) -> bool {
        match self {
            // Uninitialized scalar has dual 0 / "" — participates in numeric comparisons like 0.
            Value::Uninit => true,
            Value::Num(_) => true,
            Value::Mpfr(_) => true,
            Value::StrLit(_) => false,
            Value::Str(s) => {
                // POSIX / gawk: a string is "numeric" only if the ENTIRE trimmed text is a
                // valid number — `"42abc"` is NOT numeric (typeof is `"string"`), even though
                // `+"42abc"` yields 42 via prefix parsing. Earlier we used the prefix check
                // alone, which made `typeof($1)` report `strnum` for noisy fields like
                // `"42abc"` and made `"42abc" == 42` a numeric compare instead of string.
                let s = s.to_str_lossy();
                let t = s.trim();
                if t.is_empty() {
                    return false;
                }
                match longest_f64_prefix(t) {
                    Some(p) => p.len() == t.len(),
                    None => false,
                }
            }
            Value::Regexp(_) => false,
            Value::Array(_) => false,
        }
    }
}

/// Format a number to string (awk rules: integer form if no fractional part).
/// Uses `{:.0}` for integer-valued floats so values past `i64::MAX` (e.g. 1e25)
/// still display as decimals — matches gawk's `printf("%.0f", n)` behavior.
#[inline]
fn format_number(n: f64) -> String {
    if !n.is_finite() {
        // gawk: "+inf", "-inf", "+nan", "-nan" — emit the same spelling that `print` /
        // OFMT-driven `%g` use so `printf "%s"` of a NaN matches `print` of the same value.
        let sign = if n.is_sign_negative() { '-' } else { '+' };
        let body = if n.is_nan() { "nan" } else { "inf" };
        return format!("{sign}{body}");
    }
    if n.fract() == 0.0 {
        // gawk parity: `print -0.0` produces `"0"`, not `"-0"`. Normalize negative
        // zero so the two awks agree on numeric zero output.
        if n == 0.0 {
            return "0".to_string();
        }
        format!("{:.0}", n)
    } else {
        format!("{n}")
    }
}

/// gawk `strtonum`-style parse (hex `0x…`, octal `0…`, else float).
#[inline]
fn parse_number_strtonum(s: &str) -> f64 {
    let t = s.trim();
    if t.is_empty() {
        return 0.0;
    }
    if t.starts_with("0x") || t.starts_with("0X") {
        return u64::from_str_radix(&t[2..], 16)
            .map(|v| v as f64)
            .unwrap_or(0.0);
    }
    if t.len() > 1 && t.starts_with('0') && !t.contains('.') && !t.contains('e') && !t.contains('E')
    {
        return i64::from_str_radix(t, 8).map(|v| v as f64).unwrap_or(0.0);
    }
    longest_f64_prefix(t)
        .and_then(|p| p.parse::<f64>().ok())
        .unwrap_or(0.0)
}

/// Parse a string to f64, returning 0.0 for non-numeric. Handles leading/trailing whitespace.
#[inline]
fn parse_number(s: &str) -> f64 {
    if s.is_empty() {
        return 0.0;
    }
    let s = s.trim();
    if s.is_empty() {
        return 0.0;
    }
    if numeric_parse_mode() {
        return parse_number_strtonum(s);
    }
    // Hot path: decimal integers (e.g. `seq`, many data columns) without float parsing.
    if let Some(n) = parse_ascii_integer(s) {
        return n as f64;
    }
    longest_f64_prefix(s)
        .and_then(|p| p.parse::<f64>().ok())
        .unwrap_or(0.0)
}

/// Returns `Some(n)` only for strings that are exactly an optional sign + ASCII digits (awk-style int).
#[inline]
fn parse_ascii_integer(s: &str) -> Option<i64> {
    let b = s.as_bytes();
    let mut i = 0usize;
    let neg = match b.first().copied() {
        Some(b'-') => {
            i = 1;
            true
        }
        Some(b'+') => {
            i = 1;
            false
        }
        _ => false,
    };
    if i >= b.len() {
        return None;
    }
    let mut acc: i64 = 0;
    while i < b.len() {
        let d = b[i];
        if !d.is_ascii_digit() {
            return None;
        }
        acc = acc.checked_mul(10)?.checked_add((d - b'0') as i64)?;
        i += 1;
    }
    Some(if neg { -acc } else { acc })
}

/// Split `record` using gawk-style **FPAT** (each regex match is one field).
/// Returns `false` if `fpat` is not a valid regex (caller may fall back to FS).
/// Split a top-level alternation regex into its alternatives (split on `|`
/// outside of `[]` and `()`). Backslash-escaped chars are passed through
/// without splitting. Returns the original pattern as a single element when
/// no top-level `|` is present.
fn split_toplevel_alternatives(pat: &str) -> Vec<String> {
    let mut alts = Vec::new();
    let mut cur = String::new();
    let mut brackets = 0i32;
    let mut parens = 0i32;
    let mut chars = pat.chars().peekable();
    while let Some(c) = chars.next() {
        match c {
            '\\' => {
                cur.push(c);
                if let Some(&next) = chars.peek() {
                    cur.push(next);
                    chars.next();
                }
            }
            '[' => {
                brackets += 1;
                cur.push(c);
            }
            ']' if brackets > 0 => {
                brackets -= 1;
                cur.push(c);
            }
            '(' => {
                parens += 1;
                cur.push(c);
            }
            ')' if parens > 0 => {
                parens -= 1;
                cur.push(c);
            }
            '|' if brackets == 0 && parens == 0 => {
                alts.push(std::mem::take(&mut cur));
            }
            _ => cur.push(c),
        }
    }
    alts.push(cur);
    alts
}

/// FPAT splitting follows gawk's **leftmost-longest** semantic (POSIX-style),
/// NOT Rust's regex crate default of leftmost-first. With a pattern like
/// `[^,]*|"[^"]*"` against `abc,"def, ghi",xyz`, leftmost-first would pick the
/// first alternative at every position (splitting on commas inside quoted
/// regions); leftmost-longest correctly preserves quoted fields.
///
/// We approximate POSIX semantics by:
///   1. Splitting the FPAT into top-level alternatives on `|`.
///   2. At each position, trying every alternative anchored at that position
///      and picking the longest non-empty match.
///   3. Skipping positions with no non-empty match.
///
/// For a single-alternative pattern this collapses to the original behavior
/// (with empty-match skipping for safety).
fn split_fields_fpat(record: &[u8], fpat: &str, field_ranges: &mut Vec<(u32, u32)>) -> bool {
    field_ranges.clear();
    if record.is_empty() {
        return true;
    }
    // "We compile them once" used to mean once per *call*, and this runs once
    // per record — `FPAT="[0-9]+"` over 300 000 records cost 3.04 s of CPU
    // against gawk's 1.39 s. Memoised like the `FS` and `split()` engines.
    with_fpat_regexes(fpat, |compiled| {
        let Some(compiled) = compiled else {
            return false;
        };
        fpat_scan(record, compiled, field_ranges)
    })
}

/// The leftmost-longest scan, once the alternatives are compiled.
fn fpat_scan(record: &[u8], compiled: &[BytesRegex], field_ranges: &mut Vec<(u32, u32)>) -> bool {
    let n = record.len();
    let bytes = record;
    let mut pos = 0usize;
    while pos < n {
        let tail = &record[pos..];
        let mut best_end: Option<usize> = None;
        for re in compiled {
            if let Some(m) = re.find(tail) {
                // `^(?:…)` ensures m.start() == 0.
                let end = m.end();
                if end > 0 && best_end.is_none_or(|b| end > b) {
                    best_end = Some(end);
                }
            }
        }
        if let Some(end) = best_end {
            let abs_end = pos + end;
            field_ranges.push((pos as u32, abs_end as u32));
            pos = abs_end;
        } else {
            // Advance one char (UTF-8 safe).
            pos += utf8_char_len_at(bytes, pos);
        }
    }
    true
}

#[inline]
fn utf8_char_len_at(bytes: &[u8], pos: usize) -> usize {
    if pos >= bytes.len() {
        return 1;
    }
    let b = bytes[pos];
    // b < 0xC0 covers both ASCII (< 0x80) and continuation bytes (0x80..0xC0);
    // both advance by 1 byte. Continuation bytes shouldn't appear at a char
    // boundary, but treat them safely as 1-byte advances.
    if b < 0xC0 {
        1
    } else if b < 0xE0 {
        2
    } else if b < 0xF0 {
        3
    } else {
        4
    }
}

/// A FIELDWIDTHS spec entry: skip `skip` bytes before the field, then take `width` bytes.
/// `width == usize::MAX` means "take everything remaining" (gawk's `*` token).
#[derive(Clone, Copy, Debug)]
pub(crate) struct FieldwidthsSpec {
    pub(crate) skip: usize,
    pub(crate) width: usize,
}

const FIELDWIDTHS_REST: usize = usize::MAX;

fn split_fields_fieldwidths(
    record: &[u8],
    specs: &[FieldwidthsSpec],
    field_ranges: &mut Vec<(u32, u32)>,
) {
    field_ranges.clear();
    if specs.is_empty() {
        return;
    }
    let b = record;
    let n = b.len();
    let mut pos = 0usize;
    for spec in specs {
        // Skip leading bytes (gawk `skip:width`); cannot read past end.
        pos = (pos + spec.skip).min(n);
        let end = if spec.width == FIELDWIDTHS_REST {
            n
        } else {
            (pos + spec.width).min(n)
        };
        field_ranges.push((pos as u32, end as u32));
        pos = end;
        if pos >= n {
            break;
        }
    }
}

/// gawk `--csv` / `-k` field splitting: comma-separated, `"..."` for quoting, `""` for a literal `"`.
/// Field ranges are **value** byte ranges (no surrounding quote characters), matching gawk’s `$n` text.
/// gawk `--csv` field splitting (RFC 4180-ish): commas separate fields,
/// double-quoted fields may contain commas, and `""` inside a quoted field is
/// an escaped quote. Empty record → 0 fields; otherwise the field count is
/// always `(commas at top level) + 1`.
fn split_csv_gawk_fields(record: &[u8], field_ranges: &mut Vec<(u32, u32)>) {
    field_ranges.clear();
    let bytes = record;
    let n = bytes.len();
    if n == 0 {
        return;
    }
    let mut i = 0usize;
    loop {
        // Each iteration emits exactly one field starting at position `i`.
        if i < n && bytes[i] == b'"' {
            i += 1;
            let val_start = i;
            while i < n {
                if bytes[i] == b'"' {
                    if i + 1 < n && bytes[i + 1] == b'"' {
                        i += 2;
                        continue;
                    }
                    break;
                }
                i += 1;
            }
            let val_end = i;
            field_ranges.push((val_start as u32, val_end as u32));
            if i < n && bytes[i] == b'"' {
                i += 1;
            }
        } else {
            let val_start = i;
            while i < n && bytes[i] != b',' {
                i += 1;
            }
            field_ranges.push((val_start as u32, i as u32));
        }
        // After the field, expect EOR or a comma separator.
        if i >= n {
            return;
        }
        // bytes[i] == ',' — consume it and loop to emit the next (possibly
        // empty) field. This is what makes `,,,` produce 4 empty fields, not 3.
        debug_assert_eq!(bytes[i], b',');
        i += 1;
    }
}

/// Compile a regex `FS`. `None` when the pattern is not a valid regex, which
/// makes the splitter fall back to a literal split.
///
/// The one place the flags are set, so the memoised engine and the ad-hoc one
/// can never disagree: `IGNORECASE` applies (only a multi-character `FS` honours
/// it), and gawk lets `.` match a newline in an ERE, which matters once `RS`
/// allows a record to contain one.
///
/// Runs the same [`translate_awk_re_to_rust`] rewrite the `~` operator gets.
/// Without it a separator reached Rust's parser raw, so the awk spellings that
/// only the rewrite understands were lost: `split(s, a, "\\101")` and
/// `split(s, a, "[\\101]")` failed to compile and fell back to a literal split
/// where all three references split on `A`, `"\\8"` likewise where they split
/// on `8`, and `"\\d"` compiled as Rust's digit class where all three match a
/// literal `d`.
fn build_fs_regex(fs: &str, ignore_case: bool) -> Option<BytesRegex> {
    let translated = translate_awk_re_to_rust(fs);
    let mut b = regex::bytes::RegexBuilder::new(&translated);
    // Same locale rule as the `~` engine — see `Runtime::ensure_regex`.
    b.unicode(crate::locale_numeric::ctype_is_utf8());
    b.case_insensitive(ignore_case);
    b.dot_matches_new_line(true);
    b.build().ok()
}

/// The memoised engine for `fs`, or [`FsRegex::Unknown`] when the cache holds a
/// different pattern.
///
/// Takes the cache and the pattern as separate borrows rather than `&self`:
/// the caller needs `&mut self.field_ranges` at the same time, and a whole-`self`
/// borrow would rule that out.
fn memoised_fs_regex<'a>(
    cache: &'a Option<(String, bool, Option<BytesRegex>)>,
    fs: &str,
) -> FsRegex<'a> {
    match cache {
        Some((pat, _, engine)) if pat == fs => match engine {
            Some(re) => FsRegex::Compiled(re),
            None => FsRegex::Invalid,
        },
        _ => FsRegex::Unknown,
    }
}

/// Separator engines for `split()`, memoised per thread by pattern text.
///
/// Same finding as the record loop's `FS`, one call site along: the separator is
/// a pure function of its text and `IGNORECASE`, and `split()` inside a record
/// loop recompiled it for every record. Keyed by the pattern alone with the flag
/// stored beside the engine, so a hit costs no allocation; a pattern that does
/// not compile is memoised as `None` so the literal fallback does not pay for
/// the failed compile again either.
///
/// Bounded rather than unbounded: a program that splits on many distinct
/// computed separators would otherwise grow this without limit, and dropping the
/// whole map is correct at any moment — every entry is reconstructible.
const SPLIT_REGEX_MEMO_MAX: usize = 256;

thread_local! {
    static SPLIT_REGEX_MEMO: std::cell::RefCell<AwkMap<String, (bool, Option<BytesRegex>)>> =
        std::cell::RefCell::new(AwkMap::default());
}

/// Run `f` with the memoised engine for `fs` (`None` when `fs` is not a valid
/// regex). The engine is handed to a closure rather than returned because it
/// lives inside the thread-local map.
fn with_split_regex<R>(fs: &str, ignore_case: bool, f: impl FnOnce(Option<&BytesRegex>) -> R) -> R {
    SPLIT_REGEX_MEMO.with(|memo| {
        let mut memo = memo.borrow_mut();
        let fresh = match memo.get(fs) {
            Some((ic, _)) => *ic != ignore_case,
            None => true,
        };
        if fresh {
            if memo.len() >= SPLIT_REGEX_MEMO_MAX {
                memo.clear();
            }
            let compiled = build_fs_regex(fs, ignore_case);
            memo.insert(fs.to_string(), (ignore_case, compiled));
        }
        let entry = memo.get(fs).expect("just inserted");
        f(entry.1.as_ref())
    })
}

// Compiled `FPAT` alternatives, memoised per thread — the `with_split_regex`
// treatment for the other per-record regex. `None` records an `FPAT` that does
// not compile, which is what makes the splitter fall back to `FS`.
//
// No `IGNORECASE` in the key because `FPAT` does not honour it. gawk is the only
// reference that implements `FPAT` at all, and it matches case-sensitively
// regardless:
//
//   printf 'AB cd EF\n' | gawk 'BEGIN{FPAT="[a-z]+";IGNORECASE=1}{print NF, $1}'
//   1 cd          # awkrs answered `3 AB` — the alternatives were compiled
//                 # case-insensitively, so `AB` and `EF` became fields too.
thread_local! {
    static FPAT_REGEX_MEMO: std::cell::RefCell<AwkMap<String, Option<Vec<BytesRegex>>>> =
        std::cell::RefCell::new(AwkMap::default());
}

fn with_fpat_regexes<R>(fpat: &str, f: impl FnOnce(Option<&[BytesRegex]>) -> R) -> R {
    FPAT_REGEX_MEMO.with(|memo| {
        let mut memo = memo.borrow_mut();
        if !memo.contains_key(fpat) {
            if memo.len() >= SPLIT_REGEX_MEMO_MAX {
                memo.clear();
            }
            // Every alternative is anchored at the candidate position, so a
            // single failure makes the whole `FPAT` unusable.
            let compiled = split_toplevel_alternatives(fpat)
                .iter()
                .map(|alt| build_fs_regex(&format!("^(?:{alt})"), false))
                .collect::<Option<Vec<BytesRegex>>>();
            memo.insert(fpat.to_string(), compiled);
        }
        let entry = memo.get(fpat).expect("just inserted");
        f(entry.as_deref())
    })
}

/// What the splitter should use for a regex `FS`.
///
/// A multi-character `FS` is a regular expression, and the splitter used to
/// compile it from scratch inside every call — which is once per record in the
/// main loop. `awk -F'[;,]' '{ print $3 }'` over a million lines therefore built
/// the same automaton a million times: 1.98 s of CPU against mawk's 0.16 s on
/// the same file. The record loop now hands in the engine it memoised.
pub enum FsRegex<'a> {
    /// Nothing memoised — compile here. The `split()` builtin and the unit tests
    /// come in this way; they are not the per-record path.
    Unknown,
    /// The engine for exactly this `FS` and `IGNORECASE`.
    Compiled(&'a BytesRegex),
    /// This `FS` is not a valid regex, and the caller already knows it. Falls
    /// back to a literal split without paying for the failed compile again.
    Invalid,
}

/// Split `record` into `field_ranges` (replaces contents). Shared by lazy split and stdin path.
/// Split `record` into field byte-ranges using `fs`.
///
/// `paragraph_mode` is `RS == ""`. POSIX: *"When RS is null … <newline> shall
/// always be a field separator, no matter what the value of FS is."* In practice
/// gawk and one-true-awk apply that only to a **single-character** FS (gawk
/// rewrites FS to `[<fs>\n]` there and leaves a regex FS untouched); mawk does
/// not apply it at all. The default `FS == " "` needs nothing extra — newline is
/// already whitespace — so the flag is only consulted in the single-char branch.
/// Every occurrence of `from` in `hay` replaced by `to`.
fn byte_replace(hay: &[u8], from: &[u8], to: &[u8]) -> Vec<u8> {
    let mut out = Vec::with_capacity(hay.len());
    let mut start = 0usize;
    while let Some(off) = memchr::memmem::find(&hay[start..], from) {
        out.extend_from_slice(&hay[start..start + off]);
        out.extend_from_slice(to);
        start += off + from.len();
    }
    out.extend_from_slice(&hay[start..]);
    out
}

/// `[AwkStr]::join` — the pieces separated by `sep`, as one `AwkStr`.
fn join_awkstrs(parts: &[AwkStr], sep: &[u8]) -> AwkStr {
    let mut out = AwkStr::with_capacity(
        parts.iter().map(|p| p.len()).sum::<usize>() + sep.len() * parts.len().saturating_sub(1),
    );
    for (i, p) in parts.iter().enumerate() {
        if i > 0 {
            out.push_bytes(sep);
        }
        out.push_awkstr(p);
    }
    out
}

/// Byte length of the UTF-8 character starting at `b[0]`, or 1 when the bytes
/// there are not a valid encoding.
///
/// An unpaired byte counting as one character is what makes a character-wise
/// operation total over arbitrary input: the references have no multi-byte
/// character to fold it into either.
pub(crate) fn utf8_char_len(b: &[u8]) -> usize {
    let want = match b[0] {
        0x00..=0x7f => return 1,
        0xc2..=0xdf => 2,
        0xe0..=0xef => 3,
        0xf0..=0xf4 => 4,
        _ => return 1,
    };
    if b.len() >= want && std::str::from_utf8(&b[..want]).is_ok() {
        want
    } else {
        1
    }
}

/// `str::split` for byte strings: the pieces of `hay` between each
/// non-overlapping occurrence of `needle`. An empty `needle` yields `hay` whole,
/// matching what the literal-split fallback wants.
fn byte_split<'h>(hay: &'h [u8], needle: &[u8]) -> Vec<&'h [u8]> {
    if needle.is_empty() {
        return vec![hay];
    }
    let mut out = Vec::new();
    let mut start = 0usize;
    while let Some(off) = memchr::memmem::find(&hay[start..], needle) {
        out.push(&hay[start..start + off]);
        start += off + needle.len();
    }
    out.push(&hay[start..]);
    out
}

fn split_fields_into(
    record: &[u8],
    fs: &str,
    field_ranges: &mut Vec<(u32, u32)>,
    ignore_case: bool,
    characters_as_bytes: bool,
    paragraph_mode: bool,
    fs_re: FsRegex<'_>,
) {
    field_ranges.clear();
    // POSIX: an empty record has zero fields, whatever FS is. Without this the
    // single-char and regex branches below both push one empty range and report
    // `NF == 1` for a blank line under `FS=":"` — gawk, mawk and one-true-awk
    // all report 0. Only the default (" ") and empty-FS branches got this right.
    if record.is_empty() {
        return;
    }
    // Rough NF estimate from record length reduces per-line `Vec` growth for whitespace/FS splits.
    let want = (record.len() / 16).saturating_add(4).clamp(8, 2048);
    if field_ranges.capacity() < want {
        field_ranges.reserve(want - field_ranges.capacity());
    }
    if fs.is_empty() {
        if characters_as_bytes {
            for i in 0..record.len() {
                field_ranges.push((i as u32, (i + 1) as u32));
            }
        } else {
            // Character split. A byte that does not begin a valid UTF-8
            // character is its own field — the references have no multi-byte
            // character to group it into either.
            let mut i = 0usize;
            while i < record.len() {
                let step = utf8_char_len(&record[i..]);
                field_ranges.push((i as u32, (i + step) as u32));
                i += step;
            }
        }
    } else if fs == " " {
        let bytes = record;
        let len = bytes.len();
        let mut i = 0;
        while i < len && bytes[i].is_ascii_whitespace() {
            i += 1;
        }
        while i < len {
            let start = i;
            while i < len && !bytes[i].is_ascii_whitespace() {
                i += 1;
            }
            field_ranges.push((start as u32, i as u32));
            while i < len && bytes[i].is_ascii_whitespace() {
                i += 1;
            }
        }
    } else if fs.len() == 1 {
        // POSIX / gawk: single-char FS is always a **literal** match; IGNORECASE
        // explicitly does not apply (only multi-char regex FS honors IGNORECASE).
        let sep = fs.as_bytes()[0];
        let bytes = record;
        let mut start = 0;
        for (i, &b) in bytes.iter().enumerate() {
            if b == sep || (paragraph_mode && b == b'\n') {
                field_ranges.push((start as u32, i as u32));
                start = i + 1;
            }
        }
        field_ranges.push((start as u32, bytes.len() as u32));
    } else {
        // POSIX: multi-character FS is treated as a regular expression. The
        // paragraph-mode newline rule deliberately does NOT apply here — gawk,
        // mawk and one-true-awk all leave a regex FS alone (gawk only rewrites
        // FS to `[<fs>\n]` in the single-character branch), so
        // `RS=""; FS="[0-9]+"` on "a12b\nc345d" is three fields in every
        // reference, with "b\nc" as $2.
        let owned;
        let re: Option<&BytesRegex> = match fs_re {
            FsRegex::Compiled(re) => Some(re),
            FsRegex::Invalid => None,
            FsRegex::Unknown => {
                owned = build_fs_regex(fs, ignore_case);
                owned.as_ref()
            }
        };
        match re {
            Some(re) => {
                let mut last = 0;
                for m in re.find_iter(record) {
                    field_ranges.push((last as u32, m.start() as u32));
                    last = m.end();
                }
                field_ranges.push((last as u32, record.len() as u32));
            }
            None => {
                // Fall back to literal split if the FS is not a valid regex.
                let mut pos = 0;
                for part in byte_split(record, fs.as_bytes()) {
                    let end = pos + part.len();
                    field_ranges.push((pos as u32, end as u32));
                    pos = end + fs.len();
                }
            }
        }
    }
}
/// Value type shared by the fuse_chunk_cache + fuse_prefix_chunk_cache pair:
/// `Some(Some(Arc<(chunk, written_slots)>))` = cached + eligible;
/// `Some(None)` = checked + not eligible (short-circuit without rebuilding).
/// `None` (HashMap miss) = never checked.
pub type FuseChunkSlot = Option<Arc<(fusevm::Chunk, Vec<u16>)>>;

/// `Runtime` — see fields for the structure layout.
pub struct Runtime {
    /// `vars` field.
    pub vars: AwkMap<String, Value>,
    /// Post-`BEGIN` globals shared across parallel record workers (`Arc` clone is O(1)).
    /// Reads resolve `vars` first (per-record overlay), then this map. Not used in the main thread.
    pub global_readonly: Option<Arc<AwkMap<String, Value>>>,
    /// Owned field strings — only populated when a field is modified via `set_field`.
    pub fields: Vec<AwkStr>,
    /// Zero-copy field byte-ranges into `record`. Each `(start, end)` is a byte offset.
    pub field_ranges: Vec<(u32, u32)>,
    /// True when `set_field` has been called and `fields` vec is authoritative.
    pub fields_dirty: bool,
    /// True when record has been set but fields have not been split yet.
    pub fields_pending_split: bool,
    /// Cached FS for lazy field splitting.
    pub cached_fs: String,
    /// `record` field.
    pub record: AwkStr,
    /// True once `$0` has ever been given a value — by reading a record, by
    /// `getline`, or by assigning `$0` / `$n` / `NF`. Only `typeof($0)` reads
    /// it: gawk reports `"unassigned"` for `$0` in a `BEGIN` that has not
    /// touched the record yet and `"string"` afterwards, and merely *reading*
    /// `$0` does not flip it (`BEGIN { x = $0 ""; print typeof($0) }` is still
    /// `"unassigned"`). Every write goes through one of the four places that
    /// assign `self.record`, so the flag is set there rather than at each caller.
    pub record_assigned: bool,
    /// Whether `$0` is a POSIX *numeric string* (see [`Value::Str`] vs
    /// [`Value::StrLit`]). A record read from input is; a record **assigned** a
    /// plain string is not, so `$0 = "42"` makes `$0 < 7` a *string* compare —
    /// `"42" < "7"` — in gawk, mawk and one-true-awk alike, where awkrs used to
    /// re-derive numeric-string-ness from the text and answer numerically.
    /// Rebuilding `$0` from fields (`$n = …`, `NF = …`) also produces a plain
    /// string in gawk and one-true-awk; mawk keeps it a numeric string there and
    /// is the outlier, so this follows the two-reference majority.
    pub record_strnum: bool,
    /// Per-field counterpart of [`record_strnum`](Self::record_strnum), parallel
    /// to `fields` and only consulted while `fields_dirty` — fields produced by
    /// *splitting* a record are always numeric strings, whatever the record was.
    /// An index past the end reads as `true`, so leaving this empty preserves
    /// the split-derived default.
    pub field_strnum: Vec<bool>,
    /// Reusable buffer for input line reading (avoids per-line allocation).
    pub line_buf: Vec<u8>,
    /// Bytes read past the previous record's terminator that belong to the next
    /// record(s). Used by streaming regex/multi-char `RS` to avoid losing input
    /// that was over-consumed in chunked reads.
    pub read_leftover: Vec<u8>,
    /// `nr` field.
    pub nr: f64,
    /// `fnr` field.
    pub fnr: f64,
    /// `filename` field.
    pub filename: String,
    /// Set by `exit`; END rules run before process exit (POSIX).
    pub exit_pending: bool,
    /// `exit_code` field.
    pub exit_code: i32,
    /// Primary input stream for `getline` without `< file` (same as main record loop).
    pub input_reader: Option<SharedInputReader>,
    /// Set once the main record loop has run the primary stream to completion and
    /// detached it. A later plain `getline` (typically in `END`) is then at EOF and
    /// must return `0`, not raise "only valid during normal input" — gawk, mawk and
    /// one-true-awk all return `0` there.
    pub primary_input_done: bool,
    /// `GAWK_READ_TIMEOUT` once read — see [`Self::read_timeout_env_ms`].
    pub read_timeout_env: Cell<Option<i32>>,
    /// Memoised regex `FS`: `(pattern, IGNORECASE, engine)`, where a `None`
    /// engine records a pattern that does not compile. See [`FsRegex`] for why
    /// the record loop cannot afford to build this per record.
    pub fs_regex: Option<(String, bool, Option<BytesRegex>)>,
    /// Open files for `getline < path` / `close`.
    pub file_handles: HashMap<String, BufReader<File>>,
    /// [`Self::read_leftover`], per redirected `getline` stream.
    ///
    /// A regex or multi-character `RS` reads in chunks and can over-consume past
    /// a record's terminator, so each stream needs its own carry-over buffer.
    /// Keyed by the same path/command string as `file_handles`, `pipe_stdout`
    /// and `coproc_handles`, and dropped by `close`.
    pub getline_leftover: HashMap<String, Vec<u8>>,
    /// Directory iteration for `getline var < dir` (gawk **readdir** extension semantics).
    pub dir_read: HashMap<String, (Vec<String>, usize)>,
    /// Open files for `print … > path` / `print … >> path` / `fflush` / `close`.
    pub output_handles: HashMap<String, BufWriter<File>>,
    /// `print`/`printf` `| "cmd"` — stdin of `sh -c cmd` (key is the command string).
    pub pipe_stdin: HashMap<String, BufWriter<ChildStdin>>,
    /// `pipe_children` field.
    pub pipe_children: HashMap<String, Child>,
    /// `"cmd" | getline …` — stdout of `sh -c cmd`, kept open between calls so the
    /// command runs **once** per logical pipe and subsequent `getline`s advance
    /// through the same stream (matches gawk; pre-fix awkrs respawned every call).
    pub pipe_stdout: HashMap<String, BufReader<std::process::ChildStdout>>,
    /// `pipe_input_children` field.
    pub pipe_input_children: HashMap<String, Child>,
    /// `print`/`printf` `|& "cmd"` / `getline <& "cmd"` — two-way `sh -c` (same key for both directions).
    pub coproc_handles: HashMap<String, CoprocHandle>,
    /// gawk `/inet/tcp/...` TCP streams (read half).
    pub inet_tcp_read: HashMap<String, BufReader<TcpStream>>,
    /// gawk `/inet/tcp/...` TCP streams (write half).
    pub inet_tcp_write: HashMap<String, TcpStream>,
    /// gawk `/inet/udp/...` connected UDP sockets (one per path; `recv` / `send` datagrams).
    pub inet_udp: HashMap<String, UdpSocket>,
    /// Last `bindtextdomain` directory (gettext stub / future real i18n).
    pub gettext_dir: String,
    /// `-M` / `--bignum`: use MPFR ([`Value::Mpfr`]) for arithmetic in the VM.
    pub bignum: bool,
    /// `rand_seed` field.
    pub rand_seed: u64,
    /// Radix for `%f` / `%g` / etc. and `print` of numbers when `-N` / `--use-lc-numeric` is set (Unix).
    pub numeric_decimal: char,
    /// Thousands separator for gawk **`%'`** (`printf` / `sprintf` integer grouping), from `localeconv()` when available.
    pub numeric_thousands_sep: Option<char>,
    /// Indexed variable slots for the bytecode VM (fast Vec access instead of HashMap).
    pub slots: Vec<Value>,
    /// Parallel to [`Self::slots`]: has this slot ever been *used* — read as a
    /// value or written to? Slots are allocated at compile time and start
    /// [`Value::Uninit`], so without this bit a name the program never mentions
    /// outside `typeof` is indistinguishable from one that was read or assigned
    /// an uninitialized value. gawk separates the two: the first is
    /// **`"untyped"`**, the second **`"unassigned"`**. `typeof` itself does not
    /// set the bit — `BEGIN{ print typeof(z); print typeof(z) }` stays
    /// `untyped` twice in gawk.
    pub slot_touched: Vec<bool>,
    /// Compiled regex cache (case-sensitive) — avoids recompiling the same pattern every record.
    pub regex_cache_cs: AwkMap<Vec<u8>, BytesRegex>,
    /// Compiled regex cache when [`Self::ignore_case_flag`] is true.
    pub regex_cache_ci: AwkMap<Vec<u8>, BytesRegex>,
    /// Cached substring searchers for literal `sub`/`gsub` patterns — faster than `str::contains` per line.
    pub memmem_finder_cache: AwkMap<Vec<u8>, memmem::Finder<'static>>,
    /// Persistent stdout buffer — shared across record iterations, flushed at file boundaries.
    pub print_buf: Vec<u8>,
    /// Cached OFS bytes — avoids HashMap lookup + Vec alloc on every `print` call.
    pub ofs_bytes: Vec<u8>,
    /// Cached ORS bytes — avoids HashMap lookup + Vec alloc on every `print` call.
    pub ors_bytes: Vec<u8>,
    /// Reusable VM stack — avoids malloc/free per VmCtx creation.
    pub vm_stack: Vec<Value>,
    /// `-k` / `--csv` (gawk-style): use [`split_csv_gawk_fields`] instead of `FPAT` / `FS` for `$n`.
    pub csv_mode: bool,
    /// gawk: `RS` longer than one character is a regex delimiter (cached here).
    pub rs_pattern_for_regex: String,
    /// `rs_regex_bytes` field.
    pub rs_regex_bytes: Option<BytesRegex>,
    /// gawk `--sandbox` / `-S`: disallow file redirects, pipes, coprocesses, inet, `system()`.
    pub sandbox: bool,
    /// gawk `-b` / `--characters-as-bytes`: `length` / `substr` / `index` use byte units (otherwise UTF-8 character units).
    pub characters_as_bytes: bool,
    /// gawk `--posix` / `-P` (reserved; stricter POSIX checks may be added incrementally).
    pub posix: bool,
    /// gawk `--traditional` / `-c` (reserved; traditional awk rules may be added incrementally).
    pub traditional: bool,
    /// Bytecode JIT (`-s` / `--no-optimize` disables when set).
    pub jit_enabled: bool,
    /// Cache of fusevm::Chunk built from awkrs Chunks via
    /// `fusevm_bridge::build_numeric_chunk`. Keyed by (chunk pointer, bignum
    /// flag): the bridge builder does an eligibility check + 2-pass op→fusevm
    /// translation that's identical for the same (chunk, bignum) combo across
    /// every record. Caching skips both passes on subsequent dispatches.
    /// `Some(None)` = checked, not eligible — short-circuit without rebuilding.
    /// The cache value is `Option<Arc<(Chunk, Vec<u16>)>>`. The second tuple
    /// element is the sorted-unique set of slot indices the chunk WRITES (via
    /// `fusevm::Op::SetSlot`), precomputed once so the per-record writeback
    /// only touches modified slots. `Arc`-wrapped so the per-record cache hit
    /// is a refcount bump rather than a deep clone of the `Vec<u16>`.
    /// fusevm's own native-code cache (op_hash-keyed TLS + on-disk) caches the
    /// JIT result; this cache catches the upstream translation work that
    /// previously rebuilt the chunk per record AND the per-record writeback
    /// over-iteration.
    pub fuse_chunk_cache: HashMap<(usize, bool), FuseChunkSlot>,
    /// Single-slot side-table that hoists the HashMap lookup out of the
    /// per-record dispatch path. Stores the cache key + Arc of the LAST
    /// chunk we looked up. For an awk program with a single record-rule
    /// (the common case), every record hits this and skips the
    /// `fuse_chunk_cache` HashMap lookup entirely; for N-rule programs the
    /// hit rate is N-1/N (each rule swaps the slot once per record). Updated
    /// only on HashMap miss; cleared in the Runtime constructors.
    pub fuse_last_chunk_key: (usize, bool),
    pub fuse_last_chunk_value: FuseChunkSlot,
    /// Same as [`fuse_chunk_cache`] but for `run_fusevm_region` which lowers
    /// just the eligible *prefix* of a chunk (when the whole chunk isn't
    /// eligible). Keyed by (slice base pointer, slice length, bignum) — the
    /// prefix length is determined by the chunk content so the same chunk
    /// always yields the same slice. Same `Arc<(Chunk, written-slots)>` value.
    pub fuse_prefix_chunk_cache: HashMap<(usize, usize, bool), FuseChunkSlot>,
    /// Recycled `fusevm::VM` instances. `try_fusevm_dispatch` / `run_fusevm_region`
    /// acquire a VM from the pool (which calls `VM::reset(chunk)` preserving
    /// internal Vec capacities — stack, frames, slot_buf, etc.) instead of
    /// allocating a fresh `fusevm::VM::new(chunk)` on every record. For an awk
    /// one-liner over millions of records, this skips per-record allocation of
    /// the VM's stack/frame storage. The pool is per-Runtime; parallel record
    /// workers get their own (fresh empty pool on Runtime clone).
    pub fuse_vm_pool: fusevm::VMPool,
    /// GNU MO catalogs loaded by `bindtextdomain` (domain → catalog).
    pub gettext_catalogs: AwkMap<String, Arc<Catalog>>,
    /// Copy of [`crate::bytecode::CompiledProgram::slot_map`] for SYMTAB / `array_keys` without VM context.
    /// Name -> slot index, with the fast hasher for the same reason
    /// [`crate::bytecode::CompiledProgram::slot_map`] uses it.
    pub symtab_slot_map: AwkMap<String, u16>,
    /// Decimal integer literals, parsed once, indexed by string-pool index.
    ///
    /// A literal is stored as its digits so `-M` can render it at full
    /// precision, but the ordinary path then parsed those digits to `f64` on
    /// every execution — a counted loop paid a full float parse per iteration
    /// for a constant. Filled on first use; empty under `-M`, which needs the
    /// digits themselves.
    pub decimal_lits: Vec<f64>,
    /// DAP debugger state. `Some` only under `awkrs --dap`; drives breakpoints,
    /// stepping, and variable inspection. The VM checks it on each
    /// [`crate::bytecode::Op::DebugLine`] marker.
    pub debugger: Option<crate::debugger::Debugger>,
    /// Active call stack for the debugger: `(function name, call-site line)`,
    /// innermost last. Only maintained when [`Self::debugger`] is set.
    pub debug_call_stack: Vec<(String, usize)>,
    /// Current source line, updated by `Op::DebugLine`. 0 when not debugging.
    pub cur_line: u32,
    /// `-p` / `--profile`: invocation count per **record** rule (index matches `CompiledProgram::record_rules`).
    pub profile_record_hits: Vec<u64>,
    /// One-shot: warn once when `PROCINFO["sorted_in"]` is set to an unsupported custom comparator name.
    pub sorted_in_warned: Cell<bool>,
    /// Last OS errno from [`Self::set_errno_io`] (gawk **`PROCINFO["errno"]`** numeric mirror).
    pub errno_code: i32,
    /// Registered AOP function-call intercepts (before/after/around advice).
    /// awkrs/zshrs-original extension; see [`crate::intercepts`]. Cloned into
    /// parallel record workers (cheap `Arc` refcount bumps) so advice fires
    /// regardless of which worker runs the intercepted call.
    pub intercepts: Vec<crate::intercepts::Intercept>,
    /// Live AOP call stack — pushed while an intercept set is firing so
    /// `intercept_proceed()` (from *around* advice) can reach the original
    /// function name + argument values. Fresh (empty) in every clone.
    pub intercept_call_stack: Vec<crate::intercepts::InterceptCall>,
    /// Unix: raw fd for [`libc::poll`] before reads on the primary input stream (stdin or first file).
    #[cfg(unix)]
    pub primary_input_poll_fd: Option<std::os::unix::io::RawFd>,
}

/// Translate an awk-style regex pattern to Rust regex syntax for the cases where
/// gawk's POSIX-ERE-with-extensions semantics diverge from Rust's regex engine:
///
///   - `\1`..`\9` (POSIX ERE has no backrefs; gawk treats as literal `\N`,
///     Rust regex parses as backreference and errors). Escape to `\\N`.
///   - `\d`, `\w`, `\s`, `\D`, `\W`, `\S` (Rust regex char classes; gawk emits
///     a warning and treats them as the literal trailing letter — e.g. `\d` ≡ `d`).
///     Strip the leading backslash so Rust sees a literal letter.
///
/// Escapes inside `[...]` bracket expressions are NOT translated — gawk's
/// bracket-interior semantics overlap enough with Rust's that the divergence
/// surface is small. The function tracks bracket-expression state by counting
/// unescaped `[` / `]` and only translates at top level.
/// One record from a redirected `getline` stream, honouring `RS`.
///
/// `getline < file`, `cmd | getline` and the coprocess differ only in which map
/// holds the reader, so the `RS` handling lives here. Returns the record with
/// its separator already removed — under the default `RS`, that means the
/// trailing newline and *only* the newline: a `\r` before it belongs to the
/// record, exactly as it does in the main loop. The old readers called
/// `BufRead::read_line` and the caller then trimmed `['\n', '\r']`, so a CRLF
/// file read through `getline` reported `length` one short of what the same file
/// read as ordinary input reported, and one short of all three references.
///
/// Returns the record and the separator text (for `RT`).
fn read_record_from_stream<R: BufRead>(
    reader: &mut R,
    rs: &str,
    regex_rs: Option<&BytesRegex>,
    leftover: &mut Vec<u8>,
) -> Result<Option<(String, Vec<u8>)>> {
    let mut buf = Vec::new();
    let mut sep = Vec::new();
    let got = crate::record_io::read_next_record_from(
        reader, rs, &mut buf, &mut sep, regex_rs, leftover,
    )?;
    if !got {
        return Ok(None);
    }
    // Only the newline path leaves its terminator in the record; every other
    // `RS` form hands the separator back through `sep` instead.
    let end = if rs == "\n" {
        crate::record_io::trim_end_record_bytes(&buf)
    } else {
        buf.len()
    };
    Ok(Some((
        String::from_utf8_lossy(&buf[..end]).into_owned(),
        sep,
    )))
}

/// [`translate_awk_re_to_rust`] for a pattern that is itself a byte string.
///
/// `regex::bytes::RegexBuilder` still takes its pattern as `&str`, so a byte
/// that no `&str` can name is written into the pattern source as `\xNN` — which,
/// with Unicode mode off, is exactly that one byte. Valid UTF-8 runs go through
/// the ordinary rewrite unchanged, so a pattern that is all text behaves exactly
/// as it did.
fn translate_awk_re_bytes_to_rust(pat: &[u8]) -> String {
    match std::str::from_utf8(pat) {
        Ok(text) => translate_awk_re_to_rust(text),
        Err(_) => {
            let mut src = String::with_capacity(pat.len() * 2);
            for chunk in pat.utf8_chunks() {
                src.push_str(&translate_awk_re_to_rust(chunk.valid()));
                for b in chunk.invalid() {
                    src.push_str(&format!("\\x{b:02x}"));
                }
            }
            src
        }
    }
}

fn translate_awk_re_to_rust(pat: &str) -> String {
    // Iterate characters, not bytes. Every rule below keys off an ASCII
    // character, but a byte loop that ends in `byte as char` latin-1-widens
    // each half of a multi-byte sequence: `é` (0xC3 0xA9) came out as `é`,
    // which re-encodes to four bytes and can never match the two-byte `é` in
    // the subject. `"café" ~ /é/` was therefore false in every locale, while
    // `gsub(/é/, ...)` worked because a metacharacter-free pattern takes the
    // literal-substring fast path and never reaches this translator.
    let chars: Vec<char> = pat.chars().collect();
    let mut out = String::with_capacity(pat.len() + 4);
    let mut i = 0;
    let mut in_bracket = false;
    // Open capture groups, so an unmatched `)` can be recognised as a literal.
    let mut depth: i32 = 0;
    while i < chars.len() {
        let c = chars[i];
        // POSIX/gawk octal escape: a backslash followed by up to three octal
        // digits is the character with that code, and it is recognised inside a
        // bracket expression as well as outside one. gawk, mawk and
        // one-true-awk all agree — `/\141/` matches `a`, `/\12/` matches a
        // newline, `/\1411/` matches `a1`, and `/[\101]/` matches `A`.
        //
        // awkrs used to rewrite `\1`..`\9` into a *literal* backslash-digit,
        // which matches nothing any of those patterns are looking for, and left
        // the in-bracket spelling for Rust's regex parser, which rejected it
        // outright: `BEGIN { print ("A" ~ /[\101]/) }` died with "backreferences
        // are not supported" where every reference prints 1.
        if c == '\\' && i + 1 < chars.len() && chars[i + 1].is_digit(8) {
            let mut code = 0u32;
            let mut digits = 0;
            while digits < 3 {
                match chars.get(i + 1 + digits) {
                    Some(d) if d.is_digit(8) => {
                        code = code * 8 + d.to_digit(8).expect("checked octal digit");
                        digits += 1;
                    }
                    _ => break,
                }
            }
            // `\x{…}` is the one spelling Rust's regex parser accepts in both
            // positions, so the same rewrite serves inside and outside brackets.
            out.push_str(&format!("\\x{{{code:x}}}"));
            i += 1 + digits;
            continue;
        }
        if c == '\\' && in_bracket {
            // Inside a bracket expression the character escapes still mean their
            // character, but the class shorthands do not name a class — all
            // three references agree, and precisely: `[\t]` matches a tab and
            // `[\n]` a newline, while `[\w]` matches the letter `w` and neither
            // a backslash nor a digit. So a shorthand loses its backslash and
            // becomes the plain letter, exactly as an unknown escape does
            // outside a bracket. Rust reads `[\w]` as the word class, which
            // matched a digit.
            let Some(&next) = chars.get(i + 1) else {
                out.push_str("\\\\");
                i += 1;
                continue;
            };
            if matches!(next, 't' | 'n' | 'r' | 'f' | 'v' | 'a' | 'x')
                || !next.is_ascii_alphanumeric()
            {
                out.push('\\');
                out.push(next);
            } else {
                out.push(next);
            }
            i += 2;
            continue;
        }
        if c == '\\' && i + 1 < chars.len() && !in_bracket {
            let next = chars[i + 1];
            if matches!(next, '8' | '9') {
                // Not an octal digit, so not an escape at all: gawk warns
                // "regexp escape sequence `\8' treated as plain `8'" and matches
                // the bare digit. mawk and one-true-awk do the same silently.
                out.push(next);
                i += 2;
                continue;
            }
            if matches!(next, 'd' | 'D') {
                // gawk doesn't support `\d`/`\D` (only `\w`/`\W`/`\s`/`\S`):
                // it emits "regexp escape sequence `\d' is not a known regexp
                // operator" and treats `\d` as the literal letter. Rust regex
                // would interpret as digit class — strip the `\` to literal.
                out.push(next);
                i += 2;
                continue;
            }
            if !rust_knows_escape(next) {
                // An escape Rust's parser does not know is a hard error there,
                // while gawk, mawk and one-true-awk all read it as the plain
                // character: `"QaE" ~ "\Qa\E"` matches in all three, and
                // awkrs died with "unrecognized escape sequence". Emit the
                // character, escaped when it is a metacharacter.
                if !next.is_ascii_alphanumeric() {
                    out.push('\\');
                }
                out.push(next);
                i += 2;
                continue;
            }
            // Other escapes (`\.`, `\(`, `\n`, `\t`, `\b`, `\B`, `\xHH`,
            // etc.) — pass through unchanged.
            out.push('\\');
            out.push(next);
            i += 2;
            continue;
        }
        if !in_bracket && c == ')' && depth == 0 {
            // An unmatched `)` is a literal in gawk, mawk and one-true-awk —
            // `"a))b" ~ "))"` matches in all three — and a hard error in Rust's
            // parser, which took awkrs's `~` down with it.
            out.push_str("\\)");
            i += 1;
            continue;
        }
        if !in_bracket && c == '(' {
            depth += 1;
        } else if !in_bracket && c == ')' {
            depth -= 1;
        }
        if !in_bracket && c == '{' && interval_end(&chars, i).is_none() {
            // A `{` that does not open a valid interval is a literal brace in
            // all three references (`"a{b" ~ "{"` matches); Rust rejects it.
            out.push_str("\\{");
            i += 1;
            continue;
        }
        if in_bracket
            && c == '-'
            && i > 0
            && i + 1 < chars.len()
            && chars[i + 1] != ']'
            && chars[i - 1] > chars[i + 1]
        {
            // A reversed range (`[z-a]`) is a hard error in Rust and in gawk,
            // but mawk and one-true-awk read the three characters literally —
            // `"z-a" ~ "[z-a]"` matches in both — so the majority is the literal
            // set. Escaping the `-` gives exactly that.
            out.push_str("\\-");
            i += 1;
            continue;
        }
        if c == '[' && !in_bracket {
            in_bracket = true;
        } else if c == ']' && in_bracket {
            in_bracket = false;
        }
        out.push(c);
        i += 1;
    }
    out
}

/// Whether Rust's regex parser gives `\<c>` a meaning.
///
/// Anything outside this set is a hard parse error there, while gawk, mawk and
/// one-true-awk all read the escape as the plain character. The list is the
/// escapes `regex-syntax` accepts: the C-style ones, the class shorthands, the
/// anchors, the numeric forms, and any punctuation (which escapes itself).
fn rust_knows_escape(c: char) -> bool {
    matches!(
        c,
        'a' | 'f'
            | 't'
            | 'n'
            | 'r'
            | 'v'
            | '0'
            | 'A'
            | 'z'
            | 'Z'
            | 'b'
            | 'B'
            | 's'
            | 'S'
            | 'w'
            | 'W'
            | 'x'
            | 'u'
            | 'U'
    ) || !c.is_ascii_alphanumeric()
}

// Verdict cache for `check_ere_separator`, keyed on the separator text.
//
// `FS` is re-read once per record and essentially never changes, so the check
// has to be free in the steady state; the walk itself only runs when the text
// differs from the last one seen.
thread_local! {
    static ERE_SEPARATOR_MEMO: std::cell::RefCell<Option<(String, Option<&'static str>)>> =
        const { std::cell::RefCell::new(None) };
}

/// Fatal-check a separator that is about to be used as a regular expression —
/// `FS`, or the third argument of `split()`.
///
/// A one-character separator is a literal in every reference (POSIX: "if `FS`
/// is any other single character, that character is used as the separator"), so
/// `FS="["` splits on a literal `[` and is never a regex error. Only multi-
/// character separators are walked.
pub fn check_ere_separator(pat: &str) -> std::result::Result<(), String> {
    if pat.chars().nth(1).is_none() {
        return Ok(());
    }
    let verdict = ERE_SEPARATOR_MEMO.with(|memo| {
        let mut memo = memo.borrow_mut();
        match memo.as_ref() {
            Some((seen, verdict)) if seen == pat => *verdict,
            _ => {
                let verdict = ere_reject_reason(pat);
                *memo = Some((pat.to_string(), verdict));
                verdict
            }
        }
    });
    match verdict {
        Some(why) => Err(format!("invalid regexp: {why}: /{pat}/")),
        None => Ok(()),
    }
}

/// Reject the ERE spellings that gawk, mawk *and* one-true-awk all refuse to
/// compile, so `FS` and `split()` can be fatal on them the way the references
/// are instead of silently degrading to a literal split.
///
/// Deliberately narrower than "whatever Rust's regex crate accepts". The two
/// acceptance sets differ in both directions, and only the patterns all three
/// references reject may be made fatal here — anything the references disagree
/// about keeps the existing literal-split fallback. Observed on gawk 5.4.1,
/// mawk 1.3.4 and one-true-awk 20200816, splitting `"xayb"`:
///
/// | pattern    | gawk  | mawk  | b-awk | verdict           |
/// |------------|-------|-------|-------|-------------------|
/// | `[[`       | fatal | fatal | fatal | rejected here     |
/// | `[]`       | fatal | fatal | fatal | rejected here     |
/// | `[^]`      | fatal | fatal | fatal | rejected here     |
/// | `[a-`      | fatal | fatal | fatal | rejected here     |
/// | `[[:alpha:]` | fatal | fatal | fatal | rejected here   |
/// | `((`       | fatal | fatal | fatal | rejected here     |
/// | `a{2,1}`   | fatal | fatal | fatal | rejected here     |
/// | `{2}`      | fatal | fatal | fatal | rejected here     |
/// | `*x`       | fatal | fatal | fatal | rejected here     |
/// | `+x`       | fatal | fatal | fatal | rejected here     |
/// | `?x`       | fatal | fatal | fatal | rejected here     |
/// | `(?:a)`    | fatal | fatal | fatal | rejected here     |
/// | `))`       | ok    | ok    | ok    | accepted          |
/// | `{`, `a{`  | ok    | ok    | ok    | accepted          |
/// | `a{1,`     | fatal | ok    | fatal | accepted (2/3)    |
/// | `|x`       | ok    | fatal | fatal | accepted (2/3)    |
/// | `[z-a]`    | fatal | ok    | ok    | accepted (1/3)    |
/// | `[[:foo:]]`| fatal | fatal | ok    | accepted (2/3)    |
/// | `[a-b-c]`  | fatal | ok    | ok    | accepted (1/3)    |
/// | `()`,`(|)` | ok    | fatal | mixed | accepted          |
///
/// Returns the reason as `Err` so the caller can name the offending pattern.
fn ere_reject_reason(pat: &str) -> Option<&'static str> {
    let c: Vec<char> = pat.chars().collect();
    let n = c.len();
    let mut i = 0;
    let mut depth = 0i32;
    // True while the next token would be the first atom of a branch, i.e. at the
    // start of the pattern or straight after `(`. A quantifier there is what all
    // three references call "illegal primary" / "unbalanced" / "no preceding
    // regular expression". Not tracked after `|`: `|x` is fatal in only two of
    // the three, so it stays accepted.
    let mut need_atom = true;
    while i < n {
        match c[i] {
            '\\' => {
                // Any escape is one atom; a trailing backslash is accepted by
                // two of the three references, so it is not rejected here.
                i += if i + 1 < n { 2 } else { 1 };
                need_atom = false;
            }
            '[' => {
                match bracket_end(&c, i) {
                    Some(end) => i = end + 1,
                    None => return Some("unterminated bracket expression"),
                }
                need_atom = false;
            }
            '(' => {
                depth += 1;
                i += 1;
                need_atom = true;
            }
            ')' => {
                // An unmatched `)` is a literal in every reference, so it only
                // closes a group that is actually open.
                if depth > 0 {
                    depth -= 1;
                }
                i += 1;
                need_atom = false;
            }
            '*' | '+' | '?' => {
                if need_atom {
                    return Some("repetition operator with no preceding expression");
                }
                i += 1;
            }
            '{' => {
                match interval_end(&c, i) {
                    // A well-formed interval: it quantifies, so it needs an atom
                    // and its bounds have to be ordered.
                    Some((end, lo, hi)) => {
                        if need_atom {
                            return Some("repetition operator with no preceding expression");
                        }
                        if let (Some(lo), Some(hi)) = (lo, hi) {
                            if lo > hi {
                                return Some("invalid interval expression");
                            }
                        }
                        i = end + 1;
                    }
                    // Not an interval at all (`{`, `a{`, `a{1,`): a literal
                    // brace in mawk and one-true-awk, so it is an atom.
                    None => {
                        i += 1;
                        need_atom = false;
                    }
                }
            }
            _ => {
                i += 1;
                need_atom = false;
            }
        }
    }
    if depth > 0 {
        return Some("unbalanced (");
    }
    None
}

/// Index of the `]` that closes the bracket expression opening at `open`, or
/// `None` when the pattern runs out first.
///
/// POSIX bracket rules: a leading `]` (after an optional `^`) is a literal, and
/// `[:class:]`, `[.coll.]` and `[=equiv=]` swallow their own closing `]`.
fn bracket_end(c: &[char], open: usize) -> Option<usize> {
    let n = c.len();
    let mut i = open + 1;
    if i < n && c[i] == '^' {
        i += 1;
    }
    if i < n && c[i] == ']' {
        i += 1;
    }
    while i < n {
        if c[i] == '[' && i + 1 < n && matches!(c[i + 1], ':' | '.' | '=') {
            let kind = c[i + 1];
            let mut j = i + 2;
            while j + 1 < n && !(c[j] == kind && c[j + 1] == ']') {
                j += 1;
            }
            if j + 1 >= n {
                return None;
            }
            i = j + 2;
            continue;
        }
        if c[i] == ']' {
            return Some(i);
        }
        i += 1;
    }
    None
}

/// Parse `{n}`, `{n,}` or `{n,m}` starting at `open`, returning the index of the
/// closing brace and the bounds. `None` when the text is not a valid interval,
/// which every reference then treats as a literal `{`.
fn interval_end(c: &[char], open: usize) -> Option<(usize, Option<u64>, Option<u64>)> {
    let n = c.len();
    let mut i = open + 1;
    let lo_start = i;
    while i < n && c[i].is_ascii_digit() {
        i += 1;
    }
    if i == lo_start {
        return None;
    }
    let lo: u64 = c[lo_start..i].iter().collect::<String>().parse().ok()?;
    if i < n && c[i] == '}' {
        return Some((i, Some(lo), Some(lo)));
    }
    if i >= n || c[i] != ',' {
        return None;
    }
    i += 1;
    let hi_start = i;
    while i < n && c[i].is_ascii_digit() {
        i += 1;
    }
    if i >= n || c[i] != '}' {
        return None;
    }
    let hi = if i == hi_start {
        None
    } else {
        Some(c[hi_start..i].iter().collect::<String>().parse().ok()?)
    };
    Some((i, Some(lo), hi))
}

impl Runtime {
    /// `new` — see implementation for the contract.
    pub fn new() -> Self {
        // gawk parity: `printf "%'d"` consults the locale's `thousands_sep`
        // regardless of `-N`/`--use-lc-numeric`. Activate LC_NUMERIC from the
        // environment unconditionally so `localeconv()` returns the user's
        // grouping char (empty under `LC_ALL=C`, "," under `en_US.UTF-8`, etc.).
        // The decimal point is still kept as '.' unless `-N` is passed (gawk's
        // behavior).
        crate::locale_numeric::set_locale_numeric_from_env();
        let mut vars = AwkMap::default();
        // POSIX/gawk: FS defaults to " " (single space — special-cased to mean
        // "split on runs of whitespace"). awkrs's splitter behavior matches this
        // even when FS is "", but exposing FS as "" makes user code like
        // `if (FS == " ")` fail unnecessarily.
        vars.insert("FS".into(), Value::Str(" ".into()));
        vars.insert("OFS".into(), Value::Str(" ".into()));
        vars.insert("ORS".into(), Value::Str("\n".into()));
        vars.insert("OFMT".into(), Value::Str("%.6g".into()));
        // POSIX: number→string coercion (distinct from OFMT, which is for print).
        vars.insert("CONVFMT".into(), Value::Str("%.6g".into()));
        // POSIX record separator (default newline).
        vars.insert("RS".into(), Value::Str("\n".into()));
        // Text of the input record separator for the last record read (gawk).
        vars.insert("RT".into(), Value::Str(String::new().into()));
        vars.insert("ERRNO".into(), Value::Str(String::new().into()));
        vars.insert("ARGIND".into(), Value::Num(0.0));
        // Process environment (gawk associative array).
        let mut environ = AwkArray::new();
        for (k, v) in std::env::vars() {
            environ.insert(k, Value::Str(v.into()));
        }
        vars.insert("ENVIRON".into(), Value::Array(environ));
        // Stub gawk special arrays (full semantics not implemented).
        vars.insert("PROCINFO".into(), Value::Array(AwkArray::new()));
        vars.insert("SYMTAB".into(), Value::Array(AwkArray::new()));
        vars.insert("FUNCTAB".into(), Value::Array(AwkArray::new()));
        // POSIX octal \034 — multidimensional array subscript separator
        vars.insert("SUBSEP".into(), Value::Str("\x1c".into()));
        // Empty FPAT means use FS for field splitting (gawk).
        vars.insert("FPAT".into(), Value::Str(String::new().into()));
        vars.insert("FIELDWIDTHS".into(), Value::Str(String::new().into()));
        vars.insert("IGNORECASE".into(), Value::Num(0.0));
        vars.insert("BINMODE".into(), Value::Num(0.0));
        vars.insert("LINT".into(), Value::Num(0.0));
        vars.insert("TEXTDOMAIN".into(), Value::Str(String::new().into()));
        Self {
            vars,
            global_readonly: None,
            fields: Vec::new(),
            field_ranges: Vec::new(),
            fields_dirty: false,
            fields_pending_split: false,
            cached_fs: " ".into(),
            record: String::new().into(),
            record_assigned: false,
            record_strnum: true,
            field_strnum: Vec::new(),
            line_buf: Vec::with_capacity(256),
            read_leftover: Vec::new(),
            nr: 0.0,
            fnr: 0.0,
            filename: String::new(),
            exit_pending: false,
            exit_code: 0,
            input_reader: None,
            primary_input_done: false,
            inet_tcp_read: HashMap::new(),
            inet_tcp_write: HashMap::new(),
            inet_udp: HashMap::new(),
            gettext_dir: String::new(),
            bignum: false,
            read_timeout_env: Cell::new(None),
            fs_regex: None,
            file_handles: HashMap::new(),
            getline_leftover: HashMap::new(),
            dir_read: HashMap::new(),
            output_handles: HashMap::new(),
            pipe_stdin: HashMap::new(),
            pipe_children: HashMap::new(),
            pipe_stdout: HashMap::new(),
            pipe_input_children: HashMap::new(),
            coproc_handles: HashMap::new(),
            rand_seed: 1,
            numeric_decimal: '.',
            // gawk parity: in the C locale, `localeconv` returns an empty
            // `thousands_sep` — `%'d` then prints WITHOUT grouping. Don't fall
            // back to `,`; preserve `None` so `printf "%'d", 1234567` matches
            // gawk under `LC_ALL=C`.
            numeric_thousands_sep: crate::locale_numeric::thousands_sep_from_locale(),
            slots: Vec::new(),
            slot_touched: Vec::new(),
            regex_cache_cs: AwkMap::default(),
            regex_cache_ci: AwkMap::default(),
            memmem_finder_cache: AwkMap::default(),
            print_buf: Vec::with_capacity(DEFAULT_PRINT_BUF_CAPACITY),
            ofs_bytes: b" ".to_vec(),
            ors_bytes: b"\n".to_vec(),
            vm_stack: Vec::with_capacity(64),
            csv_mode: false,
            rs_pattern_for_regex: String::new(),
            rs_regex_bytes: None,
            sandbox: false,
            characters_as_bytes: false,
            posix: false,
            traditional: false,
            jit_enabled: true,
            fuse_chunk_cache: HashMap::new(),
            fuse_last_chunk_key: (0, false),
            fuse_last_chunk_value: None,
            fuse_prefix_chunk_cache: HashMap::new(),
            fuse_vm_pool: fusevm::VMPool::new(),
            gettext_catalogs: AwkMap::default(),
            symtab_slot_map: AwkMap::default(),
            decimal_lits: Vec::new(),
            debugger: None,
            debug_call_stack: Vec::new(),
            cur_line: 0,
            profile_record_hits: Vec::new(),
            sorted_in_warned: Cell::new(false),
            errno_code: 0,
            intercepts: Vec::new(),
            intercept_call_stack: Vec::new(),
            #[cfg(unix)]
            primary_input_poll_fd: None,
        }
    }

    /// True when the **`LINT`** variable is set to a truthy value (after `BEGIN`, includes `-v LINT=1`).
    pub fn lint_runtime_active(&self) -> bool {
        self.get_global_var("LINT")
            .map(|v| v.truthy())
            .unwrap_or(false)
    }

    /// Emit a **`LINT`**-controlled warning to stderr (no-op when `LINT` is unset/false).
    pub fn lint_warn(&self, msg: &str) {
        if self.lint_runtime_active() {
            eprintln!("awkrs: warning: {msg}");
        }
    }

    /// gawk warns for **`log(x)`** / **`sqrt(x)`** when **`x < 0`**, even when **`LINT`** is off.
    pub fn warn_builtin_negative_arg(&self, name: &str, x: f64) {
        if x.is_nan() {
            return;
        }
        eprintln!("awkrs: warning: {name}: received negative argument {x}");
    }

    /// gawk **`PROCINFO["prec"]`**: MPFR precision in bits when **`-M`** / **`--bignum`** is active.
    pub fn mpfr_prec_bits(&self) -> u32 {
        if !self.bignum {
            return MPFR_PREC;
        }
        match self.get_global_var("PROCINFO") {
            Some(Value::Array(m)) => m
                .get("prec")
                .map(|v| v.as_number() as u32)
                .filter(|&p| (53..=1_000_000).contains(&p))
                .unwrap_or(MPFR_PREC),
            _ => MPFR_PREC,
        }
    }

    /// gawk **`PROCINFO["roundmode"]`**: MPFR rounding (`N` nearest, `Z` zero, `U` up, `D` down, `A` away).
    pub fn mpfr_round(&self) -> Round {
        let s = match self.get_global_var("PROCINFO") {
            Some(Value::Array(m)) => m.get("roundmode").map(|v| v.as_str()).unwrap_or_default(),
            _ => String::new(),
        };
        let c = s.trim().chars().next().unwrap_or('N');
        match c.to_ascii_uppercase() {
            'N' => Round::Nearest,
            'Z' => Round::Zero,
            'U' => Round::Up,
            'D' => Round::Down,
            'A' => Round::AwayZero,
            _ => Round::Nearest,
        }
    }

    /// gawk **`SUBSEP`** string used for **`PROCINFO[input, "READ_TIMEOUT"]`** composite keys.
    pub fn procinfo_subsep_string(&self) -> String {
        self.get_global_var("SUBSEP")
            .map(|v| v.as_str().to_string())
            .unwrap_or_else(|| "\x1c".into())
    }

    /// gawk: default **`PROCINFO["READ_TIMEOUT"]`** — explicit **`0`** disables; absent key uses **`GAWK_READ_TIMEOUT`**.
    pub fn global_read_timeout_ms(&self) -> i32 {
        match self.get_global_var("PROCINFO") {
            Some(Value::Array(m)) => match m.get("READ_TIMEOUT") {
                Some(v) => (v.as_number() as i32).max(0),
                None => self.read_timeout_env_ms(),
            },
            _ => self.read_timeout_env_ms(),
        }
    }

    /// `GAWK_READ_TIMEOUT`, read once per process rather than once per
    /// `getline`.
    ///
    /// The env lookup was 14% of a `while ((getline l < f) > 0)` loop in the
    /// profile — `std::env::var` walks the environment block on every call, and
    /// nothing in an awk program can change it. Memoised here rather than inside
    /// `gawk_read_timeout_env` so that function keeps re-reading, which is what
    /// its own tests check.
    fn read_timeout_env_ms(&self) -> i32 {
        match self.read_timeout_env.get() {
            Some(v) => v,
            None => {
                let v = crate::procinfo::gawk_read_timeout_env().max(0);
                self.read_timeout_env.set(Some(v));
                v
            }
        }
    }

    /// Per-input **`PROCINFO[input_name, "READ_TIMEOUT"]`** (gawk), else [`Self::global_read_timeout_ms`].
    pub fn procinfo_read_timeout_ms_for(&self, input_key: &str) -> i32 {
        let sep = self.procinfo_subsep_string();
        let composite = format!("{input_key}{sep}READ_TIMEOUT");
        if let Some(Value::Array(m)) = self.get_global_var("PROCINFO") {
            if let Some(v) = m.get(&composite) {
                return (v.as_number() as i32).max(0);
            }
        }
        self.global_read_timeout_ms()
    }

    /// gawk **`PROCINFO[input_name, "RETRY"]`**: when truthy, retryable I/O errors map to **`getline`** **`-2`**.
    pub fn procinfo_retry_enabled_for(&self, input_key: &str) -> bool {
        let sep = self.procinfo_subsep_string();
        let composite = format!("{input_key}{sep}RETRY");
        self.get_global_var("PROCINFO")
            .and_then(|v| match v {
                Value::Array(m) => m.get(&composite).map(|v| v.truthy()),
                _ => None,
            })
            .unwrap_or(false)
    }

    /// gawk **`FILENAME`**-style key for primary-input timeouts (`"-"` when unset / stdin).
    pub fn primary_input_procinfo_key(&self) -> String {
        let f = self.filename.trim();
        if f.is_empty() {
            "-".into()
        } else {
            f.to_string()
        }
    }

    /// gawk **`getline`** return **`-2`** when **`PROCINFO[input, "RETRY"]`** is set and errno is retryable.
    pub fn getline_io_return_code(&self, e: &std::io::Error, input_key: &str) -> f64 {
        if !self.procinfo_retry_enabled_for(input_key) {
            return -1.0;
        }
        let retry = matches!(
            e.kind(),
            std::io::ErrorKind::WouldBlock
                | std::io::ErrorKind::TimedOut
                | std::io::ErrorKind::Interrupted
        );
        if retry {
            -2.0
        } else {
            -1.0
        }
    }

    /// Map a **`getline`** I/O failure to **`-1`** / **`-2`** (sets **`ERRNO`**).
    pub fn getline_error_code_for_key(&mut self, err: &Error, input_key: &str) -> f64 {
        match err {
            Error::Io(e) => {
                self.set_errno_io(e);
                self.getline_io_return_code(e, input_key)
            }
            _ => {
                self.set_errno_str(err.to_string());
                -1.0
            }
        }
    }

    /// Refresh **`PROCINFO`**, **`FUNCTAB`**, and a **`SYMTAB`** mirror of globals (best-effort vs gawk introspection).
    pub fn refresh_special_arrays(&mut self, cp: &CompiledProgram, bin_name: &str) {
        self.procinfo_refresh(cp, bin_name);
        self.functab_refresh(cp);
        self.symtab_mirror_refresh(cp);
    }

    fn procinfo_refresh(&mut self, cp: &CompiledProgram, bin_name: &str) {
        let mut p = AwkArray::new();
        if let Some(Value::Array(old)) = self.vars.get("PROCINFO") {
            for (k, v) in old.iter() {
                p.insert_bytes(k.as_bytes(), v.clone());
            }
        }
        p.insert(
            "version".into(),
            Value::Str(env!("CARGO_PKG_VERSION").into()),
        );
        p.insert("api".into(), Value::Str("awkrs".into()));
        p.insert("api_major".into(), Value::Num(4.0));
        p.insert("api_minor".into(), Value::Num(1.0));
        p.insert("program".into(), Value::Str(bin_name.into()));
        // gawk: `posix` / `mingw` / `vms` — not Rust `std::env::consts::OS` (`macos`, `linux`, …).
        p.insert(
            "platform".into(),
            Value::Str(crate::procinfo::gawk_platform_string().into()),
        );
        if let Some(pma) = crate::procinfo::AWKRS_PMA_VERSION {
            p.insert("pma".into(), Value::Str(pma.into()));
        }
        p.insert("pid".into(), Value::Num(std::process::id() as f64));
        p.insert("errno".into(), Value::Num(self.errno_code as f64));
        #[cfg(unix)]
        {
            unsafe {
                p.insert("ppid".into(), Value::Num(libc::getppid() as f64));
                p.insert("uid".into(), Value::Num(libc::getuid() as f64));
                p.insert("euid".into(), Value::Num(libc::geteuid() as f64));
                p.insert("gid".into(), Value::Num(libc::getgid() as f64));
                p.insert("egid".into(), Value::Num(libc::getegid() as f64));
                p.insert("pgrpid".into(), Value::Num(libc::getpgrp() as f64));
            }
            for (k, v) in crate::procinfo::supplementary_group_entries() {
                p.insert(k, Value::Num(v));
            }
        }
        p.insert(
            "FS".into(),
            Value::Str(crate::procinfo::field_split_mode(self).into()),
        );
        // gawk parity: `PROCINFO["strftime"]` is the default format used when
        // `strftime()` is called with no arguments. Use gawk's actual default
        // (date(1)-equivalent) instead of `%c`.
        p.insert(
            "strftime".into(),
            Value::Str("%a %b %e %H:%M:%S %Z %Y".into()),
        );

        // `args_os`, not `args`: the latter panics on an argument that is not
        // valid UTF-8, and awk accepts one — a program written on the command
        // line may hold a byte inside a string or regex literal.
        let mut argv_proc = AwkArray::new();
        for (i, a) in std::env::args_os().enumerate() {
            argv_proc.insert(
                i.to_string(),
                Value::Str(AwkStr::from(crate::os_arg_bytes(&a))),
            );
        }
        p.insert("argv".into(), Value::Array(argv_proc));

        p.insert(
            "mb_cur_max".into(),
            Value::Num(crate::procinfo::mb_cur_max_value()),
        );

        if self.bignum && !p.contains_key("prec") {
            p.insert("prec".into(), Value::Num(MPFR_PREC as f64));
        }
        if !p.contains_key("roundmode") {
            p.insert("roundmode".into(), Value::Str("N".into()));
        }
        if !p.contains_key("READ_TIMEOUT") {
            let env_to = crate::procinfo::gawk_read_timeout_env();
            if env_to > 0 {
                p.insert("READ_TIMEOUT".into(), Value::Num(env_to as f64));
            }
        }
        if self.bignum {
            p.insert(
                "gmp_version".into(),
                Value::Str(crate::procinfo::gmp_version_string().into()),
            );
            p.insert(
                "mpfr_version".into(),
                Value::Str(crate::procinfo::mpfr_version_string().into()),
            );
            p.insert(
                "prec_min".into(),
                Value::Num(gmp_mpfr_sys::mpfr::PREC_MIN as f64),
            );
            p.insert(
                "prec_max".into(),
                Value::Num(gmp_mpfr_sys::mpfr::PREC_MAX as f64),
            );
        }
        let binmode = self
            .get_global_var("BINMODE")
            .map(|v| v.as_number())
            .unwrap_or(0.0);
        p.insert("awkrs_binmode".into(), Value::Num(binmode));

        // nproc: number of available CPUs
        p.or_insert(
            "nproc".into(),
            Value::Num(
                std::thread::available_parallelism()
                    .map(|n| n.get() as f64)
                    .unwrap_or(1.0),
            ),
        );

        // sorted_in: default sort order for for-in (gawk compat)
        p.or_insert("sorted_in".into(), Value::Str(String::new().into()));

        // PREC (default precision) when not in bignum mode
        if !self.bignum {
            p.or_insert("prec".into(), Value::Num(53.0));
        }

        crate::procinfo::merge_procinfo_identifiers(&mut p, cp);

        let sep = self
            .get_global_var("SUBSEP")
            .map(|v| v.as_str().to_string())
            .unwrap_or_else(|| "\x1c".into());
        let global_to = match p.get("READ_TIMEOUT") {
            Some(v) => v.as_number(),
            None => crate::procinfo::gawk_read_timeout_env() as f64,
        };
        let mut paths: Vec<String> = Vec::new();
        if let Some(Value::Array(argv)) = self.get_global_var("ARGV") {
            let argc = self
                .get_global_var("ARGC")
                .map(|v| v.as_number() as i64)
                .unwrap_or(0);
            for i in 1..argc {
                if let Some(v) = argv.get(&i.to_string()) {
                    paths.push(v.as_str().to_string());
                }
            }
        }
        paths.push("-".into());
        for path in paths {
            let k_rt = format!("{path}{sep}READ_TIMEOUT");
            p.or_insert(k_rt, Value::Num(global_to));
            let k_retry = format!("{path}{sep}RETRY");
            p.or_insert(k_retry, Value::Num(0.0));
        }

        self.vars.insert("PROCINFO".into(), Value::Array(p));
    }

    fn functab_refresh(&mut self, cp: &CompiledProgram) {
        let mut ft = AwkArray::new();
        // User-defined functions
        for (name, f) in &cp.functions {
            let mut meta = AwkArray::new();
            meta.insert("type".into(), Value::Str("user".into()));
            meta.insert("arity".into(), Value::Num(f.params.len() as f64));
            ft.insert(name.clone(), Value::Array(meta));
        }
        // Builtin functions (gawk includes these in FUNCTAB)
        for &name in crate::namespace::BUILTIN_NAMES {
            if !ft.contains_key(name) {
                let mut meta = AwkArray::new();
                meta.insert("type".into(), Value::Str("builtin".into()));
                ft.insert(name.into(), Value::Array(meta));
            }
        }
        self.vars.insert("FUNCTAB".into(), Value::Array(ft));
    }

    fn symtab_mirror_refresh(&mut self, cp: &CompiledProgram) {
        self.symtab_slot_map = cp.slot_map.clone();
        // SYMTAB subscripts resolve live via [`VmCtx`] / [`Runtime::symtab_elem_get`]; keep empty placeholder.
        self.vars
            .insert("SYMTAB".into(), Value::Array(AwkArray::new()));
    }

    ///
    /// `ARGV[0]` uses the basename of `argv[0]` (gawk convention — `"gawk"` rather than
    /// `"/opt/homebrew/bin/gawk"`); awk scripts that key off the interpreter name
    /// (`ARGV[0] == "awkrs"`) work regardless of how the binary was launched.
    ///
    /// In `--traditional` mode the full `argv[0]` is preserved verbatim, matching
    /// BSD `/usr/bin/awk` and bell-labs nawk semantics (no basename strip).
    pub fn init_argv(&mut self, files: &[std::path::PathBuf]) {
        use std::env;
        let raw = env::args_os()
            .next()
            .map(|a| String::from_utf8_lossy(crate::os_arg_bytes(&a)).into_owned())
            .unwrap_or_else(|| "awkrs".to_string());
        let bin = if self.traditional {
            raw.clone()
        } else {
            std::path::Path::new(&raw)
                .file_name()
                .map(|s| s.to_string_lossy().into_owned())
                .unwrap_or_else(|| raw.clone())
        };
        let mut argv = vec![bin];
        for f in files {
            argv.push(f.to_string_lossy().into_owned());
        }
        let argc = argv.len();
        self.vars.insert("ARGC".into(), Value::Num(argc as f64));
        let mut map = AwkArray::new();
        for (i, s) in argv.iter().enumerate() {
            map.insert(i.to_string(), Value::Str(s.clone().into()));
        }
        self.vars.insert("ARGV".into(), Value::Array(map));
    }

    /// Worker runtime for parallel record processing: empty overlay `vars`, shared read-only globals.
    #[allow(clippy::too_many_arguments)]
    pub fn for_parallel_worker(
        shared_globals: Arc<AwkMap<String, Value>>,
        filename: String,
        rand_seed: u64,
        numeric_decimal: char,
        numeric_thousands_sep: Option<char>,
        csv_mode: bool,
        bignum: bool,
        sandbox: bool,
        characters_as_bytes: bool,
        posix: bool,
        traditional: bool,
        jit_enabled: bool,
        gettext_catalogs: AwkMap<String, Arc<Catalog>>,
    ) -> Self {
        Self {
            vars: AwkMap::default(),
            global_readonly: Some(shared_globals),
            fields: Vec::new(),
            field_ranges: Vec::new(),
            fields_dirty: false,
            fields_pending_split: false,
            cached_fs: " ".into(),
            record: String::new().into(),
            record_assigned: false,
            record_strnum: true,
            field_strnum: Vec::new(),
            line_buf: Vec::new(),
            read_leftover: Vec::new(),
            nr: 0.0,
            fnr: 0.0,
            filename,
            exit_pending: false,
            exit_code: 0,
            input_reader: None,
            primary_input_done: false,
            inet_tcp_read: HashMap::new(),
            inet_tcp_write: HashMap::new(),
            inet_udp: HashMap::new(),
            gettext_dir: String::new(),
            bignum,
            read_timeout_env: Cell::new(None),
            fs_regex: None,
            file_handles: HashMap::new(),
            getline_leftover: HashMap::new(),
            dir_read: HashMap::new(),
            output_handles: HashMap::new(),
            pipe_stdin: HashMap::new(),
            pipe_children: HashMap::new(),
            pipe_stdout: HashMap::new(),
            pipe_input_children: HashMap::new(),
            coproc_handles: HashMap::new(),
            rand_seed,
            numeric_decimal,
            numeric_thousands_sep,
            slots: Vec::new(),
            slot_touched: Vec::new(),
            regex_cache_cs: AwkMap::default(),
            regex_cache_ci: AwkMap::default(),
            memmem_finder_cache: AwkMap::default(),
            print_buf: Vec::new(),
            ofs_bytes: b" ".to_vec(),
            ors_bytes: b"\n".to_vec(),
            vm_stack: Vec::with_capacity(64),
            csv_mode,
            rs_pattern_for_regex: String::new(),
            rs_regex_bytes: None,
            sandbox,
            characters_as_bytes,
            posix,
            traditional,
            jit_enabled,
            fuse_chunk_cache: HashMap::new(),
            fuse_last_chunk_key: (0, false),
            fuse_last_chunk_value: None,
            fuse_prefix_chunk_cache: HashMap::new(),
            fuse_vm_pool: fusevm::VMPool::new(),
            gettext_catalogs,
            symtab_slot_map: AwkMap::default(),
            decimal_lits: Vec::new(),
            debugger: None,
            debug_call_stack: Vec::new(),
            cur_line: 0,
            profile_record_hits: Vec::new(),
            sorted_in_warned: Cell::new(false),
            errno_code: 0,
            intercepts: Vec::new(),
            intercept_call_stack: Vec::new(),
            #[cfg(unix)]
            primary_input_poll_fd: None,
        }
    }

    /// Refused when [`Self::sandbox`] is set (gawk-style `-S`).
    pub fn require_unsandboxed_io(&self) -> Result<()> {
        if self.sandbox {
            return Err(Error::Runtime(
                "sandbox: file I/O, pipes, coprocesses, inet, and system() are disabled".into(),
            ));
        }
        Ok(())
    }

    /// Fatal-check the `FS` the current record is about to be split with.
    ///
    /// gawk, mawk and one-true-awk all abort with a non-zero status once a
    /// record is read under an `FS` that is not a valid ERE; awkrs used to fall
    /// back to splitting on the separator text literally and carry on. The
    /// check is per record rather than per assignment because that is where the
    /// three agree: `awk 'BEGIN{FS="[["} END{print}' </dev/null` is fatal in
    /// gawk and mawk but exits 0 in one-true-awk, so an assignment no record
    /// ever reaches must stay silent.
    ///
    /// Memoised on the separator text, so the steady state is one string
    /// compare per record.
    pub fn check_fs_ere(&self) -> std::result::Result<(), String> {
        check_ere_separator(&self.cached_fs)
    }

    /// Ensure a regex is compiled and cached. Call before `regex_ref()`.
    /// [`Self::ensure_regex`] with a byte pattern.
    ///
    /// A pattern can itself hold a byte that is not part of valid UTF-8 —
    /// `$1 ~ $2` where the second field is one — and the cache is keyed on the
    /// bytes so two different patterns cannot collide on the same rendering.
    pub fn ensure_regex_bytes(&mut self, pat: &[u8]) -> std::result::Result<(), String> {
        let ic = self.ignore_case_flag();
        let cache = if ic {
            &mut self.regex_cache_ci
        } else {
            &mut self.regex_cache_cs
        };
        if cache.contains_key(pat) {
            return Ok(());
        }
        // The same ERE walk `FS` and `split()` use. Rust's parser accepts two
        // spellings ERE has no notion of — `(?:…)` and `(?i)…` — so `~` used to
        // honour them as a non-capturing group and an inline flag where gawk,
        // mawk and one-true-awk are all fatal ("illegal primary": a `?` with no
        // preceding expression, which is what the walk calls it too).
        if let Some(why) = ere_reject_reason(&String::from_utf8_lossy(pat)) {
            return Err(format!(
                "invalid regexp: {why}: /{}/",
                String::from_utf8_lossy(pat)
            ));
        }
        let translated = translate_awk_re_bytes_to_rust(pat);
        let mut b = regex::bytes::RegexBuilder::new(&translated);
        b.unicode(crate::locale_numeric::ctype_is_utf8());
        b.case_insensitive(ic);
        b.dot_matches_new_line(true);
        let re = b.build().map_err(|e| e.to_string())?;
        cache.insert(pat.to_vec(), re);
        Ok(())
    }

    /// [`Self::regex_ref`] with a byte pattern.
    pub fn regex_ref_bytes(&self, pat: &[u8]) -> &BytesRegex {
        if self.ignore_case_flag() {
            &self.regex_cache_ci[pat]
        } else {
            &self.regex_cache_cs[pat]
        }
    }

    pub fn ensure_regex(&mut self, pat: &str) -> std::result::Result<(), String> {
        self.ensure_regex_bytes(pat.as_bytes())
    }

    /// Get a cached regex (must call `ensure_regex` first).
    pub fn regex_ref(&self, pat: &str) -> &BytesRegex {
        self.regex_ref_bytes(pat.as_bytes())
    }

    /// gawk **`IGNORECASE`**: truthy value enables case-insensitive regex and string compares.
    #[inline]
    pub fn ignore_case_flag(&self) -> bool {
        self.get_global_var("IGNORECASE")
            .map(|v| v.truthy())
            .unwrap_or(false)
    }
    /// `clear_errno` — see implementation for the contract.
    pub fn clear_errno(&mut self) {
        self.errno_code = 0;
        self.vars
            .insert("ERRNO".into(), Value::Str(String::new().into()));
    }
    /// `set_errno_io` — see implementation for the contract.
    pub fn set_errno_io(&mut self, e: &std::io::Error) {
        self.errno_code = e.raw_os_error().unwrap_or(0);
        // gawk's ERRNO is the strerror string ("No such file or directory"),
        // not Rust's default Display which appends " (os error <code>)". Strip
        // that suffix so awkrs's ERRNO matches the gawk parity tests.
        let msg = e.to_string();
        let cleaned = match msg.rfind(" (os error ") {
            Some(pos) if msg.ends_with(')') => msg[..pos].to_string(),
            _ => msg,
        };
        self.vars.insert("ERRNO".into(), Value::Str(cleaned.into()));
    }
    /// `set_errno_str` — see implementation for the contract.
    pub fn set_errno_str(&mut self, msg: impl Into<String>) {
        self.errno_code = 0;
        self.vars
            .insert("ERRNO".into(), Value::Str(msg.into().into()));
    }
    /// `ensure_rs_regex_bytes` — see implementation for the contract.
    pub fn ensure_rs_regex_bytes(&mut self) -> Result<()> {
        let rs = self.rs_string();
        if self.rs_pattern_for_regex == rs {
            return Ok(());
        }
        self.rs_pattern_for_regex.clear();
        self.rs_pattern_for_regex.push_str(&rs);
        if rs == "\n" || rs.is_empty() {
            self.rs_regex_bytes = None;
            return Ok(());
        }
        if rs.chars().count() <= 1 {
            self.rs_regex_bytes = None;
            return Ok(());
        }
        self.rs_regex_bytes = Some(
            BytesRegex::new(&rs).map_err(|e| Error::Runtime(format!("invalid RS regex: {e}")))?,
        );
        Ok(())
    }
    /// Publish `RT` — the separator text that ended the record just read.
    ///
    /// Runs once per record, so it writes through the existing entry rather than
    /// re-inserting. The straightforward `vars.insert("RT".into(), Value::Str(t))`
    /// allocated twice on every record — a fresh `String` key for a name already
    /// in the map, and a fresh value string — which is two million allocations
    /// across a million-record input, for a variable most programs never read.
    /// Reusing the stored `String`'s buffer makes the steady state allocation-free.
    pub fn set_rt_from_bytes(&mut self, sep: &[u8]) {
        if let Some(Value::Str(s)) = self.vars.get_mut("RT") {
            s.clear();
            match std::str::from_utf8(sep) {
                Ok(t) => s.push_str(t),
                Err(_) => s.push_str(&String::from_utf8_lossy(sep)),
            }
            return;
        }
        // First record, or a program that assigned a non-string to `RT`.
        let t = String::from_utf8_lossy(sep).into_owned();
        self.vars.insert("RT".into(), Value::Str(t.into()));
    }

    /// Cached [`memmem::Finder`] for a literal pattern string (non-empty).
    /// Used by literal `gsub`/`sub` to scan records with SIMD-friendly substring search.
    pub fn literal_substring_finder(&mut self, pat: &[u8]) -> &memmem::Finder<'static> {
        if !self.memmem_finder_cache.contains_key(pat) {
            let f = memmem::Finder::new(pat).into_owned();
            self.memmem_finder_cache.insert(pat.to_vec(), f);
        }
        &self.memmem_finder_cache[pat]
    }

    /// Resolve a global name: per-record overlay, then shared `BEGIN` snapshot.
    #[inline]
    pub fn get_global_var(&self, name: &str) -> Option<&Value> {
        self.vars
            .get(name)
            .or_else(|| self.global_readonly.as_ref()?.get(name))
    }

    /// `print … | "cmd"` / `printf … | "cmd"` — append bytes to the coprocess stdin (spawn on first use).
    pub fn write_pipe_line(&mut self, cmd: &str, data: &[u8]) -> Result<()> {
        self.require_unsandboxed_io()?;
        if self.coproc_handles.contains_key(cmd) {
            return Err(Error::Runtime(format!(
                "one-way pipe `|` conflicts with two-way `|&` for `{cmd}`"
            )));
        }
        if !self.pipe_stdin.contains_key(cmd) {
            // The child inherits this process's stdout, so anything awk has
            // buffered and not yet written would appear *after* the child's
            // output even though the program printed it first. mawk and
            // one-true-awk both flush all output when they open a pipe, and
            // gawk flushes as well; awkrs flushed neither, so
            // `print "A"; print "B" | "cat"; close("cat")` emitted B before A
            // in all three references' disagreement-free case. Flushing at open
            // (rather than at close) is what mawk and one-true-awk do, and it
            // costs one flush per distinct command, not one per write.
            self.flush_stdout_before_child();
            let mut child = Command::new("sh")
                .arg("-c")
                .arg(cmd)
                .stdin(Stdio::piped())
                .spawn()
                .map_err(|e| Error::Runtime(format!("pipe `{cmd}`: {e}")))?;
            let stdin = child
                .stdin
                .take()
                .ok_or_else(|| Error::Runtime(format!("pipe `{cmd}`: no stdin")))?;
            self.pipe_children.insert(cmd.to_string(), child);
            self.pipe_stdin
                .insert(cmd.to_string(), BufWriter::new(stdin));
        }
        let w = self.pipe_stdin.get_mut(cmd).unwrap();
        w.write_all(data).map_err(Error::Io)?;
        Ok(())
    }

    fn ensure_coproc(&mut self, cmd: &str) -> Result<()> {
        self.require_unsandboxed_io()?;
        if self.coproc_handles.contains_key(cmd) {
            return Ok(());
        }
        if self.pipe_stdin.contains_key(cmd) {
            return Err(Error::Runtime(format!(
                "two-way pipe `|&` conflicts with one-way `|` for `{cmd}`"
            )));
        }
        let mut child = Command::new("sh")
            .arg("-c")
            .arg(cmd)
            .stdin(Stdio::piped())
            .stdout(Stdio::piped())
            .spawn()
            .map_err(|e| Error::Runtime(format!("coprocess `{cmd}`: {e}")))?;
        let stdin = child
            .stdin
            .take()
            .ok_or_else(|| Error::Runtime(format!("coprocess `{cmd}`: no stdin")))?;
        let stdout = child
            .stdout
            .take()
            .ok_or_else(|| Error::Runtime(format!("coprocess `{cmd}`: no stdout")))?;
        self.coproc_handles.insert(
            cmd.to_string(),
            CoprocHandle {
                child,
                stdin: BufWriter::new(stdin),
                stdout: BufReader::new(stdout),
            },
        );
        Ok(())
    }

    /// `print … |& "cmd"` / `printf … |& "cmd"` — append bytes to the two-way pipe stdin.
    pub fn write_coproc_line(&mut self, cmd: &str, data: &[u8]) -> Result<()> {
        self.ensure_coproc(cmd)?;
        let w = self.coproc_handles.get_mut(cmd).unwrap();
        w.stdin.write_all(data).map_err(Error::Io)?;
        Ok(())
    }

    /// `getline … <& "cmd"` — one line from the coprocess stdout.
    pub fn read_line_coproc(&mut self, cmd: &str) -> Result<Option<String>> {
        self.ensure_coproc(cmd)?;
        let to = self.procinfo_read_timeout_ms_for(cmd);
        #[cfg(unix)]
        if to > 0 {
            use std::os::unix::io::AsRawFd;
            let h = self.coproc_handles.get_mut(cmd).unwrap();
            let fd = h.stdout.get_ref().as_raw_fd();
            wait_fd_read_timeout(fd, to)?;
        }
        let (rs, re, mut leftover) = self.take_getline_rs_state(cmd)?;
        let h = self.coproc_handles.get_mut(cmd).unwrap();
        let read = read_record_from_stream(&mut h.stdout, &rs, re.as_ref(), &mut leftover);
        self.restore_getline_rs_state(cmd, re, leftover);
        self.finish_redirected_getline(read?)
    }

    /// `expr | getline` — one line from `sh -c expr` stdout.
    ///
    /// Spawns the subprocess on first call for `cmd` and caches the stdout handle so
    /// subsequent `getline`s advance through the **same** stream. `close(cmd)` tears
    /// the pipe down. Before this caching, every call respawned `cmd`, which made
    /// `while ((cmd | getline x) > 0)` loop forever on the first line of output.
    pub fn read_line_pipe(&mut self, cmd: &str) -> Result<Option<String>> {
        self.require_unsandboxed_io()?;
        if !self.pipe_stdout.contains_key(cmd) {
            let mut child = Command::new("sh")
                .arg("-c")
                .arg(cmd)
                .stdout(Stdio::piped())
                .spawn()
                .map_err(|e| Error::Runtime(format!("pipe getline `{cmd}`: {e}")))?;
            let stdout = child
                .stdout
                .take()
                .ok_or_else(|| Error::Runtime(format!("pipe getline `{cmd}`: no stdout")))?;
            self.pipe_stdout
                .insert(cmd.to_string(), BufReader::new(stdout));
            self.pipe_input_children.insert(cmd.to_string(), child);
        }
        let to = self.procinfo_read_timeout_ms_for(cmd);
        #[cfg(unix)]
        if to > 0 {
            use std::os::unix::io::AsRawFd;
            let fd = self
                .pipe_stdout
                .get(cmd)
                .expect("pipe_stdout entry exists; just inserted")
                .get_ref()
                .as_raw_fd();
            wait_fd_read_timeout(fd, to)?;
        }
        let (rs, re, mut leftover) = self.take_getline_rs_state(cmd)?;
        let reader = self
            .pipe_stdout
            .get_mut(cmd)
            .expect("pipe_stdout entry exists; just inserted");
        let read = read_record_from_stream(reader, &rs, re.as_ref(), &mut leftover);
        self.restore_getline_rs_state(cmd, re, leftover);
        self.finish_redirected_getline(read?)
    }

    /// Write one `print` line (including `ORS`) to `path`. First open uses truncate (`>`) or
    /// append (`>>`); later writes reuse the same handle until `close`.
    pub fn write_output_line(&mut self, path: &str, data: &[u8], append: bool) -> Result<()> {
        self.require_unsandboxed_io()?;
        if is_program_stdout(path) {
            // `>` and `>>` mean the same thing here: there is nothing to
            // truncate on a stream the process already holds open.
            let _ = append;
            self.print_buf.extend_from_slice(data);
            return Ok(());
        }
        if path.starts_with("/inet/udp/") {
            let _ = append;
            self.ensure_inet_udp(path)?;
            let s = self.inet_udp.get_mut(path).unwrap();
            s.send(data)
                .map_err(|e| Error::Runtime(format!("inet udp send `{path}`: {e}")))?;
            return Ok(());
        }
        if path.starts_with("/inet/tcp/") {
            let _ = append;
            self.ensure_inet_tcp_pair(path)?;
            let w = self.inet_tcp_write.get_mut(path).unwrap();
            w.write_all(data).map_err(Error::Io)?;
            return Ok(());
        }
        self.ensure_output_writer(path, append)?;
        let w = self.output_handles.get_mut(path).unwrap();
        w.write_all(data).map_err(Error::Io)?;
        Ok(())
    }

    fn ensure_output_writer(&mut self, path: &str, append: bool) -> Result<()> {
        if path.starts_with("/inet/udp/") {
            return self.ensure_inet_udp(path);
        }
        if path.starts_with("/inet/tcp/") {
            return self.ensure_inet_tcp_pair(path);
        }
        if self.output_handles.contains_key(path) {
            return Ok(());
        }
        let f = if append {
            OpenOptions::new().create(true).append(true).open(path)
        } else {
            OpenOptions::new()
                .create(true)
                .write(true)
                .truncate(true)
                .open(path)
        }
        .map_err(|e| Error::Runtime(format!("open {path}: {e}")))?;
        self.output_handles
            .insert(path.to_string(), BufWriter::new(f));
        Ok(())
    }

    /// Flush buffered output for a file or pipe opened with `print`/`printf` redirection.
    pub fn flush_redirect_target(&mut self, key: &str) -> Result<()> {
        if is_program_stdout(key) {
            crate::vm::flush_print_buf(&mut self.print_buf)?;
            std::io::stdout().flush().map_err(Error::Io)?;
            return Ok(());
        }
        if let Some(w) = self.output_handles.get_mut(key) {
            w.flush().map_err(Error::Io)?;
            return Ok(());
        }
        if let Some(w) = self.inet_tcp_write.get_mut(key) {
            w.flush().map_err(Error::Io)?;
            return Ok(());
        }
        if self.inet_udp.contains_key(key) {
            return Ok(());
        }
        if let Some(w) = self.pipe_stdin.get_mut(key) {
            w.flush().map_err(Error::Io)?;
            return Ok(());
        }
        if let Some(h) = self.coproc_handles.get_mut(key) {
            h.stdin.flush().map_err(Error::Io)?;
            return Ok(());
        }
        Err(Error::Runtime(format!(
            "fflush: {key} is not an open output file, pipe, or coprocess"
        )))
    }
    /// `attach_input_reader` — see implementation for the contract.
    #[cfg_attr(unix, allow(dead_code))]
    pub fn attach_input_reader(&mut self, r: SharedInputReader) {
        self.attach_input_reader_with_poll_fd(r, None);
    }

    /// Attach primary input; **`poll_fd`** (Unix) is used with [`libc::poll`] for gawk-style **`READ_TIMEOUT`** on the record / primary-`getline` stream.
    pub fn attach_input_reader_with_poll_fd(
        &mut self,
        r: SharedInputReader,
        #[cfg(unix)] poll_fd: Option<std::os::unix::io::RawFd>,
        #[cfg(not(unix))] _poll_fd: Option<()>,
    ) {
        self.input_reader = Some(r);
        #[cfg(unix)]
        {
            self.primary_input_poll_fd = poll_fd;
        }
    }
    /// `detach_input_reader` — see implementation for the contract.
    pub fn detach_input_reader(&mut self) {
        self.input_reader = None;
        // The primary stream is now spent; a later plain `getline` is at EOF.
        self.primary_input_done = true;
        #[cfg(unix)]
        {
            self.primary_input_poll_fd = None;
        }
    }

    /// Unix: honor **`PROCINFO[FILENAME,"READ_TIMEOUT"]`** before each primary record read.
    #[cfg(unix)]
    pub fn poll_primary_read_timeout_if_needed(&self) -> Result<()> {
        let to = self.procinfo_read_timeout_ms_for(&self.primary_input_procinfo_key());
        if to > 0 {
            if let Some(fd) = self.primary_input_poll_fd {
                wait_fd_read_timeout(fd, to)?;
            }
        }
        Ok(())
    }

    /// Current [`RS`](https://www.gnu.org/software/gawk/manual/html_node/Built_002din-Variables.html) value.
    /// `RS == ""` — awk's paragraph mode. Records are separated by blank lines and
    /// <newline> is an additional field separator regardless of `FS`.
    pub fn paragraph_mode(&self) -> bool {
        match self.get_global_var("RS") {
            Some(v) => v.as_str_cow().is_empty(),
            None => false,
        }
    }

    pub fn rs_string(&self) -> String {
        match self.get_global_var("RS") {
            Some(Value::Str(s)) => s.clone().to_lossy_string(),
            Some(v) => v.as_str(),
            None => "\n".to_string(),
        }
    }

    /// Convert a [`Value`] to a string suitable for use as an array key.
    /// Numeric values pass through `CONVFMT` (POSIX-required for subscripts);
    /// all other values use their default string form. Integer-valued numbers
    /// bypass `CONVFMT` via the same heuristic as `as_str()` (e.g. `a[1]`
    /// stays as `"1"` regardless of CONVFMT).
    /// The array subscript `v` names, without allocating in either hot case.
    ///
    /// A string subscript IS the key, so it is borrowed straight from the value
    /// — `a[k]` inside `for (k in a)` used to clone the key once per element.
    /// An integral number is written into `buf` through the integer writer
    /// rather than the float formatter, and borrowed from there. Only the rare
    /// The subscript a value names, in bytes, borrowing when it already holds
    /// them.
    ///
    /// A rendered subscript cannot round-trip: `a[$1]` where the field holds a
    /// byte that is not part of valid UTF-8 would key on `U+FFFD`, and
    /// `for (k in a)` would hand that back instead of what the program stored.
    /// Numbers follow the same integral / `CONVFMT` rules as the rendered form
    /// — that text is ASCII, so the two agree for every numeric subscript.
    pub fn array_key_bytes_in<'a>(
        &self,
        v: &'a Value,
        buf: &'a mut KeyBuf,
    ) -> std::borrow::Cow<'a, [u8]> {
        use std::borrow::Cow;
        match v {
            Value::Str(s) | Value::StrLit(s) | Value::Regexp(s) => Cow::Borrowed(s.as_bytes()),
            Value::Uninit | Value::Array(_) => Cow::Borrowed(b""),
            // `a[1]` must key on "1", never "1.000000", so an integral value
            // never reaches CONVFMT. Every integral `f64` below 2^53 is exactly
            // an `i64`, so the cast cannot change a digit.
            Value::Num(n)
                if n.is_finite() && n.fract() == 0.0 && n.abs() < 9_007_199_254_740_992.0 =>
            {
                Cow::Borrowed(buf.write_i64(*n as i64).as_bytes())
            }
            other => Cow::Owned(self.value_to_array_key(other).into_bytes()),
        }
    }

    pub fn value_to_array_key(&self, v: &Value) -> String {
        match v {
            Value::Num(n) => {
                // Integer-valued numbers must stay exact: a[1] keys on "1" not "1.000000".
                if n.is_finite() && n.fract() == 0.0 {
                    // Negative zero subscripts the same element as zero, as it
                    // does in gawk: `a[0] = 1; a[-0] = 2` leaves ONE element.
                    // `format!("{:.0}", -0.0)` writes "-0", which made it two.
                    if *n == 0.0 {
                        return "0".to_string();
                    }
                    if n.abs() < 9_007_199_254_740_992.0 {
                        // The integer writer, not the float one — the same
                        // digits, and this runs once per subscript stored.
                        return KeyBuf::new().write_i64(*n as i64).to_string();
                    }
                    format!("{:.0}", *n)
                } else {
                    self.num_to_string_convfmt(*n)
                }
            }
            Value::Mpfr(f) => {
                if f.is_finite() && f.is_integer() {
                    format!("{}", crate::bignum::float_trunc_integer(f))
                } else {
                    self.mpfr_to_string_convfmt(f)
                }
            }
            Value::Uninit => String::new(),
            Value::Str(s) | Value::StrLit(s) | Value::Regexp(s) => s.clone().to_lossy_string(),
            Value::Array(_) => String::new(),
        }
    }

    /// POSIX / gawk: format a number using **`CONVFMT`** (string coercion).
    /// Integer-valued numbers bypass CONVFMT and display in integer form —
    /// matches gawk where any integer-valued `n` prints exact via `%.0f`.
    /// Append `n`'s string form to `out` — [`Self::num_to_string_convfmt`]
    /// without the intermediate `String`.
    ///
    /// Concatenation appends a number in a loop, and the temporary was
    /// allocated, copied out of and dropped once per iteration. An integral
    /// value that fits an `i64` also formats through the integer writer rather
    /// than the float one, which is the same digits by a much shorter route:
    /// every integral `f64` below 2^53 is exactly representable as an `i64`, so
    /// the cast cannot lose one.
    pub fn push_num_convfmt(&self, out: &mut String, n: f64) {
        use std::fmt::Write as _;
        if n.is_finite() && n.fract() == 0.0 {
            // gawk parity: -0.0 prints as "0", not "-0".
            if n == 0.0 {
                out.push('0');
                return;
            }
            if n.abs() < 9_007_199_254_740_992.0 {
                let _ = write!(out, "{}", n as i64);
            } else {
                let _ = write!(out, "{n:.0}");
            }
            return;
        }
        out.push_str(&self.num_to_string_convfmt(n));
    }

    pub fn num_to_string_convfmt(&self, n: f64) -> String {
        if n.is_finite() && n.fract() == 0.0 {
            // gawk parity: -0.0 prints as "0", not "-0".
            if n == 0.0 {
                return "0".to_string();
            }
            return format!("{:.0}", n);
        }
        let fmt = self
            .get_global_var("CONVFMT")
            .map(|v| v.as_str())
            .unwrap_or_else(|| "%.6g".to_string());
        crate::format::awk_sprintf_with_decimal(
            &fmt,
            &[Value::Num(n)],
            self.numeric_decimal,
            self.numeric_thousands_sep,
            None,
        )
        .unwrap_or_else(|_| format_number(n))
    }

    /// POSIX string coercion of an arbitrary scalar: the **one** conversion every
    /// user-visible "this value is used as a string" site must go through.
    ///
    /// Only `Num` / `Mpfr` are affected — they render through `CONVFMT` (integral
    /// values bypass it inside [`Self::num_to_string_convfmt`]). Everything else
    /// delegates to `as_str_cow`, which matters as much as the numeric case: a
    /// field is a `Value::Str` carrying the *original input text*, so `$1` of the
    /// record `1.23456` stays `"1.23456"` under `CONVFMT="%.2f"` in all three
    /// reference awks rather than being re-rendered to `"1.23"`.
    ///
    /// The conversion is deliberately performed **at the point of use**, not
    /// cached at assignment: `CONVFMT="%.2f"; x=1.23456; a=length(x);
    /// CONVFMT="%.4f"; b=length(x)` yields `4 6` in gawk, mawk and one-true-awk
    /// alike, so re-reading `CONVFMT` on every coercion is the observable
    /// behaviour, not an optimisation to hoist away.
    ///
    /// `Value::as_str` / `as_str_cow` render a number via `format_number` (full
    /// f64 precision) and are therefore **not** a substitute here; they remain
    /// correct only for values that are already strings, and for the internal
    /// bookkeeping that must not be reshaped by a user-settable format.
    #[inline]
    pub fn value_to_str_convfmt<'a>(&self, v: &'a Value) -> Cow<'a, str> {
        match v {
            Value::Num(n) => Cow::Owned(self.num_to_string_convfmt(*n)),
            Value::Mpfr(f) => Cow::Owned(self.mpfr_to_string_convfmt(f)),
            _ => v.as_str_cow(),
        }
    }

    /// [`Self::value_to_str_convfmt`] in bytes — the form for anything the awk
    /// program can observe.
    ///
    /// The `&str` version renders through `U+FFFD`, so a builtin that takes its
    /// argument that way can never hand back a byte it was given. `CONVFMT`
    /// output is ASCII, so the two agree for every numeric value.
    pub fn value_to_bytes_convfmt<'a>(&self, v: &'a Value) -> Cow<'a, [u8]> {
        match v {
            Value::Num(n) => Cow::Owned(self.num_to_string_convfmt(*n).into_bytes()),
            Value::Mpfr(f) => Cow::Owned(self.mpfr_to_string_convfmt(f).into_bytes()),
            _ => v.as_bytes_cow(),
        }
    }

    /// POSIX: `print` formats numbers with **`OFMT`** (distinct from [`Self::num_to_string_convfmt`]).
    /// Integer-valued numbers bypass OFMT and display in integer form so e.g.
    /// `print 999999999999` produces `"999999999999"` not `"1e+12"`. Large
    /// integers past `i64::MAX` still print via `%.0f`.
    pub fn num_to_string_ofmt(&self, n: f64) -> String {
        if n.is_finite() && n.fract() == 0.0 {
            // gawk parity: -0.0 prints as "0", not "-0".
            if n == 0.0 {
                return "0".to_string();
            }
            return format!("{:.0}", n);
        }
        let fmt = self
            .get_global_var("OFMT")
            .map(|v| v.as_str())
            .unwrap_or_else(|| "%.6g".to_string());
        crate::format::awk_sprintf_with_decimal(
            &fmt,
            &[Value::Num(n)],
            self.numeric_decimal,
            self.numeric_thousands_sep,
            None,
        )
        .unwrap_or_else(|_| format_number(n))
    }

    /// `CONVFMT` formatting for an MPFR value (`-M`).
    pub fn mpfr_to_string_convfmt(&self, f: &Float) -> String {
        // gawk parity: integer-valued bignums bypass CONVFMT so the full
        // precision is preserved (otherwise `%.6g` would truncate `2^100` to
        // "1.2677e+30" instead of the 31-digit exact value).
        if f.is_finite() && f.is_integer() {
            return format!("{}", crate::bignum::float_trunc_integer(f));
        }
        let fmt = self
            .get_global_var("CONVFMT")
            .map(|v| v.as_str())
            .unwrap_or_else(|| "%.6g".to_string());
        crate::format::awk_sprintf_with_decimal(
            &fmt,
            &[Value::Mpfr(f.clone())],
            self.numeric_decimal,
            self.numeric_thousands_sep,
            Some((self.mpfr_prec_bits(), self.mpfr_round())),
        )
        .unwrap_or_else(|_| f.to_string())
    }

    /// `OFMT` formatting for an MPFR value (`-M`).
    pub fn mpfr_to_string_ofmt(&self, f: &Float) -> String {
        // Same integer fast path as the CONVFMT variant — see comment there.
        if f.is_finite() && f.is_integer() {
            return format!("{}", crate::bignum::float_trunc_integer(f));
        }
        let fmt = self
            .get_global_var("OFMT")
            .map(|v| v.as_str())
            .unwrap_or_else(|| "%.6g".to_string());
        crate::format::awk_sprintf_with_decimal(
            &fmt,
            &[Value::Mpfr(f.clone())],
            self.numeric_decimal,
            self.numeric_thousands_sep,
            Some((self.mpfr_prec_bits(), self.mpfr_round())),
        )
        .unwrap_or_else(|_| f.to_string())
    }

    /// Write `$n` from an MPFR using **`CONVFMT`**-style string (field materialization).
    pub fn set_field_from_mpfr(&mut self, i: i32, f: &Float) -> crate::error::Result<()> {
        let s = self.mpfr_to_string_convfmt(f);
        self.set_field(i, &s)
    }

    /// Next **record** from the primary input stream (respects `RS`), used by `getline` with no redirection.
    pub fn read_line_primary(&mut self) -> Result<Option<String>> {
        let Some(reader) = self.input_reader.clone() else {
            if self.primary_input_done {
                // Main input already ran to completion (we are in `END`, or after
                // an `exit`). POSIX makes this an ordinary end-of-file: `getline`
                // yields 0. gawk / mawk / one-true-awk all agree.
                return Ok(None);
            }
            return Err(Error::Runtime(
                "`getline` with no file is only valid during normal input".into(),
            ));
        };
        let to = self.procinfo_read_timeout_ms_for(&self.primary_input_procinfo_key());
        #[cfg(unix)]
        if to > 0 {
            if let Some(fd) = self.primary_input_poll_fd {
                wait_fd_read_timeout(fd, to)?;
            }
        }
        let rs = self.rs_string();
        self.ensure_rs_regex_bytes()?;
        let mut rt_sep = Vec::new();
        let mut line_buf = std::mem::take(&mut self.line_buf);
        let mut leftover = std::mem::take(&mut self.read_leftover);
        let read_ok = crate::record_io::read_next_record(
            &reader,
            &rs,
            &mut line_buf,
            &mut rt_sep,
            self.rs_regex_bytes.as_ref(),
            &mut leftover,
        )?;
        self.line_buf = line_buf;
        self.read_leftover = leftover;
        if !read_ok {
            return Ok(None);
        }
        self.set_rt_from_bytes(&rt_sep);
        let end = if rs == "\n" {
            crate::record_io::trim_end_record_bytes(&self.line_buf)
        } else {
            self.line_buf.len()
        };
        Ok(Some(
            String::from_utf8_lossy(&self.line_buf[..end]).into_owned(),
        ))
    }

    /// `RS`, its compiled regex form, and this stream's carry-over buffer, all
    /// *moved out* of `self` — the reader is borrowed out of one of the handle
    /// maps for the read below, so nothing else may still be borrowed from
    /// `self` at that point. Every caller must hand all three back through
    /// [`Self::restore_getline_rs_state`], on the error path too.
    ///
    /// Moved rather than cloned. A cloned `regex` engine starts with an empty
    /// match-cache pool, so the lazy DFA it needs is rebuilt on the next match
    /// — a cache that hands out clones looks like it is working (pattern
    /// compilation vanishes from the profile) while the expensive half of the
    /// work still happens per call. Measured on a `while ((getline l < f) > 0)`
    /// loop over 200 000 records with `RS = "[0-9]"`: 1.98 s of CPU cloning,
    /// against gawk's 0.63 s.
    fn take_getline_rs_state(
        &mut self,
        key: &str,
    ) -> Result<(String, Option<BytesRegex>, Vec<u8>)> {
        let rs = self.rs_string();
        self.ensure_rs_regex_bytes()?;
        let re = self.rs_regex_bytes.take();
        // `get_mut` + `take` rather than `remove`: `remove` would be paired with
        // an `insert` that allocates the key string on every getline call, for a
        // key that is already in the map after the first one.
        let leftover = match self.getline_leftover.get_mut(key) {
            Some(buf) => std::mem::take(buf),
            None => Vec::new(),
        };
        Ok((rs, re, leftover))
    }

    /// The other half of [`Self::take_getline_rs_state`].
    fn restore_getline_rs_state(&mut self, key: &str, re: Option<BytesRegex>, leftover: Vec<u8>) {
        self.rs_regex_bytes = re;
        match self.getline_leftover.get_mut(key) {
            Some(buf) => *buf = leftover,
            None => {
                self.getline_leftover.insert(key.to_string(), leftover);
            }
        }
    }

    /// `getline var < filename` — one record from a kept-open file handle.
    pub fn read_line_file(&mut self, path: &str) -> Result<Option<String>> {
        self.require_unsandboxed_io()?;
        if path.starts_with("/inet/udp/") {
            self.ensure_inet_udp(path)?;
            let s = self.inet_udp.get_mut(path).unwrap();
            let mut buf = [0u8; 65536];
            let n = s
                .recv(&mut buf)
                .map_err(|e| Error::Runtime(format!("inet udp recv `{path}`: {e}")))?;
            if n == 0 {
                return Ok(None);
            }
            return Ok(Some(String::from_utf8_lossy(&buf[..n]).into_owned()));
        }
        if path.starts_with("/inet/tcp/") {
            self.ensure_inet_tcp_pair(path)?;
            let reader = self.inet_tcp_read.get_mut(path).unwrap();
            let mut line = String::new();
            let n = reader.read_line(&mut line).map_err(Error::Io)?;
            if n == 0 {
                return Ok(None);
            }
            // The caller no longer trims, so strip the terminator here.
            let keep = line.trim_end_matches('\n').len();
            line.truncate(keep);
            return Ok(Some(line));
        }
        if path.starts_with("/inet/") {
            return Err(Error::Runtime(format!(
                "unsupported inet path `{path}` (use /inet/tcp/... or /inet/udp/...)"
            )));
        }
        let p = Path::new(path);
        // `is_dir` is a `stat` syscall, and this runs once per `getline` — 66%
        // of a `while ((getline l < f) > 0)` loop in the profile, more than the
        // read itself. Only the *first* call for a path can discover that it is
        // a directory: after that the path is either already being iterated as
        // one, or has an open file handle. Both are answered from a map.
        let is_dir = self.dir_read.contains_key(path)
            || (!self.file_handles.contains_key(path) && p.is_dir());
        if is_dir {
            self.require_unsandboxed_io()?;
            if !self.dir_read.contains_key(path) {
                let mut names: Vec<String> = std::fs::read_dir(p)
                    .map_err(|e| Error::Runtime(format!("read_dir {path}: {e}")))?
                    .filter_map(|e| e.ok().map(|x| x.file_name().to_string_lossy().into_owned()))
                    .collect();
                names.sort();
                self.dir_read.insert(path.to_string(), (names, 0));
            }
            let (names, i) = self.dir_read.get_mut(path).unwrap();
            if *i >= names.len() {
                return Ok(None);
            }
            let name = names[*i].clone();
            *i += 1;
            return Ok(Some(name));
        }
        if !self.file_handles.contains_key(path) {
            // Preserve the underlying `io::Error` so `getline_error_code_for_key`
            // can route through `set_errno_io`, giving ERRNO the clean OS error
            // message ("No such file or directory") that gawk produces, rather
            // than the noisier "open <path>: <full Rust display>" prefix.
            let f = File::open(getline_open_path(p)).map_err(Error::Io)?;
            self.file_handles
                .insert(path.to_string(), BufReader::new(f));
        }
        let to = self.procinfo_read_timeout_ms_for(path);
        #[cfg(unix)]
        if to > 0 {
            use std::os::unix::io::AsRawFd;
            let fd = self.file_handles[path].get_ref().as_raw_fd();
            wait_fd_read_timeout(fd, to)?;
        }
        let (rs, re, mut leftover) = self.take_getline_rs_state(path)?;
        let reader = self.file_handles.get_mut(path).unwrap();
        let read = read_record_from_stream(reader, &rs, re.as_ref(), &mut leftover);
        self.restore_getline_rs_state(path, re, leftover);
        self.finish_redirected_getline(read?)
    }

    /// Publish `RT` for a redirected `getline` and hand back the record.
    ///
    /// gawk sets `RT` from every `getline`, not just the main record loop:
    /// `BEGIN { RS="2"; getline l < "input.txt"; print RT }` prints `2`. mawk and
    /// one-true-awk have no `RT` at all, so there is nothing to disagree with.
    fn finish_redirected_getline(
        &mut self,
        read: Option<(String, Vec<u8>)>,
    ) -> Result<Option<String>> {
        match read {
            Some((record, sep)) => {
                self.set_rt_from_bytes(&sep);
                Ok(Some(record))
            }
            None => Ok(None),
        }
    }

    fn ensure_inet_tcp_pair(&mut self, path: &str) -> Result<()> {
        if self.inet_tcp_read.contains_key(path) {
            return Ok(());
        }
        let (lport, host, rport) = parse_inet_tcp(path)
            .ok_or_else(|| Error::Runtime(format!("invalid /inet/tcp/ path `{path}`")))?;
        let stream = if lport == 0 {
            TcpStream::connect((host.as_str(), rport))
                .map_err(|e| Error::Runtime(format!("inet connect `{path}`: {e}")))?
        } else {
            tcp_connect_with_local_port(&host, lport, rport)?
        };
        let w = stream
            .try_clone()
            .map_err(|e| Error::Runtime(format!("inet: {e}")))?;
        let to = self.procinfo_read_timeout_ms_for(path);
        if to > 0 {
            let d = Duration::from_millis(to as u64);
            stream
                .set_read_timeout(Some(d))
                .map_err(|e| Error::Runtime(format!("inet tcp read timeout: {e}")))?;
        }
        self.inet_tcp_read
            .insert(path.to_string(), BufReader::new(stream));
        self.inet_tcp_write.insert(path.to_string(), w);
        Ok(())
    }

    fn ensure_inet_udp(&mut self, path: &str) -> Result<()> {
        if self.inet_udp.contains_key(path) {
            return Ok(());
        }
        let (lport, host, rport) = parse_inet_udp(path)
            .ok_or_else(|| Error::Runtime(format!("invalid /inet/udp/ path `{path}`")))?;
        let mut addrs = format!("{host}:{rport}")
            .to_socket_addrs()
            .map_err(|e| Error::Runtime(format!("inet udp resolve `{host}`: {e}")))?;
        let addr = addrs
            .next()
            .ok_or_else(|| Error::Runtime(format!("inet udp: no address for `{host}:{rport}`")))?;
        let socket = match addr {
            SocketAddr::V4(_) => UdpSocket::bind((Ipv4Addr::UNSPECIFIED, lport)),
            SocketAddr::V6(_) => UdpSocket::bind((Ipv6Addr::UNSPECIFIED, lport)),
        }
        .map_err(|e| Error::Runtime(format!("inet udp bind `{path}`: {e}")))?;
        socket
            .connect(addr)
            .map_err(|e| Error::Runtime(format!("inet udp connect `{path}`: {e}")))?;
        let to = self.procinfo_read_timeout_ms_for(path);
        if to > 0 {
            socket
                .set_read_timeout(Some(Duration::from_millis(to as u64)))
                .map_err(|e| Error::Runtime(format!("inet udp read timeout: {e}")))?;
        }
        self.inet_udp.insert(path.to_string(), socket);
        Ok(())
    }

    /// Flush every open output handle (files via `>` / `>>` and pipes via `|`),
    /// best-effort — used by `system()` to interleave subprocess output correctly.
    pub fn flush_all_output_handles(&mut self) {
        for w in self.output_handles.values_mut() {
            let _ = w.flush();
        }
        for w in self.pipe_stdin.values_mut() {
            let _ = w.flush();
        }
    }

    /// Push every byte awk still holds to the OS before handing a child process
    /// a descriptor it shares with us.
    ///
    /// A child inherits this process's stdout, so whatever is sitting in
    /// `print_buf` or in libc's stdout buffer would surface *after* the child's
    /// own writes and reorder output the program emitted first. `system()` and
    /// opening an output pipe both hand stdout to a child, and both need this.
    pub fn flush_stdout_before_child(&mut self) {
        let _ = crate::vm::flush_print_buf(&mut self.print_buf);
        let _ = std::io::stdout().flush();
        self.flush_all_output_handles();
    }
    /// `close_handle` — see implementation for the contract.
    pub fn close_handle(&mut self, path: &str) -> f64 {
        let mut exit_status: f64 = 0.0;
        let mut had_any = false;
        if is_program_stdout(path) {
            // The stream stays open — awk keeps printing after it — so this is
            // a flush that reports success, matching gawk's `close("/dev/stdout")`
            // answer of 0. Reporting -1 ("no such stream") would be wrong now
            // that writes to the name are accepted.
            let _ = crate::vm::flush_print_buf(&mut self.print_buf);
            let _ = std::io::stdout().flush();
            return 0.0;
        }
        // Any carry-over bytes belong to the stream being torn down; a reopen
        // under the same name must start clean.
        self.getline_leftover.remove(path);
        if let Some(h) = self.coproc_handles.remove(path) {
            had_any = true;
            let _ = shutdown_coproc(h);
        }
        if let Some(mut w) = self.output_handles.remove(path) {
            had_any = true;
            let _ = w.flush();
        }
        if let Some(mut w) = self.pipe_stdin.remove(path) {
            had_any = true;
            let _ = w.flush();
        }
        if let Some(mut ch) = self.pipe_children.remove(path) {
            had_any = true;
            exit_status = awk_process_status(ch.wait());
        }
        // `cmd | getline` reader/child — drop the reader (closes the read fd), then
        // reap the child so subsequent calls with the same key respawn cleanly.
        if self.pipe_stdout.remove(path).is_some() {
            had_any = true;
        }
        if let Some(mut ch) = self.pipe_input_children.remove(path) {
            had_any = true;
            exit_status = awk_process_status(ch.wait());
        }
        if self.file_handles.remove(path).is_some() {
            had_any = true;
        }
        if self.dir_read.remove(path).is_some() {
            had_any = true;
        }
        if self.inet_tcp_read.remove(path).is_some() {
            had_any = true;
        }
        if self.inet_tcp_write.remove(path).is_some() {
            had_any = true;
        }
        if self.inet_udp.remove(path).is_some() {
            had_any = true;
        }
        // gawk parity: `close()` on a name that does not correspond to an open handle
        // returns -1 (POSIX awk allows the return value to be implementation-defined,
        // but gawk's contract is "-1 for unknown name, 0 for a clean close of a file/pipe").
        if !had_any {
            return -1.0;
        }
        exit_status
    }
    /// `rand` — see implementation for the contract.
    pub fn rand(&mut self) -> f64 {
        self.rand_seed = self.rand_seed.wrapping_mul(1103515245).wrapping_add(12345);
        f64::from((self.rand_seed >> 16) as u32 & 0x7fff) / 32768.0
    }

    /// Seed PRNG; **`n`** is the full **`u64`** seed (POSIX/gawk-style **`srand(x)`** truncates **`x`** to an integer first).
    pub fn srand(&mut self, n: Option<u64>) -> f64 {
        let prev = self.rand_seed;
        self.rand_seed = n.unwrap_or_else(|| {
            std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .map(|d| d.as_secs() ^ (d.subsec_nanos() as u64))
                .unwrap_or(1)
        });
        (prev & 0xffff_ffff) as f64
    }
    /// `set_field_sep_split` — see implementation for the contract.
    pub fn set_field_sep_split(&mut self, fs: &str, line: &[u8]) {
        self.reset_record(line);
        self.cached_fs.clear();
        self.cached_fs.push_str(fs);
    }

    /// [`set_field_sep_split`](Self::set_field_sep_split) minus the `FS` copy —
    /// for the record loop, where `FS` is usually the same as last record's.
    fn reset_record(&mut self, line: &[u8]) {
        self.record.clear();
        self.record.push_bytes(line);
        self.record_assigned = true;
        self.record_strnum = true;
        self.field_strnum.clear();
        self.fields_dirty = false;
        self.fields_pending_split = true;
        self.fields.clear();
        self.field_ranges.clear();
    }

    /// Start a new record, reading `FS` from the variable map.
    ///
    /// POSIX makes a rule's assignment to `FS` take effect on the *next* record,
    /// so the record loop has to re-read it every time and cannot hoist it. It
    /// used to do that through `Value::as_str()`, which clones — a `String`
    /// allocation per record for a value that changes in essentially no
    /// programs. Comparing against the copy `cached_fs` already holds keeps the
    /// steady state allocation-free while re-reading just as faithfully.
    pub fn set_record_with_current_fs(&mut self, line: &[u8]) {
        let unchanged = match self.vars.get("FS") {
            Some(Value::Str(s)) | Some(Value::StrLit(s)) | Some(Value::Regexp(s)) => {
                s == &self.cached_fs
            }
            // A numeric `FS`, or none at all, goes the long way — both are rare
            // and neither can be compared without building the string.
            _ => false,
        };
        if unchanged {
            self.reset_record(line);
            return;
        }
        let fs = self
            .vars
            .get("FS")
            .map(|v| v.as_str())
            .unwrap_or_else(|| " ".into());
        self.set_field_sep_split(&fs, line);
    }

    /// Like [`set_field_sep_split`](Self::set_field_sep_split) but takes an owned line (avoids extra
    /// copies when the caller already has a `String`, e.g. `gsub` replacing `$0`).
    pub fn set_field_sep_split_owned(&mut self, fs: &str, line: AwkStr) {
        self.record = line;
        self.record_assigned = true;
        self.record_strnum = true;
        self.field_strnum.clear();
        self.fields_dirty = false;
        self.fields_pending_split = true;
        self.cached_fs.clear();
        self.cached_fs.push_str(fs);
        self.fields.clear();
        self.field_ranges.clear();
    }

    /// Ensure fields are split. Called lazily before any field access.
    /// Uses **`FPAT`** when set to a non-empty pattern (gawk-style field-by-content); otherwise **`FS`**.
    #[inline]
    pub fn ensure_fields_split(&mut self) {
        if self.fields_pending_split {
            self.fields_pending_split = false;
            // Before `split_record_fields` borrows `self.record`: memoising the
            // compiled `FS` needs `&mut self`, and the split holds the record
            // and the field vector at once.
            self.sync_fs_regex();
            self.split_record_fields();
        }
    }

    /// Split `self.record` into `field_ranges` using current **`FPAT`** (if non-empty) or **`FS`**.
    /// Uses `cached_fs` when available (set by `set_field_sep_split`) to avoid per-record
    /// HashMap lookups and String allocations for the common case.
    /// Bring [`Self::fs_regex`] in step with the current `FS` and `IGNORECASE`.
    ///
    /// Runs before `self.record` is borrowed for the split, because it needs
    /// `&mut self`. The clone only happens on a miss — i.e. when `FS` or
    /// `IGNORECASE` actually changed, which is essentially never inside a record
    /// loop. A single-character, empty or default `FS` never reaches the regex
    /// engine, so nothing is compiled for it.
    fn sync_fs_regex(&mut self) {
        // Only a multi-character `FS` reaches the regex engine, so the common
        // separators return before even reading `IGNORECASE`.
        let fs_len = self.cached_fs.len();
        if fs_len <= 1 || self.cached_fs == " " {
            return;
        }
        let ignore_case = self.ignore_case_flag();
        if self
            .fs_regex
            .as_ref()
            .is_some_and(|(pat, ic, _)| *ic == ignore_case && pat.as_str() == self.cached_fs)
        {
            return;
        }
        let compiled = build_fs_regex(&self.cached_fs, ignore_case);
        self.fs_regex = Some((self.cached_fs.clone(), ignore_case, compiled));
    }

    fn split_record_fields(&mut self) {
        let record: &[u8] = self.record.as_bytes();
        if self.csv_mode {
            split_csv_gawk_fields(record, &mut self.field_ranges);
            self.fields.clear();
            for &(s, e) in &self.field_ranges {
                let raw = &record[s as usize..e as usize];
                // CSV doubled-quote escape: `""` → `"` inside a quoted field (gawk / RFC 4180).
                self.fields
                    .push(if memchr::memmem::find(raw, b"\"\"").is_some() {
                        AwkStr::from_vec(byte_replace(raw, b"\"\"", b"\""))
                    } else {
                        AwkStr::from(raw)
                    });
            }
            self.fields_dirty = true;
            return;
        }
        let ic = self.ignore_case_flag();
        if let Some(fw) = self.fieldwidths_vec() {
            if !fw.is_empty() {
                split_fields_fieldwidths(record, &fw, &mut self.field_ranges);
                self.fields.clear();
                self.fields_dirty = false;
                return;
            }
        }
        let fpat_trimmed: Option<String> = self.get_global_var("FPAT").and_then(|fv| {
            if !matches!(
                fv,
                Value::Str(ref s) | Value::StrLit(ref s) if !s.to_str_lossy().trim().is_empty()
            ) {
                return None;
            }
            let t = fv.as_str_cow();
            let tr = t.as_ref().trim();
            if tr.is_empty() {
                None
            } else {
                Some(tr.to_string())
            }
        });
        if let Some(ref fp_trimmed) = fpat_trimmed {
            if split_fields_fpat(record, fp_trimmed, &mut self.field_ranges) {
                return;
            }
        }
        // Use cached_fs (set by set_field_sep_split) to avoid HashMap lookup + String clone.
        let cab = self.characters_as_bytes;
        // Paragraph mode (`RS == ""`) makes <newline> an extra field separator for
        // a single-character FS. `FS == " "` already splits on newline and a regex
        // FS is left alone, so RS is only looked up in the one case that needs it —
        // the common paths keep their single global lookup.
        if !self.cached_fs.is_empty() {
            let para = self.cached_fs.len() == 1 && self.cached_fs != " " && self.paragraph_mode();
            let fs_re = memoised_fs_regex(&self.fs_regex, &self.cached_fs);
            split_fields_into(
                record,
                &self.cached_fs,
                &mut self.field_ranges,
                ic,
                cab,
                para,
                fs_re,
            );
        } else {
            match self.get_global_var("FS") {
                None => split_fields_into(
                    record,
                    " ",
                    &mut self.field_ranges,
                    ic,
                    cab,
                    false,
                    FsRegex::Unknown,
                ),
                Some(v) => {
                    let fs = v.as_str_cow().into_owned();
                    let para = fs.len() == 1 && fs != " " && self.paragraph_mode();
                    split_fields_into(
                        record,
                        &fs,
                        &mut self.field_ranges,
                        ic,
                        cab,
                        para,
                        FsRegex::Unknown,
                    );
                }
            }
        }
    }

    fn fieldwidths_vec(&self) -> Option<Vec<FieldwidthsSpec>> {
        let t = self.get_global_var("FIELDWIDTHS")?.as_str();
        let t = t.trim();
        if t.is_empty() {
            return None;
        }
        // gawk tokens:
        //   "N"      → take an N-byte field (skip 0)
        //   "M:N"    → skip M bytes, then take N bytes
        //   "*"      → take everything remaining (must be the last token)
        let mut specs = Vec::new();
        for tok in t.split_whitespace() {
            if tok == "*" {
                specs.push(FieldwidthsSpec {
                    skip: 0,
                    width: FIELDWIDTHS_REST,
                });
                continue;
            }
            if let Some((skip_s, width_s)) = tok.split_once(':') {
                let skip = skip_s.parse::<usize>().ok()?;
                if width_s == "*" {
                    specs.push(FieldwidthsSpec {
                        skip,
                        width: FIELDWIDTHS_REST,
                    });
                } else {
                    let width = width_s.parse::<usize>().ok()?;
                    if width == 0 {
                        continue;
                    }
                    specs.push(FieldwidthsSpec { skip, width });
                }
            } else if let Ok(width) = tok.parse::<usize>() {
                if width == 0 {
                    continue;
                }
                specs.push(FieldwidthsSpec { skip: 0, width });
            }
        }
        if specs.is_empty() {
            None
        } else {
            Some(specs)
        }
    }
    /// `field` — see implementation for the contract.
    pub fn field(&mut self, i: i32) -> crate::error::Result<Value> {
        if i < 0 {
            return Err(crate::error::Error::Runtime(
                "attempt to access field number -1".into(),
            ));
        }
        let idx = i as usize;
        if idx == 0 {
            let rec = self.record.clone();
            // A record assigned a plain string is not a numeric string, so it
            // must come back as `StrLit` — that is what makes `$0 < 7` compare
            // as text after `$0 = "42"`.
            return Ok(if self.record_strnum {
                Value::Str(rec)
            } else {
                Value::StrLit(rec)
            });
        }
        self.ensure_fields_split();
        if self.fields_dirty {
            // Same rule per field: only a field *assigned* a plain string loses
            // numeric-string status; everything the splitter produced keeps it.
            let strnum = self.field_strnum.get(idx - 1).copied().unwrap_or(true);
            let make = if strnum { Value::Str } else { Value::StrLit };
            Ok(self
                .fields
                .get(idx - 1)
                .map(|f| make(f.clone()))
                .unwrap_or_else(|| Value::Str(String::new().into())))
        } else {
            Ok(self
                .field_ranges
                .get(idx - 1)
                .map(|&(s, e)| Value::Str(AwkStr::from(&self.record[s as usize..e as usize])))
                .unwrap_or_else(|| Value::Str(String::new().into())))
        }
    }

    /// Get field value as f64 directly without allocating a String.
    #[inline]
    pub fn field_as_number(&mut self, i: i32) -> crate::error::Result<f64> {
        if i < 0 {
            return Err(crate::error::Error::Runtime(
                "attempt to access field number -1".into(),
            ));
        }
        let idx = i as usize;
        if idx == 0 {
            return Ok(parse_number(&self.record.to_str_lossy()));
        }
        self.ensure_fields_split();
        if self.fields_dirty {
            Ok(self
                .fields
                .get(idx - 1)
                .map(|s| parse_number(&s.to_str_lossy()))
                .unwrap_or(0.0))
        } else {
            Ok(self
                .field_ranges
                .get(idx - 1)
                .map(|&(s, e)| {
                    parse_number(&String::from_utf8_lossy(
                        &self.record[s as usize..e as usize],
                    ))
                })
                .unwrap_or(0.0))
        }
    }

    /// Write field bytes directly into print_buf without allocating a String.
    /// Uses split borrowing within the method to avoid borrow conflicts.
    #[inline]
    pub fn print_field_to_buf(&mut self, idx: usize) {
        if idx == 0 {
            self.print_buf.extend_from_slice(self.record.as_bytes());
            return;
        }
        self.ensure_fields_split();
        if self.fields_dirty {
            if let Some(s) = self.fields.get(idx - 1) {
                self.print_buf.extend_from_slice(s.as_bytes());
            }
        } else if let Some(&(s, e)) = self.field_ranges.get(idx - 1) {
            self.print_buf
                .extend_from_slice(&self.record.as_bytes()[s as usize..e as usize]);
        }
    }

    /// Get a field as &str without allocating (zero-copy from record).
    #[allow(dead_code)]
    pub fn field_str(&self, i: usize) -> &[u8] {
        if i == 0 {
            return self.record.as_bytes();
        }
        if self.fields_dirty {
            self.fields.get(i - 1).map(|s| s.as_bytes()).unwrap_or(b"")
        } else {
            self.field_ranges
                .get(i - 1)
                .map(|&(s, e)| &self.record[s as usize..e as usize])
                .unwrap_or(b"")
        }
    }

    /// Number of fields in the current record.
    #[inline]
    #[allow(dead_code)]
    pub fn nf(&mut self) -> usize {
        self.ensure_fields_split();
        if self.fields_dirty {
            self.fields.len()
        } else {
            self.field_ranges.len()
        }
    }

    /// Mark a slot as used (read as a value, or written). See
    /// [`Self::slot_touched`]. Grows the bit vector on demand so the many
    /// places that rebuild `slots` wholesale need no matching update — an
    /// absent bit means "never touched", which is the right default.
    #[inline]
    pub fn touch_slot(&mut self, slot: usize) {
        if self.slot_touched.len() <= slot {
            self.slot_touched.resize(slot + 1, false);
        }
        self.slot_touched[slot] = true;
    }

    /// Has this slot ever been read or written? See [`Self::touch_slot`].
    #[inline]
    pub fn slot_was_touched(&self, slot: usize) -> bool {
        self.slot_touched.get(slot).copied().unwrap_or(false)
    }

    /// True when `$i` is out of range for the current record (`i >= 1` and `i > NF`).
    #[inline]
    pub fn field_is_unassigned(&mut self, i: i32) -> bool {
        if i < 1 {
            return false;
        }
        (i as usize) > self.nf()
    }

    /// Assign `$0` — replace the record and re-split fields / `NF` per POSIX.
    ///
    /// The new record keeps numeric-string status, which is right for the
    /// callers that hand over an input-derived line. Assignments from awk source
    /// go through [`set_record_str_strnum`](Self::set_record_str_strnum) so a
    /// plain string like `$0 = "42"` stays a plain string.
    pub fn set_record_str(&mut self, val: &str) {
        self.set_record_str_strnum(val.as_bytes(), true);
    }

    /// [`set_record_str`](Self::set_record_str), stating whether the assigned
    /// text is a POSIX numeric string. Splitting always yields numeric-string
    /// fields, so only `$0` itself is affected.
    pub fn set_record_str_strnum(&mut self, val: &[u8], strnum: bool) {
        let fs = self
            .get_global_var("FS")
            .map(|v| v.as_str())
            .unwrap_or_else(|| " ".into());
        self.set_field_sep_split(&fs, val);
        self.ensure_fields_split();
        self.record_strnum = strnum;
        let nf = self.nf() as f64;
        self.vars.insert("NF".into(), Value::Num(nf));
    }

    /// Assign `NF` — truncate or extend fields and rebuild `$0` with `OFS`.
    pub fn set_nf(&mut self, n: i32) -> crate::error::Result<()> {
        if n < 0 {
            return Err(crate::error::Error::Runtime(
                "NF set to negative value".into(),
            ));
        }
        let nf = n as usize;
        self.ensure_fields_split();
        if !self.fields_dirty {
            self.fields.clear();
            for &(s, e) in &self.field_ranges {
                self.fields
                    .push(AwkStr::from(&self.record[s as usize..e as usize]));
            }
            self.fields_dirty = true;
        }
        if self.fields.len() > nf {
            self.fields.truncate(nf);
        } else {
            self.fields.resize(nf, String::new().into());
        }
        self.rebuild_record();
        self.vars.insert("NF".into(), Value::Num(nf as f64));
        Ok(())
    }
    /// `set_field` — see implementation for the contract.
    ///
    /// Treats `val` as a numeric string; [`set_field_strnum`](Self::set_field_strnum)
    /// is the form that says otherwise.
    pub fn set_field(&mut self, i: i32, val: &str) -> crate::error::Result<()> {
        self.set_field_strnum(i, val.as_bytes(), true)
    }

    /// [`set_field`](Self::set_field), stating whether the assigned text is a
    /// POSIX numeric string. `$1 = "42"` is not one, so `$1 < 7` compares as
    /// text in gawk, mawk and one-true-awk; `$1 = $2` and `$1 = 42` are.
    pub fn set_field_strnum(
        &mut self,
        i: i32,
        val: &[u8],
        strnum: bool,
    ) -> crate::error::Result<()> {
        if i == 0 {
            self.set_record_str_strnum(val, strnum);
            return Ok(());
        }
        if i < 1 {
            return Err(crate::error::Error::Runtime(
                "attempt to access field number -1".into(),
            ));
        }
        // Force the pending lazy split *before* materializing. Splitting is
        // deferred until a field is read, so a record whose first use is an
        // assignment still has an empty `field_ranges`: materializing from it
        // produced zero fields and `$1 = "Z"` silently destroyed every other
        // field and most of `$0`. Only the streaming input path split eagerly
        // enough to hide it, and the differential corpora feed every case on
        // stdin, so the file/mmap path never showed the loss.
        self.ensure_fields_split();
        // Materialize owned fields from ranges if needed
        if !self.fields_dirty {
            self.fields.clear();
            for &(s, e) in &self.field_ranges {
                self.fields
                    .push(AwkStr::from(&self.record[s as usize..e as usize]));
            }
            self.fields_dirty = true;
        }
        let idx = (i - 1) as usize;
        if self.fields.len() <= idx {
            self.fields.resize(idx + 1, String::new().into());
        }
        self.fields[idx] = AwkStr::from(val);
        // Absent entries read as `true`, so only a non-numeric-string assignment
        // has to be recorded — and the vec has to reach `idx` to record it.
        if !strnum || self.field_strnum.len() > idx {
            if self.field_strnum.len() <= idx {
                self.field_strnum.resize(idx + 1, true);
            }
            self.field_strnum[idx] = strnum;
        }
        self.rebuild_record();
        let nf = self.fields.len() as f64;
        self.vars.insert("NF".into(), Value::Num(nf));
        Ok(())
    }

    /// Set a field to a numeric value directly, formatting in-place without
    /// allocating a temporary `Value::Num` and round-tripping through `as_str()`.
    pub fn set_field_num(&mut self, i: i32, n: f64) -> crate::error::Result<()> {
        if i == 0 {
            let s = if n.is_finite() && n.fract() == 0.0 {
                format!("{:.0}", n)
            } else {
                format!("{n}")
            };
            self.set_record_str(&s);
            return Ok(());
        }
        if i < 1 {
            return Err(crate::error::Error::Runtime(
                "attempt to access field number -1".into(),
            ));
        }
        // Same pending-split hazard as [`set_field_strnum`](Self::set_field_strnum).
        self.ensure_fields_split();
        if !self.fields_dirty {
            self.fields.clear();
            for &(s, e) in &self.field_ranges {
                self.fields
                    .push(AwkStr::from(&self.record[s as usize..e as usize]));
            }
            self.fields_dirty = true;
        }
        let idx = (i - 1) as usize;
        if self.fields.len() <= idx {
            self.fields.resize(idx + 1, String::new().into());
        }
        // Format number into the existing String, reusing its allocation.
        self.fields[idx].clear();
        if n.is_finite() && n.fract() == 0.0 {
            use std::fmt::Write;
            let _ = write!(self.fields[idx], "{:.0}", n);
        } else {
            use std::fmt::Write;
            let _ = write!(self.fields[idx], "{n}");
        }
        self.rebuild_record();
        let nf = self.fields.len() as f64;
        self.vars.insert("NF".into(), Value::Num(nf));
        Ok(())
    }

    fn rebuild_record(&mut self) {
        let ofs = self
            .vars
            .get("OFS")
            .map(|v| v.as_str())
            .unwrap_or_else(|| " ".into());
        self.record = join_awkstrs(&self.fields, ofs.as_bytes());
        self.record_assigned = true;
        // A record joined back together from fields is a computed string. gawk
        // and one-true-awk both treat it that way (`$1 = $1` then `$0 < 7` is a
        // string compare on a record of `42`); mawk keeps it a numeric string
        // and is the outlier here.
        self.record_strnum = false;
    }
    /// `set_record_from_line` — see implementation for the contract.
    pub fn set_record_from_line(&mut self, line: &str) {
        let trimmed = line.trim_end_matches(['\n', '\r']);
        let fs = self
            .vars
            .get("FS")
            .map(|v| v.as_str())
            .unwrap_or_else(|| " ".into());
        self.set_field_sep_split(&fs, trimmed.as_bytes());
    }

    /// Parse the current `line_buf` as a record. Avoids the borrow-checker conflict
    /// of borrowing `line_buf` and calling `set_field_sep_split` simultaneously.
    pub fn set_record_from_line_buf(&mut self) {
        let rs = self.rs_string();
        let mut end = self.line_buf.len();
        if rs == "\n" {
            // gawk parity: only `\n` is the record terminator on Unix; a trailing
            // `\r` is part of the record (kept in `$0` / `length`). Older awkrs
            // also stripped `\r` here, breaking CRLF parity.
            while end > 0 && self.line_buf[end - 1] == b'\n' {
                end -= 1;
            }
        }
        // Copy the trimmed line into record (reuses allocation)
        self.record.clear();
        self.record_assigned = true;
        self.record_strnum = true;
        self.field_strnum.clear();
        // The record is the bytes that were read — no decode, no validation, no
        // substitution. This is the line that used to replace every byte that is
        // not part of valid UTF-8 with `U+FFFD`, which is where byte
        // transparency was lost for `$0`, every field cut from it, and every
        // `print` of either.
        self.record.push_bytes(&self.line_buf[..end]);
        // Sync cached_fs from vars (non-allocating check; only copies when changed).
        let fs_changed = match self.vars.get("FS") {
            Some(Value::Str(s)) | Some(Value::StrLit(s)) | Some(Value::Regexp(s)) => {
                s != &self.cached_fs
            }
            _ => false,
        };
        if fs_changed {
            if let Some(Value::Str(s)) | Some(Value::StrLit(s)) | Some(Value::Regexp(s)) =
                self.vars.get("FS")
            {
                self.cached_fs.clear();
                self.cached_fs.push_str(&s.to_str_lossy());
            }
        }
        // Split using current FPAT or FS
        self.fields_dirty = false;
        self.fields.clear();
        self.field_ranges.clear();
        // The streaming path splits eagerly rather than through
        // `ensure_fields_split`, so it memoises the compiled `FS` itself.
        self.sync_fs_regex();
        self.split_record_fields();
        let nf = self.nf() as f64;
        self.vars.insert("NF".into(), Value::Num(nf));
    }

    /// `SYMTAB[name]` — live global / slot value (gawk introspection).
    pub fn symtab_elem_get(&self, key: &str) -> Value {
        if let Some(&slot) = self.symtab_slot_map.get(key) {
            let i = slot as usize;
            if i < self.slots.len() {
                return self.slots[i].clone();
            }
        }
        self.get_global_var(key)
            .cloned()
            .unwrap_or_else(|| self.builtin_scalar_symtab(key))
    }

    fn builtin_scalar_symtab(&self, name: &str) -> Value {
        match name {
            "NR" => Value::Num(self.nr),
            "FNR" => Value::Num(self.fnr),
            "NF" => Value::Num(if self.fields_dirty {
                self.fields.len()
            } else {
                self.field_ranges.len()
            } as f64),
            "FILENAME" => Value::Str(self.filename.clone().into()),
            _ => Value::Uninit,
        }
    }

    /// Enumerate SYMTAB keys (globals, slot-backed names, special scalars).
    pub fn symtab_keys_reflect(&self) -> Vec<AwkStr> {
        use rustc_hash::FxHashSet;
        let mut seen = FxHashSet::default();
        for k in self.vars.keys() {
            if matches!(k.as_str(), "SYMTAB" | "FUNCTAB" | "PROCINFO") {
                continue;
            }
            seen.insert(AwkStr::from(k.as_str()));
        }
        if let Some(g) = &self.global_readonly {
            for k in g.keys() {
                if matches!(k.as_str(), "SYMTAB" | "FUNCTAB" | "PROCINFO") {
                    continue;
                }
                seen.insert(AwkStr::from(k.as_str()));
            }
        }
        for k in self.symtab_slot_map.keys() {
            seen.insert(AwkStr::from(k.as_str()));
        }
        for &s in crate::namespace::SPECIAL_GLOBAL_NAMES {
            seen.insert(AwkStr::from(s));
        }
        let mut out: Vec<_> = seen.into_iter().collect();
        out.sort();
        out
    }

    fn symtab_has_key(&self, key: &str) -> bool {
        if self.symtab_slot_map.contains_key(key) {
            return true;
        }
        if self.vars.contains_key(key) && !matches!(key, "SYMTAB" | "FUNCTAB" | "PROCINFO") {
            return true;
        }
        if self
            .global_readonly
            .as_ref()
            .is_some_and(|g| g.contains_key(key))
        {
            return true;
        }
        !matches!(self.symtab_elem_get(key), Value::Uninit)
    }

    /// `SYMTAB[name] = v` — assign global or slot (not a materialized mirror array).
    /// `s = s expr` without reading `s` out first: append to the string the
    /// scalar already holds.
    ///
    /// The read-modify-write spelling copies the whole accumulator three times
    /// per iteration — once out of the symtab, once inside the concat, once
    /// back in on assignment — so an append loop is quadratic in the length it
    /// builds. gawk grows the value in place for the same reason, which is why
    /// it finishes a 100 K-iteration build in the time this took for a few
    /// thousand.
    ///
    /// Observably identical to `symtab_elem_set(key, Str(<value of key> +
    /// suffix))`, kind included: a concatenation yields a dynamic `Str`, so a
    /// later relational still sees a numeric string. `OFS`/`ORS` are excluded
    /// from the in-place path because their byte caches are refreshed by
    /// `symtab_elem_set`, which the fallback below still goes through.
    pub fn symtab_elem_append(&mut self, key: &str, suffix: &str) {
        if !matches!(key, "OFS" | "ORS") {
            let slot = self.symtab_slot_map.get(key).map(|&s| s as usize);
            let held = match slot {
                Some(i) => self.slots.get_mut(i),
                None => self.vars.get_mut(key),
            };
            if let Some(v) = held {
                if let Value::Str(s) | Value::StrLit(s) = v {
                    let mut owned = std::mem::take(s);
                    owned.push_str(suffix);
                    *v = Value::Str(owned);
                    return;
                }
            }
        }
        // A number, an unset name, `OFS`/`ORS`: no string to grow, so take the
        // same read/convert/store path the fused op replaced.
        let cur = self.symtab_elem_get(key);
        let mut s = self.value_to_str_convfmt(&cur).into_owned();
        s.push_str(suffix);
        self.symtab_elem_set(key, Value::Str(s.into()));
    }

    pub fn symtab_elem_set(&mut self, key: &str, val: Value) {
        if let Some(&slot) = self.symtab_slot_map.get(key) {
            let i = slot as usize;
            if i < self.slots.len() {
                self.slots[i] = val;
                return;
            }
        }
        match key {
            "OFS" => self.ofs_bytes = val.as_str().into_bytes(),
            "ORS" => self.ors_bytes = val.as_str().into_bytes(),
            _ => {}
        }
        self.vars.insert(key.to_string(), val);
    }
    /// `array_get` — see implementation for the contract.
    #[inline]
    pub fn array_get(&self, name: &str, key: &str) -> Value {
        if name == "SYMTAB" {
            return self.symtab_elem_get(key);
        }
        match self.get_global_var(name) {
            Some(Value::Array(a)) => match a.get(key) {
                Some(Value::Num(n)) => Value::Num(*n),
                Some(v) => v.clone(),
                None => Value::Str(String::new().into()),
            },
            _ => Value::Str(String::new().into()),
        }
    }
    /// [`Self::array_get`] with a byte subscript.
    pub fn array_get_bytes(&self, name: &str, key: &[u8]) -> Value {
        if name == "SYMTAB" {
            return self.symtab_elem_get(&String::from_utf8_lossy(key));
        }
        match self.get_global_var(name) {
            Some(Value::Array(a)) => match a.get_bytes(key) {
                Some(Value::Num(n)) => Value::Num(*n),
                Some(v) => v.clone(),
                None => Value::Str(AwkStr::new()),
            },
            _ => Value::Str(AwkStr::new()),
        }
    }

    /// `array_set` — see implementation for the contract.
    /// The integer an array subscript names when it is one, so the VM can reach
    /// [`AwkArray`]'s integer half without rendering the number to a string for
    /// the array to parse straight back.
    pub fn subscript_int(v: &Value) -> Option<i64> {
        match v {
            Value::Num(n)
                if n.is_finite() && n.fract() == 0.0 && n.abs() < 9_007_199_254_740_992.0 =>
            {
                // `-0` subscripts the same element as `0`; the cast already
                // gives 0 for both, which is what the string path renders too.
                Some(*n as i64)
            }
            _ => None,
        }
    }

    /// `a[k]` as an rvalue, creating the element POSIX says the read brings into
    /// existence. Byte subscript — see [`Self::array_key_bytes_in`].
    pub fn array_get_vivify_bytes(&mut self, name: &str, key: &[u8]) -> Value {
        if name == "SYMTAB" {
            return self.symtab_elem_get(&String::from_utf8_lossy(key));
        }
        if let Some(Value::Array(a)) = self.vars.get_mut(name) {
            if let Some(v) = a.get_bytes(key) {
                return match v {
                    Value::Num(n) => Value::Num(*n),
                    other => other.clone(),
                };
            }
            a.insert_bytes(key, Value::Uninit);
            return Value::Uninit;
        }
        // First touch of this name, or one still to be copied out of the
        // readonly globals: `array_set_bytes` handles both, once per array.
        self.array_set_bytes(name, key, Value::Uninit);
        Value::Uninit
    }

    /// `a[i]` with an integer subscript — the counted-loop shape, with no key
    /// rendered and none parsed.
    pub fn array_get_vivify_int(&mut self, name: &str, i: i64) -> Value {
        if name == "SYMTAB" {
            let mut b = KeyBuf::new();
            let k = b.write_i64(i).to_string();
            return self.symtab_elem_get(&k);
        }
        if let Some(Value::Array(a)) = self.vars.get_mut(name) {
            if let Some(v) = a.get_int(i) {
                return match v {
                    Value::Num(n) => Value::Num(*n),
                    other => other.clone(),
                };
            }
            a.insert_int(i, Value::Uninit);
            return Value::Uninit;
        }
        let mut b = KeyBuf::new();
        let k = b.write_i64(i).to_string();
        self.array_set(name, k, Value::Uninit);
        Value::Uninit
    }

    /// `a[i] = v` with an integer subscript.
    pub fn array_set_int(&mut self, name: &str, i: i64, val: Value) {
        if name == "SYMTAB" {
            let mut b = KeyBuf::new();
            let k = b.write_i64(i).to_string();
            self.symtab_elem_set(&k, val);
            return;
        }
        if let Some(Value::Array(a)) = self.vars.get_mut(name) {
            a.insert_int(i, val);
            return;
        }
        let mut b = KeyBuf::new();
        let k = b.write_i64(i).to_string();
        self.array_set(name, k, val);
    }

    /// [`array_set`](Self::array_set) with a borrowed subscript — see
    /// [`AwkArray::insert_str`] for why the borrow matters on this path.
    /// The caller is [`crate::vm::VmCtx::array_elem_set_str`], the store side
    /// of `a[$1] = v` / `a[$1] op= v`, which already holds the key text.
    /// [`Self::array_set_str`] with a byte subscript.
    pub fn array_set_bytes(&mut self, name: &str, key: &[u8], val: Value) {
        if name == "SYMTAB" {
            self.symtab_elem_set(&String::from_utf8_lossy(key), val);
            return;
        }
        if let Some(existing) = self.vars.get_mut(name) {
            match existing {
                Value::Array(a) => {
                    a.insert_bytes(key, val);
                }
                _ => {
                    let mut m = AwkArray::new();
                    m.insert_bytes(key, val);
                    *existing = Value::Array(m);
                }
            }
            return;
        }
        if let Some(Value::Array(a)) = self.global_readonly.as_ref().and_then(|g| g.get(name)) {
            let mut copy = a.clone();
            copy.insert_bytes(key, val);
            self.vars.insert(name.to_string(), Value::Array(copy));
        } else {
            let mut m = AwkArray::new();
            m.insert_bytes(key, val);
            self.vars.insert(name.to_string(), Value::Array(m));
        }
    }

    pub fn array_set_str(&mut self, name: &str, key: &str, val: Value) {
        if name == "SYMTAB" {
            self.symtab_elem_set(key, val);
            return;
        }
        if let Some(existing) = self.vars.get_mut(name) {
            match existing {
                Value::Array(a) => {
                    a.insert_str(key, val);
                }
                _ => {
                    let mut m = AwkArray::new();
                    m.insert_str(key, val);
                    *existing = Value::Array(m);
                }
            }
            return;
        }
        // First access: seed from the read-only `BEGIN` snapshot if it has one.
        if let Some(Value::Array(a)) = self.global_readonly.as_ref().and_then(|g| g.get(name)) {
            let mut copy = a.clone();
            copy.insert_str(key, val);
            self.vars.insert(name.to_string(), Value::Array(copy));
        } else {
            let mut m = AwkArray::new();
            m.insert_str(key, val);
            self.vars.insert(name.to_string(), Value::Array(m));
        }
    }

    pub fn array_set(&mut self, name: &str, key: String, val: Value) {
        if name == "SYMTAB" {
            self.symtab_elem_set(&key, val);
            return;
        }
        // Fast path: array already exists in vars — no name allocation needed.
        if let Some(existing) = self.vars.get_mut(name) {
            match existing {
                Value::Array(a) => {
                    a.insert(key, val);
                    return;
                }
                _ => {
                    let mut m = AwkArray::new();
                    m.insert(key, val);
                    *existing = Value::Array(m);
                    return;
                }
            }
        }
        // Slow path: first access — copy from readonly globals or create new.
        if let Some(Value::Array(a)) = self.global_readonly.as_ref().and_then(|g| g.get(name)) {
            let mut copy = a.clone();
            copy.insert(key, val);
            self.vars.insert(name.to_string(), Value::Array(copy));
        } else {
            let mut m = AwkArray::new();
            m.insert(key, val);
            self.vars.insert(name.to_string(), Value::Array(m));
        }
    }

    /// Fused `a[$field] += delta` (constant field index, e.g. `$5`): build the key from
    /// the split record once and update the array in one map pass.
    ///
    /// Avoids `field(i).as_str()` which allocated twice per call (field string + clone for
    /// `as_str()`), and avoids separate `array_get` + `array_set` lookups.
    ///
    /// Uses a substring of `record` / `fields` as `&str` for `get_mut` so repeated field
    /// values do not allocate a `String` per line; inserts still allocate once for the key.
    ///
    /// Two shapes leave the `f64` fast path because it cannot express them:
    /// `$0` as the subscript (the key is the whole record, not a field), and
    /// `-M`, where the sum has to be an [`Value::Mpfr`] built at the requested
    /// precision. Both used to be answered with the fast path anyway — `$0`
    /// keyed every record under `""`, collapsing the array to a single element,
    /// and `-M` silently did the arithmetic in `f64`.
    pub fn array_field_add_delta(&mut self, name: &str, field: i32, delta: f64) {
        self.ensure_fields_split();
        if field == 0 || self.bignum {
            let key = match field {
                0 => self.record.clone(),
                n => self.field(n).map(|v| v.as_str()).unwrap_or_default().into(),
            };
            if self.bignum {
                let prec = self.mpfr_prec_bits();
                let round = self.mpfr_round();
                let old = value_to_mpfr(&self.array_get(name, &key.to_str_lossy()), prec, round);
                // The delta is a *literal*, so it goes through the same
                // decimal recovery `Op::PushNum` uses — widening the `f64`
                // would make `a[$1] += 0.1` accumulate the double rather than
                // one tenth.
                let d = crate::bignum::literal_f64_to_mpfr(delta, self);
                let sum = Float::with_val_round(prec, old + d, round).0;
                self.array_set(name, key.to_lossy_string(), Value::Mpfr(sum));
            } else {
                Self::apply_array_numeric_delta(
                    &mut self.vars,
                    &self.global_readonly,
                    name,
                    &key.to_str_lossy(),
                    delta,
                );
            }
            return;
        }
        if field < 0 {
            Self::apply_array_numeric_delta(&mut self.vars, &self.global_readonly, name, "", delta);
            return;
        }
        let idx = (field - 1) as usize;
        if self.fields_dirty {
            let key = self
                .fields
                .get(idx)
                .map(|s| s.to_str_lossy())
                .unwrap_or_default();
            Self::apply_array_numeric_delta(
                &mut self.vars,
                &self.global_readonly,
                name,
                &key,
                delta,
            );
            return;
        }
        let (s, e) = match self.field_ranges.get(idx) {
            Some(&(s, e)) => (s as usize, e as usize),
            None => {
                Self::apply_array_numeric_delta(
                    &mut self.vars,
                    &self.global_readonly,
                    name,
                    "",
                    delta,
                );
                return;
            }
        };
        let key = String::from_utf8_lossy(&self.record[s..e]);
        Self::apply_array_numeric_delta(&mut self.vars, &self.global_readonly, name, &key, delta);
    }

    /// Shared body for [`array_field_add_delta`](Self::array_field_add_delta); separate from
    /// `&mut self` so callers can borrow `record` / `fields` for `key` while mutating `vars`.
    fn apply_array_numeric_delta(
        vars: &mut AwkMap<String, Value>,
        global_readonly: &Option<Arc<AwkMap<String, Value>>>,
        name: &str,
        key: &str,
        delta: f64,
    ) {
        if let Some(existing) = vars.get_mut(name) {
            match existing {
                Value::Array(a) => {
                    if let Some(v) = a.get_mut(key) {
                        let n = v.as_number() + delta;
                        *v = Value::Num(n);
                    } else {
                        a.insert(key.to_string(), Value::Num(delta));
                    }
                    return;
                }
                _ => {
                    let mut m = AwkArray::new();
                    m.insert(key.to_string(), Value::Num(delta));
                    *existing = Value::Array(m);
                    return;
                }
            }
        }
        if let Some(Value::Array(a)) = global_readonly.as_ref().and_then(|g| g.get(name)) {
            let mut copy = a.clone();
            let old = copy.get(key).map(|v| v.as_number()).unwrap_or(0.0);
            copy.insert(key.to_string(), Value::Num(old + delta));
            vars.insert(name.to_string(), Value::Array(copy));
        } else {
            let mut m = AwkArray::new();
            m.insert(key.to_string(), Value::Num(delta));
            vars.insert(name.to_string(), Value::Array(m));
        }
    }
    /// `array_delete` — see implementation for the contract.
    pub fn array_delete(&mut self, name: &str, key: Option<&str>) {
        if let Some(k) = key {
            if let Some(Value::Array(a)) = self.vars.get_mut(name) {
                a.remove(k);
            } else if let Some(Value::Array(a)) =
                self.global_readonly.as_ref().and_then(|g| g.get(name))
            {
                let mut copy = a.clone();
                copy.remove(k);
                self.vars.insert(name.to_string(), Value::Array(copy));
            }
        } else {
            // `delete arr` empties the array; it does not untype the name.
            // gawk keeps reporting `typeof(arr) == "array"` afterwards and
            // still rejects `arr = 5` as "attempt to use array `arr' in a
            // scalar context", and mawk and one-true-awk agree. Removing the
            // entry outright — what awkrs used to do — silently turned the
            // name back into a fresh untyped variable.
            match self.vars.get_mut(name) {
                Some(Value::Array(a)) => a.clear(),
                // Anything else — absent, or present but unassigned — becomes
                // an empty array. `delete a` on a fresh name is how gawk types
                // it: `BEGIN { delete a; print typeof(a) }` answers `array`.
                _ => {
                    self.vars
                        .insert(name.to_string(), Value::Array(AwkArray::new()));
                }
            }
        }
    }

    /// Keys for `for (k in arr)` / `SYMTAB` in **sorted** order. When `PROCINFO["sorted_in"]` names a
    /// **user function**, sorting requires VM context — use [`crate::vm::VmCtx::for_in_keys`];
    /// this method returns **unsorted** hash iteration order in that case.
    pub fn array_keys(&self, name: &str) -> Vec<AwkStr> {
        if name == "SYMTAB" {
            let mut keys = self.symtab_keys_reflect();
            if self.posix {
                return keys;
            }
            let mode = sorted_in_mode(self);
            if matches!(mode, SortedInMode::CustomFn(_)) {
                return keys;
            }
            let mut tmp = AwkArray::new();
            for k in &keys {
                tmp.insert_bytes(k.as_bytes(), self.symtab_elem_get(&k.to_str_lossy()));
            }
            sort_for_in_keys(&mut keys, &tmp, mode);
            return keys;
        }
        let Some(Value::Array(a)) = self.get_global_var(name) else {
            return Vec::new();
        };
        let mut keys: Vec<AwkStr> = a.keys();
        if self.posix {
            return keys;
        }
        let mode = sorted_in_mode(self);
        if matches!(mode, SortedInMode::CustomFn(_)) {
            return keys;
        }
        sort_for_in_keys(&mut keys, a, mode);
        keys
    }

    /// `key in arr` — true iff `arr` is an array that has `key` (POSIX: subscript was used).
    #[inline]
    /// [`Self::array_has`] with a byte subscript.
    pub fn array_has_bytes(&self, name: &str, key: &[u8]) -> bool {
        match self.get_global_var(name) {
            Some(Value::Array(a)) => a.contains_key_bytes(key),
            _ => false,
        }
    }

    pub fn array_has(&self, name: &str, key: &str) -> bool {
        if name == "SYMTAB" {
            return self.symtab_has_key(key);
        }
        match self.get_global_var(name) {
            Some(Value::Array(a)) => a.contains_key(key),
            _ => false,
        }
    }
    /// `split_into_array` — see implementation for the contract.
    pub fn split_into_array(&mut self, arr_name: &str, parts: &[AwkStr]) {
        self.array_delete(arr_name, None);
        // `split()` makes the target an array even when it produces no fields:
        // gawk reports `typeof(z)` as `"array"` after `split("", z)`. Without
        // this the name stays absent and reads back as `untyped`.
        self.vars
            .entry(arr_name.to_string())
            .or_insert_with(|| Value::Array(AwkArray::new()));
        for (i, p) in parts.iter().enumerate() {
            self.array_set(arr_name, format!("{}", i + 1), Value::Str(p.clone()));
        }
    }
}

/// Field-splitting for `split(s, a [, fs])` — same algorithm as [`crate::bytecode::Op::Split`].
///
/// Thin wrapper around `split_string_with_seps`; unused outside this module's
/// test suite, kept for direct splitter testing without going through `Op::Split`.
#[allow(dead_code)]
pub fn split_string_by_field_separator(s: &[u8], fs: &str, ignore_case: bool) -> Vec<AwkStr> {
    split_string_with_seps(s, fs, ignore_case).0
}

/// Field-splitting for `split(s, a, fs, seps)` — returns both the fields and the
/// separator strings between consecutive fields (gawk 4-arg extension).
/// `seps.len() == fields.len().saturating_sub(1)` on a non-empty record.
pub fn split_string_with_seps(s: &[u8], fs: &str, ignore_case: bool) -> (Vec<AwkStr>, Vec<AwkStr>) {
    split_string_impl(s, fs, ignore_case, false)
}

/// `split(s, a, /re/ [, seps])` — the separator came from a **regex literal**,
/// so the FS shorthands never apply: `/ /` splits on one literal space (not on
/// runs of whitespace with leading blanks stripped), and `/./` is the
/// any-character regex (not a literal dot). gawk, mawk and one-true-awk all
/// take the regex path for a literal; awkrs used to stringify it and re-enter
/// the FS rules, so `split("  a  b  ", A, / /)` answered 2 instead of 7.
pub fn split_string_with_seps_regex(
    s: &[u8],
    re: &str,
    ignore_case: bool,
) -> (Vec<AwkStr>, Vec<AwkStr>) {
    split_string_impl(s, re, ignore_case, true)
}

fn split_string_impl(
    s: &[u8],
    fs: &str,
    ignore_case: bool,
    fs_is_regex: bool,
) -> (Vec<AwkStr>, Vec<AwkStr>) {
    if s.is_empty() {
        return (Vec::new(), Vec::new());
    }
    // An empty separator splits into characters whether it was written `""` or
    // `//`: gawk, mawk and one-true-awk all return 3 for
    // `split("abc", a, //)`. Only the `" "` and single-character shorthands are
    // string-FS rules that a regex literal must bypass.
    if fs.is_empty() {
        // Empty FS: each character becomes a field; separators between them are
        // empty. A byte that does not begin a valid UTF-8 character is one
        // character, the same rule the record splitter uses.
        let mut parts: Vec<AwkStr> = Vec::new();
        let mut i = 0usize;
        while i < s.len() {
            let n = utf8_char_len(&s[i..]);
            parts.push(AwkStr::from(&s[i..i + n]));
            i += n;
        }
        let seps = vec![AwkStr::new(); parts.len().saturating_sub(1)];
        return (parts, seps);
    }
    if !fs_is_regex && fs == " " {
        // Default whitespace: leading whitespace is stripped (no leading empty field).
        let mut parts: Vec<AwkStr> = Vec::new();
        let mut seps: Vec<AwkStr> = Vec::new();
        let bytes = s;
        let mut i = 0usize;
        while i < bytes.len() && (bytes[i] == b' ' || bytes[i] == b'\t' || bytes[i] == b'\n') {
            i += 1;
        }
        while i < bytes.len() {
            let start = i;
            while i < bytes.len() && bytes[i] != b' ' && bytes[i] != b'\t' && bytes[i] != b'\n' {
                i += 1;
            }
            parts.push(AwkStr::from(&s[start..i]));
            let ws_start = i;
            while i < bytes.len() && (bytes[i] == b' ' || bytes[i] == b'\t' || bytes[i] == b'\n') {
                i += 1;
            }
            if i < bytes.len() {
                seps.push(AwkStr::from(&s[ws_start..i]));
            }
        }
        return (parts, seps);
    }
    // Single-char FS — awk POSIX semantics treat it as a literal character,
    // never a regex metachar. gawk additionally documents that **IGNORECASE
    // does not apply** to a single-char string FS (only multi-char regex FS
    // honors it), so the literal split is always correct here regardless of
    // `ignore_case`.
    if !fs_is_regex && fs.chars().count() == 1 {
        let parts: Vec<AwkStr> = byte_split(s, fs.as_bytes())
            .into_iter()
            .map(AwkStr::from)
            .collect();
        let seps = vec![AwkStr::from(fs); parts.len().saturating_sub(1)];
        return (parts, seps);
    }
    // Regex FS (or multi-char / case-insensitive): use the regex engine and
    // capture each match for the seps array. The engine comes from the memo —
    // `split()` is routinely called once per record, and compiling the
    // separator per call is what made `{ n += split($0, a, "[ ,]+") }` cost
    // 3.18 s of CPU over 300 000 records where mawk needs 0.04 s.
    with_split_regex(fs, ignore_case, |re| {
        let Some(re) = re else {
            let parts: Vec<AwkStr> = byte_split(s, fs.as_bytes())
                .into_iter()
                .map(AwkStr::from)
                .collect();
            let seps = vec![AwkStr::from(fs); parts.len().saturating_sub(1)];
            return (parts, seps);
        };
        split_on_regex_bytes(s, re)
    })
}

/// The separator-capturing split, once the engine is in hand.
///
/// The pieces are cut from the subject's own bytes, so an element of the target
/// array holds exactly what the record held there.
fn split_on_regex_bytes(hay: &[u8], re: &BytesRegex) -> (Vec<AwkStr>, Vec<AwkStr>) {
    let mut parts: Vec<AwkStr> = Vec::new();
    let mut seps: Vec<AwkStr> = Vec::new();
    let mut last = 0usize;
    for m in re.find_iter(hay) {
        // gawk parity: zero-width matches are ignored during split. Without
        // this, `split("abc", a, /x*/)` would emit one split between every
        // character because `/x*/` matches the empty string everywhere; gawk
        // returns 1 field ("abc"). (Regexes that match `""` AND a real
        // substring — e.g. `/a*/` on "aaab" — still emit splits at the
        // non-empty matches because those have `m.start() < m.end()`.)
        if m.start() == m.end() {
            continue;
        }
        parts.push(AwkStr::from(&hay[last..m.start()]));
        seps.push(AwkStr::from(m.as_bytes()));
        last = m.end();
    }
    parts.push(AwkStr::from(&hay[last..]));
    (parts, seps)
}

fn shutdown_coproc(mut h: CoprocHandle) -> Result<()> {
    h.stdin.flush().map_err(Error::Io)?;
    drop(h.stdin);
    let mut buf = String::new();
    loop {
        buf.clear();
        let n = h.stdout.read_line(&mut buf).map_err(Error::Io)?;
        if n == 0 {
            break;
        }
    }
    drop(h.stdout);
    let _ = h.child.wait();
    Ok(())
}

impl Clone for Runtime {
    fn clone(&self) -> Self {
        Self {
            vars: self.vars.clone(),
            global_readonly: self.global_readonly.clone(),
            fields: self.fields.clone(),
            field_ranges: self.field_ranges.clone(),
            fields_dirty: self.fields_dirty,
            fields_pending_split: self.fields_pending_split,
            cached_fs: self.cached_fs.clone(),
            record: self.record.clone(),
            record_assigned: self.record_assigned,
            record_strnum: self.record_strnum,
            field_strnum: self.field_strnum.clone(),
            line_buf: Vec::new(),
            read_leftover: Vec::new(),
            nr: self.nr,
            fnr: self.fnr,
            filename: self.filename.clone(),
            exit_pending: self.exit_pending,
            exit_code: self.exit_code,
            input_reader: None,
            primary_input_done: false,
            inet_tcp_read: HashMap::new(),
            inet_tcp_write: HashMap::new(),
            inet_udp: HashMap::new(),
            gettext_dir: self.gettext_dir.clone(),
            bignum: self.bignum,
            read_timeout_env: Cell::new(None),
            fs_regex: None,
            file_handles: HashMap::new(),
            getline_leftover: HashMap::new(),
            dir_read: HashMap::new(),
            output_handles: HashMap::new(),
            pipe_stdin: HashMap::new(),
            pipe_children: HashMap::new(),
            pipe_stdout: HashMap::new(),
            pipe_input_children: HashMap::new(),
            coproc_handles: HashMap::new(),
            rand_seed: self.rand_seed,
            numeric_decimal: self.numeric_decimal,
            numeric_thousands_sep: self.numeric_thousands_sep,
            slots: self.slots.clone(),
            slot_touched: self.slot_touched.clone(),
            regex_cache_cs: self.regex_cache_cs.clone(),
            regex_cache_ci: self.regex_cache_ci.clone(),
            memmem_finder_cache: self.memmem_finder_cache.clone(),
            print_buf: Vec::new(),
            ofs_bytes: self.ofs_bytes.clone(),
            ors_bytes: self.ors_bytes.clone(),
            vm_stack: Vec::with_capacity(64),
            csv_mode: self.csv_mode,
            rs_pattern_for_regex: self.rs_pattern_for_regex.clone(),
            rs_regex_bytes: self.rs_regex_bytes.clone(),
            sandbox: self.sandbox,
            characters_as_bytes: self.characters_as_bytes,
            posix: self.posix,
            traditional: self.traditional,
            jit_enabled: self.jit_enabled,
            // Parallel record worker — don't share cache; rebuild lazily per
            // worker (each has its own thread-local fusevm state too).
            fuse_chunk_cache: HashMap::new(),
            fuse_last_chunk_key: (0, false),
            fuse_last_chunk_value: None,
            fuse_prefix_chunk_cache: HashMap::new(),
            fuse_vm_pool: fusevm::VMPool::new(),
            gettext_catalogs: self.gettext_catalogs.clone(),
            symtab_slot_map: self.symtab_slot_map.clone(),
            decimal_lits: Vec::new(),
            // The debugger lives only on the main thread's runtime; parallel
            // worker clones never debug.
            debugger: None,
            debug_call_stack: Vec::new(),
            cur_line: 0,
            profile_record_hits: Vec::new(),
            sorted_in_warned: Cell::new(self.sorted_in_warned.get()),
            errno_code: self.errno_code,
            // Advice set carries into parallel workers (cheap Arc clones); the
            // live AOP call stack is per-execution, so start each clone empty.
            intercepts: self.intercepts.clone(),
            intercept_call_stack: Vec::new(),
            #[cfg(unix)]
            primary_input_poll_fd: self.primary_input_poll_fd,
        }
    }
}

impl Drop for Runtime {
    fn drop(&mut self) {
        for (_, h) in self.coproc_handles.drain() {
            let _ = shutdown_coproc(h);
        }
        for (_, mut w) in self.output_handles.drain() {
            let _ = w.flush();
        }
        for (_, mut w) in self.pipe_stdin.drain() {
            let _ = w.flush();
        }
        for (_, mut ch) in self.pipe_children.drain() {
            let _ = ch.wait();
        }
        self.pipe_stdout.clear();
        for (_, mut ch) in self.pipe_input_children.drain() {
            let _ = ch.wait();
        }
    }
}

#[cfg(test)]
mod regex_translator_tests {
    use super::translate_awk_re_to_rust;

    #[test]
    fn passes_through_plain_patterns_unchanged() {
        assert_eq!(translate_awk_re_to_rust("abc"), "abc");
        assert_eq!(translate_awk_re_to_rust("[a-z]+"), "[a-z]+");
        // (`\d\w\s` is no longer pass-through — see `strips_backslash_from_unsupported_class_escapes`)
    }

    /// `\N` is an **octal escape**, not a backreference and not a literal
    /// backslash-digit. These three tests used to pin the literal reading, which
    /// no reference awk produces — verified against all three under `LC_ALL=C`:
    ///
    /// ```text
    /// BEGIN { print ("aa" ~ /(.)\1/), ("a\\1" ~ /(.)\1/), ("a\001" ~ /(.)\1/),
    ///               ("a\002b" ~ /a\2b/), ("\011" ~ /\9/), ("9" ~ /\9/) }
    /// gawk 5.4.1        → 0 0 1 1 0 1
    /// one-true-awk      → 0 0 1 1 0 1
    /// mawk 1.3.4        → 0 0 1 1 0 1
    /// ```
    ///
    /// So `\1` matches the byte 0x01 (not `aa`, and not the two characters
    /// `\1`), and `\9` — `9` is not an octal digit — is the plain digit.
    #[test]
    fn numeric_escape_is_octal_not_a_backreference() {
        assert_eq!(translate_awk_re_to_rust(r"(.)\1"), r"(.)\x{1}");
        assert_eq!(translate_awk_re_to_rust(r"a\2b"), r"a\x{2}b");
        assert_eq!(translate_awk_re_to_rust(r"\9"), "9");
        // Up to three octal digits, greedily: `\141` is `a`, and a fourth digit
        // is an ordinary character after it.
        assert_eq!(translate_awk_re_to_rust(r"\141"), r"\x{61}");
        assert_eq!(translate_awk_re_to_rust(r"\1411"), r"\x{61}1");
    }

    #[test]
    fn handles_multiple_octal_escapes_in_one_pattern() {
        assert_eq!(translate_awk_re_to_rust(r"(.)(.)\1\2"), r"(.)(.)\x{1}\x{2}");
    }

    /// A bracket expression is not an exception: `("\001" ~ /[\1\2]/)` and
    /// `("\002" ~ /[\1\2]/)` are both 1 in gawk, mawk and one-true-awk, while
    /// `("1" ~ /[\1\2]/)` is 0 — so the digits are octal codes there too, not
    /// literal members of the set. Left untranslated, Rust's parser rejected the
    /// whole pattern as an unsupported backreference.
    #[test]
    fn octal_escape_is_recognised_inside_a_character_class() {
        assert_eq!(translate_awk_re_to_rust(r"[\1\2]"), r"[\x{1}\x{2}]");
        assert_eq!(translate_awk_re_to_rust(r"[\101]"), r"[\x{41}]");
    }

    #[test]
    fn strips_backslash_from_d_class_escape() {
        // gawk supports `\w`/`\W`/`\s`/`\S` as char classes but NOT `\d`/`\D`.
        // It emits a warning for `\d` and treats it as literal `d`. Rust regex
        // would interpret `\d` as digit class — strip the `\` to make literal.
        assert_eq!(translate_awk_re_to_rust(r"\d+"), "d+");
        assert_eq!(translate_awk_re_to_rust(r"\D+"), "D+");
        assert_eq!(translate_awk_re_to_rust(r"\d\D"), "dD");
    }

    #[test]
    fn preserves_supported_class_escapes() {
        // `\w`/`\W`/`\s`/`\S` are gawk extensions and match Rust regex semantics —
        // pass through. POSIX escapes (`\.`, `\(`, `\n`, etc.) too.
        assert_eq!(translate_awk_re_to_rust(r"\w+"), r"\w+");
        assert_eq!(translate_awk_re_to_rust(r"\W"), r"\W");
        assert_eq!(translate_awk_re_to_rust(r"\s"), r"\s");
        assert_eq!(translate_awk_re_to_rust(r"\S+"), r"\S+");
        assert_eq!(translate_awk_re_to_rust(r"\."), r"\.");
        assert_eq!(translate_awk_re_to_rust(r"\("), r"\(");
        assert_eq!(translate_awk_re_to_rust(r"\n\t\r"), r"\n\t\r");
        assert_eq!(translate_awk_re_to_rust(r"\b"), r"\b");
    }

    #[test]
    fn octal_escape_compiles_after_translation() {
        // End-to-end: a pattern Rust's parser rejects outright now compiles,
        // and matches what the three references match — the byte 0x01, not a
        // repeated character and not the two characters `\` and `1`.
        use regex::Regex;
        let translated = translate_awk_re_to_rust(r"(.)\1");
        let re = Regex::new(&translated).expect("should compile after translation");
        assert!(
            !re.is_match("aa"),
            "repeated-char should NOT match (no real backref)"
        );
        assert!(
            !re.is_match("a\\1"),
            "the two characters `\\` `1` are not what `\\1` means"
        );
        assert!(re.is_match("a\u{1}"), "octal 1 SHOULD match");
    }
}

#[cfg(test)]
mod value_tests {
    use super::Value;

    #[test]
    fn value_as_number_from_int_string() {
        assert_eq!(Value::Str("42".into()).as_number(), 42.0);
    }

    #[test]
    fn value_as_number_regexp_uses_pattern_text_as_numeric_string() {
        assert_eq!(Value::Regexp("3.5".into()).as_number(), 3.5);
        assert_eq!(Value::Regexp("notnum".into()).as_number(), 0.0);
    }

    #[test]
    fn value_as_number_empty_string_zero() {
        assert_eq!(Value::Str("".into()).as_number(), 0.0);
    }

    #[test]
    fn value_truthy_numeric_string_zero() {
        assert!(!Value::Str("0".into()).truthy());
    }

    #[test]
    fn value_truthy_non_numeric_string() {
        assert!(Value::Str("hello".into()).truthy());
    }

    #[test]
    fn value_truthy_cond_rejects_whole_array() {
        let mut m = super::AwkArray::new();
        m.insert("k".into(), Value::Num(1.0));
        let v = Value::Array(m);
        assert!(v.truthy_cond().is_err());
        assert!(v.truthy());
    }

    #[test]
    fn value_is_numeric_str_detects_decimal() {
        assert!(Value::Str("3.14".into()).is_numeric_str());
        assert!(!Value::Str("x".into()).is_numeric_str());
    }

    #[test]
    fn str_lit_not_numeric_string_for_relops() {
        assert!(!Value::StrLit("10".into()).is_numeric_str());
        assert!(Value::Uninit.is_numeric_str());
    }

    #[test]
    fn as_number_longest_numeric_prefix() {
        assert_eq!(Value::StrLit("42trailing".into()).as_number(), 42.0);
    }

    #[test]
    fn split_empty_source_zero_fields() {
        let v = super::split_string_by_field_separator(b"", ",", false);
        assert!(v.is_empty());
    }

    #[test]
    fn set_nf_truncates_and_rebuilds_record() {
        let mut rt = super::Runtime::new();
        rt.set_field_sep_split(" ", b"a b c d e");
        rt.ensure_fields_split();
        rt.set_nf(3).unwrap();
        assert_eq!(rt.record, "a b c");
        assert_eq!(rt.nf(), 3);
    }

    #[test]
    fn set_record_str_resplits_nf() {
        let mut rt = super::Runtime::new();
        rt.vars.insert("FS".into(), Value::Str(" ".into()));
        rt.set_field_sep_split(" ", b"a b c");
        rt.ensure_fields_split();
        rt.set_record_str("x y");
        assert_eq!(rt.nf(), 2);
    }

    #[test]
    fn value_append_to_string_concat() {
        let mut buf = String::from("a");
        Value::Str("b".into()).append_to_string(&mut buf);
        Value::Num(7.0).append_to_string(&mut buf);
        assert_eq!(buf, "ab7");
    }

    #[test]
    fn value_into_string_from_num_integer_form() {
        assert_eq!(Value::Num(12.0).into_string(), "12");
    }

    #[test]
    fn value_write_to_buf_str_and_num() {
        let mut v = Vec::new();
        Value::Str("ok".into()).write_to(&mut v);
        Value::Num(5.0).write_to(&mut v);
        assert_eq!(v, b"ok5");
    }

    #[test]
    fn value_truthy_num_zero() {
        assert!(!Value::Num(0.0).truthy());
    }

    #[test]
    fn value_truthy_num_nonzero() {
        assert!(Value::Num(-3.0).truthy());
    }

    #[test]
    fn value_empty_array_not_truthy() {
        let m = super::AwkArray::new();
        assert!(!Value::Array(m).truthy());
    }

    #[test]
    fn value_as_number_negative_float_string() {
        assert_eq!(Value::Str("-2.5".into()).as_number(), -2.5);
    }

    #[test]
    fn value_as_number_scientific_notation_string() {
        assert_eq!(Value::Str("1e2".into()).as_number(), 100.0);
    }

    #[test]
    fn value_as_number_hex_only_when_non_decimal_parse_mode() {
        super::set_numeric_parse_mode(false);
        assert_eq!(Value::Str("0x10".into()).as_number(), 0.0);
        super::set_numeric_parse_mode(true);
        assert_eq!(Value::Str("0x10".into()).as_number(), 16.0);
        super::set_numeric_parse_mode(false);
    }

    #[test]
    fn value_as_number_leading_zero_octal_only_in_non_decimal_mode() {
        super::set_numeric_parse_mode(false);
        assert_eq!(Value::Str("010".into()).as_number(), 10.0);
        super::set_numeric_parse_mode(true);
        assert_eq!(Value::Str("010".into()).as_number(), 8.0);
        super::set_numeric_parse_mode(false);
    }

    #[test]
    fn value_into_string_float_fraction() {
        let s = Value::Num(0.25).into_string();
        assert!(s.contains('2') && s.contains('5'), "{s}");
    }

    #[test]
    fn csv_mode_quoted_comma_three_fields() {
        let mut rt = super::Runtime::new();
        rt.csv_mode = true;
        rt.set_field_sep_split(",", r#"a,"b,c",d"#.as_bytes());
        rt.ensure_fields_split();
        assert_eq!(rt.nf(), 3);
        assert_eq!(rt.field(1).unwrap().as_str(), "a");
        assert_eq!(rt.field(2).unwrap().as_str(), "b,c");
        assert_eq!(rt.field(3).unwrap().as_str(), "d");
    }

    #[test]
    fn csv_mode_escape_double_quote_in_field() {
        let mut rt = super::Runtime::new();
        rt.csv_mode = true;
        rt.set_field_sep_split(",", b"\"a\"\"b\"");
        rt.ensure_fields_split();
        assert_eq!(rt.field(1).unwrap().as_str(), "a\"b");
    }

    #[test]
    fn csv_mode_trailing_comma_empty_field() {
        let mut rt = super::Runtime::new();
        rt.csv_mode = true;
        rt.set_field_sep_split(",", b"a,");
        rt.ensure_fields_split();
        assert_eq!(rt.nf(), 2);
        assert_eq!(rt.field(1).unwrap().as_str(), "a");
        assert_eq!(rt.field(2).unwrap().as_str(), "");
    }

    // ── More CSV (RFC 4180) edge cases ───────────────────────────────────────

    #[test]
    fn csv_mode_leading_empty_field() {
        let mut rt = super::Runtime::new();
        rt.csv_mode = true;
        rt.set_field_sep_split(",", b",a,b");
        rt.ensure_fields_split();
        assert_eq!(rt.nf(), 3);
        assert_eq!(rt.field(1).unwrap().as_str(), "");
        assert_eq!(rt.field(2).unwrap().as_str(), "a");
        assert_eq!(rt.field(3).unwrap().as_str(), "b");
    }

    #[test]
    fn csv_mode_all_empty_fields() {
        let mut rt = super::Runtime::new();
        rt.csv_mode = true;
        rt.set_field_sep_split(",", b",,,");
        rt.ensure_fields_split();
        // 3 commas = 4 fields, all empty
        assert_eq!(rt.nf(), 4);
        for i in 1..=4 {
            assert_eq!(rt.field(i).unwrap().as_str(), "");
        }
    }

    #[test]
    fn csv_mode_mixed_quoted_and_unquoted() {
        let mut rt = super::Runtime::new();
        rt.csv_mode = true;
        rt.set_field_sep_split(",", r#"a,"b,c",d,"e"#.as_bytes());
        rt.ensure_fields_split();
        // "e is unterminated; awkrs should accept (RFC 4180 strict would reject,
        // gawk is lenient). Pin actual behavior: 4 fields including the
        // unterminated one.
        assert!(rt.nf() >= 3, "got NF={}", rt.nf());
        assert_eq!(rt.field(1).unwrap().as_str(), "a");
        assert_eq!(rt.field(2).unwrap().as_str(), "b,c");
        assert_eq!(rt.field(3).unwrap().as_str(), "d");
    }

    #[test]
    fn csv_mode_double_quote_escape_in_middle() {
        let mut rt = super::Runtime::new();
        rt.csv_mode = true;
        rt.set_field_sep_split(",", r#""he said ""hi""","next""#.as_bytes());
        rt.ensure_fields_split();
        assert_eq!(rt.nf(), 2);
        assert_eq!(rt.field(1).unwrap().as_str(), r#"he said "hi""#);
    }

    #[test]
    fn csv_mode_single_comma_two_empty_fields() {
        // `,` is one separator → 2 empty fields. Previously broken (returned 1).
        let mut rt = super::Runtime::new();
        rt.csv_mode = true;
        rt.set_field_sep_split(",", b",");
        rt.ensure_fields_split();
        assert_eq!(rt.nf(), 2);
        assert_eq!(rt.field(1).unwrap().as_str(), "");
        assert_eq!(rt.field(2).unwrap().as_str(), "");
    }

    #[test]
    fn csv_mode_leading_comma_with_trailing_value() {
        // `,a,b` → 3 fields. Previously broken — leading-comma path was a
        // separate branch that didn't account for the implicit empty start.
        let mut rt = super::Runtime::new();
        rt.csv_mode = true;
        rt.set_field_sep_split(",", b",a,b");
        rt.ensure_fields_split();
        assert_eq!(rt.nf(), 3);
        assert_eq!(rt.field(1).unwrap().as_str(), "");
        assert_eq!(rt.field(2).unwrap().as_str(), "a");
        assert_eq!(rt.field(3).unwrap().as_str(), "b");
    }

    #[test]
    fn csv_mode_empty_record_zero_fields() {
        let mut rt = super::Runtime::new();
        rt.csv_mode = true;
        rt.set_field_sep_split(",", b"");
        rt.ensure_fields_split();
        assert_eq!(rt.nf(), 0);
    }

    #[test]
    fn csv_mode_disabled_treats_comma_normally() {
        // Without csv_mode the quotes are part of the field text.
        let mut rt = super::Runtime::new();
        rt.csv_mode = false;
        rt.set_field_sep_split(",", r#"a,"b,c",d"#.as_bytes());
        rt.ensure_fields_split();
        // Without CSV mode: comma-only splitting → 4 fields.
        assert_eq!(rt.nf(), 4);
        assert_eq!(rt.field(1).unwrap().as_str(), "a");
        assert_eq!(rt.field(2).unwrap().as_str(), "\"b");
        assert_eq!(rt.field(3).unwrap().as_str(), "c\"");
        assert_eq!(rt.field(4).unwrap().as_str(), "d");
    }

    #[test]
    fn ignore_case_false_when_unset_or_zero() {
        let rt = super::Runtime::new();
        assert!(!rt.ignore_case_flag());
        let mut rt0 = super::Runtime::new();
        rt0.vars.insert("IGNORECASE".into(), Value::Num(0.0));
        assert!(!rt0.ignore_case_flag());
    }

    #[test]
    fn ignore_case_true_for_numeric_one() {
        let mut rt = super::Runtime::new();
        rt.vars.insert("IGNORECASE".into(), Value::Num(1.0));
        assert!(rt.ignore_case_flag());
    }

    #[test]
    fn ignore_case_true_for_non_numeric_string() {
        let mut rt = super::Runtime::new();
        rt.vars
            .insert("IGNORECASE".into(), Value::Str("yes".into()));
        assert!(rt.ignore_case_flag());
    }

    #[test]
    fn num_to_string_convfmt_uses_convfmt_global() {
        let mut rt = super::Runtime::new();
        rt.vars.insert("CONVFMT".into(), Value::Str("%.0f".into()));
        assert_eq!(rt.num_to_string_convfmt(3.2), "3");
    }

    #[test]
    fn num_to_string_ofmt_uses_ofmt_global() {
        let mut rt = super::Runtime::new();
        rt.vars.insert("OFMT".into(), Value::Str("%.2f".into()));
        assert_eq!(rt.num_to_string_ofmt(1.2), "1.20");
    }
}

#[cfg(test)]
mod longest_prefix_and_sorted_in_tests {
    use super::{
        longest_f64_prefix, sort_for_in_keys, sorted_in_mode, Runtime, SortedInMode, Value,
    };

    #[test]
    fn longest_f64_prefix_empty_none() {
        assert_eq!(longest_f64_prefix(""), None);
    }

    #[test]
    fn longest_f64_prefix_multibyte_first_char_returns_none() {
        // Regression: vigenere.awk (examples/) round-trips bytes 0..255 through
        // `sprintf("%c", i)` and uses the result as an array key — strings like
        // "ÿ" reach `longest_f64_prefix`. The old loop sliced bytewise at
        // `&s[..end]`, panicking with "end byte index 1 is not a char boundary;
        // it is inside 'ÿ' (bytes 0..2 of string)". The fix bounds the loop to
        // the leading ASCII run (numeric prefixes are always ASCII).
        assert_eq!(longest_f64_prefix("\u{ff}"), None);
        assert_eq!(longest_f64_prefix("é"), None);
        assert_eq!(longest_f64_prefix("é42"), None); // leading non-ASCII → no prefix
    }

    #[test]
    fn longest_f64_prefix_ascii_then_multibyte() {
        // Leading ASCII digits, then a multibyte char — should pick the ASCII run.
        assert_eq!(longest_f64_prefix("42é"), Some("42"));
        assert_eq!(longest_f64_prefix("1.5ÿ"), Some("1.5"));
    }

    #[test]
    fn longest_f64_prefix_scientific_ok() {
        assert_eq!(longest_f64_prefix("1e2"), Some("1e2"));
    }

    #[test]
    fn longest_f64_prefix_non_monotonic_stops_at_last_valid() {
        assert_eq!(longest_f64_prefix("1ex"), Some("1"));
    }

    #[test]
    fn longest_f64_prefix_trailing_non_numeric() {
        assert_eq!(longest_f64_prefix("3.5abc"), Some("3.5"));
    }

    #[test]
    fn sorted_in_posix_forces_unsorted() {
        let mut rt = Runtime::new();
        rt.posix = true;
        let mut pi = crate::runtime::AwkArray::new();
        pi.insert("sorted_in".into(), Value::Str("@ind_str_desc".into()));
        rt.vars.insert("PROCINFO".into(), Value::Array(pi));
        assert_eq!(sorted_in_mode(&rt), SortedInMode::Unsorted);
    }

    #[test]
    fn sorted_in_reads_at_tokens_with_trim() {
        let mut rt = Runtime::new();
        let mut pi = crate::runtime::AwkArray::new();
        pi.insert("sorted_in".into(), Value::Str("  @val_num_asc  ".into()));
        rt.vars.insert("PROCINFO".into(), Value::Array(pi));
        assert_eq!(sorted_in_mode(&rt), SortedInMode::ValNumAsc);
    }

    #[test]
    fn sorted_in_user_function_name() {
        let mut rt = Runtime::new();
        let mut pi = crate::runtime::AwkArray::new();
        pi.insert("sorted_in".into(), Value::Str("my_cmp".into()));
        rt.vars.insert("PROCINFO".into(), Value::Array(pi));
        assert_eq!(sorted_in_mode(&rt), SortedInMode::CustomFn("my_cmp".into()));
    }

    #[test]
    fn sorted_in_missing_or_empty_is_unsorted() {
        let rt = Runtime::new();
        assert_eq!(sorted_in_mode(&rt), SortedInMode::Unsorted);

        let mut rt2 = Runtime::new();
        let mut pi = crate::runtime::AwkArray::new();
        pi.insert("sorted_in".into(), Value::Str("  ".into()));
        rt2.vars.insert("PROCINFO".into(), Value::Array(pi));
        assert_eq!(sorted_in_mode(&rt2), SortedInMode::Unsorted);

        let mut rt3 = Runtime::new();
        let pi = super::AwkArray::new();
        rt3.vars.insert("PROCINFO".into(), Value::Array(pi));
        assert_eq!(sorted_in_mode(&rt3), SortedInMode::Unsorted);
    }

    #[test]
    fn sort_for_in_ind_num_asc_numeric_not_lexicographic() {
        let mut keys = vec!["10".into(), "2".into(), "1".into()];
        let arr = super::AwkArray::new();
        sort_for_in_keys(&mut keys, &arr, SortedInMode::IndNumAsc);
        assert_eq!(keys, vec!["1", "2", "10"]);
    }

    #[test]
    fn sort_for_in_val_num_desc_by_values() {
        let mut keys = vec!["a".into(), "b".into()];
        let mut arr = super::AwkArray::new();
        arr.insert("a".into(), Value::Num(1.0));
        arr.insert("b".into(), Value::Num(10.0));
        sort_for_in_keys(&mut keys, &arr, SortedInMode::ValNumDesc);
        assert_eq!(keys, vec!["b", "a"]);
    }

    #[test]
    fn sort_for_in_val_str_asc_by_string_values() {
        let mut keys = vec!["a".into(), "b".into()];
        let mut arr = super::AwkArray::new();
        arr.insert("a".into(), Value::Str("z".into()));
        arr.insert("b".into(), Value::Str("a".into()));
        sort_for_in_keys(&mut keys, &arr, SortedInMode::ValStrAsc);
        assert_eq!(keys, vec!["b", "a"]);
    }

    #[test]
    fn sorted_in_mode_ind_str_asc_token() {
        let mut rt = Runtime::new();
        let mut pi = crate::runtime::AwkArray::new();
        pi.insert("sorted_in".into(), Value::Str("@ind_str_asc".into()));
        rt.vars.insert("PROCINFO".into(), Value::Array(pi));
        assert_eq!(sorted_in_mode(&rt), SortedInMode::IndStrAsc);
    }

    #[test]
    fn sorted_in_mode_val_type_asc_token() {
        let mut rt = Runtime::new();
        let mut pi = crate::runtime::AwkArray::new();
        pi.insert("sorted_in".into(), Value::Str("@val_type_asc".into()));
        rt.vars.insert("PROCINFO".into(), Value::Array(pi));
        assert_eq!(sorted_in_mode(&rt), SortedInMode::ValTypeAsc);
    }

    #[test]
    fn sorted_in_mode_val_type_desc_token() {
        let mut rt = Runtime::new();
        let mut pi = crate::runtime::AwkArray::new();
        pi.insert("sorted_in".into(), Value::Str("@val_type_desc".into()));
        rt.vars.insert("PROCINFO".into(), Value::Array(pi));
        assert_eq!(sorted_in_mode(&rt), SortedInMode::ValTypeDesc);
    }

    #[test]
    fn sort_for_in_val_type_asc_orders_type_rank_then_value_string() {
        let mut keys = vec!["str".into(), "num".into(), "absent".into()];
        let mut arr = super::AwkArray::new();
        arr.insert("num".into(), Value::Num(1.0));
        arr.insert("str".into(), Value::Str("z".into()));
        sort_for_in_keys(&mut keys, &arr, SortedInMode::ValTypeAsc);
        assert_eq!(keys, vec!["absent", "num", "str"]);
    }

    #[test]
    fn sort_for_in_val_type_desc_reverses_type_rank() {
        let mut keys = vec!["absent".into(), "num".into(), "str".into()];
        let mut arr = super::AwkArray::new();
        arr.insert("num".into(), Value::Num(1.0));
        arr.insert("str".into(), Value::Str("z".into()));
        sort_for_in_keys(&mut keys, &arr, SortedInMode::ValTypeDesc);
        assert_eq!(keys, vec!["str", "num", "absent"]);
    }

    #[test]
    fn sort_for_in_ind_str_desc() {
        let mut keys = vec!["a".into(), "c".into(), "b".into()];
        let arr = super::AwkArray::new();
        sort_for_in_keys(&mut keys, &arr, SortedInMode::IndStrDesc);
        assert_eq!(keys, vec!["c", "b", "a"]);
    }

    #[test]
    fn sort_for_in_unsorted_no_op() {
        let mut keys = vec!["z".into(), "a".into()];
        let arr = super::AwkArray::new();
        sort_for_in_keys(&mut keys, &arr, SortedInMode::Unsorted);
        assert_eq!(keys, vec!["z", "a"]);
    }

    #[test]
    fn sort_for_in_custom_fn_no_op_in_runtime_helper() {
        let mut keys = vec!["b".into(), "a".into()];
        let arr = super::AwkArray::new();
        sort_for_in_keys(&mut keys, &arr, SortedInMode::CustomFn("cmp".into()));
        assert_eq!(keys, vec!["b", "a"]);
    }
}

#[cfg(test)]
mod init_argv_tests {
    use super::{Runtime, Value};
    use std::path::PathBuf;

    #[test]
    fn init_argv_sets_argc_and_numeric_string_keys() {
        let mut rt = Runtime::new();
        rt.init_argv(&[
            PathBuf::from("/data/one.txt"),
            PathBuf::from("/data/two.txt"),
        ]);
        assert_eq!(rt.vars.get("ARGC").unwrap().as_number(), 3.0);
        let Value::Array(argv) = rt.vars.get("ARGV").expect("ARGV") else {
            panic!("ARGV not array");
        };
        assert!(!argv.get("0").unwrap().as_str().is_empty());
        assert_eq!(argv.get("1").unwrap().as_str(), "/data/one.txt");
        assert_eq!(argv.get("2").unwrap().as_str(), "/data/two.txt");
    }

    #[test]
    fn init_argv_empty_file_list_leaves_only_program_name() {
        let mut rt = Runtime::new();
        rt.init_argv(&[]);
        assert_eq!(rt.vars.get("ARGC").unwrap().as_number(), 1.0);
        let Value::Array(argv) = rt.vars.get("ARGV").expect("ARGV") else {
            panic!("ARGV not array");
        };
        assert_eq!(argv.len(), 1);
        assert!(argv.get("0").is_some());
    }
}

// ── awk_binop_values: pin POSIX arithmetic-coercion semantics ────────────────
//
// Every `+= -= *= /= %= ^=` flows through here. These tests pin the contracts:
//   - string ↔ number coercion follows longest-numeric-prefix rule
//   - uninitialized values arithmetic to 0
//   - division by zero returns Err
//   - array-as-scalar is a fatal runtime error
//   - compound-only ops (Pow / Mod) return Num in non-MPFR mode
// Any of these getting "fixed" silently breaks awk programs in user-invisible
// ways, so each is a named test that has to be acknowledged.

#[cfg(test)]
mod awk_binop_values_pinning {
    use super::{awk_binop_values, Runtime, Value};
    use crate::ast::BinOp;

    fn rt() -> Runtime {
        Runtime::new()
    }

    fn binop_num(op: BinOp, a: Value, b: Value) -> f64 {
        let rt = rt();
        match awk_binop_values(op, &a, &b, false, &rt).unwrap() {
            Value::Num(n) => n,
            v => panic!("expected Num, got {v:?}"),
        }
    }

    #[test]
    fn binop_add_numbers() {
        assert_eq!(binop_num(BinOp::Add, Value::Num(2.0), Value::Num(3.0)), 5.0);
    }

    #[test]
    fn binop_sub_string_to_number_coercion() {
        // "10" - "4" coerces both via longest-numeric-prefix.
        assert_eq!(
            binop_num(BinOp::Sub, Value::Str("10".into()), Value::Str("4".into())),
            6.0
        );
    }

    #[test]
    fn binop_mul_with_alpha_suffix_coerces_to_prefix() {
        // POSIX: "2.5abc" + 0 → 2.5. Multiplication should see 2.5 on the LHS.
        // (Use 2.5 instead of 3.14 to avoid clippy::approx_constant flagging the
        // intermediate 6.28 as approximating TAU.)
        let n = binop_num(BinOp::Mul, Value::Str("2.5abc".into()), Value::Num(4.0));
        assert!((n - 10.0).abs() < 1e-9, "expected 10.0, got {n}");
    }

    #[test]
    fn binop_div_by_zero_returns_error() {
        let rt = rt();
        let err = awk_binop_values(BinOp::Div, &Value::Num(1.0), &Value::Num(0.0), false, &rt)
            .unwrap_err();
        assert!(
            format!("{err}").contains("division by zero"),
            "unexpected error: {err}"
        );
    }

    #[test]
    fn binop_mod_returns_remainder() {
        assert_eq!(
            binop_num(BinOp::Mod, Value::Num(10.0), Value::Num(3.0)),
            1.0
        );
    }

    #[test]
    fn binop_pow_returns_exponent() {
        assert_eq!(
            binop_num(BinOp::Pow, Value::Num(2.0), Value::Num(10.0)),
            1024.0
        );
    }

    #[test]
    fn binop_uninit_treated_as_zero() {
        // POSIX: uninitialized variable in arithmetic context is 0.
        assert_eq!(binop_num(BinOp::Add, Value::Uninit, Value::Num(7.0)), 7.0);
        assert_eq!(binop_num(BinOp::Mul, Value::Uninit, Value::Num(99.0)), 0.0);
    }

    #[test]
    fn binop_array_as_scalar_is_fatal() {
        // gawk-style: using an array name in scalar context is a fatal error,
        // not a silent empty-string coercion.
        let rt = rt();
        let arr = Value::Array(super::AwkArray::new());
        let err = awk_binop_values(BinOp::Add, &arr, &Value::Num(1.0), false, &rt).unwrap_err();
        let msg = format!("{err}");
        assert!(
            msg.contains("array") || msg.contains("scalar"),
            "expected array-as-scalar error, got: {msg}"
        );
    }

    #[test]
    fn binop_non_arithmetic_op_returns_error() {
        // Comparison / logical ops aren't valid compound-assignment ops; the
        // dispatch must reject them with a clear error message instead of
        // silently doing the wrong thing.
        let rt = rt();
        let err = awk_binop_values(BinOp::Eq, &Value::Num(1.0), &Value::Num(1.0), false, &rt)
            .unwrap_err();
        assert!(
            format!("{err}").contains("invalid compound assignment"),
            "expected reject message, got: {err}"
        );
    }
}

// ── parse_inet_tcp / parse_inet_udp: pin gawk /inet path grammar ─────────────
#[cfg(test)]
mod parse_inet_pinning {
    use super::{parse_inet_tcp, parse_inet_udp};

    #[test]
    fn inet_tcp_valid_three_components() {
        assert_eq!(
            parse_inet_tcp("/inet/tcp/0/example.com/443"),
            Some((0, "example.com".to_string(), 443))
        );
    }

    #[test]
    fn inet_tcp_with_explicit_local_port() {
        assert_eq!(
            parse_inet_tcp("/inet/tcp/8080/localhost/9000"),
            Some((8080, "localhost".to_string(), 9000))
        );
    }

    #[test]
    fn inet_udp_valid() {
        assert_eq!(
            parse_inet_udp("/inet/udp/0/dns.example/53"),
            Some((0, "dns.example".to_string(), 53))
        );
    }

    #[test]
    fn inet_wrong_prefix_returns_none() {
        // tcp parser must reject udp paths and vice versa.
        assert!(parse_inet_tcp("/inet/udp/0/h/53").is_none());
        assert!(parse_inet_udp("/inet/tcp/0/h/443").is_none());
        assert!(parse_inet_tcp("/inet/raw/0/h/1").is_none());
    }

    #[test]
    fn inet_missing_rport_returns_none() {
        assert!(parse_inet_tcp("/inet/tcp/0/host").is_none());
    }

    #[test]
    fn inet_extra_trailing_component_returns_none() {
        // Trailing slash + extra component must reject (no silent acceptance).
        assert!(parse_inet_tcp("/inet/tcp/0/host/443/extra").is_none());
    }

    #[test]
    fn inet_non_numeric_port_returns_none() {
        assert!(parse_inet_tcp("/inet/tcp/abc/host/443").is_none());
        assert!(parse_inet_tcp("/inet/tcp/0/host/xyz").is_none());
    }

    #[test]
    fn inet_port_out_of_u16_range_returns_none() {
        // 70000 doesn't fit in u16 — must reject, not silently truncate.
        assert!(parse_inet_tcp("/inet/tcp/0/host/70000").is_none());
    }
}

// ── awk_locale_str_cmp: pin string ordering contract ─────────────────────────
#[cfg(test)]
mod awk_locale_str_cmp_pinning {
    use super::awk_locale_str_cmp;
    use std::cmp::Ordering;

    #[test]
    fn equal_strings_compare_equal() {
        assert_eq!(awk_locale_str_cmp("abc", "abc"), Ordering::Equal);
    }

    #[test]
    fn empty_string_orders_first() {
        assert_eq!(awk_locale_str_cmp("", "a"), Ordering::Less);
        assert_eq!(awk_locale_str_cmp("a", ""), Ordering::Greater);
        assert_eq!(awk_locale_str_cmp("", ""), Ordering::Equal);
    }

    #[test]
    fn null_byte_falls_back_to_byte_compare() {
        // CString::new fails on embedded NUL — the function must fall back to
        // a.cmp(b) instead of panicking or returning Equal incorrectly.
        let with_nul = "a\0b";
        let without = "ab";
        let o = awk_locale_str_cmp(with_nul, without);
        // Should not panic; the relative ordering is implementation-defined but
        // must be one of Less/Greater (not erroneously Equal).
        assert_ne!(
            o,
            Ordering::Equal,
            "NUL-containing strings shouldn't compare equal to clean strings"
        );
    }

    #[test]
    fn value_to_number_conversions() {
        use crate::runtime::Value;
        assert_eq!(Value::Str("123".into()).as_number(), 123.0);
        assert_eq!(Value::Str("12.3".into()).as_number(), 12.3);
        assert_eq!(Value::Str("abc".into()).as_number(), 0.0);
    }

    #[test]
    fn runtime_array_operations() {
        use crate::runtime::{Runtime, Value};
        let mut rt = Runtime::new();
        rt.array_set("a", "k1".to_string(), Value::Num(42.0));
        assert!(rt.array_has("a", "k1"));
        assert!(!rt.array_has("a", "k2"));
        rt.array_delete("a", Some("k1"));
        assert!(!rt.array_has("a", "k1"));
    }

    #[test]
    fn runtime_global_var_ops() {
        use crate::runtime::{Runtime, Value};
        let mut rt = Runtime::new();
        rt.vars.insert("x".into(), Value::Num(100.0));
        assert_eq!(rt.get_global_var("x").unwrap().as_number(), 100.0);
    }
}

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

    #[test]
    fn value_truthiness() {
        assert!(!Value::Uninit.truthy());
        assert!(Value::Num(1.0).truthy());
        assert!(!Value::Num(0.0).truthy());
        assert!(Value::StrLit("0".into()).truthy()); // "0" as literal is truthy
        assert!(!Value::Str("0".into()).truthy()); // "0" as dynamic string is numeric 0 -> falsy
        assert!(!Value::Str("00".into()).truthy()); // "00" is numeric 0 -> falsy
                                                    // regex literals are truthy if non-empty
        assert!(Value::Regexp(".".into()).truthy());
        assert!(!Value::Regexp("".into()).truthy());
    }

    #[test]
    fn value_numeric_conversions() {
        assert_eq!(Value::Uninit.as_number(), 0.0);
        assert_eq!(Value::Str("  123.45  ".into()).as_number(), 123.45);
        assert_eq!(Value::Str("1e2".into()).as_number(), 100.0);
        // gawk parity: bare "inf"/"nan" coerce to 0 (no sign prefix → not a
        // numeric prefix in gawk). Signed three-letter forms are accepted.
        assert_eq!(Value::Str("inf".into()).as_number(), 0.0);
        assert_eq!(Value::Str("nan".into()).as_number(), 0.0);
        assert_eq!(Value::Str("+inf".into()).as_number(), f64::INFINITY);
        assert!(Value::Str("+nan".into()).as_number().is_nan());
        // large integers
        assert_eq!(
            Value::Str("9223372036854775807".into()).as_number(),
            9223372036854775807.0
        );
    }

    #[test]
    fn value_is_numeric_str() {
        assert!(Value::Uninit.is_numeric_str());
        assert!(Value::Num(1.0).is_numeric_str());
        assert!(Value::Str("123".into()).is_numeric_str());
        assert!(Value::Str(" -12.3e-1 ".into()).is_numeric_str());
        assert!(!Value::Str("abc".into()).is_numeric_str());
        assert!(!Value::StrLit("123".into()).is_numeric_str());
        // extreme scientific
        assert!(Value::Str("1.234567e+10".into()).is_numeric_str());
        // gawk parity: bare "inf"/"nan" are NOT numeric strings (no sign prefix
        // → not a numeric prefix in gawk). Only signed forms count.
        assert!(!Value::Str("inf".into()).is_numeric_str());
        assert!(!Value::Str("nan".into()).is_numeric_str());
        assert!(Value::Str("+inf".into()).is_numeric_str());
        assert!(Value::Str("-nan".into()).is_numeric_str());
    }

    #[test]
    fn parse_ascii_integer_logic() {
        assert_eq!(parse_ascii_integer("123"), Some(123));
        assert_eq!(parse_ascii_integer("-456"), Some(-456));
        assert_eq!(parse_ascii_integer("+789"), Some(789));
        assert_eq!(parse_ascii_integer("12.3"), None);
        assert_eq!(parse_ascii_integer("abc"), None);
    }

    #[test]
    fn longest_f64_prefix_logic() {
        assert_eq!(longest_f64_prefix("123abc"), Some("123"));
        assert_eq!(longest_f64_prefix("1.2.3"), Some("1.2"));
        assert_eq!(longest_f64_prefix("1e2e3"), Some("1e2"));
        assert_eq!(longest_f64_prefix("abc"), None);
        // scientific notation with + sign
        assert_eq!(longest_f64_prefix("1.2E+10x"), Some("1.2E+10"));
        // negative sign (handled by as_number, but prefix should include it)
        assert_eq!(longest_f64_prefix("-42.5z"), Some("-42.5"));
        // lowercase e
        assert_eq!(longest_f64_prefix("3.14e-5y"), Some("3.14e-5"));
    }

    #[test]
    fn parse_ascii_integer_edge_cases() {
        assert_eq!(parse_ascii_integer("0"), Some(0));
        assert_eq!(parse_ascii_integer("-0"), Some(0));
        assert_eq!(parse_ascii_integer("+0"), Some(0));
        assert_eq!(parse_ascii_integer("007"), Some(7));
        // Overflows/underflows? i64::MAX is large, but let's test a large one.
        assert_eq!(
            parse_ascii_integer("9223372036854775807"),
            Some(9223372036854775807)
        );
    }

    #[test]
    fn split_fields_whitespace() {
        let mut ranges = Vec::new();
        split_fields_into(
            b"  a  b   c  ",
            " ",
            &mut ranges,
            false,
            false,
            false,
            FsRegex::Unknown,
        );
        assert_eq!(ranges.len(), 3);
        assert_eq!(ranges[0], (2, 3)); // "a"
        assert_eq!(ranges[1], (5, 6)); // "b"
        assert_eq!(ranges[2], (9, 10)); // "c"
    }

    #[test]
    fn split_fields_comma() {
        let mut ranges = Vec::new();
        split_fields_into(
            b"a,b,,c",
            ",",
            &mut ranges,
            false,
            false,
            false,
            FsRegex::Unknown,
        );
        assert_eq!(ranges.len(), 4);
        assert_eq!(ranges[0], (0, 1)); // "a"
        assert_eq!(ranges[1], (2, 3)); // "b"
        assert_eq!(ranges[2], (4, 4)); // ""
        assert_eq!(ranges[3], (5, 6)); // "c"
    }

    #[test]
    fn split_fields_regex() {
        let mut ranges = Vec::new();
        split_fields_into(
            b"a1b22c",
            "[0-9]+",
            &mut ranges,
            false,
            false,
            false,
            FsRegex::Unknown,
        );
        assert_eq!(ranges.len(), 3);
        assert_eq!(ranges[0], (0, 1)); // "a"
        assert_eq!(ranges[1], (2, 3)); // "b"
        assert_eq!(ranges[2], (5, 6)); // "c"
    }

    #[test]
    fn split_csv_gawk_rfc4180() {
        let mut ranges = Vec::new();
        split_csv_gawk_fields(b"a,\"b,c\",d", &mut ranges);
        assert_eq!(ranges.len(), 3);
        assert_eq!(ranges[0], (0, 1)); // "a"
        assert_eq!(ranges[1], (3, 6)); // "b,c"
        assert_eq!(ranges[2], (8, 9)); // "d"

        split_csv_gawk_fields(b"\"\"\"\"", &mut ranges); // escaped quote
        assert_eq!(ranges.len(), 1);
        assert_eq!(ranges[0], (1, 3)); // ""
    }

    #[test]
    fn runtime_variable_overlay() {
        let mut globals = AwkMap::default();
        globals.insert("x".into(), Value::Num(1.0));
        globals.insert("y".into(), Value::Num(2.0));

        let shared = Arc::new(globals);
        let mut rt = Runtime::new();
        rt.global_readonly = Some(shared);

        rt.vars.insert("x".into(), Value::Num(10.0)); // overlay

        assert_eq!(rt.get_global_var("x").unwrap().as_number(), 10.0);
        assert_eq!(rt.get_global_var("y").unwrap().as_number(), 2.0);
        assert!(rt.get_global_var("z").is_none());
    }

    #[test]
    fn value_as_number_hex_and_octal_strings() {
        // as_number() on Value::Str does NOT automatically parse hex/octal.
        // It uses standard f64 parsing.
        assert_eq!(Value::Str("0x10".into()).as_number(), 0.0);
        assert_eq!(Value::Str("010".into()).as_number(), 10.0);
    }

    #[test]
    fn value_str_coercion_from_float() {
        let rt = Runtime::new();
        // CONVFMT is "%.6g" by default
        let v = Value::Num(1.23456789);
        assert_eq!(rt.value_to_array_key(&v), "1.23457");
    }

    #[test]
    fn value_str_coercion_from_large_integer() {
        let rt = Runtime::new();
        // Large integers should use exact decimal form
        let v = Value::Num(1e12);
        assert_eq!(rt.value_to_array_key(&v), "1000000000000");
    }

    #[test]
    fn awk_map_nested_keys() {
        let mut m: AwkMap<String, Value> = AwkMap::default();
        m.insert("1\x1c2".into(), Value::Num(42.0)); // SUBSEP is \x1c
        assert_eq!(m.get("1\x1c2").unwrap().as_number(), 42.0);
    }

    #[test]
    fn awk_map_delete_non_existent() {
        let mut m: AwkMap<String, Value> = AwkMap::default();
        m.remove("x"); // should not panic
    }

    #[test]
    fn awk_map_iteration_order() {
        let mut m: AwkMap<String, Value> = AwkMap::default();
        m.insert("a".into(), Value::Num(1.0));
        m.insert("b".into(), Value::Num(2.0));
        // Order is not guaranteed, but we can verify we get both.
        let keys: Vec<_> = m.keys().collect();
        assert_eq!(keys.len(), 2);
    }

    #[test]
    fn runtime_set_field_reconstructs_record() {
        let mut rt = Runtime::new();
        rt.set_record_str("a b c");
        rt.set_field(2, "X").unwrap();
        // $0 should be "a X c" (using current OFS, which is " " by default)
        assert_eq!(rt.record, "a X c");
    }

    #[test]
    fn runtime_set_field_beyond_nf() {
        let mut rt = Runtime::new();
        rt.set_record_str("a");
        rt.set_field(3, "c").unwrap();
        // $0 should be "a  c" (OFS is " ")
        assert_eq!(rt.record, "a  c");
        assert_eq!(rt.nf(), 3);

        // Custom OFS
        rt.vars.insert("OFS".into(), Value::Str(",".into()));
        rt.set_field(2, "b").unwrap();
        assert_eq!(rt.record, "a,b,c");
    }

    #[test]
    fn runtime_set_record_updates_nf() {
        let mut rt = Runtime::new();
        rt.vars.insert("FS".into(), Value::Str(",".into()));
        rt.set_record_str("1,2,3");
        // NF should be 3
        assert_eq!(rt.nf(), 3);
    }

    #[test]
    fn split_string_by_fs_character_mode() {
        // Empty FS -> split into individual characters
        let res = split_string_by_field_separator(b"abc", "", false);
        assert_eq!(res, vec!["a".to_string(), "b".to_string(), "c".to_string()]);
    }

    #[test]
    fn split_string_by_fs_whitespace_mode() {
        // Space FS -> split by any whitespace, stripping leading/trailing
        let res = split_string_by_field_separator(b"  x  y   z  ", " ", false);
        assert_eq!(res, vec!["x".to_string(), "y".to_string(), "z".to_string()]);
    }

    #[test]
    fn split_string_by_fs_regex_case_insensitive() {
        let res = split_string_by_field_separator(b"aXbYc", "[xy]", true);
        assert_eq!(res, vec!["a".to_string(), "b".to_string(), "c".to_string()]);
    }

    #[test]
    fn runtime_vars_initial_state() {
        let rt = Runtime::new();
        // RS and OFS are initialized in vars map.
        assert_eq!(rt.vars.get("RS").unwrap().as_str(), "\n");
        assert_eq!(rt.vars.get("OFS").unwrap().as_str(), " ");
        // FS might be missing from vars map (uses cached_fs until first split/assignment).
        assert_eq!(rt.vars.get("OFMT").unwrap().as_str(), "%.6g");
    }

    // ── split_string_with_seps (gawk 4-arg split) ───────────────────────────

    #[test]
    fn split_with_seps_regex_captures_each_matched_run() {
        // Regression: `split(s, a, /[0-9]+/, seps)` must populate seps with the
        // actual matched separator strings, not leave them empty.
        let (parts, seps) = split_string_with_seps(b"a1b22c333d", "[0-9]+", false);
        assert_eq!(parts, vec!["a", "b", "c", "d"]);
        assert_eq!(seps, vec!["1", "22", "333"]);
    }

    #[test]
    fn split_with_seps_single_char_fs_separator_is_the_char() {
        let (parts, seps) = split_string_with_seps(b"a-b-c", "-", false);
        assert_eq!(parts, vec!["a", "b", "c"]);
        assert_eq!(seps, vec!["-", "-"]);
    }

    #[test]
    fn split_with_seps_single_char_fs_is_literal_not_regex_dot() {
        // POSIX awk: single-char FS is always literal. `"."` splits on the dot character,
        // never as "any byte" (the regex metachar meaning).
        let (parts, seps) = split_string_with_seps(b"a.b.c", ".", false);
        assert_eq!(parts, vec!["a", "b", "c"]);
        assert_eq!(seps, vec![".", "."]);
    }

    #[test]
    fn split_with_seps_empty_fs_yields_empty_separators_between_chars() {
        let (parts, seps) = split_string_with_seps(b"abc", "", false);
        assert_eq!(parts, vec!["a", "b", "c"]);
        assert_eq!(seps, vec!["", ""]);
    }

    #[test]
    fn split_with_seps_whitespace_fs_captures_actual_whitespace_run() {
        // Default-whitespace FS (`" "`): leading whitespace is dropped (no leading empty
        // field), and each captured separator is the literal whitespace run found.
        let (parts, seps) = split_string_with_seps(b"  a   b\tc", " ", false);
        assert_eq!(parts, vec!["a", "b", "c"]);
        assert_eq!(seps, vec!["   ", "\t"]);
    }

    #[test]
    fn split_with_seps_no_match_returns_single_field_no_seps() {
        let (parts, seps) = split_string_with_seps(b"zzz", "[0-9]+", false);
        assert_eq!(parts, vec!["zzz"]);
        assert!(seps.is_empty());
    }

    #[test]
    fn split_with_seps_empty_input_returns_empty_vec() {
        let (parts, seps) = split_string_with_seps(b"", ",", false);
        assert!(parts.is_empty());
        assert!(seps.is_empty());
    }

    #[test]
    fn split_with_seps_trailing_separator_keeps_empty_tail_field() {
        // Single-char FS uses `str::split`, which yields an empty trailing field
        // when the input ends in the separator. seps has one entry (the trailing match).
        let (parts, seps) = split_string_with_seps(b"a,b,", ",", false);
        assert_eq!(parts, vec!["a", "b", ""]);
        assert_eq!(seps, vec![",", ","]);
    }

    #[test]
    fn awk_map_insertion_lookup_deletion_v3() {
        let mut m: super::AwkMap<String, Value> = super::AwkMap::default();
        m.insert(String::from("key1"), Value::Num(100.0));
        m.insert(String::from("key2"), Value::Str("val2".into()));

        assert_eq!(m.get(&String::from("key1")).unwrap().as_number(), 100.0);
        assert_eq!(m.get(&String::from("key2")).unwrap().as_str(), "val2");

        m.remove(&String::from("key1"));
        assert!(!m.contains_key(&String::from("key1")));
    }

    #[test]
    fn runtime_set_field_edge_cases_v3() {
        let mut rt = super::Runtime::new();
        rt.set_field_sep_split(" ", b"a b c");
        rt.ensure_fields_split();
        rt.set_field(5, "e").unwrap();
        assert_eq!(rt.nf(), 5);
        assert_eq!(rt.field(5).unwrap().as_str(), "e");
        rt.set_field(0, "x y").unwrap();
        assert_eq!(rt.record, "x y");
        assert_eq!(rt.nf(), 2);
    }

    #[test]
    fn value_truthiness_v2() {
        assert!(!Value::Uninit.truthy());
        assert!(Value::Num(1.0).truthy());
        assert!(!Value::Num(0.0).truthy());
        // Value::Str parses as number if possible; "0" -> 0.0 -> false
        assert!(!Value::Str("0".into()).truthy());
        assert!(Value::StrLit("0".into()).truthy()); // Literals are truthy if non-empty
        assert!(!Value::Str("".into()).truthy());
    }

    #[test]
    fn value_numeric_conversions_v2() {
        assert_eq!(Value::Uninit.as_number(), 0.0);
        assert_eq!(Value::Str("123.45".into()).as_number(), 123.45);
        assert_eq!(Value::Str("abc".into()).as_number(), 0.0);
    }

    #[test]
    fn runtime_variable_overlay_v2() {
        let mut rt = super::Runtime::new();
        rt.vars.insert("x".into(), Value::Num(10.0));
        assert_eq!(rt.vars.get("x").unwrap().as_number(), 10.0);
    }

    #[test]
    fn runtime_nf_truncation_v2() {
        let mut rt = super::Runtime::new();
        rt.set_field_sep_split(":", b"a:b:c:d");
        rt.ensure_fields_split();
        let _ = rt.set_nf(2);
        assert_eq!(rt.nf(), 2);
        // Default OFS is " "
        assert_eq!(rt.record, "a b");
    }

    #[test]
    fn runtime_record_reconstruction_with_ofs_v2() {
        let mut rt = super::Runtime::new();
        rt.set_field_sep_split(",", b"a,b");
        rt.ensure_fields_split();
        rt.vars.insert("OFS".into(), Value::Str("|".into()));
        rt.set_field(1, "x").unwrap();
        // Changing a field should rebuild the record using OFS
        assert_eq!(rt.record, "x|b");
    }

    #[test]
    fn value_num_v9() {
        let _ = Value::Num(0.0).clone();
    }
    #[test]
    fn value_str_v9() {
        let _ = Value::Str("".into()).clone();
    }
    #[test]
    fn value_uninit_v9() {
        let _ = Value::Uninit.clone();
    }
    #[test]
    fn value_as_number_v9() {
        assert_eq!(Value::Num(1.2).as_number(), 1.2);
    }
    #[test]
    fn value_as_str_v9() {
        assert_eq!(Value::Str("abc".into()).as_str(), "abc");
    }
    #[test]
    fn runtime_nf_initial_v9() {
        assert_eq!(super::Runtime::new().nf(), 0);
    }
    #[test]
    fn runtime_nr_initial_v9() {
        assert_eq!(super::Runtime::new().nr, 0.0);
    }
    #[test]
    fn runtime_fnr_initial_v9() {
        assert_eq!(super::Runtime::new().fnr, 0.0);
    }
    #[test]
    fn runtime_fpat_initial_v9() {
        assert_eq!(super::Runtime::new().vars.get("FPAT").unwrap().as_str(), "");
    }
    #[test]
    fn runtime_fs_initial_v9() {
        // POSIX/gawk default: FS = " " (single space, special-cased to mean
        // "split on whitespace runs").
        let rt = super::Runtime::new();
        let fs = rt
            .vars
            .get("FS")
            .expect("FS must be initialized to gawk default");
        assert_eq!(fs.as_str(), " ", "FS default should be single space");
    }

    #[test]
    fn runtime_ors_initial_v56() {
        assert_eq!(
            super::Runtime::new().vars.get("ORS").unwrap().as_str(),
            "\n"
        );
    }
    #[test]
    fn runtime_ofs_initial_v56() {
        assert_eq!(super::Runtime::new().vars.get("OFS").unwrap().as_str(), " ");
    }
    #[test]
    fn runtime_ofmt_initial_v56() {
        assert_eq!(
            super::Runtime::new().vars.get("OFMT").unwrap().as_str(),
            "%.6g"
        );
    }
    #[test]
    fn runtime_convfmt_initial_v56() {
        assert_eq!(
            super::Runtime::new().vars.get("CONVFMT").unwrap().as_str(),
            "%.6g"
        );
    }
    #[test]
    fn runtime_subsep_initial_v56() {
        assert_eq!(
            super::Runtime::new().vars.get("SUBSEP").unwrap().as_str(),
            "\x1c"
        );
    }

    #[test]
    fn runtime_field_sep_v56_0() {
        let mut rt = super::Runtime::new();
        rt.set_field_sep_split(":", b"a:b:c");
        rt.ensure_fields_split();
        assert_eq!(rt.nf(), 3);
    }
    #[test]
    fn runtime_field_sep_v56_1() {
        let mut rt = super::Runtime::new();
        rt.set_field_sep_split(",", b"a,b,c");
        rt.ensure_fields_split();
        assert_eq!(rt.nf(), 3);
    }
    #[test]
    fn runtime_field_sep_v56_2() {
        let mut rt = super::Runtime::new();
        rt.set_field_sep_split(" ", b"a b c");
        rt.ensure_fields_split();
        assert_eq!(rt.nf(), 3);
    }
}

/// An awk array.
///
/// Subscripts are strings by definition, but one that IS the canonical decimal
/// spelling of an integer is stored as that integer. `a[1]` and `a["1"]` name
/// the same element, so both spellings must normalize to one key — and once
/// they do, the `a[i]` of a counted loop neither renders a string nor
/// allocates one.
///
/// gawk draws the same distinction, and it is most of the difference between
/// them: denying gawk its integer representation (by subscripting with
/// `"k" i`) costs it 2.5x on a million-element build, while awkrs, which had
/// only the string map, barely moves.
///
/// Iteration yields the integer-subscripted elements first. awk leaves
/// `for (k in a)` order unspecified and every implementation answers
/// differently, so this is a legal order — just not the one the single hash
/// map happened to produce.
#[derive(Debug, Clone, Default)]
pub struct AwkArray {
    ints: AwkMap<i64, Value>,
    strs: AwkMap<Box<[u8]>, Value>,
}

/// The integer a subscript names, when the subscript is exactly that integer's
/// decimal spelling.
///
/// `"1"` is `1`. `"01"`, `"+1"`, `" 1"`, `"1.0"`, `"-0"` and `"9223372036854775808"`
/// are not: awk keeps each as its own element, so each has to stay a string
/// key. Checking by rendering the parse back is what makes that exact.
fn canonical_int(b: &[u8]) -> Option<i64> {
    let (neg, d) = match b.split_first() {
        Some((b'-', rest)) => (true, rest),
        _ => (false, b),
    };
    // 19 digits is the most an `i64` can hold without overflow checks per digit;
    // longer subscripts stay strings, as does anything with a leading zero, a
    // sign it does not render, or a non-digit. `-0` is not canonical either:
    // zero renders as "0".
    if d.is_empty() || d.len() > 19 || (d[0] == b'0' && d.len() > 1) || (neg && d == b"0") {
        return None;
    }
    let mut acc: i64 = 0;
    for &c in d {
        if !c.is_ascii_digit() {
            return None;
        }
        acc = acc.checked_mul(10)?.checked_add((c - b'0') as i64)?;
    }
    Some(if neg { -acc } else { acc })
}

impl AwkArray {
    pub fn new() -> Self {
        Self::default()
    }

    /// The element `key` names, whichever half holds it.
    ///
    /// Subscripts are byte strings: `a[$1]` where the field holds a byte that is
    /// not part of valid UTF-8 has to name an entry, and `for (k in a)` has to
    /// hand that subscript back unchanged. The `&str` spellings below are thin
    /// wrappers for the many call sites whose key is text by construction.
    pub fn get_bytes(&self, key: &[u8]) -> Option<&Value> {
        match canonical_int(key) {
            Some(i) => self.ints.get(&i),
            None => self.strs.get(key),
        }
    }

    pub fn get(&self, key: &str) -> Option<&Value> {
        self.get_bytes(key.as_bytes())
    }

    /// `a[i]` where the subscript is already an integer — the hot path, with no
    /// rendering and no parse.
    pub fn get_int(&self, i: i64) -> Option<&Value> {
        self.ints.get(&i)
    }

    pub fn get_mut_bytes(&mut self, key: &[u8]) -> Option<&mut Value> {
        match canonical_int(key) {
            Some(i) => self.ints.get_mut(&i),
            None => self.strs.get_mut(key),
        }
    }

    pub fn get_mut(&mut self, key: &str) -> Option<&mut Value> {
        self.get_mut_bytes(key.as_bytes())
    }

    pub fn insert(&mut self, key: String, val: Value) -> Option<Value> {
        self.insert_bytes(key.as_bytes(), val)
    }

    /// `a[k] = v` with a borrowed subscript.
    ///
    /// The owned form has to be handed a `String` even when the subscript names
    /// an integer — the overwhelmingly common case for `a[$1]`, `a[NR]` and
    /// `a[i]` — and then drops it unread, because an integer subscript is stored
    /// in the `ints` half. That is one wasted allocation per element stored.
    /// Taking the key by reference defers the allocation to the `strs` half,
    /// which is the only half that needs to own it.
    pub fn insert_bytes(&mut self, key: &[u8], val: Value) -> Option<Value> {
        match canonical_int(key) {
            Some(i) => self.ints.insert(i, val),
            None => self.strs.insert(key.into(), val),
        }
    }

    pub fn insert_str(&mut self, key: &str, val: Value) -> Option<Value> {
        self.insert_bytes(key.as_bytes(), val)
    }

    /// `a[i] = v` with an integer subscript — stores without building a key.
    pub fn insert_int(&mut self, i: i64, val: Value) -> Option<Value> {
        self.ints.insert(i, val)
    }

    pub fn remove_bytes(&mut self, key: &[u8]) -> Option<Value> {
        match canonical_int(key) {
            Some(i) => self.ints.remove(&i),
            None => self.strs.remove(key),
        }
    }

    pub fn remove(&mut self, key: &str) -> Option<Value> {
        self.remove_bytes(key.as_bytes())
    }

    pub fn contains_key_bytes(&self, key: &[u8]) -> bool {
        match canonical_int(key) {
            Some(i) => self.ints.contains_key(&i),
            None => self.strs.contains_key(key),
        }
    }

    pub fn contains_key(&self, key: &str) -> bool {
        self.contains_key_bytes(key.as_bytes())
    }

    pub fn len(&self) -> usize {
        self.ints.len() + self.strs.len()
    }

    pub fn is_empty(&self) -> bool {
        self.ints.is_empty() && self.strs.is_empty()
    }

    pub fn clear(&mut self) {
        self.ints.clear();
        self.strs.clear();
    }

    /// Every subscript, rendered. Owned rather than borrowed because an integer
    /// subscript has no stored string to lend.
    pub fn keys(&self) -> Vec<AwkStr> {
        let mut out = Vec::with_capacity(self.len());
        let mut b = KeyBuf::new();
        for i in self.ints.keys() {
            out.push(AwkStr::from(b.write_i64(*i)));
        }
        out.extend(self.strs.keys().map(|k| AwkStr::from(&k[..])));
        out
    }

    pub fn iter(&self) -> impl Iterator<Item = (AwkStr, &Value)> {
        let ints = self.ints.iter().map(|(i, v)| {
            let mut b = KeyBuf::new();
            (AwkStr::from(b.write_i64(*i)), v)
        });
        ints.chain(self.strs.iter().map(|(k, v)| (AwkStr::from(&k[..]), v)))
    }

    /// `entry(k).or_insert(v)` in one call — store `val` only when the
    /// subscript is absent, and hand back what the subscript now names.
    pub fn or_insert(&mut self, key: String, val: Value) -> &mut Value {
        match canonical_int(key.as_bytes()) {
            Some(i) => self.ints.entry(i).or_insert(val),
            None => self
                .strs
                .entry(key.into_bytes().into_boxed_slice())
                .or_insert(val),
        }
    }

    /// [`AwkArray::or_insert`] with the value built only when it is needed.
    pub fn or_insert_with(&mut self, key: String, f: impl FnOnce() -> Value) -> &mut Value {
        match canonical_int(key.as_bytes()) {
            Some(i) => self.ints.entry(i).or_insert_with(f),
            None => self
                .strs
                .entry(key.into_bytes().into_boxed_slice())
                .or_insert_with(f),
        }
    }

    pub fn values(&self) -> impl Iterator<Item = &Value> {
        self.ints.values().chain(self.strs.values())
    }
}

impl FromIterator<(String, Value)> for AwkArray {
    fn from_iter<T: IntoIterator<Item = (String, Value)>>(it: T) -> Self {
        let mut a = AwkArray::new();
        for (k, v) in it {
            a.insert(k, v);
        }
        a
    }
}

/// Scratch for rendering an integral array subscript in place.
///
/// Wide enough for any `i64` in decimal with its sign, so writing one can never
/// need to grow or allocate.
pub struct KeyBuf([u8; 20]);

impl Default for KeyBuf {
    fn default() -> Self {
        Self::new()
    }
}

impl KeyBuf {
    pub fn new() -> Self {
        KeyBuf([0; 20])
    }

    /// `n` in decimal, written back-to-front, returned as a borrow of this
    /// buffer — the integer writer without the formatting machinery, on a path
    /// that runs once per array subscript.
    pub fn write_i64(&mut self, n: i64) -> &str {
        let neg = n < 0;
        // `-i64::MIN` overflows, so accumulate the magnitude as `u64`.
        let mut m = if neg {
            (n as i128).unsigned_abs() as u64
        } else {
            n as u64
        };
        let mut i = self.0.len();
        loop {
            i -= 1;
            self.0[i] = b'0' + (m % 10) as u8;
            m /= 10;
            if m == 0 {
                break;
            }
        }
        if neg {
            i -= 1;
            self.0[i] = b'-';
        }
        // Every byte written is an ASCII digit or '-', so this is UTF-8 by
        // construction.
        std::str::from_utf8(&self.0[i..]).expect("ascii digits")
    }
}