1//! The string Pattern API.
2//!
3//! The Pattern API provides a generic mechanism for using different pattern
4//! types when searching through a string.
5//!
6//! For more details, see the traits [`Pattern`], [`Searcher`],
7//! [`ReverseSearcher`], and [`DoubleEndedSearcher`].
8//!
9//! Although this API is unstable, it is exposed via stable APIs on the
10//! [`str`] type.
11//!
12//! # Examples
13//!
14//! [`Pattern`] is [implemented][pattern-impls] in the stable API for
15//! [`&str`][`str`], [`char`], slices of [`char`], and functions and closures
16//! implementing `FnMut(char) -> bool`.
17//!
18//! ```
19//! let s = "Can you find a needle in a haystack?";
20//!
21//! // &str pattern
22//! assert_eq!(s.find("you"), Some(4));
23//! // char pattern
24//! assert_eq!(s.find('n'), Some(2));
25//! // array of chars pattern
26//! assert_eq!(s.find(&['a', 'e', 'i', 'o', 'u']), Some(1));
27//! // slice of chars pattern
28//! assert_eq!(s.find(&['a', 'e', 'i', 'o', 'u'][..]), Some(1));
29//! // closure pattern
30//! assert_eq!(s.find(|c: char| c.is_ascii_punctuation()), Some(35));
31//! ```
32//!
33//! [pattern-impls]: Pattern#implementors
3435#![unstable(
36 feature = "pattern",
37 reason = "API not fully fleshed out and ready to be stabilized",
38 issue = "27721"
39)]
4041use crate::cmp::Ordering;
42use crate::convert::TryIntoas _;
43use crate::slice::memchr;
44use crate::{cmp, fmt};
4546// Pattern
4748/// A string pattern.
49///
50/// A `Pattern` expresses that the implementing type
51/// can be used as a string pattern for searching in a [`&str`][str].
52///
53/// For example, both `'a'` and `"aa"` are patterns that
54/// would match at index `1` in the string `"baaaab"`.
55///
56/// The trait itself acts as a builder for an associated
57/// [`Searcher`] type, which does the actual work of finding
58/// occurrences of the pattern in a string.
59///
60/// Depending on the type of the pattern, the behavior of methods like
61/// [`str::find`] and [`str::contains`] can change. The table below describes
62/// some of those behaviors.
63///
64/// | Pattern type | Match condition |
65/// |--------------------------|-------------------------------------------|
66/// | `&str` | is substring |
67/// | `char` | is contained in string |
68/// | `&[char]` | any char in slice is contained in string |
69/// | `F: FnMut(char) -> bool` | `F` returns `true` for a char in string |
70/// | `&&str` | is substring |
71/// | `&String` | is substring |
72///
73/// # Examples
74///
75/// ```
76/// // &str
77/// assert_eq!("abaaa".find("ba"), Some(1));
78/// assert_eq!("abaaa".find("bac"), None);
79///
80/// // char
81/// assert_eq!("abaaa".find('a'), Some(0));
82/// assert_eq!("abaaa".find('b'), Some(1));
83/// assert_eq!("abaaa".find('c'), None);
84///
85/// // &[char; N]
86/// assert_eq!("ab".find(&['b', 'a']), Some(0));
87/// assert_eq!("abaaa".find(&['a', 'z']), Some(0));
88/// assert_eq!("abaaa".find(&['c', 'd']), None);
89///
90/// // &[char]
91/// assert_eq!("ab".find(&['b', 'a'][..]), Some(0));
92/// assert_eq!("abaaa".find(&['a', 'z'][..]), Some(0));
93/// assert_eq!("abaaa".find(&['c', 'd'][..]), None);
94///
95/// // FnMut(char) -> bool
96/// assert_eq!("abcdef_z".find(|ch| ch > 'd' && ch < 'y'), Some(4));
97/// assert_eq!("abcddd_z".find(|ch| ch > 'd' && ch < 'y'), None);
98/// ```
99pub trait Pattern: Sized {
100/// Associated searcher for this pattern
101type Searcher<'a>: Searcher<'a>;
102103/// Constructs the associated searcher from
104 /// `self` and the `haystack` to search in.
105fn into_searcher(self, haystack: &str) -> Self::Searcher<'_>;
106107/// Checks whether the pattern matches anywhere in the haystack
108#[inline]
109fn is_contained_in(self, haystack: &str) -> bool {
110self.into_searcher(haystack).next_match().is_some()
111 }
112113/// Checks whether the pattern matches at the front of the haystack
114#[inline]
115fn is_prefix_of(self, haystack: &str) -> bool {
116#[allow(non_exhaustive_omitted_patterns)] match self.into_searcher(haystack).next()
{
SearchStep::Match(0, _) => true,
_ => false,
}matches!(self.into_searcher(haystack).next(), SearchStep::Match(0, _))117 }
118119/// Checks whether the pattern matches at the back of the haystack
120#[inline]
121fn is_suffix_of<'a>(self, haystack: &'a str) -> bool122where
123Self::Searcher<'a>: ReverseSearcher<'a>,
124 {
125#[allow(non_exhaustive_omitted_patterns)] match self.into_searcher(haystack).next_back()
{
SearchStep::Match(_, j) if haystack.len() == j => true,
_ => false,
}matches!(self.into_searcher(haystack).next_back(), SearchStep::Match(_, j) if haystack.len() == j)126 }
127128/// Removes the pattern from the front of haystack, if it matches.
129#[inline]
130fn strip_prefix_of(self, haystack: &str) -> Option<&str> {
131if let SearchStep::Match(start, len) = self.into_searcher(haystack).next() {
132if true {
match (&start, &0) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = crate::panicking::AssertKind::Eq;
crate::panicking::assert_failed(kind, &*left_val, &*right_val,
crate::option::Option::Some(format_args!("The first search step from Searcher must include the first character")));
}
}
};
};debug_assert_eq!(
133 start, 0,
134"The first search step from Searcher \
135 must include the first character"
136);
137// SAFETY: `Searcher` is known to return valid indices.
138unsafe { Some(haystack.get_unchecked(len..)) }
139 } else {
140None141 }
142 }
143144/// Removes the pattern from the back of haystack, if it matches.
145#[inline]
146fn strip_suffix_of<'a>(self, haystack: &'a str) -> Option<&'a str>
147where
148Self::Searcher<'a>: ReverseSearcher<'a>,
149 {
150if let SearchStep::Match(start, end) = self.into_searcher(haystack).next_back() {
151if true {
match (&end, &haystack.len()) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = crate::panicking::AssertKind::Eq;
crate::panicking::assert_failed(kind, &*left_val, &*right_val,
crate::option::Option::Some(format_args!("The first search step from ReverseSearcher must include the last character")));
}
}
};
};debug_assert_eq!(
152 end,
153 haystack.len(),
154"The first search step from ReverseSearcher \
155 must include the last character"
156);
157// SAFETY: `Searcher` is known to return valid indices.
158unsafe { Some(haystack.get_unchecked(..start)) }
159 } else {
160None161 }
162 }
163164/// Returns the pattern as utf-8 bytes if possible.
165fn as_utf8_pattern(&self) -> Option<Utf8Pattern<'_>> {
166None167 }
168}
169/// Result of calling [`Pattern::as_utf8_pattern()`].
170/// Can be used for inspecting the contents of a [`Pattern`] in cases
171/// where the underlying representation can be represented as UTF-8.
172#[derive(#[automatically_derived]
impl<'a> crate::marker::Copy for Utf8Pattern<'a> { }Copy, #[automatically_derived]
impl<'a> crate::clone::Clone for Utf8Pattern<'a> {
#[inline]
fn clone(&self) -> Utf8Pattern<'a> {
let _: crate::clone::AssertParamIsClone<&'a [u8]>;
let _: crate::clone::AssertParamIsClone<char>;
*self
}
}Clone, #[automatically_derived]
impl<'a> crate::cmp::Eq for Utf8Pattern<'a> {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {
let _: crate::cmp::AssertParamIsEq<&'a [u8]>;
let _: crate::cmp::AssertParamIsEq<char>;
}
}Eq, #[automatically_derived]
impl<'a> crate::cmp::PartialEq for Utf8Pattern<'a> {
#[inline]
fn eq(&self, other: &Utf8Pattern<'a>) -> bool {
let __self_discr = crate::intrinsics::discriminant_value(self);
let __arg1_discr = crate::intrinsics::discriminant_value(other);
__self_discr == __arg1_discr &&
match (self, other) {
(Utf8Pattern::StringPattern(__self_0),
Utf8Pattern::StringPattern(__arg1_0)) =>
__self_0 == __arg1_0,
(Utf8Pattern::CharPattern(__self_0),
Utf8Pattern::CharPattern(__arg1_0)) => __self_0 == __arg1_0,
_ => unsafe { crate::intrinsics::unreachable() }
}
}
}PartialEq, #[automatically_derived]
impl<'a> crate::fmt::Debug for Utf8Pattern<'a> {
#[inline]
fn fmt(&self, f: &mut crate::fmt::Formatter) -> crate::fmt::Result {
match self {
Utf8Pattern::StringPattern(__self_0) =>
crate::fmt::Formatter::debug_tuple_field1_finish(f,
"StringPattern", &__self_0),
Utf8Pattern::CharPattern(__self_0) =>
crate::fmt::Formatter::debug_tuple_field1_finish(f,
"CharPattern", &__self_0),
}
}
}Debug)]
173pub enum Utf8Pattern<'a> {
174/// Type returned by String and str types.
175StringPattern(&'a [u8]),
176/// Type returned by char types.
177CharPattern(char),
178}
179180// Searcher
181182/// Result of calling [`Searcher::next()`] or [`ReverseSearcher::next_back()`].
183#[derive(#[automatically_derived]
impl crate::marker::Copy for SearchStep { }Copy, #[automatically_derived]
impl crate::clone::Clone for SearchStep {
#[inline]
fn clone(&self) -> SearchStep {
let _: crate::clone::AssertParamIsClone<usize>;
*self
}
}Clone, #[automatically_derived]
impl crate::cmp::Eq for SearchStep {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {
let _: crate::cmp::AssertParamIsEq<usize>;
}
}Eq, #[automatically_derived]
impl crate::cmp::PartialEq for SearchStep {
#[inline]
fn eq(&self, other: &SearchStep) -> bool {
let __self_discr = crate::intrinsics::discriminant_value(self);
let __arg1_discr = crate::intrinsics::discriminant_value(other);
__self_discr == __arg1_discr &&
match (self, other) {
(SearchStep::Match(__self_0, __self_1),
SearchStep::Match(__arg1_0, __arg1_1)) =>
__self_0 == __arg1_0 && __self_1 == __arg1_1,
(SearchStep::Reject(__self_0, __self_1),
SearchStep::Reject(__arg1_0, __arg1_1)) =>
__self_0 == __arg1_0 && __self_1 == __arg1_1,
_ => true,
}
}
}PartialEq, #[automatically_derived]
impl crate::fmt::Debug for SearchStep {
#[inline]
fn fmt(&self, f: &mut crate::fmt::Formatter) -> crate::fmt::Result {
match self {
SearchStep::Match(__self_0, __self_1) =>
crate::fmt::Formatter::debug_tuple_field2_finish(f, "Match",
__self_0, &__self_1),
SearchStep::Reject(__self_0, __self_1) =>
crate::fmt::Formatter::debug_tuple_field2_finish(f, "Reject",
__self_0, &__self_1),
SearchStep::Done => crate::fmt::Formatter::write_str(f, "Done"),
}
}
}Debug)]
184pub enum SearchStep {
185/// Expresses that a match of the pattern has been found at
186 /// `haystack[a..b]`.
187Match(usize, usize),
188/// Expresses that `haystack[a..b]` has been rejected as a possible match
189 /// of the pattern.
190 ///
191 /// Note that there might be more than one `Reject` between two `Match`es,
192 /// there is no requirement for them to be combined into one.
193Reject(usize, usize),
194/// Expresses that every byte of the haystack has been visited, ending
195 /// the iteration.
196Done,
197}
198199/// A searcher for a string pattern.
200///
201/// This trait provides methods for searching for non-overlapping
202/// matches of a pattern starting from the front (left) of a string.
203///
204/// It will be implemented by associated `Searcher`
205/// types of the [`Pattern`] trait.
206///
207/// The trait is marked unsafe because the indices returned by the
208/// [`next()`][Searcher::next] methods are required to lie on valid utf8
209/// boundaries in the haystack. This enables consumers of this trait to
210/// slice the haystack without additional runtime checks.
211pub unsafe trait Searcher<'a> {
212/// Getter for the underlying string to be searched in
213 ///
214 /// Will always return the same [`&str`][str].
215fn haystack(&self) -> &'a str;
216217/// Performs the next search step starting from the front.
218 ///
219 /// - Returns [`Match(a, b)`][SearchStep::Match] if `haystack[a..b]` matches
220 /// the pattern.
221 /// - Returns [`Reject(a, b)`][SearchStep::Reject] if `haystack[a..b]` can
222 /// not match the pattern, even partially.
223 /// - Returns [`Done`][SearchStep::Done] if every byte of the haystack has
224 /// been visited.
225 ///
226 /// The stream of [`Match`][SearchStep::Match] and
227 /// [`Reject`][SearchStep::Reject] values up to a [`Done`][SearchStep::Done]
228 /// will contain index ranges that are adjacent, non-overlapping,
229 /// covering the whole haystack, and laying on utf8 boundaries.
230 ///
231 /// A [`Match`][SearchStep::Match] result needs to contain the whole matched
232 /// pattern, however [`Reject`][SearchStep::Reject] results may be split up
233 /// into arbitrary many adjacent fragments. Both ranges may have zero length.
234 ///
235 /// As an example, the pattern `"aaa"` and the haystack `"cbaaaaab"`
236 /// might produce the stream
237 /// `[Reject(0, 1), Reject(1, 2), Match(2, 5), Reject(5, 8)]`
238fn next(&mut self) -> SearchStep;
239240/// Finds the next [`Match`][SearchStep::Match] result. See [`next()`][Searcher::next].
241 ///
242 /// Unlike [`next()`][Searcher::next], there is no guarantee that the returned ranges
243 /// of this and [`next_reject`][Searcher::next_reject] will overlap. This will return
244 /// `(start_match, end_match)`, where start_match is the index of where
245 /// the match begins, and end_match is the index after the end of the match.
246#[inline]
247fn next_match(&mut self) -> Option<(usize, usize)> {
248loop {
249match self.next() {
250 SearchStep::Match(a, b) => return Some((a, b)),
251 SearchStep::Done => return None,
252_ => continue,
253 }
254 }
255 }
256257/// Finds the next [`Reject`][SearchStep::Reject] result. See [`next()`][Searcher::next]
258 /// and [`next_match()`][Searcher::next_match].
259 ///
260 /// Unlike [`next()`][Searcher::next], there is no guarantee that the returned ranges
261 /// of this and [`next_match`][Searcher::next_match] will overlap.
262#[inline]
263fn next_reject(&mut self) -> Option<(usize, usize)> {
264loop {
265match self.next() {
266 SearchStep::Reject(a, b) => return Some((a, b)),
267 SearchStep::Done => return None,
268_ => continue,
269 }
270 }
271 }
272}
273274/// A reverse searcher for a string pattern.
275///
276/// This trait provides methods for searching for non-overlapping
277/// matches of a pattern starting from the back (right) of a string.
278///
279/// It will be implemented by associated [`Searcher`]
280/// types of the [`Pattern`] trait if the pattern supports searching
281/// for it from the back.
282///
283/// The index ranges returned by this trait are not required
284/// to exactly match those of the forward search in reverse.
285///
286/// For the reason why this trait is marked unsafe, see the
287/// parent trait [`Searcher`].
288pub unsafe trait ReverseSearcher<'a>: Searcher<'a> {
289/// Performs the next search step starting from the back.
290 ///
291 /// - Returns [`Match(a, b)`][SearchStep::Match] if `haystack[a..b]`
292 /// matches the pattern.
293 /// - Returns [`Reject(a, b)`][SearchStep::Reject] if `haystack[a..b]`
294 /// can not match the pattern, even partially.
295 /// - Returns [`Done`][SearchStep::Done] if every byte of the haystack
296 /// has been visited
297 ///
298 /// The stream of [`Match`][SearchStep::Match] and
299 /// [`Reject`][SearchStep::Reject] values up to a [`Done`][SearchStep::Done]
300 /// will contain index ranges that are adjacent, non-overlapping,
301 /// covering the whole haystack, and laying on utf8 boundaries.
302 ///
303 /// A [`Match`][SearchStep::Match] result needs to contain the whole matched
304 /// pattern, however [`Reject`][SearchStep::Reject] results may be split up
305 /// into arbitrary many adjacent fragments. Both ranges may have zero length.
306 ///
307 /// As an example, the pattern `"aaa"` and the haystack `"cbaaaaab"`
308 /// might produce the stream
309 /// `[Reject(7, 8), Match(4, 7), Reject(1, 4), Reject(0, 1)]`.
310fn next_back(&mut self) -> SearchStep;
311312/// Finds the next [`Match`][SearchStep::Match] result.
313 /// See [`next_back()`][ReverseSearcher::next_back].
314#[inline]
315fn next_match_back(&mut self) -> Option<(usize, usize)> {
316loop {
317match self.next_back() {
318 SearchStep::Match(a, b) => return Some((a, b)),
319 SearchStep::Done => return None,
320_ => continue,
321 }
322 }
323 }
324325/// Finds the next [`Reject`][SearchStep::Reject] result.
326 /// See [`next_back()`][ReverseSearcher::next_back].
327#[inline]
328fn next_reject_back(&mut self) -> Option<(usize, usize)> {
329loop {
330match self.next_back() {
331 SearchStep::Reject(a, b) => return Some((a, b)),
332 SearchStep::Done => return None,
333_ => continue,
334 }
335 }
336 }
337}
338339/// A marker trait to express that a [`ReverseSearcher`]
340/// can be used for a [`DoubleEndedIterator`] implementation.
341///
342/// For this, the impl of [`Searcher`] and [`ReverseSearcher`] need
343/// to follow these conditions:
344///
345/// - All results of `next()` need to be identical
346/// to the results of `next_back()` in reverse order.
347/// - `next()` and `next_back()` need to behave as
348/// the two ends of a range of values, that is they
349/// can not "walk past each other".
350///
351/// # Examples
352///
353/// `char::Searcher` is a `DoubleEndedSearcher` because searching for a
354/// [`char`] only requires looking at one at a time, which behaves the same
355/// from both ends.
356///
357/// `(&str)::Searcher` is not a `DoubleEndedSearcher` because
358/// the pattern `"aa"` in the haystack `"aaa"` matches as either
359/// `"[aa]a"` or `"a[aa]"`, depending on which side it is searched.
360pub trait DoubleEndedSearcher<'a>: ReverseSearcher<'a> {}
361362/////////////////////////////////////////////////////////////////////////////
363// Impl for char
364/////////////////////////////////////////////////////////////////////////////
365366/// Associated type for `<char as Pattern>::Searcher<'a>`.
367#[derive(#[automatically_derived]
impl<'a> crate::clone::Clone for CharSearcher<'a> {
#[inline]
fn clone(&self) -> CharSearcher<'a> {
CharSearcher {
haystack: crate::clone::Clone::clone(&self.haystack),
finger: crate::clone::Clone::clone(&self.finger),
finger_back: crate::clone::Clone::clone(&self.finger_back),
needle: crate::clone::Clone::clone(&self.needle),
utf8_size: crate::clone::Clone::clone(&self.utf8_size),
utf8_encoded: crate::clone::Clone::clone(&self.utf8_encoded),
}
}
}Clone, #[automatically_derived]
impl<'a> crate::fmt::Debug for CharSearcher<'a> {
#[inline]
fn fmt(&self, f: &mut crate::fmt::Formatter) -> crate::fmt::Result {
let names: &'static _ =
&["haystack", "finger", "finger_back", "needle", "utf8_size",
"utf8_encoded"];
let values: &[&dyn crate::fmt::Debug] =
&[&self.haystack, &self.finger, &self.finger_back, &self.needle,
&self.utf8_size, &&self.utf8_encoded];
crate::fmt::Formatter::debug_struct_fields_finish(f, "CharSearcher",
names, values)
}
}Debug)]
368pub struct CharSearcher<'a> {
369 haystack: &'a str,
370// safety invariant: `finger`/`finger_back` must be a valid utf8 byte index of `haystack`
371 // This invariant can be broken *within* next_match and next_match_back, however
372 // they must exit with fingers on valid code point boundaries.
373/// `finger` is the current byte index of the forward search.
374 /// Imagine that it exists before the byte at its index, i.e.
375 /// `haystack[finger]` is the first byte of the slice we must inspect during
376 /// forward searching
377finger: usize,
378/// `finger_back` is the current byte index of the reverse search.
379 /// Imagine that it exists after the byte at its index, i.e.
380 /// haystack[finger_back - 1] is the last byte of the slice we must inspect during
381 /// forward searching (and thus the first byte to be inspected when calling next_back()).
382finger_back: usize,
383/// The character being searched for
384needle: char,
385386// safety invariant: `utf8_size` must be less than 5
387/// The number of bytes `needle` takes up when encoded in utf8.
388utf8_size: u8,
389/// A utf8 encoded copy of the `needle`
390utf8_encoded: [u8; 4],
391}
392393impl CharSearcher<'_> {
394fn utf8_size(&self) -> usize {
395self.utf8_size.into()
396 }
397}
398399unsafe impl<'a> Searcher<'a> for CharSearcher<'a> {
400#[inline]
401fn haystack(&self) -> &'a str {
402self.haystack
403 }
404#[inline]
405fn next(&mut self) -> SearchStep {
406let old_finger = self.finger;
407// SAFETY: 1-4 guarantee safety of `get_unchecked`
408 // 1. `self.finger` and `self.finger_back` are kept on unicode boundaries
409 // (this is invariant)
410 // 2. `self.finger >= 0` since it starts at 0 and only increases
411 // 3. `self.finger < self.finger_back` because otherwise the char `iter`
412 // would return `SearchStep::Done`
413 // 4. `self.finger` comes before the end of the haystack because `self.finger_back`
414 // starts at the end and only decreases
415let slice = unsafe { self.haystack.get_unchecked(old_finger..self.finger_back) };
416let mut iter = slice.chars();
417let old_len = iter.iter.len();
418if let Some(ch) = iter.next() {
419// add byte offset of current character
420 // without re-encoding as utf-8
421self.finger += old_len - iter.iter.len();
422if ch == self.needle {
423 SearchStep::Match(old_finger, self.finger)
424 } else {
425 SearchStep::Reject(old_finger, self.finger)
426 }
427 } else {
428 SearchStep::Done429 }
430 }
431#[inline]
432fn next_match(&mut self) -> Option<(usize, usize)> {
433loop {
434// get the haystack after the last character found
435let bytes = self.haystack.as_bytes().get(self.finger..self.finger_back)?;
436// the last byte of the utf8 encoded needle
437 // SAFETY: we have an invariant that `utf8_size < 5`
438let last_byte = unsafe { *self.utf8_encoded.get_unchecked(self.utf8_size() - 1) };
439if let Some(index) = memchr::memchr(last_byte, bytes) {
440// The new finger is the index of the byte we found,
441 // plus one, since we memchr'd for the last byte of the character.
442 //
443 // Note that this doesn't always give us a finger on a UTF8 boundary.
444 // If we *didn't* find our character
445 // we may have indexed to the non-last byte of a 3-byte or 4-byte character.
446 // We can't just skip to the next valid starting byte because a character like
447 // ꁁ (U+A041 YI SYLLABLE PA), utf-8 `EA 81 81` will have us always find
448 // the second byte when searching for the third.
449 //
450 // However, this is totally okay. While we have the invariant that
451 // self.finger is on a UTF8 boundary, this invariant is not relied upon
452 // within this method (it is relied upon in CharSearcher::next()).
453 //
454 // We only exit this method when we reach the end of the string, or if we
455 // find something. When we find something the `finger` will be set
456 // to a UTF8 boundary.
457self.finger += index + 1;
458if self.finger >= self.utf8_size() {
459let found_char = self.finger - self.utf8_size();
460if let Some(slice) = self.haystack.as_bytes().get(found_char..self.finger) {
461if slice == &self.utf8_encoded[0..self.utf8_size()] {
462return Some((found_char, self.finger));
463 }
464 }
465 }
466 } else {
467// found nothing, exit
468self.finger = self.finger_back;
469return None;
470 }
471 }
472 }
473474// let next_reject use the default implementation from the Searcher trait
475}
476477unsafe impl<'a> ReverseSearcher<'a> for CharSearcher<'a> {
478#[inline]
479fn next_back(&mut self) -> SearchStep {
480let old_finger = self.finger_back;
481// SAFETY: see the comment for next() above
482let slice = unsafe { self.haystack.get_unchecked(self.finger..old_finger) };
483let mut iter = slice.chars();
484let old_len = iter.iter.len();
485if let Some(ch) = iter.next_back() {
486// subtract byte offset of current character
487 // without re-encoding as utf-8
488self.finger_back -= old_len - iter.iter.len();
489if ch == self.needle {
490 SearchStep::Match(self.finger_back, old_finger)
491 } else {
492 SearchStep::Reject(self.finger_back, old_finger)
493 }
494 } else {
495 SearchStep::Done496 }
497 }
498#[inline]
499fn next_match_back(&mut self) -> Option<(usize, usize)> {
500let haystack = self.haystack.as_bytes();
501loop {
502// get the haystack up to but not including the last character searched
503let bytes = haystack.get(self.finger..self.finger_back)?;
504// the last byte of the utf8 encoded needle
505 // SAFETY: we have an invariant that `utf8_size < 5`
506let last_byte = unsafe { *self.utf8_encoded.get_unchecked(self.utf8_size() - 1) };
507if let Some(index) = memchr::memrchr(last_byte, bytes) {
508// we searched a slice that was offset by self.finger,
509 // add self.finger to recoup the original index
510let index = self.finger + index;
511// memrchr will return the index of the byte we wish to
512 // find. In case of an ASCII character, this is indeed
513 // were we wish our new finger to be ("after" the found
514 // char in the paradigm of reverse iteration). For
515 // multibyte chars we need to skip down by the number of more
516 // bytes they have than ASCII
517let shift = self.utf8_size() - 1;
518if index >= shift {
519let found_char = index - shift;
520if let Some(slice) = haystack.get(found_char..(found_char + self.utf8_size())) {
521if slice == &self.utf8_encoded[0..self.utf8_size()] {
522// move finger to before the character found (i.e., at its start index)
523self.finger_back = found_char;
524return Some((self.finger_back, self.finger_back + self.utf8_size()));
525 }
526 }
527 }
528// We can't use finger_back = index - size + 1 here. If we found the last char
529 // of a different-sized character (or the middle byte of a different character)
530 // we need to bump the finger_back down to `index`. This similarly makes
531 // `finger_back` have the potential to no longer be on a boundary,
532 // but this is OK since we only exit this function on a boundary
533 // or when the haystack has been searched completely.
534 //
535 // Unlike next_match this does not
536 // have the problem of repeated bytes in utf-8 because
537 // we're searching for the last byte, and we can only have
538 // found the last byte when searching in reverse.
539self.finger_back = index;
540 } else {
541self.finger_back = self.finger;
542// found nothing, exit
543return None;
544 }
545 }
546 }
547548// let next_reject_back use the default implementation from the Searcher trait
549}
550551impl<'a> DoubleEndedSearcher<'a> for CharSearcher<'a> {}
552553/// Searches for chars that are equal to a given [`char`].
554///
555/// # Examples
556///
557/// ```
558/// assert_eq!("Hello world".find('o'), Some(4));
559/// ```
560impl Patternfor char {
561type Searcher<'a> = CharSearcher<'a>;
562563#[inline]
564fn into_searcher<'a>(self, haystack: &'a str) -> Self::Searcher<'a> {
565let mut utf8_encoded = [0; char::MAX_LEN_UTF8];
566let utf8_size = self567 .encode_utf8(&mut utf8_encoded)
568 .len()
569 .try_into()
570 .expect("char len should be less than 255");
571572CharSearcher {
573haystack,
574 finger: 0,
575 finger_back: haystack.len(),
576 needle: self,
577utf8_size,
578utf8_encoded,
579 }
580 }
581582#[inline]
583fn is_contained_in(self, haystack: &str) -> bool {
584if (selfas u32) < 128 {
585haystack.as_bytes().contains(&(selfas u8))
586 } else {
587let mut buffer = [0u8; 4];
588self.encode_utf8(&mut buffer).is_contained_in(haystack)
589 }
590 }
591592#[inline]
593fn is_prefix_of(self, haystack: &str) -> bool {
594self.encode_utf8(&mut [0u8; 4]).is_prefix_of(haystack)
595 }
596597#[inline]
598fn strip_prefix_of(self, haystack: &str) -> Option<&str> {
599self.encode_utf8(&mut [0u8; 4]).strip_prefix_of(haystack)
600 }
601602#[inline]
603fn is_suffix_of<'a>(self, haystack: &'a str) -> bool604where
605Self::Searcher<'a>: ReverseSearcher<'a>,
606 {
607self.encode_utf8(&mut [0u8; 4]).is_suffix_of(haystack)
608 }
609610#[inline]
611fn strip_suffix_of<'a>(self, haystack: &'a str) -> Option<&'a str>
612where
613Self::Searcher<'a>: ReverseSearcher<'a>,
614 {
615self.encode_utf8(&mut [0u8; 4]).strip_suffix_of(haystack)
616 }
617618#[inline]
619fn as_utf8_pattern(&self) -> Option<Utf8Pattern<'_>> {
620Some(Utf8Pattern::CharPattern(*self))
621 }
622}
623624/////////////////////////////////////////////////////////////////////////////
625// Impl for a MultiCharEq wrapper
626/////////////////////////////////////////////////////////////////////////////
627628#[doc(hidden)]
629trait MultiCharEq {
630fn matches(&mut self, c: char) -> bool;
631}
632633impl<F> MultiCharEqfor F
634where
635F: FnMut(char) -> bool,
636{
637#[inline]
638fn matches(&mut self, c: char) -> bool {
639 (*self)(c)
640 }
641}
642643impl<const N: usize> MultiCharEqfor [char; N] {
644#[inline]
645fn matches(&mut self, c: char) -> bool {
646self.contains(&c)
647 }
648}
649650impl<const N: usize> MultiCharEqfor &[char; N] {
651#[inline]
652fn matches(&mut self, c: char) -> bool {
653self.contains(&c)
654 }
655}
656657impl MultiCharEqfor &[char] {
658#[inline]
659fn matches(&mut self, c: char) -> bool {
660self.contains(&c)
661 }
662}
663664struct MultiCharEqPattern<C: MultiCharEq>(C);
665666#[derive(#[automatically_derived]
impl<'a, C: crate::clone::Clone + MultiCharEq> crate::clone::Clone for
MultiCharEqSearcher<'a, C> {
#[inline]
fn clone(&self) -> MultiCharEqSearcher<'a, C> {
MultiCharEqSearcher {
char_eq: crate::clone::Clone::clone(&self.char_eq),
haystack: crate::clone::Clone::clone(&self.haystack),
char_indices: crate::clone::Clone::clone(&self.char_indices),
}
}
}Clone, #[automatically_derived]
impl<'a, C: crate::fmt::Debug + MultiCharEq> crate::fmt::Debug for
MultiCharEqSearcher<'a, C> {
#[inline]
fn fmt(&self, f: &mut crate::fmt::Formatter) -> crate::fmt::Result {
crate::fmt::Formatter::debug_struct_field3_finish(f,
"MultiCharEqSearcher", "char_eq", &self.char_eq, "haystack",
&self.haystack, "char_indices", &&self.char_indices)
}
}Debug)]
667struct MultiCharEqSearcher<'a, C: MultiCharEq> {
668 char_eq: C,
669 haystack: &'a str,
670 char_indices: super::CharIndices<'a>,
671}
672673impl<C: MultiCharEq> Patternfor MultiCharEqPattern<C> {
674type Searcher<'a> = MultiCharEqSearcher<'a, C>;
675676#[inline]
677fn into_searcher(self, haystack: &str) -> MultiCharEqSearcher<'_, C> {
678MultiCharEqSearcher { haystack, char_eq: self.0, char_indices: haystack.char_indices() }
679 }
680}
681682unsafe impl<'a, C: MultiCharEq> Searcher<'a> for MultiCharEqSearcher<'a, C> {
683#[inline]
684fn haystack(&self) -> &'a str {
685self.haystack
686 }
687688#[inline]
689fn next(&mut self) -> SearchStep {
690let s = &mut self.char_indices;
691// Compare lengths of the internal byte slice iterator
692 // to find length of current char
693let pre_len = s.iter.iter.len();
694if let Some((i, c)) = s.next() {
695let len = s.iter.iter.len();
696let char_len = pre_len - len;
697if self.char_eq.matches(c) {
698return SearchStep::Match(i, i + char_len);
699 } else {
700return SearchStep::Reject(i, i + char_len);
701 }
702 }
703 SearchStep::Done704 }
705}
706707unsafe impl<'a, C: MultiCharEq> ReverseSearcher<'a> for MultiCharEqSearcher<'a, C> {
708#[inline]
709fn next_back(&mut self) -> SearchStep {
710let s = &mut self.char_indices;
711// Compare lengths of the internal byte slice iterator
712 // to find length of current char
713let pre_len = s.iter.iter.len();
714if let Some((i, c)) = s.next_back() {
715let len = s.iter.iter.len();
716let char_len = pre_len - len;
717if self.char_eq.matches(c) {
718return SearchStep::Match(i, i + char_len);
719 } else {
720return SearchStep::Reject(i, i + char_len);
721 }
722 }
723 SearchStep::Done724 }
725}
726727impl<'a, C: MultiCharEq> DoubleEndedSearcher<'a> for MultiCharEqSearcher<'a, C> {}
728729/////////////////////////////////////////////////////////////////////////////
730731macro_rules!pattern_methods {
732 ($a:lifetime, $t:ty, $pmap:expr, $smap:expr) => {
733type Searcher<$a> = $t;
734735#[inline]
736fn into_searcher<$a>(self, haystack: &$a str) -> $t {
737 ($smap)(($pmap)(self).into_searcher(haystack))
738 }
739740#[inline]
741fn is_contained_in<$a>(self, haystack: &$a str) -> bool {
742 ($pmap)(self).is_contained_in(haystack)
743 }
744745#[inline]
746fn is_prefix_of<$a>(self, haystack: &$a str) -> bool {
747 ($pmap)(self).is_prefix_of(haystack)
748 }
749750#[inline]
751fn strip_prefix_of<$a>(self, haystack: &$a str) -> Option<&$a str> {
752 ($pmap)(self).strip_prefix_of(haystack)
753 }
754755#[inline]
756fn is_suffix_of<$a>(self, haystack: &$a str) -> bool757where
758$t: ReverseSearcher<$a>,
759 {
760 ($pmap)(self).is_suffix_of(haystack)
761 }
762763#[inline]
764fn strip_suffix_of<$a>(self, haystack: &$a str) -> Option<&$a str>
765where
766$t: ReverseSearcher<$a>,
767 {
768 ($pmap)(self).strip_suffix_of(haystack)
769 }
770 };
771}
772773macro_rules!searcher_methods {
774 (forward) => {
775#[inline]
776fn haystack(&self) -> &'a str {
777self.0.haystack()
778 }
779#[inline]
780fn next(&mut self) -> SearchStep {
781self.0.next()
782 }
783#[inline]
784fn next_match(&mut self) -> Option<(usize, usize)> {
785self.0.next_match()
786 }
787#[inline]
788fn next_reject(&mut self) -> Option<(usize, usize)> {
789self.0.next_reject()
790 }
791 };
792 (reverse) => {
793#[inline]
794fn next_back(&mut self) -> SearchStep {
795self.0.next_back()
796 }
797#[inline]
798fn next_match_back(&mut self) -> Option<(usize, usize)> {
799self.0.next_match_back()
800 }
801#[inline]
802fn next_reject_back(&mut self) -> Option<(usize, usize)> {
803self.0.next_reject_back()
804 }
805 };
806}
807808/// Associated type for `<[char; N] as Pattern>::Searcher<'a>`.
809#[derive(#[automatically_derived]
impl<'a, const N : usize> crate::clone::Clone for CharArraySearcher<'a, N> {
#[inline]
fn clone(&self) -> CharArraySearcher<'a, N> {
CharArraySearcher(crate::clone::Clone::clone(&self.0))
}
}Clone, #[automatically_derived]
impl<'a, const N : usize> crate::fmt::Debug for CharArraySearcher<'a, N> {
#[inline]
fn fmt(&self, f: &mut crate::fmt::Formatter) -> crate::fmt::Result {
crate::fmt::Formatter::debug_tuple_field1_finish(f,
"CharArraySearcher", &&self.0)
}
}Debug)]
810pub struct CharArraySearcher<'a, const N: usize>(
811 <MultiCharEqPattern<[char; N]> as Pattern>::Searcher<'a>,
812);
813814/// Associated type for `<&[char; N] as Pattern>::Searcher<'a>`.
815#[derive(#[automatically_derived]
impl<'a, 'b, const N : usize> crate::clone::Clone for
CharArrayRefSearcher<'a, 'b, N> {
#[inline]
fn clone(&self) -> CharArrayRefSearcher<'a, 'b, N> {
CharArrayRefSearcher(crate::clone::Clone::clone(&self.0))
}
}Clone, #[automatically_derived]
impl<'a, 'b, const N : usize> crate::fmt::Debug for
CharArrayRefSearcher<'a, 'b, N> {
#[inline]
fn fmt(&self, f: &mut crate::fmt::Formatter) -> crate::fmt::Result {
crate::fmt::Formatter::debug_tuple_field1_finish(f,
"CharArrayRefSearcher", &&self.0)
}
}Debug)]
816pub struct CharArrayRefSearcher<'a, 'b, const N: usize>(
817 <MultiCharEqPattern<&'b [char; N]> as Pattern>::Searcher<'a>,
818);
819820/// Searches for chars that are equal to any of the [`char`]s in the array.
821///
822/// # Examples
823///
824/// ```
825/// assert_eq!("Hello world".find(['o', 'l']), Some(2));
826/// assert_eq!("Hello world".find(['h', 'w']), Some(6));
827/// ```
828impl<const N: usize> Patternfor [char; N] {
829self
&'a str
haystack
(CharArraySearcher)((MultiCharEqPattern)(self).into_searcher(haystack));
Self
self
&'a str
haystack
bool
(MultiCharEqPattern)(self).is_contained_in(haystack);
Self
self
&'a str
haystack
bool
(MultiCharEqPattern)(self).is_prefix_of(haystack);
Self
self
&'a str
haystack
Option<&'a str>
(MultiCharEqPattern)(self).strip_prefix_of(haystack);
Self
self
&'a str
haystack
bool
(MultiCharEqPattern)(self).is_suffix_of(haystack);
Self
self
&'a str
haystack
Option<&'a str>
(MultiCharEqPattern)(self).strip_suffix_of(haystack);pattern_methods!('a, CharArraySearcher<'a, N>, MultiCharEqPattern, CharArraySearcher);
830}
831832unsafe impl<'a, const N: usize> Searcher<'a> for CharArraySearcher<'a, N> {
833self
&'a str
self.0.haystack();
&mut Self
self
SearchStep
self.0.next();
&mut Self
self
Option<(usize, usize)>
self.0.next_match();
&mut Self
self
Option<(usize, usize)>
self.0.next_reject();searcher_methods!(forward);
834}
835836unsafe impl<'a, const N: usize> ReverseSearcher<'a> for CharArraySearcher<'a, N> {
837self
SearchStep
self.0.next_back();
&mut Self
self
Option<(usize, usize)>
self.0.next_match_back();
&mut Self
self
Option<(usize, usize)>
self.0.next_reject_back();searcher_methods!(reverse);
838}
839840impl<'a, const N: usize> DoubleEndedSearcher<'a> for CharArraySearcher<'a, N> {}
841842/// Searches for chars that are equal to any of the [`char`]s in the array.
843///
844/// # Examples
845///
846/// ```
847/// assert_eq!("Hello world".find(&['o', 'l']), Some(2));
848/// assert_eq!("Hello world".find(&['h', 'w']), Some(6));
849/// ```
850impl<'b, const N: usize> Patternfor &'b [char; N] {
851self
&'a str
haystack
(CharArrayRefSearcher)((MultiCharEqPattern)(self).into_searcher(haystack));
Self
self
&'a str
haystack
bool
(MultiCharEqPattern)(self).is_contained_in(haystack);
Self
self
&'a str
haystack
bool
(MultiCharEqPattern)(self).is_prefix_of(haystack);
Self
self
&'a str
haystack
Option<&'a str>
(MultiCharEqPattern)(self).strip_prefix_of(haystack);
Self
self
&'a str
haystack
bool
(MultiCharEqPattern)(self).is_suffix_of(haystack);
Self
self
&'a str
haystack
Option<&'a str>
(MultiCharEqPattern)(self).strip_suffix_of(haystack);pattern_methods!('a, CharArrayRefSearcher<'a, 'b, N>, MultiCharEqPattern, CharArrayRefSearcher);
852}
853854unsafe impl<'a, 'b, const N: usize> Searcher<'a> for CharArrayRefSearcher<'a, 'b, N> {
855self
&'a str
self.0.haystack();
&mut Self
self
SearchStep
self.0.next();
&mut Self
self
Option<(usize, usize)>
self.0.next_match();
&mut Self
self
Option<(usize, usize)>
self.0.next_reject();searcher_methods!(forward);
856}
857858unsafe impl<'a, 'b, const N: usize> ReverseSearcher<'a> for CharArrayRefSearcher<'a, 'b, N> {
859self
SearchStep
self.0.next_back();
&mut Self
self
Option<(usize, usize)>
self.0.next_match_back();
&mut Self
self
Option<(usize, usize)>
self.0.next_reject_back();searcher_methods!(reverse);
860}
861862impl<'a, 'b, const N: usize> DoubleEndedSearcher<'a> for CharArrayRefSearcher<'a, 'b, N> {}
863864/////////////////////////////////////////////////////////////////////////////
865// Impl for &[char]
866/////////////////////////////////////////////////////////////////////////////
867868// Todo: Change / Remove due to ambiguity in meaning.
869870/// Associated type for `<&[char] as Pattern>::Searcher<'a>`.
871#[derive(#[automatically_derived]
impl<'a, 'b> crate::clone::Clone for CharSliceSearcher<'a, 'b> {
#[inline]
fn clone(&self) -> CharSliceSearcher<'a, 'b> {
CharSliceSearcher(crate::clone::Clone::clone(&self.0))
}
}Clone, #[automatically_derived]
impl<'a, 'b> crate::fmt::Debug for CharSliceSearcher<'a, 'b> {
#[inline]
fn fmt(&self, f: &mut crate::fmt::Formatter) -> crate::fmt::Result {
crate::fmt::Formatter::debug_tuple_field1_finish(f,
"CharSliceSearcher", &&self.0)
}
}Debug)]
872pub struct CharSliceSearcher<'a, 'b>(<MultiCharEqPattern<&'b [char]> as Pattern>::Searcher<'a>);
873874unsafe impl<'a, 'b> Searcher<'a> for CharSliceSearcher<'a, 'b> {
875self
&'a str
self.0.haystack();
&mut Self
self
SearchStep
self.0.next();
&mut Self
self
Option<(usize, usize)>
self.0.next_match();
&mut Self
self
Option<(usize, usize)>
self.0.next_reject();searcher_methods!(forward);
876}
877878unsafe impl<'a, 'b> ReverseSearcher<'a> for CharSliceSearcher<'a, 'b> {
879self
SearchStep
self.0.next_back();
&mut Self
self
Option<(usize, usize)>
self.0.next_match_back();
&mut Self
self
Option<(usize, usize)>
self.0.next_reject_back();searcher_methods!(reverse);
880}
881882impl<'a, 'b> DoubleEndedSearcher<'a> for CharSliceSearcher<'a, 'b> {}
883884/// Searches for chars that are equal to any of the [`char`]s in the slice.
885///
886/// # Examples
887///
888/// ```
889/// assert_eq!("Hello world".find(&['o', 'l'][..]), Some(2));
890/// assert_eq!("Hello world".find(&['h', 'w'][..]), Some(6));
891/// ```
892impl<'b> Patternfor &'b [char] {
893self
&'a str
haystack
(CharSliceSearcher)((MultiCharEqPattern)(self).into_searcher(haystack));
Self
self
&'a str
haystack
bool
(MultiCharEqPattern)(self).is_contained_in(haystack);
Self
self
&'a str
haystack
bool
(MultiCharEqPattern)(self).is_prefix_of(haystack);
Self
self
&'a str
haystack
Option<&'a str>
(MultiCharEqPattern)(self).strip_prefix_of(haystack);
Self
self
&'a str
haystack
bool
(MultiCharEqPattern)(self).is_suffix_of(haystack);
Self
self
&'a str
haystack
Option<&'a str>
(MultiCharEqPattern)(self).strip_suffix_of(haystack);pattern_methods!('a, CharSliceSearcher<'a, 'b>, MultiCharEqPattern, CharSliceSearcher);
894}
895896/////////////////////////////////////////////////////////////////////////////
897// Impl for F: FnMut(char) -> bool
898/////////////////////////////////////////////////////////////////////////////
899900/// Associated type for `<F as Pattern>::Searcher<'a>`.
901#[derive(#[automatically_derived]
impl<'a, F: crate::clone::Clone> crate::clone::Clone for
CharPredicateSearcher<'a, F> where F: FnMut(char) -> bool {
#[inline]
fn clone(&self) -> CharPredicateSearcher<'a, F> {
CharPredicateSearcher(crate::clone::Clone::clone(&self.0))
}
}Clone)]
902pub struct CharPredicateSearcher<'a, F>(<MultiCharEqPattern<F> as Pattern>::Searcher<'a>)
903where
904F: FnMut(char) -> bool;
905906impl<F> fmt::Debugfor CharPredicateSearcher<'_, F>
907where
908F: FnMut(char) -> bool,
909{
910fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
911f.debug_struct("CharPredicateSearcher")
912 .field("haystack", &self.0.haystack)
913 .field("char_indices", &self.0.char_indices)
914 .finish()
915 }
916}
917unsafe impl<'a, F> Searcher<'a> for CharPredicateSearcher<'a, F>
918where
919F: FnMut(char) -> bool,
920{
921self
&'a str
self.0.haystack();
&mut Self
self
SearchStep
self.0.next();
&mut Self
self
Option<(usize, usize)>
self.0.next_match();
&mut Self
self
Option<(usize, usize)>
self.0.next_reject();searcher_methods!(forward);
922}
923924unsafe impl<'a, F> ReverseSearcher<'a> for CharPredicateSearcher<'a, F>
925where
926F: FnMut(char) -> bool,
927{
928self
SearchStep
self.0.next_back();
&mut Self
self
Option<(usize, usize)>
self.0.next_match_back();
&mut Self
self
Option<(usize, usize)>
self.0.next_reject_back();searcher_methods!(reverse);
929}
930931impl<'a, F> DoubleEndedSearcher<'a> for CharPredicateSearcher<'a, F> where F: FnMut(char) -> bool {}
932933/// Searches for [`char`]s that match the given predicate.
934///
935/// # Examples
936///
937/// ```
938/// assert_eq!("Hello world".find(char::is_uppercase), Some(0));
939/// assert_eq!("Hello world".find(|c| "aeiou".contains(c)), Some(1));
940/// ```
941impl<F> Patternfor F
942where
943F: FnMut(char) -> bool,
944{
945self
&'a str
haystack
(CharPredicateSearcher)((MultiCharEqPattern)(self).into_searcher(haystack));
Self
self
&'a str
haystack
bool
(MultiCharEqPattern)(self).is_contained_in(haystack);
Self
self
&'a str
haystack
bool
(MultiCharEqPattern)(self).is_prefix_of(haystack);
Self
self
&'a str
haystack
Option<&'a str>
(MultiCharEqPattern)(self).strip_prefix_of(haystack);
Self
self
&'a str
haystack
bool
(MultiCharEqPattern)(self).is_suffix_of(haystack);
Self
self
&'a str
haystack
Option<&'a str>
(MultiCharEqPattern)(self).strip_suffix_of(haystack);pattern_methods!('a, CharPredicateSearcher<'a, F>, MultiCharEqPattern, CharPredicateSearcher);
946}
947948/////////////////////////////////////////////////////////////////////////////
949// Impl for &&str
950/////////////////////////////////////////////////////////////////////////////
951952/// Delegates to the `&str` impl.
953impl<'b, 'c> Patternfor &'c &'b str {
954self
&'a str
haystack
(|s| s)((|&s| s)(self).into_searcher(haystack));
Self
self
&'a str
haystack
bool
(|&s| s)(self).is_contained_in(haystack);
Self
self
&'a str
haystack
bool
(|&s| s)(self).is_prefix_of(haystack);
Self
self
&'a str
haystack
Option<&'a str>
(|&s| s)(self).strip_prefix_of(haystack);
Self
self
&'a str
haystack
bool
(|&s| s)(self).is_suffix_of(haystack);
Self
self
&'a str
haystack
Option<&'a str>
(|&s| s)(self).strip_suffix_of(haystack);pattern_methods!('a, StrSearcher<'a, 'b>, |&s| s, |s| s);
955}
956957/////////////////////////////////////////////////////////////////////////////
958// Impl for &str
959/////////////////////////////////////////////////////////////////////////////
960961/// Non-allocating substring search.
962///
963/// Will handle the pattern `""` as returning empty matches at each character
964/// boundary.
965///
966/// # Examples
967///
968/// ```
969/// assert_eq!("Hello world".find("world"), Some(6));
970/// ```
971impl<'b> Patternfor &'b str {
972type Searcher<'a> = StrSearcher<'a, 'b>;
973974#[inline]
975fn into_searcher(self, haystack: &str) -> StrSearcher<'_, 'b> {
976StrSearcher::new(haystack, self)
977 }
978979/// Checks whether the pattern matches at the front of the haystack.
980#[inline]
981fn is_prefix_of(self, haystack: &str) -> bool {
982haystack.as_bytes().starts_with(self.as_bytes())
983 }
984985/// Checks whether the pattern matches anywhere in the haystack
986#[inline]
987fn is_contained_in(self, haystack: &str) -> bool {
988if self.len() == 0 {
989return true;
990 }
991992match self.len().cmp(&haystack.len()) {
993 Ordering::Less => {
994if self.len() == 1 {
995return haystack.as_bytes().contains(&self.as_bytes()[0]);
996 }
997998#[cfg(any(
999 all(target_arch = "x86_64", target_feature = "sse2"),
1000 all(target_arch = "loongarch64", target_feature = "lsx"),
1001 all(target_arch = "aarch64", target_feature = "neon")
1002 ))]
1003if self.len() <= 32 {
1004if let Some(result) = simd_contains(self, haystack) {
1005return result;
1006 }
1007 }
10081009self.into_searcher(haystack).next_match().is_some()
1010 }
1011_ => self == haystack,
1012 }
1013 }
10141015/// Removes the pattern from the front of haystack, if it matches.
1016#[inline]
1017fn strip_prefix_of(self, haystack: &str) -> Option<&str> {
1018if self.is_prefix_of(haystack) {
1019// SAFETY: prefix was just verified to exist.
1020unsafe { Some(haystack.get_unchecked(self.as_bytes().len()..)) }
1021 } else {
1022None1023 }
1024 }
10251026/// Checks whether the pattern matches at the back of the haystack.
1027#[inline]
1028fn is_suffix_of<'a>(self, haystack: &'a str) -> bool1029where
1030Self::Searcher<'a>: ReverseSearcher<'a>,
1031 {
1032haystack.as_bytes().ends_with(self.as_bytes())
1033 }
10341035/// Removes the pattern from the back of haystack, if it matches.
1036#[inline]
1037fn strip_suffix_of<'a>(self, haystack: &'a str) -> Option<&'a str>
1038where
1039Self::Searcher<'a>: ReverseSearcher<'a>,
1040 {
1041if self.is_suffix_of(haystack) {
1042let i = haystack.len() - self.as_bytes().len();
1043// SAFETY: suffix was just verified to exist.
1044unsafe { Some(haystack.get_unchecked(..i)) }
1045 } else {
1046None1047 }
1048 }
10491050#[inline]
1051fn as_utf8_pattern(&self) -> Option<Utf8Pattern<'_>> {
1052Some(Utf8Pattern::StringPattern(self.as_bytes()))
1053 }
1054}
10551056/////////////////////////////////////////////////////////////////////////////
1057// Two Way substring searcher
1058/////////////////////////////////////////////////////////////////////////////
10591060#[derive(#[automatically_derived]
impl<'a, 'b> crate::clone::Clone for StrSearcher<'a, 'b> {
#[inline]
fn clone(&self) -> StrSearcher<'a, 'b> {
StrSearcher {
haystack: crate::clone::Clone::clone(&self.haystack),
needle: crate::clone::Clone::clone(&self.needle),
searcher: crate::clone::Clone::clone(&self.searcher),
}
}
}Clone, #[automatically_derived]
impl<'a, 'b> crate::fmt::Debug for StrSearcher<'a, 'b> {
#[inline]
fn fmt(&self, f: &mut crate::fmt::Formatter) -> crate::fmt::Result {
crate::fmt::Formatter::debug_struct_field3_finish(f, "StrSearcher",
"haystack", &self.haystack, "needle", &self.needle, "searcher",
&&self.searcher)
}
}Debug)]
1061/// Associated type for `<&str as Pattern>::Searcher<'a>`.
1062pub struct StrSearcher<'a, 'b> {
1063 haystack: &'a str,
1064 needle: &'b str,
10651066 searcher: StrSearcherImpl,
1067}
10681069#[derive(#[automatically_derived]
impl crate::clone::Clone for StrSearcherImpl {
#[inline]
fn clone(&self) -> StrSearcherImpl {
match self {
StrSearcherImpl::Empty(__self_0) =>
StrSearcherImpl::Empty(crate::clone::Clone::clone(__self_0)),
StrSearcherImpl::TwoWay(__self_0) =>
StrSearcherImpl::TwoWay(crate::clone::Clone::clone(__self_0)),
}
}
}Clone, #[automatically_derived]
impl crate::fmt::Debug for StrSearcherImpl {
#[inline]
fn fmt(&self, f: &mut crate::fmt::Formatter) -> crate::fmt::Result {
match self {
StrSearcherImpl::Empty(__self_0) =>
crate::fmt::Formatter::debug_tuple_field1_finish(f, "Empty",
&__self_0),
StrSearcherImpl::TwoWay(__self_0) =>
crate::fmt::Formatter::debug_tuple_field1_finish(f, "TwoWay",
&__self_0),
}
}
}Debug)]
1070enum StrSearcherImpl {
1071 Empty(EmptyNeedle),
1072 TwoWay(TwoWaySearcher),
1073}
10741075#[derive(#[automatically_derived]
impl crate::clone::Clone for EmptyNeedle {
#[inline]
fn clone(&self) -> EmptyNeedle {
EmptyNeedle {
position: crate::clone::Clone::clone(&self.position),
end: crate::clone::Clone::clone(&self.end),
is_match_fw: crate::clone::Clone::clone(&self.is_match_fw),
is_match_bw: crate::clone::Clone::clone(&self.is_match_bw),
is_finished: crate::clone::Clone::clone(&self.is_finished),
}
}
}Clone, #[automatically_derived]
impl crate::fmt::Debug for EmptyNeedle {
#[inline]
fn fmt(&self, f: &mut crate::fmt::Formatter) -> crate::fmt::Result {
crate::fmt::Formatter::debug_struct_field5_finish(f, "EmptyNeedle",
"position", &self.position, "end", &self.end, "is_match_fw",
&self.is_match_fw, "is_match_bw", &self.is_match_bw,
"is_finished", &&self.is_finished)
}
}Debug)]
1076struct EmptyNeedle {
1077 position: usize,
1078 end: usize,
1079 is_match_fw: bool,
1080 is_match_bw: bool,
1081// Needed in case of an empty haystack, see #85462
1082is_finished: bool,
1083}
10841085impl<'a, 'b> StrSearcher<'a, 'b> {
1086fn new(haystack: &'a str, needle: &'b str) -> StrSearcher<'a, 'b> {
1087if needle.is_empty() {
1088StrSearcher {
1089haystack,
1090needle,
1091 searcher: StrSearcherImpl::Empty(EmptyNeedle {
1092 position: 0,
1093 end: haystack.len(),
1094 is_match_fw: true,
1095 is_match_bw: true,
1096 is_finished: false,
1097 }),
1098 }
1099 } else {
1100StrSearcher {
1101haystack,
1102needle,
1103 searcher: StrSearcherImpl::TwoWay(TwoWaySearcher::new(
1104needle.as_bytes(),
1105haystack.len(),
1106 )),
1107 }
1108 }
1109 }
1110}
11111112unsafe impl<'a, 'b> Searcher<'a> for StrSearcher<'a, 'b> {
1113#[inline]
1114fn haystack(&self) -> &'a str {
1115self.haystack
1116 }
11171118#[inline]
1119fn next(&mut self) -> SearchStep {
1120match self.searcher {
1121 StrSearcherImpl::Empty(ref mut searcher) => {
1122if searcher.is_finished {
1123return SearchStep::Done;
1124 }
1125// empty needle rejects every char and matches every empty string between them
1126let is_match = searcher.is_match_fw;
1127searcher.is_match_fw = !searcher.is_match_fw;
1128let pos = searcher.position;
1129match self.haystack[pos..].chars().next() {
1130_ if is_match => SearchStep::Match(pos, pos),
1131None => {
1132searcher.is_finished = true;
1133 SearchStep::Done1134 }
1135Some(ch) => {
1136searcher.position += ch.len_utf8();
1137 SearchStep::Reject(pos, searcher.position)
1138 }
1139 }
1140 }
1141 StrSearcherImpl::TwoWay(ref mut searcher) => {
1142// TwoWaySearcher produces valid *Match* indices that split at char boundaries
1143 // as long as it does correct matching and that haystack and needle are
1144 // valid UTF-8
1145 // *Rejects* from the algorithm can fall on any indices, but we will walk them
1146 // manually to the next character boundary, so that they are utf-8 safe.
1147if searcher.position == self.haystack.len() {
1148return SearchStep::Done;
1149 }
1150let is_long = searcher.memory == usize::MAX;
1151match searcher.next::<RejectAndMatch>(
1152self.haystack.as_bytes(),
1153self.needle.as_bytes(),
1154is_long,
1155 ) {
1156 SearchStep::Reject(a, mut b) => {
1157// skip to next char boundary
1158while !self.haystack.is_char_boundary(b) {
1159 b += 1;
1160 }
1161searcher.position = cmp::max(b, searcher.position);
1162 SearchStep::Reject(a, b)
1163 }
1164 otherwise => otherwise,
1165 }
1166 }
1167 }
1168 }
11691170#[inline]
1171fn next_match(&mut self) -> Option<(usize, usize)> {
1172match self.searcher {
1173 StrSearcherImpl::Empty(..) => loop {
1174match self.next() {
1175 SearchStep::Match(a, b) => return Some((a, b)),
1176 SearchStep::Done => return None,
1177 SearchStep::Reject(..) => {}
1178 }
1179 },
1180 StrSearcherImpl::TwoWay(ref mut searcher) => {
1181let is_long = searcher.memory == usize::MAX;
1182// write out `true` and `false` cases to encourage the compiler
1183 // to specialize the two cases separately.
1184if is_long {
1185searcher.next::<MatchOnly>(
1186self.haystack.as_bytes(),
1187self.needle.as_bytes(),
1188true,
1189 )
1190 } else {
1191searcher.next::<MatchOnly>(
1192self.haystack.as_bytes(),
1193self.needle.as_bytes(),
1194false,
1195 )
1196 }
1197 }
1198 }
1199 }
1200}
12011202unsafe impl<'a, 'b> ReverseSearcher<'a> for StrSearcher<'a, 'b> {
1203#[inline]
1204fn next_back(&mut self) -> SearchStep {
1205match self.searcher {
1206 StrSearcherImpl::Empty(ref mut searcher) => {
1207if searcher.is_finished {
1208return SearchStep::Done;
1209 }
1210let is_match = searcher.is_match_bw;
1211searcher.is_match_bw = !searcher.is_match_bw;
1212let end = searcher.end;
1213match self.haystack[..end].chars().next_back() {
1214_ if is_match => SearchStep::Match(end, end),
1215None => {
1216searcher.is_finished = true;
1217 SearchStep::Done1218 }
1219Some(ch) => {
1220searcher.end -= ch.len_utf8();
1221 SearchStep::Reject(searcher.end, end)
1222 }
1223 }
1224 }
1225 StrSearcherImpl::TwoWay(ref mut searcher) => {
1226if searcher.end == 0 {
1227return SearchStep::Done;
1228 }
1229let is_long = searcher.memory == usize::MAX;
1230match searcher.next_back::<RejectAndMatch>(
1231self.haystack.as_bytes(),
1232self.needle.as_bytes(),
1233is_long,
1234 ) {
1235 SearchStep::Reject(mut a, b) => {
1236// skip to next char boundary
1237while !self.haystack.is_char_boundary(a) {
1238 a -= 1;
1239 }
1240searcher.end = cmp::min(a, searcher.end);
1241 SearchStep::Reject(a, b)
1242 }
1243 otherwise => otherwise,
1244 }
1245 }
1246 }
1247 }
12481249#[inline]
1250fn next_match_back(&mut self) -> Option<(usize, usize)> {
1251match self.searcher {
1252 StrSearcherImpl::Empty(..) => loop {
1253match self.next_back() {
1254 SearchStep::Match(a, b) => return Some((a, b)),
1255 SearchStep::Done => return None,
1256 SearchStep::Reject(..) => {}
1257 }
1258 },
1259 StrSearcherImpl::TwoWay(ref mut searcher) => {
1260let is_long = searcher.memory == usize::MAX;
1261// write out `true` and `false`, like `next_match`
1262if is_long {
1263searcher.next_back::<MatchOnly>(
1264self.haystack.as_bytes(),
1265self.needle.as_bytes(),
1266true,
1267 )
1268 } else {
1269searcher.next_back::<MatchOnly>(
1270self.haystack.as_bytes(),
1271self.needle.as_bytes(),
1272false,
1273 )
1274 }
1275 }
1276 }
1277 }
1278}
12791280/// The internal state of the two-way substring search algorithm.
1281#[derive(#[automatically_derived]
impl crate::clone::Clone for TwoWaySearcher {
#[inline]
fn clone(&self) -> TwoWaySearcher {
TwoWaySearcher {
crit_pos: crate::clone::Clone::clone(&self.crit_pos),
crit_pos_back: crate::clone::Clone::clone(&self.crit_pos_back),
period: crate::clone::Clone::clone(&self.period),
byteset: crate::clone::Clone::clone(&self.byteset),
position: crate::clone::Clone::clone(&self.position),
end: crate::clone::Clone::clone(&self.end),
memory: crate::clone::Clone::clone(&self.memory),
memory_back: crate::clone::Clone::clone(&self.memory_back),
}
}
}Clone, #[automatically_derived]
impl crate::fmt::Debug for TwoWaySearcher {
#[inline]
fn fmt(&self, f: &mut crate::fmt::Formatter) -> crate::fmt::Result {
let names: &'static _ =
&["crit_pos", "crit_pos_back", "period", "byteset", "position",
"end", "memory", "memory_back"];
let values: &[&dyn crate::fmt::Debug] =
&[&self.crit_pos, &self.crit_pos_back, &self.period,
&self.byteset, &self.position, &self.end, &self.memory,
&&self.memory_back];
crate::fmt::Formatter::debug_struct_fields_finish(f, "TwoWaySearcher",
names, values)
}
}Debug)]
1282struct TwoWaySearcher {
1283// constants
1284/// critical factorization index
1285crit_pos: usize,
1286/// critical factorization index for reversed needle
1287crit_pos_back: usize,
1288 period: usize,
1289/// `byteset` is an extension (not part of the two way algorithm);
1290 /// it's a 64-bit "fingerprint" where each set bit `j` corresponds
1291 /// to a (byte & 63) == j present in the needle.
1292byteset: u64,
12931294// variables
1295position: usize,
1296 end: usize,
1297/// index into needle before which we have already matched
1298memory: usize,
1299/// index into needle after which we have already matched
1300memory_back: usize,
1301}
13021303/*
1304 This is the Two-Way search algorithm, which was introduced in the paper:
1305 Crochemore, M., Perrin, D., 1991, Two-way string-matching, Journal of the ACM 38(3):651-675.
13061307 Here's some background information.
13081309 A *word* is a string of symbols. The *length* of a word should be a familiar
1310 notion, and here we denote it for any word x by |x|.
1311 (We also allow for the possibility of the *empty word*, a word of length zero).
13121313 If x is any non-empty word, then an integer p with 0 < p <= |x| is said to be a
1314 *period* for x iff for all i with 0 <= i <= |x| - p - 1, we have x[i] == x[i+p].
1315 For example, both 1 and 2 are periods for the string "aa". As another example,
1316 the only period of the string "abcd" is 4.
13171318 We denote by period(x) the *smallest* period of x (provided that x is non-empty).
1319 This is always well-defined since every non-empty word x has at least one period,
1320 |x|. We sometimes call this *the period* of x.
13211322 If u, v and x are words such that x = uv, where uv is the concatenation of u and
1323 v, then we say that (u, v) is a *factorization* of x.
13241325 Let (u, v) be a factorization for a word x. Then if w is a non-empty word such
1326 that both of the following hold
13271328 - either w is a suffix of u or u is a suffix of w
1329 - either w is a prefix of v or v is a prefix of w
13301331 then w is said to be a *repetition* for the factorization (u, v).
13321333 Just to unpack this, there are four possibilities here. Let w = "abc". Then we
1334 might have:
13351336 - w is a suffix of u and w is a prefix of v. ex: ("lolabc", "abcde")
1337 - w is a suffix of u and v is a prefix of w. ex: ("lolabc", "ab")
1338 - u is a suffix of w and w is a prefix of v. ex: ("bc", "abchi")
1339 - u is a suffix of w and v is a prefix of w. ex: ("bc", "a")
13401341 Note that the word vu is a repetition for any factorization (u,v) of x = uv,
1342 so every factorization has at least one repetition.
13431344 If x is a string and (u, v) is a factorization for x, then a *local period* for
1345 (u, v) is an integer r such that there is some word w such that |w| = r and w is
1346 a repetition for (u, v).
13471348 We denote by local_period(u, v) the smallest local period of (u, v). We sometimes
1349 call this *the local period* of (u, v). Provided that x = uv is non-empty, this
1350 is well-defined (because each non-empty word has at least one factorization, as
1351 noted above).
13521353 It can be proven that the following is an equivalent definition of a local period
1354 for a factorization (u, v): any positive integer r such that x[i] == x[i+r] for
1355 all i such that |u| - r <= i <= |u| - 1 and such that both x[i] and x[i+r] are
1356 defined. (i.e., i > 0 and i + r < |x|).
13571358 Using the above reformulation, it is easy to prove that
13591360 1 <= local_period(u, v) <= period(uv)
13611362 A factorization (u, v) of x such that local_period(u,v) = period(x) is called a
1363 *critical factorization*.
13641365 The algorithm hinges on the following theorem, which is stated without proof:
13661367 **Critical Factorization Theorem** Any word x has at least one critical
1368 factorization (u, v) such that |u| < period(x).
13691370 The purpose of maximal_suffix is to find such a critical factorization.
13711372 If the period is short, compute another factorization x = u' v' to use
1373 for reverse search, chosen instead so that |v'| < period(x).
13741375*/
1376impl TwoWaySearcher {
1377fn new(needle: &[u8], end: usize) -> TwoWaySearcher {
1378let (crit_pos_false, period_false) = TwoWaySearcher::maximal_suffix(needle, false);
1379let (crit_pos_true, period_true) = TwoWaySearcher::maximal_suffix(needle, true);
13801381let (crit_pos, period) = if crit_pos_false > crit_pos_true {
1382 (crit_pos_false, period_false)
1383 } else {
1384 (crit_pos_true, period_true)
1385 };
13861387// A particularly readable explanation of what's going on here can be found
1388 // in Crochemore and Rytter's book "Text Algorithms", ch 13. Specifically
1389 // see the code for "Algorithm CP" on p. 323.
1390 //
1391 // What's going on is we have some critical factorization (u, v) of the
1392 // needle, and we want to determine whether u is a suffix of
1393 // &v[..period]. If it is, we use "Algorithm CP1". Otherwise we use
1394 // "Algorithm CP2", which is optimized for when the period of the needle
1395 // is large.
1396if needle[..crit_pos] == needle[period..period + crit_pos] {
1397// short period case -- the period is exact
1398 // compute a separate critical factorization for the reversed needle
1399 // x = u' v' where |v'| < period(x).
1400 //
1401 // This is sped up by the period being known already.
1402 // Note that a case like x = "acba" may be factored exactly forwards
1403 // (crit_pos = 1, period = 3) while being factored with approximate
1404 // period in reverse (crit_pos = 2, period = 2). We use the given
1405 // reverse factorization but keep the exact period.
1406let crit_pos_back = needle.len()
1407 - cmp::max(
1408TwoWaySearcher::reverse_maximal_suffix(needle, period, false),
1409TwoWaySearcher::reverse_maximal_suffix(needle, period, true),
1410 );
14111412TwoWaySearcher {
1413crit_pos,
1414crit_pos_back,
1415period,
1416 byteset: Self::byteset_create(&needle[..period]),
14171418 position: 0,
1419end,
1420 memory: 0,
1421 memory_back: needle.len(),
1422 }
1423 } else {
1424// long period case -- we have an approximation to the actual period,
1425 // and don't use memorization.
1426 //
1427 // Approximate the period by lower bound max(|u|, |v|) + 1.
1428 // The critical factorization is efficient to use for both forward and
1429 // reverse search.
14301431TwoWaySearcher {
1432crit_pos,
1433 crit_pos_back: crit_pos,
1434 period: cmp::max(crit_pos, needle.len() - crit_pos) + 1,
1435 byteset: Self::byteset_create(needle),
14361437 position: 0,
1438end,
1439 memory: usize::MAX, // Dummy value to signify that the period is long
1440memory_back: usize::MAX,
1441 }
1442 }
1443 }
14441445#[inline]
1446fn byteset_create(bytes: &[u8]) -> u64 {
1447bytes.iter().fold(0, |a, &b| (1 << (b & 0x3f)) | a)
1448 }
14491450#[inline]
1451fn byteset_contains(&self, byte: u8) -> bool {
1452 (self.byteset >> ((byte & 0x3f) as usize)) & 1 != 0
1453}
14541455// One of the main ideas of Two-Way is that we factorize the needle into
1456 // two halves, (u, v), and begin trying to find v in the haystack by scanning
1457 // left to right. If v matches, we try to match u by scanning right to left.
1458 // How far we can jump when we encounter a mismatch is all based on the fact
1459 // that (u, v) is a critical factorization for the needle.
1460#[inline]
1461fn next<S>(&mut self, haystack: &[u8], needle: &[u8], long_period: bool) -> S::Output
1462where
1463S: TwoWayStrategy,
1464 {
1465// `next()` uses `self.position` as its cursor
1466let old_pos = self.position;
1467let needle_last = needle.len() - 1;
1468'search: loop {
1469// Check that we have room to search in
1470 // position + needle_last can not overflow if we assume slices
1471 // are bounded by isize's range.
1472let tail_byte = match haystack.get(self.position + needle_last) {
1473Some(&b) => b,
1474None => {
1475self.position = haystack.len();
1476return S::rejecting(old_pos, self.position);
1477 }
1478 };
14791480if S::use_early_reject() && old_pos != self.position {
1481return S::rejecting(old_pos, self.position);
1482 }
14831484// Quickly skip by large portions unrelated to our substring
1485if !self.byteset_contains(tail_byte) {
1486self.position += needle.len();
1487if !long_period {
1488self.memory = 0;
1489 }
1490continue 'search;
1491 }
14921493// See if the right part of the needle matches
1494let start =
1495if long_period { self.crit_pos } else { cmp::max(self.crit_pos, self.memory) };
1496for i in start..needle.len() {
1497if needle[i] != haystack[self.position + i] {
1498self.position += i - self.crit_pos + 1;
1499if !long_period {
1500self.memory = 0;
1501 }
1502continue 'search;
1503 }
1504 }
15051506// See if the left part of the needle matches
1507let start = if long_period { 0 } else { self.memory };
1508for i in (start..self.crit_pos).rev() {
1509if needle[i] != haystack[self.position + i] {
1510self.position += self.period;
1511if !long_period {
1512self.memory = needle.len() - self.period;
1513 }
1514continue 'search;
1515 }
1516 }
15171518// We have found a match!
1519let match_pos = self.position;
15201521// Note: add self.period instead of needle.len() to have overlapping matches
1522self.position += needle.len();
1523if !long_period {
1524self.memory = 0; // set to needle.len() - self.period for overlapping matches
1525}
15261527return S::matching(match_pos, match_pos + needle.len());
1528 }
1529 }
15301531// Follows the ideas in `next()`.
1532 //
1533 // The definitions are symmetrical, with period(x) = period(reverse(x))
1534 // and local_period(u, v) = local_period(reverse(v), reverse(u)), so if (u, v)
1535 // is a critical factorization, so is (reverse(v), reverse(u)).
1536 //
1537 // For the reverse case we have computed a critical factorization x = u' v'
1538 // (field `crit_pos_back`). We need |u| < period(x) for the forward case and
1539 // thus |v'| < period(x) for the reverse.
1540 //
1541 // To search in reverse through the haystack, we search forward through
1542 // a reversed haystack with a reversed needle, matching first u' and then v'.
1543#[inline]
1544fn next_back<S>(&mut self, haystack: &[u8], needle: &[u8], long_period: bool) -> S::Output
1545where
1546S: TwoWayStrategy,
1547 {
1548// `next_back()` uses `self.end` as its cursor -- so that `next()` and `next_back()`
1549 // are independent.
1550let old_end = self.end;
1551'search: loop {
1552// Check that we have room to search in
1553 // end - needle.len() will wrap around when there is no more room,
1554 // but due to slice length limits it can never wrap all the way back
1555 // into the length of haystack.
1556let front_byte = match haystack.get(self.end.wrapping_sub(needle.len())) {
1557Some(&b) => b,
1558None => {
1559self.end = 0;
1560return S::rejecting(0, old_end);
1561 }
1562 };
15631564if S::use_early_reject() && old_end != self.end {
1565return S::rejecting(self.end, old_end);
1566 }
15671568// Quickly skip by large portions unrelated to our substring
1569if !self.byteset_contains(front_byte) {
1570self.end -= needle.len();
1571if !long_period {
1572self.memory_back = needle.len();
1573 }
1574continue 'search;
1575 }
15761577// See if the left part of the needle matches
1578let crit = if long_period {
1579self.crit_pos_back
1580 } else {
1581 cmp::min(self.crit_pos_back, self.memory_back)
1582 };
1583for i in (0..crit).rev() {
1584if needle[i] != haystack[self.end - needle.len() + i] {
1585self.end -= self.crit_pos_back - i;
1586if !long_period {
1587self.memory_back = needle.len();
1588 }
1589continue 'search;
1590 }
1591 }
15921593// See if the right part of the needle matches
1594let needle_end = if long_period { needle.len() } else { self.memory_back };
1595for i in self.crit_pos_back..needle_end {
1596if needle[i] != haystack[self.end - needle.len() + i] {
1597self.end -= self.period;
1598if !long_period {
1599self.memory_back = self.period;
1600 }
1601continue 'search;
1602 }
1603 }
16041605// We have found a match!
1606let match_pos = self.end - needle.len();
1607// Note: sub self.period instead of needle.len() to have overlapping matches
1608self.end -= needle.len();
1609if !long_period {
1610self.memory_back = needle.len();
1611 }
16121613return S::matching(match_pos, match_pos + needle.len());
1614 }
1615 }
16161617// Compute the maximal suffix of `arr`.
1618 //
1619 // The maximal suffix is a possible critical factorization (u, v) of `arr`.
1620 //
1621 // Returns (`i`, `p`) where `i` is the starting index of v and `p` is the
1622 // period of v.
1623 //
1624 // `order_greater` determines if lexical order is `<` or `>`. Both
1625 // orders must be computed -- the ordering with the largest `i` gives
1626 // a critical factorization.
1627 //
1628 // For long period cases, the resulting period is not exact (it is too short).
1629#[inline]
1630fn maximal_suffix(arr: &[u8], order_greater: bool) -> (usize, usize) {
1631let mut left = 0; // Corresponds to i in the paper
1632let mut right = 1; // Corresponds to j in the paper
1633let mut offset = 0; // Corresponds to k in the paper, but starting at 0
1634 // to match 0-based indexing.
1635let mut period = 1; // Corresponds to p in the paper
16361637while let Some(&a) = arr.get(right + offset) {
1638// `left` will be inbounds when `right` is.
1639let b = arr[left + offset];
1640if (a < b && !order_greater) || (a > b && order_greater) {
1641// Suffix is smaller, period is entire prefix so far.
1642right += offset + 1;
1643 offset = 0;
1644 period = right - left;
1645 } else if a == b {
1646// Advance through repetition of the current period.
1647if offset + 1 == period {
1648 right += offset + 1;
1649 offset = 0;
1650 } else {
1651 offset += 1;
1652 }
1653 } else {
1654// Suffix is larger, start over from current location.
1655left = right;
1656 right += 1;
1657 offset = 0;
1658 period = 1;
1659 }
1660 }
1661 (left, period)
1662 }
16631664// Compute the maximal suffix of the reverse of `arr`.
1665 //
1666 // The maximal suffix is a possible critical factorization (u', v') of `arr`.
1667 //
1668 // Returns `i` where `i` is the starting index of v', from the back;
1669 // returns immediately when a period of `known_period` is reached.
1670 //
1671 // `order_greater` determines if lexical order is `<` or `>`. Both
1672 // orders must be computed -- the ordering with the largest `i` gives
1673 // a critical factorization.
1674 //
1675 // For long period cases, the resulting period is not exact (it is too short).
1676fn reverse_maximal_suffix(arr: &[u8], known_period: usize, order_greater: bool) -> usize {
1677let mut left = 0; // Corresponds to i in the paper
1678let mut right = 1; // Corresponds to j in the paper
1679let mut offset = 0; // Corresponds to k in the paper, but starting at 0
1680 // to match 0-based indexing.
1681let mut period = 1; // Corresponds to p in the paper
1682let n = arr.len();
16831684while right + offset < n {
1685let a = arr[n - (1 + right + offset)];
1686let b = arr[n - (1 + left + offset)];
1687if (a < b && !order_greater) || (a > b && order_greater) {
1688// Suffix is smaller, period is entire prefix so far.
1689right += offset + 1;
1690 offset = 0;
1691 period = right - left;
1692 } else if a == b {
1693// Advance through repetition of the current period.
1694if offset + 1 == period {
1695 right += offset + 1;
1696 offset = 0;
1697 } else {
1698 offset += 1;
1699 }
1700 } else {
1701// Suffix is larger, start over from current location.
1702left = right;
1703 right += 1;
1704 offset = 0;
1705 period = 1;
1706 }
1707if period == known_period {
1708break;
1709 }
1710 }
1711if true {
if !(period <= known_period) {
crate::panicking::panic("assertion failed: period <= known_period")
};
};debug_assert!(period <= known_period);
1712left1713 }
1714}
17151716// TwoWayStrategy allows the algorithm to either skip non-matches as quickly
1717// as possible, or to work in a mode where it emits Rejects relatively quickly.
1718trait TwoWayStrategy {
1719type Output;
1720fn use_early_reject() -> bool;
1721fn rejecting(a: usize, b: usize) -> Self::Output;
1722fn matching(a: usize, b: usize) -> Self::Output;
1723}
17241725/// Skip to match intervals as quickly as possible
1726enum MatchOnly {}
17271728impl TwoWayStrategyfor MatchOnly {
1729type Output = Option<(usize, usize)>;
17301731#[inline]
1732fn use_early_reject() -> bool {
1733false
1734}
1735#[inline]
1736fn rejecting(_a: usize, _b: usize) -> Self::Output {
1737None1738 }
1739#[inline]
1740fn matching(a: usize, b: usize) -> Self::Output {
1741Some((a, b))
1742 }
1743}
17441745/// Emit Rejects regularly
1746enum RejectAndMatch {}
17471748impl TwoWayStrategyfor RejectAndMatch {
1749type Output = SearchStep;
17501751#[inline]
1752fn use_early_reject() -> bool {
1753true
1754}
1755#[inline]
1756fn rejecting(a: usize, b: usize) -> Self::Output {
1757 SearchStep::Reject(a, b)
1758 }
1759#[inline]
1760fn matching(a: usize, b: usize) -> Self::Output {
1761 SearchStep::Match(a, b)
1762 }
1763}
17641765/// SIMD search for short needles based on
1766/// Wojciech Muła's "SIMD-friendly algorithms for substring searching"[0]
1767///
1768/// It skips ahead by the vector width on each iteration (rather than the needle length as two-way
1769/// does) by probing the first and last byte of the needle for the whole vector width
1770/// and only doing full needle comparisons when the vectorized probe indicated potential matches.
1771///
1772/// Since the x86_64 baseline only offers SSE2 we only use u8x16 here.
1773/// If we ever ship std with for x86-64-v3 or adapt this for other platforms then wider vectors
1774/// should be evaluated.
1775///
1776/// Similarly, on LoongArch the 128-bit LSX vector extension is the baseline,
1777/// so we also use `u8x16` there. Wider vector widths may be considered
1778/// for future LoongArch extensions (e.g., LASX).
1779///
1780/// For haystacks smaller than vector-size + needle length it falls back to
1781/// a naive O(n*m) search so this implementation should not be called on larger needles.
1782///
1783/// [0]: http://0x80.pl/articles/simd-strfind.html#sse-avx2
1784#[cfg(any(
1785 all(target_arch = "x86_64", target_feature = "sse2"),
1786 all(target_arch = "loongarch64", target_feature = "lsx"),
1787 all(target_arch = "aarch64", target_feature = "neon")
1788))]
1789#[inline]
1790fn simd_contains(needle: &str, haystack: &str) -> Option<bool> {
1791let needle = needle.as_bytes();
1792let haystack = haystack.as_bytes();
17931794if true {
if !(needle.len() > 1) {
crate::panicking::panic("assertion failed: needle.len() > 1")
};
};debug_assert!(needle.len() > 1);
17951796use crate::ops::BitAnd;
1797use crate::simd::cmp::SimdPartialEq;
1798use crate::simd::{mask8x16as Mask, u8x16as Block};
17991800let first_probe = needle[0];
1801let last_byte_offset = needle.len() - 1;
18021803// the offset used for the 2nd vector
1804let second_probe_offset = if needle.len() == 2 {
1805// never bail out on len=2 needles because the probes will fully cover them and have
1806 // no degenerate cases.
18071
1808} else {
1809// try a few bytes in case first and last byte of the needle are the same
1810let Some(second_probe_offset) =
1811 (needle.len().saturating_sub(4)..needle.len()).rfind(|&idx| needle[idx] != first_probe)
1812else {
1813// fall back to other search methods if we can't find any different bytes
1814 // since we could otherwise hit some degenerate cases
1815return None;
1816 };
1817second_probe_offset1818 };
18191820// do a naive search if the haystack is too small to fit
1821if haystack.len() < Block::LEN + last_byte_offset {
1822return Some(haystack.windows(needle.len()).any(|c| c == needle));
1823 }
18241825let first_probe: Block = Block::splat(first_probe);
1826let second_probe: Block = Block::splat(needle[second_probe_offset]);
1827// first byte are already checked by the outer loop. to verify a match only the
1828 // remainder has to be compared.
1829let trimmed_needle = &needle[1..];
18301831// this #[cold] is load-bearing, benchmark before removing it...
1832let check_mask = #[cold]
1833|idx, mask: u16, skip: bool| -> bool {
1834if skip {
1835return false;
1836 }
18371838// and so is this. optimizations are weird.
1839let mut mask = mask;
18401841while mask != 0 {
1842let trailing = mask.trailing_zeros();
1843let offset = idx + trailing as usize + 1;
1844// SAFETY: mask is between 0 and 15 trailing zeroes, we skip one additional byte that was already compared
1845 // and then take trimmed_needle.len() bytes. This is within the bounds defined by the outer loop
1846unsafe {
1847let sub = haystack.get_unchecked(offset..).get_unchecked(..trimmed_needle.len());
1848if small_slice_eq(sub, trimmed_needle) {
1849return true;
1850 }
1851 }
1852 mask &= !(1 << trailing);
1853 }
1854false
1855};
18561857let test_chunk = |idx| -> u16 {
1858// SAFETY: this requires at least LANES bytes being readable at idx
1859 // that is ensured by the loop ranges (see comments below)
1860let a: Block = unsafe { haystack.as_ptr().add(idx).cast::<Block>().read_unaligned() };
1861// SAFETY: this requires LANES + block_offset bytes being readable at idx
1862let b: Block = unsafe {
1863haystack.as_ptr().add(idx).add(second_probe_offset).cast::<Block>().read_unaligned()
1864 };
1865let eq_first: Mask = a.simd_eq(first_probe);
1866let eq_last: Mask = b.simd_eq(second_probe);
1867let both = eq_first.bitand(eq_last);
1868let mask = both.to_bitmask() as u16;
18691870mask1871 };
18721873let mut i = 0;
1874let mut result = false;
1875// The loop condition must ensure that there's enough headroom to read LANE bytes,
1876 // and not only at the current index but also at the index shifted by block_offset
1877const UNROLL: usize = 4;
1878while i + last_byte_offset + UNROLL * Block::LEN < haystack.len() && !result {
1879let mut masks = [0u16; UNROLL];
1880for j in 0..UNROLL {
1881 masks[j] = test_chunk(i + j * Block::LEN);
1882 }
1883for j in 0..UNROLL {
1884let mask = masks[j];
1885if mask != 0 {
1886 result |= check_mask(i + j * Block::LEN, mask, result);
1887 }
1888 }
1889 i += UNROLL * Block::LEN;
1890 }
1891while i + last_byte_offset + Block::LEN < haystack.len() && !result {
1892let mask = test_chunk(i);
1893if mask != 0 {
1894 result |= check_mask(i, mask, result);
1895 }
1896 i += Block::LEN;
1897 }
18981899// Process the tail that didn't fit into LANES-sized steps.
1900 // This simply repeats the same procedure but as right-aligned chunk instead
1901 // of a left-aligned one. The last byte must be exactly flush with the string end so
1902 // we don't miss a single byte or read out of bounds.
1903let i = haystack.len() - last_byte_offset - Block::LEN;
1904let mask = test_chunk(i);
1905if mask != 0 {
1906result |= check_mask(i, mask, result);
1907 }
19081909Some(result)
1910}
19111912/// Compares short slices for equality.
1913///
1914/// It avoids a call to libc's memcmp which is faster on long slices
1915/// due to SIMD optimizations but it incurs a function call overhead.
1916///
1917/// # Safety
1918///
1919/// Both slices must have the same length.
1920#[cfg(any(
1921 all(target_arch = "x86_64", target_feature = "sse2"),
1922 all(target_arch = "loongarch64", target_feature = "lsx"),
1923 all(target_arch = "aarch64", target_feature = "neon")
1924))]
1925#[inline]
1926unsafe fn small_slice_eq(x: &[u8], y: &[u8]) -> bool {
1927if true {
match (&x.len(), &y.len()) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = crate::panicking::AssertKind::Eq;
crate::panicking::assert_failed(kind, &*left_val, &*right_val,
crate::option::Option::None);
}
}
};
};debug_assert_eq!(x.len(), y.len());
1928// This function is adapted from
1929 // https://github.com/BurntSushi/memchr/blob/8037d11b4357b0f07be2bb66dc2659d9cf28ad32/src/memmem/util.rs#L32
19301931 // If we don't have enough bytes to do 4-byte at a time loads, then
1932 // fall back to the naive slow version.
1933 //
1934 // Potential alternative: We could do a copy_nonoverlapping combined with a mask instead
1935 // of a loop. Benchmark it.
1936if x.len() < 4 {
1937for (&b1, &b2) in x.iter().zip(y) {
1938if b1 != b2 {
1939return false;
1940 }
1941 }
1942return true;
1943 }
1944// When we have 4 or more bytes to compare, then proceed in chunks of 4 at
1945 // a time using unaligned loads.
1946 //
1947 // Also, why do 4 byte loads instead of, say, 8 byte loads? The reason is
1948 // that this particular version of memcmp is likely to be called with tiny
1949 // needles. That means that if we do 8 byte loads, then a higher proportion
1950 // of memcmp calls will use the slower variant above. With that said, this
1951 // is a hypothesis and is only loosely supported by benchmarks. There's
1952 // likely some improvement that could be made here. The main thing here
1953 // though is to optimize for latency, not throughput.
19541955 // SAFETY: Via the conditional above, we know that both `px` and `py`
1956 // have the same length, so `px < pxend` implies that `py < pyend`.
1957 // Thus, dereferencing both `px` and `py` in the loop below is safe.
1958 //
1959 // Moreover, we set `pxend` and `pyend` to be 4 bytes before the actual
1960 // end of `px` and `py`. Thus, the final dereference outside of the
1961 // loop is guaranteed to be valid. (The final comparison will overlap with
1962 // the last comparison done in the loop for lengths that aren't multiples
1963 // of four.)
1964 //
1965 // Finally, we needn't worry about alignment here, since we do unaligned
1966 // loads.
1967unsafe {
1968let (mut px, mut py) = (x.as_ptr(), y.as_ptr());
1969let (pxend, pyend) = (px.add(x.len() - 4), py.add(y.len() - 4));
1970while px < pxend {
1971let vx = (px as *const u32).read_unaligned();
1972let vy = (py as *const u32).read_unaligned();
1973if vx != vy {
1974return false;
1975 }
1976 px = px.add(4);
1977 py = py.add(4);
1978 }
1979let vx = (pxendas *const u32).read_unaligned();
1980let vy = (pyendas *const u32).read_unaligned();
1981vx == vy1982 }
1983}