(pat: &str, txt: &str)
| 25 | |
| 26 | #[deprecated(note = "brute force search used by benchmark, use KMP in production")] |
| 27 | pub fn search2(pat: &str, txt: &str) -> Option<usize> { |
| 28 | let M = pat.len(); |
| 29 | let N = txt.len(); |
| 30 | let mut i = 0; |
| 31 | let mut j = 0; |
| 32 | while i < N && j < M { |
| 33 | let ic = common::util::byte_at(txt, i); |
| 34 | let jc = common::util::byte_at(pat, j); |
| 35 | if ic == jc { |
| 36 | j += 1; |
| 37 | } else { |
| 38 | i -= j; |
| 39 | j = 0; |
| 40 | } |
| 41 | i += 1; |
| 42 | } |
| 43 | if j == M { |
| 44 | Some(i - M) |
| 45 | } else { |
| 46 | None |
| 47 | } |
| 48 | } |