Skip to content

Commit

Permalink
internal/itoa, os: move os.uitox to itoa.Uitox
Browse files Browse the repository at this point in the history
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 <[email protected]>
Run-TryBot: Tobias Klauser <[email protected]>
Run-TryBot: Ian Lance Taylor <[email protected]>
Auto-Submit: Tobias Klauser <[email protected]>
Reviewed-by: Ian Lance Taylor <[email protected]>
TryBot-Result: Gopher Robot <[email protected]>
  • Loading branch information
tklauser authored and gopherbot committed Aug 11, 2023
1 parent 9c3ffbf commit e3d7f7c
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 32 deletions.
24 changes: 24 additions & 0 deletions src/internal/itoa/itoa.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:])
}
11 changes: 11 additions & 0 deletions src/internal/itoa/itoa_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
2 changes: 1 addition & 1 deletion src/os/exec_posix.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
31 changes: 0 additions & 31 deletions src/os/str.go

This file was deleted.

0 comments on commit e3d7f7c

Please sign in to comment.