From: Tobias Klauser Date: Thu, 10 Aug 2023 19:48:58 +0000 (+0200) Subject: internal/itoa, os: move os.uitox to itoa.Uitox X-Git-Tag: go1.22rc1~1329 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=e3d7f7c3f8b0537d84e704986d88eb35271fa233;p=gostls13.git 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 Run-TryBot: Tobias Klauser Run-TryBot: Ian Lance Taylor Auto-Submit: Tobias Klauser Reviewed-by: Ian Lance Taylor TryBot-Result: Gopher Robot --- diff --git a/src/internal/itoa/itoa.go b/src/internal/itoa/itoa.go index c6062d9fe1..4340ae0e2d 100644 --- a/src/internal/itoa/itoa.go +++ b/src/internal/itoa/itoa.go @@ -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:]) +} diff --git a/src/internal/itoa/itoa_test.go b/src/internal/itoa/itoa_test.go index 71931c1e3a..8bed888532 100644 --- a/src/internal/itoa/itoa_test.go +++ b/src/internal/itoa/itoa_test.go @@ -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) + } + } +} diff --git a/src/os/exec_posix.go b/src/os/exec_posix.go index a512d5199a..4f9ea08cde 100644 --- a/src/os/exec_posix.go +++ b/src/os/exec_posix.go @@ -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 } diff --git a/src/os/str.go b/src/os/str.go deleted file mode 100644 index 2089b548e6..0000000000 --- a/src/os/str.go +++ /dev/null @@ -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:]) -}