File tree 1 file changed +17
-17
lines changed
1 file changed +17
-17
lines changed Original file line number Diff line number Diff line change @@ -468,24 +468,24 @@ impl String {
468
468
#[ inline]
469
469
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
470
470
pub fn push ( & mut self , ch : char ) {
471
- if ( ch as u32 ) < 0x80 {
472
- self . vec . push ( ch as u8 ) ;
473
- return ;
474
- }
475
-
476
- let cur_len = self . len ( ) ;
477
- // This may use up to 4 bytes.
478
- self . vec . reserve ( 4 ) ;
471
+ match ch. len_utf8 ( ) {
472
+ 1 => self . vec . push ( ch as u8 ) ,
473
+ ch_len => {
474
+ let cur_len = self . len ( ) ;
475
+ // This may use up to 4 bytes.
476
+ self . vec . reserve ( ch_len) ;
479
477
480
- unsafe {
481
- // Attempt to not use an intermediate buffer by just pushing bytes
482
- // directly onto this string.
483
- let slice = slice:: from_raw_parts_mut (
484
- self . vec . as_mut_ptr ( ) . offset ( cur_len as isize ) ,
485
- 4
486
- ) ;
487
- let used = ch. encode_utf8 ( slice) . unwrap_or ( 0 ) ;
488
- self . vec . set_len ( cur_len + used) ;
478
+ unsafe {
479
+ // Attempt to not use an intermediate buffer by just pushing bytes
480
+ // directly onto this string.
481
+ let slice = slice:: from_raw_parts_mut (
482
+ self . vec . as_mut_ptr ( ) . offset ( cur_len as isize ) ,
483
+ ch_len
484
+ ) ;
485
+ let used = ch. encode_utf8 ( slice) . unwrap_or ( 0 ) ;
486
+ self . vec . set_len ( cur_len + used) ;
487
+ }
488
+ }
489
489
}
490
490
}
491
491
You can’t perform that action at this time.
0 commit comments