Skip to content

Commit

Permalink
feat(stream): Allow finding chars in byte slices
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Feb 12, 2024
1 parent e38ab2b commit ed56b33
Showing 1 changed file with 46 additions and 18 deletions.
64 changes: 46 additions & 18 deletions src/stream/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2489,6 +2489,48 @@ impl<'i, 's> FindSlice<(&'s [u8], &'s [u8], &'s [u8])> for &'i [u8] {
}
}

impl<'i> FindSlice<char> for &'i [u8] {
#[inline(always)]
fn find_slice(&self, substr: char) -> Option<usize> {
let mut b = [0; 4];
let substr = substr.encode_utf8(&mut b);
self.find_slice(&*substr)

Check warning on line 2497 in src/stream/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/stream/mod.rs#L2494-L2497

Added lines #L2494 - L2497 were not covered by tests
}
}

impl<'i> FindSlice<(char,)> for &'i [u8] {
#[inline(always)]
fn find_slice(&self, substr: (char,)) -> Option<usize> {
let mut b = [0; 4];
let substr0 = substr.0.encode_utf8(&mut b);
self.find_slice((&*substr0,))

Check warning on line 2506 in src/stream/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/stream/mod.rs#L2503-L2506

Added lines #L2503 - L2506 were not covered by tests
}
}

impl<'i> FindSlice<(char, char)> for &'i [u8] {
#[inline(always)]
fn find_slice(&self, substr: (char, char)) -> Option<usize> {
let mut b = [0; 4];
let substr0 = substr.0.encode_utf8(&mut b);
let mut b = [0; 4];
let substr1 = substr.1.encode_utf8(&mut b);
self.find_slice((&*substr0, &*substr1))

Check warning on line 2517 in src/stream/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/stream/mod.rs#L2512-L2517

Added lines #L2512 - L2517 were not covered by tests
}
}

impl<'i> FindSlice<(char, char, char)> for &'i [u8] {
#[inline(always)]
fn find_slice(&self, substr: (char, char, char)) -> Option<usize> {
let mut b = [0; 4];
let substr0 = substr.0.encode_utf8(&mut b);
let mut b = [0; 4];
let substr1 = substr.1.encode_utf8(&mut b);
let mut b = [0; 4];
let substr2 = substr.2.encode_utf8(&mut b);
self.find_slice((&*substr0, &*substr1, &*substr2))

Check warning on line 2530 in src/stream/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/stream/mod.rs#L2523-L2530

Added lines #L2523 - L2530 were not covered by tests
}
}

impl<'i> FindSlice<u8> for &'i [u8] {
#[inline(always)]
fn find_slice(&self, substr: u8) -> Option<usize> {
Expand Down Expand Up @@ -2583,42 +2625,28 @@ impl<'i, 's> FindSlice<(&'s str, &'s str, &'s str)> for &'i str {
impl<'i> FindSlice<char> for &'i str {
#[inline(always)]
fn find_slice(&self, substr: char) -> Option<usize> {
let mut b = [0; 4];
let substr = substr.encode_utf8(&mut b);
self.find_slice(&*substr)
self.as_bytes().find_slice(substr)

Check warning on line 2628 in src/stream/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/stream/mod.rs#L2628

Added line #L2628 was not covered by tests
}
}

impl<'i> FindSlice<(char,)> for &'i str {
#[inline(always)]
fn find_slice(&self, substr: (char,)) -> Option<usize> {
let mut b = [0; 4];
let substr0 = substr.0.encode_utf8(&mut b);
self.find_slice((&*substr0,))
self.as_bytes().find_slice(substr)

Check warning on line 2635 in src/stream/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/stream/mod.rs#L2635

Added line #L2635 was not covered by tests
}
}

impl<'i> FindSlice<(char, char)> for &'i str {
#[inline(always)]
fn find_slice(&self, substr: (char, char)) -> Option<usize> {
let mut b = [0; 4];
let substr0 = substr.0.encode_utf8(&mut b);
let mut b = [0; 4];
let substr1 = substr.1.encode_utf8(&mut b);
self.find_slice((&*substr0, &*substr1))
self.as_bytes().find_slice(substr)

Check warning on line 2642 in src/stream/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/stream/mod.rs#L2642

Added line #L2642 was not covered by tests
}
}

impl<'i> FindSlice<(char, char, char)> for &'i str {
#[inline(always)]
fn find_slice(&self, substr: (char, char, char)) -> Option<usize> {
let mut b = [0; 4];
let substr0 = substr.0.encode_utf8(&mut b);
let mut b = [0; 4];
let substr1 = substr.1.encode_utf8(&mut b);
let mut b = [0; 4];
let substr2 = substr.2.encode_utf8(&mut b);
self.find_slice((&*substr0, &*substr1, &*substr2))
self.as_bytes().find_slice(substr)

Check warning on line 2649 in src/stream/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/stream/mod.rs#L2649

Added line #L2649 was not covered by tests
}
}

Expand Down

0 comments on commit ed56b33

Please sign in to comment.