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

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:

  1. It provides a few different build routines for actually constructing a DFA from different kinds of inputs. The most convenient is Builder::build, which builds a DFA directly from a pattern string. The most flexible is Builder::build_from_nfa, which builds a DFA straight from an NFA.
  2. The builder permits configuring a number of things. Builder::configure is used with Config to configure aspects of the DFA and the construction process itself. Builder::syntax and Builder::thompson permit 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 \b are no longer Unicode-aware. \w and \b are 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 for a are permitted.
  • Unanchored patterns can search through invalid UTF-8. That is, for unanchored patterns, the implicit prefix is (?s-u:.)*? instead of (?s:.)*?.
use regex_automata::{
    hybrid::dfa::DFA,
    nfa::thompson,
    HalfMatch, SyntaxConfig,
};

let dfa = DFA::builder()
    .configure(DFA::config().cache_capacity(5_000))
    .syntax(SyntaxConfig::new().unicode(false).utf8(false))
    .thompson(thompson::Config::new().utf8(false))
    .build(r"foo[^b]ar.*")?;
let mut cache = dfa.create_cache();

let haystack = b"\xFEfoo\xFFar\xE2\x98\xFF\n";
let expected = Some(HalfMatch::must(0, 10));
let got = dfa.find_leftmost_fwd(&mut cache, haystack)?;
assert_eq!(expected, got);

Implementations

Create a new lazy DFA builder with the default configuration.

Build a lazy DFA from the given pattern.

If there was a problem parsing or compiling the pattern, then an error is returned.

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.

Build a DFA from the given NFA.

Note that this requires an Arc<thompson::NFA> instead of a &thompson::NFA because the lazy DFA builds itself from the NFA at search time. This means that the lazy DFA must hold on to its source NFA for the entirety of its lifetime. An Arc is used so that callers aren’t forced to clone the NFA if it is needed elsewhere.

Example

This example shows how to build a lazy DFA if you already have an NFA in hand.

use std::sync::Arc;
use regex_automata::{hybrid::dfa::DFA, nfa::thompson, HalfMatch};

let haystack = "foo123bar".as_bytes();

// This shows how to set non-default options for building an NFA.
let nfa = thompson::Builder::new()
    .configure(thompson::Config::new().shrink(false))
    .build(r"[0-9]+")?;
let dfa = DFA::builder().build_from_nfa(Arc::new(nfa))?;
let mut cache = dfa.create_cache();
let expected = Some(HalfMatch::must(0, 6));
let got = dfa.find_leftmost_fwd(&mut cache, haystack)?;
assert_eq!(expected, got);

Apply the given lazy DFA configuration options to this builder.

Set the syntax configuration for this builder using SyntaxConfig.

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.

Set 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.

Trait Implementations

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

🔬 This is a nightly-only experimental API. (toowned_clone_into)

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.