Struct memchr::memmem::Finder

source ·
pub struct Finder<'n> { /* private fields */ }
Expand description

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 find 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§

source§

impl<'n> Finder<'n>

source

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

Create a new finder for the given needle.

source

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

Returns the index of the first occurrence of this needle in the given 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::Finder;

let haystack = b"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));
source

pub fn find_iter<'a, 'h>(&'a self, haystack: &'h [u8]) -> FindIter<'h, 'a>

Returns an 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::Finder;

let haystack = b"foo bar foo baz foo";
let finder = Finder::new(b"foo");
let mut it = finder.find_iter(haystack);
assert_eq!(Some(0), it.next());
assert_eq!(Some(8), it.next());
assert_eq!(Some(16), it.next());
assert_eq!(None, it.next());
source

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

source

pub fn as_ref(&self) -> Finder<'_>

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.

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 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§

source§

impl<'n> Clone for Finder<'n>

source§

fn clone(&self) -> Finder<'n>

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<'n> Debug for Finder<'n>

source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'n> RefUnwindSafe for Finder<'n>

§

impl<'n> Send for Finder<'n>

§

impl<'n> Sync for Finder<'n>

§

impl<'n> Unpin for Finder<'n>

§

impl<'n> UnwindSafe for Finder<'n>

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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 Twhere 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 Twhere 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 Twhere 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.