"math/bits": {},
"math/cmplx": {"math"},
"math/rand": {"L0", "math"},
- "strconv": {"L0", "unicode/utf8", "math"},
+ "strconv": {"L0", "unicode/utf8", "math", "math/bits"},
"unicode/utf16": {},
"unicode/utf8": {},
package strconv
+import "math/bits"
+
const fastSmalls = true // enable fast path for small integers
// FormatUint returns the string representation of i in the given base,
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 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
a[i] = smallsString[is]
}
- } else if s := shifts[base]; s > 0 {
- // base is power of 2: use shifts and masks instead of / and %
+ } else if isPowerOfTwo(base) {
+ // It is known that base is a power of two and
+ // 2 <= base <= len(digits).
+ // Use shifts and masks instead of / and %.
+ shift := uint(bits.TrailingZeros(uint(base))) & 31
b := uint64(base)
- m := uint(base) - 1 // == 1<<s - 1
+ m := uint(base) - 1 // == 1<<shift - 1
for u >= b {
i--
a[i] = digits[uint(u)&m]
- u >>= s
+ u >>= shift
}
// u < base
i--
s = string(a[i:])
return
}
+
+func isPowerOfTwo(x int) bool {
+ return x&(x-1) == 0
+}