Struct bstr::FinderReverse

source ·
pub struct FinderReverse<'a>(/* 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 ByteSlice::rfind or ByteSlice::rfind_iter is good enough, but FinderReverse 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 FinderReverse that is not connected to the lifetime of its needle.

Implementations§

source§

impl<'a> FinderReverse<'a>

source

pub fn new<B: ?Sized + AsRef<[u8]>>(needle: &'a B) -> FinderReverse<'a>

Create a new reverse finder for the given needle.

source

pub fn into_owned(self) -> FinderReverse<'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.

source

pub fn needle(&self) -> &[u8]

Returns the needle that this finder searches for.

Note that the lifetime of the needle returned is tied to the lifetime of this finder, and may be shorter than the 'a 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.

source

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 bstr::FinderReverse;

let haystack = "foo bar baz";
assert_eq!(Some(0), FinderReverse::new("foo").rfind(haystack));
assert_eq!(Some(4), FinderReverse::new("bar").rfind(haystack));
assert_eq!(None, FinderReverse::new("quux").rfind(haystack));

Trait Implementations§

source§

impl<'a> Clone for FinderReverse<'a>

source§

fn clone(&self) -> FinderReverse<'a>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<'a> Debug for FinderReverse<'a>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a> RefUnwindSafe for FinderReverse<'a>

§

impl<'a> Send for FinderReverse<'a>

§

impl<'a> Sync for FinderReverse<'a>

§

impl<'a> Unpin for FinderReverse<'a>

§

impl<'a> UnwindSafe for FinderReverse<'a>

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.