2008-11-17 13:34:03 -07:00
|
|
|
// 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.
|
|
|
|
|
|
|
|
package strconv
|
|
|
|
|
2009-07-02 17:57:46 -06:00
|
|
|
// Uitob64 returns the string representation of i in the given base.
|
|
|
|
func Uitob64(u uint64, base uint) string {
|
2010-07-01 15:44:21 -06:00
|
|
|
if base < 2 || 36 < base {
|
|
|
|
panic("invalid base " + Uitoa(base))
|
|
|
|
}
|
2009-07-02 17:57:46 -06:00
|
|
|
if u == 0 {
|
2009-11-09 13:07:39 -07:00
|
|
|
return "0"
|
2008-11-17 13:34:03 -07:00
|
|
|
}
|
2008-11-19 13:50:34 -07:00
|
|
|
|
2008-11-17 13:34:03 -07:00
|
|
|
// Assemble decimal in reverse order.
|
2010-07-01 15:44:21 -06:00
|
|
|
var buf [64]byte
|
2009-12-15 16:40:16 -07:00
|
|
|
j := len(buf)
|
|
|
|
b := uint64(base)
|
2009-03-02 18:52:58 -07:00
|
|
|
for u > 0 {
|
2009-12-15 16:40:16 -07:00
|
|
|
j--
|
|
|
|
buf[j] = "0123456789abcdefghijklmnopqrstuvwxyz"[u%b]
|
|
|
|
u /= b
|
2008-11-17 13:34:03 -07:00
|
|
|
}
|
2009-03-02 18:52:58 -07:00
|
|
|
|
2009-12-15 16:40:16 -07:00
|
|
|
return string(buf[j:])
|
2009-07-02 17:57:46 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Itob64 returns the string representation of i in the given base.
|
|
|
|
func Itob64(i int64, base uint) string {
|
|
|
|
if i == 0 {
|
2009-11-09 13:07:39 -07:00
|
|
|
return "0"
|
2008-11-17 13:34:03 -07:00
|
|
|
}
|
2008-11-19 13:50:34 -07:00
|
|
|
|
2009-07-02 17:57:46 -06:00
|
|
|
if i < 0 {
|
2009-11-09 13:07:39 -07:00
|
|
|
return "-" + Uitob64(-uint64(i), base)
|
2009-07-02 17:57:46 -06:00
|
|
|
}
|
2009-12-15 16:40:16 -07:00
|
|
|
return Uitob64(uint64(i), base)
|
2008-11-17 13:34:03 -07:00
|
|
|
}
|
|
|
|
|
2009-03-05 16:29:04 -07:00
|
|
|
// Itoa64 returns the decimal string representation of i.
|
2009-12-15 16:40:16 -07:00
|
|
|
func Itoa64(i int64) string { return Itob64(i, 10) }
|
2008-11-17 13:34:03 -07:00
|
|
|
|
2009-07-02 17:57:46 -06:00
|
|
|
// Uitoa64 returns the decimal string representation of i.
|
2009-12-15 16:40:16 -07:00
|
|
|
func Uitoa64(i uint64) string { return Uitob64(i, 10) }
|
2009-07-02 17:57:46 -06:00
|
|
|
|
|
|
|
// Uitob returns the string representation of i in the given base.
|
2009-12-15 16:40:16 -07:00
|
|
|
func Uitob(i uint, base uint) string { return Uitob64(uint64(i), base) }
|
2009-07-02 17:57:46 -06:00
|
|
|
|
2009-03-05 16:29:04 -07:00
|
|
|
// Itob returns the string representation of i in the given base.
|
2009-12-15 16:40:16 -07:00
|
|
|
func Itob(i int, base uint) string { return Itob64(int64(i), base) }
|
2009-03-02 18:52:58 -07:00
|
|
|
|
2009-03-05 16:29:04 -07:00
|
|
|
// Itoa returns the decimal string representation of i.
|
2009-12-15 16:40:16 -07:00
|
|
|
func Itoa(i int) string { return Itob64(int64(i), 10) }
|
2009-07-02 17:57:46 -06:00
|
|
|
|
|
|
|
// Uitoa returns the decimal string representation of i.
|
2009-12-15 16:40:16 -07:00
|
|
|
func Uitoa(i uint) string { return Uitob64(uint64(i), 10) }
|