package strconv
+const fastSmalls = true // enable fast path for small integers
+
// 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 i < uint64(len(smallints)) && base == 10 {
- return smallints[i]
+ if fastSmalls && i < nSmalls && base == 10 {
+ return small(int(i))
}
_, s := formatBits(nil, i, base, false, false)
return s
// 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 0 <= i && i < int64(len(smallints)) && base == 10 {
- return smallints[i]
+ if fastSmalls && 0 <= i && i < nSmalls && base == 10 {
+ return small(int(i))
}
_, s := formatBits(nil, uint64(i), base, i < 0, false)
return s
// 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 {
- if 0 <= i && i < int64(len(smallints)) && base == 10 {
- return append(dst, smallints[i]...)
+ if fastSmalls && 0 <= i && i < nSmalls && base == 10 {
+ return append(dst, small(int(i))...)
}
dst, _ = formatBits(dst, uint64(i), base, i < 0, true)
return dst
// 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 i < uint64(len(smallints)) && base == 10 {
- return append(dst, smallints[i]...)
+ if fastSmalls && i < nSmalls && base == 10 {
+ return append(dst, small(int(i))...)
}
dst, _ = formatBits(dst, i, base, false, true)
return dst
}
+// small returns the string for an i with 0 <= i < nSmalls.
+func small(i int) string {
+ off := 0
+ if i < 10 {
+ off = 1
+ }
+ return smallsString[i*2+off : i*2+2]
+}
+
+const nSmalls = 100
+
+const smallsString = "00010203040506070809" +
+ "10111213141516171819" +
+ "20212223242526272829" +
+ "30313233343536373839" +
+ "40414243444546474849" +
+ "50515253545556575859" +
+ "60616263646566676869" +
+ "70717273747576777879" +
+ "80818283848586878889" +
+ "90919293949596979899"
+
const host32bit = ^uint(0)>>32 == 0
const digits = "0123456789abcdefghijklmnopqrstuvwxyz"
-var smallints = [...]string{
- "0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
- "10", "11", "12", "13", "14", "15", "16", "17", "18", "19",
- "20", "21", "22", "23", "24", "25", "26", "27", "28", "29",
- "30", "31", "32", "33", "34", "35", "36", "37", "38", "39",
- "40", "41", "42", "43", "44", "45", "46", "47", "48", "49",
- "50", "51", "52", "53", "54", "55", "56", "57", "58", "59",
- "60", "61", "62", "63", "64", "65", "66", "67", "68", "69",
- "70", "71", "72", "73", "74", "75", "76", "77", "78", "79",
- "80", "81", "82", "83", "84", "85", "86", "87", "88", "89",
- "90", "91", "92", "93", "94", "95", "96", "97", "98", "99",
-}
-
var shifts = [len(digits) + 1]uint{
1 << 1: 1,
1 << 2: 2,