/rust/registry/src/index.crates.io-1949cf8c6b5b557f/bstr-1.12.0/src/byteset/mod.rs
Line | Count | Source |
1 | | use memchr::{memchr, memchr2, memchr3, memrchr, memrchr2, memrchr3}; |
2 | | |
3 | | mod scalar; |
4 | | |
5 | | #[inline] |
6 | 0 | fn build_table(byteset: &[u8]) -> [u8; 256] { |
7 | 0 | let mut table = [0u8; 256]; |
8 | 0 | for &b in byteset { |
9 | 0 | table[b as usize] = 1; |
10 | 0 | } |
11 | 0 | table |
12 | 0 | } |
13 | | |
14 | | #[inline] |
15 | 0 | pub(crate) fn find(haystack: &[u8], byteset: &[u8]) -> Option<usize> { |
16 | 0 | match byteset.len() { |
17 | 0 | 0 => None, |
18 | 0 | 1 => memchr(byteset[0], haystack), |
19 | 0 | 2 => memchr2(byteset[0], byteset[1], haystack), |
20 | 0 | 3 => memchr3(byteset[0], byteset[1], byteset[2], haystack), |
21 | | _ => { |
22 | 0 | let table = build_table(byteset); |
23 | 0 | scalar::forward_search_bytes(haystack, |b| table[b as usize] != 0) |
24 | | } |
25 | | } |
26 | 0 | } |
27 | | |
28 | | #[inline] |
29 | 0 | pub(crate) fn rfind(haystack: &[u8], byteset: &[u8]) -> Option<usize> { |
30 | 0 | match byteset.len() { |
31 | 0 | 0 => None, |
32 | 0 | 1 => memrchr(byteset[0], haystack), |
33 | 0 | 2 => memrchr2(byteset[0], byteset[1], haystack), |
34 | 0 | 3 => memrchr3(byteset[0], byteset[1], byteset[2], haystack), |
35 | | _ => { |
36 | 0 | let table = build_table(byteset); |
37 | 0 | scalar::reverse_search_bytes(haystack, |b| table[b as usize] != 0) |
38 | | } |
39 | | } |
40 | 0 | } |
41 | | |
42 | | #[inline] |
43 | 0 | pub(crate) fn find_not(haystack: &[u8], byteset: &[u8]) -> Option<usize> { |
44 | 0 | if haystack.is_empty() { |
45 | 0 | return None; |
46 | 0 | } |
47 | 0 | match byteset.len() { |
48 | 0 | 0 => Some(0), |
49 | 0 | 1 => scalar::inv_memchr(byteset[0], haystack), |
50 | 0 | 2 => scalar::forward_search_bytes(haystack, |b| { |
51 | 0 | b != byteset[0] && b != byteset[1] |
52 | 0 | }), |
53 | 0 | 3 => scalar::forward_search_bytes(haystack, |b| { |
54 | 0 | b != byteset[0] && b != byteset[1] && b != byteset[2] |
55 | 0 | }), |
56 | | _ => { |
57 | 0 | let table = build_table(byteset); |
58 | 0 | scalar::forward_search_bytes(haystack, |b| table[b as usize] == 0) |
59 | | } |
60 | | } |
61 | 0 | } |
62 | | #[inline] |
63 | 0 | pub(crate) fn rfind_not(haystack: &[u8], byteset: &[u8]) -> Option<usize> { |
64 | 0 | if haystack.is_empty() { |
65 | 0 | return None; |
66 | 0 | } |
67 | 0 | match byteset.len() { |
68 | 0 | 0 => Some(haystack.len() - 1), |
69 | 0 | 1 => scalar::inv_memrchr(byteset[0], haystack), |
70 | 0 | 2 => scalar::reverse_search_bytes(haystack, |b| { |
71 | 0 | b != byteset[0] && b != byteset[1] |
72 | 0 | }), |
73 | 0 | 3 => scalar::reverse_search_bytes(haystack, |b| { |
74 | 0 | b != byteset[0] && b != byteset[1] && b != byteset[2] |
75 | 0 | }), |
76 | | _ => { |
77 | 0 | let table = build_table(byteset); |
78 | 0 | scalar::reverse_search_bytes(haystack, |b| table[b as usize] == 0) |
79 | | } |
80 | | } |
81 | 0 | } |
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 | | } |