@@ -1617,12 +1617,7 @@ mod traits {
1617
1617
1618
1618
#[ inline]
1619
1619
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 )
1626
1621
}
1627
1622
}
1628
1623
@@ -1636,12 +1631,7 @@ mod traits {
1636
1631
impl ops:: IndexMut < ops:: RangeTo < usize > > for str {
1637
1632
#[ inline]
1638
1633
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 )
1645
1635
}
1646
1636
}
1647
1637
@@ -1657,12 +1647,7 @@ mod traits {
1657
1647
1658
1648
#[ inline]
1659
1649
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 )
1666
1651
}
1667
1652
}
1668
1653
@@ -1676,13 +1661,7 @@ mod traits {
1676
1661
impl ops:: IndexMut < ops:: RangeFrom < usize > > for str {
1677
1662
#[ inline]
1678
1663
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 )
1686
1665
}
1687
1666
}
1688
1667
@@ -1724,9 +1703,7 @@ mod traits {
1724
1703
1725
1704
#[ inline]
1726
1705
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 )
1730
1707
}
1731
1708
}
1732
1709
@@ -1738,9 +1715,7 @@ mod traits {
1738
1715
1739
1716
#[ inline]
1740
1717
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 )
1744
1719
}
1745
1720
}
1746
1721
@@ -1750,9 +1725,7 @@ mod traits {
1750
1725
impl ops:: IndexMut < ops:: RangeInclusive < usize > > for str {
1751
1726
#[ inline]
1752
1727
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 )
1756
1729
}
1757
1730
}
1758
1731
#[ unstable( feature = "inclusive_range" ,
@@ -1761,9 +1734,7 @@ mod traits {
1761
1734
impl ops:: IndexMut < ops:: RangeToInclusive < usize > > for str {
1762
1735
#[ inline]
1763
1736
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 )
1767
1738
}
1768
1739
}
1769
1740
@@ -1886,6 +1857,7 @@ mod traits {
1886
1857
}
1887
1858
#[ inline]
1888
1859
fn index_mut ( self , slice : & mut str ) -> & mut Self :: Output {
1860
+ // is_char_boundary checks that the index is in [0, .len()]
1889
1861
if slice. is_char_boundary ( self . end ) {
1890
1862
unsafe { self . get_unchecked_mut ( slice) }
1891
1863
} else {
@@ -1932,6 +1904,7 @@ mod traits {
1932
1904
}
1933
1905
#[ inline]
1934
1906
fn index_mut ( self , slice : & mut str ) -> & mut Self :: Output {
1907
+ // is_char_boundary checks that the index is in [0, .len()]
1935
1908
if slice. is_char_boundary ( self . start ) {
1936
1909
unsafe { self . get_unchecked_mut ( slice) }
1937
1910
} else {
@@ -1945,11 +1918,19 @@ mod traits {
1945
1918
type Output = str ;
1946
1919
#[ inline]
1947
1920
fn get ( self , slice : & str ) -> Option < & Self :: Output > {
1948
- ( self . start ..self . end +1 ) . get ( slice)
1921
+ if let Some ( end) = self . end . checked_add ( 1 ) {
1922
+ ( self . start ..end) . get ( slice)
1923
+ } else {
1924
+ None
1925
+ }
1949
1926
}
1950
1927
#[ inline]
1951
1928
fn get_mut ( self , slice : & mut str ) -> Option < & mut Self :: Output > {
1952
- ( self . start ..self . end +1 ) . get_mut ( slice)
1929
+ if let Some ( end) = self . end . checked_add ( 1 ) {
1930
+ ( self . start ..end) . get_mut ( slice)
1931
+ } else {
1932
+ None
1933
+ }
1953
1934
}
1954
1935
#[ inline]
1955
1936
unsafe fn get_unchecked ( self , slice : & str ) -> & Self :: Output {
@@ -1961,10 +1942,14 @@ mod traits {
1961
1942
}
1962
1943
#[ inline]
1963
1944
fn index ( self , slice : & str ) -> & Self :: Output {
1945
+ assert ! ( self . end != usize :: max_value( ) ,
1946
+ "attempted to index str up to maximum usize" ) ;
1964
1947
( self . start ..self . end +1 ) . index ( slice)
1965
1948
}
1966
1949
#[ inline]
1967
1950
fn index_mut ( self , slice : & mut str ) -> & mut Self :: Output {
1951
+ assert ! ( self . end != usize :: max_value( ) ,
1952
+ "attempted to index str up to maximum usize" ) ;
1968
1953
( self . start ..self . end +1 ) . index_mut ( slice)
1969
1954
}
1970
1955
}
@@ -1976,15 +1961,15 @@ mod traits {
1976
1961
type Output = str ;
1977
1962
#[ inline]
1978
1963
fn get ( self , slice : & str ) -> Option < & Self :: Output > {
1979
- if slice. is_char_boundary ( self . end + 1 ) {
1964
+ if self . end < usize :: max_value ( ) && slice. is_char_boundary ( self . end + 1 ) {
1980
1965
Some ( unsafe { self . get_unchecked ( slice) } )
1981
1966
} else {
1982
1967
None
1983
1968
}
1984
1969
}
1985
1970
#[ inline]
1986
1971
fn get_mut ( self , slice : & mut str ) -> Option < & mut Self :: Output > {
1987
- if slice. is_char_boundary ( self . end + 1 ) {
1972
+ if self . end < usize :: max_value ( ) && slice. is_char_boundary ( self . end + 1 ) {
1988
1973
Some ( unsafe { self . get_unchecked_mut ( slice) } )
1989
1974
} else {
1990
1975
None
@@ -2002,11 +1987,15 @@ mod traits {
2002
1987
}
2003
1988
#[ inline]
2004
1989
fn index ( self , slice : & str ) -> & Self :: Output {
1990
+ assert ! ( self . end != usize :: max_value( ) ,
1991
+ "attempted to index str up to maximum usize" ) ;
2005
1992
let end = self . end + 1 ;
2006
1993
self . get ( slice) . unwrap_or_else ( || super :: slice_error_fail ( slice, 0 , end) )
2007
1994
}
2008
1995
#[ inline]
2009
1996
fn index_mut ( self , slice : & mut str ) -> & mut Self :: Output {
1997
+ assert ! ( self . end != usize :: max_value( ) ,
1998
+ "attempted to index str up to maximum usize" ) ;
2010
1999
if slice. is_char_boundary ( self . end ) {
2011
2000
unsafe { self . get_unchecked_mut ( slice) }
2012
2001
} else {
0 commit comments