| 1 |
use memchr::{memchr, memchr2, memchr3, memrchr, memrchr2, memrchr3}; |
| 2 |
|
| 3 |
mod scalar; |
| 4 |
|
| 5 |
#[inline] |
| 6 |
fn build_table(byteset: &[u8]) -> [u8; 256] { |
| 7 |
let mut table = [0u8; 256]; |
| 8 |
for &b in byteset { |
| 9 |
table[b as usize] = 1; |
| 10 |
} |
| 11 |
table |
| 12 |
} |
| 13 |
|
| 14 |
#[inline] |
| 15 |
pub(crate) fn find(haystack: &[u8], byteset: &[u8]) -> Option<usize> { |
| 16 |
match byteset.len() { |
| 17 |
0 => None, |
| 18 |
1 => memchr(byteset[0], haystack), |
| 19 |
2 => memchr2(byteset[0], byteset[1], haystack), |
| 20 |
3 => memchr3(byteset[0], byteset[1], byteset[2], haystack), |
| 21 |
_ => { |
| 22 |
let table = build_table(byteset); |
| 23 |
scalar::forward_search_bytes(haystack, |b| table[b as usize] != 0) |
| 24 |
} |
| 25 |
} |
| 26 |
} |
| 27 |
|
| 28 |
#[inline] |
| 29 |
pub(crate) fn rfind(haystack: &[u8], byteset: &[u8]) -> Option<usize> { |
| 30 |
match byteset.len() { |
| 31 |
0 => None, |
| 32 |
1 => memrchr(byteset[0], haystack), |
| 33 |
2 => memrchr2(byteset[0], byteset[1], haystack), |
| 34 |
3 => memrchr3(byteset[0], byteset[1], byteset[2], haystack), |
| 35 |
_ => { |
| 36 |
let table = build_table(byteset); |
| 37 |
scalar::reverse_search_bytes(haystack, |b| table[b as usize] != 0) |
| 38 |
} |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
#[inline] |
| 43 |
pub(crate) fn find_not(haystack: &[u8], byteset: &[u8]) -> Option<usize> { |
| 44 |
if haystack.is_empty() { |
| 45 |
return None; |
| 46 |
} |
| 47 |
match byteset.len() { |
| 48 |
0 => Some(0), |
| 49 |
1 => scalar::inv_memchr(byteset[0], haystack), |
| 50 |
2 => scalar::forward_search_bytes(haystack, |b| { |
| 51 |
b != byteset[0] && b != byteset[1] |
| 52 |
}), |
| 53 |
3 => scalar::forward_search_bytes(haystack, |b| { |
| 54 |
b != byteset[0] && b != byteset[1] && b != byteset[2] |
| 55 |
}), |
| 56 |
_ => { |
| 57 |
let table = build_table(byteset); |
| 58 |
scalar::forward_search_bytes(haystack, |b| table[b as usize] == 0) |
| 59 |
} |
| 60 |
} |
| 61 |
} |
| 62 |
#[inline] |
| 63 |
pub(crate) fn rfind_not(haystack: &[u8], byteset: &[u8]) -> Option<usize> { |
| 64 |
if haystack.is_empty() { |
| 65 |
return None; |
| 66 |
} |
| 67 |
match byteset.len() { |
| 68 |
0 => Some(haystack.len() - 1), |
| 69 |
1 => scalar::inv_memrchr(byteset[0], haystack), |
| 70 |
2 => scalar::reverse_search_bytes(haystack, |b| { |
| 71 |
b != byteset[0] && b != byteset[1] |
| 72 |
}), |
| 73 |
3 => scalar::reverse_search_bytes(haystack, |b| { |
| 74 |
b != byteset[0] && b != byteset[1] && b != byteset[2] |
| 75 |
}), |
| 76 |
_ => { |
| 77 |
let table = build_table(byteset); |
| 78 |
scalar::reverse_search_bytes(haystack, |b| table[b as usize] == 0) |
| 79 |
} |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
#[cfg(all(test, feature = "std", not(miri)))] |
| 84 |
mod tests { |
| 85 |
use alloc::vec::Vec; |
| 86 |
|
| 87 |
quickcheck::quickcheck! { |
| 88 |
fn qc_byteset_forward_matches_naive( |
| 89 |
haystack: Vec<u8>, |
| 90 |
needles: Vec<u8> |
| 91 |
) -> bool { |
| 92 |
super::find(&haystack, &needles) |
| 93 |
== haystack.iter().position(|b| needles.contains(b)) |
| 94 |
} |
| 95 |
fn qc_byteset_backwards_matches_naive( |
| 96 |
haystack: Vec<u8>, |
| 97 |
needles: Vec<u8> |
| 98 |
) -> bool { |
| 99 |
super::rfind(&haystack, &needles) |
| 100 |
== haystack.iter().rposition(|b| needles.contains(b)) |
| 101 |
} |
| 102 |
fn qc_byteset_forward_not_matches_naive( |
| 103 |
haystack: Vec<u8>, |
| 104 |
needles: Vec<u8> |
| 105 |
) -> bool { |
| 106 |
super::find_not(&haystack, &needles) |
| 107 |
== haystack.iter().position(|b| !needles.contains(b)) |
| 108 |
} |
| 109 |
fn qc_byteset_backwards_not_matches_naive( |
| 110 |
haystack: Vec<u8>, |
| 111 |
needles: Vec<u8> |
| 112 |
) -> bool { |
| 113 |
super::rfind_not(&haystack, &needles) |
| 114 |
== haystack.iter().rposition(|b| !needles.contains(b)) |
| 115 |
} |
| 116 |
} |
| 117 |
} |