about summary refs log tree commit diff
path: root/src/libstd
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2013-10-18 00:26:19 -0700
committerbors <bors@rust-lang.org>2013-10-18 00:26:19 -0700
commit71c3f8c20c67e8e18f9b35817929fc2d28dd434d (patch)
tree4e3a63498862bdfd204c49653abecc85ac909462 /src/libstd
parentd0d554456a9c641c873f34c3f03a8679065d0f07 (diff)
parent090b2453a1a2df8ebd6216855515339822823d80 (diff)
downloadrust-71c3f8c20c67e8e18f9b35817929fc2d28dd434d.tar.gz
rust-71c3f8c20c67e8e18f9b35817929fc2d28dd434d.zip
auto merge of #9924 : metajack/rust/fix-starts-with-ends-with, r=huonw
d4a32386f3b6 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.
Diffstat (limited to 'src/libstd')
-rw-r--r--src/libstd/str.rs8
1 files changed, 6 insertions, 2 deletions
diff --git a/src/libstd/str.rs b/src/libstd/str.rs
index 5c6974de17d..9d2f60fc27c 100644
--- a/src/libstd/str.rs
+++ b/src/libstd/str.rs
@@ -2010,13 +2010,13 @@ impl<'self> StrSlice<'self> for &'self str {
     #[inline]
     fn starts_with<'a>(&self, needle: &'a str) -> bool {
         let n = needle.len();
-        self.len() >= n && needle == self.slice_to(n)
+        self.len() >= n && needle.as_bytes() == self.as_bytes().slice_to(n)
     }
 
     #[inline]
     fn ends_with(&self, needle: &str) -> bool {
         let (m, n) = (self.len(), needle.len());
-        m >= n && needle == self.slice_from(m - n)
+        m >= n && needle.as_bytes() == self.as_bytes().slice_from(m - n)
     }
 
     fn escape_default(&self) -> ~str {
@@ -2886,6 +2886,8 @@ mod tests {
         assert!(("abc".starts_with("a")));
         assert!((!"a".starts_with("abc")));
         assert!((!"".starts_with("abc")));
+        assert!((!"ödd".starts_with("-")));
+        assert!(("ödd".starts_with("öd")));
     }
 
     #[test]
@@ -2895,6 +2897,8 @@ mod tests {
         assert!(("abc".ends_with("c")));
         assert!((!"a".ends_with("abc")));
         assert!((!"".ends_with("abc")));
+        assert!((!"ddö".ends_with("-")));
+        assert!(("ddö".ends_with("dö")));
     }
 
     #[test]