Skip to content

Commit 18612b2

Browse files
committed
Delegate str:Index(Mut) to SliceIndex<str>
Move any extra logic that the former had into the latter, so they're consistent.
1 parent bb907ad commit 18612b2

File tree

1 file changed

+18
-37
lines changed

1 file changed

+18
-37
lines changed

src/libcore/str/mod.rs

Lines changed: 18 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1617,12 +1617,7 @@ mod traits {
16171617

16181618
#[inline]
16191619
fn index(&self, index: ops::RangeTo<usize>) -> &str {
1620-
// is_char_boundary checks that the index is in [0, .len()]
1621-
if self.is_char_boundary(index.end) {
1622-
unsafe { self.slice_unchecked(0, index.end) }
1623-
} else {
1624-
super::slice_error_fail(self, 0, index.end)
1625-
}
1620+
index.index(self)
16261621
}
16271622
}
16281623

@@ -1636,12 +1631,7 @@ mod traits {
16361631
impl ops::IndexMut<ops::RangeTo<usize>> for str {
16371632
#[inline]
16381633
fn index_mut(&mut self, index: ops::RangeTo<usize>) -> &mut str {
1639-
// is_char_boundary checks that the index is in [0, .len()]
1640-
if self.is_char_boundary(index.end) {
1641-
unsafe { self.slice_mut_unchecked(0, index.end) }
1642-
} else {
1643-
super::slice_error_fail(self, 0, index.end)
1644-
}
1634+
index.index_mut(self)
16451635
}
16461636
}
16471637

@@ -1657,12 +1647,7 @@ mod traits {
16571647

16581648
#[inline]
16591649
fn index(&self, index: ops::RangeFrom<usize>) -> &str {
1660-
// is_char_boundary checks that the index is in [0, .len()]
1661-
if self.is_char_boundary(index.start) {
1662-
unsafe { self.slice_unchecked(index.start, self.len()) }
1663-
} else {
1664-
super::slice_error_fail(self, index.start, self.len())
1665-
}
1650+
index.index(self)
16661651
}
16671652
}
16681653

@@ -1676,13 +1661,7 @@ mod traits {
16761661
impl ops::IndexMut<ops::RangeFrom<usize>> for str {
16771662
#[inline]
16781663
fn index_mut(&mut self, index: ops::RangeFrom<usize>) -> &mut str {
1679-
// is_char_boundary checks that the index is in [0, .len()]
1680-
if self.is_char_boundary(index.start) {
1681-
let len = self.len();
1682-
unsafe { self.slice_mut_unchecked(index.start, len) }
1683-
} else {
1684-
super::slice_error_fail(self, index.start, self.len())
1685-
}
1664+
index.index_mut(self)
16861665
}
16871666
}
16881667

@@ -1724,9 +1703,7 @@ mod traits {
17241703

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

@@ -1738,9 +1715,7 @@ mod traits {
17381715

17391716
#[inline]
17401717
fn index(&self, index: ops::RangeToInclusive<usize>) -> &str {
1741-
assert!(index.end != usize::max_value(),
1742-
"attempted to index str up to maximum usize");
1743-
self.index(.. index.end+1)
1718+
index.index(self)
17441719
}
17451720
}
17461721

@@ -1750,9 +1725,7 @@ mod traits {
17501725
impl ops::IndexMut<ops::RangeInclusive<usize>> for str {
17511726
#[inline]
17521727
fn index_mut(&mut self, index: ops::RangeInclusive<usize>) -> &mut str {
1753-
assert!(index.end != usize::max_value(),
1754-
"attempted to index str up to maximum usize");
1755-
self.index_mut(index.start .. index.end+1)
1728+
index.index_mut(self)
17561729
}
17571730
}
17581731
#[unstable(feature = "inclusive_range",
@@ -1761,9 +1734,7 @@ mod traits {
17611734
impl ops::IndexMut<ops::RangeToInclusive<usize>> for str {
17621735
#[inline]
17631736
fn index_mut(&mut self, index: ops::RangeToInclusive<usize>) -> &mut str {
1764-
assert!(index.end != usize::max_value(),
1765-
"attempted to index str up to maximum usize");
1766-
self.index_mut(.. index.end+1)
1737+
index.index_mut(self)
17671738
}
17681739
}
17691740

@@ -1886,6 +1857,7 @@ mod traits {
18861857
}
18871858
#[inline]
18881859
fn index_mut(self, slice: &mut str) -> &mut Self::Output {
1860+
// is_char_boundary checks that the index is in [0, .len()]
18891861
if slice.is_char_boundary(self.end) {
18901862
unsafe { self.get_unchecked_mut(slice) }
18911863
} else {
@@ -1932,6 +1904,7 @@ mod traits {
19321904
}
19331905
#[inline]
19341906
fn index_mut(self, slice: &mut str) -> &mut Self::Output {
1907+
// is_char_boundary checks that the index is in [0, .len()]
19351908
if slice.is_char_boundary(self.start) {
19361909
unsafe { self.get_unchecked_mut(slice) }
19371910
} else {
@@ -1961,10 +1934,14 @@ mod traits {
19611934
}
19621935
#[inline]
19631936
fn index(self, slice: &str) -> &Self::Output {
1937+
assert!(self.end != usize::max_value(),
1938+
"attempted to index str up to maximum usize");
19641939
(self.start..self.end+1).index(slice)
19651940
}
19661941
#[inline]
19671942
fn index_mut(self, slice: &mut str) -> &mut Self::Output {
1943+
assert!(self.end != usize::max_value(),
1944+
"attempted to index str up to maximum usize");
19681945
(self.start..self.end+1).index_mut(slice)
19691946
}
19701947
}
@@ -2002,11 +1979,15 @@ mod traits {
20021979
}
20031980
#[inline]
20041981
fn index(self, slice: &str) -> &Self::Output {
1982+
assert!(self.end != usize::max_value(),
1983+
"attempted to index str up to maximum usize");
20051984
let end = self.end + 1;
20061985
self.get(slice).unwrap_or_else(|| super::slice_error_fail(slice, 0, end))
20071986
}
20081987
#[inline]
20091988
fn index_mut(self, slice: &mut str) -> &mut Self::Output {
1989+
assert!(self.end != usize::max_value(),
1990+
"attempted to index str up to maximum usize");
20101991
if slice.is_char_boundary(self.end) {
20111992
unsafe { self.get_unchecked_mut(slice) }
20121993
} else {

0 commit comments

Comments
 (0)