regex_automata::hybrid::regex

Struct Builder

Source
pub struct Builder { /* private fields */ }
Available on crate feature hybrid only.
Expand description

A builder for a regex based on a hybrid NFA/DFA.

This builder permits configuring options for the syntax of a pattern, the NFA construction, the lazy DFA construction and finally the regex searching itself. This builder is different from a general purpose regex builder in that it permits fine grain configuration of the construction process. The trade off for this is complexity, and the possibility of setting a configuration that might not make sense. For example, there are two different UTF-8 modes:

  • syntax::Config::utf8 controls whether the pattern itself can contain sub-expressions that match invalid UTF-8.
  • thompson::Config::utf8 controls how the regex iterators themselves advance the starting position of the next search when a match with zero length is found.

Generally speaking, callers will want to either enable all of these or disable all of these.

Internally, building a regex requires building two hybrid NFA/DFAs, where one is responsible for finding the end of a match and the other is responsible for finding the start of a match. If you only need to detect whether something matched, or only the end of a match, then you should use a dfa::Builder to construct a single hybrid NFA/DFA, which is cheaper than building two of them.

§Example

This example shows how to disable UTF-8 mode in the syntax and the regex itself. This is generally what you want for matching on arbitrary bytes.

use regex_automata::{
    hybrid::regex::Regex, nfa::thompson, util::syntax, Match,
};

let re = Regex::builder()
    .syntax(syntax::Config::new().utf8(false))
    .thompson(thompson::Config::new().utf8(false))
    .build(r"foo(?-u:[^b])ar.*")?;
let mut cache = re.create_cache();

let haystack = b"\xFEfoo\xFFarzz\xE2\x98\xFF\n";
let expected = Some(Match::must(0, 1..9));
let got = re.find(&mut cache, haystack);
assert_eq!(expected, got);
// Notice that `(?-u:[^b])` matches invalid UTF-8,
// but the subsequent `.*` does not! Disabling UTF-8
// on the syntax permits this.
assert_eq!(b"foo\xFFarzz", &haystack[got.unwrap().range()]);

Implementations§

Source§

impl Builder

Source

pub fn new() -> Builder

Create a new regex builder with the default configuration.

Source

pub fn build(&self, pattern: &str) -> Result<Regex, BuildError>

Available on crate feature syntax only.

Build a regex from the given pattern.

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

Source

pub fn build_many<P: AsRef<str>>( &self, patterns: &[P], ) -> Result<Regex, BuildError>

Available on crate feature syntax only.

Build a regex from the given patterns.

Source

pub fn build_from_dfas(&self, forward: DFA, reverse: DFA) -> Regex

Build a regex from its component forward and reverse hybrid NFA/DFAs.

This is useful when you’ve built a forward and reverse lazy DFA separately, and want to combine them into a single regex. Once build, the individual DFAs given can still be accessed via Regex::forward and Regex::reverse.

It is important that the reverse lazy DFA be compiled under the following conditions:

  • It should use MatchKind::All semantics.
  • It should match in reverse.
  • Otherwise, its configuration should match the forward DFA.

If these conditions aren’t satisfied, then the behavior of searches is unspecified.

Note that when using this constructor, no configuration is applied. Since this routine provides the DFAs to the builder, there is no opportunity to apply other configuration options.

§Example

This shows how to build individual lazy forward and reverse DFAs, and then combine them into a single Regex.

use regex_automata::{
    hybrid::{dfa::DFA, regex::Regex},
    nfa::thompson,
    MatchKind,
};

let fwd = DFA::new(r"foo[0-9]+")?;
let rev = DFA::builder()
    .configure(DFA::config().match_kind(MatchKind::All))
    .thompson(thompson::Config::new().reverse(true))
    .build(r"foo[0-9]+")?;

let re = Regex::builder().build_from_dfas(fwd, rev);
let mut cache = re.create_cache();
assert_eq!(true, re.is_match(&mut cache, "foo123"));
Source

pub fn syntax(&mut self, config: Config) -> &mut Builder

Available on crate feature syntax only.

Set the syntax configuration for this builder using syntax::Config.

This permits setting things like case insensitivity, Unicode and multi line mode.

Source

pub fn thompson(&mut self, config: Config) -> &mut Builder

Available on crate feature syntax only.

Set the Thompson NFA configuration for this builder using nfa::thompson::Config.

This permits setting things like whether additional time should be spent shrinking the size of the NFA.

Source

pub fn dfa(&mut self, config: Config) -> &mut Builder

Set the lazy DFA compilation configuration for this builder using dfa::Config.

This permits setting things like whether Unicode word boundaries should be heuristically supported or settings how the behavior of the cache.

Trait Implementations§

Source§

impl Clone for Builder

Source§

fn clone(&self) -> Builder

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Builder

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Builder

Source§

fn default() -> Builder

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.