// 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]
+ }
_, 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]
+ }
_, 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]...)
+ }
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]...)
+ }
dst, _ = formatBits(dst, i, base, false, true)
return dst
}
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,
func BenchmarkFormatInt(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, test := range itob64tests {
- FormatInt(test.in, test.base)
+ s := FormatInt(test.in, test.base)
+ BenchSink += len(s)
}
}
}
dst := make([]byte, 0, 30)
for i := 0; i < b.N; i++ {
for _, test := range itob64tests {
- AppendInt(dst, test.in, test.base)
+ dst = AppendInt(dst[:0], test.in, test.base)
+ BenchSink += len(dst)
}
}
}
func BenchmarkFormatUint(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, test := range uitob64tests {
- FormatUint(test.in, test.base)
+ s := FormatUint(test.in, test.base)
+ BenchSink += len(s)
}
}
}
dst := make([]byte, 0, 30)
for i := 0; i < b.N; i++ {
for _, test := range uitob64tests {
- AppendUint(dst, test.in, test.base)
+ dst = AppendUint(dst[:0], test.in, test.base)
+ BenchSink += len(dst)
}
}
}
+
+func BenchmarkFormatIntSmall(b *testing.B) {
+ const smallInt = 42
+ for i := 0; i < b.N; i++ {
+ s := FormatInt(smallInt, 10)
+ BenchSink += len(s)
+ }
+}
+
+func BenchmarkAppendIntSmall(b *testing.B) {
+ dst := make([]byte, 0, 30)
+ const smallInt = 42
+ for i := 0; i < b.N; i++ {
+ dst = AppendInt(dst[:0], smallInt, 10)
+ BenchSink += len(dst)
+ }
+}
+
+var BenchSink int // make sure compiler cannot optimize away benchmarks