@@ -1116,6 +1116,23 @@ mod traits {
1116
1116
}
1117
1117
}
1118
1118
1119
+ /// Returns a mutable slice of the given string from the byte range
1120
+ /// [`begin`..`end`).
1121
+ #[ stable( feature = "derefmut_for_string" , since = "1.2.0" ) ]
1122
+ impl ops:: IndexMut < ops:: Range < usize > > for str {
1123
+ #[ inline]
1124
+ fn index_mut ( & mut self , index : ops:: Range < usize > ) -> & mut str {
1125
+ // is_char_boundary checks that the index is in [0, .len()]
1126
+ if index. start <= index. end &&
1127
+ self . is_char_boundary ( index. start ) &&
1128
+ self . is_char_boundary ( index. end ) {
1129
+ unsafe { self . slice_mut_unchecked ( index. start , index. end ) }
1130
+ } else {
1131
+ super :: slice_error_fail ( self , index. start , index. end )
1132
+ }
1133
+ }
1134
+ }
1135
+
1119
1136
/// Returns a slice of the string from the beginning to byte
1120
1137
/// `end`.
1121
1138
///
@@ -1138,6 +1155,21 @@ mod traits {
1138
1155
}
1139
1156
}
1140
1157
1158
+ /// Returns a mutable slice of the string from the beginning to byte
1159
+ /// `end`.
1160
+ #[ stable( feature = "derefmut_for_string" , since = "1.2.0" ) ]
1161
+ impl ops:: IndexMut < ops:: RangeTo < usize > > for str {
1162
+ #[ inline]
1163
+ fn index_mut ( & mut self , index : ops:: RangeTo < usize > ) -> & mut str {
1164
+ // is_char_boundary checks that the index is in [0, .len()]
1165
+ if self . is_char_boundary ( index. end ) {
1166
+ unsafe { self . slice_mut_unchecked ( 0 , index. end ) }
1167
+ } else {
1168
+ super :: slice_error_fail ( self , 0 , index. end )
1169
+ }
1170
+ }
1171
+ }
1172
+
1141
1173
/// Returns a slice of the string from `begin` to its end.
1142
1174
///
1143
1175
/// Equivalent to `self[begin .. self.len()]`.
@@ -1159,6 +1191,21 @@ mod traits {
1159
1191
}
1160
1192
}
1161
1193
1194
+ /// Returns a slice of the string from `begin` to its end.
1195
+ #[ stable( feature = "derefmut_for_string" , since = "1.2.0" ) ]
1196
+ impl ops:: IndexMut < ops:: RangeFrom < usize > > for str {
1197
+ #[ inline]
1198
+ fn index_mut ( & mut self , index : ops:: RangeFrom < usize > ) -> & mut str {
1199
+ // is_char_boundary checks that the index is in [0, .len()]
1200
+ if self . is_char_boundary ( index. start ) {
1201
+ let len = self . len ( ) ;
1202
+ unsafe { self . slice_mut_unchecked ( index. start , len) }
1203
+ } else {
1204
+ super :: slice_error_fail ( self , index. start , self . len ( ) )
1205
+ }
1206
+ }
1207
+ }
1208
+
1162
1209
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1163
1210
impl ops:: Index < ops:: RangeFull > for str {
1164
1211
type Output = str ;
@@ -1168,6 +1215,14 @@ mod traits {
1168
1215
self
1169
1216
}
1170
1217
}
1218
+
1219
+ #[ stable( feature = "derefmut_for_string" , since = "1.2.0" ) ]
1220
+ impl ops:: IndexMut < ops:: RangeFull > for str {
1221
+ #[ inline]
1222
+ fn index_mut ( & mut self , _index : ops:: RangeFull ) -> & mut str {
1223
+ self
1224
+ }
1225
+ }
1171
1226
}
1172
1227
1173
1228
/// Methods for string slices
@@ -1204,6 +1259,7 @@ pub trait StrExt {
1204
1259
fn char_len ( & self ) -> usize ;
1205
1260
fn slice_chars < ' a > ( & ' a self , begin : usize , end : usize ) -> & ' a str ;
1206
1261
unsafe fn slice_unchecked < ' a > ( & ' a self , begin : usize , end : usize ) -> & ' a str ;
1262
+ unsafe fn slice_mut_unchecked < ' a > ( & ' a mut self , begin : usize , end : usize ) -> & ' a mut str ;
1207
1263
fn starts_with < ' a , P : Pattern < ' a > > ( & ' a self , pat : P ) -> bool ;
1208
1264
fn ends_with < ' a , P : Pattern < ' a > > ( & ' a self , pat : P ) -> bool
1209
1265
where P :: Searcher : ReverseSearcher < ' a > ;
@@ -1223,6 +1279,7 @@ pub trait StrExt {
1223
1279
where P :: Searcher : ReverseSearcher < ' a > ;
1224
1280
fn find_str < ' a , P : Pattern < ' a > > ( & ' a self , pat : P ) -> Option < usize > ;
1225
1281
fn split_at ( & self , mid : usize ) -> ( & str , & str ) ;
1282
+ fn split_at_mut ( & mut self , mid : usize ) -> ( & mut str , & mut str ) ;
1226
1283
fn slice_shift_char < ' a > ( & ' a self ) -> Option < ( char , & ' a str ) > ;
1227
1284
fn subslice_offset ( & self , inner : & str ) -> usize ;
1228
1285
fn as_ptr ( & self ) -> * const u8 ;
@@ -1379,6 +1436,14 @@ impl StrExt for str {
1379
1436
} )
1380
1437
}
1381
1438
1439
+ #[ inline]
1440
+ unsafe fn slice_mut_unchecked ( & mut self , begin : usize , end : usize ) -> & mut str {
1441
+ mem:: transmute ( Slice {
1442
+ data : self . as_ptr ( ) . offset ( begin as isize ) ,
1443
+ len : end - begin,
1444
+ } )
1445
+ }
1446
+
1382
1447
#[ inline]
1383
1448
fn starts_with < ' a , P : Pattern < ' a > > ( & ' a self , pat : P ) -> bool {
1384
1449
pat. is_prefix_of ( self )
@@ -1527,6 +1592,20 @@ impl StrExt for str {
1527
1592
}
1528
1593
}
1529
1594
1595
+ fn split_at_mut ( & mut self , mid : usize ) -> ( & mut str , & mut str ) {
1596
+ // is_char_boundary checks that the index is in [0, .len()]
1597
+ if self . is_char_boundary ( mid) {
1598
+ let len = self . len ( ) ;
1599
+ unsafe {
1600
+ let self2: & mut str = mem:: transmute_copy ( & self ) ;
1601
+ ( self . slice_mut_unchecked ( 0 , mid) ,
1602
+ self2. slice_mut_unchecked ( mid, len) )
1603
+ }
1604
+ } else {
1605
+ slice_error_fail ( self , 0 , mid)
1606
+ }
1607
+ }
1608
+
1530
1609
#[ inline]
1531
1610
fn slice_shift_char ( & self ) -> Option < ( char , & str ) > {
1532
1611
if self . is_empty ( ) {
0 commit comments