2010-12-03 05:34:57 +01: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
|
|
|
|
|
2011-12-14 16:41:54 +01:00
|
|
|
// FormatUint returns the string representation of i in the given base.
|
|
|
|
func FormatUint(i uint64, base int) string {
|
|
|
|
_, s := formatBits(nil, i, base, false, false)
|
|
|
|
return s
|
|
|
|
}
|
2010-12-03 05:34:57 +01:00
|
|
|
|
2011-12-14 16:41:54 +01:00
|
|
|
// FormatInt returns the string representation of i in the given base.
|
|
|
|
func FormatInt(i int64, base int) string {
|
|
|
|
_, s := formatBits(nil, uint64(i), base, i < 0, false)
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
// Itoa is shorthand for FormatInt(i, 10).
|
|
|
|
func Itoa(i int) string {
|
|
|
|
return FormatInt(int64(i), 10)
|
|
|
|
}
|
|
|
|
|
|
|
|
// AppendInt appends the string form of the integer i,
|
|
|
|
// as generated by FormatInt, to dst and returns the extended buffer.
|
|
|
|
func AppendInt(dst []byte, i int64, base int) []byte {
|
|
|
|
dst, _ = formatBits(dst, uint64(i), base, i < 0, true)
|
|
|
|
return dst
|
|
|
|
}
|
2010-12-03 05:34:57 +01:00
|
|
|
|
2011-12-14 16:41:54 +01:00
|
|
|
// AppendUint appends the string form of the unsigned integer i,
|
|
|
|
// as generated by FormatUint, to dst and returns the extended buffer.
|
|
|
|
func AppendUint(dst []byte, i uint64, base int) []byte {
|
|
|
|
dst, _ = formatBits(dst, i, base, false, true)
|
|
|
|
return dst
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
2011-12-14 16:41:54 +01:00
|
|
|
const digits = "0123456789abcdefghijklmnopqrstuvwxyz"
|
|
|
|
|
|
|
|
var shifts = [len(digits) + 1]uint{
|
|
|
|
1 << 1: 1,
|
|
|
|
1 << 2: 2,
|
|
|
|
1 << 3: 3,
|
|
|
|
1 << 4: 4,
|
|
|
|
1 << 5: 5,
|
|
|
|
}
|
|
|
|
|
|
|
|
// formatBits computes the string representation of u in the given base.
|
|
|
|
// If negative is set, u is treated as negative int64 value. If append_
|
|
|
|
// is set, the string is appended to dst and the resulting byte slice is
|
|
|
|
// returned as the first result value; otherwise the string is returned
|
|
|
|
// as the second result value.
|
|
|
|
//
|
|
|
|
func formatBits(dst []byte, u uint64, base int, negative, append_ bool) (d []byte, s string) {
|
|
|
|
if base < 2 || base > len(digits) {
|
|
|
|
panic("invalid base")
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2011-12-14 16:41:54 +01:00
|
|
|
// 2 <= base && base <= len(digits)
|
2010-12-03 05:34:57 +01:00
|
|
|
|
2011-12-14 16:41:54 +01:00
|
|
|
var a [64 + 1]byte // +1 for sign of 64bit value in base 2
|
|
|
|
i := len(a)
|
|
|
|
|
|
|
|
if negative {
|
|
|
|
u = -u
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
2011-12-14 16:41:54 +01:00
|
|
|
// convert bits
|
|
|
|
if base == 10 {
|
|
|
|
// common case: use constant 10 for / and % because
|
|
|
|
// the compiler can optimize it into a multiply+shift
|
|
|
|
for u >= 10 {
|
|
|
|
i--
|
|
|
|
a[i] = digits[u%10]
|
|
|
|
u /= 10
|
|
|
|
}
|
2010-12-03 05:34:57 +01:00
|
|
|
|
2011-12-14 16:41:54 +01:00
|
|
|
} else if s := shifts[base]; s > 0 {
|
|
|
|
// base is power of 2: use shifts and masks instead of / and %
|
|
|
|
b := uint64(base)
|
|
|
|
m := uintptr(b) - 1 // == 1<<s - 1
|
|
|
|
for u >= b {
|
|
|
|
i--
|
|
|
|
a[i] = digits[uintptr(u)&m]
|
|
|
|
u >>= s
|
|
|
|
}
|
2010-12-03 05:34:57 +01:00
|
|
|
|
2011-12-14 16:41:54 +01:00
|
|
|
} else {
|
|
|
|
// general case
|
|
|
|
b := uint64(base)
|
|
|
|
for u >= b {
|
|
|
|
i--
|
|
|
|
a[i] = digits[u%b]
|
|
|
|
u /= b
|
|
|
|
}
|
|
|
|
}
|
2010-12-03 05:34:57 +01:00
|
|
|
|
2011-12-14 16:41:54 +01:00
|
|
|
// u < base
|
|
|
|
i--
|
|
|
|
a[i] = digits[uintptr(u)]
|
2010-12-03 05:34:57 +01:00
|
|
|
|
2011-12-14 16:41:54 +01:00
|
|
|
// add sign, if any
|
|
|
|
if negative {
|
|
|
|
i--
|
|
|
|
a[i] = '-'
|
|
|
|
}
|
2010-12-03 05:34:57 +01:00
|
|
|
|
2011-12-14 16:41:54 +01:00
|
|
|
if append_ {
|
|
|
|
d = append(dst, a[i:]...)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
s = string(a[i:])
|
|
|
|
return
|
|
|
|
}
|