Skip to content

Commit 6b749b0

Browse files
committed
Clean up the other Slice*Inclusive impls for str
A previous PR fixed one method that was legitimately buggy; this cleans up the rest to be less diverse, mirroring the corresponding impls on [T] to the greatest extent possible without introducing any unnecessary UTF-8 boundary checks at 0.
1 parent 5ab4c81 commit 6b749b0

File tree

1 file changed

+10
-24
lines changed

1 file changed

+10
-24
lines changed

src/libcore/str/mod.rs

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2035,19 +2035,13 @@ mod traits {
20352035
type Output = str;
20362036
#[inline]
20372037
fn get(self, slice: &str) -> Option<&Self::Output> {
2038-
if let Some(end) = self.end.checked_add(1) {
2039-
(self.start..end).get(slice)
2040-
} else {
2041-
None
2042-
}
2038+
if self.end == usize::max_value() { None }
2039+
else { (self.start..self.end+1).get(slice) }
20432040
}
20442041
#[inline]
20452042
fn get_mut(self, slice: &mut str) -> Option<&mut Self::Output> {
2046-
if let Some(end) = self.end.checked_add(1) {
2047-
(self.start..end).get_mut(slice)
2048-
} else {
2049-
None
2050-
}
2043+
if self.end == usize::max_value() { None }
2044+
else { (self.start..self.end+1).get_mut(slice) }
20512045
}
20522046
#[inline]
20532047
unsafe fn get_unchecked(self, slice: &str) -> &Self::Output {
@@ -2076,29 +2070,21 @@ mod traits {
20762070
type Output = str;
20772071
#[inline]
20782072
fn get(self, slice: &str) -> Option<&Self::Output> {
2079-
if self.end < usize::max_value() && slice.is_char_boundary(self.end + 1) {
2080-
Some(unsafe { self.get_unchecked(slice) })
2081-
} else {
2082-
None
2083-
}
2073+
if self.end == usize::max_value() { None }
2074+
else { (..self.end+1).get(slice) }
20842075
}
20852076
#[inline]
20862077
fn get_mut(self, slice: &mut str) -> Option<&mut Self::Output> {
2087-
if self.end < usize::max_value() && slice.is_char_boundary(self.end + 1) {
2088-
Some(unsafe { self.get_unchecked_mut(slice) })
2089-
} else {
2090-
None
2091-
}
2078+
if self.end == usize::max_value() { None }
2079+
else { (..self.end+1).get_mut(slice) }
20922080
}
20932081
#[inline]
20942082
unsafe fn get_unchecked(self, slice: &str) -> &Self::Output {
2095-
let ptr = slice.as_ptr();
2096-
super::from_utf8_unchecked(slice::from_raw_parts(ptr, self.end + 1))
2083+
(..self.end+1).get_unchecked(slice)
20972084
}
20982085
#[inline]
20992086
unsafe fn get_unchecked_mut(self, slice: &mut str) -> &mut Self::Output {
2100-
let ptr = slice.as_ptr();
2101-
super::from_utf8_unchecked_mut(slice::from_raw_parts_mut(ptr as *mut u8, self.end + 1))
2087+
(..self.end+1).get_unchecked_mut(slice)
21022088
}
21032089
#[inline]
21042090
fn index(self, slice: &str) -> &Self::Output {

0 commit comments

Comments
 (0)