From b74d6922ff15d9f3bf39d317ccd7141518f4f5ec Mon Sep 17 00:00:00 2001 From: Michael Lamparski Date: Mon, 16 Apr 2018 16:34:09 -0400 Subject: [PATCH 1/2] smaller PR just to fix #50002 --- src/liballoc/tests/str.rs | 16 ++++++++++++++++ src/libcore/str/mod.rs | 9 ++------- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/src/liballoc/tests/str.rs b/src/liballoc/tests/str.rs index a3f4c385fe23b..e3f198f736232 100644 --- a/src/liballoc/tests/str.rs +++ b/src/liballoc/tests/str.rs @@ -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"; diff --git a/src/libcore/str/mod.rs b/src/libcore/str/mod.rs index f1fe23092de93..5b52119d0310a 100644 --- a/src/libcore/str/mod.rs +++ b/src/libcore/str/mod.rs @@ -2100,18 +2100,13 @@ mod traits { 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) } } From 90b361b3a748e9fb01cd9aec7b83edca2d9e996e Mon Sep 17 00:00:00 2001 From: Michael Lamparski Date: Wed, 18 Apr 2018 16:48:34 -0400 Subject: [PATCH 2/2] fix my unit test that was horrendously wrong and add one for non-mut slicing since I touched that method too --- src/liballoc/tests/str.rs | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/liballoc/tests/str.rs b/src/liballoc/tests/str.rs index e3f198f736232..a03b61ec97e51 100644 --- a/src/liballoc/tests/str.rs +++ b/src/liballoc/tests/str.rs @@ -401,12 +401,26 @@ fn test_str_get_maxinclusive() { } } +#[test] +fn test_str_slice_rangetoinclusive_ok() { + let s = "abcαβγ"; + assert_eq!(&s[..=2], "abc"); + assert_eq!(&s[..=4], "abcα"); +} + +#[test] +#[should_panic] +fn test_str_slice_rangetoinclusive_notok() { + let s = "abcαβγ"; + &s[..=3]; +} + #[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 + assert_eq!(&mut s[..=2], "abc"); + assert_eq!(&mut s[..=4], "abcα"); } #[test] @@ -414,7 +428,7 @@ fn test_str_slicemut_rangetoinclusive_ok() { 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 + &mut s[..=3]; } #[test]