pubusecrate::memmem::searcher::PrefilterConfig as Prefilter;
// This is exported here for use in the crate::arch::all::twoway // implementation. This is essentially an abstraction breaker. Namely, the // public API of twoway doesn't support providing a prefilter, but its crate // internal API does. The main reason for this is that I didn't want to do the // API design required to support it without a concrete use case. pub(crate) usecrate::memmem::searcher::Pre;
/// Returns an iterator over all non-overlapping 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; /// /// let haystack = b"foo bar foo baz foo"; /// let mut it = memmem::find_iter(haystack, b"foo"); /// assert_eq!(Some(0), it.next()); /// assert_eq!(Some(8), it.next()); /// assert_eq!(Some(16), it.next()); /// assert_eq!(None, it.next()); /// ``` #[inline] pubfn find_iter<'h, 'n, N: 'n + ?Sized + AsRef<[u8]>>(
haystack: &'h [u8],
needle: &'n N,
) -> FindIter<'h, 'n> {
FindIter::new(haystack, Finder::new(needle))
}
/// Returns a reverse iterator over all non-overlapping 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; /// /// let haystack = b"foo bar foo baz foo"; /// let mut it = memmem::rfind_iter(haystack, b"foo"); /// assert_eq!(Some(16), it.next()); /// assert_eq!(Some(8), it.next()); /// assert_eq!(Some(0), it.next()); /// assert_eq!(None, it.next()); /// ``` #[inline] pubfn rfind_iter<'h, 'n, N: 'n + ?Sized + AsRef<[u8]>>(
haystack: &'h [u8],
needle: &'n N,
) -> FindRevIter<'h, 'n> {
FindRevIter::new(haystack, FinderRev::new(needle))
}
/// Returns the index of the first occurrence of the given needle. /// /// Note that if you're are searching for the same needle in many different /// small haystacks, it may be faster to initialize a [`Finder`] once, /// and reuse it for each search. /// /// # 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; /// /// let haystack = b"foo bar baz"; /// assert_eq!(Some(0), memmem::find(haystack, b"foo")); /// assert_eq!(Some(4), memmem::find(haystack, b"bar")); /// assert_eq!(None, memmem::find(haystack, b"quux")); /// ``` #[inline] pubfn find(haystack: &[u8], needle: &[u8]) -> Option<usize> { if haystack.len() < 64 {
rabinkarp::Finder::new(needle).find(haystack, needle)
} else {
Finder::new(needle).find(haystack)
}
}
/// Returns the index of the last occurrence of the given needle. /// /// Note that if you're are searching for the same needle in many different /// small haystacks, it may be faster to initialize a [`FinderRev`] once, /// and reuse it for each search. /// /// # 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; /// /// let haystack = b"foo bar baz"; /// assert_eq!(Some(0), memmem::rfind(haystack, b"foo")); /// assert_eq!(Some(4), memmem::rfind(haystack, b"bar")); /// assert_eq!(Some(8), memmem::rfind(haystack, b"ba")); /// assert_eq!(None, memmem::rfind(haystack, b"quux")); /// ``` #[inline] pubfn rfind(haystack: &[u8], needle: &[u8]) -> Option<usize> { if haystack.len() < 64 {
rabinkarp::FinderRev::new(needle).rfind(haystack, needle)
} else {
FinderRev::new(needle).rfind(haystack)
}
}
/// An iterator over non-overlapping substring matches. /// /// Matches are reported by the byte offset at which they begin. /// /// `'h` is the lifetime of the haystack while `'n` is the lifetime of the /// needle. #[derive(Debug, Clone)] pubstruct FindIter<'h, 'n> {
haystack: &'h [u8],
prestate: PrefilterState,
finder: Finder<'n>,
pos: usize,
}
/// Convert this iterator into its owned variant, such that it no longer /// borrows the finder and needle. /// /// If this is already an owned iterator, then this is a no-op. Otherwise, /// this copies the needle. /// /// This is only available when the `alloc` feature is enabled. #[cfg(feature = "alloc")] #[inline] pubfn into_owned(self) -> FindIter<'h, 'static> {
FindIter {
haystack: self.haystack,
prestate: self.prestate,
finder: self.finder.into_owned(),
pos: self.pos,
}
}
}
impl<'h, 'n> Iterator for FindIter<'h, 'n> { type Item = usize;
fn next(&mutself) -> Option<usize> { let needle = self.finder.needle(); let haystack = self.haystack.get(self.pos..)?; let idx = self.finder.searcher.find(&mutself.prestate, haystack, needle)?;
fn size_hint(&self) -> (usize, Option<usize>) { // The largest possible number of non-overlapping matches is the // quotient of the haystack and the needle (or the length of the // haystack, if the needle is empty) matchself.haystack.len().checked_sub(self.pos) {
None => (0, Some(0)),
Some(haystack_len) => matchself.finder.needle().len() { // Empty needles always succeed and match at every point // (including the very end) 0 => (
haystack_len.saturating_add(1),
haystack_len.checked_add(1),
),
needle_len => (0, Some(haystack_len / needle_len)),
},
}
}
}
/// An iterator over non-overlapping substring matches in reverse. /// /// Matches are reported by the byte offset at which they begin. /// /// `'h` is the lifetime of the haystack while `'n` is the lifetime of the /// needle. #[derive(Clone, Debug)] pubstruct FindRevIter<'h, 'n> {
haystack: &'h [u8],
finder: FinderRev<'n>, /// When searching with an empty needle, this gets set to `None` after /// we've yielded the last element at `0`.
pos: Option<usize>,
}
/// Convert this iterator into its owned variant, such that it no longer /// borrows the finder and needle. /// /// If this is already an owned iterator, then this is a no-op. Otherwise, /// this copies the needle. /// /// This is only available when the `std` feature is enabled. #[cfg(feature = "alloc")] #[inline] pubfn into_owned(self) -> FindRevIter<'h, 'static> {
FindRevIter {
haystack: self.haystack,
finder: self.finder.into_owned(),
pos: self.pos,
}
}
}
impl<'h, 'n> Iterator for FindRevIter<'h, 'n> { type Item = usize;
fn next(&mutself) -> Option<usize> { let pos = matchself.pos {
None => return None,
Some(pos) => pos,
}; let result = self.finder.rfind(&self.haystack[..pos]); match result {
None => None,
Some(i) => { if pos == i { self.pos = pos.checked_sub(1);
} else { self.pos = Some(i);
}
Some(i)
}
}
}
}
/// 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. #[derive(Clone, Debug)] pubstruct Finder<'n> {
needle: CowBytes<'n>,
searcher: Searcher,
}
impl<'n> Finder<'n> { /// Create a new finder for the given needle. #[inline] pubfn new<B: ?Sized + AsRef<[u8]>>(needle: &'n B) -> Finder<'n> {
FinderBuilder::new().build_forward(needle)
}
/// 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)); /// ``` #[inline] pubfn find(&self, haystack: &[u8]) -> Option<usize> { letmut prestate = PrefilterState::new(); let needle = self.needle.as_slice(); self.searcher.find(&mut prestate, haystack, needle)
}
/// 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()); /// ``` #[inline] pubfn find_iter<'a, 'h>(
&'a self,
haystack: &'h [u8],
) -> FindIter<'h, 'a> {
FindIter::new(haystack, self.as_ref())
}
/// 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. #[cfg(feature = "alloc")] #[inline] pubfn into_owned(self) -> Finder<'static> {
Finder {
needle: self.needle.into_owned(),
searcher: self.searcher.clone(),
}
}
/// 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. #[inline] pubfn as_ref(&self) -> Finder<'_> {
Finder {
needle: CowBytes::new(self.needle()),
searcher: self.searcher.clone(),
}
}
/// 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. #[inline] pubfn needle(&self) -> &[u8] { self.needle.as_slice()
}
}
/// A single substring reverse 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 [`rfind`] is good enough, but `FinderRev` 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 `FinderRev` that is not connected to /// the lifetime of its needle. #[derive(Clone, Debug)] pubstruct FinderRev<'n> {
needle: CowBytes<'n>,
searcher: SearcherRev,
}
impl<'n> FinderRev<'n> { /// Create a new reverse finder for the given needle. #[inline] pubfn new<B: ?Sized + AsRef<[u8]>>(needle: &'n B) -> FinderRev<'n> {
FinderBuilder::new().build_reverse(needle)
}
/// Returns the index of the last 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 memchr::memmem::FinderRev; /// /// let haystack = b"foo bar baz"; /// assert_eq!(Some(0), FinderRev::new("foo").rfind(haystack)); /// assert_eq!(Some(4), FinderRev::new("bar").rfind(haystack)); /// assert_eq!(None, FinderRev::new("quux").rfind(haystack)); /// ``` pubfn rfind<B: AsRef<[u8]>>(&self, haystack: B) -> Option<usize> { self.searcher.rfind(haystack.as_ref(), self.needle.as_slice())
}
/// Returns a reverse 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::FinderRev; /// /// let haystack = b"foo bar foo baz foo"; /// let finder = FinderRev::new(b"foo"); /// let mut it = finder.rfind_iter(haystack); /// assert_eq!(Some(16), it.next()); /// assert_eq!(Some(8), it.next()); /// assert_eq!(Some(0), it.next()); /// assert_eq!(None, it.next()); /// ``` #[inline] pubfn rfind_iter<'a, 'h>(
&'a self,
haystack: &'h [u8],
) -> FindRevIter<'h, 'a> {
FindRevIter::new(haystack, self.as_ref())
}
/// 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. #[cfg(feature = "alloc")] #[inline] pubfn into_owned(self) -> FinderRev<'static> {
FinderRev {
needle: self.needle.into_owned(),
searcher: self.searcher.clone(),
}
}
/// 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. #[inline] pubfn as_ref(&self) -> FinderRev<'_> {
FinderRev {
needle: CowBytes::new(self.needle()),
searcher: self.searcher.clone(),
}
}
/// 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. #[inline] pubfn needle(&self) -> &[u8] { self.needle.as_slice()
}
}
/// A builder for constructing non-default forward or reverse memmem finders. /// /// A builder is primarily useful for configuring a substring searcher. /// Currently, the only configuration exposed is the ability to disable /// heuristic prefilters used to speed up certain searches. #[derive(Clone, Debug, Default)] pubstruct FinderBuilder {
prefilter: Prefilter,
}
impl FinderBuilder { /// Create a new finder builder with default settings. pubfn new() -> FinderBuilder {
FinderBuilder::default()
}
/// Build a forward finder using the given needle from the current /// settings. pubfn build_forward<'n, B: ?Sized + AsRef<[u8]>>(
&self,
needle: &'n B,
) -> Finder<'n> { self.build_forward_with_ranker(DefaultFrequencyRank, needle)
}
/// Build a forward finder using the given needle and a custom heuristic for /// determining the frequency of a given byte in the dataset. /// See [`HeuristicFrequencyRank`] for more details. pubfn build_forward_with_ranker< 'n,
R: HeuristicFrequencyRank,
B: ?Sized + AsRef<[u8]>,
>(
&self,
ranker: R,
needle: &'n B,
) -> Finder<'n> { let needle = needle.as_ref();
Finder {
needle: CowBytes::new(needle),
searcher: Searcher::new(self.prefilter, ranker, needle),
}
}
/// Build a reverse finder using the given needle from the current /// settings. pubfn build_reverse<'n, B: ?Sized + AsRef<[u8]>>(
&self,
needle: &'n B,
) -> FinderRev<'n> { let needle = needle.as_ref();
FinderRev {
needle: CowBytes::new(needle),
searcher: SearcherRev::new(needle),
}
}
/// Configure the prefilter setting for the finder. /// /// See the documentation for [`Prefilter`] for more discussion on why /// you might want to configure this. pubfn prefilter(&mutself, prefilter: Prefilter) -> &mut FinderBuilder { self.prefilter = prefilter; self
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.