pub struct Builder { /* private fields */ }
Expand description
A builder for a bounded backtracker.
This builder permits configuring options for the syntax of a pattern, the
NFA construction and the BoundedBacktracker
construction. 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.
§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::{
nfa::thompson::{self, backtrack::BoundedBacktracker},
util::syntax,
Match,
};
let re = BoundedBacktracker::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(Ok(Match::must(0, 1..9)));
let got = re.try_find_iter(&mut cache, haystack).next();
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.
//
// N.B. This example does not show the impact of
// disabling UTF-8 mode on a BoundedBacktracker Config, since that
// only impacts regexes that can produce matches of
// length 0.
assert_eq!(b"foo\xFFarzz", &haystack[got.unwrap()?.range()]);
Implementations§
Source§impl Builder
impl Builder
Sourcepub fn new() -> Builder
pub fn new() -> Builder
Create a new BoundedBacktracker builder with its default configuration.
Sourcepub fn build(&self, pattern: &str) -> Result<BoundedBacktracker, BuildError>
pub fn build(&self, pattern: &str) -> Result<BoundedBacktracker, BuildError>
Build a BoundedBacktracker
from the given pattern.
If there was a problem parsing or compiling the pattern, then an error is returned.
Sourcepub fn build_many<P: AsRef<str>>(
&self,
patterns: &[P],
) -> Result<BoundedBacktracker, BuildError>
pub fn build_many<P: AsRef<str>>( &self, patterns: &[P], ) -> Result<BoundedBacktracker, BuildError>
Build a BoundedBacktracker
from the given patterns.
Sourcepub fn build_from_nfa(&self, nfa: NFA) -> Result<BoundedBacktracker, BuildError>
pub fn build_from_nfa(&self, nfa: NFA) -> Result<BoundedBacktracker, BuildError>
Build a BoundedBacktracker
directly from its NFA.
Note that when using this method, any configuration that applies to the construction of the NFA itself will of course be ignored, since the NFA given here is already built.
Sourcepub fn configure(&mut self, config: Config) -> &mut Builder
pub fn configure(&mut self, config: Config) -> &mut Builder
Apply the given BoundedBacktracker
configuration options to this
builder.
Sourcepub fn syntax(&mut self, config: Config) -> &mut Builder
pub fn syntax(&mut self, config: Config) -> &mut Builder
Set 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 BoundedBacktracker
directly from a pattern.
Sourcepub fn thompson(&mut self, config: Config) -> &mut Builder
pub fn thompson(&mut self, config: Config) -> &mut Builder
Set the Thompson NFA configuration for this builder using
nfa::thompson::Config
.
This permits setting things like if additional time should be spent shrinking the size of the NFA.
These settings only apply when constructing a BoundedBacktracker
directly from a pattern.