pub struct Three {
sse2: Three<__m128i>,
avx2: Three<__m256i>,
}
Expand description
Finds all occurrences of three bytes in a haystack.
That is, this reports matches of one of three possible bytes. For example,
searching for a
, b
or o
in afoobar
would report matches at offsets
0
, 2
, 3
, 4
and 5
.
Fields§
§sse2: Three<__m128i>
Used for haystacks less than 32 bytes.
avx2: Three<__m256i>
Used for haystacks bigger than 32 bytes.
Implementations§
source§impl Three
impl Three
sourcepub fn new(needle1: u8, needle2: u8, needle3: u8) -> Option<Three>
pub fn new(needle1: u8, needle2: u8, needle3: u8) -> Option<Three>
Create a new searcher that finds occurrences of the needle bytes given.
This particular searcher is specialized to use AVX2 vector instructions that typically make it quite fast. (SSE2 is used for haystacks that are too short to accommodate an AVX2 vector.)
If either SSE2 or AVX2 is unavailable in the current environment, then
None
is returned.
sourcepub unsafe fn new_unchecked(needle1: u8, needle2: u8, needle3: u8) -> Three
Available on target features sse2
and avx2
only.
pub unsafe fn new_unchecked(needle1: u8, needle2: u8, needle3: u8) -> Three
sse2
and avx2
only.Create a new finder specific to AVX2 vectors and routines without checking that either SSE2 or AVX2 is available.
§Safety
Callers must guarantee that it is safe to execute both sse2
and
avx2
instructions in the current environment.
Note that it is a common misconception that if one compiles for an
x86_64
target, then they therefore automatically have access to SSE2
instructions. While this is almost always the case, it isn’t true in
100% of cases.
sourcepub fn is_available() -> bool
pub fn is_available() -> bool
Returns true when this implementation is available in the current environment.
When this is true, it is guaranteed that Three::new
will return
a Some
value. Similarly, when it is false, it is guaranteed that
Three::new
will return a None
value.
Note also that for the lifetime of a single program, if this returns true then it will always return true.
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 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 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.
sourceunsafe fn find_raw_sse2(
&self,
start: *const u8,
end: *const u8,
) -> Option<*const u8>
Available with target feature sse2
only.
unsafe fn find_raw_sse2( &self, start: *const u8, end: *const u8, ) -> Option<*const u8>
sse2
only.Execute a search using SSE2 vectors and routines.
§Safety
Same as Three::find_raw
, except the distance between start
and
end
must be at least the size of an SSE2 vector (in bytes).
(The target feature safety obligation is automatically fulfilled by
virtue of being a method on Three
, which can only be constructed
when it is safe to call sse2
/avx2
routines.)
sourceunsafe fn rfind_raw_sse2(
&self,
start: *const u8,
end: *const u8,
) -> Option<*const u8>
Available with target feature sse2
only.
unsafe fn rfind_raw_sse2( &self, start: *const u8, end: *const u8, ) -> Option<*const u8>
sse2
only.Execute a search using SSE2 vectors and routines.
§Safety
Same as Three::rfind_raw
, except the distance between start
and
end
must be at least the size of an SSE2 vector (in bytes).
(The target feature safety obligation is automatically fulfilled by
virtue of being a method on Three
, which can only be constructed
when it is safe to call sse2
/avx2
routines.)
sourceunsafe fn find_raw_avx2(
&self,
start: *const u8,
end: *const u8,
) -> Option<*const u8>
Available with target feature avx2
only.
unsafe fn find_raw_avx2( &self, start: *const u8, end: *const u8, ) -> Option<*const u8>
avx2
only.Execute a search using AVX2 vectors and routines.
§Safety
Same as Three::find_raw
, except the distance between start
and
end
must be at least the size of an AVX2 vector (in bytes).
(The target feature safety obligation is automatically fulfilled by
virtue of being a method on Three
, which can only be constructed
when it is safe to call sse2
/avx2
routines.)
sourceunsafe fn rfind_raw_avx2(
&self,
start: *const u8,
end: *const u8,
) -> Option<*const u8>
Available with target feature avx2
only.
unsafe fn rfind_raw_avx2( &self, start: *const u8, end: *const u8, ) -> Option<*const u8>
avx2
only.Execute a search using AVX2 vectors and routines.
§Safety
Same as Three::rfind_raw
, except the distance between start
and
end
must be at least the size of an AVX2 vector (in bytes).
(The target feature safety obligation is automatically fulfilled by
virtue of being a method on Three
, which can only be constructed
when it is safe to call sse2
/avx2
routines.)
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Three
impl RefUnwindSafe for Three
impl Send for Three
impl Sync for Three
impl Unpin for Three
impl UnwindSafe for Three
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)