Enum Anchored
pub enum Anchored
The type of anchored search to perform.
This is almost a boolean option. That is, you can either do an unanchored search for any pattern in a regex, or you can do an anchored search for any pattern in a regex.
A third option exists that, assuming the regex engine supports it, permits you to do an anchored search for a specific pattern.
Note that there is no way to run an unanchored search for a specific pattern. If you need that, you'll need to build separate regexes for each pattern.
Errors
If a regex engine does not support the anchored mode selected, then the regex engine will return an error. While any non-trivial regex engine should support at least one of the available anchored modes, there is no singular mode that is guaranteed to be universally supported. Some regex engines might only support unanchored searches (DFAs compiled without anchored starting states) and some regex engines might only support anchored searches (like the one-pass DFA).
The specific error returned is a MatchError with a
MatchErrorKind::UnsupportedAnchored kind. The kind includes the
Anchored value given that is unsupported.
Note that regex engines should report "no match" if, for example, an
Anchored::Pattern is provided with an invalid pattern ID but where
anchored searches for a specific pattern are supported. This is smooths out
behavior such that it's possible to guarantee that an error never occurs
based on how the regex engine is configured. All regex engines in this
crate report "no match" when searching for an invalid pattern ID, but where
searching for a valid pattern ID is otherwise supported.
Example
This example shows how to use the various Anchored modes to run a
search. We use the PikeVM
because it supports all modes unconditionally. Some regex engines, like
the onepass::DFA cannot support unanchored
searches.
# if cfg! // miri takes too long
use ;
let re = new_many?;
let mut cache = re.create_cache;
let hay = "Hello Mr. Springsteen!";
// The default is to do an unanchored search.
assert_eq!;
// Explicitly ask for an unanchored search. Same as above.
let input = new.anchored;
assert_eq!;
// Now try an anchored search. Since the match doesn't start at the
// beginning of the haystack, no match is found!
let input = new.anchored;
assert_eq!;
// We can try an anchored search again, but move the location of where
// we start the search. Note that the offsets reported are still in
// terms of the overall haystack and not relative to where we started
// the search.
let input = new.anchored.range;
assert_eq!;
// Now try an anchored search for a specific pattern. We specifically
// choose a pattern that we know doesn't match to prove that the search
// only looks for the pattern we provide.
let input = new
.anchored
.range;
assert_eq!;
// But if we switch it to the pattern that we know matches, then we find
// the match.
let input = new
.anchored
.range;
assert_eq!;
# Ok::
Variants
-
No Run an unanchored search. This means a match may occur anywhere at or after the start position of the search.
This search can return a match for any pattern in the regex.
-
Yes Run an anchored search. This means that a match must begin at the start position of the search.
This search can return a match for any pattern in the regex.
-
Pattern(PatternID) Run an anchored search for a specific pattern. This means that a match must be for the given pattern and must begin at the start position of the search.
Implementations
impl Anchored
fn is_anchored(&self) -> boolReturns true if and only if this anchor mode corresponds to any kind of anchored search.
Example
This examples shows that both
Anchored::YesandAnchored::Patternare considered anchored searches.use ; assert!; assert!; assert!;fn pattern(&self) -> Option<PatternID>Returns the pattern ID associated with this configuration if it is an anchored search for a specific pattern. Otherwise
Noneis returned.Example
use ; assert_eq!; assert_eq!; let pid = must; assert_eq!;
Trait Implementations
impl Clone for Anchored
fn clone(&self) -> Self
impl Copy for Anchored
impl Debug for Anchored
fn fmt(&self, f: &mut Formatter<'_>) -> Result
impl Eq for Anchored
impl PartialEq for Anchored
fn eq(&self, other: &Self) -> bool
impl StructuralPartialEq for Anchored
Auto Trait Implementations
impl Freeze for Anchored
impl RefUnwindSafe for Anchored
impl Send for Anchored
impl Sync for Anchored
impl Unpin for Anchored
impl UnsafeUnpin for Anchored
impl UnwindSafe for Anchored
Blanket Implementations
impl<T> Any for Anchored
where
T: 'static + ?Sized,
fn type_id(&self) -> TypeId
impl<T> Borrow<T> for Anchored
where
T: ?Sized,
fn borrow(&self) -> &T
impl<T> BorrowMut<T> for Anchored
where
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
impl<T> CloneToUninit for Anchored
where
T: Clone,
unsafe fn clone_to_uninit(&self, dest: *mut u8)
impl<T> From<T> for Anchored
fn from(t: T) -> TReturns the argument unchanged.
impl<T> ToOwned for Anchored
where
T: Clone,
type Owned = T;fn to_owned(&self) -> Tfn clone_into(&self, target: &mut T)
impl<T, U> Into<U> for Anchored
where
U: From<T>,
fn into(self) -> UCalls
U::from(self).That is, this conversion is whatever the implementation of
[From]<T> for Uchooses to do.
impl<T, U> TryFrom<U> for Anchored
where
U: Into<T>,
type Error = never;fn try_from(value: U) -> Result<T, never>
impl<T, U> TryInto<U> for Anchored
where
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error;fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>