Builder

Struct Builder 

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

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

Implementations§

Source§

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

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::{lita::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>>(())
Source

pub fn hir_ascii(self, value: (Hir, bool)) -> Builder<'a, 'f1, SetHirAscii<S>>
where S::HirAscii: IsUnset,

Optional (Some / Option setters).

If the provided hir is Unicode-aware, providing a ASCII-aware-only Hir as hir_ascii can improve performance.

The second bool is whether the provided hir_ascii is case insensitive:

  • If it’s false but ib.case_insensitive is true, then hir_ascii will be converted to case insensitive. (Used by glob)
  • If it’s true but ib.case_insensitive is false, build() will panic.
Source

pub fn maybe_hir_ascii( self, value: Option<(Hir, bool)>, ) -> Builder<'a, 'f1, SetHirAscii<S>>
where S::HirAscii: IsUnset,

Optional (Some / Option setters).

If the provided hir is Unicode-aware, providing a ASCII-aware-only Hir as hir_ascii can improve performance.

The second bool is whether the provided hir_ascii is case insensitive:

  • If it’s false but ib.case_insensitive is true, then hir_ascii will be converted to case insensitive. (Used by glob)
  • If it’s true but ib.case_insensitive is false, build() will panic.
Source

pub fn dfa_dense(self, value: Config) -> Builder<'a, 'f1, SetDfaDense<S>>
where S::DfaDense: IsUnset,

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

Source

pub fn maybe_dfa_dense( self, value: Option<Config>, ) -> Builder<'a, 'f1, SetDfaDense<S>>
where S::DfaDense: IsUnset,

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

Source

pub fn thompson(self, value: Config) -> Builder<'a, 'f1, SetThompson<S>>
where S::Thompson: 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_thompson( self, value: Option<Config>, ) -> Builder<'a, 'f1, SetThompson<S>>
where S::Thompson: 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::lita::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::lita::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::{lita::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 build(self, pattern: &str) -> Result<Regex<'a>, BuildError>
where S::HirAscii: IsUnset,

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::{lita::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));

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.