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
use crate::{
dfa::search,
util::{
id::{PatternID, StateID},
matchtypes::{HalfMatch, MatchError},
prefilter,
},
};
/// A trait describing the interface of a deterministic finite automaton (DFA).
///
/// The complexity of this trait probably means that it's unlikely for others
/// to implement it. The primary purpose of the trait is to provide for a way
/// of abstracting over different types of DFAs. In this crate, that means
/// dense DFAs and sparse DFAs. (Dense DFAs are fast but memory hungry, where
/// as sparse DFAs are slower but come with a smaller memory footprint. But
/// they otherwise provide exactly equivalent expressive power.) For example, a
/// [`dfa::regex::Regex`](crate::dfa::regex::Regex) is generic over this trait.
///
/// Normally, a DFA's execution model is very simple. You might have a single
/// start state, zero or more final or "match" states and a function that
/// transitions from one state to the next given the next byte of input.
/// Unfortunately, the interface described by this trait is significantly
/// more complicated than this. The complexity has a number of different
/// reasons, mostly motivated by performance, functionality or space savings:
///
/// * A DFA can search for multiple patterns simultaneously. This
/// means extra information is returned when a match occurs. Namely,
/// a match is not just an offset, but an offset plus a pattern ID.
/// [`Automaton::pattern_count`] returns the number of patterns compiled into
/// the DFA, [`Automaton::match_count`] returns the total number of patterns
/// that match in a particular state and [`Automaton::match_pattern`] permits
/// iterating over the patterns that match in a particular state.
/// * A DFA can have multiple start states, and the choice of which start
/// state to use depends on the content of the string being searched and
/// position of the search, as well as whether the search is an anchored
/// search for a specific pattern in the DFA. Moreover, computing the start
/// state also depends on whether you're doing a forward or a reverse search.
/// [`Automaton::start_state_forward`] and [`Automaton::start_state_reverse`]
/// are used to compute the start state for forward and reverse searches,
/// respectively.
/// * All matches are delayed by one byte to support things like `$` and `\b`
/// at the end of a pattern. Therefore, every use of a DFA is required to use
/// [`Automaton::next_eoi_state`]
/// at the end of the search to compute the final transition.
/// * For optimization reasons, some states are treated specially. Every
/// state is either special or not, which can be determined via the
/// [`Automaton::is_special_state`] method. If it's special, then the state
/// must be at least one of a few possible types of states. (Note that some
/// types can overlap, for example, a match state can also be an accel state.
/// But some types can't. If a state is a dead state, then it can never be any
/// other type of state.) Those types are:
/// * A dead state. A dead state means the DFA will never enter a match
/// state. This can be queried via the [`Automaton::is_dead_state`] method.
/// * A quit state. A quit state occurs if the DFA had to stop the search
/// prematurely for some reason. This can be queried via the
/// [`Automaton::is_quit_state`] method.
/// * A match state. A match state occurs when a match is found. When a DFA
/// enters a match state, the search may stop immediately (when looking
/// for the earliest match), or it may continue to find the leftmost-first
/// match. This can be queried via the [`Automaton::is_match_state`]
/// method.
/// * A start state. A start state is where a search begins. For every
/// search, there is exactly one start state that is used, however, a
/// DFA may contain many start states. When the search is in a start
/// state, it may use a prefilter to quickly skip to candidate matches
/// without executing the DFA on every byte. This can be queried via the
/// [`Automaton::is_start_state`] method.
/// * An accel state. An accel state is a state that is accelerated.
/// That is, it is a state where _most_ of its transitions loop back to
/// itself and only a small number of transitions lead to other states.
/// This kind of state is said to be accelerated because a search routine
/// can quickly look for the bytes leading out of the state instead of
/// continuing to execute the DFA on each byte. This can be queried via the
/// [`Automaton::is_accel_state`] method. And the bytes that lead out of
/// the state can be queried via the [`Automaton::accelerator`] method.
///
/// There are a number of provided methods on this trait that implement
/// efficient searching (for forwards and backwards) with a DFA using all of
/// the above features of this trait. In particular, given the complexity of
/// all these features, implementing a search routine in this trait is not
/// straight forward. If you need to do this for specialized reasons, then
/// it's recommended to look at the source of this crate. It is intentionally
/// well commented to help with this. With that said, it is possible to
/// somewhat simplify the search routine. For example, handling accelerated
/// states is strictly optional, since it is always correct to assume that
/// `Automaton::is_accel_state` returns false. However, one complex part of
/// writing a search routine using this trait is handling the 1-byte delay of a
/// match. That is not optional.
///
/// # Safety
///
/// This trait is unsafe to implement because DFA searching may rely on the
/// correctness of the implementation for memory safety. For example, DFA
/// searching may use explicit bounds check elision, which will in turn rely
/// on the correctness of every function that returns a state ID.
///
/// When implementing this trait, one must uphold the documented correctness
/// guarantees. Otherwise, undefined behavior may occur.
pub unsafe trait Automaton {
/// Transitions from the current state to the next state, given the next
/// byte of input.
///
/// Implementations must guarantee that the returned ID is always a valid
/// ID when `current` refers to a valid ID. Moreover, the transition
/// function must be defined for all possible values of `input`.
///
/// # Panics
///
/// If the given ID does not refer to a valid state, then this routine
/// may panic but it also may not panic and instead return an invalid ID.
/// However, if the caller provides an invalid ID then this must never
/// sacrifice memory safety.
///
/// # Example
///
/// This shows a simplistic example for walking a DFA for a given haystack
/// by using the `next_state` method.
///
/// ```
/// use regex_automata::dfa::{Automaton, dense};
///
/// let dfa = dense::DFA::new(r"[a-z]+r")?;
/// let haystack = "bar".as_bytes();
///
/// // The start state is determined by inspecting the position and the
/// // initial bytes of the haystack.
/// let mut state = dfa.start_state_forward(
/// None, haystack, 0, haystack.len(),
/// );
/// // Walk all the bytes in the haystack.
/// for &b in haystack {
/// state = dfa.next_state(state, b);
/// }
/// // Matches are always delayed by 1 byte, so we must explicitly walk the
/// // special "EOI" transition at the end of the search.
/// state = dfa.next_eoi_state(state);
/// assert!(dfa.is_match_state(state));
///
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
fn next_state(&self, current: StateID, input: u8) -> StateID;
/// Transitions from the current state to the next state, given the next
/// byte of input.
///
/// Unlike [`Automaton::next_state`], implementations may implement this
/// more efficiently by assuming that the `current` state ID is valid.
/// Typically, this manifests by eliding bounds checks.
///
/// # Safety
///
/// Callers of this method must guarantee that `current` refers to a valid
/// state ID. If `current` is not a valid state ID for this automaton, then
/// calling this routine may result in undefined behavior.
///
/// If `current` is valid, then implementations must guarantee that the ID
/// returned is valid for all possible values of `input`.
unsafe fn next_state_unchecked(
&self,
current: StateID,
input: u8,
) -> StateID;
/// Transitions from the current state to the next state for the special
/// EOI symbol.
///
/// Implementations must guarantee that the returned ID is always a valid
/// ID when `current` refers to a valid ID.
///
/// This routine must be called at the end of every search in a correct
/// implementation of search. Namely, DFAs in this crate delay matches
/// by one byte in order to support look-around operators. Thus, after
/// reaching the end of a haystack, a search implementation must follow one
/// last EOI transition.
///
/// It is best to think of EOI as an additional symbol in the alphabet of
/// a DFA that is distinct from every other symbol. That is, the alphabet
/// of DFAs in this crate has a logical size of 257 instead of 256, where
/// 256 corresponds to every possible inhabitant of `u8`. (In practice, the
/// physical alphabet size may be smaller because of alphabet compression
/// via equivalence classes, but EOI is always represented somehow in the
/// alphabet.)
///
/// # Panics
///
/// If the given ID does not refer to a valid state, then this routine
/// may panic but it also may not panic and instead return an invalid ID.
/// However, if the caller provides an invalid ID then this must never
/// sacrifice memory safety.
///
/// # Example
///
/// This shows a simplistic example for walking a DFA for a given haystack,
/// and then finishing the search with the final EOI transition.
///
/// ```
/// use regex_automata::dfa::{Automaton, dense};
///
/// let dfa = dense::DFA::new(r"[a-z]+r")?;
/// let haystack = "bar".as_bytes();
///
/// // The start state is determined by inspecting the position and the
/// // initial bytes of the haystack.
/// let mut state = dfa.start_state_forward(
/// None, haystack, 0, haystack.len(),
/// );
/// // Walk all the bytes in the haystack.
/// for &b in haystack {
/// state = dfa.next_state(state, b);
/// }
/// // Matches are always delayed by 1 byte, so we must explicitly walk
/// // the special "EOI" transition at the end of the search. Without this
/// // final transition, the assert below will fail since the DFA will not
/// // have entered a match state yet!
/// state = dfa.next_eoi_state(state);
/// assert!(dfa.is_match_state(state));
///
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
fn next_eoi_state(&self, current: StateID) -> StateID;
/// Return the ID of the start state for this DFA when executing a forward
/// search.
///
/// Unlike typical DFA implementations, the start state for DFAs in this
/// crate is dependent on a few different factors:
///
/// * The pattern ID, if present. When the underlying DFA has been compiled
/// with multiple patterns _and_ the DFA has been configured to compile
/// an anchored start state for each pattern, then a pattern ID may be
/// specified to execute an anchored search for that specific pattern.
/// If `pattern_id` is invalid or if the DFA doesn't have start states
/// compiled for each pattern, then implementations must panic. DFAs in
/// this crate can be configured to compile start states for each pattern
/// via
/// [`dense::Config::starts_for_each_pattern`](crate::dfa::dense::Config::starts_for_each_pattern).
/// * When `start > 0`, the byte at index `start - 1` may influence the
/// start state if the regex uses `^` or `\b`.
/// * Similarly, when `start == 0`, it may influence the start state when
/// the regex uses `^` or `\A`.
/// * Currently, `end` is unused.
/// * Whether the search is a forward or reverse search. This routine can
/// only be used for forward searches.
///
/// # Panics
///
/// Implementations must panic if `start..end` is not a valid sub-slice of
/// `bytes`. Implementations must also panic if `pattern_id` is non-None
/// and does not refer to a valid pattern, or if the DFA was not compiled
/// with anchored start states for each pattern.
fn start_state_forward(
&self,
pattern_id: Option<PatternID>,
bytes: &[u8],
start: usize,
end: usize,
) -> StateID;
/// Return the ID of the start state for this DFA when executing a reverse
/// search.
///
/// Unlike typical DFA implementations, the start state for DFAs in this
/// crate is dependent on a few different factors:
///
/// * The pattern ID, if present. When the underlying DFA has been compiled
/// with multiple patterns _and_ the DFA has been configured to compile an
/// anchored start state for each pattern, then a pattern ID may be
/// specified to execute an anchored search for that specific pattern. If
/// `pattern_id` is invalid or if the DFA doesn't have start states compiled
/// for each pattern, then implementations must panic. DFAs in this crate
/// can be configured to compile start states for each pattern via
/// [`dense::Config::starts_for_each_pattern`](crate::dfa::dense::Config::starts_for_each_pattern).
/// * When `end < bytes.len()`, the byte at index `end` may influence the
/// start state if the regex uses `$` or `\b`.
/// * Similarly, when `end == bytes.len()`, it may influence the start
/// state when the regex uses `$` or `\z`.
/// * Currently, `start` is unused.
/// * Whether the search is a forward or reverse search. This routine can
/// only be used for reverse searches.
///
/// # Panics
///
/// Implementations must panic if `start..end` is not a valid sub-slice of
/// `bytes`. Implementations must also panic if `pattern_id` is non-None
/// and does not refer to a valid pattern, or if the DFA was not compiled
/// with anchored start states for each pattern.
fn start_state_reverse(
&self,
pattern_id: Option<PatternID>,
bytes: &[u8],
start: usize,
end: usize,
) -> StateID;
/// Returns true if and only if the given identifier corresponds to a
/// "special" state. A special state is one or more of the following:
/// a dead state, a quit state, a match state, a start state or an
/// accelerated state.
///
/// A correct implementation _may_ always return false for states that
/// are either start states or accelerated states, since that information
/// is only intended to be used for optimization purposes. Correct
/// implementations must return true if the state is a dead, quit or match
/// state. This is because search routines using this trait must be able
/// to rely on `is_special_state` as an indicator that a state may need
/// special treatment. (For example, when a search routine sees a dead
/// state, it must terminate.)
///
/// This routine permits search implementations to use a single branch to
/// check whether a state needs special attention before executing the next
/// transition. The example below shows how to do this.
///
/// # Example
///
/// This example shows how `is_special_state` can be used to implement a
/// correct search routine with minimal branching. In particular, this
/// search routine implements "leftmost" matching, which means that it
/// doesn't immediately stop once a match is found. Instead, it continues
/// until it reaches a dead state.
///
/// ```
/// use regex_automata::{
/// dfa::{Automaton, dense},
/// HalfMatch, MatchError, PatternID,
/// };
///
/// fn find_leftmost_first<A: Automaton>(
/// dfa: &A,
/// haystack: &[u8],
/// ) -> Result<Option<HalfMatch>, MatchError> {
/// // The start state is determined by inspecting the position and the
/// // initial bytes of the haystack. Note that start states can never
/// // be match states (since DFAs in this crate delay matches by 1
/// // byte), so we don't need to check if the start state is a match.
/// let mut state = dfa.start_state_forward(
/// None, haystack, 0, haystack.len(),
/// );
/// let mut last_match = None;
/// // Walk all the bytes in the haystack. We can quit early if we see
/// // a dead or a quit state. The former means the automaton will
/// // never transition to any other state. The latter means that the
/// // automaton entered a condition in which its search failed.
/// for (i, &b) in haystack.iter().enumerate() {
/// state = dfa.next_state(state, b);
/// if dfa.is_special_state(state) {
/// if dfa.is_match_state(state) {
/// last_match = Some(HalfMatch::new(
/// dfa.match_pattern(state, 0),
/// i,
/// ));
/// } else if dfa.is_dead_state(state) {
/// return Ok(last_match);
/// } else if dfa.is_quit_state(state) {
/// // It is possible to enter into a quit state after
/// // observing a match has occurred. In that case, we
/// // should return the match instead of an error.
/// if last_match.is_some() {
/// return Ok(last_match);
/// }
/// return Err(MatchError::Quit { byte: b, offset: i });
/// }
/// // Implementors may also want to check for start or accel
/// // states and handle them differently for performance
/// // reasons. But it is not necessary for correctness.
/// }
/// }
/// // Matches are always delayed by 1 byte, so we must explicitly walk
/// // the special "EOI" transition at the end of the search.
/// state = dfa.next_eoi_state(state);
/// if dfa.is_match_state(state) {
/// last_match = Some(HalfMatch::new(
/// dfa.match_pattern(state, 0),
/// haystack.len(),
/// ));
/// }
/// Ok(last_match)
/// }
///
/// // We use a greedy '+' operator to show how the search doesn't just
/// // stop once a match is detected. It continues extending the match.
/// // Using '[a-z]+?' would also work as expected and stop the search
/// // early. Greediness is built into the automaton.
/// let dfa = dense::DFA::new(r"[a-z]+")?;
/// let haystack = "123 foobar 4567".as_bytes();
/// let mat = find_leftmost_first(&dfa, haystack)?.unwrap();
/// assert_eq!(mat.pattern().as_usize(), 0);
/// assert_eq!(mat.offset(), 10);
///
/// // Here's another example that tests our handling of the special EOI
/// // transition. This will fail to find a match if we don't call
/// // 'next_eoi_state' at the end of the search since the match isn't
/// // found until the final byte in the haystack.
/// let dfa = dense::DFA::new(r"[0-9]{4}")?;
/// let haystack = "123 foobar 4567".as_bytes();
/// let mat = find_leftmost_first(&dfa, haystack)?.unwrap();
/// assert_eq!(mat.pattern().as_usize(), 0);
/// assert_eq!(mat.offset(), 15);
///
/// // And note that our search implementation above automatically works
/// // with multi-DFAs. Namely, `dfa.match_pattern(match_state, 0)` selects
/// // the appropriate pattern ID for us.
/// let dfa = dense::DFA::new_many(&[r"[a-z]+", r"[0-9]+"])?;
/// let haystack = "123 foobar 4567".as_bytes();
/// let mat = find_leftmost_first(&dfa, haystack)?.unwrap();
/// assert_eq!(mat.pattern().as_usize(), 1);
/// assert_eq!(mat.offset(), 3);
/// let mat = find_leftmost_first(&dfa, &haystack[3..])?.unwrap();
/// assert_eq!(mat.pattern().as_usize(), 0);
/// assert_eq!(mat.offset(), 7);
/// let mat = find_leftmost_first(&dfa, &haystack[10..])?.unwrap();
/// assert_eq!(mat.pattern().as_usize(), 1);
/// assert_eq!(mat.offset(), 5);
///
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
fn is_special_state(&self, id: StateID) -> bool;
/// Returns true if and only if the given identifier corresponds to a dead
/// state. When a DFA enters a dead state, it is impossible to leave. That
/// is, every transition on a dead state by definition leads back to the
/// same dead state.
///
/// In practice, the dead state always corresponds to the identifier `0`.
/// Moreover, in practice, there is only one dead state.
///
/// The existence of a dead state is not strictly required in the classical
/// model of finite state machines, where one generally only cares about
/// the question of whether an input sequence matches or not. Dead states
/// are not needed to answer that question, since one can immediately quit
/// as soon as one enters a final or "match" state. However, we don't just
/// care about matches but also care about the location of matches, and
/// more specifically, care about semantics like "greedy" matching.
///
/// For example, given the pattern `a+` and the input `aaaz`, the dead
/// state won't be entered until the state machine reaches `z` in the
/// input, at which point, the search routine can quit. But without the
/// dead state, the search routine wouldn't know when to quit. In a
/// classical representation, the search routine would stop after seeing
/// the first `a` (which is when the search would enter a match state). But
/// this wouldn't implement "greedy" matching where `a+` matches as many
/// `a`'s as possible.
///
/// # Example
///
/// See the example for [`Automaton::is_special_state`] for how to use this
/// method correctly.
fn is_dead_state(&self, id: StateID) -> bool;
/// Returns true if and only if the given identifier corresponds to a quit
/// state. A quit state is like a dead state (it has no transitions other
/// than to itself), except it indicates that the DFA failed to complete
/// the search. When this occurs, callers can neither accept or reject that
/// a match occurred.
///
/// In practice, the quit state always corresponds to the state immediately
/// following the dead state. (Which is not usually represented by `1`,
/// since state identifiers are pre-multiplied by the state machine's
/// alphabet stride, and the alphabet stride varies between DFAs.)
///
/// By default, state machines created by this crate will never enter a
/// quit state. Since entering a quit state is the only way for a DFA
/// in this crate to fail at search time, it follows that the default
/// configuration can never produce a match error. Nevertheless, handling
/// quit states is necessary to correctly support all configurations in
/// this crate.
///
/// The typical way in which a quit state can occur is when heuristic
/// support for Unicode word boundaries is enabled via the
/// [`dense::Config::unicode_word_boundary`](crate::dfa::dense::Config::unicode_word_boundary)
/// option. But other options, like the lower level
/// [`dense::Config::quit`](crate::dfa::dense::Config::quit)
/// configuration, can also result in a quit state being entered. The
/// purpose of the quit state is to provide a way to execute a fast DFA
/// in common cases while delegating to slower routines when the DFA quits.
///
/// The default search implementations provided by this crate will return
/// a [`MatchError::Quit`](crate::MatchError::Quit) error when a quit state
/// is entered.
///
/// # Example
///
/// See the example for [`Automaton::is_special_state`] for how to use this
/// method correctly.
fn is_quit_state(&self, id: StateID) -> bool;
/// Returns true if and only if the given identifier corresponds to a
/// match state. A match state is also referred to as a "final" state and
/// indicates that a match has been found.
///
/// If all you care about is whether a particular pattern matches in the
/// input sequence, then a search routine can quit early as soon as the
/// machine enters a match state. However, if you're looking for the
/// standard "leftmost-first" match location, then search _must_ continue
/// until either the end of the input or until the machine enters a dead
/// state. (Since either condition implies that no other useful work can
/// be done.) Namely, when looking for the location of a match, then
/// search implementations should record the most recent location in
/// which a match state was entered, but otherwise continue executing the
/// search as normal. (The search may even leave the match state.) Once
/// the termination condition is reached, the most recently recorded match
/// location should be returned.
///
/// Finally, one additional power given to match states in this crate
/// is that they are always associated with a specific pattern in order
/// to support multi-DFAs. See [`Automaton::match_pattern`] for more
/// details and an example for how to query the pattern associated with a
/// particular match state.
///
/// # Example
///
/// See the example for [`Automaton::is_special_state`] for how to use this
/// method correctly.
fn is_match_state(&self, id: StateID) -> bool;
/// Returns true if and only if the given identifier corresponds to a
/// start state. A start state is a state in which a DFA begins a search.
/// All searches begin in a start state. Moreover, since all matches are
/// delayed by one byte, a start state can never be a match state.
///
/// The main role of a start state is, as mentioned, to be a starting
/// point for a DFA. This starting point is determined via one of
/// [`Automaton::start_state_forward`] or
/// [`Automaton::start_state_reverse`], depending on whether one is doing
/// a forward or a reverse search, respectively.
///
/// A secondary use of start states is for prefix acceleration. Namely,
/// while executing a search, if one detects that you're in a start state,
/// then it may be faster to look for the next match of a prefix of the
/// pattern, if one exists. If a prefix exists and since all matches must
/// begin with that prefix, then skipping ahead to occurrences of that
/// prefix may be much faster than executing the DFA.
///
/// # Example
///
/// This example shows how to implement your own search routine that does
/// a prefix search whenever the search enters a start state.
///
/// Note that you do not need to implement your own search routine to
/// make use of prefilters like this. The search routines provided
/// by this crate already implement prefilter support via the
/// [`Prefilter`](crate::util::prefilter::Prefilter) trait. The various
/// `find_*_at` routines on this trait support the `Prefilter` trait
/// through [`Scanner`](crate::util::prefilter::Scanner)s. This example is
/// meant to show how you might deal with prefilters in a simplified case
/// if you are implementing your own search routine.
///
/// ```
/// use regex_automata::{
/// MatchError, PatternID,
/// dfa::{Automaton, dense},
/// HalfMatch,
/// };
///
/// fn find_byte(slice: &[u8], at: usize, byte: u8) -> Option<usize> {
/// // Would be faster to use the memchr crate, but this is still
/// // faster than running through the DFA.
/// slice[at..].iter().position(|&b| b == byte).map(|i| at + i)
/// }
///
/// fn find_leftmost_first<A: Automaton>(
/// dfa: &A,
/// haystack: &[u8],
/// prefix_byte: Option<u8>,
/// ) -> Result<Option<HalfMatch>, MatchError> {
/// // See the Automaton::is_special_state example for similar code
/// // with more comments.
///
/// let mut state = dfa.start_state_forward(
/// None, haystack, 0, haystack.len(),
/// );
/// let mut last_match = None;
/// let mut pos = 0;
/// while pos < haystack.len() {
/// let b = haystack[pos];
/// state = dfa.next_state(state, b);
/// pos += 1;
/// if dfa.is_special_state(state) {
/// if dfa.is_match_state(state) {
/// last_match = Some(HalfMatch::new(
/// dfa.match_pattern(state, 0),
/// pos - 1,
/// ));
/// } else if dfa.is_dead_state(state) {
/// return Ok(last_match);
/// } else if dfa.is_quit_state(state) {
/// // It is possible to enter into a quit state after
/// // observing a match has occurred. In that case, we
/// // should return the match instead of an error.
/// if last_match.is_some() {
/// return Ok(last_match);
/// }
/// return Err(MatchError::Quit {
/// byte: b, offset: pos - 1,
/// });
/// } else if dfa.is_start_state(state) {
/// // If we're in a start state and know all matches begin
/// // with a particular byte, then we can quickly skip to
/// // candidate matches without running the DFA through
/// // every byte inbetween.
/// if let Some(prefix_byte) = prefix_byte {
/// pos = match find_byte(haystack, pos, prefix_byte) {
/// Some(pos) => pos,
/// None => break,
/// };
/// }
/// }
/// }
/// }
/// // Matches are always delayed by 1 byte, so we must explicitly walk
/// // the special "EOI" transition at the end of the search.
/// state = dfa.next_eoi_state(state);
/// if dfa.is_match_state(state) {
/// last_match = Some(HalfMatch::new(
/// dfa.match_pattern(state, 0),
/// haystack.len(),
/// ));
/// }
/// Ok(last_match)
/// }
///
/// // In this example, it's obvious that all occurrences of our pattern
/// // begin with 'Z', so we pass in 'Z'.
/// let dfa = dense::DFA::new(r"Z[a-z]+")?;
/// let haystack = "123 foobar Zbaz quux".as_bytes();
/// let mat = find_leftmost_first(&dfa, haystack, Some(b'Z'))?.unwrap();
/// assert_eq!(mat.pattern().as_usize(), 0);
/// assert_eq!(mat.offset(), 15);
///
/// // But note that we don't need to pass in a prefix byte. If we don't,
/// // then the search routine does no acceleration.
/// let mat = find_leftmost_first(&dfa, haystack, None)?.unwrap();
/// assert_eq!(mat.pattern().as_usize(), 0);
/// assert_eq!(mat.offset(), 15);
///
/// // However, if we pass an incorrect byte, then the prefix search will
/// // result in incorrect results.
/// assert_eq!(find_leftmost_first(&dfa, haystack, Some(b'X'))?, None);
///
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
fn is_start_state(&self, id: StateID) -> bool;
/// Returns true if and only if the given identifier corresponds to an
/// accelerated state.
///
/// An accelerated state is a special optimization
/// trick implemented by this crate. Namely, if
/// [`dense::Config::accelerate`](crate::dfa::dense::Config::accelerate) is
/// enabled (and it is by default), then DFAs generated by this crate will
/// tag states meeting certain characteristics as accelerated. States meet
/// this criteria whenever most of their transitions are self-transitions.
/// That is, transitions that loop back to the same state. When a small
/// number of transitions aren't self-transitions, then it follows that
/// there are only a small number of bytes that can cause the DFA to leave
/// that state. Thus, there is an opportunity to look for those bytes
/// using more optimized routines rather than continuing to run through
/// the DFA. This trick is similar to the prefilter idea described in
/// the documentation of [`Automaton::is_start_state`] with two main
/// differences:
///
/// 1. It is more limited since acceleration only applies to single bytes.
/// This means states are rarely accelerated when Unicode mode is enabled
/// (which is enabled by default).
/// 2. It can occur anywhere in the DFA, which increases optimization
/// opportunities.
///
/// Like the prefilter idea, the main downside (and a possible reason to
/// disable it) is that it can lead to worse performance in some cases.
/// Namely, if a state is accelerated for very common bytes, then the
/// overhead of checking for acceleration and using the more optimized
/// routines to look for those bytes can cause overall performance to be
/// worse than if acceleration wasn't enabled at all.
///
/// A simple example of a regex that has an accelerated state is
/// `(?-u)[^a]+a`. Namely, the `[^a]+` sub-expression gets compiled down
/// into a single state where all transitions except for `a` loop back to
/// itself, and where `a` is the only transition (other than the special
/// EOI transition) that goes to some other state. Thus, this state can
/// be accelerated and implemented more efficiently by calling an
/// optimized routine like `memchr` with `a` as the needle. Notice that
/// the `(?-u)` to disable Unicode is necessary here, as without it,
/// `[^a]` will match any UTF-8 encoding of any Unicode scalar value other
/// than `a`. This more complicated expression compiles down to many DFA
/// states and the simple acceleration optimization is no longer available.
///
/// Typically, this routine is used to guard calls to
/// [`Automaton::accelerator`], which returns the accelerated bytes for
/// the specified state.
fn is_accel_state(&self, id: StateID) -> bool;
/// Returns the total number of patterns compiled into this DFA.
///
/// In the case of a DFA that contains no patterns, this must return `0`.
///
/// # Example
///
/// This example shows the pattern count for a DFA that never matches:
///
/// ```
/// use regex_automata::dfa::{Automaton, dense::DFA};
///
/// let dfa: DFA<Vec<u32>> = DFA::never_match()?;
/// assert_eq!(dfa.pattern_count(), 0);
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
///
/// And another example for a DFA that matches at every position:
///
/// ```
/// use regex_automata::dfa::{Automaton, dense::DFA};
///
/// let dfa: DFA<Vec<u32>> = DFA::always_match()?;
/// assert_eq!(dfa.pattern_count(), 1);
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
///
/// And finally, a DFA that was constructed from multiple patterns:
///
/// ```
/// use regex_automata::dfa::{Automaton, dense::DFA};
///
/// let dfa = DFA::new_many(&["[0-9]+", "[a-z]+", "[A-Z]+"])?;
/// assert_eq!(dfa.pattern_count(), 3);
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
fn pattern_count(&self) -> usize;
/// Returns the total number of patterns that match in this state.
///
/// If the given state is not a match state, then implementations may
/// panic.
///
/// If the DFA was compiled with one pattern, then this must necessarily
/// always return `1` for all match states.
///
/// Implementations must guarantee that [`Automaton::match_pattern`] can
/// be called with indices up to (but not including) the count returned by
/// this routine without panicking.
///
/// # Panics
///
/// Implementations are permitted to panic if the provided state ID does
/// not correspond to a match state.
///
/// # Example
///
/// This example shows a simple instance of implementing overlapping
/// matches. In particular, it shows not only how to determine how many
/// patterns have matched in a particular state, but also how to access
/// which specific patterns have matched.
///
/// Notice that we must use [`MatchKind::All`](crate::MatchKind::All)
/// when building the DFA. If we used
/// [`MatchKind::LeftmostFirst`](crate::MatchKind::LeftmostFirst)
/// instead, then the DFA would not be constructed in a way that supports
/// overlapping matches. (It would only report a single pattern that
/// matches at any particular point in time.)
///
/// Another thing to take note of is the patterns used and the order in
/// which the pattern IDs are reported. In the example below, pattern `3`
/// is yielded first. Why? Because it corresponds to the match that
/// appears first. Namely, the `@` symbol is part of `\S+` but not part
/// of any of the other patterns. Since the `\S+` pattern has a match that
/// starts to the left of any other pattern, its ID is returned before any
/// other.
///
/// ```
/// use regex_automata::{
/// dfa::{Automaton, dense},
/// MatchKind,
/// };
///
/// let dfa = dense::Builder::new()
/// .configure(dense::Config::new().match_kind(MatchKind::All))
/// .build_many(&[
/// r"\w+", r"[a-z]+", r"[A-Z]+", r"\S+",
/// ])?;
/// let haystack = "@bar".as_bytes();
///
/// // The start state is determined by inspecting the position and the
/// // initial bytes of the haystack.
/// let mut state = dfa.start_state_forward(
/// None, haystack, 0, haystack.len(),
/// );
/// // Walk all the bytes in the haystack.
/// for &b in haystack {
/// state = dfa.next_state(state, b);
/// }
/// state = dfa.next_eoi_state(state);
///
/// assert!(dfa.is_match_state(state));
/// assert_eq!(dfa.match_count(state), 3);
/// // The following calls are guaranteed to not panic since `match_count`
/// // returned `3` above.
/// assert_eq!(dfa.match_pattern(state, 0).as_usize(), 3);
/// assert_eq!(dfa.match_pattern(state, 1).as_usize(), 0);
/// assert_eq!(dfa.match_pattern(state, 2).as_usize(), 1);
///
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
fn match_count(&self, id: StateID) -> usize;
/// Returns the pattern ID corresponding to the given match index in the
/// given state.
///
/// See [`Automaton::match_count`] for an example of how to use this
/// method correctly. Note that if you know your DFA is compiled with a
/// single pattern, then this routine is never necessary since it will
/// always return a pattern ID of `0` for an index of `0` when `id`
/// corresponds to a match state.
///
/// Typically, this routine is used when implementing an overlapping
/// search, as the example for `Automaton::match_count` does.
///
/// # Panics
///
/// If the state ID is not a match state or if the match index is out
/// of bounds for the given state, then this routine may either panic
/// or produce an incorrect result. If the state ID is correct and the
/// match index is correct, then this routine must always produce a valid
/// `PatternID`.
fn match_pattern(&self, id: StateID, index: usize) -> PatternID;
/// Return a slice of bytes to accelerate for the given state, if possible.
///
/// If the given state has no accelerator, then an empty slice must be
/// returned. If `Automaton::is_accel_state` returns true for the given
/// ID, then this routine _must_ return a non-empty slice, but it is not
/// required to do so.
///
/// If the given ID is not a valid state ID for this automaton, then
/// implementations may panic or produce incorrect results.
///
/// See [`Automaton::is_accel_state`] for more details on state
/// acceleration.
///
/// By default, this method will always return an empty slice.
///
/// # Example
///
/// This example shows a contrived case in which we build a regex that we
/// know is accelerated and extract the accelerator from a state.
///
/// ```
/// use regex_automata::{
/// nfa::thompson,
/// dfa::{Automaton, dense},
/// util::id::StateID,
/// SyntaxConfig,
/// };
///
/// let dfa = dense::Builder::new()
/// // We disable Unicode everywhere and permit the regex to match
/// // invalid UTF-8. e.g., `[^abc]` matches `\xFF`, which is not valid
/// // UTF-8.
/// .syntax(SyntaxConfig::new().unicode(false).utf8(false))
/// // This makes the implicit `(?s:.)*?` prefix added to the regex
/// // match through arbitrary bytes instead of being UTF-8 aware. This
/// // isn't necessary to get acceleration to work in this case, but
/// // it does make the DFA substantially simpler.
/// .thompson(thompson::Config::new().utf8(false))
/// .build("[^abc]+a")?;
///
/// // Here we just pluck out the state that we know is accelerated.
/// // While the stride calculations are something that can be relied
/// // on by callers, the specific position of the accelerated state is
/// // implementation defined.
/// //
/// // N.B. We get '3' by inspecting the state machine using 'regex-cli'.
/// // e.g., try `regex-cli debug dfa dense '[^abc]+a' -BbUC`.
/// let id = StateID::new(3 * dfa.stride()).unwrap();
/// let accelerator = dfa.accelerator(id);
/// // The `[^abc]+` sub-expression permits [a, b, c] to be accelerated.
/// assert_eq!(accelerator, &[b'a', b'b', b'c']);
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
fn accelerator(&self, _id: StateID) -> &[u8] {
&[]
}
/// Executes a forward search and returns the end position of the first
/// match that is found as early as possible. If no match exists, then
/// `None` is returned.
///
/// This routine stops scanning input as soon as the search observes a
/// match state. This is useful for implementing boolean `is_match`-like
/// routines, where as little work is done as possible.
///
/// See [`Automaton::find_earliest_fwd_at`] for additional functionality,
/// such as providing a prefilter, a specific pattern to match and the
/// bounds of the search within the haystack. This routine is meant as
/// a convenience for common cases where the additional functionality is
/// not needed.
///
/// # Errors
///
/// This routine only errors if the search could not complete. For
/// DFAs generated by this crate, this only occurs in a non-default
/// configuration where quit bytes are used or Unicode word boundaries are
/// heuristically enabled.
///
/// When a search cannot complete, callers cannot know whether a match
/// exists or not.
///
/// # Example
///
/// This example shows how to use this method with a
/// [`dense::DFA`](crate::dfa::dense::DFA). In particular, it demonstrates
/// how the position returned might differ from what one might expect when
/// executing a traditional leftmost search.
///
/// ```
/// use regex_automata::{
/// dfa::{Automaton, dense},
/// HalfMatch,
/// };
///
/// let dfa = dense::DFA::new("foo[0-9]+")?;
/// // Normally, the end of the leftmost first match here would be 8,
/// // corresponding to the end of the input. But the "earliest" semantics
/// // this routine cause it to stop as soon as a match is known, which
/// // occurs once 'foo[0-9]' has matched.
/// let expected = HalfMatch::must(0, 4);
/// assert_eq!(Some(expected), dfa.find_earliest_fwd(b"foo12345")?);
///
/// let dfa = dense::DFA::new("abc|a")?;
/// // Normally, the end of the leftmost first match here would be 3,
/// // but the shortest match semantics detect a match earlier.
/// let expected = HalfMatch::must(0, 1);
/// assert_eq!(Some(expected), dfa.find_earliest_fwd(b"abc")?);
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
#[inline]
fn find_earliest_fwd(
&self,
bytes: &[u8],
) -> Result<Option<HalfMatch>, MatchError> {
self.find_earliest_fwd_at(None, None, bytes, 0, bytes.len())
}
/// Executes a reverse search and returns the start position of the first
/// match that is found as early as possible. If no match exists, then
/// `None` is returned.
///
/// This routine stops scanning input as soon as the search observes a
/// match state.
///
/// Note that while it is not technically necessary to build a reverse
/// automaton to use a reverse search, it is likely that you'll want to do
/// so. Namely, the typical use of a reverse search is to find the starting
/// location of a match once its end is discovered from a forward search. A
/// reverse DFA automaton can be built by configuring the intermediate NFA
/// to be reversed via
/// [`nfa::thompson::Config::reverse`](crate::nfa::thompson::Config::reverse).
///
/// # Errors
///
/// This routine only errors if the search could not complete. For
/// DFAs generated by this crate, this only occurs in a non-default
/// configuration where quit bytes are used or Unicode word boundaries are
/// heuristically enabled.
///
/// When a search cannot complete, callers cannot know whether a match
/// exists or not.
///
/// # Example
///
/// This example shows how to use this method with a
/// [`dense::DFA`](crate::dfa::dense::DFA). In particular, it demonstrates
/// how the position returned might differ from what one might expect when
/// executing a traditional leftmost reverse search.
///
/// ```
/// use regex_automata::{
/// nfa::thompson,
/// dfa::{Automaton, dense},
/// HalfMatch,
/// };
///
/// let dfa = dense::Builder::new()
/// .thompson(thompson::Config::new().reverse(true))
/// .build("[a-z]+[0-9]+")?;
/// // Normally, the end of the leftmost first match here would be 0,
/// // corresponding to the beginning of the input. But the "earliest"
/// // semantics of this routine cause it to stop as soon as a match is
/// // known, which occurs once '[a-z][0-9]+' has matched.
/// let expected = HalfMatch::must(0, 2);
/// assert_eq!(Some(expected), dfa.find_earliest_rev(b"foo12345")?);
///
/// let dfa = dense::Builder::new()
/// .thompson(thompson::Config::new().reverse(true))
/// .build("abc|c")?;
/// // Normally, the end of the leftmost first match here would be 0,
/// // but the shortest match semantics detect a match earlier.
/// let expected = HalfMatch::must(0, 2);
/// assert_eq!(Some(expected), dfa.find_earliest_rev(b"abc")?);
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
#[inline]
fn find_earliest_rev(
&self,
bytes: &[u8],
) -> Result<Option<HalfMatch>, MatchError> {
self.find_earliest_rev_at(None, bytes, 0, bytes.len())
}
/// Executes a forward search and returns the end position of the leftmost
/// match that is found. If no match exists, then `None` is returned.
///
/// # Errors
///
/// This routine only errors if the search could not complete. For
/// DFAs generated by this crate, this only occurs in a non-default
/// configuration where quit bytes are used or Unicode word boundaries are
/// heuristically enabled.
///
/// When a search cannot complete, callers cannot know whether a match
/// exists or not.
///
/// # Notes for implementors
///
/// Implementors of this trait are not required to implement any particular
/// match semantics (such as leftmost-first), which are instead manifest in
/// the DFA's transitions.
///
/// In particular, this method must continue searching even after it enters
/// a match state. The search should only terminate once it has reached
/// the end of the input or when it has entered a dead or quit state. Upon
/// termination, the position of the last byte seen while still in a match
/// state is returned.
///
/// Since this trait provides an implementation for this method by default,
/// it's unlikely that one will need to implement this.
///
/// # Example
///
/// This example shows how to use this method with a
/// [`dense::DFA`](crate::dfa::dense::DFA). By default, a dense DFA uses
/// "leftmost first" match semantics.
///
/// Leftmost first match semantics corresponds to the match with the
/// smallest starting offset, but where the end offset is determined by
/// preferring earlier branches in the original regular expression. For
/// example, `Sam|Samwise` will match `Sam` in `Samwise`, but `Samwise|Sam`
/// will match `Samwise` in `Samwise`.
///
/// Generally speaking, the "leftmost first" match is how most backtracking
/// regular expressions tend to work. This is in contrast to POSIX-style
/// regular expressions that yield "leftmost longest" matches. Namely,
/// both `Sam|Samwise` and `Samwise|Sam` match `Samwise` when using
/// leftmost longest semantics. (This crate does not currently support
/// leftmost longest semantics.)
///
/// ```
/// use regex_automata::{
/// dfa::{Automaton, dense},
/// HalfMatch,
/// };
///
/// let dfa = dense::DFA::new("foo[0-9]+")?;
/// let expected = HalfMatch::must(0, 8);
/// assert_eq!(Some(expected), dfa.find_leftmost_fwd(b"foo12345")?);
///
/// // Even though a match is found after reading the first byte (`a`),
/// // the leftmost first match semantics demand that we find the earliest
/// // match that prefers earlier parts of the pattern over latter parts.
/// let dfa = dense::DFA::new("abc|a")?;
/// let expected = HalfMatch::must(0, 3);
/// assert_eq!(Some(expected), dfa.find_leftmost_fwd(b"abc")?);
///
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
#[inline]
fn find_leftmost_fwd(
&self,
bytes: &[u8],
) -> Result<Option<HalfMatch>, MatchError> {
self.find_leftmost_fwd_at(None, None, bytes, 0, bytes.len())
}
/// Executes a reverse search and returns the start of the position of the
/// leftmost match that is found. If no match exists, then `None` is
/// returned.
///
/// # Errors
///
/// This routine only errors if the search could not complete. For
/// DFAs generated by this crate, this only occurs in a non-default
/// configuration where quit bytes are used or Unicode word boundaries are
/// heuristically enabled.
///
/// When a search cannot complete, callers cannot know whether a match
/// exists or not.
///
/// # Notes for implementors
///
/// Implementors of this trait are not required to implement any particular
/// match semantics (such as leftmost-first), which are instead manifest in
/// the DFA's transitions.
///
/// In particular, this method must continue searching even after it enters
/// a match state. The search should only terminate once it has reached
/// the end of the input or when it has entered a dead or quit state. Upon
/// termination, the position of the last byte seen while still in a match
/// state is returned.
///
/// Since this trait provides an implementation for this method by default,
/// it's unlikely that one will need to implement this.
///
/// # Example
///
/// This example shows how to use this method with a
/// [`dense::DFA`](crate::dfa::dense::DFA). In particular, this routine
/// is principally useful when used in conjunction with the
/// [`nfa::thompson::Config::reverse`](crate::nfa::thompson::Config::reverse)
/// configuration. In general, it's unlikely to be correct to use both
/// `find_leftmost_fwd` and `find_leftmost_rev` with the same DFA since any
/// particular DFA will only support searching in one direction with
/// respect to the pattern.
///
/// ```
/// use regex_automata::{
/// nfa::thompson,
/// dfa::{Automaton, dense},
/// HalfMatch,
/// };
///
/// let dfa = dense::Builder::new()
/// .thompson(thompson::Config::new().reverse(true))
/// .build("foo[0-9]+")?;
/// let expected = HalfMatch::must(0, 0);
/// assert_eq!(Some(expected), dfa.find_leftmost_rev(b"foo12345")?);
///
/// // Even though a match is found after reading the last byte (`c`),
/// // the leftmost first match semantics demand that we find the earliest
/// // match that prefers earlier parts of the pattern over latter parts.
/// let dfa = dense::Builder::new()
/// .thompson(thompson::Config::new().reverse(true))
/// .build("abc|c")?;
/// let expected = HalfMatch::must(0, 0);
/// assert_eq!(Some(expected), dfa.find_leftmost_rev(b"abc")?);
///
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
#[inline]
fn find_leftmost_rev(
&self,
bytes: &[u8],
) -> Result<Option<HalfMatch>, MatchError> {
self.find_leftmost_rev_at(None, bytes, 0, bytes.len())
}
/// Executes an overlapping forward search and returns the end position of
/// matches as they are found. If no match exists, then `None` is returned.
///
/// This routine is principally only useful when searching for multiple
/// patterns on inputs where multiple patterns may match the same regions
/// of text. In particular, callers must preserve the automaton's search
/// state from prior calls so that the implementation knows where the last
/// match occurred.
///
/// # Errors
///
/// This routine only errors if the search could not complete. For
/// DFAs generated by this crate, this only occurs in a non-default
/// configuration where quit bytes are used or Unicode word boundaries are
/// heuristically enabled.
///
/// When a search cannot complete, callers cannot know whether a match
/// exists or not.
///
/// # Example
///
/// This example shows how to run a basic overlapping search with a
/// [`dense::DFA`](crate::dfa::dense::DFA). Notice that we build the
/// automaton with a `MatchKind::All` configuration. Overlapping searches
/// are unlikely to work as one would expect when using the default
/// `MatchKind::LeftmostFirst` match semantics, since leftmost-first
/// matching is fundamentally incompatible with overlapping searches.
/// Namely, overlapping searches need to report matches as they are seen,
/// where as leftmost-first searches will continue searching even after a
/// match has been observed in order to find the conventional end position
/// of the match. More concretely, leftmost-first searches use dead states
/// to terminate a search after a specific match can no longer be extended.
/// Overlapping searches instead do the opposite by continuing the search
/// to find totally new matches (potentially of other patterns).
///
/// ```
/// use regex_automata::{
/// dfa::{Automaton, OverlappingState, dense},
/// HalfMatch,
/// MatchKind,
/// };
///
/// let dfa = dense::Builder::new()
/// .configure(dense::Config::new().match_kind(MatchKind::All))
/// .build_many(&[r"\w+$", r"\S+$"])?;
/// let haystack = "@foo".as_bytes();
/// let mut state = OverlappingState::start();
///
/// let expected = Some(HalfMatch::must(1, 4));
/// let got = dfa.find_overlapping_fwd(haystack, &mut state)?;
/// assert_eq!(expected, got);
///
/// // The first pattern also matches at the same position, so re-running
/// // the search will yield another match. Notice also that the first
/// // pattern is returned after the second. This is because the second
/// // pattern begins its match before the first, is therefore an earlier
/// // match and is thus reported first.
/// let expected = Some(HalfMatch::must(0, 4));
/// let got = dfa.find_overlapping_fwd(haystack, &mut state)?;
/// assert_eq!(expected, got);
///
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
#[inline]
fn find_overlapping_fwd(
&self,
bytes: &[u8],
state: &mut OverlappingState,
) -> Result<Option<HalfMatch>, MatchError> {
self.find_overlapping_fwd_at(None, None, bytes, 0, bytes.len(), state)
}
/// Executes a forward search and returns the end position of the first
/// match that is found as early as possible. If no match exists, then
/// `None` is returned.
///
/// This routine stops scanning input as soon as the search observes a
/// match state. This is useful for implementing boolean `is_match`-like
/// routines, where as little work is done as possible.
///
/// This is like [`Automaton::find_earliest_fwd`], except it provides some
/// additional control over how the search is executed:
///
/// * `pre` is a prefilter scanner that, when given, is used whenever the
/// DFA enters its starting state. This is meant to speed up searches where
/// one or a small number of literal prefixes are known.
/// * `pattern_id` specifies a specific pattern in the DFA to run an
/// anchored search for. If not given, then a search for any pattern is
/// performed. For DFAs built by this crate,
/// [`dense::Config::starts_for_each_pattern`](crate::dfa::dense::Config::starts_for_each_pattern)
/// must be enabled to use this functionality.
/// * `start` and `end` permit searching a specific region of the haystack
/// `bytes`. This is useful when implementing an iterator over matches
/// within the same haystack, which cannot be done correctly by simply
/// providing a subslice of `bytes`. (Because the existence of look-around
/// operations such as `\b`, `^` and `$` need to take the surrounding
/// context into account. This cannot be done if the haystack doesn't
/// contain it.)
///
/// The examples below demonstrate each of these additional parameters.
///
/// # Errors
///
/// This routine only errors if the search could not complete. For
/// DFAs generated by this crate, this only occurs in a non-default
/// configuration where quit bytes are used or Unicode word boundaries are
/// heuristically enabled.
///
/// When a search cannot complete, callers cannot know whether a match
/// exists or not.
///
/// # Panics
///
/// This routine must panic if a `pattern_id` is given and the underlying
/// DFA does not support specific pattern searches.
///
/// It must also panic if the given haystack range is not valid.
///
/// # Example: prefilter
///
/// This example shows how to provide a prefilter for a pattern where all
/// matches start with a `z` byte.
///
/// ```
/// use regex_automata::{
/// dfa::{Automaton, dense},
/// util::prefilter::{Candidate, Prefilter, Scanner, State},
/// HalfMatch,
/// };
///
/// #[derive(Debug)]
/// pub struct ZPrefilter;
///
/// impl Prefilter for ZPrefilter {
/// fn next_candidate(
/// &self,
/// _: &mut State,
/// haystack: &[u8],
/// at: usize,
/// ) -> Candidate {
/// // Try changing b'z' to b'q' and observe this test fail since
/// // the prefilter will skip right over the match.
/// match haystack.iter().position(|&b| b == b'z') {
/// None => Candidate::None,
/// Some(i) => Candidate::PossibleStartOfMatch(at + i),
/// }
/// }
///
/// fn heap_bytes(&self) -> usize {
/// 0
/// }
/// }
///
/// let dfa = dense::DFA::new("z[0-9]{3}")?;
/// let haystack = "foobar z123 q123".as_bytes();
/// // A scanner executes a prefilter while tracking some state that helps
/// // determine whether a prefilter is still "effective" or not.
/// let mut scanner = Scanner::new(&ZPrefilter);
///
/// let expected = Some(HalfMatch::must(0, 11));
/// let got = dfa.find_earliest_fwd_at(
/// Some(&mut scanner),
/// None,
/// haystack,
/// 0,
/// haystack.len(),
/// )?;
/// assert_eq!(expected, got);
///
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
///
/// # Example: specific pattern search
///
/// This example shows how to build a multi-DFA that permits searching for
/// specific patterns.
///
/// ```
/// use regex_automata::{
/// dfa::{Automaton, dense},
/// HalfMatch,
/// PatternID,
/// };
///
/// let dfa = dense::Builder::new()
/// .configure(dense::Config::new().starts_for_each_pattern(true))
/// .build_many(&["[a-z0-9]{6}", "[a-z][a-z0-9]{5}"])?;
/// let haystack = "foo123".as_bytes();
///
/// // Since we are using the default leftmost-first match and both
/// // patterns match at the same starting position, only the first pattern
/// // will be returned in this case when doing a search for any of the
/// // patterns.
/// let expected = Some(HalfMatch::must(0, 6));
/// let got = dfa.find_earliest_fwd_at(
/// None,
/// None,
/// haystack,
/// 0,
/// haystack.len(),
/// )?;
/// assert_eq!(expected, got);
///
/// // But if we want to check whether some other pattern matches, then we
/// // can provide its pattern ID.
/// let expected = Some(HalfMatch::must(1, 6));
/// let got = dfa.find_earliest_fwd_at(
/// None,
/// Some(PatternID::must(1)),
/// haystack,
/// 0,
/// haystack.len(),
/// )?;
/// assert_eq!(expected, got);
///
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
///
/// # Example: specifying the bounds of a search
///
/// This example shows how providing the bounds of a search can produce
/// different results than simply sub-slicing the haystack.
///
/// ```
/// use regex_automata::{
/// dfa::{Automaton, dense},
/// HalfMatch,
/// };
///
/// // N.B. We disable Unicode here so that we use a simple ASCII word
/// // boundary. Alternatively, we could enable heuristic support for
/// // Unicode word boundaries.
/// let dfa = dense::DFA::new(r"(?-u)\b[0-9]{3}\b")?;
/// let haystack = "foo123bar".as_bytes();
///
/// // Since we sub-slice the haystack, the search doesn't know about the
/// // larger context and assumes that `123` is surrounded by word
/// // boundaries. And of course, the match position is reported relative
/// // to the sub-slice as well, which means we get `3` instead of `6`.
/// let expected = Some(HalfMatch::must(0, 3));
/// let got = dfa.find_earliest_fwd_at(
/// None,
/// None,
/// &haystack[3..6],
/// 0,
/// haystack[3..6].len(),
/// )?;
/// assert_eq!(expected, got);
///
/// // But if we provide the bounds of the search within the context of the
/// // entire haystack, then the search can take the surrounding context
/// // into account. (And if we did find a match, it would be reported
/// // as a valid offset into `haystack` instead of its sub-slice.)
/// let expected = None;
/// let got = dfa.find_earliest_fwd_at(
/// None,
/// None,
/// haystack,
/// 3,
/// 6,
/// )?;
/// assert_eq!(expected, got);
///
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
#[inline]
fn find_earliest_fwd_at(
&self,
pre: Option<&mut prefilter::Scanner>,
pattern_id: Option<PatternID>,
bytes: &[u8],
start: usize,
end: usize,
) -> Result<Option<HalfMatch>, MatchError> {
search::find_earliest_fwd(pre, self, pattern_id, bytes, start, end)
}
/// Executes a reverse search and returns the start position of the first
/// match that is found as early as possible. If no match exists, then
/// `None` is returned.
///
/// This routine stops scanning input as soon as the search observes a
/// match state.
///
/// This is like [`Automaton::find_earliest_rev`], except it provides some
/// additional control over how the search is executed. See the
/// documentation of [`Automaton::find_earliest_fwd_at`] for more details
/// on the additional parameters along with examples of their usage.
///
/// # Errors
///
/// This routine only errors if the search could not complete. For
/// DFAs generated by this crate, this only occurs in a non-default
/// configuration where quit bytes are used or Unicode word boundaries are
/// heuristically enabled.
///
/// When a search cannot complete, callers cannot know whether a match
/// exists or not.
///
/// # Panics
///
/// This routine must panic if a `pattern_id` is given and the underlying
/// DFA does not support specific pattern searches.
///
/// It must also panic if the given haystack range is not valid.
#[inline]
fn find_earliest_rev_at(
&self,
pattern_id: Option<PatternID>,
bytes: &[u8],
start: usize,
end: usize,
) -> Result<Option<HalfMatch>, MatchError> {
search::find_earliest_rev(self, pattern_id, bytes, start, end)
}
/// Executes a forward search and returns the end position of the leftmost
/// match that is found. If no match exists, then `None` is returned.
///
/// This is like [`Automaton::find_leftmost_fwd`], except it provides some
/// additional control over how the search is executed. See the
/// documentation of [`Automaton::find_earliest_fwd_at`] for more details
/// on the additional parameters along with examples of their usage.
///
/// # Errors
///
/// This routine only errors if the search could not complete. For
/// DFAs generated by this crate, this only occurs in a non-default
/// configuration where quit bytes are used or Unicode word boundaries are
/// heuristically enabled.
///
/// When a search cannot complete, callers cannot know whether a match
/// exists or not.
///
/// # Panics
///
/// This routine must panic if a `pattern_id` is given and the underlying
/// DFA does not support specific pattern searches.
///
/// It must also panic if the given haystack range is not valid.
#[inline]
fn find_leftmost_fwd_at(
&self,
pre: Option<&mut prefilter::Scanner>,
pattern_id: Option<PatternID>,
bytes: &[u8],
start: usize,
end: usize,
) -> Result<Option<HalfMatch>, MatchError> {
search::find_leftmost_fwd(pre, self, pattern_id, bytes, start, end)
}
/// Executes a reverse search and returns the start of the position of the
/// leftmost match that is found. If no match exists, then `None` is
/// returned.
///
/// This is like [`Automaton::find_leftmost_rev`], except it provides some
/// additional control over how the search is executed. See the
/// documentation of [`Automaton::find_earliest_fwd_at`] for more details
/// on the additional parameters along with examples of their usage.
///
/// # Errors
///
/// This routine only errors if the search could not complete. For
/// DFAs generated by this crate, this only occurs in a non-default
/// configuration where quit bytes are used or Unicode word boundaries are
/// heuristically enabled.
///
/// When a search cannot complete, callers cannot know whether a match
/// exists or not.
///
/// # Panics
///
/// This routine must panic if a `pattern_id` is given and the underlying
/// DFA does not support specific pattern searches.
///
/// It must also panic if the given haystack range is not valid.
#[inline]
fn find_leftmost_rev_at(
&self,
pattern_id: Option<PatternID>,
bytes: &[u8],
start: usize,
end: usize,
) -> Result<Option<HalfMatch>, MatchError> {
search::find_leftmost_rev(self, pattern_id, bytes, start, end)
}
/// Executes an overlapping forward search and returns the end position of
/// matches as they are found. If no match exists, then `None` is returned.
///
/// This routine is principally only useful when searching for multiple
/// patterns on inputs where multiple patterns may match the same regions
/// of text. In particular, callers must preserve the automaton's search
/// state from prior calls so that the implementation knows where the last
/// match occurred.
///
/// This is like [`Automaton::find_overlapping_fwd`], except it provides
/// some additional control over how the search is executed. See the
/// documentation of [`Automaton::find_earliest_fwd_at`] for more details
/// on the additional parameters along with examples of their usage.
///
/// When using this routine to implement an iterator of overlapping
/// matches, the `start` of the search should always be set to the end
/// of the last match. If more patterns match at the previous location,
/// then they will be immediately returned. (This is tracked by the given
/// overlapping state.) Otherwise, the search continues at the starting
/// position given.
///
/// If for some reason you want the search to forget about its previous
/// state and restart the search at a particular position, then setting the
/// state to [`OverlappingState::start`] will accomplish that.
///
/// # Errors
///
/// This routine only errors if the search could not complete. For
/// DFAs generated by this crate, this only occurs in a non-default
/// configuration where quit bytes are used or Unicode word boundaries are
/// heuristically enabled.
///
/// When a search cannot complete, callers cannot know whether a match
/// exists or not.
///
/// # Panics
///
/// This routine must panic if a `pattern_id` is given and the underlying
/// DFA does not support specific pattern searches.
///
/// It must also panic if the given haystack range is not valid.
#[inline]
fn find_overlapping_fwd_at(
&self,
pre: Option<&mut prefilter::Scanner>,
pattern_id: Option<PatternID>,
bytes: &[u8],
start: usize,
end: usize,
state: &mut OverlappingState,
) -> Result<Option<HalfMatch>, MatchError> {
search::find_overlapping_fwd(
pre, self, pattern_id, bytes, start, end, state,
)
}
}
unsafe impl<'a, T: Automaton> Automaton for &'a T {
#[inline]
fn next_state(&self, current: StateID, input: u8) -> StateID {
(**self).next_state(current, input)
}
#[inline]
unsafe fn next_state_unchecked(
&self,
current: StateID,
input: u8,
) -> StateID {
(**self).next_state_unchecked(current, input)
}
#[inline]
fn next_eoi_state(&self, current: StateID) -> StateID {
(**self).next_eoi_state(current)
}
#[inline]
fn start_state_forward(
&self,
pattern_id: Option<PatternID>,
bytes: &[u8],
start: usize,
end: usize,
) -> StateID {
(**self).start_state_forward(pattern_id, bytes, start, end)
}
#[inline]
fn start_state_reverse(
&self,
pattern_id: Option<PatternID>,
bytes: &[u8],
start: usize,
end: usize,
) -> StateID {
(**self).start_state_reverse(pattern_id, bytes, start, end)
}
#[inline]
fn is_special_state(&self, id: StateID) -> bool {
(**self).is_special_state(id)
}
#[inline]
fn is_dead_state(&self, id: StateID) -> bool {
(**self).is_dead_state(id)
}
#[inline]
fn is_quit_state(&self, id: StateID) -> bool {
(**self).is_quit_state(id)
}
#[inline]
fn is_match_state(&self, id: StateID) -> bool {
(**self).is_match_state(id)
}
#[inline]
fn is_start_state(&self, id: StateID) -> bool {
(**self).is_start_state(id)
}
#[inline]
fn is_accel_state(&self, id: StateID) -> bool {
(**self).is_accel_state(id)
}
#[inline]
fn pattern_count(&self) -> usize {
(**self).pattern_count()
}
#[inline]
fn match_count(&self, id: StateID) -> usize {
(**self).match_count(id)
}
#[inline]
fn match_pattern(&self, id: StateID, index: usize) -> PatternID {
(**self).match_pattern(id, index)
}
#[inline]
fn accelerator(&self, id: StateID) -> &[u8] {
(**self).accelerator(id)
}
#[inline]
fn find_earliest_fwd(
&self,
bytes: &[u8],
) -> Result<Option<HalfMatch>, MatchError> {
(**self).find_earliest_fwd(bytes)
}
#[inline]
fn find_earliest_rev(
&self,
bytes: &[u8],
) -> Result<Option<HalfMatch>, MatchError> {
(**self).find_earliest_rev(bytes)
}
#[inline]
fn find_leftmost_fwd(
&self,
bytes: &[u8],
) -> Result<Option<HalfMatch>, MatchError> {
(**self).find_leftmost_fwd(bytes)
}
#[inline]
fn find_leftmost_rev(
&self,
bytes: &[u8],
) -> Result<Option<HalfMatch>, MatchError> {
(**self).find_leftmost_rev(bytes)
}
#[inline]
fn find_overlapping_fwd(
&self,
bytes: &[u8],
state: &mut OverlappingState,
) -> Result<Option<HalfMatch>, MatchError> {
(**self).find_overlapping_fwd(bytes, state)
}
#[inline]
fn find_earliest_fwd_at(
&self,
pre: Option<&mut prefilter::Scanner>,
pattern_id: Option<PatternID>,
bytes: &[u8],
start: usize,
end: usize,
) -> Result<Option<HalfMatch>, MatchError> {
(**self).find_earliest_fwd_at(pre, pattern_id, bytes, start, end)
}
#[inline]
fn find_earliest_rev_at(
&self,
pattern_id: Option<PatternID>,
bytes: &[u8],
start: usize,
end: usize,
) -> Result<Option<HalfMatch>, MatchError> {
(**self).find_earliest_rev_at(pattern_id, bytes, start, end)
}
#[inline]
fn find_leftmost_fwd_at(
&self,
pre: Option<&mut prefilter::Scanner>,
pattern_id: Option<PatternID>,
bytes: &[u8],
start: usize,
end: usize,
) -> Result<Option<HalfMatch>, MatchError> {
(**self).find_leftmost_fwd_at(pre, pattern_id, bytes, start, end)
}
#[inline]
fn find_leftmost_rev_at(
&self,
pattern_id: Option<PatternID>,
bytes: &[u8],
start: usize,
end: usize,
) -> Result<Option<HalfMatch>, MatchError> {
(**self).find_leftmost_rev_at(pattern_id, bytes, start, end)
}
#[inline]
fn find_overlapping_fwd_at(
&self,
pre: Option<&mut prefilter::Scanner>,
pattern_id: Option<PatternID>,
bytes: &[u8],
start: usize,
end: usize,
state: &mut OverlappingState,
) -> Result<Option<HalfMatch>, MatchError> {
(**self)
.find_overlapping_fwd_at(pre, pattern_id, bytes, start, end, state)
}
}
/// Represents the current state of an overlapping search.
///
/// This is used for overlapping searches since they need to know something
/// about the previous search. For example, when multiple patterns match at the
/// same position, this state tracks the last reported pattern so that the next
/// search knows whether to report another matching pattern or continue with
/// the search at the next position. Additionally, it also tracks which state
/// the last search call terminated in.
///
/// This type provides no introspection capabilities. The only thing a caller
/// can do is construct it and pass it around to permit search routines to use
/// it to track state.
///
/// Callers should always provide a fresh state constructed via
/// [`OverlappingState::start`] when starting a new search. Reusing state from
/// a previous search may result in incorrect results.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct OverlappingState {
/// The state ID of the state at which the search was in when the call
/// terminated. When this is a match state, `last_match` must be set to a
/// non-None value.
///
/// A `None` value indicates the start state of the corresponding
/// automaton. We cannot use the actual ID, since any one automaton may
/// have many start states, and which one is in use depends on several
/// search-time factors.
id: Option<StateID>,
/// Information associated with a match when `id` corresponds to a match
/// state.
last_match: Option<StateMatch>,
}
/// Internal state about the last match that occurred. This records both the
/// offset of the match and the match index.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) struct StateMatch {
/// The index into the matching patterns for the current match state.
pub(crate) match_index: usize,
/// The offset in the haystack at which the match occurred. This is used
/// when reporting multiple matches at the same offset. That is, when
/// an overlapping search runs, the first thing it checks is whether it's
/// already in a match state, and if so, whether there are more patterns
/// to report as matches in that state. If so, it increments `match_index`
/// and returns the pattern and this offset. Once `match_index` exceeds the
/// number of matching patterns in the current state, the search continues.
pub(crate) offset: usize,
}
impl OverlappingState {
/// Create a new overlapping state that begins at the start state of any
/// automaton.
pub fn start() -> OverlappingState {
OverlappingState { id: None, last_match: None }
}
pub(crate) fn id(&self) -> Option<StateID> {
self.id
}
pub(crate) fn set_id(&mut self, id: StateID) {
self.id = Some(id);
}
pub(crate) fn last_match(&mut self) -> Option<&mut StateMatch> {
self.last_match.as_mut()
}
pub(crate) fn set_last_match(&mut self, last_match: StateMatch) {
self.last_match = Some(last_match);
}
}
/// Write a prefix "state" indicator for fmt::Debug impls.
///
/// Specifically, this tries to succinctly distinguish the different types of
/// states: dead states, quit states, accelerated states, start states and
/// match states. It even accounts for the possible overlappings of different
/// state types.
pub(crate) fn fmt_state_indicator<A: Automaton>(
f: &mut core::fmt::Formatter<'_>,
dfa: A,
id: StateID,
) -> core::fmt::Result {
if dfa.is_dead_state(id) {
write!(f, "D")?;
if dfa.is_start_state(id) {
write!(f, ">")?;
} else {
write!(f, " ")?;
}
} else if dfa.is_quit_state(id) {
write!(f, "Q ")?;
} else if dfa.is_start_state(id) {
if dfa.is_accel_state(id) {
write!(f, "A>")?;
} else {
write!(f, " >")?;
}
} else if dfa.is_match_state(id) {
if dfa.is_accel_state(id) {
write!(f, "A*")?;
} else {
write!(f, " *")?;
}
} else if dfa.is_accel_state(id) {
write!(f, "A ")?;
} else {
write!(f, " ")?;
}
Ok(())
}