Coverage Report

Created: 2025-07-23 07:29

/rust/registry/src/index.crates.io-6f17d22bba15001f/memchr-2.4.1/src/memchr/iter.rs
Line
Count
Source (jump to first uncovered line)
1
use crate::{memchr, memchr2, memchr3, memrchr, memrchr2, memrchr3};
2
3
macro_rules! iter_next {
4
    // Common code for the memchr iterators:
5
    // update haystack and position and produce the index
6
    //
7
    // self: &mut Self where Self is the iterator
8
    // search_result: Option<usize> which is the result of the corresponding
9
    // memchr function.
10
    //
11
    // Returns Option<usize> (the next iterator element)
12
    ($self_:expr, $search_result:expr) => {
13
0
        $search_result.map(move |index| {
14
0
            // split and take the remaining back half
15
0
            $self_.haystack = $self_.haystack.split_at(index + 1).1;
16
0
            let found_position = $self_.position + index;
17
0
            $self_.position = found_position + 1;
18
0
            found_position
19
0
        })
Unexecuted instantiation: <memchr::memchr::iter::Memchr as core::iter::traits::iterator::Iterator>::next::{closure#0}
Unexecuted instantiation: <memchr::memchr::iter::Memchr2 as core::iter::traits::iterator::Iterator>::next::{closure#0}
Unexecuted instantiation: <memchr::memchr::iter::Memchr3 as core::iter::traits::iterator::Iterator>::next::{closure#0}
20
    };
21
}
22
23
macro_rules! iter_next_back {
24
    ($self_:expr, $search_result:expr) => {
25
0
        $search_result.map(move |index| {
26
0
            // split and take the remaining front half
27
0
            $self_.haystack = $self_.haystack.split_at(index).0;
28
0
            $self_.position + index
29
0
        })
Unexecuted instantiation: <memchr::memchr::iter::Memchr as core::iter::traits::double_ended::DoubleEndedIterator>::next_back::{closure#0}
Unexecuted instantiation: <memchr::memchr::iter::Memchr2 as core::iter::traits::double_ended::DoubleEndedIterator>::next_back::{closure#0}
Unexecuted instantiation: <memchr::memchr::iter::Memchr3 as core::iter::traits::double_ended::DoubleEndedIterator>::next_back::{closure#0}
30
    };
31
}
32
33
/// An iterator for `memchr`.
34
pub struct Memchr<'a> {
35
    needle: u8,
36
    // The haystack to iterate over
37
    haystack: &'a [u8],
38
    // The index
39
    position: usize,
40
}
41
42
impl<'a> Memchr<'a> {
43
    /// Creates a new iterator that yields all positions of needle in haystack.
44
    #[inline]
45
0
    pub fn new(needle: u8, haystack: &[u8]) -> Memchr<'_> {
46
0
        Memchr { needle: needle, haystack: haystack, position: 0 }
47
0
    }
48
}
49
50
impl<'a> Iterator for Memchr<'a> {
51
    type Item = usize;
52
53
    #[inline]
54
0
    fn next(&mut self) -> Option<usize> {
55
0
        iter_next!(self, memchr(self.needle, self.haystack))
56
0
    }
57
58
    #[inline]
59
0
    fn size_hint(&self) -> (usize, Option<usize>) {
60
0
        (0, Some(self.haystack.len()))
61
0
    }
62
}
63
64
impl<'a> DoubleEndedIterator for Memchr<'a> {
65
    #[inline]
66
0
    fn next_back(&mut self) -> Option<Self::Item> {
67
0
        iter_next_back!(self, memrchr(self.needle, self.haystack))
68
0
    }
69
}
70
71
/// An iterator for `memchr2`.
72
pub struct Memchr2<'a> {
73
    needle1: u8,
74
    needle2: u8,
75
    // The haystack to iterate over
76
    haystack: &'a [u8],
77
    // The index
78
    position: usize,
79
}
80
81
impl<'a> Memchr2<'a> {
82
    /// Creates a new iterator that yields all positions of needle in haystack.
83
    #[inline]
84
0
    pub fn new(needle1: u8, needle2: u8, haystack: &[u8]) -> Memchr2<'_> {
85
0
        Memchr2 {
86
0
            needle1: needle1,
87
0
            needle2: needle2,
88
0
            haystack: haystack,
89
0
            position: 0,
90
0
        }
91
0
    }
92
}
93
94
impl<'a> Iterator for Memchr2<'a> {
95
    type Item = usize;
96
97
    #[inline]
98
0
    fn next(&mut self) -> Option<usize> {
99
0
        iter_next!(self, memchr2(self.needle1, self.needle2, self.haystack))
100
0
    }
101
102
    #[inline]
103
0
    fn size_hint(&self) -> (usize, Option<usize>) {
104
0
        (0, Some(self.haystack.len()))
105
0
    }
106
}
107
108
impl<'a> DoubleEndedIterator for Memchr2<'a> {
109
    #[inline]
110
0
    fn next_back(&mut self) -> Option<Self::Item> {
111
0
        iter_next_back!(
112
0
            self,
113
0
            memrchr2(self.needle1, self.needle2, self.haystack)
114
0
        )
115
0
    }
116
}
117
118
/// An iterator for `memchr3`.
119
pub struct Memchr3<'a> {
120
    needle1: u8,
121
    needle2: u8,
122
    needle3: u8,
123
    // The haystack to iterate over
124
    haystack: &'a [u8],
125
    // The index
126
    position: usize,
127
}
128
129
impl<'a> Memchr3<'a> {
130
    /// Create a new `Memchr3` that's initialized to zero with a haystack
131
    #[inline]
132
0
    pub fn new(
133
0
        needle1: u8,
134
0
        needle2: u8,
135
0
        needle3: u8,
136
0
        haystack: &[u8],
137
0
    ) -> Memchr3<'_> {
138
0
        Memchr3 {
139
0
            needle1: needle1,
140
0
            needle2: needle2,
141
0
            needle3: needle3,
142
0
            haystack: haystack,
143
0
            position: 0,
144
0
        }
145
0
    }
146
}
147
148
impl<'a> Iterator for Memchr3<'a> {
149
    type Item = usize;
150
151
    #[inline]
152
0
    fn next(&mut self) -> Option<usize> {
153
0
        iter_next!(
154
0
            self,
155
0
            memchr3(self.needle1, self.needle2, self.needle3, self.haystack)
156
0
        )
157
0
    }
158
159
    #[inline]
160
0
    fn size_hint(&self) -> (usize, Option<usize>) {
161
0
        (0, Some(self.haystack.len()))
162
0
    }
163
}
164
165
impl<'a> DoubleEndedIterator for Memchr3<'a> {
166
    #[inline]
167
0
    fn next_back(&mut self) -> Option<Self::Item> {
168
0
        iter_next_back!(
169
0
            self,
170
0
            memrchr3(self.needle1, self.needle2, self.needle3, self.haystack)
171
0
        )
172
0
    }
173
}