@@ -160,10 +160,12 @@ fn format_with_modifiers(
160160 let width = if width_str. is_empty ( ) {
161161 0
162162 } else {
163- width_str. parse ( ) . map_err ( |_| FormatError :: FieldWidthTooLarge {
164- width : width_str. to_string ( ) ,
165- specifier : spec. to_string ( ) ,
166- } ) ?
163+ width_str
164+ . parse ( )
165+ . map_err ( |_| FormatError :: FieldWidthTooLarge {
166+ width : width_str. to_string ( ) ,
167+ specifier : spec. to_string ( ) ,
168+ } ) ?
167169 } ;
168170 let modified = apply_modifiers ( & formatted, flags, width, spec) ?;
169171 result. push_str ( & modified) ;
@@ -325,40 +327,38 @@ fn apply_modifiers(
325327 // Zero padding: sign first, then zeros (e.g., "-0022")
326328 let sign = result. chars ( ) . next ( ) . unwrap ( ) ;
327329 let rest = & result[ 1 ..] ;
328- let target_len = result
329- . len ( )
330- . checked_add ( padding)
331- . ok_or_else ( || FormatError :: FieldWidthTooLarge {
332- width : width. to_string ( ) ,
333- specifier : specifier. to_string ( ) ,
334- } ) ?;
335- let mut padded = String :: new ( ) ;
336- padded. try_reserve ( target_len) . map_err ( |_| {
330+ let target_len = result. len ( ) . checked_add ( padding) . ok_or_else ( || {
337331 FormatError :: FieldWidthTooLarge {
338332 width : width. to_string ( ) ,
339333 specifier : specifier. to_string ( ) ,
340334 }
341335 } ) ?;
336+ let mut padded = String :: new ( ) ;
337+ padded
338+ . try_reserve ( target_len)
339+ . map_err ( |_| FormatError :: FieldWidthTooLarge {
340+ width : width. to_string ( ) ,
341+ specifier : specifier. to_string ( ) ,
342+ } ) ?;
342343 padded. push ( sign) ;
343344 padded. extend ( std:: iter:: repeat_n ( '0' , padding) ) ;
344345 padded. push_str ( rest) ;
345346 result = padded;
346347 } else {
347348 // Default: pad on the left (e.g., " -22" or " 1999")
348- let target_len = result
349- . len ( )
350- . checked_add ( padding)
351- . ok_or_else ( || FormatError :: FieldWidthTooLarge {
352- width : width. to_string ( ) ,
353- specifier : specifier. to_string ( ) ,
354- } ) ?;
355- let mut padded = String :: new ( ) ;
356- padded. try_reserve ( target_len) . map_err ( |_| {
349+ let target_len = result. len ( ) . checked_add ( padding) . ok_or_else ( || {
357350 FormatError :: FieldWidthTooLarge {
358351 width : width. to_string ( ) ,
359352 specifier : specifier. to_string ( ) ,
360353 }
361354 } ) ?;
355+ let mut padded = String :: new ( ) ;
356+ padded
357+ . try_reserve ( target_len)
358+ . map_err ( |_| FormatError :: FieldWidthTooLarge {
359+ width : width. to_string ( ) ,
360+ specifier : specifier. to_string ( ) ,
361+ } ) ?;
362362 padded. extend ( std:: iter:: repeat_n ( pad_char, padding) ) ;
363363 padded. push_str ( & result) ;
364364 result = padded;
0 commit comments