Trait pattern_3::needle::ReverseSearcher
source · pub unsafe trait ReverseSearcher<A: Hay + ?Sized>: Searcher<A> {
fn rsearch(&mut self, span: Span<&A>) -> Option<Range<A::Index>>;
}
Expand description
A searcher which can be searched from the end.
This trait provides methods for searching for non-overlapping matches of a needle starting from the back (right) of a hay.
Safety
This trait is marked unsafe because the range returned by its methods are required to lie on valid codeword boundaries in the haystack. This enables users of this trait to slice the haystack without additional runtime checks.
Required Methods§
sourcefn rsearch(&mut self, span: Span<&A>) -> Option<Range<A::Index>>
fn rsearch(&mut self, span: Span<&A>) -> Option<Range<A::Index>>
Searches for the last range which the needle can be found in the span.
This method is used to support the following standard algorithms:
The hay and the restricted range for searching can be recovered by
calling span
.into_parts()
. The returned range
should be relative to the hay and must be contained within the
restricted range from the span.
If the needle is not found, this method should return None
.
Examples
Search for the locations of a substring inside a string, using the searcher primitive.
extern crate pattern_3;
use pattern_3::{ReverseSearcher, Needle, Span};
let mut searcher = Needle::<&str>::into_searcher("::");
let span = Span::from("lion::tiger::leopard");
// ^ ^ ^
// string indices: 0 4 11
// found the last "::".
assert_eq!(searcher.rsearch(span.clone()), Some(11..13));
// slice the span to skip the last match.
let span = unsafe { span.slice_unchecked(0..11) };
// found the second to last "::".
assert_eq!(searcher.rsearch(span.clone()), Some(4..6));
// should found nothing now.
let span = unsafe { span.slice_unchecked(0..4) };
assert_eq!(searcher.rsearch(span.clone()), None);