Skip to content

Commit 0da6996

Browse files
committedJul 4, 2015
core: Use memcmp in is_prefix_of / is_suffix_of
The basic str equality in core::str calls memcmp, re-use the same function in StrSearcher's is_prefix_of, is_suffix_of.
1 parent 0dc0824 commit 0da6996

File tree

1 file changed

+4
-5
lines changed

1 file changed

+4
-5
lines changed
 

‎src/libcore/str/pattern.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -513,17 +513,16 @@ impl<'a, 'b> Pattern<'a> for &'b str {
513513
/// Checks whether the pattern matches at the front of the haystack
514514
#[inline]
515515
fn is_prefix_of(self, haystack: &'a str) -> bool {
516-
// Use `as_bytes` so that we can slice through a character in the haystack.
517-
// Since self is always valid UTF-8, this can't result in a false positive.
518-
self.len() <= haystack.len() &&
519-
self.as_bytes() == &haystack.as_bytes()[..self.len()]
516+
haystack.is_char_boundary(self.len()) &&
517+
self == &haystack[..self.len()]
520518
}
521519

522520
/// Checks whether the pattern matches at the back of the haystack
523521
#[inline]
524522
fn is_suffix_of(self, haystack: &'a str) -> bool {
525523
self.len() <= haystack.len() &&
526-
self.as_bytes() == &haystack.as_bytes()[haystack.len() - self.len()..]
524+
haystack.is_char_boundary(haystack.len() - self.len()) &&
525+
self == &haystack[haystack.len() - self.len()..]
527526
}
528527
}
529528

0 commit comments

Comments
 (0)
Please sign in to comment.