Skip to content

Commit 71c3f8c

Browse files
committed
auto merge of #9924 : metajack/rust/fix-starts-with-ends-with, r=huonw
d4a3238 broke these since slice_to() and slice_from() must get character boundaries, and arbitrary needle lengths don't necessarily map to character boundaries of the haystack. This also adds new tests that would have caught this bug.
2 parents d0d5544 + 090b245 commit 71c3f8c

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

src/libstd/str.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -2010,13 +2010,13 @@ impl<'self> StrSlice<'self> for &'self str {
20102010
#[inline]
20112011
fn starts_with<'a>(&self, needle: &'a str) -> bool {
20122012
let n = needle.len();
2013-
self.len() >= n && needle == self.slice_to(n)
2013+
self.len() >= n && needle.as_bytes() == self.as_bytes().slice_to(n)
20142014
}
20152015
20162016
#[inline]
20172017
fn ends_with(&self, needle: &str) -> bool {
20182018
let (m, n) = (self.len(), needle.len());
2019-
m >= n && needle == self.slice_from(m - n)
2019+
m >= n && needle.as_bytes() == self.as_bytes().slice_from(m - n)
20202020
}
20212021
20222022
fn escape_default(&self) -> ~str {
@@ -2886,6 +2886,8 @@ mod tests {
28862886
assert!(("abc".starts_with("a")));
28872887
assert!((!"a".starts_with("abc")));
28882888
assert!((!"".starts_with("abc")));
2889+
assert!((!"ödd".starts_with("-")));
2890+
assert!(("ödd".starts_with("öd")));
28892891
}
28902892
28912893
#[test]
@@ -2895,6 +2897,8 @@ mod tests {
28952897
assert!(("abc".ends_with("c")));
28962898
assert!((!"a".ends_with("abc")));
28972899
assert!((!"".ends_with("abc")));
2900+
assert!((!"ddö".ends_with("-")));
2901+
assert!(("ddö".ends_with("")));
28982902
}
28992903
29002904
#[test]

0 commit comments

Comments
 (0)