-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathfallback.rs
40 lines (33 loc) · 844 Bytes
/
fallback.rs
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
// TODO: Try boxing the closure to see if we can hide the type
// TODO: Or maybe use a function pointer?
pub struct Bytes<F> {
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().copied().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)
}
}