1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
use ByteStr;
pub trait Matcher {}
pub trait IntoMatcher {
type Matcher: Matcher;
fn into_matcher(self) -> Self::Matcher;
}
pub trait PrefixMatcher: Matcher {
fn is_prefix_of(&self, haystack: &ByteStr) -> bool;
}
pub trait SufixMatcher: Matcher {
fn is_sufix_of(&self, haystack: &ByteStr) -> bool;
}
pub trait FullMatcher: Matcher {
fn matches(&self, haystack: &ByteStr) -> bool;
}
pub trait ForwardSearcher: Matcher {
fn find(&self, haystack: &ByteStr) -> Option<(usize, usize)>;
}
pub trait ReverseSearcher: Matcher {
fn rfind(&self, haystack: &ByteStr) -> Option<(usize, usize)>;
}
pub trait DoubleEndedSearcher: ForwardSearcher + ReverseSearcher {}
pub struct StrMatcher<'a> {
needle: &'a ByteStr,
}
impl<'a> StrMatcher<'a> {
#[inline]
pub fn new(needle: &'a ByteStr) -> Self {
Self { needle: needle }
}
}
impl<'a> Matcher for StrMatcher<'a> {}
impl<'a, T: AsRef<[u8]>> IntoMatcher for &'a T {
type Matcher = StrMatcher<'a>;
#[inline]
fn into_matcher(self) -> StrMatcher<'a> {
StrMatcher::new(ByteStr::from_slice(self.as_ref()))
}
}
impl<'a> PrefixMatcher for StrMatcher<'a> {
fn is_prefix_of(&self, haystack: &ByteStr) -> bool {
haystack.len() >= self.needle.len() && haystack[0 .. self.needle.len()] == self.needle[..]
}
}
impl<'a> SufixMatcher for StrMatcher<'a> {
fn is_sufix_of(&self, haystack: &ByteStr) -> bool {
haystack.len() >= self.needle.len() &&
haystack[haystack.len() - self.needle.len() .. haystack.len()] == self.needle[..]
}
}
impl<'a> FullMatcher for StrMatcher<'a> {
#[inline]
fn matches(&self, haystack: &ByteStr) -> bool {
haystack == self.needle
}
}
impl<'a> ForwardSearcher for StrMatcher<'a> {
fn find(&self, haystack: &ByteStr) -> Option<(usize, usize)> {
for (i, window) in haystack.windows(self.needle.len()).enumerate() {
if window == self.needle {
return Some((i, i + self.needle.len()));
}
}
None
}
}
impl<'a> ReverseSearcher for StrMatcher<'a> {
fn rfind(&self, haystack: &ByteStr) -> Option<(usize, usize)> {
for (i, window) in haystack.windows(self.needle.len()).enumerate().rev() {
if window == self.needle {
return Some((i, i + self.needle.len()));
}
}
None
}
}
pub struct ByteMatcher {
needle: u8,
}
impl ByteMatcher {
#[inline]
pub fn new(needle: u8) -> Self {
ByteMatcher { needle: needle }
}
}
impl Matcher for ByteMatcher {}
impl IntoMatcher for u8 {
type Matcher = ByteMatcher;
fn into_matcher(self) -> ByteMatcher {
ByteMatcher::new(self)
}
}
impl PrefixMatcher for ByteMatcher {
#[inline]
fn is_prefix_of(&self, haystack: &ByteStr) -> bool {
haystack.first().map_or(false, |&byte| byte == self.needle)
}
}
impl SufixMatcher for ByteMatcher {
#[inline]
fn is_sufix_of(&self, haystack: &ByteStr) -> bool {
haystack.last().map_or(false, |&byte| byte == self.needle)
}
}
impl FullMatcher for ByteMatcher {
#[inline]
fn matches(&self, haystack: &ByteStr) -> bool {
haystack == [self.needle]
}
}
impl ForwardSearcher for ByteMatcher {
fn find(&self, haystack: &ByteStr) -> Option<(usize, usize)> {
for (i, &byte) in haystack.iter().enumerate() {
if byte == self.needle {
return Some((i, i + 1));
}
}
None
}
}
impl ReverseSearcher for ByteMatcher {
fn rfind(&self, haystack: &ByteStr) -> Option<(usize, usize)> {
for (i, &byte) in haystack.iter().enumerate().rev() {
if byte == self.needle {
return Some((i, i + 1));
}
}
None
}
}
impl DoubleEndedSearcher for ByteMatcher {}