File tree 1 file changed +22
-15
lines changed
1 file changed +22
-15
lines changed Original file line number Diff line number Diff line change @@ -1233,37 +1233,44 @@ impl String {
1233
1233
where
1234
1234
F : FnMut ( char ) -> bool ,
1235
1235
{
1236
- let len = self . len ( ) ;
1237
- let mut del_bytes = 0 ;
1238
- let mut idx = 0 ;
1236
+ struct SetLenOnDrop < ' a > {
1237
+ s : & ' a mut String ,
1238
+ idx : usize ,
1239
+ del_bytes : usize ,
1240
+ }
1239
1241
1240
- unsafe {
1241
- self . vec . set_len ( 0 ) ;
1242
+ impl < ' a > Drop for SetLenOnDrop < ' a > {
1243
+ fn drop ( & mut self ) {
1244
+ let new_len = self . idx - self . del_bytes ;
1245
+ debug_assert ! ( new_len <= self . s. len( ) ) ;
1246
+ unsafe { self . s . vec . set_len ( new_len) } ;
1247
+ }
1242
1248
}
1243
1249
1244
- while idx < len {
1245
- let ch = unsafe { self . get_unchecked ( idx..len) . chars ( ) . next ( ) . unwrap ( ) } ;
1250
+ let len = self . len ( ) ;
1251
+ let mut guard = SetLenOnDrop { s : self , idx : 0 , del_bytes : 0 } ;
1252
+
1253
+ while guard. idx < len {
1254
+ let ch = unsafe { guard. s . get_unchecked ( guard. idx ..len) . chars ( ) . next ( ) . unwrap ( ) } ;
1246
1255
let ch_len = ch. len_utf8 ( ) ;
1247
1256
1248
1257
if !f ( ch) {
1249
- del_bytes += ch_len;
1250
- } else if del_bytes > 0 {
1258
+ guard . del_bytes += ch_len;
1259
+ } else if guard . del_bytes > 0 {
1251
1260
unsafe {
1252
1261
ptr:: copy (
1253
- self . vec . as_ptr ( ) . add ( idx) ,
1254
- self . vec . as_mut_ptr ( ) . add ( idx - del_bytes) ,
1262
+ guard . s . vec . as_ptr ( ) . add ( guard . idx ) ,
1263
+ guard . s . vec . as_mut_ptr ( ) . add ( guard . idx - guard . del_bytes ) ,
1255
1264
ch_len,
1256
1265
) ;
1257
1266
}
1258
1267
}
1259
1268
1260
1269
// Point idx to the next char
1261
- idx += ch_len;
1270
+ guard . idx += ch_len;
1262
1271
}
1263
1272
1264
- unsafe {
1265
- self . vec . set_len ( len - del_bytes) ;
1266
- }
1273
+ drop ( guard) ;
1267
1274
}
1268
1275
1269
1276
/// Inserts a character into this `String` at a byte position.
You can’t perform that action at this time.
0 commit comments