@@ -359,20 +359,23 @@ func formatLogfmtValue(value interface{}, term bool) string {
359359 return strconv .FormatFloat (float64 (v ), floatFormat , 3 , 64 )
360360 case float64 :
361361 return strconv .FormatFloat (v , floatFormat , 3 , 64 )
362- case int8 , uint8 :
363- return fmt . Sprintf ( "%d" , value )
364- case int :
365- return FormatLogfmtInt64 (int64 (v ))
362+ case int8 :
363+ return strconv . FormatInt ( int64 ( v ), 10 )
364+ case uint8 :
365+ return strconv . FormatInt (int64 (v ), 10 )
366366 case int16 :
367+ return strconv .FormatInt (int64 (v ), 10 )
368+ case uint16 :
369+ return strconv .FormatInt (int64 (v ), 10 )
370+ // Larger integers get thousands separators.
371+ case int :
367372 return FormatLogfmtInt64 (int64 (v ))
368373 case int32 :
369374 return FormatLogfmtInt64 (int64 (v ))
370375 case int64 :
371376 return FormatLogfmtInt64 (v )
372377 case uint :
373378 return FormatLogfmtUint64 (uint64 (v ))
374- case uint16 :
375- return FormatLogfmtUint64 (uint64 (v ))
376379 case uint32 :
377380 return FormatLogfmtUint64 (uint64 (v ))
378381 case uint64 :
@@ -384,15 +387,15 @@ func formatLogfmtValue(value interface{}, term bool) string {
384387 }
385388}
386389
387- // FormatLogfmtInt64 formats a potentially big number in a friendlier split format .
390+ // FormatLogfmtInt64 formats n with thousand separators .
388391func FormatLogfmtInt64 (n int64 ) string {
389392 if n < 0 {
390393 return formatLogfmtUint64 (uint64 (- n ), true )
391394 }
392395 return formatLogfmtUint64 (uint64 (n ), false )
393396}
394397
395- // FormatLogfmtUint64 formats a potentially big number in a friendlier split format .
398+ // FormatLogfmtUint64 formats n with thousand separators .
396399func FormatLogfmtUint64 (n uint64 ) string {
397400 return formatLogfmtUint64 (n , false )
398401}
@@ -431,31 +434,38 @@ func formatLogfmtUint64(n uint64, neg bool) string {
431434 return string (out [i + 1 :])
432435}
433436
434- var big1000 = big .NewInt (1000 )
435-
436- // formatLogfmtBigInt formats a potentially gigantic number in a friendlier split
437- // format.
437+ // formatLogfmtBigInt formats n with thousand separators.
438438func formatLogfmtBigInt (n * big.Int ) string {
439- // Most number don't need fancy handling, just downcast
440439 if n .IsUint64 () {
441440 return FormatLogfmtUint64 (n .Uint64 ())
442441 }
443442 if n .IsInt64 () {
444443 return FormatLogfmtInt64 (n .Int64 ())
445444 }
446- // Ok, huge number needs huge effort
447- groups := make ([]string , 0 , 8 ) // random initial size to cover most cases
448- for n .Cmp (big1000 ) >= 0 {
449- _ , mod := n .DivMod (n , big1000 , nil )
450- groups = append (groups , fmt .Sprintf ("%03d" , mod ))
451- }
452- groups = append (groups , n .String ())
453445
454- last := len (groups ) - 1
455- for i := 0 ; i < len (groups )/ 2 ; i ++ {
456- groups [i ], groups [last - i ] = groups [last - i ], groups [i ]
446+ var (
447+ text = n .String ()
448+ buf = make ([]byte , len (text )+ len (text )/ 3 )
449+ comma = 0
450+ i = len (buf ) - 1
451+ )
452+ for j := len (text ) - 1 ; j >= 0 ; j , i = j - 1 , i - 1 {
453+ c := text [j ]
454+
455+ switch {
456+ case c == '-' :
457+ buf [i ] = c
458+ case comma == 3 :
459+ buf [i ] = ','
460+ i --
461+ comma = 0
462+ fallthrough
463+ default :
464+ buf [i ] = c
465+ comma ++
466+ }
457467 }
458- return strings . Join ( groups , "," )
468+ return string ( buf [ i + 1 :] )
459469}
460470
461471// escapeString checks if the provided string needs escaping/quoting, and
0 commit comments