pub struct IbMatcher<'a, HaystackStr = str>where
HaystackStr: EncodedStr + ?Sized,{ /* private fields */ }
Expand description
§Example
// cargo add ib-matcher --features pinyin,romaji
use ib_matcher::{
matcher::{IbMatcher, PinyinMatchConfig, RomajiMatchConfig},
pinyin::PinyinNotation,
};
let matcher = IbMatcher::builder("la vie est drôle").build();
assert!(matcher.is_match("LA VIE EST DRÔLE"));
let matcher = IbMatcher::builder("βίος").build();
assert!(matcher.is_match("Βίοσ"));
assert!(matcher.is_match("ΒΊΟΣ"));
let matcher = IbMatcher::builder("pysousuoeve")
.pinyin(PinyinMatchConfig::notations(
PinyinNotation::Ascii | PinyinNotation::AsciiFirstLetter,
))
.build();
assert!(matcher.is_match("拼音搜索Everything"));
let matcher = IbMatcher::builder("konosuba")
.romaji(RomajiMatchConfig::default())
.is_pattern_partial(true)
.build();
assert!(matcher.is_match("この素晴らしい世界に祝福を"));
§Design
API follows regex::Regex
.
§Performance
- If you need to build
IbMatcher
multiple times, passPinyinMatchConfigBuilder::data
to the builder or usePinyinMatchConfig::shallow_clone()
to avoid re-initializing the pinyin data every time. Same forRomajiMatchConfig
. - For matching more than 1000 strings, enable
IbMatcherBuilder::analyze
to optimize the pattern further. (The analysis costs ~65us, equivalent to about 220~1100 matches.) - If you only need to call
IbMatcher::test
(orIbMatcher::test_and_try_for_each
), setstarts_with
to improve performance.
TODO: No-pinyin pattern optimization
TODO: Anchors, *_at
TODO: Unicode normalization
TODO: No-hanzi haystack optimization (0.2/0.9%)
TODO: If pattern doesn’t contain .
, only match before .
in the haystack
Implementations§
Source§impl<'a, HaystackStr> IbMatcher<'a, HaystackStr>where
HaystackStr: EncodedStr + ?Sized,
impl<'a, HaystackStr> IbMatcher<'a, HaystackStr>where
HaystackStr: EncodedStr + ?Sized,
pub fn with_config<'p>(
pattern: impl Into<Pattern<'p, HaystackStr>>,
config: MatchConfig<'a>,
) -> Selfwhere
HaystackStr: 'p,
Sourcepub fn find<'h>(
&'a self,
input: impl Into<Input<'h, HaystackStr>>,
) -> Option<Match>where
HaystackStr: 'h,
pub fn find<'h>(
&'a self,
input: impl Into<Input<'h, HaystackStr>>,
) -> Option<Match>where
HaystackStr: 'h,
This routine searches for the first match of this pattern in the haystack given, and if found, returns a Match
. The Match
provides access to both the byte offsets of the match and Match::is_pattern_partial()
.
Note that this should only be used if you want to find the entire match. If instead you just want to test the existence of a match, it’s potentially faster to use IbMatcher::is_match()
instead of IbMatcher::find().is_some()
.
Sourcepub fn is_match<'h>(&self, input: impl Into<Input<'h, HaystackStr>>) -> boolwhere
HaystackStr: 'h,
pub fn is_match<'h>(&self, input: impl Into<Input<'h, HaystackStr>>) -> boolwhere
HaystackStr: 'h,
Returns true if and only if there is a match for the pattern anywhere in the haystack given.
It is recommended to use this method if all you need to do is test whether a match exists, since the underlying matching engine may be able to do less work.
Sourcepub fn test<'h>(
&self,
input: impl Into<Input<'h, HaystackStr>>,
) -> Option<Match>where
HaystackStr: 'h,
pub fn test<'h>(
&self,
input: impl Into<Input<'h, HaystackStr>>,
) -> Option<Match>where
HaystackStr: 'h,
This routine tests if this pattern matches the haystack at the start, and if found, returns a Match
. The Match
provides access to both the byte offsets of the match and Match::is_pattern_partial()
.
§Returns
Match.start()
is guaranteed to be 0.- If there are multiple possible matches, the longer ones are preferred. But the result is not guaranteed to be the longest one.
Sourcepub fn test_and_try_for_each<'h, T>(
&self,
input: impl Into<Input<'h, HaystackStr>>,
f: &mut impl FnMut(Match) -> Option<T>,
) -> Option<T>where
HaystackStr: 'h,
pub fn test_and_try_for_each<'h, T>(
&self,
input: impl Into<Input<'h, HaystackStr>>,
f: &mut impl FnMut(Match) -> Option<T>,
) -> Option<T>where
HaystackStr: 'h,
This routine tests if this pattern matches the haystack at the start, and if found, calls f
, and returns a [T
] if it returns Some
.
§Arguments
f
: TheMatch
provides access to both the byte offsets of the match andMatch::is_pattern_partial()
.Match.start()
is guaranteed to be 0.
Sourcepub fn is_haystack_too_short(&self, haystack: &HaystackStr) -> bool
pub fn is_haystack_too_short(&self, haystack: &HaystackStr) -> bool
Already tested in match methods.
pub fn builder<'p>( pattern: impl Into<Pattern<'p, HaystackStr>>, ) -> IbMatcherBuilder<'a, 'p, HaystackStr>
Trait Implementations§
Auto Trait Implementations§
impl<'a, HaystackStr = str> !Freeze for IbMatcher<'a, HaystackStr>
impl<'a, HaystackStr> RefUnwindSafe for IbMatcher<'a, HaystackStr>where
HaystackStr: RefUnwindSafe + ?Sized,
impl<'a, HaystackStr> Send for IbMatcher<'a, HaystackStr>
impl<'a, HaystackStr> Sync for IbMatcher<'a, HaystackStr>
impl<'a, HaystackStr> Unpin for IbMatcher<'a, HaystackStr>
impl<'a, HaystackStr> UnwindSafe for IbMatcher<'a, HaystackStr>where
HaystackStr: UnwindSafe + ?Sized,
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