twoway/
lib.rs

1#![cfg_attr(not(feature = "use_std"), no_std)]
2#![cfg_attr(feature = "pattern", feature(pattern))]
3
4//! **This crate is deprecated. Use crate `memchr` instead.**
5//!
6//! Fast substring search for strings and byte strings, using the [two-way algorithm][tw].
7//! 
8//! This is the same code as is included in Rust's libstd that powers
9//! `str::find(&str)`, but here it is exposed with some improvements:
10//! 
11//! - Available for byte string searches using ``&[u8]``
12//! - Having an optional SSE4.2 accelerated version (if detected at runtime) which is even faster.
13//!   Runtime detection requires the default std feature.
14//! - Using `memchr` for the single byte case, which is ultra fast.
15//! 
16//! [tw]: http://www-igm.univ-mlv.fr/~lecroq/string/node26.html
17
18#[cfg(not(feature = "use_std"))]
19extern crate core as std;
20
21use std::cmp;
22use std::usize;
23
24extern crate memchr;
25
26mod tw;
27#[cfg(all(feature="benchmarks", any(target_arch = "x86", target_arch = "x86_64")))]
28pub mod pcmp;
29#[cfg(all(not(feature="benchmarks"), any(target_arch = "x86", target_arch = "x86_64")))]
30mod pcmp;
31
32#[cfg(feature="benchmarks")]
33pub mod bmh;
34
35#[cfg(feature = "pattern")]
36use std::str::pattern::{
37    Pattern,
38    Searcher,
39    ReverseSearcher,
40    SearchStep,
41};
42
43/// `find_str` finds the first ocurrence of `pattern` in the `text`.
44///
45/// Uses the SSE42 version if it is available at runtime.
46#[inline]
47pub fn find_str(text: &str, pattern: &str) -> Option<usize> {
48    find_bytes(text.as_bytes(), pattern.as_bytes())
49}
50
51/// `find_bytes` finds the first ocurrence of `pattern` in the `text`.
52///
53/// Uses the SSE42 version if it is available at runtime.
54pub fn find_bytes(text: &[u8], pattern: &[u8]) -> Option<usize> {
55    if pattern.is_empty() {
56        Some(0)
57    } else if text.len() < pattern.len() {
58        return None;
59    } else if pattern.len() == 1 {
60        memchr::memchr(pattern[0], text)
61    } else {
62        #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] {
63            let compile_time_disable = option_env!("TWOWAY_TEST_DISABLE_PCMP")
64                .map(|s| !s.is_empty())
65                .unwrap_or(false);
66            if !compile_time_disable && pcmp::is_supported() {
67                return unsafe { pcmp::find_inner(text, pattern) };
68            }
69        }
70        let mut searcher = TwoWaySearcher::new(pattern, text.len());
71        let is_long = searcher.memory == usize::MAX;
72        // write out `true` and `false` cases to encourage the compiler
73        // to specialize the two cases separately.
74        if is_long {
75            searcher.next::<MatchOnly>(text, pattern, true).map(|t| t.0)
76        } else {
77            searcher.next::<MatchOnly>(text, pattern, false).map(|t| t.0)
78        }
79    }
80}
81
82/// `rfind_str` finds the last ocurrence of `pattern` in the `text`
83/// and returns the index of the start of the match.
84///
85/// As of this writing, this function uses the two way algorithm
86/// in pure rust (with no SSE4.2 support).
87#[inline]
88pub fn rfind_str(text: &str, pattern: &str) -> Option<usize> {
89    rfind_bytes(text.as_bytes(), pattern.as_bytes())
90}
91
92/// `rfind_bytes` finds the last ocurrence of `pattern` in the `text`,
93/// and returns the index of the start of the match.
94///
95/// As of this writing, this function uses the two way algorithm
96/// in pure rust (with no SSE4.2 support).
97pub fn rfind_bytes(text: &[u8], pattern: &[u8]) -> Option<usize> {
98    if pattern.is_empty() {
99        Some(text.len())
100    } else if pattern.len() == 1 {
101        memchr::memrchr(pattern[0], text)
102    } else {
103        let mut searcher = TwoWaySearcher::new(pattern, text.len());
104        let is_long = searcher.memory == usize::MAX;
105        // write out `true` and `false` cases to encourage the compiler
106        // to specialize the two cases separately.
107        if is_long {
108            searcher.next_back::<MatchOnly>(text, pattern, true).map(|t| t.0)
109        } else {
110            searcher.next_back::<MatchOnly>(text, pattern, false).map(|t| t.0)
111        }
112    }
113}
114
115
116/// Dummy wrapper for &str
117#[doc(hidden)]
118pub struct Str<'a>(pub &'a str);
119
120#[cfg(feature = "pattern")]
121/// Non-allocating substring search.
122///
123/// Will handle the pattern `""` as returning empty matches at each character
124/// boundary.
125impl<'a, 'b> Pattern<'a> for Str<'b> {
126    type Searcher = StrSearcher<'a, 'b>;
127
128    #[inline]
129    fn into_searcher(self, haystack: &'a str) -> StrSearcher<'a, 'b> {
130        StrSearcher::new(haystack, self.0)
131    }
132
133    /// Checks whether the pattern matches at the front of the haystack
134    #[inline]
135    fn is_prefix_of(self, haystack: &'a str) -> bool {
136        let self_ = self.0;
137        haystack.is_char_boundary(self_.len()) &&
138            self_ == &haystack[..self_.len()]
139    }
140
141    /// Checks whether the pattern matches at the back of the haystack
142    #[inline]
143    fn is_suffix_of(self, haystack: &'a str) -> bool {
144        let self_ = self.0;
145        self_.len() <= haystack.len() &&
146            haystack.is_char_boundary(haystack.len() - self_.len()) &&
147            self_ == &haystack[haystack.len() - self_.len()..]
148    }
149
150}
151
152#[derive(Clone, Debug)]
153#[doc(hidden)]
154/// Associated type for `<&str as Pattern<'a>>::Searcher`.
155pub struct StrSearcher<'a, 'b> {
156    haystack: &'a str,
157    needle: &'b str,
158
159    searcher: StrSearcherImpl,
160}
161
162#[derive(Clone, Debug)]
163enum StrSearcherImpl {
164    Empty(EmptyNeedle),
165    TwoWay(TwoWaySearcher),
166}
167
168#[derive(Clone, Debug)]
169struct EmptyNeedle {
170    position: usize,
171    end: usize,
172    is_match_fw: bool,
173    is_match_bw: bool,
174}
175
176impl<'a, 'b> StrSearcher<'a, 'b> {
177    pub fn new(haystack: &'a str, needle: &'b str) -> StrSearcher<'a, 'b> {
178        if needle.is_empty() {
179            StrSearcher {
180                haystack: haystack,
181                needle: needle,
182                searcher: StrSearcherImpl::Empty(EmptyNeedle {
183                    position: 0,
184                    end: haystack.len(),
185                    is_match_fw: true,
186                    is_match_bw: true,
187                }),
188            }
189        } else {
190            StrSearcher {
191                haystack: haystack,
192                needle: needle,
193                searcher: StrSearcherImpl::TwoWay(
194                    TwoWaySearcher::new(needle.as_bytes(), haystack.len())
195                ),
196            }
197        }
198    }
199}
200
201#[cfg(feature = "pattern")]
202unsafe impl<'a, 'b> Searcher<'a> for StrSearcher<'a, 'b> {
203    fn haystack(&self) -> &'a str { self.haystack }
204
205    #[inline]
206    fn next(&mut self) -> SearchStep {
207        match self.searcher {
208            StrSearcherImpl::Empty(ref mut searcher) => {
209                // empty needle rejects every char and matches every empty string between them
210                let is_match = searcher.is_match_fw;
211                searcher.is_match_fw = !searcher.is_match_fw;
212                let pos = searcher.position;
213                match self.haystack[pos..].chars().next() {
214                    _ if is_match => SearchStep::Match(pos, pos),
215                    None => SearchStep::Done,
216                    Some(ch) => {
217                        searcher.position += ch.len_utf8();
218                        SearchStep::Reject(pos, searcher.position)
219                    }
220                }
221            }
222            StrSearcherImpl::TwoWay(ref mut searcher) => {
223                // TwoWaySearcher produces valid *Match* indices that split at char boundaries
224                // as long as it does correct matching and that haystack and needle are
225                // valid UTF-8
226                // *Rejects* from the algorithm can fall on any indices, but we will walk them
227                // manually to the next character boundary, so that they are utf-8 safe.
228                if searcher.position == self.haystack.len() {
229                    return SearchStep::Done;
230                }
231                let is_long = searcher.memory == usize::MAX;
232                match searcher.next::<RejectAndMatch>(self.haystack.as_bytes(),
233                                                      self.needle.as_bytes(),
234                                                      is_long)
235                {
236                    SearchStep::Reject(a, mut b) => {
237                        // skip to next char boundary
238                        while !self.haystack.is_char_boundary(b) {
239                            b += 1;
240                        }
241                        searcher.position = cmp::max(b, searcher.position);
242                        SearchStep::Reject(a, b)
243                    }
244                    otherwise => otherwise,
245                }
246            }
247        }
248    }
249
250    #[inline(always)]
251    fn next_match(&mut self) -> Option<(usize, usize)> {
252        match self.searcher {
253            StrSearcherImpl::Empty(..) => {
254                loop {
255                    match self.next() {
256                        SearchStep::Match(a, b) => return Some((a, b)),
257                        SearchStep::Done => return None,
258                        SearchStep::Reject(..) => { }
259                    }
260                }
261            }
262
263            StrSearcherImpl::TwoWay(ref mut searcher) => {
264                let is_long = searcher.memory == usize::MAX;
265                // write out `true` and `false` cases to encourage the compiler
266                // to specialize the two cases separately.
267                if is_long {
268                    searcher.next::<MatchOnly>(self.haystack.as_bytes(),
269                                               self.needle.as_bytes(),
270                                               true)
271                } else {
272                    searcher.next::<MatchOnly>(self.haystack.as_bytes(),
273                                               self.needle.as_bytes(),
274                                               false)
275                }
276            }
277        }
278    }
279}
280
281#[cfg(feature = "pattern")]
282unsafe impl<'a, 'b> ReverseSearcher<'a> for StrSearcher<'a, 'b> {
283    #[inline]
284    fn next_back(&mut self) -> SearchStep {
285        match self.searcher {
286            StrSearcherImpl::Empty(ref mut searcher) => {
287                let is_match = searcher.is_match_bw;
288                searcher.is_match_bw = !searcher.is_match_bw;
289                let end = searcher.end;
290                match self.haystack[..end].chars().next_back() {
291                    _ if is_match => SearchStep::Match(end, end),
292                    None => SearchStep::Done,
293                    Some(ch) => {
294                        searcher.end -= ch.len_utf8();
295                        SearchStep::Reject(searcher.end, end)
296                    }
297                }
298            }
299            StrSearcherImpl::TwoWay(ref mut searcher) => {
300                if searcher.end == 0 {
301                    return SearchStep::Done;
302                }
303                let is_long = searcher.memory == usize::MAX;
304                match searcher.next_back::<RejectAndMatch>(self.haystack.as_bytes(),
305                                                           self.needle.as_bytes(),
306                                                           is_long)
307                {
308                    SearchStep::Reject(mut a, b) => {
309                        // skip to next char boundary
310                        while !self.haystack.is_char_boundary(a) {
311                            a -= 1;
312                        }
313                        searcher.end = cmp::min(a, searcher.end);
314                        SearchStep::Reject(a, b)
315                    }
316                    otherwise => otherwise,
317                }
318            }
319        }
320    }
321
322    #[inline]
323    fn next_match_back(&mut self) -> Option<(usize, usize)> {
324        match self.searcher {
325            StrSearcherImpl::Empty(..) => {
326                loop {
327                    match self.next_back() {
328                        SearchStep::Match(a, b) => return Some((a, b)),
329                        SearchStep::Done => return None,
330                        SearchStep::Reject(..) => { }
331                    }
332                }
333            }
334            StrSearcherImpl::TwoWay(ref mut searcher) => {
335                let is_long = searcher.memory == usize::MAX;
336                // write out `true` and `false`, like `next_match`
337                if is_long {
338                    searcher.next_back::<MatchOnly>(self.haystack.as_bytes(),
339                                                    self.needle.as_bytes(),
340                                                    true)
341                } else {
342                    searcher.next_back::<MatchOnly>(self.haystack.as_bytes(),
343                                                    self.needle.as_bytes(),
344                                                    false)
345                }
346            }
347        }
348    }
349}
350
351/// The internal state of the two-way substring search algorithm.
352#[derive(Clone, Debug)]
353#[doc(hidden)]
354pub struct TwoWaySearcher {
355    // constants
356    /// critical factorization index
357    crit_pos: usize,
358    /// critical factorization index for reversed needle
359    crit_pos_back: usize,
360    period: usize,
361    /// `byteset` is an extension (not part of the two way algorithm);
362    /// it's a 64-bit "fingerprint" where each set bit `j` corresponds
363    /// to a (byte & 63) == j present in the needle.
364    byteset: u64,
365
366    // variables
367    position: usize,
368    end: usize,
369    /// index into needle before which we have already matched
370    memory: usize,
371    /// index into needle after which we have already matched
372    memory_back: usize,
373}
374
375/*
376    This is the Two-Way search algorithm, which was introduced in the paper:
377    Crochemore, M., Perrin, D., 1991, Two-way string-matching, Journal of the ACM 38(3):651-675.
378
379    Here's some background information.
380
381    A *word* is a string of symbols. The *length* of a word should be a familiar
382    notion, and here we denote it for any word x by |x|.
383    (We also allow for the possibility of the *empty word*, a word of length zero).
384
385    If x is any non-empty word, then an integer p with 0 < p <= |x| is said to be a
386    *period* for x iff for all i with 0 <= i <= |x| - p - 1, we have x[i] == x[i+p].
387    For example, both 1 and 2 are periods for the string "aa". As another example,
388    the only period of the string "abcd" is 4.
389
390    We denote by period(x) the *smallest* period of x (provided that x is non-empty).
391    This is always well-defined since every non-empty word x has at least one period,
392    |x|. We sometimes call this *the period* of x.
393
394    If u, v and x are words such that x = uv, where uv is the concatenation of u and
395    v, then we say that (u, v) is a *factorization* of x.
396
397    Let (u, v) be a factorization for a word x. Then if w is a non-empty word such
398    that both of the following hold
399
400      - either w is a suffix of u or u is a suffix of w
401      - either w is a prefix of v or v is a prefix of w
402
403    then w is said to be a *repetition* for the factorization (u, v).
404
405    Just to unpack this, there are four possibilities here. Let w = "abc". Then we
406    might have:
407
408      - w is a suffix of u and w is a prefix of v. ex: ("lolabc", "abcde")
409      - w is a suffix of u and v is a prefix of w. ex: ("lolabc", "ab")
410      - u is a suffix of w and w is a prefix of v. ex: ("bc", "abchi")
411      - u is a suffix of w and v is a prefix of w. ex: ("bc", "a")
412
413    Note that the word vu is a repetition for any factorization (u,v) of x = uv,
414    so every factorization has at least one repetition.
415
416    If x is a string and (u, v) is a factorization for x, then a *local period* for
417    (u, v) is an integer r such that there is some word w such that |w| = r and w is
418    a repetition for (u, v).
419
420    We denote by local_period(u, v) the smallest local period of (u, v). We sometimes
421    call this *the local period* of (u, v). Provided that x = uv is non-empty, this
422    is well-defined (because each non-empty word has at least one factorization, as
423    noted above).
424
425    It can be proven that the following is an equivalent definition of a local period
426    for a factorization (u, v): any positive integer r such that x[i] == x[i+r] for
427    all i such that |u| - r <= i <= |u| - 1 and such that both x[i] and x[i+r] are
428    defined. (i.e. i > 0 and i + r < |x|).
429
430    Using the above reformulation, it is easy to prove that
431
432        1 <= local_period(u, v) <= period(uv)
433
434    A factorization (u, v) of x such that local_period(u,v) = period(x) is called a
435    *critical factorization*.
436
437    The algorithm hinges on the following theorem, which is stated without proof:
438
439    **Critical Factorization Theorem** Any word x has at least one critical
440    factorization (u, v) such that |u| < period(x).
441
442    The purpose of maximal_suffix is to find such a critical factorization.
443
444    If the period is short, compute another factorization x = u' v' to use
445    for reverse search, chosen instead so that |v'| < period(x).
446
447*/
448impl TwoWaySearcher {
449    pub fn new(needle: &[u8], end: usize) -> TwoWaySearcher {
450        let (crit_pos, period) = TwoWaySearcher::crit_params(needle);
451
452        // A particularly readable explanation of what's going on here can be found
453        // in Crochemore and Rytter's book "Text Algorithms", ch 13. Specifically
454        // see the code for "Algorithm CP" on p. 323.
455        //
456        // What's going on is we have some critical factorization (u, v) of the
457        // needle, and we want to determine whether u is a suffix of
458        // &v[..period]. If it is, we use "Algorithm CP1". Otherwise we use
459        // "Algorithm CP2", which is optimized for when the period of the needle
460        // is large.
461        if &needle[..crit_pos] == &needle[period.. period + crit_pos] {
462            // short period case -- the period is exact
463            // compute a separate critical factorization for the reversed needle
464            // x = u' v' where |v'| < period(x).
465            //
466            // This is sped up by the period being known already.
467            // Note that a case like x = "acba" may be factored exactly forwards
468            // (crit_pos = 1, period = 3) while being factored with approximate
469            // period in reverse (crit_pos = 2, period = 2). We use the given
470            // reverse factorization but keep the exact period.
471            let crit_pos_back = needle.len() - cmp::max(
472                TwoWaySearcher::reverse_maximal_suffix(needle, period, false),
473                TwoWaySearcher::reverse_maximal_suffix(needle, period, true));
474
475            TwoWaySearcher {
476                crit_pos: crit_pos,
477                crit_pos_back: crit_pos_back,
478                period: period,
479                byteset: Self::byteset_create(&needle[..period]),
480
481                position: 0,
482                end: end,
483                memory: 0,
484                memory_back: needle.len(),
485            }
486        } else {
487            // long period case -- we have an approximation to the actual period,
488            // and don't use memorization.
489            //
490            // Approximate the period by lower bound max(|u|, |v|) + 1.
491            // The critical factorization is efficient to use for both forward and
492            // reverse search.
493
494            TwoWaySearcher {
495                crit_pos: crit_pos,
496                crit_pos_back: crit_pos,
497                period: cmp::max(crit_pos, needle.len() - crit_pos) + 1,
498                byteset: Self::byteset_create(needle),
499
500                position: 0,
501                end: end,
502                memory: usize::MAX, // Dummy value to signify that the period is long
503                memory_back: usize::MAX,
504            }
505        }
506    }
507
508    /// Return the zero-based critical position and period of the provided needle.
509    ///
510    /// The returned period is incorrect when the actual period is "long." In
511    /// that case the approximation must be computed separately.
512    #[inline(always)]
513    fn crit_params(needle: &[u8]) -> (usize, usize) {
514        let (crit_pos_false, period_false) = TwoWaySearcher::maximal_suffix(needle, false);
515        let (crit_pos_true, period_true) = TwoWaySearcher::maximal_suffix(needle, true);
516
517        if crit_pos_false > crit_pos_true {
518            (crit_pos_false, period_false)
519        } else {
520            (crit_pos_true, period_true)
521        }
522    }
523
524    #[inline]
525    fn byteset_create(bytes: &[u8]) -> u64 {
526        bytes.iter().fold(0, |a, &b| (1 << (b & 0x3f)) | a)
527    }
528
529    #[inline(always)]
530    fn byteset_contains(&self, byte: u8) -> bool {
531        (self.byteset >> ((byte & 0x3f) as usize)) & 1 != 0
532    }
533
534    // One of the main ideas of Two-Way is that we factorize the needle into
535    // two halves, (u, v), and begin trying to find v in the haystack by scanning
536    // left to right. If v matches, we try to match u by scanning right to left.
537    // How far we can jump when we encounter a mismatch is all based on the fact
538    // that (u, v) is a critical factorization for the needle.
539    #[inline(always)]
540    fn next<S>(&mut self, haystack: &[u8], needle: &[u8], long_period: bool)
541        -> S::Output
542        where S: TwoWayStrategy
543    {
544        // `next()` uses `self.position` as its cursor
545        let old_pos = self.position;
546        let needle_last = needle.len() - 1;
547        'search: loop {
548            // Check that we have room to search in
549            // position + needle_last can not overflow if we assume slices
550            // are bounded by isize's range.
551            let tail_byte = match haystack.get(self.position + needle_last) {
552                Some(&b) => b,
553                None => {
554                    self.position = haystack.len();
555                    return S::rejecting(old_pos, self.position);
556                }
557            };
558
559            if S::use_early_reject() && old_pos != self.position {
560                return S::rejecting(old_pos, self.position);
561            }
562
563            // Quickly skip by large portions unrelated to our substring
564            if !self.byteset_contains(tail_byte) {
565                self.position += needle.len();
566                if !long_period {
567                    self.memory = 0;
568                }
569                continue 'search;
570            }
571
572            // See if the right part of the needle matches
573            let start = if long_period { self.crit_pos }
574                        else { cmp::max(self.crit_pos, self.memory) };
575            for i in start..needle.len() {
576                if needle[i] != haystack[self.position + i] {
577                    self.position += i - self.crit_pos + 1;
578                    if !long_period {
579                        self.memory = 0;
580                    }
581                    continue 'search;
582                }
583            }
584
585            // See if the left part of the needle matches
586            let start = if long_period { 0 } else { self.memory };
587            for i in (start..self.crit_pos).rev() {
588                if needle[i] != haystack[self.position + i] {
589                    self.position += self.period;
590                    if !long_period {
591                        self.memory = needle.len() - self.period;
592                    }
593                    continue 'search;
594                }
595            }
596
597            // We have found a match!
598            let match_pos = self.position;
599
600            // Note: add self.period instead of needle.len() to have overlapping matches
601            self.position += needle.len();
602            if !long_period {
603                self.memory = 0; // set to needle.len() - self.period for overlapping matches
604            }
605
606            return S::matching(match_pos, match_pos + needle.len());
607        }
608    }
609
610    // Follows the ideas in `next()`.
611    //
612    // The definitions are symmetrical, with period(x) = period(reverse(x))
613    // and local_period(u, v) = local_period(reverse(v), reverse(u)), so if (u, v)
614    // is a critical factorization, so is (reverse(v), reverse(u)).
615    //
616    // For the reverse case we have computed a critical factorization x = u' v'
617    // (field `crit_pos_back`). We need |u| < period(x) for the forward case and
618    // thus |v'| < period(x) for the reverse.
619    //
620    // To search in reverse through the haystack, we search forward through
621    // a reversed haystack with a reversed needle, matching first u' and then v'.
622    #[inline]
623    fn next_back<S>(&mut self, haystack: &[u8], needle: &[u8], long_period: bool)
624        -> S::Output
625        where S: TwoWayStrategy
626    {
627        // `next_back()` uses `self.end` as its cursor -- so that `next()` and `next_back()`
628        // are independent.
629        let old_end = self.end;
630        'search: loop {
631            // Check that we have room to search in
632            // end - needle.len() will wrap around when there is no more room,
633            // but due to slice length limits it can never wrap all the way back
634            // into the length of haystack.
635            let front_byte = match haystack.get(self.end.wrapping_sub(needle.len())) {
636                Some(&b) => b,
637                None => {
638                    self.end = 0;
639                    return S::rejecting(0, old_end);
640                }
641            };
642
643            if S::use_early_reject() && old_end != self.end {
644                return S::rejecting(self.end, old_end);
645            }
646
647            // Quickly skip by large portions unrelated to our substring
648            if !self.byteset_contains(front_byte) {
649                self.end -= needle.len();
650                if !long_period {
651                    self.memory_back = needle.len();
652                }
653                continue 'search;
654            }
655
656            // See if the left part of the needle matches
657            let crit = if long_period { self.crit_pos_back }
658                       else { cmp::min(self.crit_pos_back, self.memory_back) };
659            for i in (0..crit).rev() {
660                if needle[i] != haystack[self.end - needle.len() + i] {
661                    self.end -= self.crit_pos_back - i;
662                    if !long_period {
663                        self.memory_back = needle.len();
664                    }
665                    continue 'search;
666                }
667            }
668
669            // See if the right part of the needle matches
670            let needle_end = if long_period { needle.len() }
671                             else { self.memory_back };
672            for i in self.crit_pos_back..needle_end {
673                if needle[i] != haystack[self.end - needle.len() + i] {
674                    self.end -= self.period;
675                    if !long_period {
676                        self.memory_back = self.period;
677                    }
678                    continue 'search;
679                }
680            }
681
682            // We have found a match!
683            let match_pos = self.end - needle.len();
684            // Note: sub self.period instead of needle.len() to have overlapping matches
685            self.end -= needle.len();
686            if !long_period {
687                self.memory_back = needle.len();
688            }
689
690            return S::matching(match_pos, match_pos + needle.len());
691        }
692    }
693
694    // Compute the maximal suffix of `arr`.
695    //
696    // The maximal suffix is a possible critical factorization (u, v) of `arr`.
697    //
698    // Returns (`i`, `p`) where `i` is the starting index of v and `p` is the
699    // period of v.
700    //
701    // `order_greater` determines if lexical order is `<` or `>`. Both
702    // orders must be computed -- the ordering with the largest `i` gives
703    // a critical factorization.
704    //
705    // For long period cases, the resulting period is not exact (it is too short).
706    #[inline]
707    pub fn maximal_suffix(arr: &[u8], order_greater: bool) -> (usize, usize) {
708        let mut left = 0; // Corresponds to i in the paper
709        let mut right = 1; // Corresponds to j in the paper
710        let mut offset = 0; // Corresponds to k in the paper, but starting at 0
711                            // to match 0-based indexing.
712        let mut period = 1; // Corresponds to p in the paper
713
714        while let Some(&a) = arr.get(right + offset) {
715            // `left` will be inbounds when `right` is.
716            let b = arr[left + offset];
717            if (a < b && !order_greater) || (a > b && order_greater) {
718                // Suffix is smaller, period is entire prefix so far.
719                right += offset + 1;
720                offset = 0;
721                period = right - left;
722            } else if a == b {
723                // Advance through repetition of the current period.
724                if offset + 1 == period {
725                    right += offset + 1;
726                    offset = 0;
727                } else {
728                    offset += 1;
729                }
730            } else {
731                // Suffix is larger, start over from current location.
732                left = right;
733                right += 1;
734                offset = 0;
735                period = 1;
736            }
737        }
738        (left, period)
739    }
740
741    // Compute the maximal suffix of the reverse of `arr`.
742    //
743    // The maximal suffix is a possible critical factorization (u', v') of `arr`.
744    //
745    // Returns `i` where `i` is the starting index of v', from the back;
746    // returns immedately when a period of `known_period` is reached.
747    //
748    // `order_greater` determines if lexical order is `<` or `>`. Both
749    // orders must be computed -- the ordering with the largest `i` gives
750    // a critical factorization.
751    //
752    // For long period cases, the resulting period is not exact (it is too short).
753    pub fn reverse_maximal_suffix(arr: &[u8], known_period: usize,
754                                  order_greater: bool) -> usize
755    {
756        let mut left = 0; // Corresponds to i in the paper
757        let mut right = 1; // Corresponds to j in the paper
758        let mut offset = 0; // Corresponds to k in the paper, but starting at 0
759                            // to match 0-based indexing.
760        let mut period = 1; // Corresponds to p in the paper
761        let n = arr.len();
762
763        while right + offset < n {
764            let a = arr[n - (1 + right + offset)];
765            let b = arr[n - (1 + left + offset)];
766            if (a < b && !order_greater) || (a > b && order_greater) {
767                // Suffix is smaller, period is entire prefix so far.
768                right += offset + 1;
769                offset = 0;
770                period = right - left;
771            } else if a == b {
772                // Advance through repetition of the current period.
773                if offset + 1 == period {
774                    right += offset + 1;
775                    offset = 0;
776                } else {
777                    offset += 1;
778                }
779            } else {
780                // Suffix is larger, start over from current location.
781                left = right;
782                right += 1;
783                offset = 0;
784                period = 1;
785            }
786            if period == known_period {
787                break;
788            }
789        }
790        debug_assert!(period <= known_period);
791        left
792    }
793}
794
795// TwoWayStrategy allows the algorithm to either skip non-matches as quickly
796// as possible, or to work in a mode where it emits Rejects relatively quickly.
797trait TwoWayStrategy {
798    type Output;
799    fn use_early_reject() -> bool;
800    fn rejecting(usize, usize) -> Self::Output;
801    fn matching(usize, usize) -> Self::Output;
802}
803
804/// Skip to match intervals as quickly as possible
805enum MatchOnly { }
806
807impl TwoWayStrategy for MatchOnly {
808    type Output = Option<(usize, usize)>;
809
810    #[inline]
811    fn use_early_reject() -> bool { false }
812    #[inline]
813    fn rejecting(_a: usize, _b: usize) -> Self::Output { None }
814    #[inline]
815    fn matching(a: usize, b: usize) -> Self::Output { Some((a, b)) }
816}
817
818#[cfg(feature = "pattern")]
819/// Emit Rejects regularly
820enum RejectAndMatch { }
821
822#[cfg(feature = "pattern")]
823impl TwoWayStrategy for RejectAndMatch {
824    type Output = SearchStep;
825
826    #[inline]
827    fn use_early_reject() -> bool { true }
828    #[inline]
829    fn rejecting(a: usize, b: usize) -> Self::Output { SearchStep::Reject(a, b) }
830    #[inline]
831    fn matching(a: usize, b: usize) -> Self::Output { SearchStep::Match(a, b) }
832}
833
834
835#[cfg(feature = "pattern")]
836#[cfg(test)]
837impl<'a, 'b> StrSearcher<'a, 'b> {
838    fn twoway(&self) -> &TwoWaySearcher {
839        match self.searcher {
840            StrSearcherImpl::TwoWay(ref inner) => inner,
841            _ => panic!("Not a TwoWaySearcher"),
842        }
843    }
844}
845
846#[cfg(feature = "pattern")]
847#[test]
848fn test_basic() {
849    let t = StrSearcher::new("", "aab");
850    println!("{:?}", t);
851    let t = StrSearcher::new("", "abaaaba");
852    println!("{:?}", t);
853    let mut t = StrSearcher::new("GCATCGCAGAGAGTATACAGTACG", "GCAGAGAG");
854    println!("{:?}", t);
855
856    loop {
857        match t.next() {
858            SearchStep::Done => break,
859            m => println!("{:?}", m),
860        }
861    }
862
863    let mut t = StrSearcher::new("GCATCGCAGAGAGTATACAGTACG", "GCAGAGAG");
864    println!("{:?}", t);
865
866    loop {
867        match t.next_back() {
868            SearchStep::Done => break,
869            m => println!("{:?}", m),
870        }
871    }
872
873    let mut t = StrSearcher::new("banana", "nana");
874    println!("{:?}", t);
875
876    loop {
877        match t.next() {
878            SearchStep::Done => break,
879            m => println!("{:?}", m),
880        }
881    }
882}
883
884#[cfg(feature = "pattern")]
885#[cfg(test)]
886fn contains(hay: &str, n: &str) -> bool {
887    let mut tws = StrSearcher::new(hay, n);
888    loop {
889        match tws.next() {
890            SearchStep::Done => return false,
891            SearchStep::Match(..) => return true,
892            _ => { }
893        }
894    }
895}
896
897#[cfg(feature = "pattern")]
898#[cfg(test)]
899fn contains_rev(hay: &str, n: &str) -> bool {
900    let mut tws = StrSearcher::new(hay, n);
901    loop {
902        match tws.next_back() {
903            SearchStep::Done => return false,
904            SearchStep::Match(..) => return true,
905            rej => { println!("{:?}", rej); }
906        }
907    }
908}
909
910
911#[cfg(feature = "pattern")]
912#[test]
913fn test_contains() {
914    let h = "";
915    let n = "";
916    assert!(contains(h, n));
917    assert!(contains_rev(h, n));
918
919    let h = "BDC\0\0\0";
920    let n = "BDC\u{0}";
921    assert!(contains(h, n));
922    assert!(contains_rev(h, n));
923
924
925    let h = "ADA\0";
926    let n = "ADA";
927    assert!(contains(h, n));
928    assert!(contains_rev(h, n));
929
930    let h = "\u{0}\u{0}\u{0}\u{0}";
931    let n = "\u{0}";
932    assert!(contains(h, n));
933    assert!(contains_rev(h, n));
934}
935
936#[cfg(feature = "pattern")]
937#[test]
938fn test_rev_2() {
939    let h = "BDC\0\0\0";
940    let n = "BDC\u{0}";
941    let mut t = StrSearcher::new(h, n);
942    println!("{:?}", t);
943    println!("{:?}", h.contains(&n));
944
945    loop {
946        match t.next_back() {
947            SearchStep::Done => break,
948            m => println!("{:?}", m),
949        }
950    }
951
952    let h = "aabaabx";
953    let n = "aabaab";
954    let mut t = StrSearcher::new(h, n);
955    println!("{:?}", t);
956    assert_eq!(t.twoway().crit_pos, 2);
957    assert_eq!(t.twoway().crit_pos_back, 5);
958
959    loop {
960        match t.next_back() {
961            SearchStep::Done => break,
962            m => println!("{:?}", m),
963        }
964    }
965
966    let h = "abababac";
967    let n = "ababab";
968    let mut t = StrSearcher::new(h, n);
969    println!("{:?}", t);
970    assert_eq!(t.twoway().crit_pos, 1);
971    assert_eq!(t.twoway().crit_pos_back, 5);
972
973    loop {
974        match t.next_back() {
975            SearchStep::Done => break,
976            m => println!("{:?}", m),
977        }
978    }
979
980    let h = "abababac";
981    let n = "abab";
982    let mut t = StrSearcher::new(h, n);
983    println!("{:?}", t);
984
985    loop {
986        match t.next_back() {
987            SearchStep::Done => break,
988            m => println!("{:?}", m),
989        }
990    }
991
992    let h = "baabbbaabc";
993    let n = "baabb";
994    let t = StrSearcher::new(h, n);
995    println!("{:?}", t);
996    assert_eq!(t.twoway().crit_pos, 3);
997    assert_eq!(t.twoway().crit_pos_back, 3);
998
999    let h = "aabaaaabaabxx";
1000    let n = "aabaaaabaa";
1001    let mut t = StrSearcher::new(h, n);
1002    println!("{:?}", t);
1003
1004    loop {
1005        match t.next_back() {
1006            SearchStep::Done => break,
1007            m => println!("{:?}", m),
1008        }
1009    }
1010
1011    let h = "babbabax";
1012    let n = "babbab";
1013    let mut t = StrSearcher::new(h, n);
1014    println!("{:?}", t);
1015    assert_eq!(t.twoway().crit_pos, 2);
1016    assert_eq!(t.twoway().crit_pos_back, 4);
1017
1018    loop {
1019        match t.next_back() {
1020            SearchStep::Done => break,
1021            m => println!("{:?}", m),
1022        }
1023    }
1024
1025    let h = "xacbaabcax";
1026    let n = "abca";
1027    let mut t = StrSearcher::new(h, n);
1028    assert_eq!(t.next_match_back(), Some((5, 9)));
1029
1030    let h = "xacbaacbxxcba";
1031    let m = "acba";
1032    let mut s = StrSearcher::new(h, m);
1033    assert_eq!(s.next_match_back(), Some((1, 5)));
1034    assert_eq!(s.twoway().crit_pos, 1);
1035    assert_eq!(s.twoway().crit_pos_back, 2);
1036}
1037
1038#[cfg(feature = "pattern")]
1039#[test]
1040fn test_rev_unicode() {
1041    let h = "ααααααβ";
1042    let n = "αβ";
1043    let mut t = StrSearcher::new(h, n);
1044    println!("{:?}", t);
1045
1046    loop {
1047        match t.next() {
1048            SearchStep::Done => break,
1049            m => println!("{:?}", m),
1050        }
1051    }
1052
1053    let mut t = StrSearcher::new(h, n);
1054    loop {
1055        match t.next_back() {
1056            SearchStep::Done => break,
1057            m => println!("{:?}", m),
1058        }
1059    }
1060}
1061
1062#[test]
1063fn maximal_suffix() {
1064    assert_eq!((2, 1), TwoWaySearcher::maximal_suffix(b"aab", false));
1065    assert_eq!((0, 3), TwoWaySearcher::maximal_suffix(b"aab", true));
1066
1067    assert_eq!((0, 3), TwoWaySearcher::maximal_suffix(b"aabaa", true));
1068    assert_eq!((2, 3), TwoWaySearcher::maximal_suffix(b"aabaa", false));
1069
1070    assert_eq!((0, 7), TwoWaySearcher::maximal_suffix(b"gcagagag", false));
1071    assert_eq!((2, 2), TwoWaySearcher::maximal_suffix(b"gcagagag", true));
1072
1073    // both of these factorizations are critial factorizations
1074    assert_eq!((2, 2), TwoWaySearcher::maximal_suffix(b"banana", false));
1075    assert_eq!((1, 2), TwoWaySearcher::maximal_suffix(b"banana", true));
1076    assert_eq!((0, 6), TwoWaySearcher::maximal_suffix(b"zanana", false));
1077    assert_eq!((1, 2), TwoWaySearcher::maximal_suffix(b"zanana", true));
1078}
1079
1080#[test]
1081fn maximal_suffix_verbose() {
1082    fn maximal_suffix(arr: &[u8], order_greater: bool) -> (usize, usize) {
1083        let mut left: usize = 0; // Corresponds to i in the paper
1084        let mut right = 1; // Corresponds to j in the paper
1085        let mut offset = 0; // Corresponds to k in the paper
1086        let mut period = 1; // Corresponds to p in the paper
1087
1088        macro_rules! asstr {
1089            ($e:expr) => (::std::str::from_utf8($e).unwrap())
1090        }
1091
1092        while let Some(&a) = arr.get(right + offset) {
1093            // `left` will be inbounds when `right` is.
1094            debug_assert!(left <= right);
1095            let b = unsafe { *arr.get_unchecked(left + offset) };
1096            println!("str={}, l={}, r={}, offset={}, p={}", asstr!(arr), left, right, offset, period);
1097            if (a < b && !order_greater) || (a > b && order_greater) {
1098                // Suffix is smaller, period is entire prefix so far.
1099                right += offset + 1;
1100                offset = 0;
1101                period = right - left;
1102            } else if a == b {
1103                // Advance through repetition of the current period.
1104                if offset + 1 == period {
1105                    right += offset + 1;
1106                    offset = 0;
1107                } else {
1108                    offset += 1;
1109                }
1110            } else {
1111                // Suffix is larger, start over from current location.
1112                left = right;
1113                right += 1;
1114                offset = 0;
1115                period = 1;
1116            }
1117        }
1118        println!("str={}, l={}, r={}, offset={}, p={} ==END==", asstr!(arr), left, right, offset, period);
1119        (left, period)
1120    }
1121
1122    fn reverse_maximal_suffix(arr: &[u8], known_period: usize, order_greater: bool) -> usize {
1123        let n = arr.len();
1124        let mut left: usize = 0; // Corresponds to i in the paper
1125        let mut right = 1; // Corresponds to j in the paper
1126        let mut offset = 0; // Corresponds to k in the paper
1127        let mut period = 1; // Corresponds to p in the paper
1128
1129        macro_rules! asstr {
1130            ($e:expr) => (::std::str::from_utf8($e).unwrap())
1131        }
1132
1133        while right + offset < n {
1134            // `left` will be inbounds when `right` is.
1135            debug_assert!(left <= right);
1136            let a = unsafe { *arr.get_unchecked(n - (1 + right + offset)) };
1137            let b = unsafe { *arr.get_unchecked(n - (1 + left + offset)) };
1138            println!("str={}, l={}, r={}, offset={}, p={}", asstr!(arr), left, right, offset, period);
1139            if (a < b && !order_greater) || (a > b && order_greater) {
1140                // Suffix is smaller, period is entire prefix so far.
1141                right += offset + 1;
1142                offset = 0;
1143                period = right - left;
1144                if period == known_period {
1145                    break;
1146                }
1147            } else if a == b {
1148                // Advance through repetition of the current period.
1149                if offset + 1 == period {
1150                    right += offset + 1;
1151                    offset = 0;
1152                } else {
1153                    offset += 1;
1154                }
1155            } else {
1156                // Suffix is larger, start over from current location.
1157                left = right;
1158                right += 1;
1159                offset = 0;
1160                period = 1;
1161            }
1162        }
1163        println!("str={}, l={}, r={}, offset={}, p={} ==END==", asstr!(arr), left, right, offset, period);
1164        debug_assert!(period == known_period);
1165        left
1166    }
1167
1168    assert_eq!((2, 2), maximal_suffix(b"banana", false));
1169    assert_eq!((1, 2), maximal_suffix(b"banana", true));
1170    assert_eq!((0, 7), maximal_suffix(b"gcagagag", false));
1171    assert_eq!((2, 2), maximal_suffix(b"gcagagag", true));
1172    assert_eq!((2, 1), maximal_suffix(b"bac", false));
1173    assert_eq!((1, 2), maximal_suffix(b"bac", true));
1174    assert_eq!((0, 9), maximal_suffix(b"baaaaaaaa", false));
1175    assert_eq!((1, 1), maximal_suffix(b"baaaaaaaa", true));
1176
1177    assert_eq!((2, 3), maximal_suffix(b"babbabbab", false));
1178    assert_eq!((1, 3), maximal_suffix(b"babbabbab", true));
1179
1180    assert_eq!(2, reverse_maximal_suffix(b"babbabbab", 3, false));
1181    assert_eq!(1, reverse_maximal_suffix(b"babbabbab", 3, true));
1182
1183    assert_eq!((0, 2), maximal_suffix(b"bababa", false));
1184    assert_eq!((1, 2), maximal_suffix(b"bababa", true));
1185
1186    assert_eq!(1, reverse_maximal_suffix(b"bababa", 2, false));
1187    assert_eq!(0, reverse_maximal_suffix(b"bababa", 2, true));
1188
1189    // NOTE: returns "long period" case per = 2, which is an approximation
1190    assert_eq!((2, 2), maximal_suffix(b"abca", false));
1191    assert_eq!((0, 3), maximal_suffix(b"abca", true));
1192
1193    assert_eq!((3, 2), maximal_suffix(b"abcda", false));
1194    assert_eq!((0, 4), maximal_suffix(b"abcda", true));
1195
1196    // "aöa"
1197    assert_eq!((1, 3), maximal_suffix(b"acba", false));
1198    assert_eq!((0, 3), maximal_suffix(b"acba", true));
1199    //assert_eq!(2, reverse_maximal_suffix(b"acba", 3, false));
1200    //assert_eq!(0, reverse_maximal_suffix(b"acba", 3, true));
1201}
1202
1203#[cfg(feature = "pattern")]
1204#[test]
1205fn test_find_rfind() {
1206    fn find(hay: &str, pat: &str) -> Option<usize> {
1207        let mut t = pat.into_searcher(hay);
1208        t.next_match().map(|(x, _)| x)
1209    }
1210
1211    fn rfind(hay: &str, pat: &str) -> Option<usize> {
1212        let mut t = pat.into_searcher(hay);
1213        t.next_match_back().map(|(x, _)| x)
1214    }
1215
1216    // find every substring -- assert that it finds it, or an earlier occurence.
1217    let string = "Việt Namacbaabcaabaaba";
1218    for (i, ci) in string.char_indices() {
1219        let ip = i + ci.len_utf8();
1220        for j in string[ip..].char_indices()
1221                             .map(|(i, _)| i)
1222                             .chain(Some(string.len() - ip))
1223        {
1224            let pat = &string[i..ip + j];
1225            assert!(match find(string, pat) {
1226                None => false,
1227                Some(x) => x <= i,
1228            });
1229            assert!(match rfind(string, pat) {
1230                None => false,
1231                Some(x) => x >= i,
1232            });
1233        }
1234    }
1235}