Skip to content

Commit 7a5e775

Browse files
committed
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 868b04c commit 7a5e775

File tree

3 files changed

+20
-20
lines changed

3 files changed

+20
-20
lines changed

src/fallback.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,23 @@ 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> where T: AsRef<[u8]> {
29+
pub /* const */ fn new(needle: T) -> Self {
3030
ByteSubstring { needle }
3131
}
3232

3333
#[cfg(feature = "pattern")]
3434
pub fn needle_len(&self) -> usize {
35-
self.needle.len()
35+
self.needle.as_ref().len()
3636
}
3737

3838
pub fn find(&self, haystack: &[u8]) -> Option<usize> {
3939
haystack
40-
.windows(self.needle.len())
41-
.position(|window| window == self.needle)
40+
.windows(self.needle.as_ref().len())
41+
.position(|window| window == self.needle.as_ref())
4242
}
4343
}

src/lib.rs

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

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

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

294-
impl<'a> ByteSubstring<'a> {
295-
pub /* const */ fn new(needle: &'a [u8]) -> Self {
294+
impl<T> ByteSubstring<T> where T: AsRef<[u8]> {
295+
pub /* const */ fn new(needle: T) -> Self {
296296
ByteSubstring {
297297
#[cfg(any(jetscii_sse4_2 = "yes", jetscii_sse4_2 = "maybe"))]
298298
simd: simd::ByteSubstring::new(needle),
@@ -321,10 +321,10 @@ impl<'a> ByteSubstring<'a> {
321321
}
322322

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

326326
/// Searches a string for the first occurence of the substring.
327-
pub struct Substring<'a>(ByteSubstring<'a>);
327+
pub struct Substring<'a>(ByteSubstring<&'a [u8]>);
328328

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

src/simd.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -248,19 +248,19 @@ 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> where T: AsRef<[u8]> {
258+
pub /* const */ fn new(needle: T) -> Self {
259259
use std::cmp;
260260

261261
let mut simd_needle = [0; 16];
262262
let len = cmp::min(simd_needle.len(), needle.len());
263-
simd_needle[..len].copy_from_slice(&needle[..len]);
263+
simd_needle[..len].copy_from_slice(&needle.as_ref()[..len]);
264264
ByteSubstring {
265265
complete_needle: needle,
266266
needle: unsafe { TransmuteToSimd { bytes: simd_needle }.simd },
@@ -270,7 +270,7 @@ impl<'a> ByteSubstring<'a> {
270270

271271
#[cfg(feature = "pattern")]
272272
pub fn needle_len(&self) -> usize {
273-
self.complete_needle.len()
273+
self.complete_needle.as_ref().len()
274274
}
275275

276276
#[inline]
@@ -281,7 +281,7 @@ impl<'a> ByteSubstring<'a> {
281281
while let Some(idx) = find(PackedCompare::<_, _SIDD_CMP_EQUAL_ORDERED>(self), &haystack[offset..]) {
282282
let abs_offset = offset + idx;
283283
// Found a match, but is it really?
284-
if haystack[abs_offset..].starts_with(self.complete_needle) {
284+
if haystack[abs_offset..].starts_with(self.complete_needle.as_ref()) {
285285
return Some(abs_offset);
286286
}
287287

0 commit comments

Comments
 (0)