smaller PR just to fix #50002
This commit is contained in:
parent
9a59133c09
commit
b74d6922ff
2 changed files with 18 additions and 7 deletions
|
@ -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]
|
#[test]
|
||||||
fn test_is_char_boundary() {
|
fn test_is_char_boundary() {
|
||||||
let s = "ศไทย中华Việt Nam β-release 🐱123";
|
let s = "ศไทย中华Việt Nam β-release 🐱123";
|
||||||
|
|
|
@ -2100,18 +2100,13 @@ mod traits {
|
||||||
fn index(self, slice: &str) -> &Self::Output {
|
fn index(self, slice: &str) -> &Self::Output {
|
||||||
assert!(self.end != usize::max_value(),
|
assert!(self.end != usize::max_value(),
|
||||||
"attempted to index str up to maximum usize");
|
"attempted to index str up to maximum usize");
|
||||||
let end = self.end + 1;
|
(..self.end+1).index(slice)
|
||||||
self.get(slice).unwrap_or_else(|| super::slice_error_fail(slice, 0, end))
|
|
||||||
}
|
}
|
||||||
#[inline]
|
#[inline]
|
||||||
fn index_mut(self, slice: &mut str) -> &mut Self::Output {
|
fn index_mut(self, slice: &mut str) -> &mut Self::Output {
|
||||||
assert!(self.end != usize::max_value(),
|
assert!(self.end != usize::max_value(),
|
||||||
"attempted to index str up to maximum usize");
|
"attempted to index str up to maximum usize");
|
||||||
if slice.is_char_boundary(self.end) {
|
(..self.end+1).index_mut(slice)
|
||||||
unsafe { self.get_unchecked_mut(slice) }
|
|
||||||
} else {
|
|
||||||
super::slice_error_fail(slice, 0, self.end + 1)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue