Skip to content

Commit 22ad6b3

Browse files
tsnoamshepmaster
authored andcommitted
ByteSubstring now takes the needle as generic type T instead of &[u8]
This allows to either own (consume) the needle or keep a reference to it.
1 parent 655bfaf commit 22ad6b3

File tree

3 files changed

+29
-20
lines changed

3 files changed

+29
-20
lines changed

src/fallback.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,26 @@ where
2121
}
2222
}
2323

24-
pub struct ByteSubstring<'a> {
25-
needle: &'a [u8],
24+
pub struct ByteSubstring<T> {
25+
needle: T,
2626
}
2727

28-
impl<'a> ByteSubstring<'a> {
29-
pub /* const */ fn new(needle: &'a[u8]) -> Self {
28+
impl<T> ByteSubstring<T>
29+
where
30+
T: AsRef<[u8]>,
31+
{
32+
pub /* const */ fn new(needle: T) -> Self {
3033
ByteSubstring { needle }
3134
}
3235

3336
#[cfg(feature = "pattern")]
3437
pub fn needle_len(&self) -> usize {
35-
self.needle.len()
38+
self.needle.as_ref().len()
3639
}
3740

3841
pub fn find(&self, haystack: &[u8]) -> Option<usize> {
3942
haystack
40-
.windows(self.needle.len())
41-
.position(|window| window == self.needle)
43+
.windows(self.needle.as_ref().len())
44+
.position(|window| window == self.needle.as_ref())
4245
}
4346
}

src/lib.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -282,16 +282,19 @@ where
282282
pub type AsciiCharsConst = AsciiChars<fn(u8) -> bool>;
283283

284284
/// Searches a slice for the first occurence of the subslice.
285-
pub struct ByteSubstring<'a> {
285+
pub struct ByteSubstring<T> {
286286
#[cfg(any(jetscii_sse4_2 = "yes", jetscii_sse4_2 = "maybe"))]
287287
simd: simd::ByteSubstring<'a>,
288288

289289
#[cfg(any(jetscii_sse4_2 = "maybe", jetscii_sse4_2 = "no"))]
290-
fallback: fallback::ByteSubstring<'a>,
290+
fallback: fallback::ByteSubstring<T>,
291291
}
292292

293-
impl<'a> ByteSubstring<'a> {
294-
pub /* const */ fn new(needle: &'a [u8]) -> Self {
293+
impl<T> ByteSubstring<T>
294+
where
295+
T: AsRef<[u8]>,
296+
{
297+
pub /* const */ fn new(needle: T) -> Self {
295298
ByteSubstring {
296299
#[cfg(any(jetscii_sse4_2 = "yes", jetscii_sse4_2 = "maybe"))]
297300
simd: simd::ByteSubstring::new(needle),
@@ -320,10 +323,10 @@ impl<'a> ByteSubstring<'a> {
320323
}
321324

322325
/// A convenience type that can be used in a constant or static.
323-
pub type ByteSubstringConst = ByteSubstring<'static>;
326+
pub type ByteSubstringConst = ByteSubstring<&'static [u8]>;
324327

325328
/// Searches a string for the first occurence of the substring.
326-
pub struct Substring<'a>(ByteSubstring<'a>);
329+
pub struct Substring<'a>(ByteSubstring<&'a [u8]>);
327330

328331
impl<'a> Substring<'a> {
329332
pub /* const */ fn new(needle: &'a str) -> Self {

src/simd.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -260,19 +260,22 @@ impl<'b> PackedCompareControl for &'b Bytes {
260260
}
261261
}
262262

263-
pub struct ByteSubstring<'a> {
264-
complete_needle: &'a [u8],
263+
pub struct ByteSubstring<T> {
264+
complete_needle: T,
265265
needle: __m128i,
266266
needle_len: i32,
267267
}
268268

269-
impl<'a> ByteSubstring<'a> {
270-
pub /* const */ fn new(needle: &'a[u8]) -> Self {
269+
impl<T> ByteSubstring<T>
270+
where
271+
T: AsRef<[u8]>,
272+
{
273+
pub /* const */ fn new(needle: T) -> Self {
271274
use std::cmp;
272275

273276
let mut simd_needle = [0; 16];
274277
let len = cmp::min(simd_needle.len(), needle.len());
275-
simd_needle[..len].copy_from_slice(&needle[..len]);
278+
simd_needle[..len].copy_from_slice(&needle.as_ref()[..len]);
276279
ByteSubstring {
277280
complete_needle: needle,
278281
needle: unsafe { TransmuteToSimd { bytes: simd_needle }.simd },
@@ -282,7 +285,7 @@ impl<'a> ByteSubstring<'a> {
282285

283286
#[cfg(feature = "pattern")]
284287
pub fn needle_len(&self) -> usize {
285-
self.complete_needle.len()
288+
self.complete_needle.as_ref().len()
286289
}
287290

288291
#[inline]
@@ -293,7 +296,7 @@ impl<'a> ByteSubstring<'a> {
293296
while let Some(idx) = find(PackedCompare::<_, _SIDD_CMP_EQUAL_ORDERED>(self), &haystack[offset..]) {
294297
let abs_offset = offset + idx;
295298
// Found a match, but is it really?
296-
if haystack[abs_offset..].starts_with(self.complete_needle) {
299+
if haystack[abs_offset..].starts_with(self.complete_needle.as_ref()) {
297300
return Some(abs_offset);
298301
}
299302

0 commit comments

Comments
 (0)