summaryrefslogtreecommitdiff
path: root/gnu/usr.bin/perl/regcomp.c
blob: c0425b766d20834e21a1d9fd1b07271de5593643 (plain)
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
/*    regcomp.c
 */

/*
 * "A fair jaw-cracker dwarf-language must be."  --Samwise Gamgee
 */

/* NOTE: this is derived from Henry Spencer's regexp code, and should not
 * confused with the original package (see point 3 below).  Thanks, Henry!
 */

/* Additional note: this code is very heavily munged from Henry's version
 * in places.  In some spots I've traded clarity for efficiency, so don't
 * blame Henry for some of the lack of readability.
 */

/* The names of the functions have been changed from regcomp and
 * regexec to  pregcomp and pregexec in order to avoid conflicts
 * with the POSIX routines of the same names.
*/

#ifdef PERL_EXT_RE_BUILD
/* need to replace pregcomp et al, so enable that */
#  ifndef PERL_IN_XSUB_RE
#    define PERL_IN_XSUB_RE
#  endif
/* need access to debugger hooks */
#  if defined(PERL_EXT_RE_DEBUG) && !defined(DEBUGGING)
#    define DEBUGGING
#  endif
#endif

#ifdef PERL_IN_XSUB_RE
/* We *really* need to overwrite these symbols: */
#  define Perl_pregcomp my_regcomp
#  define Perl_regdump my_regdump
#  define Perl_regprop my_regprop
#  define Perl_pregfree my_regfree
#  define Perl_re_intuit_string my_re_intuit_string
/* *These* symbols are masked to allow static link. */
#  define Perl_regnext my_regnext
#  define Perl_save_re_context my_save_re_context
#  define Perl_reginitcolors my_reginitcolors 

#  define PERL_NO_GET_CONTEXT
#endif 

/*SUPPRESS 112*/
/*
 * pregcomp and pregexec -- regsub and regerror are not used in perl
 *
 *	Copyright (c) 1986 by University of Toronto.
 *	Written by Henry Spencer.  Not derived from licensed software.
 *
 *	Permission is granted to anyone to use this software for any
 *	purpose on any computer system, and to redistribute it freely,
 *	subject to the following restrictions:
 *
 *	1. The author is not responsible for the consequences of use of
 *		this software, no matter how awful, even if they arise
 *		from defects in it.
 *
 *	2. The origin of this software must not be misrepresented, either
 *		by explicit claim or by omission.
 *
 *	3. Altered versions must be plainly marked as such, and must not
 *		be misrepresented as being the original software.
 *
 *
 ****    Alterations to Henry's code are...
 ****
 ****    Copyright (c) 1991-2000, Larry Wall
 ****
 ****    You may distribute under the terms of either the GNU General Public
 ****    License or the Artistic License, as specified in the README file.

 *
 * Beware that some of this code is subtly aware of the way operator
 * precedence is structured in regular expressions.  Serious changes in
 * regular-expression syntax might require a total rethink.
 */
#include "EXTERN.h"
#define PERL_IN_REGCOMP_C
#include "perl.h"

#ifdef PERL_IN_XSUB_RE
#  if defined(PERL_CAPI) || defined(PERL_OBJECT)
#    include "XSUB.h"
#  endif
#else
#  include "INTERN.h"
#endif

#define REG_COMP_C
#include "regcomp.h"

#ifdef op
#undef op
#endif /* op */

#ifdef MSDOS
# if defined(BUGGY_MSC6)
 /* MSC 6.00A breaks on op/regexp.t test 85 unless we turn this off */
 # pragma optimize("a",off)
 /* But MSC 6.00A is happy with 'w', for aliases only across function calls*/
 # pragma optimize("w",on )
# endif /* BUGGY_MSC6 */
#endif /* MSDOS */

#ifndef STATIC
#define	STATIC	static
#endif

#define	ISMULT1(c)	((c) == '*' || (c) == '+' || (c) == '?')
#define	ISMULT2(s)	((*s) == '*' || (*s) == '+' || (*s) == '?' || \
	((*s) == '{' && regcurly(s)))
#ifdef atarist
#define	PERL_META	"^$.[()|?+*\\"
#else
#define	META	"^$.[()|?+*\\"
#endif

#ifdef SPSTART
#undef SPSTART		/* dratted cpp namespace... */
#endif
/*
 * Flags to be passed up and down.
 */
#define	WORST		0	/* Worst case. */
#define	HASWIDTH	0x1	/* Known to match non-null strings. */
#define	SIMPLE		0x2	/* Simple enough to be STAR/PLUS operand. */
#define	SPSTART		0x4	/* Starts with * or +. */
#define TRYAGAIN	0x8	/* Weeded out a declaration. */

/* Length of a variant. */

typedef struct scan_data_t {
    I32 len_min;
    I32 len_delta;
    I32 pos_min;
    I32 pos_delta;
    SV *last_found;
    I32 last_end;			/* min value, <0 unless valid. */
    I32 last_start_min;
    I32 last_start_max;
    SV **longest;			/* Either &l_fixed, or &l_float. */
    SV *longest_fixed;
    I32 offset_fixed;
    SV *longest_float;
    I32 offset_float_min;
    I32 offset_float_max;
    I32 flags;
    I32 whilem_c;
    struct regnode_charclass_class *start_class;
} scan_data_t;

/*
 * Forward declarations for pregcomp()'s friends.
 */

static scan_data_t zero_scan_data = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
				      0, 0, 0, 0, 0 };

#define SF_BEFORE_EOL		(SF_BEFORE_SEOL|SF_BEFORE_MEOL)
#define SF_BEFORE_SEOL		0x1
#define SF_BEFORE_MEOL		0x2
#define SF_FIX_BEFORE_EOL	(SF_FIX_BEFORE_SEOL|SF_FIX_BEFORE_MEOL)
#define SF_FL_BEFORE_EOL	(SF_FL_BEFORE_SEOL|SF_FL_BEFORE_MEOL)

#ifdef NO_UNARY_PLUS
#  define SF_FIX_SHIFT_EOL	(0+2)
#  define SF_FL_SHIFT_EOL		(0+4)
#else
#  define SF_FIX_SHIFT_EOL	(+2)
#  define SF_FL_SHIFT_EOL		(+4)
#endif

#define SF_FIX_BEFORE_SEOL	(SF_BEFORE_SEOL << SF_FIX_SHIFT_EOL)
#define SF_FIX_BEFORE_MEOL	(SF_BEFORE_MEOL << SF_FIX_SHIFT_EOL)

#define SF_FL_BEFORE_SEOL	(SF_BEFORE_SEOL << SF_FL_SHIFT_EOL)
#define SF_FL_BEFORE_MEOL	(SF_BEFORE_MEOL << SF_FL_SHIFT_EOL) /* 0x20 */
#define SF_IS_INF		0x40
#define SF_HAS_PAR		0x80
#define SF_IN_PAR		0x100
#define SF_HAS_EVAL		0x200
#define SCF_DO_SUBSTR		0x400
#define SCF_DO_STCLASS_AND	0x0800
#define SCF_DO_STCLASS_OR	0x1000
#define SCF_DO_STCLASS		(SCF_DO_STCLASS_AND|SCF_DO_STCLASS_OR)

#define RF_utf8		8
#define UTF (PL_reg_flags & RF_utf8)
#define LOC (PL_regflags & PMf_LOCALE)
#define FOLD (PL_regflags & PMf_FOLD)

#define OOB_CHAR8		1234
#define OOB_UTF8		123456
#define OOB_NAMEDCLASS		-1

#define CHR_SVLEN(sv) (UTF ? sv_len_utf8(sv) : SvCUR(sv))
#define CHR_DIST(a,b) (UTF ? utf8_distance(a,b) : a - b)

/* Allow for side effects in s */
#define REGC(c,s) STMT_START { if (!SIZE_ONLY) *(s) = (c); else (s);} STMT_END

static void clear_re(pTHXo_ void *r);

/* Mark that we cannot extend a found fixed substring at this point.
   Updata the longest found anchored substring and the longest found
   floating substrings if needed. */

STATIC void
S_scan_commit(pTHX_ scan_data_t *data)
{
    dTHR;
    STRLEN l = CHR_SVLEN(data->last_found);
    STRLEN old_l = CHR_SVLEN(*data->longest);
    
    if ((l >= old_l) && ((l > old_l) || (data->flags & SF_BEFORE_EOL))) {
	sv_setsv(*data->longest, data->last_found);
	if (*data->longest == data->longest_fixed) {
	    data->offset_fixed = l ? data->last_start_min : data->pos_min;
	    if (data->flags & SF_BEFORE_EOL)
		data->flags 
		    |= ((data->flags & SF_BEFORE_EOL) << SF_FIX_SHIFT_EOL);
	    else
		data->flags &= ~SF_FIX_BEFORE_EOL;
	}
	else {
	    data->offset_float_min = l ? data->last_start_min : data->pos_min;
	    data->offset_float_max = (l 
				      ? data->last_start_max 
				      : data->pos_min + data->pos_delta);
	    if (data->flags & SF_BEFORE_EOL)
		data->flags 
		    |= ((data->flags & SF_BEFORE_EOL) << SF_FL_SHIFT_EOL);
	    else
		data->flags &= ~SF_FL_BEFORE_EOL;
	}
    }
    SvCUR_set(data->last_found, 0);
    data->last_end = -1;
    data->flags &= ~SF_BEFORE_EOL;
}

/* Can match anything (initialization) */
STATIC void
S_cl_anything(pTHX_ struct regnode_charclass_class *cl)
{
    int value;

    ANYOF_CLASS_ZERO(cl);
    for (value = 0; value < 256; ++value)
	ANYOF_BITMAP_SET(cl, value);
    cl->flags = ANYOF_EOS;
    if (LOC)
	cl->flags |= ANYOF_LOCALE;
}

/* Can match anything (initialization) */
STATIC int
S_cl_is_anything(pTHX_ struct regnode_charclass_class *cl)
{
    int value;

    for (value = 0; value < ANYOF_MAX; value += 2)
	if (ANYOF_CLASS_TEST(cl, value) && ANYOF_CLASS_TEST(cl, value + 1))
	    return 1;
    for (value = 0; value < 256; ++value)
	if (!ANYOF_BITMAP_TEST(cl, value))
	    return 0;
    return 1;
}

/* Can match anything (initialization) */
STATIC void
S_cl_init(pTHX_ struct regnode_charclass_class *cl)
{
    Zero(cl, 1, struct regnode_charclass_class);
    cl->type = ANYOF;
    cl_anything(cl);
}

STATIC void
S_cl_init_zero(pTHX_ struct regnode_charclass_class *cl)
{
    Zero(cl, 1, struct regnode_charclass_class);
    cl->type = ANYOF;
    cl_anything(cl);
    if (LOC)
	cl->flags |= ANYOF_LOCALE;
}

/* 'And' a given class with another one.  Can create false positives */
/* We assume that cl is not inverted */
STATIC void
S_cl_and(pTHX_ struct regnode_charclass_class *cl,
	 struct regnode_charclass_class *and_with)
{
    if (!(and_with->flags & ANYOF_CLASS)
	&& !(cl->flags & ANYOF_CLASS)
	&& (and_with->flags & ANYOF_LOCALE) == (cl->flags & ANYOF_LOCALE)
	&& !(and_with->flags & ANYOF_FOLD)
	&& !(cl->flags & ANYOF_FOLD)) {
	int i;

	if (and_with->flags & ANYOF_INVERT)
	    for (i = 0; i < ANYOF_BITMAP_SIZE; i++)
		cl->bitmap[i] &= ~and_with->bitmap[i];
	else
	    for (i = 0; i < ANYOF_BITMAP_SIZE; i++)
		cl->bitmap[i] &= and_with->bitmap[i];
    } /* XXXX: logic is complicated otherwise, leave it along for a moment. */
    if (!(and_with->flags & ANYOF_EOS))
	cl->flags &= ~ANYOF_EOS;
}

/* 'OR' a given class with another one.  Can create false positives */
/* We assume that cl is not inverted */
STATIC void
S_cl_or(pTHX_ struct regnode_charclass_class *cl, struct regnode_charclass_class *or_with)
{
    if (or_with->flags & ANYOF_INVERT) {
	/* We do not use
	 * (B1 | CL1) | (!B2 & !CL2) = (B1 | !B2 & !CL2) | (CL1 | (!B2 & !CL2))
	 *   <= (B1 | !B2) | (CL1 | !CL2)
	 * which is wasteful if CL2 is small, but we ignore CL2:
	 *   (B1 | CL1) | (!B2 & !CL2) <= (B1 | CL1) | !B2 = (B1 | !B2) | CL1
	 * XXXX Can we handle case-fold?  Unclear:
	 *   (OK1(i) | OK1(i')) | !(OK1(i) | OK1(i')) =
	 *   (OK1(i) | OK1(i')) | (!OK1(i) & !OK1(i'))
	 */
	if ( (or_with->flags & ANYOF_LOCALE) == (cl->flags & ANYOF_LOCALE)
	     && !(or_with->flags & ANYOF_FOLD)
	     && !(cl->flags & ANYOF_FOLD) ) {
	    int i;

	    for (i = 0; i < ANYOF_BITMAP_SIZE; i++)
		cl->bitmap[i] |= ~or_with->bitmap[i];
	} /* XXXX: logic is complicated otherwise */
	else {
	    cl_anything(cl);
	}
    } else {
	/* (B1 | CL1) | (B2 | CL2) = (B1 | B2) | (CL1 | CL2)) */
	if ( (or_with->flags & ANYOF_LOCALE) == (cl->flags & ANYOF_LOCALE)
	     && (!(or_with->flags & ANYOF_FOLD) 
		 || (cl->flags & ANYOF_FOLD)) ) {
	    int i;

	    /* OR char bitmap and class bitmap separately */
	    for (i = 0; i < ANYOF_BITMAP_SIZE; i++)
		cl->bitmap[i] |= or_with->bitmap[i];
	    if (or_with->flags & ANYOF_CLASS) {
		for (i = 0; i < ANYOF_CLASSBITMAP_SIZE; i++)
		    cl->classflags[i] |= or_with->classflags[i];
		cl->flags |= ANYOF_CLASS;
	    }
	}
	else { /* XXXX: logic is complicated, leave it along for a moment. */
	    cl_anything(cl);
	}
    }
    if (or_with->flags & ANYOF_EOS)
	cl->flags |= ANYOF_EOS;
}

/* REx optimizer.  Converts nodes into quickier variants "in place".
   Finds fixed substrings.  */

/* Stops at toplevel WHILEM as well as at `last'. At end *scanp is set
   to the position after last scanned or to NULL. */

STATIC I32
S_study_chunk(pTHX_ regnode **scanp, I32 *deltap, regnode *last, scan_data_t *data, U32 flags)
			/* scanp: Start here (read-write). */
			/* deltap: Write maxlen-minlen here. */
			/* last: Stop before this one. */
{
    dTHR;
    I32 min = 0, pars = 0, code;
    regnode *scan = *scanp, *next;
    I32 delta = 0;
    int is_inf = (flags & SCF_DO_SUBSTR) && (data->flags & SF_IS_INF);
    int is_inf_internal = 0;		/* The studied chunk is infinite */
    I32 is_par = OP(scan) == OPEN ? ARG(scan) : 0;
    scan_data_t data_fake;
    struct regnode_charclass_class and_with; /* Valid if flags & SCF_DO_STCLASS_OR */
    
    while (scan && OP(scan) != END && scan < last) {
	/* Peephole optimizer: */

	if (PL_regkind[(U8)OP(scan)] == EXACT) {
	    /* Merge several consecutive EXACTish nodes into one. */
	    regnode *n = regnext(scan);
	    U32 stringok = 1;
#ifdef DEBUGGING
	    regnode *stop = scan;
#endif 

	    next = scan + NODE_SZ_STR(scan);
	    /* Skip NOTHING, merge EXACT*. */
	    while (n &&
		   ( PL_regkind[(U8)OP(n)] == NOTHING || 
		     (stringok && (OP(n) == OP(scan))))
		   && NEXT_OFF(n)
		   && NEXT_OFF(scan) + NEXT_OFF(n) < I16_MAX) {
		if (OP(n) == TAIL || n > next)
		    stringok = 0;
		if (PL_regkind[(U8)OP(n)] == NOTHING) {
		    NEXT_OFF(scan) += NEXT_OFF(n);
		    next = n + NODE_STEP_REGNODE;
#ifdef DEBUGGING
		    if (stringok)
			stop = n;
#endif 
		    n = regnext(n);
		}
		else {
		    int oldl = STR_LEN(scan);
		    regnode *nnext = regnext(n);
		    
		    if (oldl + STR_LEN(n) > U8_MAX) 
			break;
		    NEXT_OFF(scan) += NEXT_OFF(n);
		    STR_LEN(scan) += STR_LEN(n);
		    next = n + NODE_SZ_STR(n);
		    /* Now we can overwrite *n : */
		    Move(STRING(n), STRING(scan) + oldl,
			 STR_LEN(n), char);
#ifdef DEBUGGING
		    if (stringok)
			stop = next - 1;
#endif 
		    n = nnext;
		}
	    }
#ifdef DEBUGGING
	    /* Allow dumping */
	    n = scan + NODE_SZ_STR(scan);
	    while (n <= stop) {
		if (PL_regkind[(U8)OP(n)] != NOTHING || OP(n) == NOTHING) {
		    OP(n) = OPTIMIZED;
		    NEXT_OFF(n) = 0;
		}
		n++;
	    }
#endif
	}
	/* Follow the next-chain of the current node and optimize
	   away all the NOTHINGs from it.  */
	if (OP(scan) != CURLYX) {
	    int max = (reg_off_by_arg[OP(scan)]
		       ? I32_MAX
		       /* I32 may be smaller than U16 on CRAYs! */
		       : (I32_MAX < U16_MAX ? I32_MAX : U16_MAX));
	    int off = (reg_off_by_arg[OP(scan)] ? ARG(scan) : NEXT_OFF(scan));
	    int noff;
	    regnode *n = scan;
	    
	    /* Skip NOTHING and LONGJMP. */
	    while ((n = regnext(n))
		   && ((PL_regkind[(U8)OP(n)] == NOTHING && (noff = NEXT_OFF(n)))
		       || ((OP(n) == LONGJMP) && (noff = ARG(n))))
		   && off + noff < max)
		off += noff;
	    if (reg_off_by_arg[OP(scan)])
		ARG(scan) = off;
	    else 
		NEXT_OFF(scan) = off;
	}
	/* The principal pseudo-switch.  Cannot be a switch, since we
	   look into several different things.  */
	if (OP(scan) == BRANCH || OP(scan) == BRANCHJ 
		   || OP(scan) == IFTHEN || OP(scan) == SUSPEND) {
	    next = regnext(scan);
	    code = OP(scan);
	    
	    if (OP(next) == code || code == IFTHEN || code == SUSPEND) { 
		I32 max1 = 0, min1 = I32_MAX, num = 0;
		struct regnode_charclass_class accum;
		
		if (flags & SCF_DO_SUBSTR) /* XXXX Add !SUSPEND? */
		    scan_commit(data);	/* Cannot merge strings after this. */
		if (flags & SCF_DO_STCLASS)
		    cl_init_zero(&accum);
		while (OP(scan) == code) {
		    I32 deltanext, minnext, f = 0;
		    struct regnode_charclass_class this_class;

		    num++;
		    data_fake.flags = 0;
		    if (data)
			data_fake.whilem_c = data->whilem_c;
		    next = regnext(scan);
		    scan = NEXTOPER(scan);
		    if (code != BRANCH)
			scan = NEXTOPER(scan);
		    if (flags & SCF_DO_STCLASS) {
			cl_init(&this_class);
			data_fake.start_class = &this_class;
			f = SCF_DO_STCLASS_AND;
		    }		    
		    /* we suppose the run is continuous, last=next...*/
		    minnext = study_chunk(&scan, &deltanext, next,
					  &data_fake, f);
		    if (min1 > minnext) 
			min1 = minnext;
		    if (max1 < minnext + deltanext)
			max1 = minnext + deltanext;
		    if (deltanext == I32_MAX)
			is_inf = is_inf_internal = 1;
		    scan = next;
		    if (data_fake.flags & (SF_HAS_PAR|SF_IN_PAR))
			pars++;
		    if (data && (data_fake.flags & SF_HAS_EVAL))
			data->flags |= SF_HAS_EVAL;
		    if (data)
			data->whilem_c = data_fake.whilem_c;
		    if (flags & SCF_DO_STCLASS)
			cl_or(&accum, &this_class);
		    if (code == SUSPEND) 
			break;
		}
		if (code == IFTHEN && num < 2) /* Empty ELSE branch */
		    min1 = 0;
		if (flags & SCF_DO_SUBSTR) {
		    data->pos_min += min1;
		    data->pos_delta += max1 - min1;
		    if (max1 != min1 || is_inf)
			data->longest = &(data->longest_float);
		}
		min += min1;
		delta += max1 - min1;
		if (flags & SCF_DO_STCLASS_OR) {
		    cl_or(data->start_class, &accum);
		    if (min1) {
			cl_and(data->start_class, &and_with);
			flags &= ~SCF_DO_STCLASS;
		    }
		}
		else if (flags & SCF_DO_STCLASS_AND) {
		    if (min1) {
			cl_and(data->start_class, &accum);
			flags &= ~SCF_DO_STCLASS;
		    }
		    else {
			/* Switch to OR mode: cache the old value of 
			 * data->start_class */
			StructCopy(data->start_class, &and_with,
				   struct regnode_charclass_class);
			flags &= ~SCF_DO_STCLASS_AND;
			StructCopy(&accum, data->start_class,
				   struct regnode_charclass_class);
			flags |= SCF_DO_STCLASS_OR;
			data->start_class->flags |= ANYOF_EOS;
		    }
		}
	    }
	    else if (code == BRANCHJ)	/* single branch is optimized. */
		scan = NEXTOPER(NEXTOPER(scan));
	    else			/* single branch is optimized. */
		scan = NEXTOPER(scan);
	    continue;
	}
	else if (OP(scan) == EXACT) {
	    I32 l = STR_LEN(scan);
	    if (UTF) {
		unsigned char *s = (unsigned char *)STRING(scan);
		unsigned char *e = s + l;
		I32 newl = 0;
		while (s < e) {
		    newl++;
		    s += UTF8SKIP(s);
		}
		l = newl;
	    }
	    min += l;
	    if (flags & SCF_DO_SUBSTR) { /* Update longest substr. */
		/* The code below prefers earlier match for fixed
		   offset, later match for variable offset.  */
		if (data->last_end == -1) { /* Update the start info. */
		    data->last_start_min = data->pos_min;
 		    data->last_start_max = is_inf
 			? I32_MAX : data->pos_min + data->pos_delta; 
		}
		sv_catpvn(data->last_found, STRING(scan), STR_LEN(scan));
		data->last_end = data->pos_min + l;
		data->pos_min += l; /* As in the first entry. */
		data->flags &= ~SF_BEFORE_EOL;
	    }
	    if (flags & SCF_DO_STCLASS_AND) {
		/* Check whether it is compatible with what we know already! */
		int compat = 1;

		if (!(data->start_class->flags & (ANYOF_CLASS | ANYOF_LOCALE)) 
		    && !ANYOF_BITMAP_TEST(data->start_class, *STRING(scan))
		    && (!(data->start_class->flags & ANYOF_FOLD)
			|| !ANYOF_BITMAP_TEST(data->start_class,
					      PL_fold[*(U8*)STRING(scan)])))
		    compat = 0;
		ANYOF_CLASS_ZERO(data->start_class);
		ANYOF_BITMAP_ZERO(data->start_class);
		if (compat)
		    ANYOF_BITMAP_SET(data->start_class, *STRING(scan));
		data->start_class->flags &= ~ANYOF_EOS;
	    }
	    else if (flags & SCF_DO_STCLASS_OR) {
		/* false positive possible if the class is case-folded */
		ANYOF_BITMAP_SET(data->start_class, *STRING(scan));	
		data->start_class->flags &= ~ANYOF_EOS;
		cl_and(data->start_class, &and_with);
	    }
	    flags &= ~SCF_DO_STCLASS;
	}
	else if (PL_regkind[(U8)OP(scan)] == EXACT) { /* But OP != EXACT! */
	    I32 l = STR_LEN(scan);

	    /* Search for fixed substrings supports EXACT only. */
	    if (flags & SCF_DO_SUBSTR) 
		scan_commit(data);
	    if (UTF) {
		unsigned char *s = (unsigned char *)STRING(scan);
		unsigned char *e = s + l;
		I32 newl = 0;
		while (s < e) {
		    newl++;
		    s += UTF8SKIP(s);
		}
		l = newl;
	    }
	    min += l;
	    if (data && (flags & SCF_DO_SUBSTR))
		data->pos_min += l;
	    if (flags & SCF_DO_STCLASS_AND) {
		/* Check whether it is compatible with what we know already! */
		int compat = 1;

		if (!(data->start_class->flags & (ANYOF_CLASS | ANYOF_LOCALE)) 
		    && !ANYOF_BITMAP_TEST(data->start_class, *STRING(scan))
		    && !ANYOF_BITMAP_TEST(data->start_class, 
					  PL_fold[*(U8*)STRING(scan)]))
		    compat = 0;
		ANYOF_CLASS_ZERO(data->start_class);
		ANYOF_BITMAP_ZERO(data->start_class);
		if (compat) {
		    ANYOF_BITMAP_SET(data->start_class, *STRING(scan));
		    data->start_class->flags &= ~ANYOF_EOS;
		    data->start_class->flags |= ANYOF_FOLD;
		    if (OP(scan) == EXACTFL)
			data->start_class->flags |= ANYOF_LOCALE;
		}
	    }
	    else if (flags & SCF_DO_STCLASS_OR) {
		if (data->start_class->flags & ANYOF_FOLD) {
		    /* false positive possible if the class is case-folded.
		       Assume that the locale settings are the same... */
		    ANYOF_BITMAP_SET(data->start_class, *STRING(scan));	
		    data->start_class->flags &= ~ANYOF_EOS;
		}
		cl_and(data->start_class, &and_with);
	    }
	    flags &= ~SCF_DO_STCLASS;
	}
	else if (strchr((char*)PL_varies,OP(scan))) {
	    I32 mincount, maxcount, minnext, deltanext, pos_before, fl;
	    I32 f = flags;
	    regnode *oscan = scan;
	    struct regnode_charclass_class this_class;
	    struct regnode_charclass_class *oclass = NULL;

	    switch (PL_regkind[(U8)OP(scan)]) {
	    case WHILEM:		/* End of (?:...)* . */
		scan = NEXTOPER(scan);
		goto finish;
	    case PLUS:
		if (flags & (SCF_DO_SUBSTR | SCF_DO_STCLASS)) {
		    next = NEXTOPER(scan);
		    if (OP(next) == EXACT || (flags & SCF_DO_STCLASS)) {
			mincount = 1; 
			maxcount = REG_INFTY; 
			next = regnext(scan);
			scan = NEXTOPER(scan);
			goto do_curly;
		    }
		}
		if (flags & SCF_DO_SUBSTR)
		    data->pos_min++;
		min++;
		/* Fall through. */
	    case STAR:
		if (flags & SCF_DO_STCLASS) {
		    mincount = 0;
		    maxcount = REG_INFTY; 
		    next = regnext(scan);
		    scan = NEXTOPER(scan);
		    goto do_curly;
		}
		is_inf = is_inf_internal = 1; 
		scan = regnext(scan);
		if (flags & SCF_DO_SUBSTR) {
		    scan_commit(data);	/* Cannot extend fixed substrings */
		    data->longest = &(data->longest_float);
		}
		goto optimize_curly_tail;
	    case CURLY:
		mincount = ARG1(scan); 
		maxcount = ARG2(scan);
		next = regnext(scan);
		scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
	      do_curly:
		if (flags & SCF_DO_SUBSTR) {
		    if (mincount == 0) scan_commit(data); /* Cannot extend fixed substrings */
		    pos_before = data->pos_min;
		}
		if (data) {
		    fl = data->flags;
		    data->flags &= ~(SF_HAS_PAR|SF_IN_PAR|SF_HAS_EVAL);
		    if (is_inf)
			data->flags |= SF_IS_INF;
		}
		if (flags & SCF_DO_STCLASS) {
		    cl_init(&this_class);
		    oclass = data->start_class;
		    data->start_class = &this_class;
		    f |= SCF_DO_STCLASS_AND;
		    f &= ~SCF_DO_STCLASS_OR;
		}

		/* This will finish on WHILEM, setting scan, or on NULL: */
		minnext = study_chunk(&scan, &deltanext, last, data, 
				      mincount == 0 
					? (f & ~SCF_DO_SUBSTR) : f);

		if (flags & SCF_DO_STCLASS)
		    data->start_class = oclass;
		if (mincount == 0 || minnext == 0) {
		    if (flags & SCF_DO_STCLASS_OR) {
			cl_or(data->start_class, &this_class);
		    }
		    else if (flags & SCF_DO_STCLASS_AND) {
			/* Switch to OR mode: cache the old value of 
			 * data->start_class */
			StructCopy(data->start_class, &and_with,
				   struct regnode_charclass_class);
			flags &= ~SCF_DO_STCLASS_AND;
			StructCopy(&this_class, data->start_class,
				   struct regnode_charclass_class);
			flags |= SCF_DO_STCLASS_OR;
			data->start_class->flags |= ANYOF_EOS;
		    }
		} else {		/* Non-zero len */
		    if (flags & SCF_DO_STCLASS_OR) {
			cl_or(data->start_class, &this_class);
			cl_and(data->start_class, &and_with);
		    }
		    else if (flags & SCF_DO_STCLASS_AND)
			cl_and(data->start_class, &this_class);
		    flags &= ~SCF_DO_STCLASS;
		}
		if (!scan) 		/* It was not CURLYX, but CURLY. */
		    scan = next;
		if (ckWARN(WARN_REGEXP) && (minnext + deltanext == 0) 
		    && !(data->flags & (SF_HAS_PAR|SF_IN_PAR))
		    && maxcount <= REG_INFTY/3) /* Complement check for big count */
		    Perl_warner(aTHX_ WARN_REGEXP,
				"Strange *+?{} on zero-length expression");
		min += minnext * mincount;
		is_inf_internal |= ((maxcount == REG_INFTY 
				     && (minnext + deltanext) > 0)
				    || deltanext == I32_MAX);
		is_inf |= is_inf_internal;
		delta += (minnext + deltanext) * maxcount - minnext * mincount;

		/* Try powerful optimization CURLYX => CURLYN. */
		if (  OP(oscan) == CURLYX && data 
		      && data->flags & SF_IN_PAR
		      && !(data->flags & SF_HAS_EVAL)
		      && !deltanext && minnext == 1 ) {
		    /* Try to optimize to CURLYN.  */
		    regnode *nxt = NEXTOPER(oscan) + EXTRA_STEP_2ARGS;
		    regnode *nxt1 = nxt, *nxt2;

		    /* Skip open. */
		    nxt = regnext(nxt);
		    if (!strchr((char*)PL_simple,OP(nxt))
			&& !(PL_regkind[(U8)OP(nxt)] == EXACT
			     && STR_LEN(nxt) == 1)) 
			goto nogo;
		    nxt2 = nxt;
		    nxt = regnext(nxt);
		    if (OP(nxt) != CLOSE) 
			goto nogo;
		    /* Now we know that nxt2 is the only contents: */
		    oscan->flags = ARG(nxt);
		    OP(oscan) = CURLYN;
		    OP(nxt1) = NOTHING;	/* was OPEN. */
#ifdef DEBUGGING
		    OP(nxt1 + 1) = OPTIMIZED; /* was count. */
		    NEXT_OFF(nxt1+ 1) = 0; /* just for consistancy. */
		    NEXT_OFF(nxt2) = 0;	/* just for consistancy with CURLY. */
		    OP(nxt) = OPTIMIZED;	/* was CLOSE. */
		    OP(nxt + 1) = OPTIMIZED; /* was count. */
		    NEXT_OFF(nxt+ 1) = 0; /* just for consistancy. */
#endif 
		}
	      nogo:

		/* Try optimization CURLYX => CURLYM. */
		if (  OP(oscan) == CURLYX && data 
		      && !(data->flags & SF_HAS_PAR)
		      && !(data->flags & SF_HAS_EVAL)
		      && !deltanext  ) {
		    /* XXXX How to optimize if data == 0? */
		    /* Optimize to a simpler form.  */
		    regnode *nxt = NEXTOPER(oscan) + EXTRA_STEP_2ARGS; /* OPEN */
		    regnode *nxt2;

		    OP(oscan) = CURLYM;
		    while ( (nxt2 = regnext(nxt)) /* skip over embedded stuff*/
			    && (OP(nxt2) != WHILEM)) 
			nxt = nxt2;
		    OP(nxt2)  = SUCCEED; /* Whas WHILEM */
		    /* Need to optimize away parenths. */
		    if (data->flags & SF_IN_PAR) {
			/* Set the parenth number.  */
			regnode *nxt1 = NEXTOPER(oscan) + EXTRA_STEP_2ARGS; /* OPEN*/

			if (OP(nxt) != CLOSE) 
			    FAIL("panic opt close");
			oscan->flags = ARG(nxt);
			OP(nxt1) = OPTIMIZED;	/* was OPEN. */
			OP(nxt) = OPTIMIZED;	/* was CLOSE. */
#ifdef DEBUGGING
			OP(nxt1 + 1) = OPTIMIZED; /* was count. */
			OP(nxt + 1) = OPTIMIZED; /* was count. */
			NEXT_OFF(nxt1 + 1) = 0; /* just for consistancy. */
			NEXT_OFF(nxt + 1) = 0; /* just for consistancy. */
#endif 
#if 0
			while ( nxt1 && (OP(nxt1) != WHILEM)) {
			    regnode *nnxt = regnext(nxt1);
			    
			    if (nnxt == nxt) {
				if (reg_off_by_arg[OP(nxt1)])
				    ARG_SET(nxt1, nxt2 - nxt1);
				else if (nxt2 - nxt1 < U16_MAX)
				    NEXT_OFF(nxt1) = nxt2 - nxt1;
				else
				    OP(nxt) = NOTHING;	/* Cannot beautify */
			    }
			    nxt1 = nnxt;
			}
#endif
			/* Optimize again: */
			study_chunk(&nxt1, &deltanext, nxt, NULL, 0);
		    }
		    else
			oscan->flags = 0;
		}
		else if (OP(oscan) == CURLYX && data && ++data->whilem_c < 16) {
		    /* This stays as CURLYX, and can put the count/of pair. */
		    /* Find WHILEM (as in regexec.c) */
		    regnode *nxt = oscan + NEXT_OFF(oscan);

		    if (OP(PREVOPER(nxt)) == NOTHING) /* LONGJMP */
			nxt += ARG(nxt);
		    PREVOPER(nxt)->flags = data->whilem_c
			| (PL_reg_whilem_seen << 4); /* On WHILEM */
		}
		if (data && fl & (SF_HAS_PAR|SF_IN_PAR)) 
		    pars++;
		if (flags & SCF_DO_SUBSTR) {
		    SV *last_str = Nullsv;
		    int counted = mincount != 0;

		    if (data->last_end > 0 && mincount != 0) { /* Ends with a string. */
			I32 b = pos_before >= data->last_start_min 
			    ? pos_before : data->last_start_min;
			STRLEN l;
			char *s = SvPV(data->last_found, l);
			I32 old = b - data->last_start_min;

			if (UTF)
			    old = utf8_hop((U8*)s, old) - (U8*)s;
			
			l -= old;
			/* Get the added string: */
			last_str = newSVpvn(s  + old, l);
			if (deltanext == 0 && pos_before == b) {
			    /* What was added is a constant string */
			    if (mincount > 1) {
				SvGROW(last_str, (mincount * l) + 1);
				repeatcpy(SvPVX(last_str) + l, 
					  SvPVX(last_str), l, mincount - 1);
				SvCUR(last_str) *= mincount;
				/* Add additional parts. */
				SvCUR_set(data->last_found, 
					  SvCUR(data->last_found) - l);
				sv_catsv(data->last_found, last_str);
				data->last_end += l * (mincount - 1);
			    }
			}
		    }
		    /* It is counted once already... */
		    data->pos_min += minnext * (mincount - counted);
		    data->pos_delta += - counted * deltanext +
			(minnext + deltanext) * maxcount - minnext * mincount;
		    if (mincount != maxcount) {
			 /* Cannot extend fixed substrings found inside
			    the group.  */
			scan_commit(data);
			if (mincount && last_str) {
			    sv_setsv(data->last_found, last_str);
			    data->last_end = data->pos_min;
			    data->last_start_min = 
				data->pos_min - CHR_SVLEN(last_str);
			    data->last_start_max = is_inf 
				? I32_MAX 
				: data->pos_min + data->pos_delta
				- CHR_SVLEN(last_str);
			}
			data->longest = &(data->longest_float);
		    }
		    SvREFCNT_dec(last_str);
		}
		if (data && (fl & SF_HAS_EVAL))
		    data->flags |= SF_HAS_EVAL;
	      optimize_curly_tail:
		if (OP(oscan) != CURLYX) {
		    while (PL_regkind[(U8)OP(next = regnext(oscan))] == NOTHING
			   && NEXT_OFF(next))
			NEXT_OFF(oscan) += NEXT_OFF(next);
		}
		continue;
	    default:			/* REF and CLUMP only? */
		if (flags & SCF_DO_SUBSTR) {
		    scan_commit(data);	/* Cannot expect anything... */
		    data->longest = &(data->longest_float);
		}
		is_inf = is_inf_internal = 1;
		if (flags & SCF_DO_STCLASS_OR)
		    cl_anything(data->start_class);
		flags &= ~SCF_DO_STCLASS;
		break;
	    }
	}
	else if (strchr((char*)PL_simple,OP(scan)) || PL_regkind[(U8)OP(scan)] == ANYUTF8) {
	    int value;

	    if (flags & SCF_DO_SUBSTR) {
		scan_commit(data);
		data->pos_min++;
	    }
	    min++;
	    if (flags & SCF_DO_STCLASS) {
		data->start_class->flags &= ~ANYOF_EOS;	/* No match on empty */

		/* Some of the logic below assumes that switching
		   locale on will only add false positives. */
		switch (PL_regkind[(U8)OP(scan)]) {
		case ANYUTF8:
		case SANY:
		case SANYUTF8:
		case ALNUMUTF8:
		case ANYOFUTF8:
		case ALNUMLUTF8:
		case NALNUMUTF8:
		case NALNUMLUTF8:
		case SPACEUTF8:
		case NSPACEUTF8:
		case SPACELUTF8:
		case NSPACELUTF8:
		case DIGITUTF8:
		case NDIGITUTF8:
		default:
		  do_default:
		    /* Perl_croak(aTHX_ "panic: unexpected simple REx opcode %d", OP(scan)); */
		    if (flags & SCF_DO_STCLASS_OR) /* Allow everything */
			cl_anything(data->start_class);
		    break;
		case REG_ANY:
		    if (OP(scan) == SANY)
			goto do_default;
		    if (flags & SCF_DO_STCLASS_OR) { /* Everything but \n */
			value = (ANYOF_BITMAP_TEST(data->start_class,'\n')
				 || (data->start_class->flags & ANYOF_CLASS));
			cl_anything(data->start_class);
		    }
		    if (flags & SCF_DO_STCLASS_AND || !value)
			ANYOF_BITMAP_CLEAR(data->start_class,'\n');
		    break;
		case ANYOF:
		    if (flags & SCF_DO_STCLASS_AND)
			cl_and(data->start_class,
			       (struct regnode_charclass_class*)scan);
		    else
			cl_or(data->start_class,
			      (struct regnode_charclass_class*)scan);
		    break;
		case ALNUM:
		    if (flags & SCF_DO_STCLASS_AND) {
			if (!(data->start_class->flags & ANYOF_LOCALE)) {
			    ANYOF_CLASS_CLEAR(data->start_class,ANYOF_NALNUM);
			    for (value = 0; value < 256; value++)
				if (!isALNUM(value))
				    ANYOF_BITMAP_CLEAR(data->start_class, value);
			}
		    }
		    else {
			if (data->start_class->flags & ANYOF_LOCALE)
			    ANYOF_CLASS_SET(data->start_class,ANYOF_ALNUM);
			else {
			    for (value = 0; value < 256; value++)
				if (isALNUM(value))
				    ANYOF_BITMAP_SET(data->start_class, value);			    
			}
		    }
		    break;
		case ALNUML:
		    if (flags & SCF_DO_STCLASS_AND) {
			if (data->start_class->flags & ANYOF_LOCALE)
			    ANYOF_CLASS_CLEAR(data->start_class,ANYOF_NALNUM);
		    }
		    else {
			ANYOF_CLASS_SET(data->start_class,ANYOF_ALNUM);
			data->start_class->flags |= ANYOF_LOCALE;
		    }
		    break;
		case NALNUM:
		    if (flags & SCF_DO_STCLASS_AND) {
			if (!(data->start_class->flags & ANYOF_LOCALE)) {
			    ANYOF_CLASS_CLEAR(data->start_class,ANYOF_ALNUM);
			    for (value = 0; value < 256; value++)
				if (isALNUM(value))
				    ANYOF_BITMAP_CLEAR(data->start_class, value);
			}
		    }
		    else {
			if (data->start_class->flags & ANYOF_LOCALE)
			    ANYOF_CLASS_SET(data->start_class,ANYOF_NALNUM);
			else {
			    for (value = 0; value < 256; value++)
				if (!isALNUM(value))
				    ANYOF_BITMAP_SET(data->start_class, value);			    
			}
		    }
		    break;
		case NALNUML:
		    if (flags & SCF_DO_STCLASS_AND) {
			if (data->start_class->flags & ANYOF_LOCALE)
			    ANYOF_CLASS_CLEAR(data->start_class,ANYOF_ALNUM);
		    }
		    else {
			data->start_class->flags |= ANYOF_LOCALE;
			ANYOF_CLASS_SET(data->start_class,ANYOF_NALNUM);
		    }
		    break;
		case SPACE:
		    if (flags & SCF_DO_STCLASS_AND) {
			if (!(data->start_class->flags & ANYOF_LOCALE)) {
			    ANYOF_CLASS_CLEAR(data->start_class,ANYOF_NSPACE);
			    for (value = 0; value < 256; value++)
				if (!isSPACE(value))
				    ANYOF_BITMAP_CLEAR(data->start_class, value);
			}
		    }
		    else {
			if (data->start_class->flags & ANYOF_LOCALE)
			    ANYOF_CLASS_SET(data->start_class,ANYOF_SPACE);
			else {
			    for (value = 0; value < 256; value++)
				if (isSPACE(value))
				    ANYOF_BITMAP_SET(data->start_class, value);			    
			}
		    }
		    break;
		case SPACEL:
		    if (flags & SCF_DO_STCLASS_AND) {
			if (data->start_class->flags & ANYOF_LOCALE)
			    ANYOF_CLASS_CLEAR(data->start_class,ANYOF_NSPACE);
		    }
		    else {
			data->start_class->flags |= ANYOF_LOCALE;
			ANYOF_CLASS_SET(data->start_class,ANYOF_SPACE);
		    }
		    break;
		case NSPACE:
		    if (flags & SCF_DO_STCLASS_AND) {
			if (!(data->start_class->flags & ANYOF_LOCALE)) {
			    ANYOF_CLASS_CLEAR(data->start_class,ANYOF_SPACE);
			    for (value = 0; value < 256; value++)
				if (isSPACE(value))
				    ANYOF_BITMAP_CLEAR(data->start_class, value);
			}
		    }
		    else {
			if (data->start_class->flags & ANYOF_LOCALE)
			    ANYOF_CLASS_SET(data->start_class,ANYOF_NSPACE);
			else {
			    for (value = 0; value < 256; value++)
				if (!isSPACE(value))
				    ANYOF_BITMAP_SET(data->start_class, value);			    
			}
		    }
		    break;
		case NSPACEL:
		    if (flags & SCF_DO_STCLASS_AND) {
			if (data->start_class->flags & ANYOF_LOCALE) {
			    ANYOF_CLASS_CLEAR(data->start_class,ANYOF_SPACE);
			    for (value = 0; value < 256; value++)
				if (!isSPACE(value))
				    ANYOF_BITMAP_CLEAR(data->start_class, value);
			}
		    }
		    else {
			data->start_class->flags |= ANYOF_LOCALE;
			ANYOF_CLASS_SET(data->start_class,ANYOF_NSPACE);
		    }
		    break;
		case DIGIT:
		    if (flags & SCF_DO_STCLASS_AND) {
			ANYOF_CLASS_CLEAR(data->start_class,ANYOF_NDIGIT);
			for (value = 0; value < 256; value++)
			    if (!isDIGIT(value))
				ANYOF_BITMAP_CLEAR(data->start_class, value);
		    }
		    else {
			if (data->start_class->flags & ANYOF_LOCALE)
			    ANYOF_CLASS_SET(data->start_class,ANYOF_DIGIT);
			else {
			    for (value = 0; value < 256; value++)
				if (isDIGIT(value))
				    ANYOF_BITMAP_SET(data->start_class, value);			    
			}
		    }
		    break;
		case NDIGIT:
		    if (flags & SCF_DO_STCLASS_AND) {
			ANYOF_CLASS_CLEAR(data->start_class,ANYOF_DIGIT);
			for (value = 0; value < 256; value++)
			    if (isDIGIT(value))
				ANYOF_BITMAP_CLEAR(data->start_class, value);
		    }
		    else {
			if (data->start_class->flags & ANYOF_LOCALE)
			    ANYOF_CLASS_SET(data->start_class,ANYOF_NDIGIT);
			else {
			    for (value = 0; value < 256; value++)
				if (!isDIGIT(value))
				    ANYOF_BITMAP_SET(data->start_class, value);			    
			}
		    }
		    break;
		}
		if (flags & SCF_DO_STCLASS_OR)
		    cl_and(data->start_class, &and_with);
		flags &= ~SCF_DO_STCLASS;
	    }
	}
	else if (PL_regkind[(U8)OP(scan)] == EOL && flags & SCF_DO_SUBSTR) {
	    data->flags |= (OP(scan) == MEOL
			    ? SF_BEFORE_MEOL
			    : SF_BEFORE_SEOL);
	}
	else if (  PL_regkind[(U8)OP(scan)] == BRANCHJ
		 /* Lookbehind, or need to calculate parens/evals/stclass: */
		   && (scan->flags || data || (flags & SCF_DO_STCLASS))
		   && (OP(scan) == IFMATCH || OP(scan) == UNLESSM)) {
	    /* Lookahead/lookbehind */
	    I32 deltanext, minnext;
	    regnode *nscan;
	    struct regnode_charclass_class intrnl;
	    int f = 0;

	    data_fake.flags = 0;
	    if (data)
		data_fake.whilem_c = data->whilem_c;
	    if ( flags & SCF_DO_STCLASS && !scan->flags
		 && OP(scan) == IFMATCH ) { /* Lookahead */
		cl_init(&intrnl);
		data_fake.start_class = &intrnl;
		f = SCF_DO_STCLASS_AND;
	    }
	    next = regnext(scan);
	    nscan = NEXTOPER(NEXTOPER(scan));
	    minnext = study_chunk(&nscan, &deltanext, last, &data_fake, f);
	    if (scan->flags) {
		if (deltanext) {
		    FAIL("variable length lookbehind not implemented");
		}
		else if (minnext > U8_MAX) {
		    FAIL2("lookbehind longer than %"UVuf" not implemented", (UV)U8_MAX);
		}
		scan->flags = minnext;
	    }
	    if (data && data_fake.flags & (SF_HAS_PAR|SF_IN_PAR))
		pars++;
	    if (data && (data_fake.flags & SF_HAS_EVAL))
		data->flags |= SF_HAS_EVAL;
	    if (data)
		data->whilem_c = data_fake.whilem_c;
	    if (f) {
		int was = (data->start_class->flags & ANYOF_EOS);

		cl_and(data->start_class, &intrnl);
		if (was)
		    data->start_class->flags |= ANYOF_EOS;
	    }
	}
	else if (OP(scan) == OPEN) {
	    pars++;
	}
	else if (OP(scan) == CLOSE && ARG(scan) == is_par) {
	    next = regnext(scan);

	    if ( next && (OP(next) != WHILEM) && next < last)
		is_par = 0;		/* Disable optimization */
	}
	else if (OP(scan) == EVAL) {
		if (data)
		    data->flags |= SF_HAS_EVAL;
	}
	else if (OP(scan) == LOGICAL && scan->flags == 2) { /* Embedded follows */
		if (flags & SCF_DO_SUBSTR) {
		    scan_commit(data);
		    data->longest = &(data->longest_float);
		}
		is_inf = is_inf_internal = 1;
		if (flags & SCF_DO_STCLASS_OR) /* Allow everything */
		    cl_anything(data->start_class);
		flags &= ~SCF_DO_STCLASS;
	}
	/* Else: zero-length, ignore. */
	scan = regnext(scan);
    }

  finish:
    *scanp = scan;
    *deltap = is_inf_internal ? I32_MAX : delta;
    if (flags & SCF_DO_SUBSTR && is_inf) 
	data->pos_delta = I32_MAX - data->pos_min;
    if (is_par > U8_MAX)
	is_par = 0;
    if (is_par && pars==1 && data) {
	data->flags |= SF_IN_PAR;
	data->flags &= ~SF_HAS_PAR;
    }
    else if (pars && data) {
	data->flags |= SF_HAS_PAR;
	data->flags &= ~SF_IN_PAR;
    }
    if (flags & SCF_DO_STCLASS_OR)
	cl_and(data->start_class, &and_with);
    return min;
}

STATIC I32
S_add_data(pTHX_ I32 n, char *s)
{
    dTHR;
    if (PL_regcomp_rx->data) {
	Renewc(PL_regcomp_rx->data, 
	       sizeof(*PL_regcomp_rx->data) + sizeof(void*) * (PL_regcomp_rx->data->count + n - 1), 
	       char, struct reg_data);
	Renew(PL_regcomp_rx->data->what, PL_regcomp_rx->data->count + n, U8);
	PL_regcomp_rx->data->count += n;
    }
    else {
	Newc(1207, PL_regcomp_rx->data, sizeof(*PL_regcomp_rx->data) + sizeof(void*) * (n - 1),
	     char, struct reg_data);
	New(1208, PL_regcomp_rx->data->what, n, U8);
	PL_regcomp_rx->data->count = n;
    }
    Copy(s, PL_regcomp_rx->data->what + PL_regcomp_rx->data->count - n, n, U8);
    return PL_regcomp_rx->data->count - n;
}

void
Perl_reginitcolors(pTHX)
{
    dTHR;
    int i = 0;
    char *s = PerlEnv_getenv("PERL_RE_COLORS");
	    
    if (s) {
	PL_colors[0] = s = savepv(s);
	while (++i < 6) {
	    s = strchr(s, '\t');
	    if (s) {
		*s = '\0';
		PL_colors[i] = ++s;
	    }
	    else
		PL_colors[i] = s = "";
	}
    } else {
	while (i < 6) 
	    PL_colors[i++] = "";
    }
    PL_colorset = 1;
}

/*
 - pregcomp - compile a regular expression into internal code
 *
 * We can't allocate space until we know how big the compiled form will be,
 * but we can't compile it (and thus know how big it is) until we've got a
 * place to put the code.  So we cheat:  we compile it twice, once with code
 * generation turned off and size counting turned on, and once "for real".
 * This also means that we don't allocate space until we are sure that the
 * thing really will compile successfully, and we never have to move the
 * code and thus invalidate pointers into it.  (Note that it has to be in
 * one piece because free() must be able to free it all.) [NB: not true in perl]
 *
 * Beware that the optimization-preparation code in here knows about some
 * of the structure of the compiled regexp.  [I'll say.]
 */
regexp *
Perl_pregcomp(pTHX_ char *exp, char *xend, PMOP *pm)
{
    dTHR;
    register regexp *r;
    regnode *scan;
    regnode *first;
    I32 flags;
    I32 minlen = 0;
    I32 sawplus = 0;
    I32 sawopen = 0;
    scan_data_t data;

    if (exp == NULL)
	FAIL("NULL regexp argument");

    if (pm->op_pmdynflags & PMdf_UTF8) {
	PL_reg_flags |= RF_utf8;
    }
    else
	PL_reg_flags = 0;

    PL_regprecomp = savepvn(exp, xend - exp);
    DEBUG_r(if (!PL_colorset) reginitcolors());
    DEBUG_r(PerlIO_printf(Perl_debug_log, "%sCompiling REx%s `%s%*s%s'\n",
		      PL_colors[4],PL_colors[5],PL_colors[0],
		      (int)(xend - exp), PL_regprecomp, PL_colors[1]));
    PL_regflags = pm->op_pmflags;
    PL_regsawback = 0;

    PL_regseen = 0;
    PL_seen_zerolen = *exp == '^' ? -1 : 0;
    PL_seen_evals = 0;
    PL_extralen = 0;

    /* First pass: determine size, legality. */
    PL_regcomp_parse = exp;
    PL_regxend = xend;
    PL_regnaughty = 0;
    PL_regnpar = 1;
    PL_regsize = 0L;
    PL_regcode = &PL_regdummy;
    PL_reg_whilem_seen = 0;
#if 0 /* REGC() is (currently) a NOP at the first pass.
       * Clever compilers notice this and complain. --jhi */
    REGC((U8)REG_MAGIC, (char*)PL_regcode);
#endif
    if (reg(0, &flags) == NULL) {
	Safefree(PL_regprecomp);
	PL_regprecomp = Nullch;
	return(NULL);
    }
    DEBUG_r(PerlIO_printf(Perl_debug_log, "size %"IVdf" ", (IV)PL_regsize));

    /* Small enough for pointer-storage convention?
       If extralen==0, this means that we will not need long jumps. */
    if (PL_regsize >= 0x10000L && PL_extralen)
        PL_regsize += PL_extralen;
    else
	PL_extralen = 0;
    if (PL_reg_whilem_seen > 15)
	PL_reg_whilem_seen = 15;

    /* Allocate space and initialize. */
    Newc(1001, r, sizeof(regexp) + (unsigned)PL_regsize * sizeof(regnode),
	 char, regexp);
    if (r == NULL)
	FAIL("regexp out of space");
#ifdef DEBUGGING
    /* avoid reading uninitialized memory in DEBUGGING code in study_chunk() */
    Zero(r, sizeof(regexp) + (unsigned)PL_regsize * sizeof(regnode), char);
#endif
    r->refcnt = 1;
    r->prelen = xend - exp;
    r->precomp = PL_regprecomp;
    r->subbeg = NULL;
    r->reganch = pm->op_pmflags & PMf_COMPILETIME;
    r->nparens = PL_regnpar - 1;	/* set early to validate backrefs */

    r->substrs = 0;			/* Useful during FAIL. */
    r->startp = 0;			/* Useful during FAIL. */
    r->endp = 0;			/* Useful during FAIL. */

    PL_regcomp_rx = r;

    /* Second pass: emit code. */
    PL_regcomp_parse = exp;
    PL_regxend = xend;
    PL_regnaughty = 0;
    PL_regnpar = 1;
    PL_regcode = r->program;
    /* Store the count of eval-groups for security checks: */
    PL_regcode->next_off = ((PL_seen_evals > U16_MAX) ? U16_MAX : PL_seen_evals);
    REGC((U8)REG_MAGIC, (char*) PL_regcode++);
    r->data = 0;
    if (reg(0, &flags) == NULL)
	return(NULL);

    /* Dig out information for optimizations. */
    r->reganch = pm->op_pmflags & PMf_COMPILETIME; /* Again? */
    pm->op_pmflags = PL_regflags;
    if (UTF)
	r->reganch |= ROPT_UTF8;
    r->regstclass = NULL;
    if (PL_regnaughty >= 10)	/* Probably an expensive pattern. */
	r->reganch |= ROPT_NAUGHTY;
    scan = r->program + 1;		/* First BRANCH. */

    /* XXXX To minimize changes to RE engine we always allocate
       3-units-long substrs field. */
    Newz(1004, r->substrs, 1, struct reg_substr_data);

    StructCopy(&zero_scan_data, &data, scan_data_t);
    /* XXXX Should not we check for something else?  Usually it is OPEN1... */
    if (OP(scan) != BRANCH) {	/* Only one top-level choice. */
	I32 fake;
	STRLEN longest_float_length, longest_fixed_length;
	struct regnode_charclass_class ch_class;
	int stclass_flag;

	first = scan;
	/* Skip introductions and multiplicators >= 1. */
	while ((OP(first) == OPEN && (sawopen = 1)) ||
	       /* An OR of *one* alternative - should not happen now. */
	    (OP(first) == BRANCH && OP(regnext(first)) != BRANCH) ||
	    (OP(first) == PLUS) ||
	    (OP(first) == MINMOD) ||
	       /* An {n,m} with n>0 */
	    (PL_regkind[(U8)OP(first)] == CURLY && ARG1(first) > 0) ) {
		if (OP(first) == PLUS)
		    sawplus = 1;
		else
		    first += regarglen[(U8)OP(first)];
		first = NEXTOPER(first);
	}

	/* Starting-point info. */
      again:
	if (PL_regkind[(U8)OP(first)] == EXACT) {
	    if (OP(first) == EXACT);	/* Empty, get anchored substr later. */
	    else if ((OP(first) == EXACTF || OP(first) == EXACTFL)
		     && !UTF)
		r->regstclass = first;
	}
	else if (strchr((char*)PL_simple,OP(first)))
	    r->regstclass = first;
	else if (PL_regkind[(U8)OP(first)] == BOUND ||
		 PL_regkind[(U8)OP(first)] == NBOUND)
	    r->regstclass = first;
	else if (PL_regkind[(U8)OP(first)] == BOL) {
	    r->reganch |= (OP(first) == MBOL
			   ? ROPT_ANCH_MBOL
			   : (OP(first) == SBOL
			      ? ROPT_ANCH_SBOL
			      : ROPT_ANCH_BOL));
	    first = NEXTOPER(first);
	    goto again;
	}
	else if (OP(first) == GPOS) {
	    r->reganch |= ROPT_ANCH_GPOS;
	    first = NEXTOPER(first);
	    goto again;
	}
	else if ((OP(first) == STAR &&
	    PL_regkind[(U8)OP(NEXTOPER(first))] == REG_ANY) &&
	    !(r->reganch & ROPT_ANCH) )
	{
	    /* turn .* into ^.* with an implied $*=1 */
	    int type = OP(NEXTOPER(first));

	    if (type == REG_ANY || type == ANYUTF8)
		type = ROPT_ANCH_MBOL;
	    else
		type = ROPT_ANCH_SBOL;

	    r->reganch |= type | ROPT_IMPLICIT;
	    first = NEXTOPER(first);
	    goto again;
	}
	if (sawplus && (!sawopen || !PL_regsawback) 
	    && !(PL_regseen & REG_SEEN_EVAL)) /* May examine pos and $& */
	    /* x+ must match at the 1st pos of run of x's */
	    r->reganch |= ROPT_SKIP;

	/* Scan is after the zeroth branch, first is atomic matcher. */
	DEBUG_r(PerlIO_printf(Perl_debug_log, "first at %"IVdf"\n", 
			      (IV)(first - scan + 1)));
	/*
	* If there's something expensive in the r.e., find the
	* longest literal string that must appear and make it the
	* regmust.  Resolve ties in favor of later strings, since
	* the regstart check works with the beginning of the r.e.
	* and avoiding duplication strengthens checking.  Not a
	* strong reason, but sufficient in the absence of others.
	* [Now we resolve ties in favor of the earlier string if
	* it happens that c_offset_min has been invalidated, since the
	* earlier string may buy us something the later one won't.]
	*/
	minlen = 0;

	data.longest_fixed = newSVpvn("",0);
	data.longest_float = newSVpvn("",0);
	data.last_found = newSVpvn("",0);
	data.longest = &(data.longest_fixed);
	first = scan;
	if (!r->regstclass) {
	    cl_init(&ch_class);
	    data.start_class = &ch_class;
	    stclass_flag = SCF_DO_STCLASS_AND;
	} else				/* XXXX Check for BOUND? */
	    stclass_flag = 0;

	minlen = study_chunk(&first, &fake, scan + PL_regsize, /* Up to end */
			     &data, SCF_DO_SUBSTR | stclass_flag);
	if ( PL_regnpar == 1 && data.longest == &(data.longest_fixed)
	     && data.last_start_min == 0 && data.last_end > 0 
	     && !PL_seen_zerolen
	     && (!(PL_regseen & REG_SEEN_GPOS) || (r->reganch & ROPT_ANCH_GPOS)))
	    r->reganch |= ROPT_CHECK_ALL;
	scan_commit(&data);
	SvREFCNT_dec(data.last_found);

	longest_float_length = CHR_SVLEN(data.longest_float);
	if (longest_float_length
	    || (data.flags & SF_FL_BEFORE_EOL
		&& (!(data.flags & SF_FL_BEFORE_MEOL)
		    || (PL_regflags & PMf_MULTILINE)))) {
	    int t;

	    if (SvCUR(data.longest_fixed) 			/* ok to leave SvCUR */
		&& data.offset_fixed == data.offset_float_min
		&& SvCUR(data.longest_fixed) == SvCUR(data.longest_float))
		    goto remove_float;		/* As in (a)+. */

	    r->float_substr = data.longest_float;
	    r->float_min_offset = data.offset_float_min;
	    r->float_max_offset = data.offset_float_max;
	    t = (data.flags & SF_FL_BEFORE_EOL /* Can't have SEOL and MULTI */
		       && (!(data.flags & SF_FL_BEFORE_MEOL)
			   || (PL_regflags & PMf_MULTILINE)));
	    fbm_compile(r->float_substr, t ? FBMcf_TAIL : 0);
	}
	else {
	  remove_float:
	    r->float_substr = Nullsv;
	    SvREFCNT_dec(data.longest_float);
	    longest_float_length = 0;
	}

	longest_fixed_length = CHR_SVLEN(data.longest_fixed);
	if (longest_fixed_length
	    || (data.flags & SF_FIX_BEFORE_EOL /* Cannot have SEOL and MULTI */
		&& (!(data.flags & SF_FIX_BEFORE_MEOL)
		    || (PL_regflags & PMf_MULTILINE)))) {
	    int t;

	    r->anchored_substr = data.longest_fixed;
	    r->anchored_offset = data.offset_fixed;
	    t = (data.flags & SF_FIX_BEFORE_EOL /* Can't have SEOL and MULTI */
		 && (!(data.flags & SF_FIX_BEFORE_MEOL)
		     || (PL_regflags & PMf_MULTILINE)));
	    fbm_compile(r->anchored_substr, t ? FBMcf_TAIL : 0);
	}
	else {
	    r->anchored_substr = Nullsv;
	    SvREFCNT_dec(data.longest_fixed);
	    longest_fixed_length = 0;
	}
	if (r->regstclass 
	    && (OP(r->regstclass) == REG_ANY || OP(r->regstclass) == ANYUTF8
		|| OP(r->regstclass) == SANYUTF8 || OP(r->regstclass) == SANY))
	    r->regstclass = NULL;
	if ((!r->anchored_substr || r->anchored_offset) && stclass_flag
	    && !(data.start_class->flags & ANYOF_EOS)
	    && !cl_is_anything(data.start_class)) {
	    SV *sv;
	    I32 n = add_data(1, "f");

	    New(1006, PL_regcomp_rx->data->data[n], 1, 
		struct regnode_charclass_class);
	    StructCopy(data.start_class,
		       (struct regnode_charclass_class*)PL_regcomp_rx->data->data[n],
		       struct regnode_charclass_class);
	    r->regstclass = (regnode*)PL_regcomp_rx->data->data[n];
	    r->reganch &= ~ROPT_SKIP;	/* Used in find_byclass(). */
	    DEBUG_r((sv = sv_newmortal(),
		     regprop(sv, (regnode*)data.start_class),
		     PerlIO_printf(Perl_debug_log, "synthetic stclass `%s'.\n",
				   SvPVX(sv))));
	}

	/* A temporary algorithm prefers floated substr to fixed one to dig more info. */
	if (longest_fixed_length > longest_float_length) {
	    r->check_substr = r->anchored_substr;
	    r->check_offset_min = r->check_offset_max = r->anchored_offset;
	    if (r->reganch & ROPT_ANCH_SINGLE)
		r->reganch |= ROPT_NOSCAN;
	}
	else {
	    r->check_substr = r->float_substr;
	    r->check_offset_min = data.offset_float_min;
	    r->check_offset_max = data.offset_float_max;
	}
	/* XXXX Currently intuiting is not compatible with ANCH_GPOS.
	   This should be changed ASAP!  */
	if (r->check_substr && !(r->reganch & ROPT_ANCH_GPOS)) {
	    r->reganch |= RE_USE_INTUIT;
	    if (SvTAIL(r->check_substr))
		r->reganch |= RE_INTUIT_TAIL;
	}
    }
    else {
	/* Several toplevels. Best we can is to set minlen. */
	I32 fake;
	struct regnode_charclass_class ch_class;
	
	DEBUG_r(PerlIO_printf(Perl_debug_log, "\n"));
	scan = r->program + 1;
	cl_init(&ch_class);
	data.start_class = &ch_class;
	minlen = study_chunk(&scan, &fake, scan + PL_regsize, &data, SCF_DO_STCLASS_AND);
	r->check_substr = r->anchored_substr = r->float_substr = Nullsv;
	if (!(data.start_class->flags & ANYOF_EOS)
	    && !cl_is_anything(data.start_class)) {
	    SV *sv;
	    I32 n = add_data(1, "f");

	    New(1006, PL_regcomp_rx->data->data[n], 1, 
		struct regnode_charclass_class);
	    StructCopy(data.start_class,
		       (struct regnode_charclass_class*)PL_regcomp_rx->data->data[n],
		       struct regnode_charclass_class);
	    r->regstclass = (regnode*)PL_regcomp_rx->data->data[n];
	    r->reganch &= ~ROPT_SKIP;	/* Used in find_byclass(). */
	    DEBUG_r((sv = sv_newmortal(),
		     regprop(sv, (regnode*)data.start_class),
		     PerlIO_printf(Perl_debug_log, "synthetic stclass `%s'.\n",
				   SvPVX(sv))));
	}
    }

    r->minlen = minlen;
    if (PL_regseen & REG_SEEN_GPOS) 
	r->reganch |= ROPT_GPOS_SEEN;
    if (PL_regseen & REG_SEEN_LOOKBEHIND)
	r->reganch |= ROPT_LOOKBEHIND_SEEN;
    if (PL_regseen & REG_SEEN_EVAL)
	r->reganch |= ROPT_EVAL_SEEN;
    Newz(1002, r->startp, PL_regnpar, I32);
    Newz(1002, r->endp, PL_regnpar, I32);
    DEBUG_r(regdump(r));
    return(r);
}

/*
 - reg - regular expression, i.e. main body or parenthesized thing
 *
 * Caller must absorb opening parenthesis.
 *
 * Combining parenthesis handling with the base level of regular expression
 * is a trifle forced, but the need to tie the tails of the branches to what
 * follows makes it hard to avoid.
 */
STATIC regnode *
S_reg(pTHX_ I32 paren, I32 *flagp)
    /* paren: Parenthesized? 0=top, 1=(, inside: changed to letter. */
{
    dTHR;
    register regnode *ret;		/* Will be the head of the group. */
    register regnode *br;
    register regnode *lastbr;
    register regnode *ender = 0;
    register I32 parno = 0;
    I32 flags, oregflags = PL_regflags, have_branch = 0, open = 0;
    char c;

    *flagp = 0;				/* Tentatively. */

    /* Make an OPEN node, if parenthesized. */
    if (paren) {
	if (*PL_regcomp_parse == '?') {
	    U16 posflags = 0, negflags = 0;
	    U16 *flagsp = &posflags;
	    int logical = 0;

	    PL_regcomp_parse++;
	    paren = *PL_regcomp_parse++;
	    ret = NULL;			/* For look-ahead/behind. */
	    switch (paren) {
	    case '<':
		PL_regseen |= REG_SEEN_LOOKBEHIND;
		if (*PL_regcomp_parse == '!') 
		    paren = ',';
		if (*PL_regcomp_parse != '=' && *PL_regcomp_parse != '!') 
		    goto unknown;
		PL_regcomp_parse++;
	    case '=':
	    case '!':
		PL_seen_zerolen++;
	    case ':':
	    case '>':
		break;
	    case '$':
	    case '@':
		FAIL2("Sequence (?%c...) not implemented", (int)paren);
		break;
	    case '#':
		while (*PL_regcomp_parse && *PL_regcomp_parse != ')')
		    PL_regcomp_parse++;
		if (*PL_regcomp_parse != ')')
		    FAIL("Sequence (?#... not terminated");
		nextchar();
		*flagp = TRYAGAIN;
		return NULL;
	    case 'p':
		if (SIZE_ONLY)
		    Perl_warner(aTHX_ WARN_REGEXP,
				"(?p{}) is deprecated - use (??{})");
		/* FALL THROUGH*/
	    case '?':
		logical = 1;
		paren = *PL_regcomp_parse++;
		/* FALL THROUGH */
	    case '{':
	    {
		dTHR;
		I32 count = 1, n = 0;
		char c;
		char *s = PL_regcomp_parse;
		SV *sv;
		OP_4tree *sop, *rop;

		PL_seen_zerolen++;
		PL_regseen |= REG_SEEN_EVAL;
		while (count && (c = *PL_regcomp_parse)) {
		    if (c == '\\' && PL_regcomp_parse[1])
			PL_regcomp_parse++;
		    else if (c == '{') 
			count++;
		    else if (c == '}') 
			count--;
		    PL_regcomp_parse++;
		}
		if (*PL_regcomp_parse != ')')
		    FAIL("Sequence (?{...}) not terminated or not {}-balanced");
		if (!SIZE_ONLY) {
		    AV *av;
		    
		    if (PL_regcomp_parse - 1 - s) 
			sv = newSVpvn(s, PL_regcomp_parse - 1 - s);
		    else
			sv = newSVpvn("", 0);

		    rop = sv_compile_2op(sv, &sop, "re", &av);

		    n = add_data(3, "nop");
		    PL_regcomp_rx->data->data[n] = (void*)rop;
		    PL_regcomp_rx->data->data[n+1] = (void*)sop;
		    PL_regcomp_rx->data->data[n+2] = (void*)av;
		    SvREFCNT_dec(sv);
		}
		else {						/* First pass */
		    if (PL_reginterp_cnt < ++PL_seen_evals
			&& PL_curcop != &PL_compiling)
			/* No compiled RE interpolated, has runtime
			   components ===> unsafe.  */
			FAIL("Eval-group not allowed at runtime, use re 'eval'");
		    if (PL_tainted)
			FAIL("Eval-group in insecure regular expression");
		}
		
		nextchar();
		if (logical) {
		    ret = reg_node(LOGICAL);
		    if (!SIZE_ONLY)
			ret->flags = 2;
		    regtail(ret, reganode(EVAL, n));
		    return ret;
		}
		return reganode(EVAL, n);
	    }
	    case '(':
	    {
		if (PL_regcomp_parse[0] == '?') {
		    if (PL_regcomp_parse[1] == '=' || PL_regcomp_parse[1] == '!' 
			|| PL_regcomp_parse[1] == '<' 
			|| PL_regcomp_parse[1] == '{') { /* Lookahead or eval. */
			I32 flag;
			
			ret = reg_node(LOGICAL);
			if (!SIZE_ONLY)
			    ret->flags = 1;
			regtail(ret, reg(1, &flag));
			goto insert_if;
		    } 
		}
		else if (PL_regcomp_parse[0] >= '1' && PL_regcomp_parse[0] <= '9' ) {
		    parno = atoi(PL_regcomp_parse++);

		    while (isDIGIT(*PL_regcomp_parse))
			PL_regcomp_parse++;
		    ret = reganode(GROUPP, parno);
		    if ((c = *nextchar()) != ')')
			FAIL2("Switch (?(number%c not recognized", c);
		  insert_if:
		    regtail(ret, reganode(IFTHEN, 0));
		    br = regbranch(&flags, 1);
		    if (br == NULL)
			br = reganode(LONGJMP, 0);
		    else
			regtail(br, reganode(LONGJMP, 0));
		    c = *nextchar();
		    if (flags&HASWIDTH)
			*flagp |= HASWIDTH;
		    if (c == '|') {
			lastbr = reganode(IFTHEN, 0); /* Fake one for optimizer. */
			regbranch(&flags, 1);
			regtail(ret, lastbr);
		 	if (flags&HASWIDTH)
			    *flagp |= HASWIDTH;
			c = *nextchar();
		    }
		    else
			lastbr = NULL;
		    if (c != ')')
			FAIL("Switch (?(condition)... contains too many branches");
		    ender = reg_node(TAIL);
		    regtail(br, ender);
		    if (lastbr) {
			regtail(lastbr, ender);
			regtail(NEXTOPER(NEXTOPER(lastbr)), ender);
		    }
		    else
			regtail(ret, ender);
		    return ret;
		}
		else {
		    FAIL2("Unknown condition for (?(%.2s", PL_regcomp_parse);
		}
	    }
            case 0:
                FAIL("Sequence (? incomplete");
                break;
	    default:
		--PL_regcomp_parse;
	      parse_flags:
		while (*PL_regcomp_parse && strchr("iogcmsx", *PL_regcomp_parse)) {
		    if (*PL_regcomp_parse != 'o')
			pmflag(flagsp, *PL_regcomp_parse);
		    ++PL_regcomp_parse;
		}
		if (*PL_regcomp_parse == '-') {
		    flagsp = &negflags;
		    ++PL_regcomp_parse;
		    goto parse_flags;
		}
		PL_regflags |= posflags;
		PL_regflags &= ~negflags;
		if (*PL_regcomp_parse == ':') {
		    PL_regcomp_parse++;
		    paren = ':';
		    break;
		}		
	      unknown:
		if (*PL_regcomp_parse != ')')
		    FAIL2("Sequence (?%c...) not recognized", *PL_regcomp_parse);
		nextchar();
		*flagp = TRYAGAIN;
		return NULL;
	    }
	}
	else {
	    parno = PL_regnpar;
	    PL_regnpar++;
	    ret = reganode(OPEN, parno);
	    open = 1;
	}
    }
    else
	ret = NULL;

    /* Pick up the branches, linking them together. */
    br = regbranch(&flags, 1);
    if (br == NULL)
	return(NULL);
    if (*PL_regcomp_parse == '|') {
	if (!SIZE_ONLY && PL_extralen) {
	    reginsert(BRANCHJ, br);
	}
	else
	    reginsert(BRANCH, br);
	have_branch = 1;
	if (SIZE_ONLY)
	    PL_extralen += 1;		/* For BRANCHJ-BRANCH. */
    }
    else if (paren == ':') {
	*flagp |= flags&SIMPLE;
    }
    if (open) {				/* Starts with OPEN. */
	regtail(ret, br);		/* OPEN -> first. */
    }
    else if (paren != '?')		/* Not Conditional */
	ret = br;
    if (flags&HASWIDTH)
	*flagp |= HASWIDTH;
    *flagp |= flags&SPSTART;
    lastbr = br;
    while (*PL_regcomp_parse == '|') {
	if (!SIZE_ONLY && PL_extralen) {
	    ender = reganode(LONGJMP,0);
	    regtail(NEXTOPER(NEXTOPER(lastbr)), ender); /* Append to the previous. */
	}
	if (SIZE_ONLY)
	    PL_extralen += 2;		/* Account for LONGJMP. */
	nextchar();
	br = regbranch(&flags, 0);
	if (br == NULL)
	    return(NULL);
	regtail(lastbr, br);		/* BRANCH -> BRANCH. */
	lastbr = br;
	if (flags&HASWIDTH)
	    *flagp |= HASWIDTH;
	*flagp |= flags&SPSTART;
    }

    if (have_branch || paren != ':') {
	/* Make a closing node, and hook it on the end. */
	switch (paren) {
	case ':':
	    ender = reg_node(TAIL);
	    break;
	case 1:
	    ender = reganode(CLOSE, parno);
	    break;
	case '<':
	case ',':
	case '=':
	case '!':
	    *flagp &= ~HASWIDTH;
	    /* FALL THROUGH */
	case '>':
	    ender = reg_node(SUCCEED);
	    break;
	case 0:
	    ender = reg_node(END);
	    break;
	}
	regtail(lastbr, ender);

	if (have_branch) {
	    /* Hook the tails of the branches to the closing node. */
	    for (br = ret; br != NULL; br = regnext(br)) {
		regoptail(br, ender);
	    }
	}
    }

    {
	char *p;
	static char parens[] = "=!<,>";

	if (paren && (p = strchr(parens, paren))) {
	    int node = ((p - parens) % 2) ? UNLESSM : IFMATCH;
	    int flag = (p - parens) > 1;

	    if (paren == '>')
		node = SUSPEND, flag = 0;
	    reginsert(node,ret);
	    ret->flags = flag;
	    regtail(ret, reg_node(TAIL));
	}
    }

    /* Check for proper termination. */
    if (paren) {
	PL_regflags = oregflags;
	if (PL_regcomp_parse >= PL_regxend || *nextchar() != ')') {
	    FAIL("unmatched () in regexp");
	}
    }
    else if (!paren && PL_regcomp_parse < PL_regxend) {
	if (*PL_regcomp_parse == ')') {
	    FAIL("unmatched () in regexp");
	}
	else
	    FAIL("junk on end of regexp");	/* "Can't happen". */
	/* NOTREACHED */
    }

    return(ret);
}

/*
 - regbranch - one alternative of an | operator
 *
 * Implements the concatenation operator.
 */
STATIC regnode *
S_regbranch(pTHX_ I32 *flagp, I32 first)
{
    dTHR;
    register regnode *ret;
    register regnode *chain = NULL;
    register regnode *latest;
    I32 flags = 0, c = 0;

    if (first) 
	ret = NULL;
    else {
	if (!SIZE_ONLY && PL_extralen) 
	    ret = reganode(BRANCHJ,0);
	else
	    ret = reg_node(BRANCH);
    }
	
    if (!first && SIZE_ONLY) 
	PL_extralen += 1;			/* BRANCHJ */
    
    *flagp = WORST;			/* Tentatively. */

    PL_regcomp_parse--;
    nextchar();
    while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != '|' && *PL_regcomp_parse != ')') {
	flags &= ~TRYAGAIN;
	latest = regpiece(&flags);
	if (latest == NULL) {
	    if (flags & TRYAGAIN)
		continue;
	    return(NULL);
	}
	else if (ret == NULL)
	    ret = latest;
	*flagp |= flags&HASWIDTH;
	if (chain == NULL) 	/* First piece. */
	    *flagp |= flags&SPSTART;
	else {
	    PL_regnaughty++;
	    regtail(chain, latest);
	}
	chain = latest;
	c++;
    }
    if (chain == NULL) {	/* Loop ran zero times. */
	chain = reg_node(NOTHING);
	if (ret == NULL)
	    ret = chain;
    }
    if (c == 1) {
	*flagp |= flags&SIMPLE;
    }

    return(ret);
}

/*
 - regpiece - something followed by possible [*+?]
 *
 * Note that the branching code sequences used for ? and the general cases
 * of * and + are somewhat optimized:  they use the same NOTHING node as
 * both the endmarker for their branch list and the body of the last branch.
 * It might seem that this node could be dispensed with entirely, but the
 * endmarker role is not redundant.
 */
STATIC regnode *
S_regpiece(pTHX_ I32 *flagp)
{
    dTHR;
    register regnode *ret;
    register char op;
    register char *next;
    I32 flags;
    char *origparse = PL_regcomp_parse;
    char *maxpos;
    I32 min;
    I32 max = REG_INFTY;

    ret = regatom(&flags);
    if (ret == NULL) {
	if (flags & TRYAGAIN)
	    *flagp |= TRYAGAIN;
	return(NULL);
    }

    op = *PL_regcomp_parse;

    if (op == '{' && regcurly(PL_regcomp_parse)) {
	next = PL_regcomp_parse + 1;
	maxpos = Nullch;
	while (isDIGIT(*next) || *next == ',') {
	    if (*next == ',') {
		if (maxpos)
		    break;
		else
		    maxpos = next;
	    }
	    next++;
	}
	if (*next == '}') {		/* got one */
	    if (!maxpos)
		maxpos = next;
	    PL_regcomp_parse++;
	    min = atoi(PL_regcomp_parse);
	    if (*maxpos == ',')
		maxpos++;
	    else
		maxpos = PL_regcomp_parse;
	    max = atoi(maxpos);
	    if (!max && *maxpos != '0')
		max = REG_INFTY;		/* meaning "infinity" */
	    else if (max >= REG_INFTY)
		FAIL2("Quantifier in {,} bigger than %d", REG_INFTY - 1);
	    PL_regcomp_parse = next;
	    nextchar();

	do_curly:
	    if ((flags&SIMPLE)) {
		PL_regnaughty += 2 + PL_regnaughty / 2;
		reginsert(CURLY, ret);
	    }
	    else {
		regnode *w = reg_node(WHILEM);

		w->flags = 0;
		regtail(ret, w);
		if (!SIZE_ONLY && PL_extralen) {
		    reginsert(LONGJMP,ret);
		    reginsert(NOTHING,ret);
		    NEXT_OFF(ret) = 3;	/* Go over LONGJMP. */
		}
		reginsert(CURLYX,ret);
		if (!SIZE_ONLY && PL_extralen)
		    NEXT_OFF(ret) = 3;	/* Go over NOTHING to LONGJMP. */
		regtail(ret, reg_node(NOTHING));
		if (SIZE_ONLY)
		    PL_reg_whilem_seen++, PL_extralen += 3;
		PL_regnaughty += 4 + PL_regnaughty;	/* compound interest */
	    }
	    ret->flags = 0;

	    if (min > 0)
		*flagp = WORST;
	    if (max > 0)
		*flagp |= HASWIDTH;
	    if (max && max < min)
		FAIL("Can't do {n,m} with n > m");
	    if (!SIZE_ONLY) {
		ARG1_SET(ret, min);
		ARG2_SET(ret, max);
	    }

	    goto nest_check;
	}
    }

    if (!ISMULT1(op)) {
	*flagp = flags;
	return(ret);
    }

#if 0				/* Now runtime fix should be reliable. */
    if (!(flags&HASWIDTH) && op != '?')
      FAIL("regexp *+ operand could be empty");
#endif 

    nextchar();

    *flagp = (op != '+') ? (WORST|SPSTART|HASWIDTH) : (WORST|HASWIDTH);

    if (op == '*' && (flags&SIMPLE)) {
	reginsert(STAR, ret);
	ret->flags = 0;
	PL_regnaughty += 4;
    }
    else if (op == '*') {
	min = 0;
	goto do_curly;
    }
    else if (op == '+' && (flags&SIMPLE)) {
	reginsert(PLUS, ret);
	ret->flags = 0;
	PL_regnaughty += 3;
    }
    else if (op == '+') {
	min = 1;
	goto do_curly;
    }
    else if (op == '?') {
	min = 0; max = 1;
	goto do_curly;
    }
  nest_check:
    if (ckWARN(WARN_REGEXP) && !SIZE_ONLY && !(flags&HASWIDTH) && max > REG_INFTY/3) {
	Perl_warner(aTHX_ WARN_REGEXP, "%.*s matches null string many times",
	    PL_regcomp_parse - origparse, origparse);
    }

    if (*PL_regcomp_parse == '?') {
	nextchar();
	reginsert(MINMOD, ret);
	regtail(ret, ret + NODE_STEP_REGNODE);
    }
    if (ISMULT2(PL_regcomp_parse))
	FAIL("nested *?+ in regexp");

    return(ret);
}

/*
 - regatom - the lowest level
 *
 * Optimization:  gobbles an entire sequence of ordinary characters so that
 * it can turn them into a single node, which is smaller to store and
 * faster to run.  Backslashed characters are exceptions, each becoming a
 * separate node; the code is simpler that way and it's not worth fixing.
 *
 * [Yes, it is worth fixing, some scripts can run twice the speed.]
 */
STATIC regnode *
S_regatom(pTHX_ I32 *flagp)
{
    dTHR;
    register regnode *ret = 0;
    I32 flags;

    *flagp = WORST;		/* Tentatively. */

tryagain:
    switch (*PL_regcomp_parse) {
    case '^':
	PL_seen_zerolen++;
	nextchar();
	if (PL_regflags & PMf_MULTILINE)
	    ret = reg_node(MBOL);
	else if (PL_regflags & PMf_SINGLELINE)
	    ret = reg_node(SBOL);
	else
	    ret = reg_node(BOL);
	break;
    case '$':
	if (PL_regcomp_parse[1]) 
	    PL_seen_zerolen++;
	nextchar();
	if (PL_regflags & PMf_MULTILINE)
	    ret = reg_node(MEOL);
	else if (PL_regflags & PMf_SINGLELINE)
	    ret = reg_node(SEOL);
	else
	    ret = reg_node(EOL);
	break;
    case '.':
	nextchar();
	if (UTF) {
	    if (PL_regflags & PMf_SINGLELINE)
		ret = reg_node(SANYUTF8);
	    else
		ret = reg_node(ANYUTF8);
	    *flagp |= HASWIDTH;
	}
	else {
	    if (PL_regflags & PMf_SINGLELINE)
		ret = reg_node(SANY);
	    else
		ret = reg_node(REG_ANY);
	    *flagp |= HASWIDTH|SIMPLE;
	}
	PL_regnaughty++;
	break;
    case '[':
	PL_regcomp_parse++;
	ret = (UTF ? regclassutf8() : regclass());
	if (*PL_regcomp_parse != ']')
	    FAIL("unmatched [] in regexp");
	nextchar();
	*flagp |= HASWIDTH|SIMPLE;
	break;
    case '(':
	nextchar();
	ret = reg(1, &flags);
	if (ret == NULL) {
		if (flags & TRYAGAIN)
		    goto tryagain;
		return(NULL);
	}
	*flagp |= flags&(HASWIDTH|SPSTART|SIMPLE);
	break;
    case '|':
    case ')':
	if (flags & TRYAGAIN) {
	    *flagp |= TRYAGAIN;
	    return NULL;
	}
	FAIL2("internal urp in regexp at /%s/", PL_regcomp_parse);
				/* Supposed to be caught earlier. */
	break;
    case '{':
	if (!regcurly(PL_regcomp_parse)) {
	    PL_regcomp_parse++;
	    goto defchar;
	}
	/* FALL THROUGH */
    case '?':
    case '+':
    case '*':
	FAIL("?+*{} follows nothing in regexp");
	break;
    case '\\':
	switch (*++PL_regcomp_parse) {
	case 'A':
	    PL_seen_zerolen++;
	    ret = reg_node(SBOL);
	    *flagp |= SIMPLE;
	    nextchar();
	    break;
	case 'G':
	    ret = reg_node(GPOS);
	    PL_regseen |= REG_SEEN_GPOS;
	    *flagp |= SIMPLE;
	    nextchar();
	    break;
	case 'Z':
	    ret = reg_node(SEOL);
	    *flagp |= SIMPLE;
	    nextchar();
	    break;
	case 'z':
	    ret = reg_node(EOS);
	    *flagp |= SIMPLE;
	    PL_seen_zerolen++;		/* Do not optimize RE away */
	    nextchar();
	    break;
	case 'C':
	    ret = reg_node(SANY);
	    *flagp |= HASWIDTH|SIMPLE;
	    nextchar();
	    break;
	case 'X':
	    ret = reg_node(CLUMP);
	    *flagp |= HASWIDTH;
	    nextchar();
	    if (UTF && !PL_utf8_mark)
		is_utf8_mark((U8*)"~");		/* preload table */
	    break;
	case 'w':
	    ret = reg_node(
		UTF
		    ? (LOC ? ALNUMLUTF8 : ALNUMUTF8)
		    : (LOC ? ALNUML     : ALNUM));
	    *flagp |= HASWIDTH|SIMPLE;
	    nextchar();
	    if (UTF && !PL_utf8_alnum)
		is_utf8_alnum((U8*)"a");	/* preload table */
	    break;
	case 'W':
	    ret = reg_node(
		UTF
		    ? (LOC ? NALNUMLUTF8 : NALNUMUTF8)
		    : (LOC ? NALNUML     : NALNUM));
	    *flagp |= HASWIDTH|SIMPLE;
	    nextchar();
	    if (UTF && !PL_utf8_alnum)
		is_utf8_alnum((U8*)"a");	/* preload table */
	    break;
	case 'b':
	    PL_seen_zerolen++;
	    PL_regseen |= REG_SEEN_LOOKBEHIND;
	    ret = reg_node(
		UTF
		    ? (LOC ? BOUNDLUTF8 : BOUNDUTF8)
		    : (LOC ? BOUNDL     : BOUND));
	    *flagp |= SIMPLE;
	    nextchar();
	    if (UTF && !PL_utf8_alnum)
		is_utf8_alnum((U8*)"a");	/* preload table */
	    break;
	case 'B':
	    PL_seen_zerolen++;
	    PL_regseen |= REG_SEEN_LOOKBEHIND;
	    ret = reg_node(
		UTF
		    ? (LOC ? NBOUNDLUTF8 : NBOUNDUTF8)
		    : (LOC ? NBOUNDL     : NBOUND));
	    *flagp |= SIMPLE;
	    nextchar();
	    if (UTF && !PL_utf8_alnum)
		is_utf8_alnum((U8*)"a");	/* preload table */
	    break;
	case 's':
	    ret = reg_node(
		UTF
		    ? (LOC ? SPACELUTF8 : SPACEUTF8)
		    : (LOC ? SPACEL     : SPACE));
	    *flagp |= HASWIDTH|SIMPLE;
	    nextchar();
	    if (UTF && !PL_utf8_space)
		is_utf8_space((U8*)" ");	/* preload table */
	    break;
	case 'S':
	    ret = reg_node(
		UTF
		    ? (LOC ? NSPACELUTF8 : NSPACEUTF8)
		    : (LOC ? NSPACEL     : NSPACE));
	    *flagp |= HASWIDTH|SIMPLE;
	    nextchar();
	    if (UTF && !PL_utf8_space)
		is_utf8_space((U8*)" ");	/* preload table */
	    break;
	case 'd':
	    ret = reg_node(UTF ? DIGITUTF8 : DIGIT);
	    *flagp |= HASWIDTH|SIMPLE;
	    nextchar();
	    if (UTF && !PL_utf8_digit)
		is_utf8_digit((U8*)"1");	/* preload table */
	    break;
	case 'D':
	    ret = reg_node(UTF ? NDIGITUTF8 : NDIGIT);
	    *flagp |= HASWIDTH|SIMPLE;
	    nextchar();
	    if (UTF && !PL_utf8_digit)
		is_utf8_digit((U8*)"1");	/* preload table */
	    break;
	case 'p':
	case 'P':
	    {	/* a lovely hack--pretend we saw [\pX] instead */
		char* oldregxend = PL_regxend;

		if (PL_regcomp_parse[1] == '{') {
		    PL_regxend = strchr(PL_regcomp_parse, '}');
		    if (!PL_regxend)
			FAIL("Missing right brace on \\p{}");
		    PL_regxend++;
		}
		else
		    PL_regxend = PL_regcomp_parse + 2;
		PL_regcomp_parse--;

		ret = regclassutf8();

		PL_regxend = oldregxend;
		PL_regcomp_parse--;
		nextchar();
		*flagp |= HASWIDTH|SIMPLE;
	    }
	    break;
	case 'n':
	case 'r':
	case 't':
	case 'f':
	case 'e':
	case 'a':
	case 'x':
	case 'c':
	case '0':
	    goto defchar;
	case '1': case '2': case '3': case '4':
	case '5': case '6': case '7': case '8': case '9':
	    {
		I32 num = atoi(PL_regcomp_parse);

		if (num > 9 && num >= PL_regnpar)
		    goto defchar;
		else {
		    if (!SIZE_ONLY && num > PL_regcomp_rx->nparens)
			FAIL("reference to nonexistent group");
		    PL_regsawback = 1;
		    ret = reganode(FOLD
				   ? (LOC ? REFFL : REFF)
				   : REF, num);
		    *flagp |= HASWIDTH;
		    while (isDIGIT(*PL_regcomp_parse))
			PL_regcomp_parse++;
		    PL_regcomp_parse--;
		    nextchar();
		}
	    }
	    break;
	case '\0':
	    if (PL_regcomp_parse >= PL_regxend)
		FAIL("trailing \\ in regexp");
	    /* FALL THROUGH */
	default:
	    /* Do not generate `unrecognized' warnings here, we fall
	       back into the quick-grab loop below */
	    goto defchar;
	}
	break;

    case '#':
	if (PL_regflags & PMf_EXTENDED) {
	    while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != '\n') PL_regcomp_parse++;
	    if (PL_regcomp_parse < PL_regxend)
		goto tryagain;
	}
	/* FALL THROUGH */

    default: {
	    register I32 len;
	    register UV ender;
	    register char *p;
	    char *oldp, *s;
	    I32 numlen;

	    PL_regcomp_parse++;

	defchar:
	    ret = reg_node(FOLD
			  ? (LOC ? EXACTFL : EXACTF)
			  : EXACT);
	    s = STRING(ret);
	    for (len = 0, p = PL_regcomp_parse - 1;
	      len < 127 && p < PL_regxend;
	      len++)
	    {
		oldp = p;

		if (PL_regflags & PMf_EXTENDED)
		    p = regwhite(p, PL_regxend);
		switch (*p) {
		case '^':
		case '$':
		case '.':
		case '[':
		case '(':
		case ')':
		case '|':
		    goto loopdone;
		case '\\':
		    switch (*++p) {
		    case 'A':
		    case 'G':
		    case 'Z':
		    case 'z':
		    case 'w':
		    case 'W':
		    case 'b':
		    case 'B':
		    case 's':
		    case 'S':
		    case 'd':
		    case 'D':
		    case 'p':
		    case 'P':
			--p;
			goto loopdone;
		    case 'n':
			ender = '\n';
			p++;
			break;
		    case 'r':
			ender = '\r';
			p++;
			break;
		    case 't':
			ender = '\t';
			p++;
			break;
		    case 'f':
			ender = '\f';
			p++;
			break;
		    case 'e':
#ifdef ASCIIish
			  ender = '\033';
#else
			  ender = '\047';
#endif
			p++;
			break;
		    case 'a':
#ifdef ASCIIish
			  ender = '\007';
#else
			  ender = '\057';
#endif
			p++;
			break;
		    case 'x':
			if (*++p == '{') {
			    char* e = strchr(p, '}');
	 
			    if (!e)
				FAIL("Missing right brace on \\x{}");
			    else if (UTF) {
				ender = (UV)scan_hex(p + 1, e - p, &numlen);
				if (numlen + len >= 127) {	/* numlen is generous */
				    p--;
				    goto loopdone;
				}
				p = e + 1;
			    }
			    else
				FAIL("Can't use \\x{} without 'use utf8' declaration");
			}
			else {
			    ender = (UV)scan_hex(p, 2, &numlen);
			    p += numlen;
			}
			break;
		    case 'c':
			p++;
			ender = UCHARAT(p++);
			ender = toCTRL(ender);
			break;
		    case '0': case '1': case '2': case '3':case '4':
		    case '5': case '6': case '7': case '8':case '9':
			if (*p == '0' ||
			  (isDIGIT(p[1]) && atoi(p) >= PL_regnpar) ) {
			    ender = (UV)scan_oct(p, 3, &numlen);
			    p += numlen;
			}
			else {
			    --p;
			    goto loopdone;
			}
			break;
		    case '\0':
			if (p >= PL_regxend)
			    FAIL("trailing \\ in regexp");
			/* FALL THROUGH */
		    default:
			if (!SIZE_ONLY && ckWARN(WARN_REGEXP) && isALPHA(*p))
			    Perl_warner(aTHX_ WARN_REGEXP, 
					"/%.127s/: Unrecognized escape \\%c passed through",
					PL_regprecomp,
					*p);
			goto normal_default;
		    }
		    break;
		default:
		  normal_default:
		    if ((*p & 0xc0) == 0xc0 && UTF) {
			ender = utf8_to_uv((U8*)p, &numlen);
			p += numlen;
		    }
		    else
			ender = *p++;
		    break;
		}
		if (PL_regflags & PMf_EXTENDED)
		    p = regwhite(p, PL_regxend);
		if (UTF && FOLD) {
		    if (LOC)
			ender = toLOWER_LC_uni(ender);
		    else
			ender = toLOWER_uni(ender);
		}
		if (ISMULT2(p)) { /* Back off on ?+*. */
		    if (len)
			p = oldp;
		    else if (ender >= 0x80 && UTF) {
			reguni(ender, s, &numlen);
			s += numlen;
			len += numlen;
		    }
		    else {
			len++;
			REGC(ender, s++);
		    }
		    break;
		}
		if (ender >= 0x80 && UTF) {
		    reguni(ender, s, &numlen);
		    s += numlen;
		    len += numlen - 1;
		}
		else
		    REGC(ender, s++);
	    }
	loopdone:
	    PL_regcomp_parse = p - 1;
	    nextchar();
	    if (len < 0)
		FAIL("internal disaster in regexp");
	    if (len > 0)
		*flagp |= HASWIDTH;
	    if (len == 1)
		*flagp |= SIMPLE;
	    if (!SIZE_ONLY)
		STR_LEN(ret) = len;
	    if (SIZE_ONLY)
		PL_regsize += STR_SZ(len);
	    else
		PL_regcode += STR_SZ(len);
	}
	break;
    }

    return(ret);
}

STATIC char *
S_regwhite(pTHX_ char *p, char *e)
{
    while (p < e) {
	if (isSPACE(*p))
	    ++p;
	else if (*p == '#') {
	    do {
		p++;
	    } while (p < e && *p != '\n');
	}
	else
	    break;
    }
    return p;
}

/* Parse POSIX character classes: [[:foo:]], [[=foo=]], [[.foo.]].
   Character classes ([:foo:]) can also be negated ([:^foo:]).
   Returns a named class id (ANYOF_XXX) if successful, -1 otherwise.
   Equivalence classes ([=foo=]) and composites ([.foo.]) are parsed,
   but trigger warnings because they are currently unimplemented. */
STATIC I32
S_regpposixcc(pTHX_ I32 value)
{
    dTHR;
    char *posixcc = 0;
    I32 namedclass = OOB_NAMEDCLASS;

    if (value == '[' && PL_regcomp_parse + 1 < PL_regxend &&
	/* I smell either [: or [= or [. -- POSIX has been here, right? */
	(*PL_regcomp_parse == ':' ||
	 *PL_regcomp_parse == '=' ||
	 *PL_regcomp_parse == '.')) {
	char  c = *PL_regcomp_parse;
	char* s = PL_regcomp_parse++;
	    
	while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != c)
	    PL_regcomp_parse++;
	if (PL_regcomp_parse == PL_regxend)
	    /* Grandfather lone [:, [=, [. */
	    PL_regcomp_parse = s;
	else {
	    char* t = PL_regcomp_parse++; /* skip over the c */

  	    if (*PL_regcomp_parse == ']') {
  		PL_regcomp_parse++; /* skip over the ending ] */
  		posixcc = s + 1;
		if (*s == ':') {
		    I32 complement = *posixcc == '^' ? *posixcc++ : 0;
		    I32 skip = 5; /* the most common skip */

		    switch (*posixcc) {
		    case 'a':
			if (strnEQ(posixcc, "alnum", 5))
			    namedclass =
				complement ? ANYOF_NALNUMC : ANYOF_ALNUMC;
			else if (strnEQ(posixcc, "alpha", 5))
			    namedclass =
				complement ? ANYOF_NALPHA : ANYOF_ALPHA;
			else if (strnEQ(posixcc, "ascii", 5))
			    namedclass =
				complement ? ANYOF_NASCII : ANYOF_ASCII;
			break;
		    case 'c':
			if (strnEQ(posixcc, "cntrl", 5))
			    namedclass =
				complement ? ANYOF_NCNTRL : ANYOF_CNTRL;
			break;
		    case 'd':
			if (strnEQ(posixcc, "digit", 5))
			    namedclass =
				complement ? ANYOF_NDIGIT : ANYOF_DIGIT;
			break;
		    case 'g':
			if (strnEQ(posixcc, "graph", 5))
			    namedclass =
				complement ? ANYOF_NGRAPH : ANYOF_GRAPH;
			break;
		    case 'l':
			if (strnEQ(posixcc, "lower", 5))
			    namedclass =
				complement ? ANYOF_NLOWER : ANYOF_LOWER;
			break;
		    case 'p':
			if (strnEQ(posixcc, "print", 5))
			    namedclass =
				complement ? ANYOF_NPRINT : ANYOF_PRINT;
			else if (strnEQ(posixcc, "punct", 5))
			    namedclass =
				complement ? ANYOF_NPUNCT : ANYOF_PUNCT;
			break;
		    case 's':
			if (strnEQ(posixcc, "space", 5))
			    namedclass =
				complement ? ANYOF_NSPACE : ANYOF_SPACE;
		    case 'u':
			if (strnEQ(posixcc, "upper", 5))
			    namedclass =
				complement ? ANYOF_NUPPER : ANYOF_UPPER;
 			break;
		    case 'w': /* this is not POSIX, this is the Perl \w */
			if (strnEQ(posixcc, "word", 4)) {
			    namedclass =
				complement ? ANYOF_NALNUM : ANYOF_ALNUM;
			    skip = 4;
			}
			break;
		    case 'x':
			if (strnEQ(posixcc, "xdigit", 6)) {
			    namedclass =
				complement ? ANYOF_NXDIGIT : ANYOF_XDIGIT;
			    skip = 6;
			}
			break;
		    }
		    if (namedclass == OOB_NAMEDCLASS ||
			posixcc[skip] != ':' ||
			posixcc[skip+1] != ']')
			Perl_croak(aTHX_
				   "Character class [:%.*s:] unknown",
				   t - s - 1, s + 1);
		} else if (ckWARN(WARN_REGEXP) && !SIZE_ONLY)
		    /* [[=foo=]] and [[.foo.]] are still future. */
		    Perl_warner(aTHX_ WARN_REGEXP,
				"Character class syntax [%c %c] is reserved for future extensions", c, c);
	    } else {
		/* Maternal grandfather:
		 * "[:" ending in ":" but not in ":]" */
		PL_regcomp_parse = s;
	    }
	}
    }

    return namedclass;
}

STATIC void
S_checkposixcc(pTHX)
{
    if (!SIZE_ONLY && ckWARN(WARN_REGEXP) &&
	(*PL_regcomp_parse == ':' ||
	 *PL_regcomp_parse == '=' ||
	 *PL_regcomp_parse == '.')) {
	char *s = PL_regcomp_parse;
 	char  c = *s++;

	while(*s && isALNUM(*s))
	    s++;
	if (*s && c == *s && s[1] == ']') {
	    Perl_warner(aTHX_ WARN_REGEXP,
			"Character class syntax [%c %c] belongs inside character classes", c, c);
	    if (c == '=' || c == '.')
		Perl_warner(aTHX_ WARN_REGEXP,
			    "Character class syntax [%c %c] is reserved for future extensions", c, c);
	}
    }
}

STATIC regnode *
S_regclass(pTHX)
{
    dTHR;
    register U32 value;
    register I32 lastvalue = OOB_CHAR8;
    register I32 range = 0;
    register regnode *ret;
    I32 numlen;
    I32 namedclass;
    char *rangebegin;
    bool need_class = 0;

    ret = reg_node(ANYOF);
    if (SIZE_ONLY)
	PL_regsize += ANYOF_SKIP;
    else {
	ret->flags = 0;
	ANYOF_BITMAP_ZERO(ret);
 	PL_regcode += ANYOF_SKIP;
	if (FOLD)
	    ANYOF_FLAGS(ret) |= ANYOF_FOLD;
	if (LOC)
	    ANYOF_FLAGS(ret) |= ANYOF_LOCALE;
    }
    if (*PL_regcomp_parse == '^') {	/* Complement of range. */
	PL_regnaughty++;
	PL_regcomp_parse++;
	if (!SIZE_ONLY)
	    ANYOF_FLAGS(ret) |= ANYOF_INVERT;
    }

    if (!SIZE_ONLY && ckWARN(WARN_REGEXP))
	checkposixcc();

    if (*PL_regcomp_parse == ']' || *PL_regcomp_parse == '-')
	goto skipcond;		/* allow 1st char to be ] or - */
    while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != ']') {
       skipcond:
	namedclass = OOB_NAMEDCLASS;
	if (!range)
	    rangebegin = PL_regcomp_parse;
	value = UCHARAT(PL_regcomp_parse++);
	if (value == '[')
	    namedclass = regpposixcc(value);
	else if (value == '\\') {
	    value = UCHARAT(PL_regcomp_parse++);
	    /* Some compilers cannot handle switching on 64-bit integer
	     * values, therefore value cannot be an UV. --jhi */
	    switch (value) {
	    case 'w':	namedclass = ANYOF_ALNUM;	break;
	    case 'W':	namedclass = ANYOF_NALNUM;	break;
	    case 's':	namedclass = ANYOF_SPACE;	break;
	    case 'S':	namedclass = ANYOF_NSPACE;	break;
	    case 'd':	namedclass = ANYOF_DIGIT;	break;
	    case 'D':	namedclass = ANYOF_NDIGIT;	break;
	    case 'n':	value = '\n';			break;
	    case 'r':	value = '\r';			break;
	    case 't':	value = '\t';			break;
	    case 'f':	value = '\f';			break;
	    case 'b':	value = '\b';			break;
#ifdef ASCIIish
	    case 'e':	value = '\033';			break;
	    case 'a':	value = '\007';			break;
#else
	    case 'e':	value = '\047';			break;
	    case 'a':	value = '\057';			break;
#endif
	    case 'x':
		value = (UV)scan_hex(PL_regcomp_parse, 2, &numlen);
		PL_regcomp_parse += numlen;
		break;
	    case 'c':
		value = UCHARAT(PL_regcomp_parse++);
		value = toCTRL(value);
		break;
	    case '0': case '1': case '2': case '3': case '4':
	    case '5': case '6': case '7': case '8': case '9':
		value = (UV)scan_oct(--PL_regcomp_parse, 3, &numlen);
		PL_regcomp_parse += numlen;
		break;
	    default:
		if (!SIZE_ONLY && ckWARN(WARN_REGEXP) && isALPHA(value))
		    Perl_warner(aTHX_ WARN_REGEXP, 
				"/%.127s/: Unrecognized escape \\%c in character class passed through",
				PL_regprecomp,
				(int)value);
		break;
	    }
	}
	if (namedclass > OOB_NAMEDCLASS) {
	    if (!need_class && !SIZE_ONLY)
		ANYOF_CLASS_ZERO(ret);
	    need_class = 1;
	    if (range) { /* a-\d, a-[:digit:] */
		if (!SIZE_ONLY) {
		    if (ckWARN(WARN_REGEXP))
			Perl_warner(aTHX_ WARN_REGEXP,
				    "/%.127s/: false [] range \"%*.*s\" in regexp",
				    PL_regprecomp,
				    PL_regcomp_parse - rangebegin,
				    PL_regcomp_parse - rangebegin,
				    rangebegin);
		    ANYOF_BITMAP_SET(ret, lastvalue);
		    ANYOF_BITMAP_SET(ret, '-');
		}
		range = 0; /* this is not a true range */
	    }
	    if (!SIZE_ONLY) {
		switch (namedclass) {
		case ANYOF_ALNUM:
		    if (LOC)
			ANYOF_CLASS_SET(ret, ANYOF_ALNUM);
		    else {
			for (value = 0; value < 256; value++)
			    if (isALNUM(value))
				ANYOF_BITMAP_SET(ret, value);
		    }
		    break;
		case ANYOF_NALNUM:
		    if (LOC)
			ANYOF_CLASS_SET(ret, ANYOF_NALNUM);
		    else {
			for (value = 0; value < 256; value++)
			    if (!isALNUM(value))
				ANYOF_BITMAP_SET(ret, value);
		    }
		    break;
		case ANYOF_SPACE:
		    if (LOC)
			ANYOF_CLASS_SET(ret, ANYOF_SPACE);
		    else {
			for (value = 0; value < 256; value++)
			    if (isSPACE(value))
				ANYOF_BITMAP_SET(ret, value);
		    }
		    break;
		case ANYOF_NSPACE:
		    if (LOC)
			ANYOF_CLASS_SET(ret, ANYOF_NSPACE);
		    else {
			for (value = 0; value < 256; value++)
			    if (!isSPACE(value))
				ANYOF_BITMAP_SET(ret, value);
		    }
		    break;
		case ANYOF_DIGIT:
		    if (LOC)
			ANYOF_CLASS_SET(ret, ANYOF_DIGIT);
		    else {
			for (value = '0'; value <= '9'; value++)
			    ANYOF_BITMAP_SET(ret, value);
		    }
		    break;
		case ANYOF_NDIGIT:
		    if (LOC)
			ANYOF_CLASS_SET(ret, ANYOF_NDIGIT);
		    else {
			for (value = 0; value < '0'; value++)
			    ANYOF_BITMAP_SET(ret, value);
			for (value = '9' + 1; value < 256; value++)
			    ANYOF_BITMAP_SET(ret, value);
		    }
		    break;
		case ANYOF_NALNUMC:
		    if (LOC)
			ANYOF_CLASS_SET(ret, ANYOF_NALNUMC);
		    else {
			for (value = 0; value < 256; value++)
			    if (!isALNUMC(value))
				ANYOF_BITMAP_SET(ret, value);
		    }
		    break;
		case ANYOF_ALNUMC:
		    if (LOC)
			ANYOF_CLASS_SET(ret, ANYOF_ALNUMC);
		    else {
			for (value = 0; value < 256; value++)
			    if (isALNUMC(value))
				ANYOF_BITMAP_SET(ret, value);
		    }
		    break;
		case ANYOF_ALPHA:
		    if (LOC)
			ANYOF_CLASS_SET(ret, ANYOF_ALPHA);
		    else {
			for (value = 0; value < 256; value++)
			    if (isALPHA(value))
				ANYOF_BITMAP_SET(ret, value);
		    }
		    break;
		case ANYOF_NALPHA:
		    if (LOC)
			ANYOF_CLASS_SET(ret, ANYOF_NALPHA);
		    else {
			for (value = 0; value < 256; value++)
			    if (!isALPHA(value))
				ANYOF_BITMAP_SET(ret, value);
		    }
		    break;
		case ANYOF_ASCII:
		    if (LOC)
			ANYOF_CLASS_SET(ret, ANYOF_ASCII);
		    else {
#ifdef ASCIIish
			for (value = 0; value < 128; value++)
			    ANYOF_BITMAP_SET(ret, value);
#else  /* EBCDIC */
			for (value = 0; value < 256; value++)
			    if (isASCII(value))
				ANYOF_BITMAP_SET(ret, value);
#endif /* EBCDIC */
		    }
		    break;
		case ANYOF_NASCII:
		    if (LOC)
			ANYOF_CLASS_SET(ret, ANYOF_NASCII);
		    else {
#ifdef ASCIIish
			for (value = 128; value < 256; value++)
			    ANYOF_BITMAP_SET(ret, value);
#else  /* EBCDIC */
			for (value = 0; value < 256; value++)
			    if (!isASCII(value))
				ANYOF_BITMAP_SET(ret, value);
#endif /* EBCDIC */
		    }
		    break;
		case ANYOF_CNTRL:
		    if (LOC)
			ANYOF_CLASS_SET(ret, ANYOF_CNTRL);
		    else {
			for (value = 0; value < 256; value++)
			    if (isCNTRL(value))
				ANYOF_BITMAP_SET(ret, value);
		    }
		    lastvalue = OOB_CHAR8;
		    break;
		case ANYOF_NCNTRL:
		    if (LOC)
			ANYOF_CLASS_SET(ret, ANYOF_NCNTRL);
		    else {
			for (value = 0; value < 256; value++)
			    if (!isCNTRL(value))
				ANYOF_BITMAP_SET(ret, value);
		    }
		    break;
		case ANYOF_GRAPH:
		    if (LOC)
			ANYOF_CLASS_SET(ret, ANYOF_GRAPH);
		    else {
			for (value = 0; value < 256; value++)
			    if (isGRAPH(value))
				ANYOF_BITMAP_SET(ret, value);
		    }
		    break;
		case ANYOF_NGRAPH:
		    if (LOC)
			ANYOF_CLASS_SET(ret, ANYOF_NGRAPH);
		    else {
			for (value = 0; value < 256; value++)
			    if (!isGRAPH(value))
				ANYOF_BITMAP_SET(ret, value);
		    }
		    break;
		case ANYOF_LOWER:
		    if (LOC)
			ANYOF_CLASS_SET(ret, ANYOF_LOWER);
		    else {
			for (value = 0; value < 256; value++)
			    if (isLOWER(value))
				ANYOF_BITMAP_SET(ret, value);
		    }
		    break;
		case ANYOF_NLOWER:
		    if (LOC)
			ANYOF_CLASS_SET(ret, ANYOF_NLOWER);
		    else {
			for (value = 0; value < 256; value++)
			    if (!isLOWER(value))
				ANYOF_BITMAP_SET(ret, value);
		    }
		    break;
		case ANYOF_PRINT:
		    if (LOC)
			ANYOF_CLASS_SET(ret, ANYOF_PRINT);
		    else {
			for (value = 0; value < 256; value++)
			    if (isPRINT(value))
				ANYOF_BITMAP_SET(ret, value);
		    }
		    break;
		case ANYOF_NPRINT:
		    if (LOC)
			ANYOF_CLASS_SET(ret, ANYOF_NPRINT);
		    else {
			for (value = 0; value < 256; value++)
			    if (!isPRINT(value))
				ANYOF_BITMAP_SET(ret, value);
		    }
		    break;
		case ANYOF_PUNCT:
		    if (LOC)
			ANYOF_CLASS_SET(ret, ANYOF_PUNCT);
		    else {
			for (value = 0; value < 256; value++)
			    if (isPUNCT(value))
				ANYOF_BITMAP_SET(ret, value);
		    }
		    break;
		case ANYOF_NPUNCT:
		    if (LOC)
			ANYOF_CLASS_SET(ret, ANYOF_NPUNCT);
		    else {
			for (value = 0; value < 256; value++)
			    if (!isPUNCT(value))
				ANYOF_BITMAP_SET(ret, value);
		    }
		    break;
		case ANYOF_UPPER:
		    if (LOC)
			ANYOF_CLASS_SET(ret, ANYOF_UPPER);
		    else {
			for (value = 0; value < 256; value++)
			    if (isUPPER(value))
				ANYOF_BITMAP_SET(ret, value);
		    }
		    break;
		case ANYOF_NUPPER:
		    if (LOC)
			ANYOF_CLASS_SET(ret, ANYOF_NUPPER);
		    else {
			for (value = 0; value < 256; value++)
			    if (!isUPPER(value))
				ANYOF_BITMAP_SET(ret, value);
		    }
		    break;
		case ANYOF_XDIGIT:
		    if (LOC)
			ANYOF_CLASS_SET(ret, ANYOF_XDIGIT);
		    else {
			for (value = 0; value < 256; value++)
			    if (isXDIGIT(value))
				ANYOF_BITMAP_SET(ret, value);
		    }
		    break;
		case ANYOF_NXDIGIT:
		    if (LOC)
			ANYOF_CLASS_SET(ret, ANYOF_NXDIGIT);
		    else {
			for (value = 0; value < 256; value++)
			    if (!isXDIGIT(value))
				ANYOF_BITMAP_SET(ret, value);
		    }
		    break;
		default:
		    FAIL("invalid [::] class in regexp");
		    break;
		}
		if (LOC)
		    ANYOF_FLAGS(ret) |= ANYOF_CLASS;
		continue;
	    }
	}
	if (range) {
	    if (lastvalue > value) /* b-a */ {
		Perl_croak(aTHX_
			   "/%.127s/: invalid [] range \"%*.*s\" in regexp",
			   PL_regprecomp,
			   PL_regcomp_parse - rangebegin,
			   PL_regcomp_parse - rangebegin,
			   rangebegin);
	    }
	    range = 0;
	}
	else {
	    lastvalue = value;
	    if (*PL_regcomp_parse == '-' && PL_regcomp_parse+1 < PL_regxend &&
		PL_regcomp_parse[1] != ']') {
		PL_regcomp_parse++;
		if (namedclass > OOB_NAMEDCLASS) { /* \w-, [:word:]- */
		    if (ckWARN(WARN_REGEXP))
			Perl_warner(aTHX_ WARN_REGEXP,
				    "/%.127s/: false [] range \"%*.*s\" in regexp",
				    PL_regprecomp,
				    PL_regcomp_parse - rangebegin,
				    PL_regcomp_parse - rangebegin,
				    rangebegin);
		    if (!SIZE_ONLY)
			ANYOF_BITMAP_SET(ret, '-');
		} else
		    range = 1;
		continue;	/* do it next time */
	    }
	}
	/* now is the next time */
	if (!SIZE_ONLY) {
#ifndef ASCIIish /* EBCDIC, for example. */
	    if ((isLOWER(lastvalue) && isLOWER(value)) ||
		(isUPPER(lastvalue) && isUPPER(value)))
	    {
		I32 i;
 		if (isLOWER(lastvalue)) {
 		    for (i = lastvalue; i <= value; i++)
			if (isLOWER(i))
			    ANYOF_BITMAP_SET(ret, i);
 		} else {
 		    for (i = lastvalue; i <= value; i++)
			if (isUPPER(i))
			    ANYOF_BITMAP_SET(ret, i);
		}
	    }
	    else
#endif
		for ( ; lastvalue <= value; lastvalue++)
		    ANYOF_BITMAP_SET(ret, lastvalue);
        }
	range = 0;
    }
    if (need_class) {
	if (SIZE_ONLY)
	    PL_regsize += ANYOF_CLASS_ADD_SKIP;
	else
	    PL_regcode += ANYOF_CLASS_ADD_SKIP;
    }
    /* optimize case-insensitive simple patterns (e.g. /[a-z]/i) */
    if (!SIZE_ONLY &&
	(ANYOF_FLAGS(ret) & (ANYOF_FLAGS_ALL ^ ANYOF_INVERT)) == ANYOF_FOLD) {
	for (value = 0; value < 256; ++value) {
	    if (ANYOF_BITMAP_TEST(ret, value)) {
		I32 cf = PL_fold[value];
		ANYOF_BITMAP_SET(ret, cf);
	    }
	}
	ANYOF_FLAGS(ret) &= ~ANYOF_FOLD;
    }
    /* optimize inverted simple patterns (e.g. [^a-z]) */
    if (!SIZE_ONLY && (ANYOF_FLAGS(ret) & ANYOF_FLAGS_ALL) == ANYOF_INVERT) {
	for (value = 0; value < ANYOF_BITMAP_SIZE; ++value)
	    ANYOF_BITMAP(ret)[value] ^= ANYOF_FLAGS_ALL;
	ANYOF_FLAGS(ret) = 0;
    }
    return ret;
}

STATIC regnode *
S_regclassutf8(pTHX)
{
    dTHR;
    register char *e;
    register U32 value;
    register U32 lastvalue = OOB_UTF8;
    register I32 range = 0;
    register regnode *ret;
    I32 numlen;
    I32 n;
    SV *listsv;
    U8 flags = 0;
    I32 namedclass;
    char *rangebegin;

    if (*PL_regcomp_parse == '^') {	/* Complement of range. */
	PL_regnaughty++;
	PL_regcomp_parse++;
	if (!SIZE_ONLY)
	    flags |= ANYOF_INVERT;
    }
    if (!SIZE_ONLY) {
	if (FOLD)
	    flags |= ANYOF_FOLD;
	if (LOC)
	    flags |= ANYOF_LOCALE;
	listsv = newSVpvn("# comment\n",10);
    }

    if (!SIZE_ONLY && ckWARN(WARN_REGEXP))
	checkposixcc();

    if (*PL_regcomp_parse == ']' || *PL_regcomp_parse == '-')
	goto skipcond;		/* allow 1st char to be ] or - */

    while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != ']') {
       skipcond:
	namedclass = OOB_NAMEDCLASS;
	if (!range)
	    rangebegin = PL_regcomp_parse;
	value = utf8_to_uv((U8*)PL_regcomp_parse, &numlen);
	PL_regcomp_parse += numlen;
	if (value == '[')
	    namedclass = regpposixcc(value);
	else if (value == '\\') {
	    value = (U32)utf8_to_uv((U8*)PL_regcomp_parse, &numlen);
	    PL_regcomp_parse += numlen;
	    /* Some compilers cannot handle switching on 64-bit integer
	     * values, therefore value cannot be an UV.  Yes, this will
	     * be a problem later if we want switch on Unicode.  --jhi */
	    switch (value) {
	    case 'w':		namedclass = ANYOF_ALNUM;		break;
	    case 'W':		namedclass = ANYOF_NALNUM;		break;
	    case 's':		namedclass = ANYOF_SPACE;		break;
	    case 'S':		namedclass = ANYOF_NSPACE;		break;
	    case 'd':		namedclass = ANYOF_DIGIT;		break;
	    case 'D':		namedclass = ANYOF_NDIGIT;		break;
	    case 'p':
	    case 'P':
		if (*PL_regcomp_parse == '{') {
		    e = strchr(PL_regcomp_parse++, '}');
                    if (!e)
                        FAIL("Missing right brace on \\p{}");
		    n = e - PL_regcomp_parse;
		}
		else {
		    e = PL_regcomp_parse;
		    n = 1;
		}
		if (!SIZE_ONLY) {
		    if (value == 'p')
			Perl_sv_catpvf(aTHX_ listsv,
				       "+utf8::%.*s\n", (int)n, PL_regcomp_parse);
		    else
			Perl_sv_catpvf(aTHX_ listsv,
				       "!utf8::%.*s\n", (int)n, PL_regcomp_parse);
		}
		PL_regcomp_parse = e + 1;
		lastvalue = OOB_UTF8;
		continue;
	    case 'n':		value = '\n';		break;
	    case 'r':		value = '\r';		break;
	    case 't':		value = '\t';		break;
	    case 'f':		value = '\f';		break;
	    case 'b':		value = '\b';		break;
#ifdef ASCIIish
	    case 'e':		value = '\033';		break;
	    case 'a':		value = '\007';		break;
#else
	    case 'e':		value = '\047';		break;
	    case 'a':		value = '\057';		break;
#endif
	    case 'x':
		if (*PL_regcomp_parse == '{') {
		    e = strchr(PL_regcomp_parse++, '}');
                    if (!e)
                        FAIL("Missing right brace on \\x{}");
		    value = (UV)scan_hex(PL_regcomp_parse,
				     e - PL_regcomp_parse,
				     &numlen);
		    PL_regcomp_parse = e + 1;
		}
		else {
		    value = (UV)scan_hex(PL_regcomp_parse, 2, &numlen);
		    PL_regcomp_parse += numlen;
		}
		break;
	    case 'c':
		value = UCHARAT(PL_regcomp_parse++);
		value = toCTRL(value);
		break;
	    case '0': case '1': case '2': case '3': case '4':
	    case '5': case '6': case '7': case '8': case '9':
		value = (UV)scan_oct(--PL_regcomp_parse, 3, &numlen);
		PL_regcomp_parse += numlen;
		break;
	    default:
		if (!SIZE_ONLY && ckWARN(WARN_REGEXP) && isALPHA(value))
		    Perl_warner(aTHX_ WARN_REGEXP, 
				"/%.127s/: Unrecognized escape \\%c in character class passed through",
				PL_regprecomp,
				(int)value);
		break;
	    }
	}
	if (namedclass > OOB_NAMEDCLASS) {
	    if (range) { /* a-\d, a-[:digit:] */
		if (!SIZE_ONLY) {
		    if (ckWARN(WARN_REGEXP))
			Perl_warner(aTHX_ WARN_REGEXP,
				    "/%.127s/: false [] range \"%*.*s\" in regexp",
				    PL_regprecomp,
				    PL_regcomp_parse - rangebegin,
				    PL_regcomp_parse - rangebegin,
				    rangebegin);
		    Perl_sv_catpvf(aTHX_ listsv,
				   /* 0x002D is Unicode for '-' */
				   "%04"UVxf"\n002D\n", (UV)lastvalue);
		}
		range = 0;
	    }
	    if (!SIZE_ONLY) {
		switch (namedclass) {
		case ANYOF_ALNUM:
		    Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsWord\n");	break;
		case ANYOF_NALNUM:
		    Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsWord\n");	break;
		case ANYOF_ALNUMC:
		    Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsAlnum\n");	break;
		case ANYOF_NALNUMC:
		    Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsAlnum\n");	break;
		case ANYOF_ALPHA:
		    Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsAlpha\n");	break;
		case ANYOF_NALPHA:
		    Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsAlpha\n");	break;
		case ANYOF_ASCII:
		    Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsASCII\n");	break;
		case ANYOF_NASCII:
		    Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsASCII\n");	break;
		case ANYOF_CNTRL:
		    Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsCntrl\n");	break;
		case ANYOF_NCNTRL:
		    Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsCntrl\n");	break;
		case ANYOF_GRAPH:
		    Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsGraph\n");	break;
		case ANYOF_NGRAPH:
		    Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsGraph\n");	break;
		case ANYOF_DIGIT:
		    Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsDigit\n");	break;
		case ANYOF_NDIGIT:
		    Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsDigit\n");	break;
		case ANYOF_LOWER:
		    Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsLower\n");	break;
		case ANYOF_NLOWER:
		    Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsLower\n");	break;
		case ANYOF_PRINT:
		    Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsPrint\n");	break;
		case ANYOF_NPRINT:
		    Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsPrint\n");	break;
		case ANYOF_PUNCT:
		    Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsPunct\n");	break;
		case ANYOF_NPUNCT:
		    Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsPunct\n");	break;
		case ANYOF_SPACE:
		    Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsSpace\n");	break;
		case ANYOF_NSPACE:
		    Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsSpace\n");	break;
		case ANYOF_UPPER:
		    Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsUpper\n");	break;
		case ANYOF_NUPPER:
		    Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsUpper\n");	break;
		case ANYOF_XDIGIT:
		    Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsXDigit\n");	break;
		case ANYOF_NXDIGIT:
		    Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsXDigit\n");	break;
		}
		continue;
	    }
	}
        if (range) {
	    if (lastvalue > value) { /* b-a */
		Perl_croak(aTHX_
			   "/%.127s/: invalid [] range \"%*.*s\" in regexp",
			   PL_regprecomp,
			   PL_regcomp_parse - rangebegin,
			   PL_regcomp_parse - rangebegin,
			   rangebegin);
	    }
	    range = 0;
	}
	else {
	    lastvalue = value;
	    if (*PL_regcomp_parse == '-' && PL_regcomp_parse+1 < PL_regxend &&
		PL_regcomp_parse[1] != ']') {
		PL_regcomp_parse++;
		if (namedclass > OOB_NAMEDCLASS) { /* \w-, [:word:]- */
		    if (ckWARN(WARN_REGEXP))
			Perl_warner(aTHX_ WARN_REGEXP,
				    "/%.127s/: false [] range \"%*.*s\" in regexp",
				    PL_regprecomp,
				    PL_regcomp_parse - rangebegin,
				    PL_regcomp_parse - rangebegin,
				    rangebegin);
		    if (!SIZE_ONLY)
			Perl_sv_catpvf(aTHX_ listsv,
				       /* 0x002D is Unicode for '-' */
				       "002D\n");
		} else
		    range = 1;
		continue;	/* do it next time */
	    }
	}
	/* now is the next time */
	if (!SIZE_ONLY)
	    Perl_sv_catpvf(aTHX_ listsv, "%04"UVxf"\t%04"UVxf"\n",
			   (UV)lastvalue, (UV)value);
	range = 0;
    }

    ret = reganode(ANYOFUTF8, 0);

    if (!SIZE_ONLY) {
	SV *rv = swash_init("utf8", "", listsv, 1, 0);
	SvREFCNT_dec(listsv);
	n = add_data(1,"s");
	PL_regcomp_rx->data->data[n] = (void*)rv;
	ARG1_SET(ret, flags);
	ARG2_SET(ret, n);
    }

    return ret;
}

STATIC char*
S_nextchar(pTHX)
{
    dTHR;
    char* retval = PL_regcomp_parse++;

    for (;;) {
	if (*PL_regcomp_parse == '(' && PL_regcomp_parse[1] == '?' &&
		PL_regcomp_parse[2] == '#') {
	    while (*PL_regcomp_parse && *PL_regcomp_parse != ')')
		PL_regcomp_parse++;
	    PL_regcomp_parse++;
	    continue;
	}
	if (PL_regflags & PMf_EXTENDED) {
	    if (isSPACE(*PL_regcomp_parse)) {
		PL_regcomp_parse++;
		continue;
	    }
	    else if (*PL_regcomp_parse == '#') {
		while (*PL_regcomp_parse && *PL_regcomp_parse != '\n')
		    PL_regcomp_parse++;
		PL_regcomp_parse++;
		continue;
	    }
	}
	return retval;
    }
}

/*
- reg_node - emit a node
*/
STATIC regnode *			/* Location. */
S_reg_node(pTHX_ U8 op)
{
    dTHR;
    register regnode *ret;
    register regnode *ptr;

    ret = PL_regcode;
    if (SIZE_ONLY) {
	SIZE_ALIGN(PL_regsize);
	PL_regsize += 1;
	return(ret);
    }

    NODE_ALIGN_FILL(ret);
    ptr = ret;
    FILL_ADVANCE_NODE(ptr, op);
    PL_regcode = ptr;

    return(ret);
}

/*
- reganode - emit a node with an argument
*/
STATIC regnode *			/* Location. */
S_reganode(pTHX_ U8 op, U32 arg)
{
    dTHR;
    register regnode *ret;
    register regnode *ptr;

    ret = PL_regcode;
    if (SIZE_ONLY) {
	SIZE_ALIGN(PL_regsize);
	PL_regsize += 2;
	return(ret);
    }

    NODE_ALIGN_FILL(ret);
    ptr = ret;
    FILL_ADVANCE_NODE_ARG(ptr, op, arg);
    PL_regcode = ptr;

    return(ret);
}

/*
- reguni - emit (if appropriate) a Unicode character
*/
STATIC void
S_reguni(pTHX_ UV uv, char* s, I32* lenp)
{
    dTHR;
    if (SIZE_ONLY) {
	U8 tmpbuf[UTF8_MAXLEN];
	*lenp = uv_to_utf8(tmpbuf, uv) - tmpbuf;
    }
    else
	*lenp = uv_to_utf8((U8*)s, uv) - (U8*)s;

}

/*
- reginsert - insert an operator in front of already-emitted operand
*
* Means relocating the operand.
*/
STATIC void
S_reginsert(pTHX_ U8 op, regnode *opnd)
{
    dTHR;
    register regnode *src;
    register regnode *dst;
    register regnode *place;
    register int offset = regarglen[(U8)op];
    
/* (PL_regkind[(U8)op] == CURLY ? EXTRA_STEP_2ARGS : 0); */

    if (SIZE_ONLY) {
	PL_regsize += NODE_STEP_REGNODE + offset;
	return;
    }

    src = PL_regcode;
    PL_regcode += NODE_STEP_REGNODE + offset;
    dst = PL_regcode;
    while (src > opnd)
	StructCopy(--src, --dst, regnode);

    place = opnd;		/* Op node, where operand used to be. */
    src = NEXTOPER(place);
    FILL_ADVANCE_NODE(place, op);
    Zero(src, offset, regnode);
}

/*
- regtail - set the next-pointer at the end of a node chain of p to val.
*/
STATIC void
S_regtail(pTHX_ regnode *p, regnode *val)
{
    dTHR;
    register regnode *scan;
    register regnode *temp;

    if (SIZE_ONLY)
	return;

    /* Find last node. */
    scan = p;
    for (;;) {
	temp = regnext(scan);
	if (temp == NULL)
	    break;
	scan = temp;
    }

    if (reg_off_by_arg[OP(scan)]) {
	ARG_SET(scan, val - scan);
    }
    else {
	NEXT_OFF(scan) = val - scan;
    }
}

/*
- regoptail - regtail on operand of first argument; nop if operandless
*/
STATIC void
S_regoptail(pTHX_ regnode *p, regnode *val)
{
    dTHR;
    /* "Operandless" and "op != BRANCH" are synonymous in practice. */
    if (p == NULL || SIZE_ONLY)
	return;
    if (PL_regkind[(U8)OP(p)] == BRANCH) {
	regtail(NEXTOPER(p), val);
    }
    else if ( PL_regkind[(U8)OP(p)] == BRANCHJ) {
	regtail(NEXTOPER(NEXTOPER(p)), val);
    }
    else
	return;
}

/*
 - regcurly - a little FSA that accepts {\d+,?\d*}
 */
STATIC I32
S_regcurly(pTHX_ register char *s)
{
    if (*s++ != '{')
	return FALSE;
    if (!isDIGIT(*s))
	return FALSE;
    while (isDIGIT(*s))
	s++;
    if (*s == ',')
	s++;
    while (isDIGIT(*s))
	s++;
    if (*s != '}')
	return FALSE;
    return TRUE;
}


STATIC regnode *
S_dumpuntil(pTHX_ regnode *start, regnode *node, regnode *last, SV* sv, I32 l)
{
#ifdef DEBUGGING
    register U8 op = EXACT;	/* Arbitrary non-END op. */
    register regnode *next;

    while (op != END && (!last || node < last)) {
	/* While that wasn't END last time... */

	NODE_ALIGN(node);
	op = OP(node);
	if (op == CLOSE)
	    l--;	
	next = regnext(node);
	/* Where, what. */
	if (OP(node) == OPTIMIZED)
	    goto after_print;
	regprop(sv, node);
	PerlIO_printf(Perl_debug_log, "%4"IVdf":%*s%s", (IV)(node - start),
		      (int)(2*l + 1), "", SvPVX(sv));
	if (next == NULL)		/* Next ptr. */
	    PerlIO_printf(Perl_debug_log, "(0)");
	else 
	    PerlIO_printf(Perl_debug_log, "(%"IVdf")", (IV)(next - start));
	(void)PerlIO_putc(Perl_debug_log, '\n');
      after_print:
	if (PL_regkind[(U8)op] == BRANCHJ) {
	    register regnode *nnode = (OP(next) == LONGJMP 
				       ? regnext(next) 
				       : next);
	    if (last && nnode > last)
		nnode = last;
	    node = dumpuntil(start, NEXTOPER(NEXTOPER(node)), nnode, sv, l + 1);
	}
	else if (PL_regkind[(U8)op] == BRANCH) {
	    node = dumpuntil(start, NEXTOPER(node), next, sv, l + 1);
	}
	else if ( op == CURLY) {   /* `next' might be very big: optimizer */
	    node = dumpuntil(start, NEXTOPER(node) + EXTRA_STEP_2ARGS,
			     NEXTOPER(node) + EXTRA_STEP_2ARGS + 1, sv, l + 1);
	}
	else if (PL_regkind[(U8)op] == CURLY && op != CURLYX) {
	    node = dumpuntil(start, NEXTOPER(node) + EXTRA_STEP_2ARGS,
			     next, sv, l + 1);
	}
	else if ( op == PLUS || op == STAR) {
	    node = dumpuntil(start, NEXTOPER(node), NEXTOPER(node) + 1, sv, l + 1);
	}
	else if (op == ANYOF) {
	    node = NEXTOPER(node);
	    node += ANYOF_SKIP;
	}
	else if (PL_regkind[(U8)op] == EXACT) {
            /* Literal string, where present. */
	    node += NODE_SZ_STR(node) - 1;
	    node = NEXTOPER(node);
	}
	else {
	    node = NEXTOPER(node);
	    node += regarglen[(U8)op];
	}
	if (op == CURLYX || op == OPEN)
	    l++;
	else if (op == WHILEM)
	    l--;
    }
#endif	/* DEBUGGING */
    return node;
}

/*
 - regdump - dump a regexp onto Perl_debug_log in vaguely comprehensible form
 */
void
Perl_regdump(pTHX_ regexp *r)
{
#ifdef DEBUGGING
    dTHR;
    SV *sv = sv_newmortal();

    (void)dumpuntil(r->program, r->program + 1, NULL, sv, 0);

    /* Header fields of interest. */
    if (r->anchored_substr)
	PerlIO_printf(Perl_debug_log,
		      "anchored `%s%.*s%s'%s at %"IVdf" ", 
		      PL_colors[0],
		      (int)(SvCUR(r->anchored_substr) - (SvTAIL(r->anchored_substr)!=0)),
		      SvPVX(r->anchored_substr), 
		      PL_colors[1],
		      SvTAIL(r->anchored_substr) ? "$" : "",
		      (IV)r->anchored_offset);
    if (r->float_substr)
	PerlIO_printf(Perl_debug_log,
		      "floating `%s%.*s%s'%s at %"IVdf"..%"UVuf" ", 
		      PL_colors[0],
		      (int)(SvCUR(r->float_substr) - (SvTAIL(r->float_substr)!=0)), 
		      SvPVX(r->float_substr),
		      PL_colors[1],
		      SvTAIL(r->float_substr) ? "$" : "",
		      (IV)r->float_min_offset, (UV)r->float_max_offset);
    if (r->check_substr)
	PerlIO_printf(Perl_debug_log, 
		      r->check_substr == r->float_substr 
		      ? "(checking floating" : "(checking anchored");
    if (r->reganch & ROPT_NOSCAN)
	PerlIO_printf(Perl_debug_log, " noscan");
    if (r->reganch & ROPT_CHECK_ALL)
	PerlIO_printf(Perl_debug_log, " isall");
    if (r->check_substr)
	PerlIO_printf(Perl_debug_log, ") ");

    if (r->regstclass) {
	regprop(sv, r->regstclass);
	PerlIO_printf(Perl_debug_log, "stclass `%s' ", SvPVX(sv));
    }
    if (r->reganch & ROPT_ANCH) {
	PerlIO_printf(Perl_debug_log, "anchored");
	if (r->reganch & ROPT_ANCH_BOL)
	    PerlIO_printf(Perl_debug_log, "(BOL)");
	if (r->reganch & ROPT_ANCH_MBOL)
	    PerlIO_printf(Perl_debug_log, "(MBOL)");
	if (r->reganch & ROPT_ANCH_SBOL)
	    PerlIO_printf(Perl_debug_log, "(SBOL)");
	if (r->reganch & ROPT_ANCH_GPOS)
	    PerlIO_printf(Perl_debug_log, "(GPOS)");
	PerlIO_putc(Perl_debug_log, ' ');
    }
    if (r->reganch & ROPT_GPOS_SEEN)
	PerlIO_printf(Perl_debug_log, "GPOS ");
    if (r->reganch & ROPT_SKIP)
	PerlIO_printf(Perl_debug_log, "plus ");
    if (r->reganch & ROPT_IMPLICIT)
	PerlIO_printf(Perl_debug_log, "implicit ");
    PerlIO_printf(Perl_debug_log, "minlen %ld ", (long) r->minlen);
    if (r->reganch & ROPT_EVAL_SEEN)
	PerlIO_printf(Perl_debug_log, "with eval ");
    PerlIO_printf(Perl_debug_log, "\n");
#endif	/* DEBUGGING */
}

STATIC void
S_put_byte(pTHX_ SV *sv, int c)
{
    if (c <= ' ' || c == 127 || c == 255)
	Perl_sv_catpvf(aTHX_ sv, "\\%o", c);
    else if (c == '-' || c == ']' || c == '\\' || c == '^')
	Perl_sv_catpvf(aTHX_ sv, "\\%c", c);
    else
	Perl_sv_catpvf(aTHX_ sv, "%c", c);
}

/*
- regprop - printable representation of opcode
*/
void
Perl_regprop(pTHX_ SV *sv, regnode *o)
{
#ifdef DEBUGGING
    dTHR;
    register int k;

    sv_setpvn(sv, "", 0);
    if (OP(o) >= reg_num)		/* regnode.type is unsigned */
	FAIL("corrupted regexp opcode");
    sv_catpv(sv, (char*)reg_name[OP(o)]); /* Take off const! */

    k = PL_regkind[(U8)OP(o)];

    if (k == EXACT)
	Perl_sv_catpvf(aTHX_ sv, " <%s%.*s%s>", PL_colors[0],
		       STR_LEN(o), STRING(o), PL_colors[1]);
    else if (k == CURLY) {
	if (OP(o) == CURLYM || OP(o) == CURLYN)
	    Perl_sv_catpvf(aTHX_ sv, "[%d]", o->flags); /* Parenth number */
	Perl_sv_catpvf(aTHX_ sv, " {%d,%d}", ARG1(o), ARG2(o));
    }
    else if (k == WHILEM && o->flags)			/* Ordinal/of */
	Perl_sv_catpvf(aTHX_ sv, "[%d/%d]", o->flags & 0xf, o->flags>>4);
    else if (k == REF || k == OPEN || k == CLOSE || k == GROUPP )
	Perl_sv_catpvf(aTHX_ sv, "%d", (int)ARG(o));	/* Parenth number */
    else if (k == LOGICAL)
	Perl_sv_catpvf(aTHX_ sv, "[%d]", o->flags);	/* 2: embedded, otherwise 1 */
    else if (k == ANYOF) {
	int i, rangestart = -1;
	const char * const out[] = {	/* Should be syncronized with
					   a table in regcomp.h */
	    "\\w",
	    "\\W",
	    "\\s",
	    "\\S",
	    "\\d",
	    "\\D",
	    "[:alnum:]",
	    "[:^alnum:]",
	    "[:alpha:]",
	    "[:^alpha:]",
	    "[:ascii:]",
	    "[:^ascii:]",
	    "[:ctrl:]",
	    "[:^ctrl:]",
	    "[:graph:]",
	    "[:^graph:]",
	    "[:lower:]",
	    "[:^lower:]",
	    "[:print:]",
	    "[:^print:]",
	    "[:punct:]",
	    "[:^punct:]",
	    "[:upper:]",
	    "[:!upper:]",
	    "[:xdigit:]",
	    "[:^xdigit:]"
	};

	if (o->flags & ANYOF_LOCALE)
	    sv_catpv(sv, "{loc}");
	if (o->flags & ANYOF_FOLD)
	    sv_catpv(sv, "{i}");
	Perl_sv_catpvf(aTHX_ sv, "[%s", PL_colors[0]);
	if (o->flags & ANYOF_INVERT)
	    sv_catpv(sv, "^");
	for (i = 0; i <= 256; i++) {
	    if (i < 256 && ANYOF_BITMAP_TEST(o,i)) {
		if (rangestart == -1)
		    rangestart = i;
	    } else if (rangestart != -1) {
		if (i <= rangestart + 3)
		    for (; rangestart < i; rangestart++)
			put_byte(sv, rangestart);
		else {
		    put_byte(sv, rangestart);
		    sv_catpv(sv, "-");
		    put_byte(sv, i - 1);
		}
		rangestart = -1;
	    }
	}
	if (o->flags & ANYOF_CLASS)
	    for (i = 0; i < sizeof(out)/sizeof(char*); i++)
		if (ANYOF_CLASS_TEST(o,i))
		    sv_catpv(sv, out[i]);
	Perl_sv_catpvf(aTHX_ sv, "%s]", PL_colors[1]);
    }
    else if (k == BRANCHJ && (OP(o) == UNLESSM || OP(o) == IFMATCH))
	Perl_sv_catpvf(aTHX_ sv, "[-%d]", o->flags);
#endif	/* DEBUGGING */
}

SV *
Perl_re_intuit_string(pTHX_ regexp *prog)
{				/* Assume that RE_INTUIT is set */
    DEBUG_r(
	{   STRLEN n_a;
	    char *s = SvPV(prog->check_substr,n_a);

	    if (!PL_colorset) reginitcolors();
	    PerlIO_printf(Perl_debug_log,
		      "%sUsing REx substr:%s `%s%.60s%s%s'\n",
		      PL_colors[4],PL_colors[5],PL_colors[0],
		      s,
		      PL_colors[1],
		      (strlen(s) > 60 ? "..." : ""));
	} );

    return prog->check_substr;
}

void
Perl_pregfree(pTHX_ struct regexp *r)
{
    dTHR;
    DEBUG_r(if (!PL_colorset) reginitcolors());

    if (!r || (--r->refcnt > 0))
	return;
    DEBUG_r(PerlIO_printf(Perl_debug_log,
		      "%sFreeing REx:%s `%s%.60s%s%s'\n",
		      PL_colors[4],PL_colors[5],PL_colors[0],
		      r->precomp,
		      PL_colors[1],
		      (strlen(r->precomp) > 60 ? "..." : "")));

    if (r->precomp)
	Safefree(r->precomp);
    if (RX_MATCH_COPIED(r))
	Safefree(r->subbeg);
    if (r->substrs) {
	if (r->anchored_substr)
	    SvREFCNT_dec(r->anchored_substr);
	if (r->float_substr)
	    SvREFCNT_dec(r->float_substr);
	Safefree(r->substrs);
    }
    if (r->data) {
	int n = r->data->count;
	AV* new_comppad = NULL;
	AV* old_comppad;
	SV** old_curpad;

	while (--n >= 0) {
	    switch (r->data->what[n]) {
	    case 's':
		SvREFCNT_dec((SV*)r->data->data[n]);
		break;
	    case 'f':
		Safefree(r->data->data[n]);
		break;
	    case 'p':
		new_comppad = (AV*)r->data->data[n];
		break;
	    case 'o':
		if (new_comppad == NULL)
		    Perl_croak(aTHX_ "panic: pregfree comppad");
		old_comppad = PL_comppad;
		old_curpad = PL_curpad;
		PL_comppad = new_comppad;
		PL_curpad = AvARRAY(new_comppad);
		op_free((OP_4tree*)r->data->data[n]);
		PL_comppad = old_comppad;
		PL_curpad = old_curpad;
		SvREFCNT_dec((SV*)new_comppad);
		new_comppad = NULL;
		break;
	    case 'n':
		break;
	    default:
		FAIL2("panic: regfree data code '%c'", r->data->what[n]);
	    }
	}
	Safefree(r->data->what);
	Safefree(r->data);
    }
    Safefree(r->startp);
    Safefree(r->endp);
    Safefree(r);
}

/*
 - regnext - dig the "next" pointer out of a node
 *
 * [Note, when REGALIGN is defined there are two places in regmatch()
 * that bypass this code for speed.]
 */
regnode *
Perl_regnext(pTHX_ register regnode *p)
{
    dTHR;
    register I32 offset;

    if (p == &PL_regdummy)
	return(NULL);

    offset = (reg_off_by_arg[OP(p)] ? ARG(p) : NEXT_OFF(p));
    if (offset == 0)
	return(NULL);

    return(p+offset);
}

STATIC void	
S_re_croak2(pTHX_ const char* pat1,const char* pat2,...)
{
    va_list args;
    STRLEN l1 = strlen(pat1);
    STRLEN l2 = strlen(pat2);
    char buf[512];
    SV *msv;
    char *message;

    if (l1 > 510)
	l1 = 510;
    if (l1 + l2 > 510)
	l2 = 510 - l1;
    Copy(pat1, buf, l1 , char);
    Copy(pat2, buf + l1, l2 , char);
    buf[l1 + l2] = '\n';
    buf[l1 + l2 + 1] = '\0';
#ifdef I_STDARG
    /* ANSI variant takes additional second argument */
    va_start(args, pat2);
#else
    va_start(args);
#endif
    msv = vmess(buf, &args);
    va_end(args);
    message = SvPV(msv,l1);
    if (l1 > 512)
	l1 = 512;
    Copy(message, buf, l1 , char);
    buf[l1] = '\0';			/* Overwrite \n */
    Perl_croak(aTHX_ "%s", buf);
}

/* XXX Here's a total kludge.  But we need to re-enter for swash routines. */

void
Perl_save_re_context(pTHX)
{                   
    dTHR;
    SAVEPPTR(PL_bostr);
    SAVEPPTR(PL_regprecomp);		/* uncompiled string. */
    SAVEI32(PL_regnpar);		/* () count. */
    SAVEI32(PL_regsize);		/* Code size. */
    SAVEI16(PL_regflags);		/* are we folding, multilining? */
    SAVEPPTR(PL_reginput);		/* String-input pointer. */
    SAVEPPTR(PL_regbol);		/* Beginning of input, for ^ check. */
    SAVEPPTR(PL_regeol);		/* End of input, for $ check. */
    SAVEVPTR(PL_regstartp);		/* Pointer to startp array. */
    SAVEVPTR(PL_regendp);		/* Ditto for endp. */
    SAVEVPTR(PL_reglastparen);		/* Similarly for lastparen. */
    SAVEPPTR(PL_regtill);		/* How far we are required to go. */
    SAVEI8(PL_regprev);			/* char before regbol, \n if none */
    SAVEVPTR(PL_reg_start_tmp);		/* from regexec.c */
    PL_reg_start_tmp = 0;
    SAVEFREEPV(PL_reg_start_tmp);
    SAVEI32(PL_reg_start_tmpl);		/* from regexec.c */
    PL_reg_start_tmpl = 0;
    SAVEVPTR(PL_regdata);
    SAVEI32(PL_reg_flags);		/* from regexec.c */
    SAVEI32(PL_reg_eval_set);		/* from regexec.c */
    SAVEI32(PL_regnarrate);		/* from regexec.c */
    SAVEVPTR(PL_regprogram);		/* from regexec.c */
    SAVEINT(PL_regindent);		/* from regexec.c */
    SAVEVPTR(PL_regcc);			/* from regexec.c */
    SAVEVPTR(PL_curcop);
    SAVEVPTR(PL_regcomp_rx);		/* from regcomp.c */
    SAVEI32(PL_regseen);		/* from regcomp.c */
    SAVEI32(PL_regsawback);		/* Did we see \1, ...? */
    SAVEI32(PL_regnaughty);		/* How bad is this pattern? */
    SAVEVPTR(PL_regcode);		/* Code-emit pointer; &regdummy = don't */
    SAVEPPTR(PL_regxend);		/* End of input for compile */
    SAVEPPTR(PL_regcomp_parse);		/* Input-scan pointer. */
    SAVEVPTR(PL_reg_call_cc);		/* from regexec.c */
    SAVEVPTR(PL_reg_re);		/* from regexec.c */
    SAVEPPTR(PL_reg_ganch);		/* from regexec.c */
    SAVESPTR(PL_reg_sv);		/* from regexec.c */
    SAVEVPTR(PL_reg_magic);		/* from regexec.c */
    SAVEI32(PL_reg_oldpos);			/* from regexec.c */
    SAVEVPTR(PL_reg_oldcurpm);		/* from regexec.c */
    SAVEVPTR(PL_reg_curpm);		/* from regexec.c */
#ifdef DEBUGGING
    SAVEPPTR(PL_reg_starttry);		/* from regexec.c */    
#endif
}

#ifdef PERL_OBJECT
#include "XSUB.h"
#undef this
#define this pPerl
#endif

static void
clear_re(pTHXo_ void *r)
{
    ReREFCNT_dec((regexp *)r);
}