Skip to main content

memmem/
two_way.rs

1// Copyright 2015 The Rust Project Developers.
2// Copyright 2015 Joe Neeman.
3//
4// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7// option. This file may not be copied, modified, or distributed
8// except according to those terms.
9
10use std::{cmp, usize};
11
12use super::Searcher;
13
14/// Searches for a substring using the "two-way" algorithm of Crochemore and Perrin, D.
15///
16/// This implementation is basically copied from rust's standard library.
17#[derive(Clone, Debug)]
18pub struct TwoWaySearcher<'a> {
19    needle: &'a [u8],
20
21    /// critical factorization index
22    crit_pos: usize,
23    period: usize,
24    /// `byteset` is an extension (not part of the two way algorithm);
25    /// it's a 64-bit "fingerprint" where each set bit `j` corresponds
26    /// to a (byte & 63) == j present in the needle.
27    byteset: u64,
28    /// TODO: docme
29    is_long: bool,
30}
31
32/// Mutable state of the searcher.
33struct TwoWayState {
34    position: usize,
35    /// index into needle before which we have already matched
36    memory: usize,
37}
38
39impl<'a> Searcher for TwoWaySearcher<'a> {
40    fn search_in(&self, haystack: &[u8]) -> Option<usize> {
41        if self.needle.is_empty() {
42            Some(0)
43        } else if self.is_long {
44            let state = TwoWayState {
45                position: 0,
46                memory: usize::MAX,
47            };
48
49            self.next(haystack, state, true)
50        } else {
51            let state = TwoWayState {
52                position: 0,
53                memory: 0,
54            };
55
56            self.next(haystack, state, false)
57        }
58    }
59}
60
61/*
62    This is the Two-Way search algorithm, which was introduced in the paper:
63    Crochemore, M., Perrin, D., 1991, Two-way string-matching, Journal of the ACM 38(3):651-675.
64
65    Here's some background information.
66
67    A *word* is a string of symbols. The *length* of a word should be a familiar
68    notion, and here we denote it for any word x by |x|.
69    (We also allow for the possibility of the *empty word*, a word of length zero).
70
71    If x is any non-empty word, then an integer p with 0 < p <= |x| is said to be a
72    *period* for x iff for all i with 0 <= i <= |x| - p - 1, we have x[i] == x[i+p].
73    For example, both 1 and 2 are periods for the string "aa". As another example,
74    the only period of the string "abcd" is 4.
75
76    We denote by period(x) the *smallest* period of x (provided that x is non-empty).
77    This is always well-defined since every non-empty word x has at least one period,
78    |x|. We sometimes call this *the period* of x.
79
80    If u, v and x are words such that x = uv, where uv is the concatenation of u and
81    v, then we say that (u, v) is a *factorization* of x.
82
83    Let (u, v) be a factorization for a word x. Then if w is a non-empty word such
84    that both of the following hold
85
86      - either w is a suffix of u or u is a suffix of w
87      - either w is a prefix of v or v is a prefix of w
88
89    then w is said to be a *repetition* for the factorization (u, v).
90
91    Just to unpack this, there are four possibilities here. Let w = "abc". Then we
92    might have:
93
94      - w is a suffix of u and w is a prefix of v. ex: ("lolabc", "abcde")
95      - w is a suffix of u and v is a prefix of w. ex: ("lolabc", "ab")
96      - u is a suffix of w and w is a prefix of v. ex: ("bc", "abchi")
97      - u is a suffix of w and v is a prefix of w. ex: ("bc", "a")
98
99    Note that the word vu is a repetition for any factorization (u,v) of x = uv,
100    so every factorization has at least one repetition.
101
102    If x is a string and (u, v) is a factorization for x, then a *local period* for
103    (u, v) is an integer r such that there is some word w such that |w| = r and w is
104    a repetition for (u, v).
105
106    We denote by local_period(u, v) the smallest local period of (u, v). We sometimes
107    call this *the local period* of (u, v). Provided that x = uv is non-empty, this
108    is well-defined (because each non-empty word has at least one factorization, as
109    noted above).
110
111    It can be proven that the following is an equivalent definition of a local period
112    for a factorization (u, v): any positive integer r such that x[i] == x[i+r] for
113    all i such that |u| - r <= i <= |u| - 1 and such that both x[i] and x[i+r] are
114    defined. (i.e. i > 0 and i + r < |x|).
115
116    Using the above reformulation, it is easy to prove that
117
118        1 <= local_period(u, v) <= period(uv)
119
120    A factorization (u, v) of x such that local_period(u,v) = period(x) is called a
121    *critical factorization*.
122
123    The algorithm hinges on the following theorem, which is stated without proof:
124
125    **Critical Factorization Theorem** Any word x has at least one critical
126    factorization (u, v) such that |u| < period(x).
127
128    The purpose of maximal_suffix is to find such a critical factorization.
129
130    If the period is short, compute another factorization x = u' v' to use
131    for reverse search, chosen instead so that |v'| < period(x).
132
133*/
134impl<'a> TwoWaySearcher<'a> {
135    /// Creates a new `TwoWaySearcher` that can be used to search for `needle`.
136    pub fn new<'b>(needle: &'b [u8]) -> TwoWaySearcher<'b> {
137        if needle.is_empty() {
138            return TwoWaySearcher {
139                needle: needle,
140                crit_pos: 0,
141                period: 0,
142                byteset: 0,
143                is_long: false,
144            };
145        }
146
147        let (crit_pos_false, period_false) = TwoWaySearcher::maximal_suffix(needle, false);
148        let (crit_pos_true, period_true) = TwoWaySearcher::maximal_suffix(needle, true);
149
150        let (crit_pos, period) = if crit_pos_false > crit_pos_true {
151            (crit_pos_false, period_false)
152        } else {
153            (crit_pos_true, period_true)
154        };
155
156        // A particularly readable explanation of what's going on here can be found
157        // in Crochemore and Rytter's book "Text Algorithms", ch 13. Specifically
158        // see the code for "Algorithm CP" on p. 323.
159        //
160        // What's going on is we have some critical factorization (u, v) of the
161        // needle, and we want to determine whether u is a suffix of
162        // &v[..period]. If it is, we use "Algorithm CP1". Otherwise we use
163        // "Algorithm CP2", which is optimized for when the period of the needle
164        // is large.
165        if &needle[..crit_pos] == &needle[period..period + crit_pos] {
166            // short period case -- the period is exact
167            // compute a separate critical factorization for the reversed needle
168            // x = u' v' where |v'| < period(x).
169            //
170            // This is sped up by the period being known already.
171
172            TwoWaySearcher {
173                needle: needle,
174                crit_pos: crit_pos,
175                period: period,
176                byteset: Self::byteset_create(&needle[..period]),
177                is_long: false,
178            }
179        } else {
180            // long period case -- we have an approximation to the actual period,
181            // and don't use memorization.
182            //
183            // Approximate the period by lower bound max(|u|, |v|) + 1.
184
185            TwoWaySearcher {
186                needle: needle,
187                crit_pos: crit_pos,
188                period: cmp::max(crit_pos, needle.len() - crit_pos) + 1,
189                byteset: Self::byteset_create(needle),
190                is_long: true,
191            }
192        }
193    }
194
195    #[inline]
196    fn byteset_create(bytes: &[u8]) -> u64 {
197        bytes.iter().fold(0, |a, &b| (1 << (b & 0x3f)) | a)
198    }
199
200    #[inline(always)]
201    fn byteset_contains(&self, byte: u8) -> bool {
202        (self.byteset >> ((byte & 0x3f) as usize)) & 1 != 0
203    }
204
205    // One of the main ideas of Two-Way is that we factorize the needle into
206    // two halves, (u, v), and begin trying to find v in the haystack by scanning
207    // left to right. If v matches, we try to match u by scanning right to left.
208    // How far we can jump when we encounter a mismatch is all based on the fact
209    // that (u, v) is a critical factorization for the needle.
210    #[inline(always)]
211    fn next(&self, haystack: &[u8], mut state: TwoWayState, long_period: bool) -> Option<usize> {
212        let needle_last = self.needle.len() - 1;
213        'search: loop {
214            // Check that we have room to search in
215            // position + needle_last can not overflow if we assume slices
216            // are bounded by isize's range.
217            let tail_byte = match haystack.get(state.position + needle_last) {
218                Some(&b) => b,
219                None => {
220                    return None;
221                }
222            };
223
224            // Quickly skip by large portions unrelated to our substring
225            if !self.byteset_contains(tail_byte) {
226                state.position += self.needle.len();
227                if !long_period {
228                    state.memory = 0;
229                }
230                continue 'search;
231            }
232
233            // See if the right part of the needle matches
234            let start = if long_period {
235                self.crit_pos
236            } else {
237                cmp::max(self.crit_pos, state.memory)
238            };
239            for i in start..self.needle.len() {
240                if self.needle[i] != haystack[state.position + i] {
241                    state.position += i - self.crit_pos + 1;
242                    if !long_period {
243                        state.memory = 0;
244                    }
245                    continue 'search;
246                }
247            }
248
249            // See if the left part of the needle matches
250            let start = if long_period { 0 } else { state.memory };
251            for i in (start..self.crit_pos).rev() {
252                if self.needle[i] != haystack[state.position + i] {
253                    state.position += self.period;
254                    if !long_period {
255                        state.memory = self.needle.len() - self.period;
256                    }
257                    continue 'search;
258                }
259            }
260
261            // We have found a match!
262            let match_pos = state.position;
263
264            state.position += self.needle.len();
265            if !long_period {
266                state.memory = 0; // set to needle.len() - self.period for overlapping matches
267            }
268
269            return Some(match_pos);
270        }
271    }
272
273    // Compute the maximal suffix of `arr`.
274    //
275    // The maximal suffix is a possible critical factorization (u, v) of `arr`.
276    //
277    // Returns (`i`, `p`) where `i` is the starting index of v and `p` is the
278    // period of v.
279    //
280    // `order_greater` determines if lexical order is `<` or `>`. Both
281    // orders must be computed -- the ordering with the largest `i` gives
282    // a critical factorization.
283    //
284    // For long period cases, the resulting period is not exact (it is too short).
285    #[inline]
286    fn maximal_suffix(arr: &[u8], order_greater: bool) -> (usize, usize) {
287        let mut left = 0; // Corresponds to i in the paper
288        let mut right = 1; // Corresponds to j in the paper
289        let mut offset = 0; // Corresponds to k in the paper, but starting at 0
290        // to match 0-based indexing.
291        let mut period = 1; // Corresponds to p in the paper
292
293        while let Some(&a) = arr.get(right + offset) {
294            // `left` will be inbounds when `right` is.
295            let b = arr[left + offset];
296            if (a < b && !order_greater) || (a > b && order_greater) {
297                // Suffix is smaller, period is entire prefix so far.
298                right += offset + 1;
299                offset = 0;
300                period = right - left;
301            } else if a == b {
302                // Advance through repetition of the current period.
303                if offset + 1 == period {
304                    right += offset + 1;
305                    offset = 0;
306                } else {
307                    offset += 1;
308                }
309            } else {
310                // Suffix is larger, start over from current location.
311                left = right;
312                right += 1;
313                offset = 0;
314                period = 1;
315            }
316        }
317        (left, period)
318    }
319}
320
321#[cfg(test)]
322mod tests {
323    use super::{Searcher, TwoWaySearcher};
324    // use quickcheck::quickcheck;
325
326    #[test]
327    fn same_as_std() {
328        let tests = vec![
329            ("something", "ing"),
330            ("something", ""),
331            ("something", "s"),
332            ("something", "z"),
333            ("", ""),
334            (" ", " "),
335            ("a", "a"),
336            ("aaaaa", "aa"),
337            ("a", "aa"),
338        ];
339
340        for test in tests {
341            let haystack = test.0;
342            let needle = test.1;
343            let search = TwoWaySearcher::new(needle.as_bytes());
344            assert_eq!(search.search_in(haystack.as_bytes()), haystack.find(needle));
345        }
346    }
347}