@@ -233,53 +233,16 @@ impl<'a> DigitInfo<'a> {
233
233
234
234
let ( integer, fraction, exponent) = & self . split_digit_parts ( ) ;
235
235
236
- let int_digits: Vec < _ > = integer. chars ( ) . rev ( ) . filter ( |& c| c != '_' ) . collect ( ) ;
237
- let int_part_hint = int_digits
238
- . chunks ( group_size)
239
- . map ( |chunk| chunk. iter ( ) . rev ( ) . collect ( ) )
240
- . rev ( )
241
- . collect :: < Vec < String > > ( )
242
- . join ( "_" ) ;
243
-
244
- // Pad leading hexidecimal group with zeros
245
- if self . radix == Radix :: Hexadecimal {
246
- debug_assert ! ( group_size > 0 ) ;
247
- let first_group_size = ( int_digits. len ( ) + group_size - 1 ) % group_size + 1 ;
248
- for _ in 0 ..group_size - first_group_size {
249
- output. push ( '0' ) ;
250
- }
251
- }
252
-
253
- output. push_str ( & int_part_hint) ;
236
+ Self :: group_digits ( & mut output, integer, group_size, true , self . radix == Radix :: Hexadecimal ) ;
254
237
255
238
if let Some ( fraction) = fraction {
256
- let frac_part_hint = fraction
257
- . chars ( )
258
- . filter ( |& c| c != '_' )
259
- . collect :: < Vec < _ > > ( )
260
- . chunks ( group_size)
261
- . map ( |chunk| chunk. iter ( ) . collect ( ) )
262
- . collect :: < Vec < String > > ( )
263
- . join ( "_" ) ;
264
-
265
239
output. push ( '.' ) ;
266
- output . push_str ( & frac_part_hint ) ;
240
+ Self :: group_digits ( & mut output , fraction , group_size , false , false ) ;
267
241
}
268
242
269
243
if let Some ( ( separator, exponent) ) = exponent {
270
- let after_e_hint = exponent
271
- . chars ( )
272
- . rev ( )
273
- . filter ( |& c| c != '_' )
274
- . collect :: < Vec < _ > > ( )
275
- . chunks ( group_size)
276
- . map ( |chunk| chunk. iter ( ) . rev ( ) . collect ( ) )
277
- . rev ( )
278
- . collect :: < Vec < String > > ( )
279
- . join ( "_" ) ;
280
-
281
244
output. push ( * separator) ;
282
- output . push_str ( & after_e_hint ) ;
245
+ Self :: group_digits ( & mut output , exponent , group_size , true , false ) ;
283
246
}
284
247
285
248
if let Some ( suffix) = self . suffix {
@@ -296,6 +259,38 @@ impl<'a> DigitInfo<'a> {
296
259
297
260
output
298
261
}
262
+
263
+ fn group_digits ( output : & mut String , input : & str , group_size : usize , partial_group_first : bool , pad : bool ) {
264
+ debug_assert ! ( group_size > 0 ) ;
265
+
266
+ let mut digits = input. chars ( ) . filter ( |& c| c != '_' ) ;
267
+
268
+ let first_group_size;
269
+
270
+ if partial_group_first {
271
+ first_group_size = ( digits. clone ( ) . count ( ) + group_size - 1 ) % group_size + 1 ;
272
+ if pad {
273
+ for _ in 0 ..group_size - first_group_size {
274
+ output. push ( '0' ) ;
275
+ }
276
+ }
277
+ } else {
278
+ first_group_size = group_size;
279
+ }
280
+
281
+ for _ in 0 ..first_group_size {
282
+ if let Some ( digit) = digits. next ( ) {
283
+ output. push ( digit) ;
284
+ }
285
+ }
286
+
287
+ for ( c, i) in digits. zip ( ( 0 ..group_size) . cycle ( ) ) {
288
+ if i == 0 {
289
+ output. push ( '_' ) ;
290
+ }
291
+ output. push ( c) ;
292
+ }
293
+ }
299
294
}
300
295
301
296
enum WarningType {
0 commit comments