// 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 import "math/bits" // FormatUint returns the string representation of i in the given base, // for 2 <= base <= 36. The result uses the lower-case letters 'a' to 'z' // for digit values >= 10. func FormatUint(i uint64, base int) string { if base == 10 { if i < nSmalls { return small(int(i)) } var a [24]byte nd := numDigits(i) formatBase10(a[:nd], i) return string(a[:nd]) } _, s := formatBits(nil, i, base, false, false) return s } // FormatInt returns the string representation of i in the given base, // for 2 <= base <= 36. The result uses the lower-case letters 'a' to 'z' // for digit values >= 10. func FormatInt(i int64, base int) string { if base == 10 { if 0 <= i && i < nSmalls { return small(int(i)) } var a [24]byte u := uint64(i) if i < 0 { u = -u } nd := numDigits(u) formatBase10(a[1:1+nd], u) if i < 0 { a[0] = '-' return string(a[:1+nd]) } return string(a[1 : 1+nd]) } _, s := formatBits(nil, uint64(i), base, i < 0, false) return s } // Itoa is equivalent to [FormatInt](int64(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 { u := uint64(i) if i < 0 { dst = append(dst, '-') u = -u } return AppendUint(dst, u, base) } // 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 { if base == 10 { if i < nSmalls { return append(dst, small(int(i))...) } var a [24]byte nd := numDigits(i) formatBase10(a[:nd], i) return append(dst, a[:nd]...) } dst, _ = formatBits(dst, i, base, false, true) return dst } const digits = "0123456789abcdefghijklmnopqrstuvwxyz" // 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. // The caller is expected to have handled base 10 separately for speed. func formatBits(dst []byte, u uint64, base int, neg, append_ bool) (d []byte, s string) { if base < 2 || base == 10 || base > len(digits) { panic("strconv: illegal AppendInt/FormatInt base") } // 2 <= base && base <= len(digits) var a [64 + 1]byte // +1 for sign of 64bit value in base 2 i := len(a) if neg { u = -u } // convert bits // We use uint values where we can because those will // fit into a single register even on a 32bit machine. if isPowerOfTwo(base) { // Use shifts and masks instead of / and %. shift := uint(bits.TrailingZeros(uint(base))) b := uint64(base) m := uint(base) - 1 // == 1<= b { i-- a[i] = digits[uint(u)&m] u >>= shift } // u < base i-- a[i] = digits[uint(u)] } else { // general case b := uint64(base) for u >= b { i-- // Avoid using r = a%b in addition to q = a/b // since 64bit division and modulo operations // are calculated by runtime functions on 32bit machines. q := u / b a[i] = digits[uint(u-q*b)] u = q } // u < base i-- a[i] = digits[uint(u)] } // add sign, if any if neg { i-- a[i] = '-' } if append_ { d = append(dst, a[i:]...) return } s = string(a[i:]) return } func isPowerOfTwo(x int) bool { return x&(x-1) == 0 } const nSmalls = 100 // smalls is the formatting of 00..99 concatenated. // It is then padded out with 56 x's to 256 bytes, // so that smalls[x&0xFF] has no bounds check. const smalls = "00010203040506070809" + "10111213141516171819" + "20212223242526272829" + "30313233343536373839" + "40414243444546474849" + "50515253545556575859" + "60616263646566676869" + "70717273747576777879" + "80818283848586878889" + "90919293949596979899" // small returns the string for an i with 0 <= i < nSmalls. func small(i int) string { if i < 10 { return digits[i : i+1] } return smalls[i*2 : i*2+2] } // RuntimeFormatBase10 formats u into the tail of a // and returns the offset to the first byte written to a. // It is only for use by package runtime. // Other packages should use AppendUint. func RuntimeFormatBase10(a []byte, u uint64) int { // Note: numDigits requires an argument ≥ 1. // The |1 changes 0 to 1 without adding an extra digit // to any other value. i := len(a) - numDigits(u|1) formatBase10(a[i:], u) return i } // formatBase10 formats the decimal representation of u into a. // The caller is responsible for ensuring that a is big enough to hold u. // If a is too big, leading zeros will be filled in as needed. func formatBase10(a []byte, u uint64) { nd := len(a) for nd >= 8 { // Format last 8 digits (4 pairs). x3210 := uint32(u % 1e8) u /= 1e8 x32, x10 := x3210/1e4, x3210%1e4 x1, x0 := (x10/100)*2, (x10%100)*2 x3, x2 := (x32/100)*2, (x32%100)*2 a[nd-1], a[nd-2] = smalls[x0+1], smalls[x0] a[nd-3], a[nd-4] = smalls[x1+1], smalls[x1] a[nd-5], a[nd-6] = smalls[x2+1], smalls[x2] a[nd-7], a[nd-8] = smalls[x3+1], smalls[x3] nd -= 8 } x := uint32(u) if nd >= 4 { // Format last 4 digits (2 pairs). x10 := x % 1e4 x /= 1e4 x1, x0 := (x10/100)*2, (x10%100)*2 a[nd-1], a[nd-2] = smalls[x0+1], smalls[x0] a[nd-3], a[nd-4] = smalls[x1+1], smalls[x1] nd -= 4 } if nd >= 2 { // Format last 2 digits. x0 := (x % 1e2) * 2 x /= 1e2 a[nd-1], a[nd-2] = smalls[x0+1], smalls[x0] nd -= 2 } if nd > 0 { // Format final digit. a[0] = byte('0' + x) } }