From 55a7d891255958fcdbc942ae33ba771709bb9cad Mon Sep 17 00:00:00 2001 From: Mikhail Gunkov Date: Sun, 20 Mar 2022 12:37:34 +0100 Subject: [PATCH 1/3] 2,5 faster implementation of Comma() is added. --- comma.go | 45 +++++++++++++++++++++++++++++---------------- 1 file changed, 29 insertions(+), 16 deletions(-) diff --git a/comma.go b/comma.go index 520ae3e..eccca48 100644 --- a/comma.go +++ b/comma.go @@ -13,34 +13,47 @@ import ( // // e.g. Comma(834142) -> 834,142 func Comma(v int64) string { - sign := "" + if v&^0b111 == 0 { + return string([]byte{byte(v) + 48}) + } // Min int64 can't be negated to a usable value, so it has to be special cased. if v == math.MinInt64 { return "-9,223,372,036,854,775,808" } + // Counting the number of digits. + var count byte = 0 + for n := v; n != 0; n = n / 10 { + count++ + } + count += (count - 1) / 3 if v < 0 { - sign = "-" v = 0 - v + count++ } + output := make([]byte, count) + currentIndex := byte(len(output) - 1) - parts := []string{"", "", "", "", "", "", ""} - j := len(parts) - 1 - - for v > 999 { - parts[j] = strconv.FormatInt(v%1000, 10) - switch len(parts[j]) { - case 2: - parts[j] = "0" + parts[j] - case 1: - parts[j] = "00" + parts[j] + var counter byte = 0 + for v > 9 { + output[currentIndex] = byte(v%10) + 48 + v = v / 10 + currentIndex-- + if counter == 2 { + counter = 0 + output[currentIndex] = ',' + currentIndex-- + } else { + counter++ } - v = v / 1000 - j-- } - parts[j] = strconv.Itoa(int(v)) - return sign + strings.Join(parts[j:], ",") + + output[currentIndex] = byte(v) + 48 + if currentIndex == 1 { + output[0] = '-' + } + return string(output) } // Commaf produces a string form of the given number in base 10 with From 11c8047d981cb052a05d442a6cb6eef6f087cf7d Mon Sep 17 00:00:00 2001 From: Mikhail Gunkov Date: Sun, 20 Mar 2022 12:49:48 +0100 Subject: [PATCH 2/3] renamed currentIndex => j back, j is int now --- comma.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/comma.go b/comma.go index eccca48..a06f6f0 100644 --- a/comma.go +++ b/comma.go @@ -33,24 +33,24 @@ func Comma(v int64) string { count++ } output := make([]byte, count) - currentIndex := byte(len(output) - 1) + j := len(output) - 1 var counter byte = 0 for v > 9 { - output[currentIndex] = byte(v%10) + 48 + output[j] = byte(v%10) + 48 v = v / 10 - currentIndex-- + j-- if counter == 2 { counter = 0 - output[currentIndex] = ',' - currentIndex-- + output[j] = ',' + j-- } else { counter++ } } - output[currentIndex] = byte(v) + 48 - if currentIndex == 1 { + output[j] = byte(v) + 48 + if j == 1 { output[0] = '-' } return string(output) From 258c2df243522611880b1e529123e4a8657b1a8f Mon Sep 17 00:00:00 2001 From: Michael Gunkoff <54396113+kaatinga@users.noreply.github.com> Date: Sat, 23 Mar 2024 23:07:26 +0100 Subject: [PATCH 3/3] Update comma.go MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit done! Co-authored-by: Olivier Mengué --- comma.go | 1 + 1 file changed, 1 insertion(+) diff --git a/comma.go b/comma.go index a06f6f0..9bd66ae 100644 --- a/comma.go +++ b/comma.go @@ -13,6 +13,7 @@ import ( // // e.g. Comma(834142) -> 834,142 func Comma(v int64) string { + // Shortcut for [0, 7] if v&^0b111 == 0 { return string([]byte{byte(v) + 48}) }