1
0
mirror of https://github.com/golang/go synced 2024-10-03 10:21:22 -06:00

add Uitoa etc.

R=r
DELTA=113  (89 added, 9 deleted, 15 changed)
OCL=31087
CL=31096
This commit is contained in:
Russ Cox 2009-07-02 16:57:46 -07:00
parent 0c33d4353e
commit b5666a123a
2 changed files with 104 additions and 24 deletions

View File

@ -4,17 +4,12 @@
package strconv package strconv
// Itob64 returns the string representation of i in the given base. // Uitob64 returns the string representation of i in the given base.
func Itob64(i int64, base uint) string { func Uitob64(u uint64, base uint) string {
if i == 0 { if u == 0 {
return "0" return "0"
} }
u := uint64(i);
if i < 0 {
u = -u;
}
// Assemble decimal in reverse order. // Assemble decimal in reverse order.
var buf [32]byte; var buf [32]byte;
j := len(buf); j := len(buf);
@ -25,12 +20,19 @@ func Itob64(i int64, base uint) string {
u /= b; u /= b;
} }
if i < 0 { // add sign return string(buf[j:len(buf)])
j--; }
buf[j] = '-'
// Itob64 returns the string representation of i in the given base.
func Itob64(i int64, base uint) string {
if i == 0 {
return "0"
} }
return string(buf[j:len(buf)]) if i < 0 {
return "-" + Uitob64(-uint64(i), base);
}
return Uitob64(uint64(i), base);
} }
// Itoa64 returns the decimal string representation of i. // Itoa64 returns the decimal string representation of i.
@ -38,6 +40,16 @@ func Itoa64(i int64) string {
return Itob64(i, 10); return Itob64(i, 10);
} }
// Uitoa64 returns the decimal string representation of i.
func Uitoa64(i uint64) string {
return Uitob64(i, 10);
}
// Uitob returns the string representation of i in the given base.
func Uitob(i uint, base uint) string {
return Uitob64(uint64(i), base);
}
// Itob returns the string representation of i in the given base. // Itob returns the string representation of i in the given base.
func Itob(i int, base uint) string { func Itob(i int, base uint) string {
return Itob64(int64(i), base); return Itob64(int64(i), base);
@ -47,3 +59,8 @@ func Itob(i int, base uint) string {
func Itoa(i int) string { func Itoa(i int) string {
return Itob64(int64(i), 10); return Itob64(int64(i), 10);
} }
// Uitoa returns the decimal string representation of i.
func Uitoa(i uint) string {
return Uitob64(uint64(i), 10);
}

View File

@ -60,21 +60,35 @@ var itob64tests = []itob64Test {
} }
func TestItoa(t *testing.T) { func TestItoa(t *testing.T) {
for i := 0; i < len(itob64tests); i++ { for i, test := range itob64tests {
test := itob64tests[i];
s := strconv.Itob64(test.in, test.base); s := strconv.Itob64(test.in, test.base);
if s != test.out { if s != test.out {
t.Errorf("strconv.Itob64(%v, %v) = %v want %v\n", t.Errorf("strconv.Itob64(%v, %v) = %v want %v\n",
test.in, test.base, s, test.out); test.in, test.base, s, test.out);
} }
if test.in >= 0 {
s := strconv.Uitob64(uint64(test.in), test.base);
if s != test.out {
t.Errorf("strconv.Uitob64(%v, %v) = %v want %v\n",
test.in, test.base, s, test.out);
}
}
if int64(int(test.in)) == test.in { if int64(int(test.in)) == test.in {
s := strconv.Itob(int(test.in), test.base); s := strconv.Itob(int(test.in), test.base);
if s != test.out { if s != test.out {
t.Errorf("strconv.Itob(%v, %v) = %v want %v\n", t.Errorf("strconv.Itob(%v, %v) = %v want %v\n",
test.in, test.base, s, test.out); test.in, test.base, s, test.out);
} }
if test.in >= 0 {
s := strconv.Uitob(uint(test.in), test.base);
if s != test.out {
t.Errorf("strconv.Uitob(%v, %v) = %v want %v\n",
test.in, test.base, s, test.out);
}
}
} }
if test.base == 10 { if test.base == 10 {
@ -84,28 +98,77 @@ func TestItoa(t *testing.T) {
test.in, s, test.out); test.in, s, test.out);
} }
if test.in >= 0 {
s := strconv.Uitob64(uint64(test.in), test.base);
if s != test.out {
t.Errorf("strconv.Uitob64(%v, %v) = %v want %v\n",
test.in, test.base, s, test.out);
}
}
if int64(int(test.in)) == test.in { if int64(int(test.in)) == test.in {
s := strconv.Itoa(int(test.in)); s := strconv.Itoa(int(test.in));
if s != test.out { if s != test.out {
t.Errorf("strconv.Itoa(%v) = %v want %v\n", t.Errorf("strconv.Itoa(%v) = %v want %v\n",
test.in, s, test.out); test.in, s, test.out);
} }
if test.in >= 0 {
s := strconv.Uitoa(uint(test.in));
if s != test.out {
t.Errorf("strconv.Uitoa(%v) = %v want %v\n",
test.in, s, test.out);
}
}
} }
} }
} }
} }
// TODO: Use once there is a strconv.uitoa type uitob64Test struct {
type uitoa64Test struct {
in uint64; in uint64;
base uint;
out string; out string;
} }
// TODO: should be able to call this atoui64tests. var uitob64tests = []uitob64Test {
var uitoa64tests = []uitoa64Test { uitob64Test{ 1<<63-1, 10, "9223372036854775807" },
uitoa64Test{ 1<<63-1, "9223372036854775807" }, uitob64Test{ 1<<63, 10, "9223372036854775808" },
uitoa64Test{ 1<<63, "9223372036854775808" }, uitob64Test{ 1<<63+1, 10, "9223372036854775809" },
uitoa64Test{ 1<<63+1, "9223372036854775809" }, uitob64Test{ 1<<64-2, 10, "18446744073709551614" },
uitoa64Test{ 1<<64-2, "18446744073709551614" }, uitob64Test{ 1<<64-1, 10, "18446744073709551615" },
uitoa64Test{ 1<<64-1, "18446744073709551615" }, }
func TestUitoa(t *testing.T) {
for i, test := range uitob64tests {
s := strconv.Uitob64(test.in, test.base);
if s != test.out {
t.Errorf("strconv.Uitob64(%v, %v) = %v want %v\n",
test.in, test.base, s, test.out);
}
if uint64(uint(test.in)) == test.in {
s := strconv.Uitob(uint(test.in), test.base);
if s != test.out {
t.Errorf("strconv.Uitob(%v, %v) = %v want %v\n",
test.in, test.base, s, test.out);
}
}
if test.base == 10 {
s := strconv.Uitoa64(test.in);
if s != test.out {
t.Errorf("strconv.Uitoa64(%v) = %v want %v\n",
test.in, s, test.out);
}
if uint64(uint(test.in)) == test.in {
s := strconv.Uitoa(uint(test.in));
if s != test.out {
t.Errorf("strconv.Uitoa(%v) = %v want %v\n",
test.in, s, test.out);
}
}
}
}
} }