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
pub struct Bytes<F>
where
F: Fn(u8) -> bool,
{
fallback: F,
}
impl<F> Bytes<F>
where
F: Fn(u8) -> bool,
{
pub fn new(fallback: F) -> Self {
Bytes { fallback }
}
pub fn find(&self, haystack: &[u8]) -> Option<usize> {
haystack.iter().cloned().position(&self.fallback)
}
}
pub struct ByteSubstring<'a> {
needle: &'a [u8],
}
impl<'a> ByteSubstring<'a> {
pub fn new(needle: &'a[u8]) -> Self {
ByteSubstring { needle }
}
#[cfg(feature = "pattern")]
pub fn needle_len(&self) -> usize {
self.needle.len()
}
pub fn find(&self, haystack: &[u8]) -> Option<usize> {
haystack
.windows(self.needle.len())
.position(|window| window == self.needle)
}
}