]> git.lizzy.rs Git - rust.git/commitdiff
smaller PR just to fix #50002
authorMichael Lamparski <diagonaldevice@gmail.com>
Mon, 16 Apr 2018 20:34:09 +0000 (16:34 -0400)
committerMichael Lamparski <diagonaldevice@gmail.com>
Wed, 18 Apr 2018 01:31:35 +0000 (21:31 -0400)
src/liballoc/tests/str.rs
src/libcore/str/mod.rs

index a3f4c385fe23b731b52f329312790a1ba494253b..e3f198f73623228a263d7ed4b9a6e2704258e818 100644 (file)
@@ -401,6 +401,22 @@ fn test_str_get_maxinclusive() {
     }
 }
 
+#[test]
+fn test_str_slicemut_rangetoinclusive_ok() {
+    let mut s = "abcαβγ".to_owned();
+    let s: &mut str = &mut s;
+    &mut s[..=3]; // before alpha
+    &mut s[..=5]; // after alpha
+}
+
+#[test]
+#[should_panic]
+fn test_str_slicemut_rangetoinclusive_notok() {
+    let mut s = "abcαβγ".to_owned();
+    let s: &mut str = &mut s;
+    &mut s[..=4]; // middle of alpha, which is 2 bytes long
+}
+
 #[test]
 fn test_is_char_boundary() {
     let s = "ศไทย中华Việt Nam β-release 🐱123";
index f1fe23092de93924f2c1549758bcb84462cfc08b..5b52119d0310abccd0035239fe33f5c427e7db4d 100644 (file)
@@ -2100,18 +2100,13 @@ unsafe fn get_unchecked_mut(self, slice: &mut str) -> &mut Self::Output {
         fn index(self, slice: &str) -> &Self::Output {
             assert!(self.end != usize::max_value(),
                 "attempted to index str up to maximum usize");
-            let end = self.end + 1;
-            self.get(slice).unwrap_or_else(|| super::slice_error_fail(slice, 0, end))
+            (..self.end+1).index(slice)
         }
         #[inline]
         fn index_mut(self, slice: &mut str) -> &mut Self::Output {
             assert!(self.end != usize::max_value(),
                 "attempted to index str up to maximum usize");
-            if slice.is_char_boundary(self.end) {
-                unsafe { self.get_unchecked_mut(slice) }
-            } else {
-                super::slice_error_fail(slice, 0, self.end + 1)
-            }
+            (..self.end+1).index_mut(slice)
         }
     }