[][src]Struct bstr::Finder

pub struct Finder<'a> { /* fields omitted */ }

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 re-use the same needle to search multiple different haystacks with as little overhead as possible. In general, using BStr::find or BStr::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.

Methods

impl<'a> Finder<'a>[src]

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

Create a new finder for the given needle.

pub fn into_owned(self) -> Finder<'static>[src]

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.

pub fn needle(&self) -> &BStr[src]

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.

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

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, &BStr, and of course, &[u8] itself.

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>[src]

default fn clone_from(&mut self, source: &Self)
1.0.0
[src]

Performs copy-assignment from source. Read more

impl<'a> Debug for Finder<'a>[src]

Auto Trait Implementations

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

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

Blanket Implementations

impl<T, U> Into for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

impl<T> From for T[src]

impl<T, U> TryFrom for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T> Borrow for T where
    T: ?Sized
[src]

impl<T> BorrowMut for T where
    T: ?Sized
[src]

impl<T, U> TryInto for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

impl<T> Any for T where
    T: 'static + ?Sized
[src]