]> Cypherpunks repositories - gostls13.git/commitdiff
internal/itoa, os: move os.uitox to itoa.Uitox
authorTobias Klauser <tklauser@distanz.ch>
Thu, 10 Aug 2023 19:48:58 +0000 (21:48 +0200)
committerGopher Robot <gobot@golang.org>
Fri, 11 Aug 2023 02:53:50 +0000 (02:53 +0000)
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>

src/internal/itoa/itoa.go
src/internal/itoa/itoa_test.go
src/os/exec_posix.go
src/os/str.go [deleted file]

index c6062d9fe112bb829a5f736ff4b7a8c5abbfe9e0..4340ae0e2d5ac355ca5b22af5d7b3cf771f9daff 100644 (file)
@@ -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:])
+}
index 71931c1e3a4abda77a4d9e162a15ee84fca50c83..8bed8885323e2ba9913f274eb642a7346a2e1580 100644 (file)
@@ -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)
+               }
+       }
+}
index a512d5199a7d10c45ee217b5d9f1593b8ac0e4aa..4f9ea08cde51d374b57f7ed63f5f07182f2a6a2f 100644 (file)
@@ -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 (file)
index 2089b54..0000000
+++ /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:])
-}