1
1
//! Trait implementations for `str`.
2
2
3
3
use crate :: cmp:: Ordering ;
4
+ use crate :: intrinsics:: unchecked_sub;
4
5
use crate :: ops;
5
6
use crate :: ptr;
6
7
use crate :: slice:: SliceIndex ;
@@ -210,9 +211,10 @@ unsafe impl SliceIndex<str> for ops::Range<usize> {
210
211
211
212
// SAFETY: the caller guarantees that `self` is in bounds of `slice`
212
213
// which satisfies all the conditions for `add`.
213
- let ptr = unsafe { slice. as_ptr ( ) . add ( self . start ) } ;
214
- let len = self . end - self . start ;
215
- ptr:: slice_from_raw_parts ( ptr, len) as * const str
214
+ unsafe {
215
+ let new_len = unchecked_sub ( self . end , self . start ) ;
216
+ ptr:: slice_from_raw_parts ( slice. as_ptr ( ) . add ( self . start ) , new_len) as * const str
217
+ }
216
218
}
217
219
#[ inline]
218
220
unsafe fn get_unchecked_mut ( self , slice : * mut str ) -> * mut Self :: Output {
@@ -229,9 +231,10 @@ unsafe impl SliceIndex<str> for ops::Range<usize> {
229
231
) ;
230
232
231
233
// SAFETY: see comments for `get_unchecked`.
232
- let ptr = unsafe { slice. as_mut_ptr ( ) . add ( self . start ) } ;
233
- let len = self . end - self . start ;
234
- ptr:: slice_from_raw_parts_mut ( ptr, len) as * mut str
234
+ unsafe {
235
+ let new_len = unchecked_sub ( self . end , self . start ) ;
236
+ ptr:: slice_from_raw_parts_mut ( slice. as_mut_ptr ( ) . add ( self . start ) , new_len) as * mut str
237
+ }
235
238
}
236
239
#[ inline]
237
240
fn index ( self , slice : & str ) -> & Self :: Output {
0 commit comments