Skip to content

Commit b381449

Browse files
committed
Auto merge of #26771 - bluss:str-prefix, r=Gankro
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.
2 parents c83f8f9 + c5a0a73 commit b381449

File tree

2 files changed

+5
-8
lines changed

2 files changed

+5
-8
lines changed

src/libcollections/str.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -483,9 +483,7 @@ impl str {
483483
/// considered to be
484484
/// boundaries.
485485
///
486-
/// # Panics
487-
///
488-
/// Panics if `index` is greater than `self.len()`.
486+
/// Returns `false` if `index` is greater than `self.len()`.
489487
///
490488
/// # Examples
491489
///

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)