pub struct Two { /* private fields */ }
Expand description
Finds all occurrences of two bytes in a haystack.
That is, this reports matches of one of two possible bytes. For example,
searching for a
or b
in afoobar
would report matches at offsets 0
,
4
and 5
.
Implementations§
source§impl Two
impl Two
sourcepub fn new(needle1: u8, needle2: u8) -> Two
pub fn new(needle1: u8, needle2: u8) -> Two
Create a new searcher that finds occurrences of the two needle bytes given.
sourcepub fn find(&self, haystack: &[u8]) -> Option<usize>
pub fn find(&self, haystack: &[u8]) -> Option<usize>
Return the first occurrence of one of the needle bytes in the given
haystack. If no such occurrence exists, then None
is returned.
The occurrence is reported as an offset into haystack
. Its maximum
value for a non-empty haystack is haystack.len() - 1
.
sourcepub fn rfind(&self, haystack: &[u8]) -> Option<usize>
pub fn rfind(&self, haystack: &[u8]) -> Option<usize>
Return the last occurrence of one of the needle bytes in the given
haystack. If no such occurrence exists, then None
is returned.
The occurrence is reported as an offset into haystack
. Its maximum
value for a non-empty haystack is haystack.len() - 1
.
sourcepub unsafe fn find_raw(
&self,
start: *const u8,
end: *const u8
) -> Option<*const u8>
pub unsafe fn find_raw( &self, start: *const u8, end: *const u8 ) -> Option<*const u8>
Like find
, but accepts and returns raw pointers.
When a match is found, the pointer returned is guaranteed to be
>= start
and < end
.
This routine is useful if you’re already using raw pointers and would like to avoid converting back to a slice before executing a search.
§Safety
- Both
start
andend
must be valid for reads. - Both
start
andend
must point to an initialized value. - Both
start
andend
must point to the same allocated object and must either be in bounds or at most one byte past the end of the allocated object. - Both
start
andend
must be derived from a pointer to the same object. - The distance between
start
andend
must not overflowisize
. - The distance being in bounds must not rely on “wrapping around” the address space.
Note that callers may pass a pair of pointers such that start >= end
.
In that case, None
will always be returned.
sourcepub unsafe fn rfind_raw(
&self,
start: *const u8,
end: *const u8
) -> Option<*const u8>
pub unsafe fn rfind_raw( &self, start: *const u8, end: *const u8 ) -> Option<*const u8>
Like rfind
, but accepts and returns raw pointers.
When a match is found, the pointer returned is guaranteed to be
>= start
and < end
.
This routine is useful if you’re already using raw pointers and would like to avoid converting back to a slice before executing a search.
§Safety
- Both
start
andend
must be valid for reads. - Both
start
andend
must point to an initialized value. - Both
start
andend
must point to the same allocated object and must either be in bounds or at most one byte past the end of the allocated object. - Both
start
andend
must be derived from a pointer to the same object. - The distance between
start
andend
must not overflowisize
. - The distance being in bounds must not rely on “wrapping around” the address space.
Note that callers may pass a pair of pointers such that start >= end
.
In that case, None
will always be returned.