aboutsummaryrefslogtreecommitdiff
path: root/src/internal
diff options
context:
space:
mode:
authorJosh Bleecher Snyder <josharian@gmail.com>2021-03-13 16:52:16 -0800
committerJosh Bleecher Snyder <josharian@gmail.com>2021-03-14 17:56:50 +0000
commit061a6903a232cb868780b1e724a75bf92a728489 (patch)
treee8dc88c37a5e79e1aa138dffbeb70439543ee54c /src/internal
parent88b8a1608987d494f3f29618e7524e61712c31ba (diff)
downloadgo-061a6903a232cb868780b1e724a75bf92a728489.tar.gz
go-061a6903a232cb868780b1e724a75bf92a728489.zip
all: add internal/itoa package
This replaces five implementations scattered across low level packages. (And I plan to use it in a sixth soon.) Three of the five were byte-for-byte identical. Change-Id: I3bbbeeac63723a487986c912b604e10ad1e042f4 Reviewed-on: https://go-review.googlesource.com/c/go/+/301549 Trust: Josh Bleecher Snyder <josharian@gmail.com> Run-TryBot: Josh Bleecher Snyder <josharian@gmail.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Emmanuel Odeke <emmanuel@orijtech.com>
Diffstat (limited to 'src/internal')
-rw-r--r--src/internal/itoa/itoa.go33
-rw-r--r--src/internal/itoa/itoa_test.go40
-rw-r--r--src/internal/poll/fd_io_plan9.go3
-rw-r--r--src/internal/poll/strconv.go28
4 files changed, 75 insertions, 29 deletions
diff --git a/src/internal/itoa/itoa.go b/src/internal/itoa/itoa.go
new file mode 100644
index 0000000000..c6062d9fe1
--- /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 0000000000..71931c1e3a
--- /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)
+ }
+ }
+}
diff --git a/src/internal/poll/fd_io_plan9.go b/src/internal/poll/fd_io_plan9.go
index 287d11bd8c..3205ac8513 100644
--- a/src/internal/poll/fd_io_plan9.go
+++ b/src/internal/poll/fd_io_plan9.go
@@ -5,6 +5,7 @@
package poll
import (
+ "internal/itoa"
"runtime"
"sync"
"syscall"
@@ -71,7 +72,7 @@ func (aio *asyncIO) Cancel() {
if aio.pid == -1 {
return
}
- f, e := syscall.Open("/proc/"+itoa(aio.pid)+"/note", syscall.O_WRONLY)
+ f, e := syscall.Open("/proc/"+itoa.Itoa(aio.pid)+"/note", syscall.O_WRONLY)
if e != nil {
return
}
diff --git a/src/internal/poll/strconv.go b/src/internal/poll/strconv.go
index fd5e20f1f4..c98332d3da 100644
--- a/src/internal/poll/strconv.go
+++ b/src/internal/poll/strconv.go
@@ -5,36 +5,8 @@
//go:build plan9
// +build plan9
-// Simple conversions to avoid depending on strconv.
-
package poll
-// Convert integer to decimal string
-func itoa(val int) string {
- if val < 0 {
- return "-" + uitoa(uint(-val))
- }
- return uitoa(uint(val))
-}
-
-// Convert unsigned integer to 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:])
-}
-
// stringsHasSuffix is strings.HasSuffix. It reports whether s ends in
// suffix.
func stringsHasSuffix(s, suffix string) bool {