File tree 1 file changed +16
-26
lines changed
1 file changed +16
-26
lines changed Original file line number Diff line number Diff line change @@ -2228,50 +2228,40 @@ impl ToString for char {
2228
2228
impl ToString for u8 {
2229
2229
#[ inline]
2230
2230
fn to_string ( & self ) -> String {
2231
- let mut result = String :: with_capacity ( 3 ) ;
2231
+ let mut buf = String :: with_capacity ( 3 ) ;
2232
2232
let mut n = * self ;
2233
- if n >= 100 {
2234
- result . push ( ( b'0' + n / 100 ) as char ) ;
2235
- n %= 100 ;
2236
- }
2237
- if !result . is_empty ( ) || n >= 10 {
2238
- result . push ( ( b'0' + n / 10 ) as char ) ;
2233
+ if n >= 10 {
2234
+ if n >= 100 {
2235
+ buf . push ( ( b'0' + n / 100 ) as char ) ;
2236
+ n %= 100 ;
2237
+ }
2238
+ buf . push ( ( b'0' + n / 10 ) as char ) ;
2239
2239
n %= 10 ;
2240
- } ;
2241
- result . push ( ( b'0' + n) as char ) ;
2242
- result
2240
+ }
2241
+ buf . push ( ( b'0' + n) as char ) ;
2242
+ buf
2243
2243
}
2244
2244
}
2245
2245
2246
2246
#[ stable( feature = "i8_to_string_specialization" , since = "1.999.0" ) ]
2247
2247
impl ToString for i8 {
2248
2248
#[ inline]
2249
2249
fn to_string ( & self ) -> String {
2250
- let mut vec = vec ! [ 0 ; 4 ] ;
2251
- let mut free = 0 ;
2250
+ let mut buf = String :: with_capacity ( 4 ) ;
2252
2251
if self . is_negative ( ) {
2253
- vec[ free] = b'-' ;
2254
- free += 1 ;
2252
+ buf. push ( '-' ) ;
2255
2253
}
2256
2254
let mut n = self . unsigned_abs ( ) ;
2257
2255
if n >= 10 {
2258
2256
if n >= 100 {
2257
+ buf. push ( '1' ) ;
2259
2258
n -= 100 ;
2260
- vec[ free] = b'1' ;
2261
- free += 1 ;
2262
2259
}
2263
- debug_assert ! ( n < 100 ) ;
2264
- vec[ free] = b'0' + n / 10 ;
2265
- free += 1 ;
2260
+ buf. push ( ( b'0' + n / 10 ) as char ) ;
2266
2261
n %= 10 ;
2267
2262
}
2268
- debug_assert ! ( n < 10 ) ;
2269
- vec[ free] = b'0' + n;
2270
- free += 1 ;
2271
- vec. truncate ( free) ;
2272
-
2273
- // SAFETY: Vec only contains ascii so valid utf8
2274
- unsafe { String :: from_utf8_unchecked ( vec) }
2263
+ buf. push ( ( b'0' + n) as char ) ;
2264
+ buf
2275
2265
}
2276
2266
}
2277
2267
You can’t perform that action at this time.
0 commit comments