| 1 |
package utils |
| 2 |
|
| 3 |
import ( |
| 4 |
"encoding/binary" |
| 5 |
"math/bits" |
| 6 |
) |
| 7 |
|
| 8 |
// smallInts contains precomputed string representations for small integers 0-99 |
| 9 |
var smallInts [100]string |
| 10 |
|
| 11 |
// smallNegInts contains precomputed string representations for small negative integers -1 to -99 |
| 12 |
var smallNegInts [100]string |
| 13 |
|
| 14 |
// uint8Strs contains precomputed string representations for all uint8 values. |
| 15 |
var uint8Strs [256]string |
| 16 |
|
| 17 |
// int8Strs contains precomputed string representations for all int8 values indexed by uint8(value). |
| 18 |
var int8Strs [256]string |
| 19 |
|
| 20 |
func init() { |
| 21 |
for i := range 100 { |
| 22 |
smallInts[i] = formatUintSmall(uint64(i)) |
| 23 |
if i > 0 { |
| 24 |
smallNegInts[i] = "-" + smallInts[i] |
| 25 |
} |
| 26 |
} |
| 27 |
|
| 28 |
for i := range 256 { |
| 29 |
v := uint8(i) |
| 30 |
uint8Strs[i] = formatUint8Slow(v) |
| 31 |
|
| 32 |
sv := int8(i) |
| 33 |
if sv >= 0 { |
| 34 |
int8Strs[i] = uint8Strs[sv] |
| 35 |
} else { |
| 36 |
int8Strs[i] = "-" + uint8Strs[uint8(-sv)] |
| 37 |
} |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
func formatUintSmall(n uint64) string { |
| 42 |
if n < 10 { |
| 43 |
return string(byte(n) + '0') |
| 44 |
} |
| 45 |
return string([]byte{byte(n/10) + '0', byte(n%10) + '0'}) |
| 46 |
} |
| 47 |
|
| 48 |
func formatUint8Slow(n uint8) string { |
| 49 |
if n < 100 { |
| 50 |
return smallInts[n] |
| 51 |
} |
| 52 |
return string([]byte{n/100 + '0', (n/10)%10 + '0', n%10 + '0'}) |
| 53 |
} |
| 54 |
|
| 55 |
// decimalPairs holds "00".."99" back to back: decimalPairs[2*n:2*n+2] is n zero-padded. |
| 56 |
const decimalPairs = "00010203040506070809101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899" |
| 57 |
|
| 58 |
// Formatting works on 8-digit groups: digits8 turns a value below 1e8 into |
| 59 |
// eight zero-padded digit lanes (most significant first) with lane-parallel |
| 60 |
// divisions, and leading zeros are zero lanes found by a trailing-zero count. |
| 61 |
const ( |
| 62 |
digitZeros = 0x3030303030303030 // '0' in every lane; digit lanes are below 16, so OR equals add |
| 63 |
digitsBufLen = 24 // three lane groups; 20 digits plus a sign fit |
| 64 |
digitsGroup = 100000000 // value of one lane group |
| 65 |
) |
| 66 |
|
| 67 |
// digits8 returns the eight zero-padded digits of n < 1e8 as 0..9 byte lanes. |
| 68 |
// Each multiply is a per-lane division by 100 or 10; no step carries across lanes. |
| 69 |
func digits8(n uint32) uint64 { |
| 70 |
hi := n / 10000 // two 4-digit halves in 32-bit lanes, high half in the low lane |
| 71 |
x := uint64(hi) | uint64(n-hi*10000)<<32 |
| 72 |
q := (x * 5243 >> 19) & 0x0000007F0000007F // lane/100, exact below 43699 |
| 73 |
y := q | (x-q*100)<<16 // lane%100 into the upper 16 bits |
| 74 |
q = (y * 103 >> 10) & 0x000F000F000F000F // lane/10, exact below 100 |
| 75 |
return q | (y-q*10)<<8 // lane%10 into the upper byte |
| 76 |
} |
| 77 |
|
| 78 |
// uintToBuf writes the digits of n >= 100 right-aligned into buf and returns |
| 79 |
// the index of the first digit. |
| 80 |
func uintToBuf(buf *[digitsBufLen]byte, n uint64) int { |
| 81 |
if n < digitsGroup { |
| 82 |
z := digits8(uint32(n)) |
| 83 |
binary.LittleEndian.PutUint64(buf[16:24], z|digitZeros) |
| 84 |
return 16 + bits.TrailingZeros64(z)>>3 |
| 85 |
} |
| 86 |
hi := n / digitsGroup |
| 87 |
binary.LittleEndian.PutUint64(buf[16:24], digits8(uint32(n-hi*digitsGroup))|digitZeros) |
| 88 |
if hi < digitsGroup { |
| 89 |
z := digits8(uint32(hi)) |
| 90 |
binary.LittleEndian.PutUint64(buf[8:16], z|digitZeros) |
| 91 |
return 8 + bits.TrailingZeros64(z)>>3 |
| 92 |
} |
| 93 |
top := uint32(hi / digitsGroup) // at most 1844 |
| 94 |
binary.LittleEndian.PutUint64(buf[8:16], digits8(uint32(hi-uint64(top)*digitsGroup))|digitZeros) |
| 95 |
z := digits8(top) |
| 96 |
binary.LittleEndian.PutUint64(buf[0:8], z|digitZeros) |
| 97 |
return bits.TrailingZeros64(z) >> 3 |
| 98 |
} |
| 99 |
|
| 100 |
// uint32ToBuf is uintToBuf for 32-bit values, which need at most two groups. |
| 101 |
func uint32ToBuf(buf *[16]byte, n uint32) int { |
| 102 |
if n < digitsGroup { |
| 103 |
z := digits8(n) |
| 104 |
binary.LittleEndian.PutUint64(buf[8:16], z|digitZeros) |
| 105 |
return 8 + bits.TrailingZeros64(z)>>3 |
| 106 |
} |
| 107 |
hi := n / digitsGroup // at most 42 |
| 108 |
binary.LittleEndian.PutUint64(buf[8:16], digits8(n-hi*digitsGroup)|digitZeros) |
| 109 |
z := digits8(hi) |
| 110 |
binary.LittleEndian.PutUint64(buf[0:8], z|digitZeros) |
| 111 |
return bits.TrailingZeros64(z) >> 3 |
| 112 |
} |
| 113 |
|
| 114 |
// FormatUint formats a uint64 as a decimal string. |
| 115 |
// It is faster than strconv.FormatUint for most inputs. |
| 116 |
func FormatUint(n uint64) string { |
| 117 |
if n < 100 { |
| 118 |
return smallInts[n] |
| 119 |
} |
| 120 |
var buf [digitsBufLen]byte |
| 121 |
i := uintToBuf(&buf, n) |
| 122 |
return string(buf[i:]) |
| 123 |
} |
| 124 |
|
| 125 |
// FormatInt formats an int64 as a decimal string. |
| 126 |
// It is faster than strconv.FormatInt for most inputs. |
| 127 |
func FormatInt(n int64) string { |
| 128 |
if n >= 0 { |
| 129 |
if n < 100 { |
| 130 |
return smallInts[n] // inline: a FormatUint call would double the cost |
| 131 |
} |
| 132 |
return FormatUint(uint64(n)) |
| 133 |
} |
| 134 |
if n > -100 { |
| 135 |
return smallNegInts[-n] |
| 136 |
} |
| 137 |
var buf [digitsBufLen]byte |
| 138 |
i := uintToBuf(&buf, uint64(-n)) - 1 // uint64(-n) is the magnitude, math.MinInt64 included |
| 139 |
buf[i] = '-' |
| 140 |
return string(buf[i:]) |
| 141 |
} |
| 142 |
|
| 143 |
// FormatUint32 formats a uint32 as a decimal string. |
| 144 |
func FormatUint32(n uint32) string { |
| 145 |
if n < 100 { |
| 146 |
return smallInts[n] |
| 147 |
} |
| 148 |
var buf [16]byte |
| 149 |
i := uint32ToBuf(&buf, n) |
| 150 |
return string(buf[i:]) |
| 151 |
} |
| 152 |
|
| 153 |
// FormatInt32 formats an int32 as a decimal string. |
| 154 |
func FormatInt32(n int32) string { |
| 155 |
if n >= 0 { |
| 156 |
if n < 100 { |
| 157 |
return smallInts[n] |
| 158 |
} |
| 159 |
return FormatUint32(uint32(n)) |
| 160 |
} |
| 161 |
if n > -100 { |
| 162 |
return smallNegInts[-n] |
| 163 |
} |
| 164 |
var buf [16]byte |
| 165 |
i := uint32ToBuf(&buf, uint32(-n)) - 1 |
| 166 |
buf[i] = '-' |
| 167 |
return string(buf[i:]) |
| 168 |
} |
| 169 |
|
| 170 |
// FormatUint16 formats a uint16 as a decimal string. |
| 171 |
func FormatUint16(n uint16) string { |
| 172 |
return FormatUint32(uint32(n)) |
| 173 |
} |
| 174 |
|
| 175 |
// FormatInt16 formats an int16 as a decimal string. |
| 176 |
func FormatInt16(n int16) string { |
| 177 |
return FormatInt32(int32(n)) |
| 178 |
} |
| 179 |
|
| 180 |
// FormatUint8 formats a uint8 as a decimal string. |
| 181 |
func FormatUint8(n uint8) string { |
| 182 |
return uint8Strs[n] |
| 183 |
} |
| 184 |
|
| 185 |
// FormatInt8 formats an int8 as a decimal string. |
| 186 |
func FormatInt8(n int8) string { |
| 187 |
return int8Strs[uint8(n)] |
| 188 |
} |
| 189 |
|
| 190 |
// pow10 holds the powers of ten representable in a uint64. |
| 191 |
var pow10 = [20]uint64{ |
| 192 |
1, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, |
| 193 |
1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19, |
| 194 |
} |
| 195 |
|
| 196 |
// uintDigits returns the number of decimal digits in n. It is loop-free so it |
| 197 |
// inlines into per-element sizing passes. |
| 198 |
func uintDigits(n uint64) int { |
| 199 |
if n < 10 { |
| 200 |
return 1 |
| 201 |
} |
| 202 |
// floor(log10(n)) approximated from floor(log2(n)): 1233/4096 ~= log10(2). |
| 203 |
t := (bits.Len64(n) * 1233) >> 12 |
| 204 |
if n < pow10[t] { |
| 205 |
return t |
| 206 |
} |
| 207 |
return t + 1 |
| 208 |
} |
| 209 |
|
| 210 |
// intDigits returns the length of the decimal string representation of n, |
| 211 |
// including the '-' sign for negative values. |
| 212 |
func intDigits(n int64) int { |
| 213 |
if n < 0 { |
| 214 |
// uint64(-n) yields the absolute value for all negatives via two's |
| 215 |
// complement, including math.MinInt64. |
| 216 |
return 1 + uintDigits(uint64(-n)) |
| 217 |
} |
| 218 |
return uintDigits(uint64(n)) |
| 219 |
} |
| 220 |
|
| 221 |
// AppendUint appends the decimal string representation of n to dst. |
| 222 |
func AppendUint(dst []byte, n uint64) []byte { |
| 223 |
if n < 100 { |
| 224 |
return append(dst, smallInts[n]...) |
| 225 |
} |
| 226 |
var buf [digitsBufLen]byte |
| 227 |
i := uintToBuf(&buf, n) |
| 228 |
return append(dst, buf[i:]...) |
| 229 |
} |
| 230 |
|
| 231 |
// AppendInt appends the decimal string representation of n to dst. |
| 232 |
func AppendInt(dst []byte, n int64) []byte { |
| 233 |
if n >= 0 { |
| 234 |
if n < 100 { |
| 235 |
return append(dst, smallInts[n]...) |
| 236 |
} |
| 237 |
return AppendUint(dst, uint64(n)) |
| 238 |
} |
| 239 |
if n > -100 { |
| 240 |
return append(dst, smallNegInts[-n]...) |
| 241 |
} |
| 242 |
var buf [digitsBufLen]byte |
| 243 |
i := uintToBuf(&buf, uint64(-n)) - 1 |
| 244 |
buf[i] = '-' |
| 245 |
return append(dst, buf[i:]...) |
| 246 |
} |