Struct aho_corasick::packed::api::Searcher

source ·
pub struct Searcher {
    patterns: Arc<Patterns>,
    rabinkarp: RabinKarp,
    search_kind: SearchKind,
    minimum_len: usize,
}
Expand description

A packed searcher for quickly finding occurrences of multiple patterns.

If callers need more flexible construction, or if one wants to change the match semantics (either leftmost-first or leftmost-longest), then one can use the Config and/or Builder types for more fine grained control.

§Example

This example shows how to create a searcher from an iterator of patterns. By default, leftmost-first match semantics are used.

use aho_corasick::{packed::{MatchKind, Searcher}, PatternID};

let searcher = Searcher::new(["foobar", "foo"].iter().cloned())?;
let matches: Vec<PatternID> = searcher
    .find_iter("foobar")
    .map(|mat| mat.pattern())
    .collect();
assert_eq!(vec![PatternID::ZERO], matches);

Fields§

§patterns: Arc<Patterns>§rabinkarp: RabinKarp§search_kind: SearchKind§minimum_len: usize

Implementations§

source§

impl Searcher

source

pub fn new<I, P>(patterns: I) -> Option<Searcher>
where I: IntoIterator<Item = P>, P: AsRef<[u8]>,

A convenience function for constructing a searcher from an iterator of things that can be converted to a &[u8].

If a searcher could not be constructed (either because of an unsupported CPU or because there are too many patterns), then None is returned.

§Example

Basic usage:

use aho_corasick::{packed::{MatchKind, Searcher}, PatternID};

let searcher = Searcher::new(["foobar", "foo"].iter().cloned())?;
let matches: Vec<PatternID> = searcher
    .find_iter("foobar")
    .map(|mat| mat.pattern())
    .collect();
assert_eq!(vec![PatternID::ZERO], matches);
source

pub fn config() -> Config

A convenience function for calling Config::new().

This is useful for avoiding an additional import.

source

pub fn builder() -> Builder

A convenience function for calling Builder::new().

This is useful for avoiding an additional import.

source

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

Return the first occurrence of any of the patterns in this searcher, according to its match semantics, in the given haystack. The Match returned will include the identifier of the pattern that matched, which corresponds to the index of the pattern (starting from 0) in which it was added.

§Example

Basic usage:

use aho_corasick::{packed::{MatchKind, Searcher}, PatternID};

let searcher = Searcher::new(["foobar", "foo"].iter().cloned())?;
let mat = searcher.find("foobar")?;
assert_eq!(PatternID::ZERO, mat.pattern());
assert_eq!(0, mat.start());
assert_eq!(6, mat.end());
source

pub fn find_in<B: AsRef<[u8]>>(&self, haystack: B, span: Span) -> Option<Match>

Return the first occurrence of any of the patterns in this searcher, according to its match semantics, in the given haystack starting from the given position.

The Match returned will include the identifier of the pattern that matched, which corresponds to the index of the pattern (starting from 0) in which it was added. The offsets in the Match will be relative to the start of haystack (and not at).

§Example

Basic usage:

use aho_corasick::{packed::{MatchKind, Searcher}, PatternID, Span};

let haystack = "foofoobar";
let searcher = Searcher::new(["foobar", "foo"].iter().cloned())?;
let mat = searcher.find_in(haystack, Span::from(3..haystack.len()))?;
assert_eq!(PatternID::ZERO, mat.pattern());
assert_eq!(3, mat.start());
assert_eq!(9, mat.end());
source

pub fn find_iter<'a, 'b, B: ?Sized + AsRef<[u8]>>( &'a self, haystack: &'b B, ) -> FindIter<'a, 'b>

Return an iterator of non-overlapping occurrences of the patterns in this searcher, according to its match semantics, in the given haystack.

§Example

Basic usage:

use aho_corasick::{packed::{MatchKind, Searcher}, PatternID};

let searcher = Searcher::new(["foobar", "foo"].iter().cloned())?;
let matches: Vec<PatternID> = searcher
    .find_iter("foobar fooba foofoo")
    .map(|mat| mat.pattern())
    .collect();
assert_eq!(vec![
    PatternID::must(0),
    PatternID::must(1),
    PatternID::must(1),
    PatternID::must(1),
], matches);
source

pub fn match_kind(&self) -> &MatchKind

Returns the match kind used by this packed searcher.

§Examples

Basic usage:

use aho_corasick::packed::{MatchKind, Searcher};

let searcher = Searcher::new(["foobar", "foo"].iter().cloned())?;
// leftmost-first is the default.
assert_eq!(&MatchKind::LeftmostFirst, searcher.match_kind());
source

pub fn minimum_len(&self) -> usize

Returns the minimum length of a haystack that is required in order for packed searching to be effective.

In some cases, the underlying packed searcher may not be able to search very short haystacks. When that occurs, the implementation will defer to a slower non-packed searcher (which is still generally faster than Aho-Corasick for a small number of patterns). However, callers may want to avoid ever using the slower variant, which one can do by never passing a haystack shorter than the minimum length returned by this method.

source

pub fn memory_usage(&self) -> usize

Returns the approximate total amount of heap used by this searcher, in units of bytes.

source

fn find_in_slow(&self, haystack: &[u8], span: Span) -> Option<Match>

Use a slow (non-packed) searcher.

This is useful when a packed searcher could be constructed, but could not be used to search a specific haystack. For example, if Teddy was built but the haystack is smaller than ~34 bytes, then Teddy might not be able to run.

Trait Implementations§

source§

impl Clone for Searcher

source§

fn clone(&self) -> Searcher

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 Debug for Searcher

source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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,

source§

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

source§

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

source§

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.