twoway/
lib.rs

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