diff options
Diffstat (limited to 'src/pkg/strconv/itoa.go')
-rw-r--r-- | src/pkg/strconv/itoa.go | 146 |
1 files changed, 108 insertions, 38 deletions
diff --git a/src/pkg/strconv/itoa.go b/src/pkg/strconv/itoa.go index a0a749664..ca40dd7ef 100644 --- a/src/pkg/strconv/itoa.go +++ b/src/pkg/strconv/itoa.go @@ -4,54 +4,124 @@ package strconv -// Uitob64 returns the string representation of i in the given base. -func Uitob64(u uint64, base uint) string { - if base < 2 || 36 < base { - panic("invalid base " + Uitoa(base)) - } - if u == 0 { - return "0" - } +// 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 +} - // Assemble decimal in reverse order. - var buf [64]byte - j := len(buf) - b := uint64(base) - for u > 0 { - j-- - buf[j] = "0123456789abcdefghijklmnopqrstuvwxyz"[u%b] - u /= b - } +// 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 +} - return string(buf[j:]) +// 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 } -// Itob64 returns the string representation of i in the given base. -func Itob64(i int64, base uint) string { - if i == 0 { - return "0" +const ( + digits = "0123456789abcdefghijklmnopqrstuvwxyz" + digits01 = "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789" + digits10 = "0000000000111111111122222222223333333333444444444455555555556666666666777777777788888888889999999999" +) + +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 neg 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, neg, append_ bool) (d []byte, s string) { + if base < 2 || base > len(digits) { + panic("strconv: illegal AppendInt/FormatInt base") } + // 2 <= base && base <= len(digits) - if i < 0 { - return "-" + Uitob64(-uint64(i), base) + var a [64 + 1]byte // +1 for sign of 64bit value in base 2 + i := len(a) + + if neg { + u = -u } - return Uitob64(uint64(i), base) -} -// Itoa64 returns the decimal string representation of i. -func Itoa64(i int64) string { return Itob64(i, 10) } + // convert bits + if base == 10 { + // common case: use constants for / and % because + // the compiler can optimize it into a multiply+shift, + // and unroll loop + for u >= 100 { + i -= 2 + q := u / 100 + j := uintptr(u - q*100) + a[i+1] = digits01[j] + a[i+0] = digits10[j] + u = q + } + if u >= 10 { + i-- + q := u / 10 + a[i] = digits[uintptr(u-q*10)] + u = q + } -// Uitoa64 returns the decimal string representation of i. -func Uitoa64(i uint64) string { return Uitob64(i, 10) } + } 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 + } -// Uitob returns the string representation of i in the given base. -func Uitob(i uint, base uint) string { return Uitob64(uint64(i), base) } + } else { + // general case + b := uint64(base) + for u >= b { + i-- + a[i] = digits[uintptr(u%b)] + u /= b + } + } -// Itob returns the string representation of i in the given base. -func Itob(i int, base uint) string { return Itob64(int64(i), base) } + // u < base + i-- + a[i] = digits[uintptr(u)] -// Itoa returns the decimal string representation of i. -func Itoa(i int) string { return Itob64(int64(i), 10) } + // add sign, if any + if neg { + i-- + a[i] = '-' + } -// Uitoa returns the decimal string representation of i. -func Uitoa(i uint) string { return Uitob64(uint64(i), 10) } + if append_ { + d = append(dst, a[i:]...) + return + } + s = string(a[i:]) + return +} |