Skip to content

Commit

Permalink
core: Use memcmp in is_prefix_of / is_suffix_of
Browse files Browse the repository at this point in the history
The basic str equality in core::str calls memcmp, re-use the same
function in StrSearcher's is_prefix_of, is_suffix_of.
  • Loading branch information
bluss committed Jul 4, 2015
1 parent 0dc0824 commit 0da6996
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions src/libcore/str/pattern.rs
Expand Up @@ -513,17 +513,16 @@ impl<'a, 'b> Pattern<'a> for &'b str {
/// Checks whether the pattern matches at the front of the haystack
#[inline]
fn is_prefix_of(self, haystack: &'a str) -> bool {
// Use `as_bytes` so that we can slice through a character in the haystack.
// Since self is always valid UTF-8, this can't result in a false positive.
self.len() <= haystack.len() &&
self.as_bytes() == &haystack.as_bytes()[..self.len()]
haystack.is_char_boundary(self.len()) &&
self == &haystack[..self.len()]
}

/// Checks whether the pattern matches at the back of the haystack
#[inline]
fn is_suffix_of(self, haystack: &'a str) -> bool {
self.len() <= haystack.len() &&
self.as_bytes() == &haystack.as_bytes()[haystack.len() - self.len()..]
haystack.is_char_boundary(haystack.len() - self.len()) &&
self == &haystack[haystack.len() - self.len()..]
}
}

Expand Down

0 comments on commit 0da6996

Please sign in to comment.