Skip to content

Commit b74d692

Browse files
committed
smaller PR just to fix #50002
1 parent 9a59133 commit b74d692

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

src/liballoc/tests/str.rs

+16
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

+2-7
Original file line numberDiff line numberDiff line change
@@ -2100,18 +2100,13 @@ mod traits {
21002100
fn index(self, slice: &str) -> &Self::Output {
21012101
assert!(self.end != usize::max_value(),
21022102
"attempted to index str up to maximum usize");
2103-
let end = self.end + 1;
2104-
self.get(slice).unwrap_or_else(|| super::slice_error_fail(slice, 0, end))
2103+
(..self.end+1).index(slice)
21052104
}
21062105
#[inline]
21072106
fn index_mut(self, slice: &mut str) -> &mut Self::Output {
21082107
assert!(self.end != usize::max_value(),
21092108
"attempted to index str up to maximum usize");
2110-
if slice.is_char_boundary(self.end) {
2111-
unsafe { self.get_unchecked_mut(slice) }
2112-
} else {
2113-
super::slice_error_fail(slice, 0, self.end + 1)
2114-
}
2109+
(..self.end+1).index_mut(slice)
21152110
}
21162111
}
21172112

0 commit comments

Comments
 (0)