Skip to content

Commit

Permalink
FindSubstring<&[u8]>: make use of memchr::memmem
Browse files Browse the repository at this point in the history
  • Loading branch information
homersimpsons authored and epage committed Jan 27, 2023
1 parent d7635eb commit 489aa2a
Show file tree
Hide file tree
Showing 2 changed files with 1,398 additions and 29 deletions.
30 changes: 1 addition & 29 deletions src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2247,35 +2247,7 @@ where

impl<'a, 'b> FindSubstring<&'b [u8]> for &'a [u8] {
fn find_substring(&self, substr: &'b [u8]) -> Option<usize> {
if substr.len() > self.len() {
return None;
}

let (&substr_first, substr_rest) = match substr.split_first() {
Some(split) => split,
// an empty substring is found at position 0
// This matches the behavior of str.find("").
None => return Some(0),
};

if substr_rest.is_empty() {
return memchr::memchr(substr_first, self);
}

let mut offset = 0;
let haystack = &self[..self.len() - substr_rest.len()];

while let Some(position) = memchr::memchr(substr_first, &haystack[offset..]) {
offset += position;
let next_offset = offset + 1;
if &self[next_offset..][..substr_rest.len()] == substr_rest {
return Some(offset);
}

offset = next_offset;
}

None
memchr::memmem::find(self, substr)
}
}

Expand Down
Loading

0 comments on commit 489aa2a

Please sign in to comment.