pub struct Builder<'a, 'f1, S: State = Empty> { /* private fields */ }
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>
impl<'a, 'f1, S: State> Builder<'a, 'f1, S>
Sourcepub fn build_many_from_hir(
self,
hirs: Vec<Hir>,
) -> Result<Regex<'a>, BuildError>where
S: IsComplete,
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>>(())
Sourcepub fn configure(self, value: Config) -> Builder<'a, 'f1, SetConfigure<S>>where
S::Configure: IsUnset,
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.
Sourcepub fn maybe_configure(
self,
value: Option<Config>,
) -> Builder<'a, 'f1, SetConfigure<S>>where
S::Configure: IsUnset,
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.
Sourcepub fn ib(self, value: MatchConfig<'a>) -> Builder<'a, 'f1, SetIb<S>>where
S::Ib: IsUnset,
pub fn ib(self, value: MatchConfig<'a>) -> Builder<'a, 'f1, SetIb<S>>where
S::Ib: IsUnset,
Sourcepub fn maybe_ib(
self,
value: Option<MatchConfig<'a>>,
) -> Builder<'a, 'f1, SetIb<S>>where
S::Ib: IsUnset,
pub fn maybe_ib(
self,
value: Option<MatchConfig<'a>>,
) -> Builder<'a, 'f1, SetIb<S>>where
S::Ib: IsUnset,
Sourcepub fn ib_parser(
self,
value: &'f1 mut dyn FnMut(&str) -> Pattern<'_, str>,
) -> Builder<'a, 'f1, SetIbParser<S>>where
S::IbParser: IsUnset,
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.
Sourcepub fn maybe_ib_parser(
self,
value: Option<&'f1 mut dyn FnMut(&str) -> Pattern<'_, str>>,
) -> Builder<'a, 'f1, SetIbParser<S>>where
S::IbParser: IsUnset,
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§impl<'a, S: State> Builder<'a, '_, S>
impl<'a, S: State> Builder<'a, '_, S>
Sourcepub fn syntax(self, syntax: Config) -> Self
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>>(())
Sourcepub 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.
pub fn callback( self, literal: impl Into<String>, callback: impl Fn(&Input<'_>, usize, &mut dyn FnMut(usize)) + 'static, ) -> Self
regex-callback
only.Add a custom matching callback.
Sourcepub fn build(self, pattern: &str) -> Result<Regex<'a>, BuildError>where
S: IsComplete,
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));
Sourcepub fn build_many<P: AsRef<str>>(
self,
patterns: &[P],
) -> Result<Regex<'a>, BuildError>where
S: IsComplete,
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(""));
Sourcepub fn build_from_hir(self, hir: Hir) -> Result<Regex<'a>, BuildError>where
S: IsComplete,
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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