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:])
+}
}
}
}
+
+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)
+ }
+ }
+}
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
}
+++ /dev/null
-// 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:])
-}