mirror of
https://github.com/golang/go
synced 2024-11-23 07:00:05 -07:00
internal/itoa, os: move os.uitox to itoa.Uitox
This packages already contains other similar functions. Also add a test for it. Change-Id: Iafa8c14f5cb1f5ef89a0e16ccc855c568a3b5727 Reviewed-on: https://go-review.googlesource.com/c/go/+/518317 Reviewed-by: Michael Knyszek <mknyszek@google.com> Run-TryBot: Tobias Klauser <tobias.klauser@gmail.com> Run-TryBot: Ian Lance Taylor <iant@google.com> Auto-Submit: Tobias Klauser <tobias.klauser@gmail.com> Reviewed-by: Ian Lance Taylor <iant@google.com> TryBot-Result: Gopher Robot <gobot@golang.org>
This commit is contained in:
parent
9c3ffbf424
commit
e3d7f7c3f8
@ -31,3 +31,27 @@ func Uitoa(val uint) string {
|
||||
buf[i] = byte('0' + val)
|
||||
return string(buf[i:])
|
||||
}
|
||||
|
||||
const hex = "0123456789abcdef"
|
||||
|
||||
// Uitox converts val (a uint) to a hexadecimal string.
|
||||
func Uitox(val uint) string {
|
||||
if val == 0 { // avoid string allocation
|
||||
return "0x0"
|
||||
}
|
||||
var buf [20]byte // big enough for 64bit value base 16 + 0x
|
||||
i := len(buf) - 1
|
||||
for val >= 16 {
|
||||
q := val / 16
|
||||
buf[i] = hex[val%16]
|
||||
i--
|
||||
val = q
|
||||
}
|
||||
// val < 16
|
||||
buf[i] = hex[val%16]
|
||||
i--
|
||||
buf[i] = 'x'
|
||||
i--
|
||||
buf[i] = '0'
|
||||
return string(buf[i:])
|
||||
}
|
||||
|
@ -38,3 +38,14 @@ func TestUitoa(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestUitox(t *testing.T) {
|
||||
tests := []uint{0, 1, 15, 100, 999, math.MaxUint32, uint(maxUint64)}
|
||||
for _, tt := range tests {
|
||||
got := itoa.Uitox(tt)
|
||||
want := fmt.Sprintf("%#x", tt)
|
||||
if want != got {
|
||||
t.Fatalf("Uitox(%x) = %s, want %s", tt, got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -105,7 +105,7 @@ func (p *ProcessState) String() string {
|
||||
case status.Exited():
|
||||
code := status.ExitStatus()
|
||||
if runtime.GOOS == "windows" && uint(code) >= 1<<16 { // windows uses large hex numbers
|
||||
res = "exit status " + uitox(uint(code))
|
||||
res = "exit status " + itoa.Uitox(uint(code))
|
||||
} else { // unix systems use small decimal integers
|
||||
res = "exit status " + itoa.Itoa(code) // unix
|
||||
}
|
||||
|
@ -1,31 +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.
|
||||
|
||||
// Simple conversions to avoid depending on strconv.
|
||||
|
||||
package os
|
||||
|
||||
const hex = "0123456789abcdef"
|
||||
|
||||
// uitox converts val (a uint) to a hexadecimal string.
|
||||
func uitox(val uint) string {
|
||||
if val == 0 { // avoid string allocation
|
||||
return "0x0"
|
||||
}
|
||||
var buf [20]byte // big enough for 64bit value base 16 + 0x
|
||||
i := len(buf) - 1
|
||||
for val >= 16 {
|
||||
q := val / 16
|
||||
buf[i] = hex[val%16]
|
||||
i--
|
||||
val = q
|
||||
}
|
||||
// val < 16
|
||||
buf[i] = hex[val%16]
|
||||
i--
|
||||
buf[i] = 'x'
|
||||
i--
|
||||
buf[i] = '0'
|
||||
return string(buf[i:])
|
||||
}
|
Loading…
Reference in New Issue
Block a user