Builder

Struct Builder 

Source
pub struct Builder<'a, 'f1, S: State = Empty> { /* private fields */ }
Available on crate features regex-automata and regex-cp only.
Expand description

Use builder syntax to set the inputs and finish with build_many_from_hir().

Implementations§

Source§

impl<'a, 'f1, S: State> Builder<'a, 'f1, S>

Source

pub fn build_many_from_hir( self, hirs: Vec<Hir>, ) -> Result<Regex<'a>, BuildError>
where S: IsComplete,

Builds a Regex directly from many Hir expressions.

This is useful if you needed to parse pattern strings into Hir expressions for other reasons (such as analysis or transformations). This routine permits building a Regex directly from the Hir expressions instead of first converting the Hir expressions back to pattern strings.

When using this method, any options set via Builder::syntax are ignored. Namely, the syntax options only apply when parsing a pattern string, which isn’t relevant here.

If there was a problem building the underlying regex matcher for the given Hir expressions, then an error is returned.

Note that unlike Builder::build_many, this can only fail as a result of building the underlying matcher. In that case, there is no single Hir expression that can be isolated as a reason for the failure. So if this routine fails, it’s not possible to determine which Hir expression caused the failure.

§Example

This example shows how one can hand-construct multiple Hir expressions and build a single regex from them without doing any parsing at all.

use ib_matcher::{
    regex::{cp::Regex, Match},
    syntax::regex::hir::{Hir, Look},
};

// (?Rm)^foo$
let hir1 = Hir::concat(vec![
    Hir::look(Look::StartCRLF),
    Hir::literal("foo".as_bytes()),
    Hir::look(Look::EndCRLF),
]);
// (?Rm)^bar$
let hir2 = Hir::concat(vec![
    Hir::look(Look::StartCRLF),
    Hir::literal("bar".as_bytes()),
    Hir::look(Look::EndCRLF),
]);
let re = Regex::builder()
    .build_many_from_hir(vec![hir1, hir2])?;
let hay = "\r\nfoo\r\nbar";
let got: Vec<Match> = re.find_iter(hay).collect();
let expected = vec![
    Match::must(0, 2..5),
    Match::must(1, 7..10),
];
assert_eq!(expected, got);

Ok::<(), Box<dyn std::error::Error>>(())
Source

pub fn configure(self, value: Config) -> Builder<'a, 'f1, SetConfigure<S>>
where S::Configure: IsUnset,

Optional (Some / Option setters). Default: <thompson::Config as Default>::default().

Thompson NFA config. Named configure to be compatible with regex_automata::meta::Builder. Although some fields are not supported and utf8_empty is named as utf8 instead.

Source

pub fn maybe_configure( self, value: Option<Config>, ) -> Builder<'a, 'f1, SetConfigure<S>>
where S::Configure: IsUnset,

Optional (Some / Option setters). Default: <thompson::Config as Default>::default().

Thompson NFA config. Named configure to be compatible with regex_automata::meta::Builder. Although some fields are not supported and utf8_empty is named as utf8 instead.

Source

pub fn ib(self, value: MatchConfig<'a>) -> Builder<'a, 'f1, SetIb<S>>
where S::Ib: IsUnset,

Optional (Some / Option setters). Default: MatchConfig::builder().case_insensitive(false).build().

IbMatcher config.

Source

pub fn maybe_ib( self, value: Option<MatchConfig<'a>>, ) -> Builder<'a, 'f1, SetIb<S>>
where S::Ib: IsUnset,

Optional (Some / Option setters). Default: MatchConfig::builder().case_insensitive(false).build().

IbMatcher config.

Source

pub fn ib_parser( self, value: &'f1 mut dyn FnMut(&str) -> Pattern<'_, str>, ) -> Builder<'a, 'f1, SetIbParser<S>>
where S::IbParser: IsUnset,

Optional (Some / Option setters).

IbMatcher pattern parser.

§Example
use ib_matcher::{regex::cp::Regex, matcher::{MatchConfig, pattern::Pattern}};

let re = Regex::builder()
    .ib(MatchConfig::builder().pinyin(Default::default()).build())
    .ib_parser(&mut |pattern| Pattern::parse_ev(pattern).call())
    .build("pinyin;py")
    .unwrap();
assert!(re.is_match("拼音搜索"));
assert!(re.is_match("pinyin") == false);

See crate::syntax::ev for more details.

Source

pub fn maybe_ib_parser( self, value: Option<&'f1 mut dyn FnMut(&str) -> Pattern<'_, str>>, ) -> Builder<'a, 'f1, SetIbParser<S>>
where S::IbParser: IsUnset,

Optional (Some / Option setters).

IbMatcher pattern parser.

§Example
use ib_matcher::{regex::cp::Regex, matcher::{MatchConfig, pattern::Pattern}};

let re = Regex::builder()
    .ib(MatchConfig::builder().pinyin(Default::default()).build())
    .ib_parser(&mut |pattern| Pattern::parse_ev(pattern).call())
    .build("pinyin;py")
    .unwrap();
assert!(re.is_match("拼音搜索"));
assert!(re.is_match("pinyin") == false);

See crate::syntax::ev for more details.

Source

pub fn backtrack(self, value: Config) -> Builder<'a, 'f1, SetBacktrack<S>>
where S::Backtrack: IsUnset,

Optional (Some / Option setters). Default: backtrack::Config::new().visited_capacity(usize::MAX / 8).

Source

pub fn maybe_backtrack( self, value: Option<Config>, ) -> Builder<'a, 'f1, SetBacktrack<S>>
where S::Backtrack: IsUnset,

Optional (Some / Option setters). Default: backtrack::Config::new().visited_capacity(usize::MAX / 8).

Source§

impl<'a, S: State> Builder<'a, '_, S>

Source

pub fn syntax(self, syntax: Config) -> Self

Configure the syntax options when parsing a pattern string while building a Regex.

These options only apply when Builder::build or Builder::build_many are used. The other build methods accept Hir values, which have already been parsed.

§Example

This example shows how to enable case insensitive mode.

use ib_matcher::regex::{cp::Regex, util::syntax, Match};

let re = Regex::builder()
    .syntax(syntax::Config::new().case_insensitive(true))
    .build(r"δ")?;
assert_eq!(Some(Match::must(0, 0..2)), re.find(r"Δ"));

Ok::<(), Box<dyn std::error::Error>>(())
Source

pub fn callback( self, literal: impl Into<String>, callback: impl Fn(&Input<'_>, usize, &mut dyn FnMut(usize)) + 'static, ) -> Self

Available on crate feature regex-callback only.
Source

pub fn build(self, pattern: &str) -> Result<Regex<'a>, BuildError>
where S: IsComplete,

Builds a Regex from a single pattern string.

If there was a problem parsing the pattern or a problem turning it into a regex matcher, then an error is returned.

§Example

This example shows how to configure syntax options.

use ib_matcher::regex::{cp::Regex, util::syntax, Match};

let re = Regex::builder()
    .syntax(syntax::Config::new().crlf(true).multi_line(true))
    .build(r"^foo$")?;
let hay = "\r\nfoo\r\n";
assert_eq!(Some(Match::must(0, 2..5)), re.find(hay));
Source

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

Builds a Regex from many pattern strings.

If there was a problem parsing any of the patterns or a problem turning them into a regex matcher, then an error is returned.

§Example: zero patterns is valid

Building a regex with zero patterns results in a regex that never matches anything. Because this routine is generic, passing an empty slice usually requires a turbo-fish (or something else to help type inference).

use ib_matcher::regex::{cp::Regex, util::syntax, Match};

let re = Regex::builder()
    .build_many::<&str>(&[])?;
assert_eq!(None, re.find(""));
Source

pub fn build_from_hir(self, hir: Hir) -> Result<Regex<'a>, BuildError>
where S: IsComplete,

Builds a Regex directly from an Hir expression.

This is useful if you needed to parse a pattern string into an Hir for other reasons (such as analysis or transformations). This routine permits building a Regex directly from the Hir expression instead of first converting the Hir back to a pattern string.

When using this method, any options set via Builder::syntax are ignored. Namely, the syntax options only apply when parsing a pattern string, which isn’t relevant here.

If there was a problem building the underlying regex matcher for the given Hir, then an error is returned.

§Example

This example shows how one can hand-construct an Hir expression and build a regex from it without doing any parsing at all.

use ib_matcher::{
    regex::{cp::Regex, Match},
    syntax::regex::hir::{Hir, Look},
};

// (?Rm)^foo$
let hir = Hir::concat(vec![
    Hir::look(Look::StartCRLF),
    Hir::literal("foo".as_bytes()),
    Hir::look(Look::EndCRLF),
]);
let re = Regex::builder()
    .build_from_hir(hir)?;
let hay = "\r\nfoo\r\n";
assert_eq!(Some(Match::must(0, 2..5)), re.find(hay));

Ok::<(), Box<dyn std::error::Error>>(())

Auto Trait Implementations§

§

impl<'a, 'f1, S = Empty> !Freeze for Builder<'a, 'f1, S>

§

impl<'a, 'f1, S = Empty> !RefUnwindSafe for Builder<'a, 'f1, S>

§

impl<'a, 'f1, S = Empty> !Send for Builder<'a, 'f1, S>

§

impl<'a, 'f1, S = Empty> !Sync for Builder<'a, 'f1, S>

§

impl<'a, 'f1, S> Unpin for Builder<'a, 'f1, S>

§

impl<'a, 'f1, S = Empty> !UnwindSafe for Builder<'a, 'f1, S>

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> 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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.