Struct Finder

pub struct Finder<'a>(/* private field */);

A single substring 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 reuse the same needle to search multiple different haystacks with as little overhead as possible. In general, using ByteSlice::find or ByteSlice::find_iter is good enough, but Finder 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 Finder that is not connected to the lifetime of its needle.

Implementations

impl<'a> Finder<'a>

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

Create a new finder for the given needle.

fn into_owned(self) -> Finder<'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 alloc feature is enabled.

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 the 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.

fn find<B: AsRef<[u8]>>(&self, haystack: B) -> Option<usize>

Returns the index of the first 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::Finder;

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

Trait Implementations

impl<'a> Clone for Finder<'a>

fn clone(&self) -> Self

impl<'a> Debug for Finder<'a>

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

Auto Trait Implementations

impl<'a> Freeze for Finder<'a>

impl<'a> RefUnwindSafe for Finder<'a>

impl<'a> Send for Finder<'a>

impl<'a> Sync for Finder<'a>

impl<'a> Unpin for Finder<'a>

impl<'a> UnsafeUnpin for Finder<'a>

impl<'a> UnwindSafe for Finder<'a>

Blanket Implementations

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

fn type_id(&self) -> TypeId

impl<T> Borrow<T> for Finder<'a> where T: ?Sized,

fn borrow(&self) -> &T

impl<T> BorrowMut<T> for Finder<'a> where T: ?Sized,

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

impl<T> CloneToUninit for Finder<'a> where T: Clone,

unsafe fn clone_to_uninit(&self, dest: *mut u8)

impl<T> From<T> for Finder<'a>

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> ToOwned for Finder<'a> where T: Clone,

type Owned = T;
fn to_owned(&self) -> T
fn clone_into(&self, target: &mut T)

impl<T, U> Into<U> for Finder<'a> where U: From<T>,

fn into(self) -> U

Calls U::from(self).

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

impl<T, U> TryFrom<U> for Finder<'a> where U: Into<T>,

type Error = never;
fn try_from(value: U) -> Result<T, never>

impl<T, U> TryInto<U> for Finder<'a> where U: TryFrom<T>,

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