Skip to content

Commit 7ef7f38

Browse files
ExpHPalexcrichton
authored andcommitted
smaller PR just to fix #50002
1 parent 7d025f4 commit 7ef7f38

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

src/liballoc/tests/str.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,22 @@ fn test_str_get_maxinclusive() {
401401
}
402402
}
403403

404+
#[test]
405+
fn test_str_slicemut_rangetoinclusive_ok() {
406+
let mut s = "abcαβγ".to_owned();
407+
let s: &mut str = &mut s;
408+
&mut s[..=3]; // before alpha
409+
&mut s[..=5]; // after alpha
410+
}
411+
412+
#[test]
413+
#[should_panic]
414+
fn test_str_slicemut_rangetoinclusive_notok() {
415+
let mut s = "abcαβγ".to_owned();
416+
let s: &mut str = &mut s;
417+
&mut s[..=4]; // middle of alpha, which is 2 bytes long
418+
}
419+
404420
#[test]
405421
fn test_is_char_boundary() {
406422
let s = "ศไทย中华Việt Nam β-release 🐱123";

src/libcore/str/mod.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2096,18 +2096,13 @@ mod traits {
20962096
fn index(self, slice: &str) -> &Self::Output {
20972097
assert!(self.end != usize::max_value(),
20982098
"attempted to index str up to maximum usize");
2099-
let end = self.end + 1;
2100-
self.get(slice).unwrap_or_else(|| super::slice_error_fail(slice, 0, end))
2099+
(..self.end+1).index(slice)
21012100
}
21022101
#[inline]
21032102
fn index_mut(self, slice: &mut str) -> &mut Self::Output {
21042103
assert!(self.end != usize::max_value(),
21052104
"attempted to index str up to maximum usize");
2106-
if slice.is_char_boundary(self.end) {
2107-
unsafe { self.get_unchecked_mut(slice) }
2108-
} else {
2109-
super::slice_error_fail(slice, 0, self.end + 1)
2110-
}
2105+
(..self.end+1).index_mut(slice)
21112106
}
21122107
}
21132108

0 commit comments

Comments
 (0)