Skip to content

Commit

Permalink
Delegate str:Index(Mut) to SliceIndex<str>
Browse files Browse the repository at this point in the history
Move any extra logic that the former had into the latter, so they're consistent.
  • Loading branch information
scottmcm committed Jun 4, 2017
1 parent bb907ad commit 18612b2
Showing 1 changed file with 18 additions and 37 deletions.
55 changes: 18 additions & 37 deletions src/libcore/str/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1617,12 +1617,7 @@ mod traits {

#[inline]
fn index(&self, index: ops::RangeTo<usize>) -> &str {
// is_char_boundary checks that the index is in [0, .len()]
if self.is_char_boundary(index.end) {
unsafe { self.slice_unchecked(0, index.end) }
} else {
super::slice_error_fail(self, 0, index.end)
}
index.index(self)
}
}

Expand All @@ -1636,12 +1631,7 @@ mod traits {
impl ops::IndexMut<ops::RangeTo<usize>> for str {
#[inline]
fn index_mut(&mut self, index: ops::RangeTo<usize>) -> &mut str {
// is_char_boundary checks that the index is in [0, .len()]
if self.is_char_boundary(index.end) {
unsafe { self.slice_mut_unchecked(0, index.end) }
} else {
super::slice_error_fail(self, 0, index.end)
}
index.index_mut(self)
}
}

Expand All @@ -1657,12 +1647,7 @@ mod traits {

#[inline]
fn index(&self, index: ops::RangeFrom<usize>) -> &str {
// is_char_boundary checks that the index is in [0, .len()]
if self.is_char_boundary(index.start) {
unsafe { self.slice_unchecked(index.start, self.len()) }
} else {
super::slice_error_fail(self, index.start, self.len())
}
index.index(self)
}
}

Expand All @@ -1676,13 +1661,7 @@ mod traits {
impl ops::IndexMut<ops::RangeFrom<usize>> for str {
#[inline]
fn index_mut(&mut self, index: ops::RangeFrom<usize>) -> &mut str {
// is_char_boundary checks that the index is in [0, .len()]
if self.is_char_boundary(index.start) {
let len = self.len();
unsafe { self.slice_mut_unchecked(index.start, len) }
} else {
super::slice_error_fail(self, index.start, self.len())
}
index.index_mut(self)
}
}

Expand Down Expand Up @@ -1724,9 +1703,7 @@ mod traits {

#[inline]
fn index(&self, index: ops::RangeInclusive<usize>) -> &str {
assert!(index.end != usize::max_value(),
"attempted to index str up to maximum usize");
self.index(index.start .. index.end+1)
index.index(self)
}
}

Expand All @@ -1738,9 +1715,7 @@ mod traits {

#[inline]
fn index(&self, index: ops::RangeToInclusive<usize>) -> &str {
assert!(index.end != usize::max_value(),
"attempted to index str up to maximum usize");
self.index(.. index.end+1)
index.index(self)
}
}

Expand All @@ -1750,9 +1725,7 @@ mod traits {
impl ops::IndexMut<ops::RangeInclusive<usize>> for str {
#[inline]
fn index_mut(&mut self, index: ops::RangeInclusive<usize>) -> &mut str {
assert!(index.end != usize::max_value(),
"attempted to index str up to maximum usize");
self.index_mut(index.start .. index.end+1)
index.index_mut(self)
}
}
#[unstable(feature = "inclusive_range",
Expand All @@ -1761,9 +1734,7 @@ mod traits {
impl ops::IndexMut<ops::RangeToInclusive<usize>> for str {
#[inline]
fn index_mut(&mut self, index: ops::RangeToInclusive<usize>) -> &mut str {
assert!(index.end != usize::max_value(),
"attempted to index str up to maximum usize");
self.index_mut(.. index.end+1)
index.index_mut(self)
}
}

Expand Down Expand Up @@ -1886,6 +1857,7 @@ mod traits {
}
#[inline]
fn index_mut(self, slice: &mut str) -> &mut Self::Output {
// is_char_boundary checks that the index is in [0, .len()]
if slice.is_char_boundary(self.end) {
unsafe { self.get_unchecked_mut(slice) }
} else {
Expand Down Expand Up @@ -1932,6 +1904,7 @@ mod traits {
}
#[inline]
fn index_mut(self, slice: &mut str) -> &mut Self::Output {
// is_char_boundary checks that the index is in [0, .len()]
if slice.is_char_boundary(self.start) {
unsafe { self.get_unchecked_mut(slice) }
} else {
Expand Down Expand Up @@ -1961,10 +1934,14 @@ mod traits {
}
#[inline]
fn index(self, slice: &str) -> &Self::Output {
assert!(self.end != usize::max_value(),
"attempted to index str up to maximum usize");
(self.start..self.end+1).index(slice)
}
#[inline]
fn index_mut(self, slice: &mut str) -> &mut Self::Output {
assert!(self.end != usize::max_value(),
"attempted to index str up to maximum usize");
(self.start..self.end+1).index_mut(slice)
}
}
Expand Down Expand Up @@ -2002,11 +1979,15 @@ mod traits {
}
#[inline]
fn index(self, slice: &str) -> &Self::Output {
assert!(self.end != usize::max_value(),
"attempted to index str up to maximum usize");
let end = self.end + 1;
self.get(slice).unwrap_or_else(|| super::slice_error_fail(slice, 0, end))
}
#[inline]
fn index_mut(self, slice: &mut str) -> &mut Self::Output {
assert!(self.end != usize::max_value(),
"attempted to index str up to maximum usize");
if slice.is_char_boundary(self.end) {
unsafe { self.get_unchecked_mut(slice) }
} else {
Expand Down

0 comments on commit 18612b2

Please sign in to comment.