pub struct DFA { /* private fields */ }
Expand description

A DFA implementation of Aho-Corasick.

When possible, prefer using AhoCorasick instead of this type directly. Using a DFA directly is typically only necessary when one needs access to the Automaton trait implementation.

This DFA can only be built by first constructing a noncontiguous::NFA. Both DFA::new and Builder::build do this for you automatically, but Builder::build_from_noncontiguous permits doing it explicitly.

A DFA provides the best possible search performance (in this crate) via two mechanisms:

  • All states use a dense representation for their transitions.
  • All failure transitions are pre-computed such that they are never explicitly handled at search time.

These two facts combined mean that every state transition is performed using a constant number of instructions. However, this comes at great cost. The memory usage of a DFA can be quite exorbitant. It is potentially multiple orders of magnitude greater than a contiguous::NFA for example. In exchange, a DFA will typically have better search speed than a contiguous::NFA, but not by orders of magnitude.

Unless you have a small number of patterns or memory usage is not a concern and search performance is critical, a DFA is usually not the best choice.

Moreover, unlike the NFAs in this crate, it is costly for a DFA to support for anchored and unanchored search configurations. Namely, since failure transitions are pre-computed, supporting both anchored and unanchored searches requires a duplication of the transition table, making the memory usage of such a DFA ever bigger. (The NFAs in this crate unconditionally support both anchored and unanchored searches because there is essentially no added cost for doing so.) It is for this reason that a DFA’s support for anchored and unanchored searches can be configured via Builder::start_kind. By default, a DFA only supports unanchored searches.

Example

This example shows how to build an DFA directly and use it to execute Automaton::try_find:

use aho_corasick::{
    automaton::Automaton,
    dfa::DFA,
    Input, Match,
};

let patterns = &["b", "abc", "abcd"];
let haystack = "abcd";

let nfa = DFA::new(patterns).unwrap();
assert_eq!(
    Some(Match::must(0, 1..2)),
    nfa.try_find(&Input::new(haystack))?,
);

It is also possible to implement your own version of try_find. See the Automaton documentation for an example.

Implementations

Create a new Aho-Corasick DFA using the default configuration.

Use a Builder if you want to change the configuration.

A convenience method for returning a new Aho-Corasick DFA builder.

This usually permits one to just import the DFA type.

Trait Implementations

Returns the starting state for the given anchor mode. Read more
Performs a state transition from sid for byte and returns the next state. Read more
Returns true if the given ID represents a “special” state. A special state is a dead, match or start state. Read more
Returns true if the given ID represents a dead state. Read more
Returns true if the given ID represents a match state. Read more
Returns true if the given ID represents a start state. Read more
Returns the match semantics that this automaton was built with.
Returns the total number of patterns compiled into this automaton.
Returns the length of the pattern for the given ID. Read more
Returns the length, in bytes, of the shortest pattern in this automaton. Read more
Returns the length, in bytes, of the longest pattern in this automaton.
Returns the total number of matches for the given state ID. Read more
Returns the pattern ID for the match state given by sid at the index given. Read more
Returns the heap memory usage, in bytes, used by this automaton.
Returns a prefilter, if available, that can be used to accelerate searches for this automaton. Read more
Executes a non-overlapping search with this automaton using the given configuration. Read more
Executes a overlapping search with this automaton using the given configuration. Read more
Returns an iterator of non-overlapping matches with this automaton using the given configuration. Read more
Returns an iterator of overlapping matches with this automaton using the given configuration. Read more
Replaces all non-overlapping matches in haystack with strings from replace_with depending on the pattern that matched. The replace_with slice must have length equal to Automaton::patterns_len. Read more
Replaces all non-overlapping matches in haystack with strings from replace_with depending on the pattern that matched. The replace_with slice must have length equal to Automaton::patterns_len. Read more
Replaces all non-overlapping matches in haystack by calling the replace_with closure given. Read more
Replaces all non-overlapping matches in haystack by calling the replace_with closure given. Read more
Returns an iterator of non-overlapping matches with this automaton from the stream given. Read more
Replaces all non-overlapping matches in rdr with strings from replace_with depending on the pattern that matched, and writes the result to wtr. The replace_with slice must have length equal to Automaton::patterns_len. Read more
Replaces all non-overlapping matches in rdr by calling the replace_with closure given and writing the result to wtr. Read more
Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.