Struct Builder
struct Builder { ... }
A builder for constructing a lazy deterministic finite automaton from regular expressions.
As a convenience, DFA::builder is an alias for Builder::new. The
advantage of the former is that it often lets you avoid importing the
Builder type directly.
This builder provides two main things:
- It provides a few different
buildroutines for actually constructing a DFA from different kinds of inputs. The most convenient isBuilder::build, which builds a DFA directly from a pattern string. The most flexible isBuilder::build_from_nfa, which builds a DFA straight from an NFA. - The builder permits configuring a number of things.
Builder::configureis used withConfigto configure aspects of the DFA and the construction process itself.Builder::syntaxandBuilder::thompsonpermit configuring the regex parser and Thompson NFA construction, respectively. The syntax and thompson configurations only apply when building from a pattern string.
This builder always constructs a single lazy DFA. As such, this builder
can only be used to construct regexes that either detect the presence
of a match or find the end location of a match. A single DFA cannot
produce both the start and end of a match. For that information, use a
Regex, which can be similarly configured
using regex::Builder. The main reason
to use a DFA directly is if the end location of a match is enough for your
use case. Namely, a Regex will construct two lazy DFAs instead of one,
since a second reverse DFA is needed to find the start of a match.
Example
This example shows how to build a lazy DFA that uses a tiny cache capacity and completely disables Unicode. That is:
- Things such as
\w,.and\bare no longer Unicode-aware.\wand\bare ASCII-only while.matches any byte except for\n(instead of any UTF-8 encoding of a Unicode scalar value except for\n). Things that are Unicode only, such as\pL, are not allowed. - The pattern itself is permitted to match invalid UTF-8. For example,
things like
[^a]that match any byte except foraare permitted.
use ;
let dfa = DFAbuilder
.configure
.thompson
.syntax
.build?;
let mut cache = dfa.create_cache;
let haystack = b"\xFEfoo\xFFar\xE2\x98\xFF\n";
let expected = Some;
let got = dfa.try_search_fwd?;
assert_eq!;
# Ok::
Implementations
impl Builder
fn new() -> BuilderCreate a new lazy DFA builder with the default configuration.
fn build(self: &Self, pattern: &str) -> Result<DFA, BuildError>Build a lazy DFA from the given pattern.
If there was a problem parsing or compiling the pattern, then an error is returned.
fn build_many<P: AsRef<str>>(self: &Self, patterns: &[P]) -> Result<DFA, BuildError>Build a lazy DFA from the given patterns.
When matches are returned, the pattern ID corresponds to the index of the pattern in the slice given.
fn build_from_nfa(self: &Self, nfa: NFA) -> Result<DFA, BuildError>Build a DFA from the given NFA.
Note that this requires owning a
thompson::NFA. While this may force you to clone the NFA, such a clone is not a deep clone. Namely, NFAs are defined internally to support shared ownership such that cloning is very cheap.Example
This example shows how to build a lazy DFA if you already have an NFA in hand.
use ; let haystack = "foo123bar"; // This shows how to set non-default options for building an NFA. let nfa = new .configure .build?; let dfa = DFAbuilder.build_from_nfa?; let mut cache = dfa.create_cache; let expected = Some; let got = dfa.try_search_fwd?; assert_eq!; # Ok::fn configure(self: &mut Self, config: Config) -> &mut BuilderApply the given lazy DFA configuration options to this builder.
fn syntax(self: &mut Self, config: Config) -> &mut BuilderSet the syntax configuration for this builder using
syntax::Config.This permits setting things like case insensitivity, Unicode and multi line mode.
These settings only apply when constructing a lazy DFA directly from a pattern.
fn thompson(self: &mut Self, config: Config) -> &mut BuilderSet the Thompson NFA configuration for this builder using
nfa::thompson::Config.This permits setting things like whether the DFA should match the regex in reverse or if additional time should be spent shrinking the size of the NFA.
These settings only apply when constructing a DFA directly from a pattern.
impl Clone for Builder
fn clone(self: &Self) -> Builder
impl Debug for Builder
fn fmt(self: &Self, f: &mut Formatter<'_>) -> Result
impl Freeze for Builder
impl RefUnwindSafe for Builder
impl Send for Builder
impl Sync for Builder
impl Unpin for Builder
impl UnsafeUnpin for Builder
impl UnwindSafe for Builder
impl<T> Any for Builder
fn type_id(self: &Self) -> TypeId
impl<T> Borrow for Builder
fn borrow(self: &Self) -> &T
impl<T> BorrowMut for Builder
fn borrow_mut(self: &mut Self) -> &mut T
impl<T> CloneToUninit for Builder
unsafe fn clone_to_uninit(self: &Self, dest: *mut u8)
impl<T> From for Builder
fn from(t: T) -> TReturns the argument unchanged.
impl<T> ToOwned for Builder
fn to_owned(self: &Self) -> Tfn clone_into(self: &Self, target: &mut T)
impl<T, U> Into for Builder
fn into(self: Self) -> UCalls
U::from(self).That is, this conversion is whatever the implementation of
[From]<T> for Uchooses to do.
impl<T, U> TryFrom for Builder
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
impl<T, U> TryInto for Builder
fn try_into(self: Self) -> Result<U, <U as TryFrom<T>>::Error>