diff options
Diffstat (limited to 'src/libcore/str')
| -rw-r--r-- | src/libcore/str/mod.rs | 59 |
1 files changed, 19 insertions, 40 deletions
diff --git a/src/libcore/str/mod.rs b/src/libcore/str/mod.rs index 6b627430904..ccd8ef8ae56 100644 --- a/src/libcore/str/mod.rs +++ b/src/libcore/str/mod.rs @@ -1724,15 +1724,12 @@ mod traits { #[inline] fn index(&self, index: ops::RangeInclusive<usize>) -> &str { - match index { - ops::RangeInclusive::Empty { .. } => "", - ops::RangeInclusive::NonEmpty { end, .. } if end == usize::max_value() => - panic!("attempted to index slice up to maximum usize"), - ops::RangeInclusive::NonEmpty { start, end } => - self.index(start .. end+1) - } + assert!(index.end != usize::max_value(), + "attempted to index str up to maximum usize"); + self.index(index.start .. index.end+1) } } + #[unstable(feature = "inclusive_range", reason = "recently added, follows RFC", issue = "28237")] @@ -1741,7 +1738,9 @@ mod traits { #[inline] fn index(&self, index: ops::RangeToInclusive<usize>) -> &str { - self.index(0...index.end) + assert!(index.end != usize::max_value(), + "attempted to index str up to maximum usize"); + self.index(.. index.end+1) } } @@ -1751,13 +1750,9 @@ mod traits { impl ops::IndexMut<ops::RangeInclusive<usize>> for str { #[inline] fn index_mut(&mut self, index: ops::RangeInclusive<usize>) -> &mut str { - match index { - ops::RangeInclusive::Empty { .. } => &mut self[0..0], // `&mut ""` doesn't work - ops::RangeInclusive::NonEmpty { end, .. } if end == usize::max_value() => - panic!("attempted to index str up to maximum usize"), - ops::RangeInclusive::NonEmpty { start, end } => - self.index_mut(start .. end+1) - } + assert!(index.end != usize::max_value(), + "attempted to index str up to maximum usize"); + self.index_mut(index.start .. index.end+1) } } #[unstable(feature = "inclusive_range", @@ -1766,7 +1761,9 @@ mod traits { impl ops::IndexMut<ops::RangeToInclusive<usize>> for str { #[inline] fn index_mut(&mut self, index: ops::RangeToInclusive<usize>) -> &mut str { - self.index_mut(0...index.end) + assert!(index.end != usize::max_value(), + "attempted to index str up to maximum usize"); + self.index_mut(.. index.end+1) } } @@ -1948,45 +1945,27 @@ mod traits { type Output = str; #[inline] fn get(self, slice: &str) -> Option<&Self::Output> { - match self { - ops::RangeInclusive::Empty { .. } => 0..0, - ops::RangeInclusive::NonEmpty { start, end } => start..end+1, - }.get(slice) + (self.start..self.end+1).get(slice) } #[inline] fn get_mut(self, slice: &mut str) -> Option<&mut Self::Output> { - match self { - ops::RangeInclusive::Empty { .. } => 0..0, - ops::RangeInclusive::NonEmpty { start, end } => start..end+1, - }.get_mut(slice) + (self.start..self.end+1).get_mut(slice) } #[inline] unsafe fn get_unchecked(self, slice: &str) -> &Self::Output { - match self { - ops::RangeInclusive::Empty { .. } => 0..0, - ops::RangeInclusive::NonEmpty { start, end } => start..end+1, - }.get_unchecked(slice) + (self.start..self.end+1).get_unchecked(slice) } #[inline] unsafe fn get_unchecked_mut(self, slice: &mut str) -> &mut Self::Output { - match self { - ops::RangeInclusive::Empty { .. } => 0..0, - ops::RangeInclusive::NonEmpty { start, end } => start..end+1, - }.get_unchecked_mut(slice) + (self.start..self.end+1).get_unchecked_mut(slice) } #[inline] fn index(self, slice: &str) -> &Self::Output { - match self { - ops::RangeInclusive::Empty { .. } => 0..0, - ops::RangeInclusive::NonEmpty { start, end } => start..end+1, - }.index(slice) + (self.start..self.end+1).index(slice) } #[inline] fn index_mut(self, slice: &mut str) -> &mut Self::Output { - match self { - ops::RangeInclusive::Empty { .. } => 0..0, - ops::RangeInclusive::NonEmpty { start, end } => start..end+1, - }.index_mut(slice) + (self.start..self.end+1).index_mut(slice) } } |
