@@ -1551,6 +1551,27 @@ impl ops::Index<ops::RangeFull> for CString {
15511551 }
15521552}
15531553
1554+ #[ stable( feature = "cstr_range_from" , since = "1.45.0" ) ]
1555+ impl ops:: Index < ops:: RangeFrom < usize > > for CStr {
1556+ type Output = CStr ;
1557+
1558+ fn index ( & self , index : ops:: RangeFrom < usize > ) -> & CStr {
1559+ let bytes = self . to_bytes_with_nul ( ) ;
1560+ // we need to manually check the starting index to account for the null
1561+ // byte, since otherwise we could get an empty string that doesn't end
1562+ // in a null.
1563+ if index. start < bytes. len ( ) {
1564+ unsafe { CStr :: from_bytes_with_nul_unchecked ( & bytes[ index. start ..] ) }
1565+ } else {
1566+ panic ! (
1567+ "index out of bounds: the len is {} but the index is {}" ,
1568+ bytes. len( ) ,
1569+ index. start
1570+ ) ;
1571+ }
1572+ }
1573+ }
1574+
15541575#[ stable( feature = "cstring_asref" , since = "1.7.0" ) ]
15551576impl AsRef < CStr > for CStr {
15561577 #[ inline]
@@ -1747,4 +1768,21 @@ mod tests {
17471768
17481769 assert_eq ! ( CSTR . to_str( ) . unwrap( ) , "Hello, world!" ) ;
17491770 }
1771+
1772+ #[ test]
1773+ fn cstr_index_from ( ) {
1774+ let original = b"Hello, world!\0 " ;
1775+ let cstr = CStr :: from_bytes_with_nul ( original) . unwrap ( ) ;
1776+ let result = CStr :: from_bytes_with_nul ( & original[ 7 ..] ) . unwrap ( ) ;
1777+
1778+ assert_eq ! ( & cstr[ 7 ..] , result) ;
1779+ }
1780+
1781+ #[ test]
1782+ #[ should_panic]
1783+ fn cstr_index_from_empty ( ) {
1784+ let original = b"Hello, world!\0 " ;
1785+ let cstr = CStr :: from_bytes_with_nul ( original) . unwrap ( ) ;
1786+ let _ = & cstr[ original. len ( ) ..] ;
1787+ }
17501788}
0 commit comments