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
use alloc::{
borrow::Cow, boxed::Box, string::String, string::ToString, sync::Arc, vec,
vec::Vec,
};
use crate::{
error::Error,
hir::{self, Hir},
int::NonMaxUsize,
interpolate,
nfa::{self, NFA},
pikevm::{self, Cache, PikeVM},
pool::CachePool,
};
/// A compiled regular expression for searching Unicode haystacks.
///
/// A `Regex` can be used to search haystacks, split haystacks into substrings
/// or replace substrings in a haystack with a different substring. All
/// searching is done with an implicit `(?s:.)*?` at the beginning and end of
/// an pattern. To force an expression to match the whole string (or a prefix
/// or a suffix), you must use an anchor like `^` or `$` (or `\A` and `\z`).
///
/// While this crate will handle Unicode strings (whether in the regular
/// expression or in the haystack), all positions returned are **byte
/// offsets**. Every byte offset is guaranteed to be at a Unicode code point
/// boundary. That is, all offsets returned by the `Regex` API are guaranteed
/// to be ranges that can slice a `&str` without panicking.
///
/// The only methods that allocate new strings are the string replacement
/// methods. All other methods (searching and splitting) return borrowed
/// references into the haystack given.
///
/// # Example
///
/// Find the offsets of a US phone number:
///
/// ```
/// use regex_lite::Regex;
///
/// let re = Regex::new("[0-9]{3}-[0-9]{3}-[0-9]{4}").unwrap();
/// let m = re.find("phone: 111-222-3333").unwrap();
/// assert_eq!(7..19, m.range());
/// ```
///
/// # Example: extracting capture groups
///
/// A common way to use regexes is with capture groups. That is, instead of
/// just looking for matches of an entire regex, parentheses are used to create
/// groups that represent part of the match.
///
/// For example, consider a haystack with multiple lines, and each line has
/// three whitespace delimited fields where the second field is expected to be
/// a number and the third field a boolean. To make this convenient, we use
/// the [`Captures::extract`] API to put the strings that match each group
/// into a fixed size array:
///
/// ```
/// use regex_lite::Regex;
///
/// let hay = "
/// rabbit 54 true
/// groundhog 2 true
/// does not match
/// fox 109 false
/// ";
/// let re = Regex::new(r"(?m)^\s*(\S+)\s+([0-9]+)\s+(true|false)\s*$").unwrap();
/// let mut fields: Vec<(&str, i64, bool)> = vec![];
/// for (_, [f1, f2, f3]) in re.captures_iter(hay).map(|caps| caps.extract()) {
/// fields.push((f1, f2.parse()?, f3.parse()?));
/// }
/// assert_eq!(fields, vec![
/// ("rabbit", 54, true),
/// ("groundhog", 2, true),
/// ("fox", 109, false),
/// ]);
///
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub struct Regex {
pikevm: Arc<PikeVM>,
pool: CachePool,
}
impl Clone for Regex {
fn clone(&self) -> Regex {
let pikevm = Arc::clone(&self.pikevm);
let pool = {
let pikevm = Arc::clone(&self.pikevm);
let create = Box::new(move || Cache::new(&pikevm));
CachePool::new(create)
};
Regex { pikevm, pool }
}
}
impl core::fmt::Display for Regex {
/// Shows the original regular expression.
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "{}", self.as_str())
}
}
impl core::fmt::Debug for Regex {
/// Shows the original regular expression.
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_tuple("Regex").field(&self.as_str()).finish()
}
}
impl core::str::FromStr for Regex {
type Err = Error;
/// Attempts to parse a string into a regular expression
fn from_str(s: &str) -> Result<Regex, Error> {
Regex::new(s)
}
}
impl TryFrom<&str> for Regex {
type Error = Error;
/// Attempts to parse a string into a regular expression
fn try_from(s: &str) -> Result<Regex, Error> {
Regex::new(s)
}
}
impl TryFrom<String> for Regex {
type Error = Error;
/// Attempts to parse a string into a regular expression
fn try_from(s: String) -> Result<Regex, Error> {
Regex::new(&s)
}
}
/// Core regular expression methods.
impl Regex {
/// Compiles a regular expression. Once compiled, it can be used repeatedly
/// to search, split or replace substrings in a haystack.
///
/// Note that regex compilation tends to be a somewhat expensive process,
/// and unlike higher level environments, compilation is not automatically
/// cached for you. One should endeavor to compile a regex once and then
/// reuse it. For example, it's a bad idea to compile the same regex
/// repeatedly in a loop.
///
/// # Errors
///
/// If an invalid pattern is given, then an error is returned.
/// An error is also returned if the pattern is valid, but would
/// produce a regex that is bigger than the configured size limit via
/// [`RegexBuilder::size_limit`]. (A reasonable size limit is enabled by
/// default.)
///
/// # Example
///
/// ```
/// use regex_lite::Regex;
///
/// // An Invalid pattern because of an unclosed parenthesis
/// assert!(Regex::new(r"foo(bar").is_err());
/// // An invalid pattern because the regex would be too big
/// // because Unicode tends to inflate things.
/// assert!(Regex::new(r"\w{1000000}").is_err());
/// ```
pub fn new(pattern: &str) -> Result<Regex, Error> {
RegexBuilder::new(pattern).build()
}
/// Returns true if and only if there is a match for the regex anywhere
/// in the haystack given.
///
/// It is recommended to use this method if all you need to do is test
/// whether a match exists, since the underlying matching engine may be
/// able to do less work.
///
/// # Example
///
/// Test if some haystack contains at least one word with exactly 13
/// word characters:
///
/// ```
/// use regex_lite::Regex;
///
/// let re = Regex::new(r"\b\w{13}\b").unwrap();
/// let hay = "I categorically deny having triskaidekaphobia.";
/// assert!(re.is_match(hay));
/// ```
#[inline]
pub fn is_match(&self, haystack: &str) -> bool {
self.is_match_at(haystack, 0)
}
/// This routine searches for the first match of this regex in the
/// haystack given, and if found, returns a [`Match`]. The `Match`
/// provides access to both the byte offsets of the match and the actual
/// substring that matched.
///
/// Note that this should only be used if you want to find the entire
/// match. If instead you just want to test the existence of a match,
/// it's potentially faster to use `Regex::is_match(hay)` instead of
/// `Regex::find(hay).is_some()`.
///
/// # Example
///
/// Find the first word with exactly 13 word characters:
///
/// ```
/// use regex_lite::Regex;
///
/// let re = Regex::new(r"\b\w{13}\b").unwrap();
/// let hay = "I categorically deny having triskaidekaphobia.";
/// let mat = re.find(hay).unwrap();
/// assert_eq!(2..15, mat.range());
/// assert_eq!("categorically", mat.as_str());
/// ```
#[inline]
pub fn find<'h>(&self, haystack: &'h str) -> Option<Match<'h>> {
self.find_at(haystack, 0)
}
/// Returns an iterator that yields successive non-overlapping matches in
/// the given haystack. The iterator yields values of type [`Match`].
///
/// # Time complexity
///
/// Note that since `find_iter` runs potentially many searches on the
/// haystack and since each search has worst case `O(m * n)` time
/// complexity, the overall worst case time complexity for iteration is
/// `O(m * n^2)`.
///
/// # Example
///
/// Find every word with exactly 13 word characters:
///
/// ```
/// use regex_lite::Regex;
///
/// let re = Regex::new(r"\b\w{13}\b").unwrap();
/// let hay = "Retroactively relinquishing remunerations is reprehensible.";
/// let matches: Vec<_> = re.find_iter(hay).map(|m| m.as_str()).collect();
/// assert_eq!(matches, vec![
/// "Retroactively",
/// "relinquishing",
/// "remunerations",
/// "reprehensible",
/// ]);
/// ```
#[inline]
pub fn find_iter<'r, 'h>(&'r self, haystack: &'h str) -> Matches<'r, 'h> {
Matches {
haystack,
it: self.pikevm.find_iter(self.pool.get(), haystack.as_bytes()),
}
}
/// This routine searches for the first match of this regex in the haystack
/// given, and if found, returns not only the overall match but also the
/// matches of each capture group in the regex. If no match is found, then
/// `None` is returned.
///
/// Capture group `0` always corresponds to an implicit unnamed group that
/// includes the entire match. If a match is found, this group is always
/// present. Subsequent groups may be named and are numbered, starting
/// at 1, by the order in which the opening parenthesis appears in the
/// pattern. For example, in the pattern `(?<a>.(?<b>.))(?<c>.)`, `a`,
/// `b` and `c` correspond to capture group indices `1`, `2` and `3`,
/// respectively.
///
/// You should only use `captures` if you need access to the capture group
/// matches. Otherwise, [`Regex::find`] is generally faster for discovering
/// just the overall match.
///
/// # Example
///
/// Say you have some haystack with movie names and their release years,
/// like "'Citizen Kane' (1941)". It'd be nice if we could search for
/// substrings looking like that, while also extracting the movie name and
/// its release year separately. The example below shows how to do that.
///
/// ```
/// use regex_lite::Regex;
///
/// let re = Regex::new(r"'([^']+)'\s+\((\d{4})\)").unwrap();
/// let hay = "Not my favorite movie: 'Citizen Kane' (1941).";
/// let caps = re.captures(hay).unwrap();
/// assert_eq!(caps.get(0).unwrap().as_str(), "'Citizen Kane' (1941)");
/// assert_eq!(caps.get(1).unwrap().as_str(), "Citizen Kane");
/// assert_eq!(caps.get(2).unwrap().as_str(), "1941");
/// // You can also access the groups by index using the Index notation.
/// // Note that this will panic on an invalid index. In this case, these
/// // accesses are always correct because the overall regex will only
/// // match when these capture groups match.
/// assert_eq!(&caps[0], "'Citizen Kane' (1941)");
/// assert_eq!(&caps[1], "Citizen Kane");
/// assert_eq!(&caps[2], "1941");
/// ```
///
/// Note that the full match is at capture group `0`. Each subsequent
/// capture group is indexed by the order of its opening `(`.
///
/// We can make this example a bit clearer by using *named* capture groups:
///
/// ```
/// use regex_lite::Regex;
///
/// let re = Regex::new(r"'(?<title>[^']+)'\s+\((?<year>\d{4})\)").unwrap();
/// let hay = "Not my favorite movie: 'Citizen Kane' (1941).";
/// let caps = re.captures(hay).unwrap();
/// assert_eq!(caps.get(0).unwrap().as_str(), "'Citizen Kane' (1941)");
/// assert_eq!(caps.name("title").unwrap().as_str(), "Citizen Kane");
/// assert_eq!(caps.name("year").unwrap().as_str(), "1941");
/// // You can also access the groups by name using the Index notation.
/// // Note that this will panic on an invalid group name. In this case,
/// // these accesses are always correct because the overall regex will
/// // only match when these capture groups match.
/// assert_eq!(&caps[0], "'Citizen Kane' (1941)");
/// assert_eq!(&caps["title"], "Citizen Kane");
/// assert_eq!(&caps["year"], "1941");
/// ```
///
/// Here we name the capture groups, which we can access with the `name`
/// method or the `Index` notation with a `&str`. Note that the named
/// capture groups are still accessible with `get` or the `Index` notation
/// with a `usize`.
///
/// The `0`th capture group is always unnamed, so it must always be
/// accessed with `get(0)` or `[0]`.
///
/// Finally, one other way to to get the matched substrings is with the
/// [`Captures::extract`] API:
///
/// ```
/// use regex_lite::Regex;
///
/// let re = Regex::new(r"'([^']+)'\s+\((\d{4})\)").unwrap();
/// let hay = "Not my favorite movie: 'Citizen Kane' (1941).";
/// let (full, [title, year]) = re.captures(hay).unwrap().extract();
/// assert_eq!(full, "'Citizen Kane' (1941)");
/// assert_eq!(title, "Citizen Kane");
/// assert_eq!(year, "1941");
/// ```
#[inline]
pub fn captures<'h>(&self, haystack: &'h str) -> Option<Captures<'h>> {
self.captures_at(haystack, 0)
}
/// Returns an iterator that yields successive non-overlapping matches in
/// the given haystack. The iterator yields values of type [`Captures`].
///
/// This is the same as [`Regex::find_iter`], but instead of only providing
/// access to the overall match, each value yield includes access to the
/// matches of all capture groups in the regex. Reporting this extra match
/// data is potentially costly, so callers should only use `captures_iter`
/// over `find_iter` when they actually need access to the capture group
/// matches.
///
/// # Time complexity
///
/// Note that since `captures_iter` runs potentially many searches on the
/// haystack and since each search has worst case `O(m * n)` time
/// complexity, the overall worst case time complexity for iteration is
/// `O(m * n^2)`.
///
/// # Example
///
/// We can use this to find all movie titles and their release years in
/// some haystack, where the movie is formatted like "'Title' (xxxx)":
///
/// ```
/// use regex_lite::Regex;
///
/// let re = Regex::new(r"'([^']+)'\s+\(([0-9]{4})\)").unwrap();
/// let hay = "'Citizen Kane' (1941), 'The Wizard of Oz' (1939), 'M' (1931).";
/// let mut movies = vec![];
/// for (_, [title, year]) in re.captures_iter(hay).map(|c| c.extract()) {
/// movies.push((title, year.parse::<i64>()?));
/// }
/// assert_eq!(movies, vec![
/// ("Citizen Kane", 1941),
/// ("The Wizard of Oz", 1939),
/// ("M", 1931),
/// ]);
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
///
/// Or with named groups:
///
/// ```
/// use regex_lite::Regex;
///
/// let re = Regex::new(r"'(?<title>[^']+)'\s+\((?<year>[0-9]{4})\)").unwrap();
/// let hay = "'Citizen Kane' (1941), 'The Wizard of Oz' (1939), 'M' (1931).";
/// let mut it = re.captures_iter(hay);
///
/// let caps = it.next().unwrap();
/// assert_eq!(&caps["title"], "Citizen Kane");
/// assert_eq!(&caps["year"], "1941");
///
/// let caps = it.next().unwrap();
/// assert_eq!(&caps["title"], "The Wizard of Oz");
/// assert_eq!(&caps["year"], "1939");
///
/// let caps = it.next().unwrap();
/// assert_eq!(&caps["title"], "M");
/// assert_eq!(&caps["year"], "1931");
/// ```
#[inline]
pub fn captures_iter<'r, 'h>(
&'r self,
haystack: &'h str,
) -> CaptureMatches<'r, 'h> {
CaptureMatches {
haystack,
re: self,
it: self
.pikevm
.captures_iter(self.pool.get(), haystack.as_bytes()),
}
}
/// Returns an iterator of substrings of the haystack given, delimited by a
/// match of the regex. Namely, each element of the iterator corresponds to
/// a part of the haystack that *isn't* matched by the regular expression.
///
/// # Time complexity
///
/// Since iterators over all matches requires running potentially many
/// searches on the haystack, and since each search has worst case
/// `O(m * n)` time complexity, the overall worst case time complexity for
/// this routine is `O(m * n^2)`.
///
/// # Example
///
/// To split a string delimited by arbitrary amounts of spaces or tabs:
///
/// ```
/// use regex_lite::Regex;
///
/// let re = Regex::new(r"[ \t]+").unwrap();
/// let hay = "a b \t c\td e";
/// let fields: Vec<&str> = re.split(hay).collect();
/// assert_eq!(fields, vec!["a", "b", "c", "d", "e"]);
/// ```
///
/// # Example: more cases
///
/// Basic usage:
///
/// ```
/// use regex_lite::Regex;
///
/// let re = Regex::new(r" ").unwrap();
/// let hay = "Mary had a little lamb";
/// let got: Vec<&str> = re.split(hay).collect();
/// assert_eq!(got, vec!["Mary", "had", "a", "little", "lamb"]);
///
/// let re = Regex::new(r"X").unwrap();
/// let hay = "";
/// let got: Vec<&str> = re.split(hay).collect();
/// assert_eq!(got, vec![""]);
///
/// let re = Regex::new(r"X").unwrap();
/// let hay = "lionXXtigerXleopard";
/// let got: Vec<&str> = re.split(hay).collect();
/// assert_eq!(got, vec!["lion", "", "tiger", "leopard"]);
///
/// let re = Regex::new(r"::").unwrap();
/// let hay = "lion::tiger::leopard";
/// let got: Vec<&str> = re.split(hay).collect();
/// assert_eq!(got, vec!["lion", "tiger", "leopard"]);
/// ```
///
/// If a haystack contains multiple contiguous matches, you will end up
/// with empty spans yielded by the iterator:
///
/// ```
/// use regex_lite::Regex;
///
/// let re = Regex::new(r"X").unwrap();
/// let hay = "XXXXaXXbXc";
/// let got: Vec<&str> = re.split(hay).collect();
/// assert_eq!(got, vec!["", "", "", "", "a", "", "b", "c"]);
///
/// let re = Regex::new(r"/").unwrap();
/// let hay = "(///)";
/// let got: Vec<&str> = re.split(hay).collect();
/// assert_eq!(got, vec!["(", "", "", ")"]);
/// ```
///
/// Separators at the start or end of a haystack are neighbored by empty
/// substring.
///
/// ```
/// use regex_lite::Regex;
///
/// let re = Regex::new(r"0").unwrap();
/// let hay = "010";
/// let got: Vec<&str> = re.split(hay).collect();
/// assert_eq!(got, vec!["", "1", ""]);
/// ```
///
/// When the empty string is used as a regex, it splits at every valid
/// UTF-8 boundary by default (which includes the beginning and end of the
/// haystack):
///
/// ```
/// use regex_lite::Regex;
///
/// let re = Regex::new(r"").unwrap();
/// let hay = "rust";
/// let got: Vec<&str> = re.split(hay).collect();
/// assert_eq!(got, vec!["", "r", "u", "s", "t", ""]);
///
/// // Splitting by an empty string is UTF-8 aware by default!
/// let re = Regex::new(r"").unwrap();
/// let hay = "☃";
/// let got: Vec<&str> = re.split(hay).collect();
/// assert_eq!(got, vec!["", "☃", ""]);
/// ```
///
/// Contiguous separators (commonly shows up with whitespace), can lead to
/// possibly surprising behavior. For example, this code is correct:
///
/// ```
/// use regex_lite::Regex;
///
/// let re = Regex::new(r" ").unwrap();
/// let hay = " a b c";
/// let got: Vec<&str> = re.split(hay).collect();
/// assert_eq!(got, vec!["", "", "", "", "a", "", "b", "c"]);
/// ```
///
/// It does *not* give you `["a", "b", "c"]`. For that behavior, you'd want
/// to match contiguous space characters:
///
/// ```
/// use regex_lite::Regex;
///
/// let re = Regex::new(r" +").unwrap();
/// let hay = " a b c";
/// let got: Vec<&str> = re.split(hay).collect();
/// // N.B. This does still include a leading empty span because ' +'
/// // matches at the beginning of the haystack.
/// assert_eq!(got, vec!["", "a", "b", "c"]);
/// ```
#[inline]
pub fn split<'r, 'h>(&'r self, haystack: &'h str) -> Split<'r, 'h> {
Split { haystack, finder: self.find_iter(haystack), last: 0 }
}
/// Returns an iterator of at most `limit` substrings of the haystack
/// given, delimited by a match of the regex. (A `limit` of `0` will return
/// no substrings.) Namely, each element of the iterator corresponds to a
/// part of the haystack that *isn't* matched by the regular expression.
/// The remainder of the haystack that is not split will be the last
/// element in the iterator.
///
/// # Time complexity
///
/// Since iterators over all matches requires running potentially many
/// searches on the haystack, and since each search has worst case
/// `O(m * n)` time complexity, the overall worst case time complexity for
/// this routine is `O(m * n^2)`.
///
/// Although note that the worst case time here has an upper bound given
/// by the `limit` parameter.
///
/// # Example
///
/// Get the first two words in some haystack:
///
/// ```
/// use regex_lite::Regex;
///
/// let re = Regex::new(r"\W+").unwrap();
/// let hay = "Hey! How are you?";
/// let fields: Vec<&str> = re.splitn(hay, 3).collect();
/// assert_eq!(fields, vec!["Hey", "How", "are you?"]);
/// ```
///
/// # Examples: more cases
///
/// ```
/// use regex_lite::Regex;
///
/// let re = Regex::new(r" ").unwrap();
/// let hay = "Mary had a little lamb";
/// let got: Vec<&str> = re.splitn(hay, 3).collect();
/// assert_eq!(got, vec!["Mary", "had", "a little lamb"]);
///
/// let re = Regex::new(r"X").unwrap();
/// let hay = "";
/// let got: Vec<&str> = re.splitn(hay, 3).collect();
/// assert_eq!(got, vec![""]);
///
/// let re = Regex::new(r"X").unwrap();
/// let hay = "lionXXtigerXleopard";
/// let got: Vec<&str> = re.splitn(hay, 3).collect();
/// assert_eq!(got, vec!["lion", "", "tigerXleopard"]);
///
/// let re = Regex::new(r"::").unwrap();
/// let hay = "lion::tiger::leopard";
/// let got: Vec<&str> = re.splitn(hay, 2).collect();
/// assert_eq!(got, vec!["lion", "tiger::leopard"]);
///
/// let re = Regex::new(r"X").unwrap();
/// let hay = "abcXdef";
/// let got: Vec<&str> = re.splitn(hay, 1).collect();
/// assert_eq!(got, vec!["abcXdef"]);
///
/// let re = Regex::new(r"X").unwrap();
/// let hay = "abcdef";
/// let got: Vec<&str> = re.splitn(hay, 2).collect();
/// assert_eq!(got, vec!["abcdef"]);
///
/// let re = Regex::new(r"X").unwrap();
/// let hay = "abcXdef";
/// let got: Vec<&str> = re.splitn(hay, 0).collect();
/// assert!(got.is_empty());
/// ```
#[inline]
pub fn splitn<'r, 'h>(
&'r self,
haystack: &'h str,
limit: usize,
) -> SplitN<'r, 'h> {
SplitN { splits: self.split(haystack), limit }
}
/// Replaces the leftmost-first match in the given haystack with the
/// replacement provided. The replacement can be a regular string (where
/// `$N` and `$name` are expanded to match capture groups) or a function
/// that takes a [`Captures`] and returns the replaced string.
///
/// If no match is found, then the haystack is returned unchanged. In that
/// case, this implementation will likely return a `Cow::Borrowed` value
/// such that no allocation is performed.
///
/// # Replacement string syntax
///
/// All instances of `$ref` in the replacement string are replaced with
/// the substring corresponding to the capture group identified by `ref`.
///
/// `ref` may be an integer corresponding to the index of the capture group
/// (counted by order of opening parenthesis where `0` is the entire match)
/// or it can be a name (consisting of letters, digits or underscores)
/// corresponding to a named capture group.
///
/// If `ref` isn't a valid capture group (whether the name doesn't exist or
/// isn't a valid index), then it is replaced with the empty string.
///
/// The longest possible name is used. For example, `$1a` looks up the
/// capture group named `1a` and not the capture group at index `1`. To
/// exert more precise control over the name, use braces, e.g., `${1}a`.
///
/// To write a literal `$` use `$$`.
///
/// # Example
///
/// Note that this function is polymorphic with respect to the replacement.
/// In typical usage, this can just be a normal string:
///
/// ```
/// use regex_lite::Regex;
///
/// let re = Regex::new(r"[^01]+").unwrap();
/// assert_eq!(re.replace("1078910", ""), "1010");
/// ```
///
/// But anything satisfying the [`Replacer`] trait will work. For example,
/// a closure of type `|&Captures| -> String` provides direct access to the
/// captures corresponding to a match. This allows one to access capturing
/// group matches easily:
///
/// ```
/// use regex_lite::{Captures, Regex};
///
/// let re = Regex::new(r"([^,\s]+),\s+(\S+)").unwrap();
/// let result = re.replace("Springsteen, Bruce", |caps: &Captures| {
/// format!("{} {}", &caps[2], &caps[1])
/// });
/// assert_eq!(result, "Bruce Springsteen");
/// ```
///
/// But this is a bit cumbersome to use all the time. Instead, a simple
/// syntax is supported (as described above) that expands `$name` into the
/// corresponding capture group. Here's the last example, but using this
/// expansion technique with named capture groups:
///
/// ```
/// use regex_lite::Regex;
///
/// let re = Regex::new(r"(?<last>[^,\s]+),\s+(?<first>\S+)").unwrap();
/// let result = re.replace("Springsteen, Bruce", "$first $last");
/// assert_eq!(result, "Bruce Springsteen");
/// ```
///
/// Note that using `$2` instead of `$first` or `$1` instead of `$last`
/// would produce the same result. To write a literal `$` use `$$`.
///
/// Sometimes the replacement string requires use of curly braces to
/// delineate a capture group replacement when it is adjacent to some other
/// literal text. For example, if we wanted to join two words together with
/// an underscore:
///
/// ```
/// use regex_lite::Regex;
///
/// let re = Regex::new(r"(?<first>\w+)\s+(?<second>\w+)").unwrap();
/// let result = re.replace("deep fried", "${first}_$second");
/// assert_eq!(result, "deep_fried");
/// ```
///
/// Without the curly braces, the capture group name `first_` would be
/// used, and since it doesn't exist, it would be replaced with the empty
/// string.
///
/// Finally, sometimes you just want to replace a literal string with no
/// regard for capturing group expansion. This can be done by wrapping a
/// string with [`NoExpand`]:
///
/// ```
/// use regex_lite::{NoExpand, Regex};
///
/// let re = Regex::new(r"(?<last>[^,\s]+),\s+(\S+)").unwrap();
/// let result = re.replace("Springsteen, Bruce", NoExpand("$2 $last"));
/// assert_eq!(result, "$2 $last");
/// ```
///
/// Using `NoExpand` may also be faster, since the replacement string won't
/// need to be parsed for the `$` syntax.
#[inline]
pub fn replace<'h, R: Replacer>(
&self,
haystack: &'h str,
rep: R,
) -> Cow<'h, str> {
self.replacen(haystack, 1, rep)
}
/// Replaces all non-overlapping matches in the haystack with the
/// replacement provided. This is the same as calling `replacen` with
/// `limit` set to `0`.
///
/// The documentation for [`Regex::replace`] goes into more detail about
/// what kinds of replacement strings are supported.
///
/// # Time complexity
///
/// Since iterators over all matches requires running potentially many
/// searches on the haystack, and since each search has worst case
/// `O(m * n)` time complexity, the overall worst case time complexity for
/// this routine is `O(m * n^2)`.
///
/// # Fallibility
///
/// If you need to write a replacement routine where any individual
/// replacement might "fail," doing so with this API isn't really feasible
/// because there's no way to stop the search process if a replacement
/// fails. Instead, if you need this functionality, you should consider
/// implementing your own replacement routine:
///
/// ```
/// use regex_lite::{Captures, Regex};
///
/// fn replace_all<E>(
/// re: &Regex,
/// haystack: &str,
/// replacement: impl Fn(&Captures) -> Result<String, E>,
/// ) -> Result<String, E> {
/// let mut new = String::with_capacity(haystack.len());
/// let mut last_match = 0;
/// for caps in re.captures_iter(haystack) {
/// let m = caps.get(0).unwrap();
/// new.push_str(&haystack[last_match..m.start()]);
/// new.push_str(&replacement(&caps)?);
/// last_match = m.end();
/// }
/// new.push_str(&haystack[last_match..]);
/// Ok(new)
/// }
///
/// // Let's replace each word with the number of bytes in that word.
/// // But if we see a word that is "too long," we'll give up.
/// let re = Regex::new(r"\w+").unwrap();
/// let replacement = |caps: &Captures| -> Result<String, &'static str> {
/// if caps[0].len() >= 5 {
/// return Err("word too long");
/// }
/// Ok(caps[0].len().to_string())
/// };
/// assert_eq!(
/// Ok("2 3 3 3?".to_string()),
/// replace_all(&re, "hi how are you?", &replacement),
/// );
/// assert!(replace_all(&re, "hi there", &replacement).is_err());
/// ```
///
/// # Example
///
/// This example shows how to flip the order of whitespace delimited
/// fields, and normalizes the whitespace that delimits the fields:
///
/// ```
/// use regex_lite::Regex;
///
/// let re = Regex::new(r"(?m)^(\S+)\s+(\S+)$").unwrap();
/// let hay = "
/// Greetings 1973
/// Wild\t1973
/// BornToRun\t\t\t\t1975
/// Darkness 1978
/// TheRiver 1980
/// ";
/// let new = re.replace_all(hay, "$2 $1");
/// assert_eq!(new, "
/// 1973 Greetings
/// 1973 Wild
/// 1975 BornToRun
/// 1978 Darkness
/// 1980 TheRiver
/// ");
/// ```
#[inline]
pub fn replace_all<'h, R: Replacer>(
&self,
haystack: &'h str,
rep: R,
) -> Cow<'h, str> {
self.replacen(haystack, 0, rep)
}
/// Replaces at most `limit` non-overlapping matches in the haystack with
/// the replacement provided. If `limit` is `0`, then all non-overlapping
/// matches are replaced. That is, `Regex::replace_all(hay, rep)` is
/// equivalent to `Regex::replacen(hay, 0, rep)`.
///
/// The documentation for [`Regex::replace`] goes into more detail about
/// what kinds of replacement strings are supported.
///
/// # Time complexity
///
/// Since iterators over all matches requires running potentially many
/// searches on the haystack, and since each search has worst case
/// `O(m * n)` time complexity, the overall worst case time complexity for
/// this routine is `O(m * n^2)`.
///
/// Although note that the worst case time here has an upper bound given
/// by the `limit` parameter.
///
/// # Fallibility
///
/// See the corresponding section in the docs for [`Regex::replace_all`]
/// for tips on how to deal with a replacement routine that can fail.
///
/// # Example
///
/// This example shows how to flip the order of whitespace delimited
/// fields, and normalizes the whitespace that delimits the fields. But we
/// only do it for the first two matches.
///
/// ```
/// use regex_lite::Regex;
///
/// let re = Regex::new(r"(?m)^(\S+)\s+(\S+)$").unwrap();
/// let hay = "
/// Greetings 1973
/// Wild\t1973
/// BornToRun\t\t\t\t1975
/// Darkness 1978
/// TheRiver 1980
/// ";
/// let new = re.replacen(hay, 2, "$2 $1");
/// assert_eq!(new, "
/// 1973 Greetings
/// 1973 Wild
/// BornToRun\t\t\t\t1975
/// Darkness 1978
/// TheRiver 1980
/// ");
/// ```
#[inline]
pub fn replacen<'h, R: Replacer>(
&self,
haystack: &'h str,
limit: usize,
mut rep: R,
) -> Cow<'h, str> {
// If we know that the replacement doesn't have any capture expansions,
// then we can use the fast path. The fast path can make a tremendous
// difference:
//
// 1) We use `find_iter` instead of `captures_iter`. Not asking for
// captures generally makes the regex engines faster.
// 2) We don't need to look up all of the capture groups and do
// replacements inside the replacement string. We just push it
// at each match and be done with it.
if let Some(rep) = rep.no_expansion() {
let mut it = self.find_iter(haystack).enumerate().peekable();
if it.peek().is_none() {
return Cow::Borrowed(haystack);
}
let mut new = String::with_capacity(haystack.len());
let mut last_match = 0;
for (i, m) in it {
new.push_str(&haystack[last_match..m.start()]);
new.push_str(&rep);
last_match = m.end();
if limit > 0 && i >= limit - 1 {
break;
}
}
new.push_str(&haystack[last_match..]);
return Cow::Owned(new);
}
// The slower path, which we use if the replacement needs access to
// capture groups.
let mut it = self.captures_iter(haystack).enumerate().peekable();
if it.peek().is_none() {
return Cow::Borrowed(haystack);
}
let mut new = String::with_capacity(haystack.len());
let mut last_match = 0;
for (i, cap) in it {
// unwrap on 0 is OK because captures only reports matches
let m = cap.get(0).unwrap();
new.push_str(&haystack[last_match..m.start()]);
rep.replace_append(&cap, &mut new);
last_match = m.end();
if limit > 0 && i >= limit - 1 {
break;
}
}
new.push_str(&haystack[last_match..]);
Cow::Owned(new)
}
}
/// A group of advanced or "lower level" search methods. Some methods permit
/// starting the search at a position greater than `0` in the haystack. Other
/// methods permit reusing allocations, for example, when extracting the
/// matches for capture groups.
impl Regex {
/// Returns the end byte offset of the first match in the haystack given.
///
/// This method may have the same performance characteristics as
/// `is_match`. Behaviorlly, it doesn't just report whether it match
/// occurs, but also the end offset for a match. In particular, the offset
/// returned *may be shorter* than the proper end of the leftmost-first
/// match that you would find via [`Regex::find`].
///
/// Note that it is not guaranteed that this routine finds the shortest or
/// "earliest" possible match. Instead, the main idea of this API is that
/// it returns the offset at the point at which the internal regex engine
/// has determined that a match has occurred. This may vary depending on
/// which internal regex engine is used, and thus, the offset itself may
/// change based on internal heuristics.
///
/// # Example
///
/// Typically, `a+` would match the entire first sequence of `a` in some
/// haystack, but `shortest_match` *may* give up as soon as it sees the
/// first `a`.
///
/// ```
/// use regex_lite::Regex;
///
/// let re = Regex::new(r"a+").unwrap();
/// let offset = re.shortest_match("aaaaa").unwrap();
/// assert_eq!(offset, 1);
/// ```
#[inline]
pub fn shortest_match(&self, haystack: &str) -> Option<usize> {
self.shortest_match_at(haystack, 0)
}
/// Returns the same as [`Regex::shortest_match`], but starts the search at
/// the given offset.
///
/// The significance of the starting point is that it takes the surrounding
/// context into consideration. For example, the `\A` anchor can only match
/// when `start == 0`.
///
/// If a match is found, the offset returned is relative to the beginning
/// of the haystack, not the beginning of the search.
///
/// # Panics
///
/// This panics when `start >= haystack.len() + 1`.
///
/// # Example
///
/// This example shows the significance of `start` by demonstrating how it
/// can be used to permit look-around assertions in a regex to take the
/// surrounding context into account.
///
/// ```
/// use regex_lite::Regex;
///
/// let re = Regex::new(r"\bchew\b").unwrap();
/// let hay = "eschew";
/// // We get a match here, but it's probably not intended.
/// assert_eq!(re.shortest_match(&hay[2..]), Some(4));
/// // No match because the assertions take the context into account.
/// assert_eq!(re.shortest_match_at(hay, 2), None);
/// ```
#[inline]
pub fn shortest_match_at(
&self,
haystack: &str,
start: usize,
) -> Option<usize> {
let mut cache = self.pool.get();
let mut slots = [None, None];
let matched = self.pikevm.search(
&mut cache,
haystack.as_bytes(),
start,
haystack.len(),
true,
&mut slots,
);
if !matched {
return None;
}
Some(slots[1].unwrap().get())
}
/// Returns the same as [`Regex::is_match`], but starts the search at the
/// given offset.
///
/// The significance of the starting point is that it takes the surrounding
/// context into consideration. For example, the `\A` anchor can only
/// match when `start == 0`.
///
/// # Panics
///
/// This panics when `start >= haystack.len() + 1`.
///
/// # Example
///
/// This example shows the significance of `start` by demonstrating how it
/// can be used to permit look-around assertions in a regex to take the
/// surrounding context into account.
///
/// ```
/// use regex_lite::Regex;
///
/// let re = Regex::new(r"\bchew\b").unwrap();
/// let hay = "eschew";
/// // We get a match here, but it's probably not intended.
/// assert!(re.is_match(&hay[2..]));
/// // No match because the assertions take the context into account.
/// assert!(!re.is_match_at(hay, 2));
/// ```
#[inline]
pub fn is_match_at(&self, haystack: &str, start: usize) -> bool {
let mut cache = self.pool.get();
self.pikevm.search(
&mut cache,
haystack.as_bytes(),
start,
haystack.len(),
true,
&mut [],
)
}
/// Returns the same as [`Regex::find`], but starts the search at the given
/// offset.
///
/// The significance of the starting point is that it takes the surrounding
/// context into consideration. For example, the `\A` anchor can only
/// match when `start == 0`.
///
/// # Panics
///
/// This panics when `start >= haystack.len() + 1`.
///
/// # Example
///
/// This example shows the significance of `start` by demonstrating how it
/// can be used to permit look-around assertions in a regex to take the
/// surrounding context into account.
///
/// ```
/// use regex_lite::Regex;
///
/// let re = Regex::new(r"\bchew\b").unwrap();
/// let hay = "eschew";
/// // We get a match here, but it's probably not intended.
/// assert_eq!(re.find(&hay[2..]).map(|m| m.range()), Some(0..4));
/// // No match because the assertions take the context into account.
/// assert_eq!(re.find_at(hay, 2), None);
/// ```
#[inline]
pub fn find_at<'h>(
&self,
haystack: &'h str,
start: usize,
) -> Option<Match<'h>> {
let mut cache = self.pool.get();
let mut slots = [None, None];
let matched = self.pikevm.search(
&mut cache,
haystack.as_bytes(),
start,
haystack.len(),
false,
&mut slots,
);
if !matched {
return None;
}
let (start, end) = (slots[0].unwrap().get(), slots[1].unwrap().get());
Some(Match::new(haystack, start, end))
}
/// Returns the same as [`Regex::captures`], but starts the search at the
/// given offset.
///
/// The significance of the starting point is that it takes the surrounding
/// context into consideration. For example, the `\A` anchor can only
/// match when `start == 0`.
///
/// # Panics
///
/// This panics when `start >= haystack.len() + 1`.
///
/// # Example
///
/// This example shows the significance of `start` by demonstrating how it
/// can be used to permit look-around assertions in a regex to take the
/// surrounding context into account.
///
/// ```
/// use regex_lite::Regex;
///
/// let re = Regex::new(r"\bchew\b").unwrap();
/// let hay = "eschew";
/// // We get a match here, but it's probably not intended.
/// assert_eq!(&re.captures(&hay[2..]).unwrap()[0], "chew");
/// // No match because the assertions take the context into account.
/// assert!(re.captures_at(hay, 2).is_none());
/// ```
#[inline]
pub fn captures_at<'h>(
&self,
haystack: &'h str,
start: usize,
) -> Option<Captures<'h>> {
let mut caps = Captures {
haystack,
slots: self.capture_locations(),
pikevm: Arc::clone(&self.pikevm),
};
let mut cache = self.pool.get();
let matched = self.pikevm.search(
&mut cache,
haystack.as_bytes(),
start,
haystack.len(),
false,
&mut caps.slots.0,
);
if !matched {
return None;
}
Some(caps)
}
/// This is like [`Regex::captures`], but writes the byte offsets of each
/// capture group match into the locations given.
///
/// A [`CaptureLocations`] stores the same byte offsets as a [`Captures`],
/// but does *not* store a reference to the haystack. This makes its API
/// a bit lower level and less convenience. But in exchange, callers
/// may allocate their own `CaptureLocations` and reuse it for multiple
/// searches. This may be helpful if allocating a `Captures` shows up in a
/// profile as too costly.
///
/// To create a `CaptureLocations` value, use the
/// [`Regex::capture_locations`] method.
///
/// This also returns the overall match if one was found. When a match is
/// found, its offsets are also always stored in `locs` at index `0`.
///
/// # Panics
///
/// This routine may panic if the given `CaptureLocations` was not created
/// by this regex.
///
/// # Example
///
/// ```
/// use regex_lite::Regex;
///
/// let re = Regex::new(r"^([a-z]+)=(\S*)$").unwrap();
/// let mut locs = re.capture_locations();
/// assert!(re.captures_read(&mut locs, "id=foo123").is_some());
/// assert_eq!(Some((0, 9)), locs.get(0));
/// assert_eq!(Some((0, 2)), locs.get(1));
/// assert_eq!(Some((3, 9)), locs.get(2));
/// ```
#[inline]
pub fn captures_read<'h>(
&self,
locs: &mut CaptureLocations,
haystack: &'h str,
) -> Option<Match<'h>> {
self.captures_read_at(locs, haystack, 0)
}
/// Returns the same as [`Regex::captures_read`], but starts the search at
/// the given offset.
///
/// The significance of the starting point is that it takes the surrounding
/// context into consideration. For example, the `\A` anchor can only
/// match when `start == 0`.
///
/// # Panics
///
/// This panics when `start >= haystack.len() + 1`.
///
/// This routine may also panic if the given `CaptureLocations` was not
/// created by this regex.
///
/// # Example
///
/// This example shows the significance of `start` by demonstrating how it
/// can be used to permit look-around assertions in a regex to take the
/// surrounding context into account.
///
/// ```
/// use regex_lite::Regex;
///
/// let re = Regex::new(r"\bchew\b").unwrap();
/// let hay = "eschew";
/// let mut locs = re.capture_locations();
/// // We get a match here, but it's probably not intended.
/// assert!(re.captures_read(&mut locs, &hay[2..]).is_some());
/// // No match because the assertions take the context into account.
/// assert!(re.captures_read_at(&mut locs, hay, 2).is_none());
/// ```
#[inline]
pub fn captures_read_at<'h>(
&self,
locs: &mut CaptureLocations,
haystack: &'h str,
start: usize,
) -> Option<Match<'h>> {
let mut cache = self.pool.get();
let matched = self.pikevm.search(
&mut cache,
haystack.as_bytes(),
start,
haystack.len(),
false,
&mut locs.0,
);
if !matched {
return None;
}
let (start, end) = locs.get(0).unwrap();
Some(Match::new(haystack, start, end))
}
}
/// Auxiliary methods.
impl Regex {
/// Returns the original string of this regex.
///
/// # Example
///
/// ```
/// use regex_lite::Regex;
///
/// let re = Regex::new(r"foo\w+bar").unwrap();
/// assert_eq!(re.as_str(), r"foo\w+bar");
/// ```
#[inline]
pub fn as_str(&self) -> &str {
&self.pikevm.nfa().pattern()
}
/// Returns an iterator over the capture names in this regex.
///
/// The iterator returned yields elements of type `Option<&str>`. That is,
/// the iterator yields values for all capture groups, even ones that are
/// unnamed. The order of the groups corresponds to the order of the group's
/// corresponding opening parenthesis.
///
/// The first element of the iterator always yields the group corresponding
/// to the overall match, and this group is always unnamed. Therefore, the
/// iterator always yields at least one group.
///
/// # Example
///
/// This shows basic usage with a mix of named and unnamed capture groups:
///
/// ```
/// use regex_lite::Regex;
///
/// let re = Regex::new(r"(?<a>.(?<b>.))(.)(?:.)(?<c>.)").unwrap();
/// let mut names = re.capture_names();
/// assert_eq!(names.next(), Some(None));
/// assert_eq!(names.next(), Some(Some("a")));
/// assert_eq!(names.next(), Some(Some("b")));
/// assert_eq!(names.next(), Some(None));
/// // the '(?:.)' group is non-capturing and so doesn't appear here!
/// assert_eq!(names.next(), Some(Some("c")));
/// assert_eq!(names.next(), None);
/// ```
///
/// The iterator always yields at least one element, even for regexes with
/// no capture groups and even for regexes that can never match:
///
/// ```
/// use regex_lite::Regex;
///
/// let re = Regex::new(r"").unwrap();
/// let mut names = re.capture_names();
/// assert_eq!(names.next(), Some(None));
/// assert_eq!(names.next(), None);
///
/// let re = Regex::new(r"[^\s\S]").unwrap();
/// let mut names = re.capture_names();
/// assert_eq!(names.next(), Some(None));
/// assert_eq!(names.next(), None);
/// ```
#[inline]
pub fn capture_names(&self) -> CaptureNames<'_> {
CaptureNames(self.pikevm.nfa().capture_names())
}
/// Returns the number of captures groups in this regex.
///
/// This includes all named and unnamed groups, including the implicit
/// unnamed group that is always present and corresponds to the entire
/// match.
///
/// Since the implicit unnamed group is always included in this length, the
/// length returned is guaranteed to be greater than zero.
///
/// # Example
///
/// ```
/// use regex_lite::Regex;
///
/// let re = Regex::new(r"foo").unwrap();
/// assert_eq!(1, re.captures_len());
///
/// let re = Regex::new(r"(foo)").unwrap();
/// assert_eq!(2, re.captures_len());
///
/// let re = Regex::new(r"(?<a>.(?<b>.))(.)(?:.)(?<c>.)").unwrap();
/// assert_eq!(5, re.captures_len());
///
/// let re = Regex::new(r"[^\s\S]").unwrap();
/// assert_eq!(1, re.captures_len());
/// ```
#[inline]
pub fn captures_len(&self) -> usize {
self.pikevm.nfa().group_len()
}
/// Returns the total number of capturing groups that appear in every
/// possible match.
///
/// If the number of capture groups can vary depending on the match, then
/// this returns `None`. That is, a value is only returned when the number
/// of matching groups is invariant or "static."
///
/// Note that like [`Regex::captures_len`], this **does** include the
/// implicit capturing group corresponding to the entire match. Therefore,
/// when a non-None value is returned, it is guaranteed to be at least `1`.
/// Stated differently, a return value of `Some(0)` is impossible.
///
/// # Example
///
/// This shows a few cases where a static number of capture groups is
/// available and a few cases where it is not.
///
/// ```
/// use regex_lite::Regex;
///
/// let len = |pattern| {
/// Regex::new(pattern).map(|re| re.static_captures_len())
/// };
///
/// assert_eq!(Some(1), len("a")?);
/// assert_eq!(Some(2), len("(a)")?);
/// assert_eq!(Some(2), len("(a)|(b)")?);
/// assert_eq!(Some(3), len("(a)(b)|(c)(d)")?);
/// assert_eq!(None, len("(a)|b")?);
/// assert_eq!(None, len("a|(b)")?);
/// assert_eq!(None, len("(b)*")?);
/// assert_eq!(Some(2), len("(b)+")?);
///
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
#[inline]
pub fn static_captures_len(&self) -> Option<usize> {
self.pikevm
.nfa()
.static_explicit_captures_len()
.map(|len| len.saturating_add(1))
}
/// Returns a fresh allocated set of capture locations that can
/// be reused in multiple calls to [`Regex::captures_read`] or
/// [`Regex::captures_read_at`].
///
/// The returned locations can be used for any subsequent search for this
/// particular regex. There is no guarantee that it is correct to use for
/// other regexes, even if they have the same number of capture groups.
///
/// # Example
///
/// ```
/// use regex_lite::Regex;
///
/// let re = Regex::new(r"(.)(.)(\w+)").unwrap();
/// let mut locs = re.capture_locations();
/// assert!(re.captures_read(&mut locs, "Padron").is_some());
/// assert_eq!(locs.get(0), Some((0, 6)));
/// assert_eq!(locs.get(1), Some((0, 1)));
/// assert_eq!(locs.get(2), Some((1, 2)));
/// assert_eq!(locs.get(3), Some((2, 6)));
/// ```
#[inline]
pub fn capture_locations(&self) -> CaptureLocations {
// OK because NFA construction would have failed if this overflowed.
let len = self.pikevm.nfa().group_len().checked_mul(2).unwrap();
CaptureLocations(vec![None; len])
}
}
/// Represents a single match of a regex in a haystack.
///
/// A `Match` contains both the start and end byte offsets of the match and the
/// actual substring corresponding to the range of those byte offsets. It is
/// guaranteed that `start <= end`. When `start == end`, the match is empty.
///
/// Since this `Match` can only be produced by the top-level `Regex` APIs
/// that only support searching UTF-8 encoded strings, the byte offsets for a
/// `Match` are guaranteed to fall on valid UTF-8 codepoint boundaries. That
/// is, slicing a `&str` with [`Match::range`] is guaranteed to never panic.
///
/// Values with this type are created by [`Regex::find`] or
/// [`Regex::find_iter`]. Other APIs can create `Match` values too. For
/// example, [`Captures::get`].
///
/// The lifetime parameter `'h` refers to the lifetime of the matched of the
/// haystack that this match was produced from.
///
/// # Numbering
///
/// The byte offsets in a `Match` form a half-open interval. That is, the
/// start of the range is inclusive and the end of the range is exclusive.
/// For example, given a haystack `abcFOOxyz` and a match of `FOO`, its byte
/// offset range starts at `3` and ends at `6`. `3` corresponds to `F` and
/// `6` corresponds to `x`, which is one past the end of the match. This
/// corresponds to the same kind of slicing that Rust uses.
///
/// For more on why this was chosen over other schemes (aside from being
/// consistent with how Rust the language works), see [this discussion] and
/// [Dijkstra's note on a related topic][note].
///
/// [this discussion]: https://github.com/rust-lang/regex/discussions/866
/// [note]: https://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/EWD831.html
///
/// # Example
///
/// This example shows the value of each of the methods on `Match` for a
/// particular search.
///
/// ```
/// use regex_lite::Regex;
///
/// let re = Regex::new(r"\d+").unwrap();
/// let hay = "numbers: 1234";
/// let m = re.find(hay).unwrap();
/// assert_eq!(9, m.start());
/// assert_eq!(13, m.end());
/// assert!(!m.is_empty());
/// assert_eq!(4, m.len());
/// assert_eq!(9..13, m.range());
/// assert_eq!("1234", m.as_str());
/// ```
#[derive(Copy, Clone, Eq, PartialEq)]
pub struct Match<'h> {
haystack: &'h str,
start: usize,
end: usize,
}
impl<'h> Match<'h> {
/// Creates a new match from the given haystack and byte offsets.
#[inline]
fn new(haystack: &'h str, start: usize, end: usize) -> Match<'h> {
Match { haystack, start, end }
}
/// Returns the byte offset of the start of the match in the haystack. The
/// start of the match corresponds to the position where the match begins
/// and includes the first byte in the match.
///
/// It is guaranteed that `Match::start() <= Match::end()`.
///
/// This is guaranteed to fall on a valid UTF-8 codepoint boundary. That
/// is, it will never be an offset that appears between the UTF-8 code
/// units of a UTF-8 encoded Unicode scalar value. Consequently, it is
/// always safe to slice the corresponding haystack using this offset.
#[inline]
pub fn start(&self) -> usize {
self.start
}
/// Returns the byte offset of the end of the match in the haystack. The
/// end of the match corresponds to the byte immediately following the last
/// byte in the match. This means that `&slice[start..end]` works as one
/// would expect.
///
/// It is guaranteed that `Match::start() <= Match::end()`.
///
/// This is guaranteed to fall on a valid UTF-8 codepoint boundary. That
/// is, it will never be an offset that appears between the UTF-8 code
/// units of a UTF-8 encoded Unicode scalar value. Consequently, it is
/// always safe to slice the corresponding haystack using this offset.
#[inline]
pub fn end(&self) -> usize {
self.end
}
/// Returns true if and only if this match has a length of zero.
///
/// Note that an empty match can only occur when the regex itself can
/// match the empty string. Here are some examples of regexes that can
/// all match the empty string: `^`, `^$`, `\b`, `a?`, `a*`, `a{0}`,
/// `(foo|\d+|quux)?`.
#[inline]
pub fn is_empty(&self) -> bool {
self.start == self.end
}
/// Returns the length, in bytes, of this match.
#[inline]
pub fn len(&self) -> usize {
self.end - self.start
}
/// Returns the range over the starting and ending byte offsets of the
/// match in the haystack.
///
/// It is always correct to slice the original haystack searched with this
/// range. That is, because the offsets are guaranteed to fall on valid
/// UTF-8 boundaries, the range returned is always valid.
#[inline]
pub fn range(&self) -> core::ops::Range<usize> {
self.start..self.end
}
/// Returns the substring of the haystack that matched.
#[inline]
pub fn as_str(&self) -> &'h str {
&self.haystack[self.range()]
}
}
impl<'h> core::fmt::Debug for Match<'h> {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
f.debug_struct("Match")
.field("start", &self.start)
.field("end", &self.end)
.field("string", &self.as_str())
.finish()
}
}
impl<'h> From<Match<'h>> for &'h str {
fn from(m: Match<'h>) -> &'h str {
m.as_str()
}
}
impl<'h> From<Match<'h>> for core::ops::Range<usize> {
fn from(m: Match<'h>) -> core::ops::Range<usize> {
m.range()
}
}
/// Represents the capture groups for a single match.
///
/// Capture groups refer to parts of a regex enclosed in parentheses. They can
/// be optionally named. The purpose of capture groups is to be able to
/// reference different parts of a match based on the original pattern. For
/// example, say you want to match the individual letters in a 5-letter word:
///
/// ```text
/// (?<first>\w)(\w)(?:\w)\w(?<last>\w)
/// ```
///
/// This regex has 4 capture groups:
///
/// * The group at index `0` corresponds to the overall match. It is always
/// present in every match and never has a name.
/// * The group at index `1` with name `first` corresponding to the first
/// letter.
/// * The group at index `2` with no name corresponding to the second letter.
/// * The group at index `3` with name `last` corresponding to the fifth and
/// last letter.
///
/// Notice that `(?:\w)` was not listed above as a capture group despite it
/// being enclosed in parentheses. That's because `(?:pattern)` is a special
/// syntax that permits grouping but *without* capturing. The reason for not
/// treating it as a capture is that tracking and reporting capture groups
/// requires additional state that may lead to slower searches. So using as few
/// capture groups as possible can help performance. (Although the difference
/// in performance of a couple of capture groups is likely immaterial.)
///
/// Values with this type are created by [`Regex::captures`] or
/// [`Regex::captures_iter`].
///
/// `'h` is the lifetime of the haystack that these captures were matched from.
///
/// # Example
///
/// ```
/// use regex_lite::Regex;
///
/// let re = Regex::new(r"(?<first>\w)(\w)(?:\w)\w(?<last>\w)").unwrap();
/// let caps = re.captures("toady").unwrap();
/// assert_eq!("toady", &caps[0]);
/// assert_eq!("t", &caps["first"]);
/// assert_eq!("o", &caps[2]);
/// assert_eq!("y", &caps["last"]);
/// ```
pub struct Captures<'h> {
haystack: &'h str,
slots: CaptureLocations,
// It's a little weird to put the PikeVM in our Captures, but it's the
// simplest thing to do and is cheap. The PikeVM gives us access to the
// NFA and the NFA gives us access to the capture name<->index mapping.
pikevm: Arc<PikeVM>,
}
impl<'h> Captures<'h> {
/// Returns the `Match` associated with the capture group at index `i`. If
/// `i` does not correspond to a capture group, or if the capture group did
/// not participate in the match, then `None` is returned.
///
/// When `i == 0`, this is guaranteed to return a non-`None` value.
///
/// # Examples
///
/// Get the substring that matched with a default of an empty string if the
/// group didn't participate in the match:
///
/// ```
/// use regex_lite::Regex;
///
/// let re = Regex::new(r"[a-z]+(?:([0-9]+)|([A-Z]+))").unwrap();
/// let caps = re.captures("abc123").unwrap();
///
/// let substr1 = caps.get(1).map_or("", |m| m.as_str());
/// let substr2 = caps.get(2).map_or("", |m| m.as_str());
/// assert_eq!(substr1, "123");
/// assert_eq!(substr2, "");
/// ```
#[inline]
pub fn get(&self, i: usize) -> Option<Match<'h>> {
self.slots.get(i).map(|(s, e)| Match::new(self.haystack, s, e))
}
/// Returns the `Match` associated with the capture group named `name`. If
/// `name` isn't a valid capture group or it refers to a group that didn't
/// match, then `None` is returned.
///
/// Note that unlike `caps["name"]`, this returns a `Match` whose lifetime
/// matches the lifetime of the haystack in this `Captures` value.
/// Conversely, the substring returned by `caps["name"]` has a lifetime
/// of the `Captures` value, which is likely shorter than the lifetime of
/// the haystack. In some cases, it may be necessary to use this method to
/// access the matching substring instead of the `caps["name"]` notation.
///
/// # Examples
///
/// Get the substring that matched with a default of an empty string if the
/// group didn't participate in the match:
///
/// ```
/// use regex_lite::Regex;
///
/// let re = Regex::new(
/// r"[a-z]+(?:(?<numbers>[0-9]+)|(?<letters>[A-Z]+))",
/// ).unwrap();
/// let caps = re.captures("abc123").unwrap();
///
/// let numbers = caps.name("numbers").map_or("", |m| m.as_str());
/// let letters = caps.name("letters").map_or("", |m| m.as_str());
/// assert_eq!(numbers, "123");
/// assert_eq!(letters, "");
/// ```
#[inline]
pub fn name(&self, name: &str) -> Option<Match<'h>> {
let i = self.pikevm.nfa().to_index(name)?;
self.get(i)
}
/// This is a convenience routine for extracting the substrings
/// corresponding to matching capture groups.
///
/// This returns a tuple where the first element corresponds to the full
/// substring of the haystack that matched the regex. The second element is
/// an array of substrings, with each corresponding to the substring that
/// matched for a particular capture group.
///
/// # Panics
///
/// This panics if the number of possible matching groups in this
/// `Captures` value is not fixed to `N` in all circumstances.
/// More precisely, this routine only works when `N` is equivalent to
/// [`Regex::static_captures_len`].
///
/// Stated more plainly, if the number of matching capture groups in a
/// regex can vary from match to match, then this function always panics.
///
/// For example, `(a)(b)|(c)` could produce two matching capture groups
/// or one matching capture group for any given match. Therefore, one
/// cannot use `extract` with such a pattern.
///
/// But a pattern like `(a)(b)|(c)(d)` can be used with `extract` because
/// the number of capture groups in every match is always equivalent,
/// even if the capture _indices_ in each match are not.
///
/// # Example
///
/// ```
/// use regex_lite::Regex;
///
/// let re = Regex::new(r"([0-9]{4})-([0-9]{2})-([0-9]{2})").unwrap();
/// let hay = "On 2010-03-14, I became a Tenneessee lamb.";
/// let Some((full, [year, month, day])) =
/// re.captures(hay).map(|caps| caps.extract()) else { return };
/// assert_eq!("2010-03-14", full);
/// assert_eq!("2010", year);
/// assert_eq!("03", month);
/// assert_eq!("14", day);
/// ```
///
/// # Example: iteration
///
/// This example shows how to use this method when iterating over all
/// `Captures` matches in a haystack.
///
/// ```
/// use regex_lite::Regex;
///
/// let re = Regex::new(r"([0-9]{4})-([0-9]{2})-([0-9]{2})").unwrap();
/// let hay = "1973-01-05, 1975-08-25 and 1980-10-18";
///
/// let mut dates: Vec<(&str, &str, &str)> = vec![];
/// for (_, [y, m, d]) in re.captures_iter(hay).map(|c| c.extract()) {
/// dates.push((y, m, d));
/// }
/// assert_eq!(dates, vec![
/// ("1973", "01", "05"),
/// ("1975", "08", "25"),
/// ("1980", "10", "18"),
/// ]);
/// ```
///
/// # Example: parsing different formats
///
/// This API is particularly useful when you need to extract a particular
/// value that might occur in a different format. Consider, for example,
/// an identifier that might be in double quotes or single quotes:
///
/// ```
/// use regex_lite::Regex;
///
/// let re = Regex::new(r#"id:(?:"([^"]+)"|'([^']+)')"#).unwrap();
/// let hay = r#"The first is id:"foo" and the second is id:'bar'."#;
/// let mut ids = vec![];
/// for (_, [id]) in re.captures_iter(hay).map(|c| c.extract()) {
/// ids.push(id);
/// }
/// assert_eq!(ids, vec!["foo", "bar"]);
/// ```
pub fn extract<const N: usize>(&self) -> (&'h str, [&'h str; N]) {
let len = self
.pikevm
.nfa()
.static_explicit_captures_len()
.expect("number of capture groups can vary in a match");
assert_eq!(N, len, "asked for {} groups, but must ask for {}", N, len);
let mut matched = self.iter().flatten();
let whole_match = matched.next().expect("a match").as_str();
let group_matches = [0; N].map(|_| {
matched.next().expect("too few matching groups").as_str()
});
(whole_match, group_matches)
}
/// Expands all instances of `$ref` in `replacement` to the corresponding
/// capture group, and writes them to the `dst` buffer given. A `ref` can
/// be a capture group index or a name. If `ref` doesn't refer to a capture
/// group that participated in the match, then it is replaced with the
/// empty string.
///
/// # Format
///
/// The format of the replacement string supports two different kinds of
/// capture references: unbraced and braced.
///
/// For the unbraced format, the format supported is `$ref` where `name`
/// can be any character in the class `[0-9A-Za-z_]`. `ref` is always
/// the longest possible parse. So for example, `$1a` corresponds to the
/// capture group named `1a` and not the capture group at index `1`. If
/// `ref` matches `^[0-9]+$`, then it is treated as a capture group index
/// itself and not a name.
///
/// For the braced format, the format supported is `${ref}` where `ref` can
/// be any sequence of bytes except for `}`. If no closing brace occurs,
/// then it is not considered a capture reference. As with the unbraced
/// format, if `ref` matches `^[0-9]+$`, then it is treated as a capture
/// group index and not a name.
///
/// The braced format is useful for exerting precise control over the name
/// of the capture reference. For example, `${1}a` corresponds to the
/// capture group reference `1` followed by the letter `a`, where as `$1a`
/// (as mentioned above) corresponds to the capture group reference `1a`.
/// The braced format is also useful for expressing capture group names
/// that use characters not supported by the unbraced format. For example,
/// `${foo[bar].baz}` refers to the capture group named `foo[bar].baz`.
///
/// If a capture group reference is found and it does not refer to a valid
/// capture group, then it will be replaced with the empty string.
///
/// To write a literal `$`, use `$$`.
///
/// # Example
///
/// ```
/// use regex_lite::Regex;
///
/// let re = Regex::new(
/// r"(?<day>[0-9]{2})-(?<month>[0-9]{2})-(?<year>[0-9]{4})",
/// ).unwrap();
/// let hay = "On 14-03-2010, I became a Tenneessee lamb.";
/// let caps = re.captures(hay).unwrap();
///
/// let mut dst = String::new();
/// caps.expand("year=$year, month=$month, day=$day", &mut dst);
/// assert_eq!(dst, "year=2010, month=03, day=14");
/// ```
#[inline]
pub fn expand(&self, replacement: &str, dst: &mut String) {
interpolate::string(
replacement,
|index, dst| {
let m = match self.get(index) {
None => return,
Some(m) => m,
};
dst.push_str(&self.haystack[m.range()]);
},
|name| self.pikevm.nfa().to_index(name),
dst,
);
}
/// Returns an iterator over all capture groups. This includes both
/// matching and non-matching groups.
///
/// The iterator always yields at least one matching group: the first group
/// (at index `0`) with no name. Subsequent groups are returned in the order
/// of their opening parenthesis in the regex.
///
/// The elements yielded have type `Option<Match<'h>>`, where a non-`None`
/// value is present if the capture group matches.
///
/// # Example
///
/// ```
/// use regex_lite::Regex;
///
/// let re = Regex::new(r"(\w)(\d)?(\w)").unwrap();
/// let caps = re.captures("AZ").unwrap();
///
/// let mut it = caps.iter();
/// assert_eq!(it.next().unwrap().map(|m| m.as_str()), Some("AZ"));
/// assert_eq!(it.next().unwrap().map(|m| m.as_str()), Some("A"));
/// assert_eq!(it.next().unwrap().map(|m| m.as_str()), None);
/// assert_eq!(it.next().unwrap().map(|m| m.as_str()), Some("Z"));
/// assert_eq!(it.next(), None);
/// ```
#[inline]
pub fn iter<'c>(&'c self) -> SubCaptureMatches<'c, 'h> {
SubCaptureMatches {
caps: self,
it: self.pikevm.nfa().capture_names().enumerate(),
}
}
/// Returns the total number of capture groups. This includes both
/// matching and non-matching groups.
///
/// The length returned is always equivalent to the number of elements
/// yielded by [`Captures::iter`]. Consequently, the length is always
/// greater than zero since every `Captures` value always includes the
/// match for the entire regex.
///
/// # Example
///
/// ```
/// use regex_lite::Regex;
///
/// let re = Regex::new(r"(\w)(\d)?(\w)").unwrap();
/// let caps = re.captures("AZ").unwrap();
/// assert_eq!(caps.len(), 4);
/// ```
#[inline]
pub fn len(&self) -> usize {
self.pikevm.nfa().group_len()
}
}
impl<'h> core::fmt::Debug for Captures<'h> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
/// A little helper type to provide a nice map-like debug
/// representation for our capturing group spans.
///
/// regex-automata has something similar, but it includes the pattern
/// ID in its debug output, which is confusing. It also doesn't include
/// that strings that match because a regex-automata `Captures` doesn't
/// borrow the haystack.
struct CapturesDebugMap<'a> {
caps: &'a Captures<'a>,
}
impl<'a> core::fmt::Debug for CapturesDebugMap<'a> {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
let mut map = f.debug_map();
let names = self.caps.pikevm.nfa().capture_names();
for (group_index, maybe_name) in names.enumerate() {
let key = Key(group_index, maybe_name);
match self.caps.get(group_index) {
None => map.entry(&key, &None::<()>),
Some(mat) => map.entry(&key, &Value(mat)),
};
}
map.finish()
}
}
struct Key<'a>(usize, Option<&'a str>);
impl<'a> core::fmt::Debug for Key<'a> {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
write!(f, "{}", self.0)?;
if let Some(name) = self.1 {
write!(f, "/{:?}", name)?;
}
Ok(())
}
}
struct Value<'a>(Match<'a>);
impl<'a> core::fmt::Debug for Value<'a> {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
write!(
f,
"{}..{}/{:?}",
self.0.start(),
self.0.end(),
self.0.as_str()
)
}
}
f.debug_tuple("Captures")
.field(&CapturesDebugMap { caps: self })
.finish()
}
}
/// Get a matching capture group's haystack substring by index.
///
/// The haystack substring returned can't outlive the `Captures` object if this
/// method is used, because of how `Index` is defined (normally `a[i]` is part
/// of `a` and can't outlive it). To work around this limitation, do that, use
/// [`Captures::get`] instead.
///
/// `'h` is the lifetime of the matched haystack, but the lifetime of the
/// `&str` returned by this implementation is the lifetime of the `Captures`
/// value itself.
///
/// # Panics
///
/// If there is no matching group at the given index.
impl<'h> core::ops::Index<usize> for Captures<'h> {
type Output = str;
// The lifetime is written out to make it clear that the &str returned
// does NOT have a lifetime equivalent to 'h.
fn index(&self, i: usize) -> &str {
self.get(i)
.map(|m| m.as_str())
.unwrap_or_else(|| panic!("no group at index '{}'", i))
}
}
/// Get a matching capture group's haystack substring by name.
///
/// The haystack substring returned can't outlive the `Captures` object if this
/// method is used, because of how `Index` is defined (normally `a[i]` is part
/// of `a` and can't outlive it). To work around this limitation, do that, use
/// [`Captures::get`] instead.
///
/// `'h` is the lifetime of the matched haystack, but the lifetime of the
/// `&str` returned by this implementation is the lifetime of the `Captures`
/// value itself.
///
/// `'n` is the lifetime of the group name used to index the `Captures` value.
///
/// # Panics
///
/// If there is no matching group at the given name.
impl<'h, 'n> core::ops::Index<&'n str> for Captures<'h> {
type Output = str;
fn index<'a>(&'a self, name: &'n str) -> &'a str {
self.name(name)
.map(|m| m.as_str())
.unwrap_or_else(|| panic!("no group named '{}'", name))
}
}
/// A low level representation of the byte offsets of each capture group.
///
/// You can think of this as a lower level [`Captures`], where this type does
/// not support named capturing groups directly and it does not borrow the
/// haystack that these offsets were matched on.
///
/// Primarily, this type is useful when using the lower level `Regex` APIs such
/// as [`Regex::captures_read`], which permits amortizing the allocation in
/// which capture match offsets are stored.
///
/// In order to build a value of this type, you'll need to call the
/// [`Regex::capture_locations`] method. The value returned can then be reused
/// in subsequent searches for that regex. Using it for other regexes may
/// result in a panic or otherwise incorrect results.
///
/// # Example
///
/// This example shows how to create and use `CaptureLocations` in a search.
///
/// ```
/// use regex_lite::Regex;
///
/// let re = Regex::new(r"(?<first>\w+)\s+(?<last>\w+)").unwrap();
/// let mut locs = re.capture_locations();
/// let m = re.captures_read(&mut locs, "Bruce Springsteen").unwrap();
/// assert_eq!(0..17, m.range());
/// assert_eq!(Some((0, 17)), locs.get(0));
/// assert_eq!(Some((0, 5)), locs.get(1));
/// assert_eq!(Some((6, 17)), locs.get(2));
///
/// // Asking for an invalid capture group always returns None.
/// assert_eq!(None, locs.get(3));
/// # // literals are too big for 32-bit usize: #1041
/// # #[cfg(target_pointer_width = "64")]
/// assert_eq!(None, locs.get(34973498648));
/// # #[cfg(target_pointer_width = "64")]
/// assert_eq!(None, locs.get(9944060567225171988));
/// ```
#[derive(Clone, Debug)]
pub struct CaptureLocations(Vec<Option<NonMaxUsize>>);
impl CaptureLocations {
/// Returns the start and end byte offsets of the capture group at index
/// `i`. This returns `None` if `i` is not a valid capture group or if the
/// capture group did not match.
///
/// # Example
///
/// ```
/// use regex_lite::Regex;
///
/// let re = Regex::new(r"(?<first>\w+)\s+(?<last>\w+)").unwrap();
/// let mut locs = re.capture_locations();
/// re.captures_read(&mut locs, "Bruce Springsteen").unwrap();
/// assert_eq!(Some((0, 17)), locs.get(0));
/// assert_eq!(Some((0, 5)), locs.get(1));
/// assert_eq!(Some((6, 17)), locs.get(2));
/// ```
#[inline]
pub fn get(&self, i: usize) -> Option<(usize, usize)> {
let slot = i.checked_mul(2)?;
let start = self.0.get(slot).copied()??.get();
let slot = slot.checked_add(1)?;
let end = self.0.get(slot).copied()??.get();
Some((start, end))
}
/// Returns the total number of capture groups (even if they didn't match).
/// That is, the length returned is unaffected by the result of a search.
///
/// This is always at least `1` since every regex has at least `1`
/// capturing group that corresponds to the entire match.
///
/// # Example
///
/// ```
/// use regex_lite::Regex;
///
/// let re = Regex::new(r"(?<first>\w+)\s+(?<last>\w+)").unwrap();
/// let mut locs = re.capture_locations();
/// assert_eq!(3, locs.len());
/// re.captures_read(&mut locs, "Bruce Springsteen").unwrap();
/// assert_eq!(3, locs.len());
/// ```
///
/// Notice that the length is always at least `1`, regardless of the regex:
///
/// ```
/// use regex_lite::Regex;
///
/// let re = Regex::new(r"").unwrap();
/// let locs = re.capture_locations();
/// assert_eq!(1, locs.len());
///
/// // [a&&b] is a regex that never matches anything.
/// let re = Regex::new(r"[^\s\S]").unwrap();
/// let locs = re.capture_locations();
/// assert_eq!(1, locs.len());
/// ```
#[inline]
pub fn len(&self) -> usize {
// We always have twice as many slots as groups.
self.0.len().checked_shr(1).unwrap()
}
}
/// An iterator over all non-overlapping matches in a haystack.
///
/// This iterator yields [`Match`] values. The iterator stops when no more
/// matches can be found.
///
/// `'r` is the lifetime of the compiled regular expression and `'h` is the
/// lifetime of the haystack.
///
/// This iterator is created by [`Regex::find_iter`].
///
/// # Time complexity
///
/// Note that since an iterator runs potentially many searches on the haystack
/// and since each search has worst case `O(m * n)` time complexity, the
/// overall worst case time complexity for iteration is `O(m * n^2)`.
#[derive(Debug)]
pub struct Matches<'r, 'h> {
haystack: &'h str,
it: pikevm::FindMatches<'r, 'h>,
}
impl<'r, 'h> Iterator for Matches<'r, 'h> {
type Item = Match<'h>;
#[inline]
fn next(&mut self) -> Option<Match<'h>> {
self.it.next().map(|(s, e)| Match::new(self.haystack, s, e))
}
#[inline]
fn count(self) -> usize {
self.it.count()
}
}
impl<'r, 'h> core::iter::FusedIterator for Matches<'r, 'h> {}
/// An iterator over all non-overlapping capture matches in a haystack.
///
/// This iterator yields [`Captures`] values. The iterator stops when no more
/// matches can be found.
///
/// `'r` is the lifetime of the compiled regular expression and `'h` is the
/// lifetime of the matched string.
///
/// This iterator is created by [`Regex::captures_iter`].
///
/// # Time complexity
///
/// Note that since an iterator runs potentially many searches on the haystack
/// and since each search has worst case `O(m * n)` time complexity, the
/// overall worst case time complexity for iteration is `O(m * n^2)`.
#[derive(Debug)]
pub struct CaptureMatches<'r, 'h> {
haystack: &'h str,
re: &'r Regex,
it: pikevm::CapturesMatches<'r, 'h>,
}
impl<'r, 'h> Iterator for CaptureMatches<'r, 'h> {
type Item = Captures<'h>;
#[inline]
fn next(&mut self) -> Option<Captures<'h>> {
self.it.next().map(|slots| Captures {
haystack: self.haystack,
slots: CaptureLocations(slots),
pikevm: Arc::clone(&self.re.pikevm),
})
}
#[inline]
fn count(self) -> usize {
self.it.count()
}
}
impl<'r, 'h> core::iter::FusedIterator for CaptureMatches<'r, 'h> {}
/// An iterator over all substrings delimited by a regex match.
///
/// `'r` is the lifetime of the compiled regular expression and `'h` is the
/// lifetime of the byte string being split.
///
/// This iterator is created by [`Regex::split`].
///
/// # Time complexity
///
/// Note that since an iterator runs potentially many searches on the haystack
/// and since each search has worst case `O(m * n)` time complexity, the
/// overall worst case time complexity for iteration is `O(m * n^2)`.
#[derive(Debug)]
pub struct Split<'r, 'h> {
haystack: &'h str,
finder: Matches<'r, 'h>,
last: usize,
}
impl<'r, 'h> Iterator for Split<'r, 'h> {
type Item = &'h str;
#[inline]
fn next(&mut self) -> Option<&'h str> {
match self.finder.next() {
None => {
let len = self.haystack.len();
if self.last > len {
None
} else {
let range = self.last..len;
self.last = len + 1; // Next call will return None
Some(&self.haystack[range])
}
}
Some(m) => {
let range = self.last..m.start();
self.last = m.end();
Some(&self.haystack[range])
}
}
}
}
impl<'r, 't> core::iter::FusedIterator for Split<'r, 't> {}
/// An iterator over at most `N` substrings delimited by a regex match.
///
/// The last substring yielded by this iterator will be whatever remains after
/// `N-1` splits.
///
/// `'r` is the lifetime of the compiled regular expression and `'h` is the
/// lifetime of the byte string being split.
///
/// This iterator is created by [`Regex::splitn`].
///
/// # Time complexity
///
/// Note that since an iterator runs potentially many searches on the haystack
/// and since each search has worst case `O(m * n)` time complexity, the
/// overall worst case time complexity for iteration is `O(m * n^2)`.
///
/// Although note that the worst case time here has an upper bound given
/// by the `limit` parameter to [`Regex::splitn`].
#[derive(Debug)]
pub struct SplitN<'r, 'h> {
splits: Split<'r, 'h>,
limit: usize,
}
impl<'r, 'h> Iterator for SplitN<'r, 'h> {
type Item = &'h str;
#[inline]
fn next(&mut self) -> Option<&'h str> {
if self.limit == 0 {
return None;
}
self.limit -= 1;
if self.limit > 0 {
return self.splits.next();
}
let len = self.splits.haystack.len();
if self.splits.last > len {
// We've already returned all substrings.
None
} else {
// self.n == 0, so future calls will return None immediately
Some(&self.splits.haystack[self.splits.last..len])
}
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.splits.size_hint()
}
}
impl<'r, 't> core::iter::FusedIterator for SplitN<'r, 't> {}
/// An iterator over the names of all capture groups in a regex.
///
/// This iterator yields values of type `Option<&str>` in order of the opening
/// capture group parenthesis in the regex pattern. `None` is yielded for
/// groups with no name. The first element always corresponds to the implicit
/// and unnamed group for the overall match.
///
/// `'r` is the lifetime of the compiled regular expression.
///
/// This iterator is created by [`Regex::capture_names`].
#[derive(Clone, Debug)]
pub struct CaptureNames<'r>(nfa::CaptureNames<'r>);
impl<'r> Iterator for CaptureNames<'r> {
type Item = Option<&'r str>;
#[inline]
fn next(&mut self) -> Option<Option<&'r str>> {
self.0.next()
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.0.size_hint()
}
#[inline]
fn count(self) -> usize {
self.0.count()
}
}
impl<'r> ExactSizeIterator for CaptureNames<'r> {}
impl<'r> core::iter::FusedIterator for CaptureNames<'r> {}
/// An iterator over all group matches in a [`Captures`] value.
///
/// This iterator yields values of type `Option<Match<'h>>`, where `'h` is the
/// lifetime of the haystack that the matches are for. The order of elements
/// yielded corresponds to the order of the opening parenthesis for the group
/// in the regex pattern. `None` is yielded for groups that did not participate
/// in the match.
///
/// The first element always corresponds to the implicit group for the overall
/// match. Since this iterator is created by a [`Captures`] value, and a
/// `Captures` value is only created when a match occurs, it follows that the
/// first element yielded by this iterator is guaranteed to be non-`None`.
///
/// The lifetime `'c` corresponds to the lifetime of the `Captures` value that
/// created this iterator, and the lifetime `'h` corresponds to the originally
/// matched haystack.
#[derive(Clone, Debug)]
pub struct SubCaptureMatches<'c, 'h> {
caps: &'c Captures<'h>,
it: core::iter::Enumerate<nfa::CaptureNames<'c>>,
}
impl<'c, 'h> Iterator for SubCaptureMatches<'c, 'h> {
type Item = Option<Match<'h>>;
#[inline]
fn next(&mut self) -> Option<Option<Match<'h>>> {
let (group_index, _) = self.it.next()?;
Some(self.caps.get(group_index))
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.it.size_hint()
}
#[inline]
fn count(self) -> usize {
self.it.count()
}
}
impl<'c, 'h> ExactSizeIterator for SubCaptureMatches<'c, 'h> {}
impl<'c, 'h> core::iter::FusedIterator for SubCaptureMatches<'c, 'h> {}
/// A trait for types that can be used to replace matches in a haystack.
///
/// In general, users of this crate shouldn't need to implement this trait,
/// since implementations are already provided for `&str` along with other
/// variants of string types, as well as `FnMut(&Captures) -> String` (or any
/// `FnMut(&Captures) -> T` where `T: AsRef<str>`). Those cover most use cases,
/// but callers can implement this trait directly if necessary.
///
/// # Example
///
/// This example shows a basic implementation of the `Replacer` trait. This
/// can be done much more simply using the replacement string interpolation
/// support (e.g., `$first $last`), but this approach avoids needing to parse
/// the replacement string at all.
///
/// ```
/// use regex_lite::{Captures, Regex, Replacer};
///
/// struct NameSwapper;
///
/// impl Replacer for NameSwapper {
/// fn replace_append(&mut self, caps: &Captures<'_>, dst: &mut String) {
/// dst.push_str(&caps["first"]);
/// dst.push_str(" ");
/// dst.push_str(&caps["last"]);
/// }
/// }
///
/// let re = Regex::new(r"(?<last>[^,\s]+),\s+(?<first>\S+)").unwrap();
/// let result = re.replace("Springsteen, Bruce", NameSwapper);
/// assert_eq!(result, "Bruce Springsteen");
/// ```
pub trait Replacer {
/// Appends possibly empty data to `dst` to replace the current match.
///
/// The current match is represented by `caps`, which is guaranteed to
/// have a match at capture group `0`.
///
/// For example, a no-op replacement would be `dst.push_str(&caps[0])`.
fn replace_append(&mut self, caps: &Captures<'_>, dst: &mut String);
/// Return a fixed unchanging replacement string.
///
/// When doing replacements, if access to [`Captures`] is not needed (e.g.,
/// the replacement string does not need `$` expansion), then it can be
/// beneficial to avoid finding sub-captures.
///
/// In general, this is called once for every call to a replacement routine
/// such as [`Regex::replace_all`].
fn no_expansion<'r>(&'r mut self) -> Option<Cow<'r, str>> {
None
}
/// Returns a type that implements `Replacer`, but that borrows and wraps
/// this `Replacer`.
///
/// This is useful when you want to take a generic `Replacer` (which might
/// not be cloneable) and use it without consuming it, so it can be used
/// more than once.
///
/// # Example
///
/// ```
/// use regex_lite::{Regex, Replacer};
///
/// fn replace_all_twice<R: Replacer>(
/// re: Regex,
/// src: &str,
/// mut rep: R,
/// ) -> String {
/// let dst = re.replace_all(src, rep.by_ref());
/// let dst = re.replace_all(&dst, rep.by_ref());
/// dst.into_owned()
/// }
/// ```
fn by_ref<'r>(&'r mut self) -> ReplacerRef<'r, Self> {
ReplacerRef(self)
}
}
impl<'a> Replacer for &'a str {
fn replace_append(&mut self, caps: &Captures<'_>, dst: &mut String) {
caps.expand(*self, dst);
}
fn no_expansion(&mut self) -> Option<Cow<'_, str>> {
no_expansion(self)
}
}
impl<'a> Replacer for &'a String {
fn replace_append(&mut self, caps: &Captures<'_>, dst: &mut String) {
self.as_str().replace_append(caps, dst)
}
fn no_expansion(&mut self) -> Option<Cow<'_, str>> {
no_expansion(self)
}
}
impl Replacer for String {
fn replace_append(&mut self, caps: &Captures<'_>, dst: &mut String) {
self.as_str().replace_append(caps, dst)
}
fn no_expansion(&mut self) -> Option<Cow<'_, str>> {
no_expansion(self)
}
}
impl<'a> Replacer for Cow<'a, str> {
fn replace_append(&mut self, caps: &Captures<'_>, dst: &mut String) {
self.as_ref().replace_append(caps, dst)
}
fn no_expansion(&mut self) -> Option<Cow<'_, str>> {
no_expansion(self)
}
}
impl<'a> Replacer for &'a Cow<'a, str> {
fn replace_append(&mut self, caps: &Captures<'_>, dst: &mut String) {
self.as_ref().replace_append(caps, dst)
}
fn no_expansion(&mut self) -> Option<Cow<'_, str>> {
no_expansion(self)
}
}
impl<F, T> Replacer for F
where
F: FnMut(&Captures<'_>) -> T,
T: AsRef<str>,
{
fn replace_append(&mut self, caps: &Captures<'_>, dst: &mut String) {
dst.push_str((*self)(caps).as_ref());
}
}
/// A by-reference adaptor for a [`Replacer`].
///
/// This permits reusing the same `Replacer` value in multiple calls to a
/// replacement routine like [`Regex::replace_all`].
///
/// This type is created by [`Replacer::by_ref`].
#[derive(Debug)]
pub struct ReplacerRef<'a, R: ?Sized>(&'a mut R);
impl<'a, R: Replacer + ?Sized + 'a> Replacer for ReplacerRef<'a, R> {
fn replace_append(&mut self, caps: &Captures<'_>, dst: &mut String) {
self.0.replace_append(caps, dst)
}
fn no_expansion(&mut self) -> Option<Cow<'_, str>> {
self.0.no_expansion()
}
}
/// A helper type for forcing literal string replacement.
///
/// It can be used with routines like [`Regex::replace`] and
/// [`Regex::replace_all`] to do a literal string replacement without expanding
/// `$name` to their corresponding capture groups. This can be both convenient
/// (to avoid escaping `$`, for example) and faster (since capture groups
/// don't need to be found).
///
/// `'s` is the lifetime of the literal string to use.
///
/// # Example
///
/// ```
/// use regex_lite::{NoExpand, Regex};
///
/// let re = Regex::new(r"(?<last>[^,\s]+),\s+(\S+)").unwrap();
/// let result = re.replace("Springsteen, Bruce", NoExpand("$2 $last"));
/// assert_eq!(result, "$2 $last");
/// ```
#[derive(Clone, Debug)]
pub struct NoExpand<'t>(pub &'t str);
impl<'t> Replacer for NoExpand<'t> {
fn replace_append(&mut self, _: &Captures<'_>, dst: &mut String) {
dst.push_str(self.0);
}
fn no_expansion(&mut self) -> Option<Cow<'_, str>> {
Some(Cow::Borrowed(self.0))
}
}
/// Quickly checks the given replacement string for whether interpolation
/// should be done on it. It returns `None` if a `$` was found anywhere in the
/// given string, which suggests interpolation needs to be done. But if there's
/// no `$` anywhere, then interpolation definitely does not need to be done. In
/// that case, the given string is returned as a borrowed `Cow`.
///
/// This is meant to be used to implement the `Replacer::no_expandsion` method
/// in its various trait impls.
fn no_expansion<T: AsRef<str>>(t: &T) -> Option<Cow<'_, str>> {
let s = t.as_ref();
match s.find('$') {
Some(_) => None,
None => Some(Cow::Borrowed(s)),
}
}
/// A configurable builder for a [`Regex`].
///
/// This builder can be used to programmatically set flags such as `i` (case
/// insensitive) and `x` (for verbose mode). This builder can also be used to
/// configure things like a size limit on the compiled regular expression.
#[derive(Debug)]
pub struct RegexBuilder {
pattern: String,
hir_config: hir::Config,
nfa_config: nfa::Config,
}
impl RegexBuilder {
/// Create a new builder with a default configuration for the given
/// pattern.
///
/// If the pattern is invalid or exceeds the configured size limits, then
/// an error will be returned when [`RegexBuilder::build`] is called.
pub fn new(pattern: &str) -> RegexBuilder {
RegexBuilder {
pattern: pattern.to_string(),
hir_config: hir::Config::default(),
nfa_config: nfa::Config::default(),
}
}
/// Compiles the pattern given to `RegexBuilder::new` with the
/// configuration set on this builder.
///
/// If the pattern isn't a valid regex or if a configured size limit was
/// exceeded, then an error is returned.
pub fn build(&self) -> Result<Regex, Error> {
let hir = Hir::parse(self.hir_config, &self.pattern)?;
let nfa = NFA::new(self.nfa_config, self.pattern.clone(), &hir)?;
let pikevm = Arc::new(PikeVM::new(nfa));
let pool = {
let pikevm = Arc::clone(&pikevm);
let create = Box::new(move || Cache::new(&pikevm));
CachePool::new(create)
};
Ok(Regex { pikevm, pool })
}
/// This configures whether to enable ASCII case insensitive matching for
/// the entire pattern.
///
/// This setting can also be configured using the inline flag `i`
/// in the pattern. For example, `(?i:foo)` matches `foo` case
/// insensitively while `(?-i:foo)` matches `foo` case sensitively.
///
/// The default for this is `false`.
///
/// # Example
///
/// ```
/// use regex_lite::RegexBuilder;
///
/// let re = RegexBuilder::new(r"foo(?-i:bar)quux")
/// .case_insensitive(true)
/// .build()
/// .unwrap();
/// assert!(re.is_match("FoObarQuUx"));
/// // Even though case insensitive matching is enabled in the builder,
/// // it can be locally disabled within the pattern. In this case,
/// // `bar` is matched case sensitively.
/// assert!(!re.is_match("fooBARquux"));
/// ```
pub fn case_insensitive(&mut self, yes: bool) -> &mut RegexBuilder {
self.hir_config.flags.case_insensitive = yes;
self
}
/// This configures multi-line mode for the entire pattern.
///
/// Enabling multi-line mode changes the behavior of the `^` and `$` anchor
/// assertions. Instead of only matching at the beginning and end of a
/// haystack, respectively, multi-line mode causes them to match at the
/// beginning and end of a line *in addition* to the beginning and end of
/// a haystack. More precisely, `^` will match at the position immediately
/// following a `\n` and `$` will match at the position immediately
/// preceding a `\n`.
///
/// The behavior of this option is impacted by the [`RegexBuilder::crlf`]
/// setting. Namely, CRLF mode changes the line terminator to be either
/// `\r` or `\n`, but never at the position between a `\r` and `\`n.
///
/// This setting can also be configured using the inline flag `m` in the
/// pattern.
///
/// The default for this is `false`.
///
/// # Example
///
/// ```
/// use regex_lite::RegexBuilder;
///
/// let re = RegexBuilder::new(r"^foo$")
/// .multi_line(true)
/// .build()
/// .unwrap();
/// assert_eq!(Some(1..4), re.find("\nfoo\n").map(|m| m.range()));
/// ```
pub fn multi_line(&mut self, yes: bool) -> &mut RegexBuilder {
self.hir_config.flags.multi_line = yes;
self
}
/// This configures dot-matches-new-line mode for the entire pattern.
///
/// Perhaps surprisingly, the default behavior for `.` is not to match
/// any character, but rather, to match any character except for the line
/// terminator (which is `\n` by default). When this mode is enabled, the
/// behavior changes such that `.` truly matches any character.
///
/// This setting can also be configured using the inline flag `s` in the
/// pattern.
///
/// The default for this is `false`.
///
/// # Example
///
/// ```
/// use regex_lite::RegexBuilder;
///
/// let re = RegexBuilder::new(r"foo.bar")
/// .dot_matches_new_line(true)
/// .build()
/// .unwrap();
/// let hay = "foo\nbar";
/// assert_eq!(Some("foo\nbar"), re.find(hay).map(|m| m.as_str()));
/// ```
pub fn dot_matches_new_line(&mut self, yes: bool) -> &mut RegexBuilder {
self.hir_config.flags.dot_matches_new_line = yes;
self
}
/// This configures CRLF mode for the entire pattern.
///
/// When CRLF mode is enabled, both `\r` ("carriage return" or CR for
/// short) and `\n` ("line feed" or LF for short) are treated as line
/// terminators. This results in the following:
///
/// * Unless dot-matches-new-line mode is enabled, `.` will now match any
/// character except for `\n` and `\r`.
/// * When multi-line mode is enabled, `^` will match immediately
/// following a `\n` or a `\r`. Similarly, `$` will match immediately
/// preceding a `\n` or a `\r`. Neither `^` nor `$` will ever match between
/// `\r` and `\n`.
///
/// This setting can also be configured using the inline flag `R` in
/// the pattern.
///
/// The default for this is `false`.
///
/// # Example
///
/// ```
/// use regex_lite::RegexBuilder;
///
/// let re = RegexBuilder::new(r"^foo$")
/// .multi_line(true)
/// .crlf(true)
/// .build()
/// .unwrap();
/// let hay = "\r\nfoo\r\n";
/// // If CRLF mode weren't enabled here, then '$' wouldn't match
/// // immediately after 'foo', and thus no match would be found.
/// assert_eq!(Some("foo"), re.find(hay).map(|m| m.as_str()));
/// ```
///
/// This example demonstrates that `^` will never match at a position
/// between `\r` and `\n`. (`$` will similarly not match between a `\r`
/// and a `\n`.)
///
/// ```
/// use regex_lite::RegexBuilder;
///
/// let re = RegexBuilder::new(r"^")
/// .multi_line(true)
/// .crlf(true)
/// .build()
/// .unwrap();
/// let hay = "\r\n\r\n";
/// let ranges: Vec<_> = re.find_iter(hay).map(|m| m.range()).collect();
/// assert_eq!(ranges, vec![0..0, 2..2, 4..4]);
/// ```
pub fn crlf(&mut self, yes: bool) -> &mut RegexBuilder {
self.hir_config.flags.crlf = yes;
self
}
/// This configures swap-greed mode for the entire pattern.
///
/// When swap-greed mode is enabled, patterns like `a+` will become
/// non-greedy and patterns like `a+?` will become greedy. In other words,
/// the meanings of `a+` and `a+?` are switched.
///
/// This setting can also be configured using the inline flag `U` in the
/// pattern.
///
/// The default for this is `false`.
///
/// # Example
///
/// ```
/// use regex_lite::RegexBuilder;
///
/// let re = RegexBuilder::new(r"a+")
/// .swap_greed(true)
/// .build()
/// .unwrap();
/// assert_eq!(Some("a"), re.find("aaa").map(|m| m.as_str()));
/// ```
pub fn swap_greed(&mut self, yes: bool) -> &mut RegexBuilder {
self.hir_config.flags.swap_greed = yes;
self
}
/// This configures verbose mode for the entire pattern.
///
/// When enabled, whitespace will treated as insignifcant in the pattern
/// and `#` can be used to start a comment until the next new line.
///
/// Normally, in most places in a pattern, whitespace is treated literally.
/// For example ` +` will match one or more ASCII whitespace characters.
///
/// When verbose mode is enabled, `\#` can be used to match a literal `#`
/// and `\ ` can be used to match a literal ASCII whitespace character.
///
/// Verbose mode is useful for permitting regexes to be formatted and
/// broken up more nicely. This may make them more easily readable.
///
/// This setting can also be configured using the inline flag `x` in the
/// pattern.
///
/// The default for this is `false`.
///
/// # Example
///
/// ```
/// use regex_lite::RegexBuilder;
///
/// let pat = r"
/// \b
/// (?<first>[A-Z]\w*) # always start with uppercase letter
/// \s+ # whitespace should separate names
/// (?: # middle name can be an initial!
/// (?:(?<initial>[A-Z])\.|(?<middle>[A-Z]\w*))
/// \s+
/// )?
/// (?<last>[A-Z]\w*)
/// \b
/// ";
/// let re = RegexBuilder::new(pat)
/// .ignore_whitespace(true)
/// .build()
/// .unwrap();
///
/// let caps = re.captures("Harry Potter").unwrap();
/// assert_eq!("Harry", &caps["first"]);
/// assert_eq!("Potter", &caps["last"]);
///
/// let caps = re.captures("Harry J. Potter").unwrap();
/// assert_eq!("Harry", &caps["first"]);
/// // Since a middle name/initial isn't required for an overall match,
/// // we can't assume that 'initial' or 'middle' will be populated!
/// assert_eq!(Some("J"), caps.name("initial").map(|m| m.as_str()));
/// assert_eq!(None, caps.name("middle").map(|m| m.as_str()));
/// assert_eq!("Potter", &caps["last"]);
///
/// let caps = re.captures("Harry James Potter").unwrap();
/// assert_eq!("Harry", &caps["first"]);
/// // Since a middle name/initial isn't required for an overall match,
/// // we can't assume that 'initial' or 'middle' will be populated!
/// assert_eq!(None, caps.name("initial").map(|m| m.as_str()));
/// assert_eq!(Some("James"), caps.name("middle").map(|m| m.as_str()));
/// assert_eq!("Potter", &caps["last"]);
/// ```
pub fn ignore_whitespace(&mut self, yes: bool) -> &mut RegexBuilder {
self.hir_config.flags.ignore_whitespace = yes;
self
}
/// Sets the approximate size limit, in bytes, of the compiled regex.
///
/// This roughly corresponds to the number of heap memory, in bytes,
/// occupied by a single regex. If the regex would otherwise approximately
/// exceed this limit, then compiling that regex will fail.
///
/// The main utility of a method like this is to avoid compiling regexes
/// that use an unexpected amount of resources, such as time and memory.
/// Even if the memory usage of a large regex is acceptable, its search
/// time may not be. Namely, worst case time complexity for search is `O(m
/// * n)`, where `m ~ len(pattern)` and `n ~ len(haystack)`. That is,
/// search time depends, in part, on the size of the compiled regex. This
/// means that putting a limit on the size of the regex limits how much a
/// regex can impact search time.
///
/// The default for this is some reasonable number that permits most
/// patterns to compile successfully.
///
/// # Example
///
/// ```
/// use regex_lite::RegexBuilder;
///
/// assert!(RegexBuilder::new(r"\w").size_limit(100).build().is_err());
/// ```
pub fn size_limit(&mut self, limit: usize) -> &mut RegexBuilder {
self.nfa_config.size_limit = Some(limit);
self
}
/// Set the nesting limit for this parser.
///
/// The nesting limit controls how deep the abstract syntax tree is allowed
/// to be. If the AST exceeds the given limit (e.g., with too many nested
/// groups), then an error is returned by the parser.
///
/// The purpose of this limit is to act as a heuristic to prevent stack
/// overflow for consumers that do structural induction on an AST using
/// explicit recursion. While this crate never does this (instead using
/// constant stack space and moving the call stack to the heap), other
/// crates may.
///
/// This limit is not checked until the entire AST is parsed. Therefore, if
/// callers want to put a limit on the amount of heap space used, then they
/// should impose a limit on the length, in bytes, of the concrete pattern
/// string. In particular, this is viable since this parser implementation
/// will limit itself to heap space proportional to the length of the
/// pattern string. See also the [untrusted inputs](crate#untrusted-input)
/// section in the top-level crate documentation for more information about
/// this.
///
/// Note that a nest limit of `0` will return a nest limit error for most
/// patterns but not all. For example, a nest limit of `0` permits `a` but
/// not `ab`, since `ab` requires an explicit concatenation, which results
/// in a nest depth of `1`. In general, a nest limit is not something that
/// manifests in an obvious way in the concrete syntax, therefore, it
/// should not be used in a granular way.
///
/// # Example
///
/// ```
/// use regex_lite::RegexBuilder;
///
/// assert!(RegexBuilder::new(r"").nest_limit(0).build().is_ok());
/// assert!(RegexBuilder::new(r"a").nest_limit(0).build().is_ok());
/// assert!(RegexBuilder::new(r"(a)").nest_limit(0).build().is_err());
/// ```
pub fn nest_limit(&mut self, limit: u32) -> &mut RegexBuilder {
self.hir_config.nest_limit = limit;
self
}
}