Struct Input
struct Input<'h> { ... }
The configuration and the haystack to use for an Aho-Corasick search.
When executing a search, there are a few parameters one might want to configure:
- The haystack to search, provided to the
Input::newconstructor. This is the only required parameter. - The span within the haystack to limit a search to. (The default
is the entire haystack.) This is configured via
Input::spanorInput::range. - Whether to run an unanchored (matches can occur anywhere after the
start of the search) or anchored (matches can only occur beginning at
the start of the search) search. Unanchored search is the default. This is
configured via
Input::anchored. - Whether to quit the search as soon as a match has been found, regardless
of the
MatchKindthat the searcher was built with. This is configured viaInput::earliest.
For most cases, the defaults for all optional parameters are appropriate. The utility of this type is that it keeps the default or common case simple while permitting tweaking parameters in more niche use cases while reusing the same search APIs.
Valid bounds and search termination
An Input permits setting the bounds of a search via either
Input::span or Input::range. The bounds set must be valid, or
else a panic will occur. Bounds are valid if and only if:
- The bounds represent a valid range into the input's haystack.
- or the end bound is a valid ending bound for the haystack and the start bound is exactly one greater than the end bound.
In the latter case, Input::is_done will return true and indicates any
search receiving such an input should immediately return with no match.
Other than representing "search is complete," the Input::span and
Input::range APIs are never necessary. Instead, callers can slice the
haystack instead, e.g., with &haystack[start..end]. With that said, they
can be more convenient than slicing because the match positions reported
when using Input::span or Input::range are in terms of the original
haystack. If you instead use &haystack[start..end], then you'll need to
add start to any match position returned in order for it to be a correct
index into haystack.
Example: &str and &[u8] automatically convert to an Input
There is a From<&T> for Input implementation for all T: AsRef<[u8]>.
Additionally, the AhoCorasick search APIs accept
a Into<Input>. These two things combined together mean you can provide
things like &str and &[u8] to search APIs when the defaults are
suitable, but also an Input when they're not. For example:
use ;
// Build a searcher that supports both unanchored and anchored modes.
let ac = builder
.start_kind
.build
.unwrap;
let haystack = "abcd";
// A search using default parameters is unanchored. With standard
// semantics, this finds `b` first.
assert_eq!;
// Using the same 'find' routine, we can provide an 'Input' explicitly
// that is configured to do an anchored search. Since 'b' doesn't start
// at the beginning of the search, it is not reported as a match.
assert_eq!;
Implementations
impl<'h> Input<'h>
fn new<H: ?Sized + AsRef<[u8]>>(haystack: &'h H) -> Input<'h>Create a new search configuration for the given haystack.
fn span<S: Into<Span>>(self: Self, span: S) -> Input<'h>Set the span for this search.
This routine is generic over how a span is provided. While a
Spanmay be given directly, one may also provide astd::ops::Range<usize>. To provide anything supported by range syntax, use theInput::rangemethod.The default span is the entire haystack.
Note that
Input::rangeoverrides this method and vice versa.Panics
This panics if the given span does not correspond to valid bounds in the haystack or the termination of a search.
Example
This example shows how the span of the search can impact whether a match is reported or not.
use ; let patterns = &; let haystack = "abcd"; let ac = builder .match_kind .build .unwrap; let input = new.span; let mat = ac.try_find?.expect; // Without the span stopping the search early, 'abcd' would be reported // because it is the correct leftmost-first match. assert_eq!; # Ok::fn range<R: RangeBounds<usize>>(self: Self, range: R) -> Input<'h>Like
Input::span, but accepts any range instead.The default range is the entire haystack.
Note that
Input::spanoverrides this method and vice versa.Panics
This routine will panic if the given range could not be converted to a valid
Range. For example, this would panic when given0..=usize::MAXsince it cannot be represented using a half-open interval in terms ofusize.This routine also panics if the given range does not correspond to valid bounds in the haystack or the termination of a search.
Example
use Input; let input = new; assert_eq!; let input = new.range; assert_eq!;fn anchored(self: Self, mode: Anchored) -> Input<'h>Sets the anchor mode of a search.
When a search is anchored (via
Anchored::Yes), a match must begin at the start of a search. When a search is not anchored (that'sAnchored::No), searchers will look for a match anywhere in the haystack.By default, the anchored mode is
Anchored::No.Support for anchored searches
Anchored or unanchored searches might not always be available, depending on the type of searcher used and its configuration:
noncontiguous::NFAalways supports both unanchored and anchored searches.contiguous::NFAalways supports both unanchored and anchored searches.dfa::DFAsupports only unanchored searches by default.dfa::Builder::start_kindcan be used to change the default to supporting both kinds of searches or even just anchored searches.AhoCorasickinherits the same setup as aDFA. Namely, it only supports unanchored searches by default, butAhoCorasickBuilder::start_kindcan change this.
If you try to execute a search using a
try_("fallible") method with an unsupported anchor mode, then an error will be returned. For calls to infallible search methods, a panic will result.Example
This demonstrates the differences between an anchored search and an unanchored search. Notice that we build our
AhoCorasicksearcher withStartKind::Bothso that it supports both unanchored and anchored searches simultaneously.use ; let patterns = &; let haystack = "abcd"; let ac = builder .start_kind .build .unwrap; // Note that 'Anchored::No' is the default, so it doesn't need to // be explicitly specified here. let input = new; let mat = ac.try_find?.expect; assert_eq!; // While 'bcd' occurs in the haystack, it does not begin where our // search begins, so no match is found. let input = new.anchored; assert_eq!; // However, if we start our search where 'bcd' starts, then we will // find a match. let input = new.range.anchored; let mat = ac.try_find?.expect; assert_eq!; # Ok::fn earliest(self: Self, yes: bool) -> Input<'h>Whether to execute an "earliest" search or not.
When running a non-overlapping search, an "earliest" search will return the match location as early as possible. For example, given the patterns
abcandb, and a haystack ofabc, a normal leftmost-first search will returnabcas a match. But an "earliest" search will return as soon as it is known that a match occurs, which happens oncebis seen.Note that when using
MatchKind::Standard, the "earliest" option has no effect since standard semantics are already "earliest." Note also that this has no effect in overlapping searches, since overlapping searches also use standard semantics and report all possible matches.This is disabled by default.
Example
This example shows the difference between "earliest" searching and normal leftmost searching.
use ; let patterns = &; let haystack = "abc"; let ac = builder .match_kind .build .unwrap; // The normal leftmost-first match. let input = new; let mat = ac.try_find?.expect; assert_eq!; // The "earliest" possible match, even if it isn't leftmost-first. let input = new.earliest; let mat = ac.try_find?.expect; assert_eq!; # Ok::fn set_span<S: Into<Span>>(self: &mut Self, span: S)Set the span for this search configuration.
This is like the
Input::spanmethod, except this mutates the span in place.This routine is generic over how a span is provided. While a
Spanmay be given directly, one may also provide astd::ops::Range<usize>.Panics
This panics if the given span does not correspond to valid bounds in the haystack or the termination of a search.
Example
use Input; let mut input = new; assert_eq!; input.set_span; assert_eq!;fn set_range<R: RangeBounds<usize>>(self: &mut Self, range: R)Set the span for this search configuration given any range.
This is like the
Input::rangemethod, except this mutates the span in place.Panics
This routine will panic if the given range could not be converted to a valid
Range. For example, this would panic when given0..=usize::MAXsince it cannot be represented using a half-open interval in terms ofusize.This routine also panics if the given range does not correspond to valid bounds in the haystack or the termination of a search.
Example
use Input; let mut input = new; assert_eq!; input.set_range; assert_eq!;fn set_start(self: &mut Self, start: usize)Set the starting offset for the span for this search configuration.
This is a convenience routine for only mutating the start of a span without having to set the entire span.
Panics
This panics if the given span does not correspond to valid bounds in the haystack or the termination of a search.
Example
use Input; let mut input = new; assert_eq!; input.set_start; assert_eq!;fn set_end(self: &mut Self, end: usize)Set the ending offset for the span for this search configuration.
This is a convenience routine for only mutating the end of a span without having to set the entire span.
Panics
This panics if the given span does not correspond to valid bounds in the haystack or the termination of a search.
Example
use Input; let mut input = new; assert_eq!; input.set_end; assert_eq!;fn set_anchored(self: &mut Self, mode: Anchored)Set the anchor mode of a search.
This is like
Input::anchored, except it mutates the search configuration in place.Example
use ; let mut input = new; assert_eq!; input.set_anchored; assert_eq!;fn set_earliest(self: &mut Self, yes: bool)Set whether the search should execute in "earliest" mode or not.
This is like
Input::earliest, except it mutates the search configuration in place.Example
use Input; let mut input = new; assert!; input.set_earliest; assert!;fn haystack(self: &Self) -> &[u8]Return a borrow of the underlying haystack as a slice of bytes.
Example
use Input; let input = new; assert_eq!;fn start(self: &Self) -> usizeReturn the start position of this search.
This is a convenience routine for
search.get_span().start().Example
use Input; let input = new; assert_eq!; let input = new.span; assert_eq!;fn end(self: &Self) -> usizeReturn the end position of this search.
This is a convenience routine for
search.get_span().end().Example
use Input; let input = new; assert_eq!; let input = new.span; assert_eq!;fn get_span(self: &Self) -> SpanReturn the span for this search configuration.
If one was not explicitly set, then the span corresponds to the entire range of the haystack.
Example
use ; let input = new; assert_eq!;fn get_range(self: &Self) -> Range<usize>Return the span as a range for this search configuration.
If one was not explicitly set, then the span corresponds to the entire range of the haystack.
Example
use Input; let input = new; assert_eq!;fn get_anchored(self: &Self) -> AnchoredReturn the anchored mode for this search configuration.
If no anchored mode was set, then it defaults to
Anchored::No.Example
use ; let mut input = new; assert_eq!; input.set_anchored; assert_eq!;fn get_earliest(self: &Self) -> boolReturn whether this search should execute in "earliest" mode.
Example
use Input; let input = new; assert!;fn is_done(self: &Self) -> boolReturn true if this input has been exhausted, which in turn means all subsequent searches will return no matches.
This occurs precisely when the start position of this search is greater than the end position of the search.
Example
use Input; let mut input = new; assert!; input.set_start; assert!; input.set_start; assert!;
impl<'h> Clone for Input<'h>
fn clone(self: &Self) -> Input<'h>
impl<'h> Debug for Input<'h>
fn fmt(self: &Self, f: &mut Formatter<'_>) -> Result
impl<'h> Freeze for Input<'h>
impl<'h> RefUnwindSafe for Input<'h>
impl<'h> Send for Input<'h>
impl<'h> Sync for Input<'h>
impl<'h> Unpin for Input<'h>
impl<'h> UnsafeUnpin for Input<'h>
impl<'h> UnwindSafe for Input<'h>
impl<'h, H: ?Sized + AsRef<[u8]>> From for Input<'h>
fn from(haystack: &'h H) -> Input<'h>
impl<T> Any for Input<'h>
fn type_id(self: &Self) -> TypeId
impl<T> Borrow for Input<'h>
fn borrow(self: &Self) -> &T
impl<T> BorrowMut for Input<'h>
fn borrow_mut(self: &mut Self) -> &mut T
impl<T> CloneToUninit for Input<'h>
unsafe fn clone_to_uninit(self: &Self, dest: *mut u8)
impl<T> From for Input<'h>
fn from(t: T) -> TReturns the argument unchanged.
impl<T> ToOwned for Input<'h>
fn to_owned(self: &Self) -> Tfn clone_into(self: &Self, target: &mut T)
impl<T, U> Into for Input<'h>
fn into(self: Self) -> UCalls
U::from(self).That is, this conversion is whatever the implementation of
[From]<T> for Uchooses to do.
impl<T, U> TryFrom for Input<'h>
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
impl<T, U> TryInto for Input<'h>
fn try_into(self: Self) -> Result<U, <U as TryFrom<T>>::Error>