diff options
author | Dan Kegel <[email protected]> | 2021-11-14 20:51:54 -0800 |
---|---|---|
committer | Ayke <[email protected]> | 2021-11-16 02:13:52 +0100 |
commit | b70b6076e3fd228f66feb12f8ba6be82c4e7926b (patch) | |
tree | 8b7e3b316929d7311cded7d8102ac8d42a8e63ff /src/internal | |
parent | 7b41d92198e9d28325ce1f0ec9be62e7f6eb1a6e (diff) | |
download | tinygo-b70b6076e3fd228f66feb12f8ba6be82c4e7926b.tar.gz tinygo-b70b6076e3fd228f66feb12f8ba6be82c4e7926b.zip |
net/ip, syscall/errno: Reduce code duplication by switching to internal/itoa.
internal/itoa wasn't around back in go 1.12 days when tinygo's syscall/errno.go was written.
It was only added as of go 1.17 ( https://github.com/golang/go/commit/061a6903a232cb868780b )
so we have to have an internal copy for now.
The internal copy should be deleted when tinygo drops support for go 1.16.
FWIW, the new version seems nicer.
It uses no allocations when converting 0,
and although the optimizer might make this moot, uses
a multiplication x 10 instead of a mod operation.
Diffstat (limited to 'src/internal')
-rw-r--r-- | src/internal/itoa/README.md | 2 | ||||
-rw-r--r-- | src/internal/itoa/itoa.go | 33 | ||||
-rw-r--r-- | src/internal/itoa/itoa_test.go | 40 |
3 files changed, 75 insertions, 0 deletions
diff --git a/src/internal/itoa/README.md b/src/internal/itoa/README.md new file mode 100644 index 000000000..6b666d121 --- /dev/null +++ b/src/internal/itoa/README.md @@ -0,0 +1,2 @@ +internal/itoa is new to go as of 1.17. +This directory should be removed when tinygo drops support for go 1.16. diff --git a/src/internal/itoa/itoa.go b/src/internal/itoa/itoa.go new file mode 100644 index 000000000..c6062d9fe --- /dev/null +++ b/src/internal/itoa/itoa.go @@ -0,0 +1,33 @@ +// Copyright 2021 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Simple conversions to avoid depending on strconv. + +package itoa + +// Itoa converts val to a decimal string. +func Itoa(val int) string { + if val < 0 { + return "-" + Uitoa(uint(-val)) + } + return Uitoa(uint(val)) +} + +// Uitoa converts val to a decimal string. +func Uitoa(val uint) string { + if val == 0 { // avoid string allocation + return "0" + } + var buf [20]byte // big enough for 64bit value base 10 + i := len(buf) - 1 + for val >= 10 { + q := val / 10 + buf[i] = byte('0' + val - q*10) + i-- + val = q + } + // val < 10 + buf[i] = byte('0' + val) + return string(buf[i:]) +} diff --git a/src/internal/itoa/itoa_test.go b/src/internal/itoa/itoa_test.go new file mode 100644 index 000000000..71931c1e3 --- /dev/null +++ b/src/internal/itoa/itoa_test.go @@ -0,0 +1,40 @@ +// Copyright 2021 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package itoa_test + +import ( + "fmt" + "internal/itoa" + "math" + "testing" +) + +var ( + minInt64 int64 = math.MinInt64 + maxInt64 int64 = math.MaxInt64 + maxUint64 uint64 = math.MaxUint64 +) + +func TestItoa(t *testing.T) { + tests := []int{int(minInt64), math.MinInt32, -999, -100, -1, 0, 1, 100, 999, math.MaxInt32, int(maxInt64)} + for _, tt := range tests { + got := itoa.Itoa(tt) + want := fmt.Sprint(tt) + if want != got { + t.Fatalf("Itoa(%d) = %s, want %s", tt, got, want) + } + } +} + +func TestUitoa(t *testing.T) { + tests := []uint{0, 1, 100, 999, math.MaxUint32, uint(maxUint64)} + for _, tt := range tests { + got := itoa.Uitoa(tt) + want := fmt.Sprint(tt) + if want != got { + t.Fatalf("Uitoa(%d) = %s, want %s", tt, got, want) + } + } +} |