Skip to content

Commit f203d0b

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 9139b3d commit f203d0b

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
@@ -248,19 +248,22 @@ impl<'b> PackedCompareControl for &'b Bytes {
248248
}
249249
}
250250

251-
pub struct ByteSubstring<'a> {
252-
complete_needle: &'a [u8],
251+
pub struct ByteSubstring<T> {
252+
complete_needle: T,
253253
needle: __m128i,
254254
needle_len: i32,
255255
}
256256

257-
impl<'a> ByteSubstring<'a> {
258-
pub /* const */ fn new(needle: &'a[u8]) -> Self {
257+
impl<T> ByteSubstring<T>
258+
where
259+
T: AsRef<[u8]>,
260+
{
261+
pub /* const */ fn new(needle: T) -> Self {
259262
use std::cmp;
260263

261264
let mut simd_needle = [0; 16];
262265
let len = cmp::min(simd_needle.len(), needle.len());
263-
simd_needle[..len].copy_from_slice(&needle[..len]);
266+
simd_needle[..len].copy_from_slice(&needle.as_ref()[..len]);
264267
ByteSubstring {
265268
complete_needle: needle,
266269
needle: unsafe { TransmuteToSimd { bytes: simd_needle }.simd },
@@ -270,7 +273,7 @@ impl<'a> ByteSubstring<'a> {
270273

271274
#[cfg(feature = "pattern")]
272275
pub fn needle_len(&self) -> usize {
273-
self.complete_needle.len()
276+
self.complete_needle.as_ref().len()
274277
}
275278

276279
#[inline]
@@ -281,7 +284,7 @@ impl<'a> ByteSubstring<'a> {
281284
while let Some(idx) = find(PackedCompare::<_, _SIDD_CMP_EQUAL_ORDERED>(self), &haystack[offset..]) {
282285
let abs_offset = offset + idx;
283286
// Found a match, but is it really?
284-
if haystack[abs_offset..].starts_with(self.complete_needle) {
287+
if haystack[abs_offset..].starts_with(self.complete_needle.as_ref()) {
285288
return Some(abs_offset);
286289
}
287290

0 commit comments

Comments
 (0)