pub unsafe trait Searcher<'a> { fn haystack(&self) -> &'a str; fn next(&mut self) -> SearchStep; fn next_match(&mut self) -> Option<(usize, usize)> { ... } fn next_reject(&mut self) -> Option<(usize, usize)> { ... } }
A searcher for a string pattern.
This trait provides methods for searching for non-overlapping matches of a pattern starting from the front (left) of a string.
It will be implemented by associated Searcher
types of the Pattern
trait.
The trait is marked unsafe because the indices returned by the next()
methods are required to lie on valid utf8 boundaries in the haystack. This enables consumers of this trait to slice the haystack without additional runtime checks.
fn haystack(&self) -> &'a str
Getter for the underlaying string to be searched in
Will always return the same &str
fn next(&mut self) -> SearchStep
Performs the next search step starting from the front.
Match(a, b)
if haystack[a..b]
matches the pattern.Reject(a, b)
if haystack[a..b]
can not match the pattern, even partially.Done
if every byte of the haystack has been visitedThe stream of Match
and Reject
values up to a Done
will contain index ranges that are adjacent, non-overlapping, covering the whole haystack, and laying on utf8 boundaries.
A Match
result needs to contain the whole matched pattern, however Reject
results may be split up into arbitrary many adjacent fragments. Both ranges may have zero length.
As an example, the pattern "aaa"
and the haystack "cbaaaaab"
might produce the stream [Reject(0, 1), Reject(1, 2), Match(2, 5), Reject(5, 8)]
fn next_match(&mut self) -> Option<(usize, usize)>
Find the next Match
result. See next()
fn next_reject(&mut self) -> Option<(usize, usize)>
Find the next Reject
result. See next()
impl<'a> Searcher<'a> for CharSearcher<'a>
impl<'a, 'b> Searcher<'a> for CharSliceSearcher<'a, 'b>
impl<'a, F> Searcher<'a> for CharPredicateSearcher<'a, F> where F: FnMut(char) -> bool
impl<'a, 'b> Searcher<'a> for StrSearcher<'a, 'b>
© 2010 The Rust Project Developers
Licensed under the Apache License, Version 2.0 or the MIT license, at your option.
https://doc.rust-lang.org/collections/str/pattern/trait.Searcher.html