1
0
mirror of https://github.com/golang/go synced 2024-11-11 23:40:22 -07:00

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>
This commit is contained in:
Josh Bleecher Snyder 2021-03-13 16:52:16 -08:00
parent 88b8a16089
commit 061a6903a2
32 changed files with 143 additions and 181 deletions

View File

@ -83,6 +83,7 @@ var depsRules = `
# RUNTIME is the core runtime group of packages, all of them very light-weight.
internal/abi, internal/cpu, unsafe
< internal/bytealg
< internal/itoa
< internal/unsafeheader
< runtime/internal/sys
< runtime/internal/atomic

33
src/internal/itoa/itoa.go Normal file
View File

@ -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:])
}

View File

@ -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)
}
}
}

View File

@ -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
}

View File

@ -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 {

View File

@ -5,6 +5,7 @@
package net
import (
"internal/itoa"
"sort"
"golang.org/x/net/dns/dnsmessage"
@ -33,7 +34,7 @@ func reverseaddr(addr string) (arpa string, err error) {
return "", &DNSError{Err: "unrecognized address", Name: addr}
}
if ip.To4() != nil {
return uitoa(uint(ip[15])) + "." + uitoa(uint(ip[14])) + "." + uitoa(uint(ip[13])) + "." + uitoa(uint(ip[12])) + ".in-addr.arpa.", nil
return itoa.Uitoa(uint(ip[15])) + "." + itoa.Uitoa(uint(ip[14])) + "." + itoa.Uitoa(uint(ip[13])) + "." + itoa.Uitoa(uint(ip[12])) + ".in-addr.arpa.", nil
}
// Must be IPv6
buf := make([]byte, 0, len(ip)*4+len("ip6.arpa."))

View File

@ -18,6 +18,7 @@ package net
import (
"context"
"errors"
"internal/itoa"
"io"
"os"
"sync"
@ -510,7 +511,7 @@ func (o hostLookupOrder) String() string {
if s, ok := lookupOrderName[o]; ok {
return s
}
return "hostLookupOrder=" + itoa(int(o)) + "??"
return "hostLookupOrder=" + itoa.Itoa(int(o)) + "??"
}
// goLookupHost is the native Go implementation of LookupHost.

View File

@ -6,6 +6,7 @@ package net
import (
"errors"
"internal/itoa"
"sync"
"time"
)
@ -230,7 +231,7 @@ func (zc *ipv6ZoneCache) name(index int) string {
zoneCache.RUnlock()
}
if !ok { // last resort
name = uitoa(uint(index))
name = itoa.Uitoa(uint(index))
}
return name
}

View File

@ -6,6 +6,7 @@ package net
import (
"errors"
"internal/itoa"
"os"
)
@ -38,8 +39,8 @@ func interfaceTable(ifindex int) ([]Interface, error) {
func readInterface(i int) (*Interface, error) {
ifc := &Interface{
Index: i + 1, // Offset the index by one to suit the contract
Name: netdir + "/ipifc/" + itoa(i), // Name is the full path to the interface path in plan9
Index: i + 1, // Offset the index by one to suit the contract
Name: netdir + "/ipifc/" + itoa.Itoa(i), // Name is the full path to the interface path in plan9
}
ifcstat := ifc.Name + "/status"

View File

@ -12,7 +12,10 @@
package net
import "internal/bytealg"
import (
"internal/bytealg"
"internal/itoa"
)
// IP address lengths (bytes).
const (
@ -531,7 +534,7 @@ func (n *IPNet) String() string {
if l == -1 {
return nn.String() + "/" + m.String()
}
return nn.String() + "/" + uitoa(uint(l))
return nn.String() + "/" + itoa.Uitoa(uint(l))
}
// Parse IPv4 address (d.d.d.d).

View File

@ -7,6 +7,7 @@ package net
import (
"context"
"internal/bytealg"
"internal/itoa"
"io/fs"
"os"
"syscall"
@ -336,9 +337,9 @@ func plan9LocalAddr(addr Addr) string {
if port == 0 {
return ""
}
return itoa(port)
return itoa.Itoa(port)
}
return ip.String() + "!" + itoa(port)
return ip.String() + "!" + itoa.Itoa(port)
}
func hangupCtlWrite(ctx context.Context, proto string, ctl *os.File, msg string) error {

View File

@ -8,6 +8,7 @@ import (
"context"
"errors"
"internal/bytealg"
"internal/itoa"
"io"
"os"
)
@ -84,7 +85,7 @@ func queryCS1(ctx context.Context, net string, ip IP, port int) (clone, dest str
if len(ip) != 0 && !ip.IsUnspecified() {
ips = ip.String()
}
lines, err := queryCS(ctx, net, ips, itoa(port))
lines, err := queryCS(ctx, net, ips, itoa.Itoa(port))
if err != nil {
return
}

View File

@ -172,32 +172,6 @@ func xtoi2(s string, e byte) (byte, bool) {
return byte(n), ok && ei == 2
}
// 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:])
}
// Convert i to a hexadecimal string. Leading zeros are not printed.
func appendHex(dst []byte, i uint32) []byte {
if i == 0 {

View File

@ -6,6 +6,7 @@ package net
import (
"context"
"internal/itoa"
"io"
"os"
"syscall"
@ -31,9 +32,9 @@ func (a *TCPAddr) String() string {
}
ip := ipEmptyString(a.IP)
if a.Zone != "" {
return JoinHostPort(ip+"%"+a.Zone, itoa(a.Port))
return JoinHostPort(ip+"%"+a.Zone, itoa.Itoa(a.Port))
}
return JoinHostPort(ip, itoa(a.Port))
return JoinHostPort(ip, itoa.Itoa(a.Port))
}
func (a *TCPAddr) isWildcard() bool {

View File

@ -7,6 +7,7 @@
package net
import (
"internal/itoa"
"syscall"
"time"
)
@ -17,7 +18,7 @@ func setNoDelay(fd *netFD, noDelay bool) error {
// Set keep alive period.
func setKeepAlivePeriod(fd *netFD, d time.Duration) error {
cmd := "keepalive " + itoa(int(d/time.Millisecond))
cmd := "keepalive " + itoa.Itoa(int(d/time.Millisecond))
_, e := fd.ctl.WriteAt([]byte(cmd), 0)
return e
}

View File

@ -6,6 +6,7 @@ package net
import (
"context"
"internal/itoa"
"syscall"
)
@ -34,9 +35,9 @@ func (a *UDPAddr) String() string {
}
ip := ipEmptyString(a.IP)
if a.Zone != "" {
return JoinHostPort(ip+"%"+a.Zone, itoa(a.Port))
return JoinHostPort(ip+"%"+a.Zone, itoa.Itoa(a.Port))
}
return JoinHostPort(ip, itoa(a.Port))
return JoinHostPort(ip, itoa.Itoa(a.Port))
}
func (a *UDPAddr) isWildcard() bool {

View File

@ -5,6 +5,7 @@
package os
import (
"internal/itoa"
"runtime"
"syscall"
"time"
@ -40,7 +41,7 @@ func startProcess(name string, argv []string, attr *ProcAttr) (p *Process, err e
}
func (p *Process) writeProcFile(file string, data string) error {
f, e := OpenFile("/proc/"+itoa(p.Pid)+"/"+file, O_WRONLY, 0)
f, e := OpenFile("/proc/"+itoa.Itoa(p.Pid)+"/"+file, O_WRONLY, 0)
if e != nil {
return e
}

View File

@ -8,6 +8,7 @@
package os
import (
"internal/itoa"
"internal/syscall/execenv"
"runtime"
"syscall"
@ -107,14 +108,14 @@ func (p *ProcessState) String() string {
if runtime.GOOS == "windows" && uint(code) >= 1<<16 { // windows uses large hex numbers
res = "exit status " + uitox(uint(code))
} else { // unix systems use small decimal integers
res = "exit status " + itoa(code) // unix
res = "exit status " + itoa.Itoa(code) // unix
}
case status.Signaled():
res = "signal: " + status.Signal().String()
case status.Stopped():
res = "stop signal: " + status.StopSignal().String()
if status.StopSignal() == syscall.SIGTRAP && status.TrapCause() != 0 {
res += " (trap " + itoa(status.TrapCause()) + ")"
res += " (trap " + itoa.Itoa(status.TrapCause()) + ")"
}
case status.Continued():
res = "continued"

View File

@ -7,10 +7,13 @@
package os
import "syscall"
import (
"internal/itoa"
"syscall"
)
func executable() (string, error) {
fn := "/proc/" + itoa(Getpid()) + "/text"
fn := "/proc/" + itoa.Itoa(Getpid()) + "/text"
f, err := Open(fn)
if err != nil {
return "", err

View File

@ -5,6 +5,7 @@
package signal
import (
"internal/itoa"
"os"
"runtime"
"syscall"
@ -155,23 +156,8 @@ func TestStop(t *testing.T) {
}
}
func itoa(val int) string {
if val < 0 {
return "-" + itoa(-val)
}
var buf [32]byte // big enough for int64
i := len(buf) - 1
for val >= 10 {
buf[i] = byte(val%10 + '0')
i--
val /= 10
}
buf[i] = byte(val + '0')
return string(buf[i:])
}
func postNote(pid int, note string) error {
f, err := os.OpenFile("/proc/"+itoa(pid)+"/note", os.O_WRONLY, 0)
f, err := os.OpenFile("/proc/"+itoa.Itoa(pid)+"/note", os.O_WRONLY, 0)
if err != nil {
return err
}

View File

@ -6,32 +6,6 @@
package os
// itoa converts val (an int) to a decimal string.
func itoa(val int) string {
if val < 0 {
return "-" + uitoa(uint(-val))
}
return uitoa(uint(val))
}
// uitoa converts val (a uint) 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:])
}
// itox converts val (an int) to a hexdecimal string.
func itox(val int) string {
if val < 0 {

View File

@ -4,7 +4,10 @@
package os
import "errors"
import (
"errors"
"internal/itoa"
)
// fastrand provided by runtime.
// We generate random temporary file names so that there's a good
@ -13,7 +16,7 @@ import "errors"
func fastrand() uint32
func nextRandom() string {
return uitoa(uint(fastrand()))
return itoa.Uitoa(uint(fastrand()))
}
// CreateTemp creates a new temporary file in the directory dir,

View File

@ -5,6 +5,7 @@
package syscall
import (
"internal/itoa"
"internal/syscall/windows/sysdll"
"sync"
"sync/atomic"
@ -215,7 +216,7 @@ func (p *Proc) Call(a ...uintptr) (r1, r2 uintptr, lastErr error) {
case 18:
return Syscall18(p.Addr(), uintptr(len(a)), a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8], a[9], a[10], a[11], a[12], a[13], a[14], a[15], a[16], a[17])
default:
panic("Call " + p.Name + " with too many arguments " + itoa(len(a)) + ".")
panic("Call " + p.Name + " with too many arguments " + itoa.Itoa(len(a)) + ".")
}
}

View File

@ -8,6 +8,7 @@
package syscall
import (
"internal/itoa"
"runtime"
"unsafe"
)
@ -568,7 +569,7 @@ func forkExecPipe(p []int) (err error) {
func formatIDMappings(idMap []SysProcIDMap) []byte {
var data []byte
for _, im := range idMap {
data = append(data, []byte(itoa(im.ContainerID)+" "+itoa(im.HostID)+" "+itoa(im.Size)+"\n")...)
data = append(data, []byte(itoa.Itoa(im.ContainerID)+" "+itoa.Itoa(im.HostID)+" "+itoa.Itoa(im.Size)+"\n")...)
}
return data
}
@ -597,7 +598,7 @@ func writeIDMappings(path string, idMap []SysProcIDMap) error {
// This is needed since kernel 3.19, because you can't write gid_map without
// disabling setgroups() system call.
func writeSetgroups(pid int, enable bool) error {
sgf := "/proc/" + itoa(pid) + "/setgroups"
sgf := "/proc/" + itoa.Itoa(pid) + "/setgroups"
fd, err := Open(sgf, O_RDWR, 0)
if err != nil {
return err
@ -622,7 +623,7 @@ func writeSetgroups(pid int, enable bool) error {
// for a process and it is called from the parent process.
func writeUidGidMappings(pid int, sys *SysProcAttr) error {
if sys.UidMappings != nil {
uidf := "/proc/" + itoa(pid) + "/uid_map"
uidf := "/proc/" + itoa.Itoa(pid) + "/uid_map"
if err := writeIDMappings(uidf, sys.UidMappings); err != nil {
return err
}
@ -633,7 +634,7 @@ func writeUidGidMappings(pid int, sys *SysProcAttr) error {
if err := writeSetgroups(pid, sys.GidMappingsEnableSetgroups); err != nil && err != ENOENT {
return err
}
gidf := "/proc/" + itoa(pid) + "/gid_map"
gidf := "/proc/" + itoa.Itoa(pid) + "/gid_map"
if err := writeIDMappings(gidf, sys.GidMappings); err != nil {
return err
}

View File

@ -7,6 +7,7 @@
package syscall
import (
"internal/itoa"
"runtime"
"sync"
"unsafe"
@ -320,7 +321,7 @@ func cexecPipe(p []int) error {
return e
}
fd, e := Open("#d/"+itoa(p[1]), O_RDWR|O_CLOEXEC)
fd, e := Open("#d/"+itoa.Itoa(p[1]), O_RDWR|O_CLOEXEC)
if e != nil {
Close(p[0])
Close(p[1])

View File

@ -1,7 +0,0 @@
// Copyright 2014 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 syscall
var Itoa = itoa

View File

@ -1,24 +0,0 @@
// Copyright 2009 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 syscall
func itoa(val int) string { // do it here rather than with fmt to avoid dependency
if val < 0 {
return "-" + uitoa(uint(-val))
}
return uitoa(uint(val))
}
func uitoa(val uint) string {
var buf [32]byte // big enough for int64
i := len(buf) - 1
for val >= 10 {
buf[i] = byte(val%10 + '0')
i--
val /= 10
}
buf[i] = byte(val + '0')
return string(buf[i:])
}

View File

@ -8,6 +8,7 @@
package syscall
import (
"internal/itoa"
"internal/oserror"
"sync"
"unsafe"
@ -60,7 +61,7 @@ func (e Errno) Error() string {
return s
}
}
return "errno " + itoa(int(e))
return "errno " + itoa.Itoa(int(e))
}
func (e Errno) Is(target error) bool {
@ -106,7 +107,7 @@ func (s Signal) String() string {
return str
}
}
return "signal " + itoa(int(s))
return "signal " + itoa.Itoa(int(s))
}
var signals = [...]string{}

View File

@ -11,7 +11,10 @@
package syscall
import "unsafe"
import (
"internal/itoa"
"unsafe"
)
func rawSyscallNoError(trap, a1, a2, a3 uintptr) (r1, r2 uintptr)
@ -225,7 +228,7 @@ func Futimesat(dirfd int, path string, tv []Timeval) (err error) {
func Futimes(fd int, tv []Timeval) (err error) {
// Believe it or not, this is the best we can do on Linux
// (and is what glibc does).
return Utimes("/proc/self/fd/"+itoa(fd), tv)
return Utimes("/proc/self/fd/"+itoa.Itoa(fd), tv)
}
const ImplementsGetwd = true

View File

@ -5,7 +5,6 @@
package syscall_test
import (
"fmt"
"internal/testenv"
"os"
"runtime"
@ -33,22 +32,6 @@ func TestEnv(t *testing.T) {
testSetGetenv(t, "TESTENV", "")
}
func TestItoa(t *testing.T) {
// Make most negative integer: 0x8000...
i := 1
for i<<1 != 0 {
i <<= 1
}
if i >= 0 {
t.Fatal("bad math")
}
s := syscall.Itoa(i)
f := fmt.Sprint(i)
if s != f {
t.Fatalf("itoa(%d) = %s, want %s", i, s, f)
}
}
// Check that permuting child process fds doesn't interfere with
// reporting of fork/exec status. See Issue 14979.
func TestExecErrPermutedFds(t *testing.T) {

View File

@ -8,6 +8,7 @@
package syscall
import (
"internal/itoa"
"internal/oserror"
"internal/race"
"internal/unsafeheader"
@ -121,7 +122,7 @@ func (e Errno) Error() string {
return s
}
}
return "errno " + itoa(int(e))
return "errno " + itoa.Itoa(int(e))
}
func (e Errno) Is(target error) bool {
@ -181,7 +182,7 @@ func (s Signal) String() string {
return str
}
}
return "signal " + itoa(int(s))
return "signal " + itoa.Itoa(int(s))
}
func Read(fd int, p []byte) (n int, err error) {

View File

@ -8,6 +8,7 @@ package syscall
import (
errorspkg "errors"
"internal/itoa"
"internal/oserror"
"internal/race"
"internal/unsafeheader"
@ -132,7 +133,7 @@ func (e Errno) Error() string {
if err != nil {
n, err = formatMessage(flags, 0, uint32(e), 0, b, nil)
if err != nil {
return "winapi error #" + itoa(int(e))
return "winapi error #" + itoa.Itoa(int(e))
}
}
// trim terminating \r and \n
@ -1152,7 +1153,7 @@ func (s Signal) String() string {
return str
}
}
return "signal " + itoa(int(s))
return "signal " + itoa.Itoa(int(s))
}
func LoadCreateSymbolicLink() error {