pub struct FinderRev<'n> { /* private fields */ }
Expand description
A single substring reverse searcher fixed to a particular needle.
The purpose of this type is to permit callers to construct a substring
searcher that can be used to search haystacks without the overhead of
constructing the searcher in the first place. This is a somewhat niche
concern when it’s necessary to re-use the same needle to search multiple
different haystacks with as little overhead as possible. In general,
using rfind
is good enough, but FinderRev
is useful when you can
meaningfully observe searcher construction time in a profile.
When the std
feature is enabled, then this type has an into_owned
version which permits building a FinderRev
that is not connected to
the lifetime of its needle.
Implementations
sourceimpl<'n> FinderRev<'n>
impl<'n> FinderRev<'n>
sourcepub fn new<B: ?Sized + AsRef<[u8]>>(needle: &'n B) -> FinderRev<'n>
pub fn new<B: ?Sized + AsRef<[u8]>>(needle: &'n B) -> FinderRev<'n>
Create a new reverse finder for the given needle.
sourcepub fn rfind<B: AsRef<[u8]>>(&self, haystack: B) -> Option<usize>
pub fn rfind<B: AsRef<[u8]>>(&self, haystack: B) -> Option<usize>
Returns the index of the last occurrence of this needle in the given haystack.
The haystack may be any type that can be cheaply converted into a
&[u8]
. This includes, but is not limited to, &str
and &[u8]
.
Complexity
This routine is guaranteed to have worst case linear time complexity
with respect to both the needle and the haystack. That is, this runs
in O(needle.len() + haystack.len())
time.
This routine is also guaranteed to have worst case constant space complexity.
Examples
Basic usage:
use memchr::memmem::FinderRev;
let haystack = b"foo bar baz";
assert_eq!(Some(0), FinderRev::new("foo").rfind(haystack));
assert_eq!(Some(4), FinderRev::new("bar").rfind(haystack));
assert_eq!(None, FinderRev::new("quux").rfind(haystack));
sourcepub fn rfind_iter<'a, 'h>(&'a self, haystack: &'h [u8]) -> FindRevIter<'h, 'a>ⓘNotable traits for FindRevIter<'h, 'n>impl<'h, 'n> Iterator for FindRevIter<'h, 'n> type Item = usize;
pub fn rfind_iter<'a, 'h>(&'a self, haystack: &'h [u8]) -> FindRevIter<'h, 'a>ⓘNotable traits for FindRevIter<'h, 'n>impl<'h, 'n> Iterator for FindRevIter<'h, 'n> type Item = usize;
Returns a reverse iterator over all occurrences of a substring in a haystack.
Complexity
This routine is guaranteed to have worst case linear time complexity
with respect to both the needle and the haystack. That is, this runs
in O(needle.len() + haystack.len())
time.
This routine is also guaranteed to have worst case constant space complexity.
Examples
Basic usage:
use memchr::memmem::FinderRev;
let haystack = b"foo bar foo baz foo";
let finder = FinderRev::new(b"foo");
let mut it = finder.rfind_iter(haystack);
assert_eq!(Some(16), it.next());
assert_eq!(Some(8), it.next());
assert_eq!(Some(0), it.next());
assert_eq!(None, it.next());
sourcepub fn into_owned(self) -> FinderRev<'static>
pub fn into_owned(self) -> FinderRev<'static>
Convert this finder into its owned variant, such that it no longer borrows the needle.
If this is already an owned finder, then this is a no-op. Otherwise, this copies the needle.
This is only available when the std
feature is enabled.
sourcepub fn as_ref(&self) -> FinderRev<'_>
pub fn as_ref(&self) -> FinderRev<'_>
Convert this finder into its borrowed variant.
This is primarily useful if your finder is owned and you’d like to store its borrowed variant in some intermediate data structure.
Note that the lifetime parameter of the returned finder is tied to the
lifetime of self
, and may be shorter than the 'n
lifetime of the
needle itself. Namely, a finder’s needle can be either borrowed or
owned, so the lifetime of the needle returned must necessarily be the
shorter of the two.
sourcepub fn needle(&self) -> &[u8]ⓘNotable traits for &'_ [u8]impl<'_> Read for &'_ [u8]impl<'_> Write for &'_ mut [u8]
pub fn needle(&self) -> &[u8]ⓘNotable traits for &'_ [u8]impl<'_> Read for &'_ [u8]impl<'_> Write for &'_ mut [u8]
Returns the needle that this finder searches for.
Note that the lifetime of the needle returned is tied to the lifetime
of the finder, and may be shorter than the 'n
lifetime. Namely, a
finder’s needle can be either borrowed or owned, so the lifetime of the
needle returned must necessarily be the shorter of the two.
Trait Implementations
Auto Trait Implementations
impl<'n> RefUnwindSafe for FinderRev<'n>
impl<'n> Send for FinderRev<'n>
impl<'n> Sync for FinderRev<'n>
impl<'n> Unpin for FinderRev<'n>
impl<'n> UnwindSafe for FinderRev<'n>
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcefn clone_into(&self, target: &mut T)
fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more