mirror of
https://github.com/rwinkhart/sys.git
synced 2026-08-31 14:26:40 -04:00
unix: fix infinite recursion in itoa
Bring in http://golang.org/cl/138650044 from syscall package. Change-Id: I554b0b31b981c682b6826644564971321a55c9e4 Reviewed-on: https://go-review.googlesource.com/10030 Reviewed-by: Rob Pike <r@golang.org>
This commit is contained in:
@@ -0,0 +1,7 @@
|
|||||||
|
// Copyright 2015 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.
|
||||||
|
|
||||||
|
package unix
|
||||||
|
|
||||||
|
var Itoa = itoa
|
||||||
+5
-1
@@ -8,8 +8,12 @@ package unix
|
|||||||
|
|
||||||
func itoa(val int) string { // do it here rather than with fmt to avoid dependency
|
func itoa(val int) string { // do it here rather than with fmt to avoid dependency
|
||||||
if val < 0 {
|
if val < 0 {
|
||||||
return "-" + itoa(-val)
|
return "-" + uitoa(uint(-val))
|
||||||
}
|
}
|
||||||
|
return uitoa(uint(val))
|
||||||
|
}
|
||||||
|
|
||||||
|
func uitoa(val uint) string {
|
||||||
var buf [32]byte // big enough for int64
|
var buf [32]byte // big enough for int64
|
||||||
i := len(buf) - 1
|
i := len(buf) - 1
|
||||||
for val >= 10 {
|
for val >= 10 {
|
||||||
|
|||||||
@@ -7,6 +7,7 @@
|
|||||||
package unix_test
|
package unix_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"golang.org/x/sys/unix"
|
"golang.org/x/sys/unix"
|
||||||
@@ -31,3 +32,19 @@ func TestEnv(t *testing.T) {
|
|||||||
// make sure TESTENV gets set to "", not deleted
|
// make sure TESTENV gets set to "", not deleted
|
||||||
testSetGetenv(t, "TESTENV", "")
|
testSetGetenv(t, "TESTENV", "")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestItoa(t *testing.T) {
|
||||||
|
// Make most negative integer: 0x8000...
|
||||||
|
i := 1
|
||||||
|
for i<<1 != 0 {
|
||||||
|
i <<= 1
|
||||||
|
}
|
||||||
|
if i >= 0 {
|
||||||
|
t.Fatal("bad math")
|
||||||
|
}
|
||||||
|
s := unix.Itoa(i)
|
||||||
|
f := fmt.Sprint(i)
|
||||||
|
if s != f {
|
||||||
|
t.Fatalf("itoa(%d) = %s, want %s", i, s, f)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user