From c1b13e2d3700222adf8e440cb1fa1673a9904170 Mon Sep 17 00:00:00 2001 From: Thomas Voss Date: Sat, 10 Aug 2024 13:13:08 +0200 Subject: Refactor strconv.Iota() out --- cmd/mfmt/main.go | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/cmd/mfmt/main.go b/cmd/mfmt/main.go index 2f204ab..5c189ea 100644 --- a/cmd/mfmt/main.go +++ b/cmd/mfmt/main.go @@ -5,7 +5,6 @@ import ( "io" "os" "path/filepath" - "strconv" "strings" "unsafe" @@ -94,7 +93,8 @@ func formatSection(out io.Writer, rows []mintages.Row) { } year = s for i, col := range row.Cols { - longestNum[i] = max(longestNum[i], intlen(col)) + n := intlen(col) + longestNum[i] = max(longestNum[i], n+(n-1)/3) } } @@ -139,18 +139,19 @@ func formatInt(n int) string { return "?" } - in := strconv.Itoa(n) - dots := (len(in) - 1) / 3 - out := make([]byte, len(in)+dots) + digits := intlen(n) + dots := (digits - 1) / 3 + out := make([]byte, digits+dots) - for i, j, k := len(in)-1, len(out)-1, 0; ; i, j = i-1, j-1 { - out[j] = in[i] - if i == 0 { + for i, j := len(out)-1, 0; ; i-- { + out[i] = byte(n%10) + 48 + n /= 10 + if n == 0 { return string(out) } - if k++; k == 3 { - j, k = j-1, 0 - out[j] = '.' + if j++; j == 3 { + i, j = i-1, 0 + out[i] = '.' } } } @@ -166,7 +167,7 @@ func intlen(v int) int { for x := v; x != 0; x /= 10 { n++ } - return n + (n-1)/3 /* Thousands separators */ + return n } } -- cgit v1.2.3