pub struct Pattern<S = usize, A = DenseDFA<Vec<S>, S>>where
S: StateID,
A: DFA<ID = S>,{ /* private fields */ }
Expand description
A compiled match pattern that can match multipe inputs, or return a
Matcher
that matches a single input.
Implementations§
Source§impl Pattern
impl Pattern
Sourcepub fn new(pattern: &str) -> Result<Self, Error>
pub fn new(pattern: &str) -> Result<Self, Error>
Returns a new Pattern
for the given regex, or an error if the regex
was invalid.
The returned Pattern
will match occurances of the pattern which start
at any in a byte or character stream — the pattern may be preceded by
any number of non-matching characters. Essentially, it will behave as
though the regular expression started with a .*?
, which enables a
match to appear anywhere. If this is not the desired behavior, use
Pattern::new_anchored
instead.
For example:
use matchers::Pattern;
// This pattern matches any number of `a`s followed by a `b`.
let pattern = Pattern::new("a+b").expect("regex is not invalid");
// Of course, the pattern matches an input where the entire sequence of
// characters matches the pattern:
assert!(pattern.display_matches(&"aaaaab"));
// And, since the pattern is unanchored, it will also match the
// sequence when it's followed by non-matching characters:
assert!(pattern.display_matches(&"hello world! aaaaab"));
Sourcepub fn new_anchored(pattern: &str) -> Result<Self, Error>
pub fn new_anchored(pattern: &str) -> Result<Self, Error>
Returns a new Pattern
anchored at the beginning of the input stream,
or an error if the regex was invalid.
The returned Pattern
will only match an occurence of the pattern in
an input sequence if the first character or byte in the input matches
the pattern. If this is not the desired behavior, use Pattern::new
instead.
For example:
use matchers::Pattern;
// This pattern matches any number of `a`s followed by a `b`.
let pattern = Pattern::new_anchored("a+b")
.expect("regex is not invalid");
// The pattern matches an input where the entire sequence of
// characters matches the pattern:
assert!(pattern.display_matches(&"aaaaab"));
// Since the pattern is anchored, it will *not* match an input that
// begins with non-matching characters:
assert!(!pattern.display_matches(&"hello world! aaaaab"));
// ...however, if we create a pattern beginning with `.*?`, it will:
let pattern2 = Pattern::new_anchored(".*?a+b")
.expect("regex is not invalid");
assert!(pattern2.display_matches(&"hello world! aaaaab"));
Source§impl<S, A> Pattern<S, A>where
S: StateID,
A: DFA<ID = S>,
Self: for<'a> ToMatcher<'a, S>,
impl<S, A> Pattern<S, A>where
S: StateID,
A: DFA<ID = S>,
Self: for<'a> ToMatcher<'a, S>,
Sourcepub fn matches(&self, s: &impl AsRef<str>) -> bool
pub fn matches(&self, s: &impl AsRef<str>) -> bool
Returns true
if this pattern matches the given string.
Sourcepub fn debug_matches(&self, d: &impl Debug) -> bool
pub fn debug_matches(&self, d: &impl Debug) -> bool
Returns true
if this pattern matches the formatted output of the given
type implementing fmt::Debug
.
For example:
use matchers::Pattern;
#[derive(Debug)]
pub struct Hello {
to: &'static str,
}
let pattern = Pattern::new(r#"Hello \{ to: "W[^"]*" \}"#).unwrap();
let hello_world = Hello { to: "World" };
assert!(pattern.debug_matches(&hello_world));
let hello_sf = Hello { to: "San Francisco" };
assert_eq!(pattern.debug_matches(&hello_sf), false);
let hello_washington = Hello { to: "Washington" };
assert!(pattern.debug_matches(&hello_washington));
Sourcepub fn display_matches(&self, d: &impl Display) -> bool
pub fn display_matches(&self, d: &impl Display) -> bool
Returns true
if this pattern matches the formatted output of the given
type implementing fmt::Display
.
For example:
use matchers::Pattern;
#[derive(Debug)]
pub struct Hello {
to: &'static str,
}
impl fmt::Display for Hello {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Hello {}", self.to)
}
}
let pattern = Pattern::new("Hello [Ww].+").unwrap();
let hello_world = Hello { to: "world" };
assert!(pattern.display_matches(&hello_world));
assert_eq!(pattern.debug_matches(&hello_world), false);
let hello_sf = Hello { to: "San Francisco" };
assert_eq!(pattern.display_matches(&hello_sf), false);
let hello_washington = Hello { to: "Washington" };
assert!(pattern.display_matches(&hello_washington));
Sourcepub fn read_matches(&self, io: impl Read) -> Result<bool>
pub fn read_matches(&self, io: impl Read) -> Result<bool>
Returns either a bool
indicating whether or not this pattern matches the
data read from the provided io::Read
stream, or an io::Error
if an
error occurred reading from the stream.
Trait Implementations§
Auto Trait Implementations§
impl<S, A> Freeze for Pattern<S, A>where
A: Freeze,
impl<S, A> RefUnwindSafe for Pattern<S, A>where
A: RefUnwindSafe,
impl<S, A> Send for Pattern<S, A>where
A: Send,
impl<S, A> Sync for Pattern<S, A>where
A: Sync,
impl<S, A> Unpin for Pattern<S, A>where
A: Unpin,
impl<S, A> UnwindSafe for Pattern<S, A>where
A: UnwindSafe,
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> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)