| 1 | package utils |
| 2 | |
| 3 | import ( |
| 4 | "math/bits" |
| 5 | ) |
| 6 | |
| 7 | // smallInts contains precomputed string representations for small integers 0-99 |
| 8 | var smallInts [100]string |
| 9 | |
| 10 | // smallNegInts contains precomputed string representations for small negative integers -1 to -99 |
| 11 | var smallNegInts [100]string |
| 12 | |
| 13 | // uint8Strs contains precomputed string representations for all uint8 values. |
| 14 | var uint8Strs [256]string |
| 15 | |
| 16 | // int8Strs contains precomputed string representations for all int8 values indexed by uint8(value). |
| 17 | var int8Strs [256]string |
| 18 | |
| 19 | func init() { |
| 20 | for i := range 100 { |
| 21 | smallInts[i] = formatUintSmall(uint64(i)) |
| 22 | if i > 0 { |
| 23 | smallNegInts[i] = "-" + smallInts[i] |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | for i := range 256 { |
| 28 | v := uint8(i) |
| 29 | uint8Strs[i] = formatUint8Slow(v) |
| 30 | |
| 31 | sv := int8(i) |
| 32 | if sv >= 0 { |
| 33 | int8Strs[i] = uint8Strs[sv] |
| 34 | } else { |
| 35 | int8Strs[i] = "-" + uint8Strs[uint8(-sv)] |
| 36 | } |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | func formatUintSmall(n uint64) string { |
| 41 | if n < 10 { |
| 42 | return string(byte(n) + '0') |
| 43 | } |
| 44 | return string([]byte{byte(n/10) + '0', byte(n%10) + '0'}) |
| 45 | } |
| 46 | |
| 47 | func formatUint8Slow(n uint8) string { |
| 48 | if n < 100 { |
| 49 | return smallInts[n] |
| 50 | } |
| 51 | return string([]byte{n/100 + '0', (n/10)%10 + '0', n%10 + '0'}) |
| 52 | } |
| 53 | |
| 54 | // formatUintBuf writes the digits of n into buf from the end and returns the start index. |
| 55 | // buf must be at least 20 bytes. |
| 56 | func formatUintBuf(buf *[20]byte, n uint64) int { |
| 57 | i := 20 |
| 58 | for n >= 10 { |
| 59 | i-- |
| 60 | buf[i] = byte(n%10) + '0' |
| 61 | n /= 10 |
| 62 | } |
| 63 | i-- |
| 64 | buf[i] = byte(n) + '0' |
| 65 | return i |
| 66 | } |
| 67 | |
| 68 | // FormatUint formats a uint64 as a decimal string. |
| 69 | // It is faster than strconv.FormatUint for most inputs. |
| 70 | func FormatUint(n uint64) string { |
| 71 | if n < 100 { |
| 72 | return smallInts[n] |
| 73 | } |
| 74 | var buf [20]byte |
| 75 | i := formatUintBuf(&buf, n) |
| 76 | return string(buf[i:]) |
| 77 | } |
| 78 | |
| 79 | // FormatInt formats an int64 as a decimal string. |
| 80 | // It is faster than strconv.FormatInt for most inputs. |
| 81 | func FormatInt(n int64) string { |
| 82 | if n >= 0 && n < 100 { |
| 83 | return smallInts[n] |
| 84 | } |
| 85 | if n < 0 && n > -100 { |
| 86 | return smallNegInts[-n] |
| 87 | } |
| 88 | if n >= 0 { |
| 89 | var buf [20]byte |
| 90 | i := formatUintBuf(&buf, uint64(n)) |
| 91 | return string(buf[i:]) |
| 92 | } |
| 93 | var buf [20]byte |
| 94 | i := formatUintBuf(&buf, uint64(-n)) |
| 95 | i-- |
| 96 | buf[i] = '-' |
| 97 | return string(buf[i:]) |
| 98 | } |
| 99 | |
| 100 | // FormatUint32 formats a uint32 as a decimal string. |
| 101 | func FormatUint32(n uint32) string { |
| 102 | if n < 100 { |
| 103 | return smallInts[n] |
| 104 | } |
| 105 | var buf [10]byte // max 4294967295 |
| 106 | i := 10 |
| 107 | for n >= 10 { |
| 108 | i-- |
| 109 | buf[i] = byte(n%10) + '0' |
| 110 | n /= 10 |
| 111 | } |
| 112 | i-- |
| 113 | buf[i] = byte(n) + '0' |
| 114 | return string(buf[i:]) |
| 115 | } |
| 116 | |
| 117 | // FormatInt32 formats an int32 as a decimal string. |
| 118 | func FormatInt32(n int32) string { |
| 119 | if n >= 0 && n < 100 { |
| 120 | return smallInts[n] |
| 121 | } |
| 122 | if n < 0 && n > -100 { |
| 123 | return smallNegInts[-n] |
| 124 | } |
| 125 | if n >= 0 { |
| 126 | return FormatUint32(uint32(n)) |
| 127 | } |
| 128 | var buf [11]byte // max -2147483648 |
| 129 | un := uint32(-n) |
| 130 | i := 11 |
| 131 | for un >= 10 { |
| 132 | i-- |
| 133 | buf[i] = byte(un%10) + '0' |
| 134 | un /= 10 |
| 135 | } |
| 136 | i-- |
| 137 | buf[i] = byte(un) + '0' |
| 138 | i-- |
| 139 | buf[i] = '-' |
| 140 | return string(buf[i:]) |
| 141 | } |
| 142 | |
| 143 | // FormatUint16 formats a uint16 as a decimal string. |
| 144 | func FormatUint16(n uint16) string { |
| 145 | if n < 100 { |
| 146 | return smallInts[n] |
| 147 | } |
| 148 | var buf [5]byte // max 65535 |
| 149 | i := 5 |
| 150 | for n >= 10 { |
| 151 | i-- |
| 152 | buf[i] = byte(n%10) + '0' |
| 153 | n /= 10 |
| 154 | } |
| 155 | i-- |
| 156 | buf[i] = byte(n) + '0' |
| 157 | return string(buf[i:]) |
| 158 | } |
| 159 | |
| 160 | // FormatInt16 formats an int16 as a decimal string. |
| 161 | func FormatInt16(n int16) string { |
| 162 | if n >= 0 && n < 100 { |
| 163 | return smallInts[n] |
| 164 | } |
| 165 | if n < 0 && n > -100 { |
| 166 | return smallNegInts[-n] |
| 167 | } |
| 168 | if n >= 0 { |
| 169 | return FormatUint16(uint16(n)) |
| 170 | } |
| 171 | var buf [6]byte // max -32768 |
| 172 | un := uint16(-n) |
| 173 | i := 6 |
| 174 | for un >= 10 { |
| 175 | i-- |
| 176 | buf[i] = byte(un%10) + '0' |
| 177 | un /= 10 |
| 178 | } |
| 179 | i-- |
| 180 | buf[i] = byte(un) + '0' |
| 181 | i-- |
| 182 | buf[i] = '-' |
| 183 | return string(buf[i:]) |
| 184 | } |
| 185 | |
| 186 | // FormatUint8 formats a uint8 as a decimal string. |
| 187 | func FormatUint8(n uint8) string { |
| 188 | return uint8Strs[n] |
| 189 | } |
| 190 | |
| 191 | // FormatInt8 formats an int8 as a decimal string. |
| 192 | func FormatInt8(n int8) string { |
| 193 | return int8Strs[uint8(n)] |
| 194 | } |
| 195 | |
| 196 | // pow10 holds the powers of ten representable in a uint64. |
| 197 | var pow10 = [20]uint64{ |
| 198 | 1, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, |
| 199 | 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19, |
| 200 | } |
| 201 | |
| 202 | // uintDigits returns the number of decimal digits in n. It is loop-free so it |
| 203 | // inlines into per-element sizing passes. |
| 204 | func uintDigits(n uint64) int { |
| 205 | if n < 10 { |
| 206 | return 1 |
| 207 | } |
| 208 | // floor(log10(n)) approximated from floor(log2(n)): 1233/4096 ~= log10(2). |
| 209 | t := (bits.Len64(n) * 1233) >> 12 |
| 210 | if n < pow10[t] { |
| 211 | return t |
| 212 | } |
| 213 | return t + 1 |
| 214 | } |
| 215 | |
| 216 | // intDigits returns the length of the decimal string representation of n, |
| 217 | // including the '-' sign for negative values. |
| 218 | func intDigits(n int64) int { |
| 219 | if n < 0 { |
| 220 | // uint64(-n) yields the absolute value for all negatives via two's |
| 221 | // complement, including math.MinInt64. |
| 222 | return 1 + uintDigits(uint64(-n)) |
| 223 | } |
| 224 | return uintDigits(uint64(n)) |
| 225 | } |
| 226 | |
| 227 | // AppendUint appends the decimal string representation of n to dst. |
| 228 | func AppendUint(dst []byte, n uint64) []byte { |
| 229 | if n < 100 { |
| 230 | return append(dst, smallInts[n]...) |
| 231 | } |
| 232 | var buf [20]byte |
| 233 | i := formatUintBuf(&buf, n) |
| 234 | return append(dst, buf[i:]...) |
| 235 | } |
| 236 | |
| 237 | // AppendInt appends the decimal string representation of n to dst. |
| 238 | func AppendInt(dst []byte, n int64) []byte { |
| 239 | if n >= 0 { |
| 240 | return AppendUint(dst, uint64(n)) |
| 241 | } |
| 242 | if n > -100 { |
| 243 | return append(dst, smallNegInts[-n]...) |
| 244 | } |
| 245 | var buf [20]byte |
| 246 | i := formatUintBuf(&buf, uint64(-n)) |
| 247 | i-- |
| 248 | buf[i] = '-' |
| 249 | return append(dst, buf[i:]...) |
| 250 | } |