Skip to content

Commit fda93f6

Browse files
fjlkaralabe
andauthored
log: fix formatting of big.Int (#22679)
* log: fix formatting of big.Int The implementation of formatLogfmtBigInt had two issues: it crashed when the number was actually large enough to hit the big integer case, and modified the big.Int while formatting it. * log: don't call FormatLogfmtInt64 for int16 * log: separate from decimals back, not front Co-authored-by: Péter Szilágyi <peterke@gmail.com>
1 parent 3cfd0fe commit fda93f6

2 files changed

Lines changed: 54 additions & 24 deletions

File tree

‎log/format.go‎

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
388391
func 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.
396399
func 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.
438438
func 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

‎log/format_test.go‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package log
22

33
import (
44
"math"
5+
"math/big"
56
"math/rand"
67
"testing"
78
)
@@ -58,6 +59,25 @@ func TestPrettyUint64(t *testing.T) {
5859
}
5960
}
6061

62+
func TestPrettyBigInt(t *testing.T) {
63+
tests := []struct {
64+
int string
65+
s string
66+
}{
67+
{"111222333444555678999", "111,222,333,444,555,678,999"},
68+
{"-111222333444555678999", "-111,222,333,444,555,678,999"},
69+
{"11122233344455567899900", "11,122,233,344,455,567,899,900"},
70+
{"-11122233344455567899900", "-11,122,233,344,455,567,899,900"},
71+
}
72+
73+
for _, tt := range tests {
74+
v, _ := new(big.Int).SetString(tt.int, 10)
75+
if have := formatLogfmtBigInt(v); have != tt.s {
76+
t.Errorf("invalid output %s, want %s", have, tt.s)
77+
}
78+
}
79+
}
80+
6181
var sink string
6282

6383
func BenchmarkPrettyInt64Logfmt(b *testing.B) {

0 commit comments

Comments
 (0)