Repository navigation
Expand file tree
/
Copy pathformat.go
More file actions
478 lines (439 loc) · 18.2 KB
/
Copy pathformat.go
File metadata and controls
478 lines (439 loc) · 18.2 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
package utils
import (
"encoding/binary"
"math/bits"
)
// smallInts contains precomputed string representations for small integers 0-99
var smallInts [100]string
// smallSigned has the strings of -99 to 999, that of n at index n+smallBias: one unsigned
// compare of that index tells whether n is in the table, whatever its sign. From 100 on they
// are parts of digits3.
var smallSigned [smallBias + 1000]string
const smallBias = 99
// uint8Strs contains precomputed string representations for all uint8 values.
var uint8Strs [256]string
// int8Strs contains precomputed string representations for all int8 values indexed by uint8(value).
var int8Strs [256]string
func init() {
for i := range 100 {
smallInts[i] = formatUintSmall(uint64(i))
smallSigned[smallBias+i] = smallInts[i]
if i > 0 {
smallSigned[smallBias-i] = "-" + smallInts[i]
}
}
for i := 100; i < 1000; i++ {
smallSigned[smallBias+i] = digits3[3*i : 3*i+3]
}
for i := range 256 {
v := uint8(i)
uint8Strs[i] = formatUint8Slow(v)
sv := int8(i)
if sv >= 0 {
int8Strs[i] = uint8Strs[sv]
} else {
int8Strs[i] = "-" + uint8Strs[uint8(-sv)]
}
}
}
func formatUintSmall(n uint64) string {
if n < 10 {
return string(byte(n) + '0')
}
return string([]byte{byte(n/10) + '0', byte(n%10) + '0'})
}
func formatUint8Slow(n uint8) string {
if n < 100 {
return smallInts[n]
}
return string([]byte{n/100 + '0', (n/10)%10 + '0', n%10 + '0'})
}
// digits3 holds "000".."999" back to back, a hundred to a line: digits3[3*n:3*n+3] is n
// zero-padded, and for n >= 100 its decimal string as it is, a substring that takes no allocation.
const digits3 = "" +
"000001002003004005006007008009010011012013014015016017018019020021022023024025026027028029030031032033034035036037038039040041042043044045046047048049050051052053054055056057058059060061062063064065066067068069070071072073074075076077078079080081082083084085086087088089090091092093094095096097098099" +
"100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199" +
"200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299" +
"300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399" +
"400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499" +
"500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599" +
"600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699" +
"700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799" +
"800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899" +
"900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999"
// decimalPairs holds "00".."99" back to back: decimalPairs[2*n:2*n+2] is n zero-padded.
const decimalPairs = "00010203040506070809101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899"
// Formatting works on 8-digit groups: digits8 turns a value below 1e8 into
// eight zero-padded digit lanes (most significant first) with lane-parallel
// divisions, and leading zeros are zero lanes found by a trailing-zero count.
const (
digitZeros = 0x3030303030303030 // '0' in every lane; digit lanes are below 16, so OR equals add
digitsBufLen = 24 // three lane groups; 20 digits plus a sign fit
digitsGroup = 100000000 // value of one lane group
)
// digits8 returns the eight zero-padded digits of n < 1e8 as 0..9 byte lanes.
// Each multiply is a per-lane division by 100 or 10; no step carries across lanes.
func digits8(n uint32) uint64 {
hi := n / 10000 // two 4-digit halves in 32-bit lanes, high half in the low lane
x := uint64(hi) | uint64(n-hi*10000)<<32
q := (x * 5243 >> 19) & 0x0000007F0000007F // lane/100, exact below 43699
y := q | (x-q*100)<<16 // lane%100 into the upper 16 bits
q = (y * 103 >> 10) & 0x000F000F000F000F // lane/10, exact below 100
return q | (y-q*10)<<8 // lane%10 into the upper byte
}
// digitsBuf is what uintToBuf fills, on a word boundary: its lane groups are stored and copied
// out as words, and a byte array on its own may be laid out off one, next to the byte-sized
// locals of its function, which costs AppendInt 7% (benchstat, Go 1.26, linux/amd64).
type digitsBuf struct {
_ [0]uint64
b [digitsBufLen]byte
}
// digitsBuf32 is digitsBuf for uint32ToBuf.
type digitsBuf32 struct {
_ [0]uint64
b [16]byte
}
// uintToBuf writes the digits of n >= 100 right-aligned into buf and returns
// the index of the first digit.
func uintToBuf(buf *[digitsBufLen]byte, n uint64) int {
if n < digitsGroup {
z := digits8(uint32(n))
binary.LittleEndian.PutUint64(buf[16:24], z|digitZeros)
return 16 + bits.TrailingZeros64(z)>>3
}
hi := n / digitsGroup
binary.LittleEndian.PutUint64(buf[16:24], digits8(uint32(n-hi*digitsGroup))|digitZeros)
if hi < digitsGroup {
z := digits8(uint32(hi))
binary.LittleEndian.PutUint64(buf[8:16], z|digitZeros)
return 8 + bits.TrailingZeros64(z)>>3
}
top := uint32(hi / digitsGroup) // at most 1844
binary.LittleEndian.PutUint64(buf[8:16], digits8(uint32(hi-uint64(top)*digitsGroup))|digitZeros)
z := digits8(top)
binary.LittleEndian.PutUint64(buf[0:8], z|digitZeros)
return bits.TrailingZeros64(z) >> 3
}
// uint32ToBuf is uintToBuf for a 32-bit value of nine or ten digits, which are two groups: its
// callers write a single group themselves.
func uint32ToBuf(buf *[16]byte, n uint32) int {
hi := n / digitsGroup // 1 to 42
binary.LittleEndian.PutUint64(buf[8:16], digits8(n-hi*digitsGroup)|digitZeros)
z := digits8(hi)
binary.LittleEndian.PutUint64(buf[0:8], z|digitZeros)
return bits.TrailingZeros64(z) >> 3
}
// FormatUint formats a uint64 as a decimal string.
// It is faster than strconv.FormatUint for most inputs, and allocates nothing below 1000: that
// result is shared by every call and, from 100 on, part of the program's read-only data, so it
// must not be written through an unsafe view such as UnsafeBytes gives.
//
// The test for the values below 1000 is all there is to the function itself, which is small
// enough to be inlined: such a value costs its caller a compare and a load, and no call.
func FormatUint(n uint64) string {
if n < 1000 {
return smallSigned[n+smallBias]
}
return formatUintRest(n)
}
// formatUintRest is FormatUint from 1000 on. It is small enough to be inlined into FormatUint
// itself, which would then be too large to be inlined into its callers: it stays a call.
//
// Up to eight digits are one lane group, written here into a word of its own: a call of
// uintToBuf would test for the group again and take a buffer of three.
//
//go:noinline
func formatUintRest(n uint64) string {
if n < digitsGroup {
var b [8]byte
z := digits8(uint32(n))
binary.LittleEndian.PutUint64(b[:], z|digitZeros)
return string(b[bits.TrailingZeros64(z)>>3:])
}
var buf digitsBuf
i := uintToBuf(&buf.b, n)
return string(buf.b[i:])
}
// FormatInt formats an int64 as a decimal string.
// It is faster than strconv.FormatInt for most inputs, and allocates nothing from -99 to 999:
// that result is shared, as FormatUint's is, and must not be written either. It is inlined as
// FormatUint is, with one compare for all of those values.
func FormatInt(n int64) string {
if i := uint64(n) + smallBias; i < uint64(len(smallSigned)) {
return smallSigned[i]
}
return formatIntRest(n)
}
// formatIntRest is FormatInt below -99 and from 1000 on, with one lane group written here as
// formatUintRest writes it, and a sign in front of it for a negative n.
func formatIntRest(n int64) string {
if n >= 0 {
if n < digitsGroup {
var b [8]byte
z := digits8(uint32(n))
binary.LittleEndian.PutUint64(b[:], z|digitZeros)
return string(b[bits.TrailingZeros64(z)>>3:])
}
var buf digitsBuf
i := uintToBuf(&buf.b, uint64(n))
return string(buf.b[i:])
}
if n > -digitsGroup {
var b digitsBuf32
z := digits8(uint32(-n))
binary.LittleEndian.PutUint64(b.b[8:], z|digitZeros)
i := 7 + bits.TrailingZeros64(z)>>3
b.b[i] = '-'
return string(b.b[i:])
}
var buf digitsBuf
i := uintToBuf(&buf.b, uint64(-n)) - 1 // uint64(-n) is the magnitude, math.MinInt64 included
buf.b[i] = '-'
return string(buf.b[i:])
}
// FormatUint32 formats a uint32 as a decimal string, with FormatUint's shared results below 1000.
func FormatUint32(n uint32) string {
if n < 1000 {
return smallSigned[n+smallBias]
}
return formatUint32Rest(n)
}
// formatUint32Rest is FormatUint32 from 1000 on, and a call as formatUintRest is.
//
//go:noinline
func formatUint32Rest(n uint32) string {
if n < digitsGroup {
var b [8]byte
z := digits8(n)
binary.LittleEndian.PutUint64(b[:], z|digitZeros)
return string(b[bits.TrailingZeros64(z)>>3:])
}
var buf digitsBuf32
i := uint32ToBuf(&buf.b, n)
return string(buf.b[i:])
}
// FormatInt32 formats an int32 as a decimal string, with FormatInt's shared results from -99 to 999.
func FormatInt32(n int32) string {
if i := uint32(n) + smallBias; i < uint32(len(smallSigned)) {
return smallSigned[i]
}
return formatInt32Rest(n)
}
// formatInt32Rest is FormatInt32 below -99 and from 1000 on.
func formatInt32Rest(n int32) string {
if n >= 0 {
if n < digitsGroup {
var b [8]byte
z := digits8(uint32(n))
binary.LittleEndian.PutUint64(b[:], z|digitZeros)
return string(b[bits.TrailingZeros64(z)>>3:])
}
var buf digitsBuf32
i := uint32ToBuf(&buf.b, uint32(n))
return string(buf.b[i:])
}
var buf digitsBuf32
if n > -digitsGroup {
z := digits8(uint32(-n))
binary.LittleEndian.PutUint64(buf.b[8:], z|digitZeros)
i := 7 + bits.TrailingZeros64(z)>>3
buf.b[i] = '-'
return string(buf.b[i:])
}
i := uint32ToBuf(&buf.b, uint32(-n)) - 1
buf.b[i] = '-'
return string(buf.b[i:])
}
// FormatUint16 formats a uint16 as a decimal string.
func FormatUint16(n uint16) string {
return FormatUint32(uint32(n))
}
// FormatInt16 formats an int16 as a decimal string.
func FormatInt16(n int16) string {
if i := uint32(n) + smallBias; i < uint32(len(smallSigned)) {
return smallSigned[i]
}
return formatInt32Rest(int32(n))
}
// FormatUint8 formats a uint8 as a decimal string.
func FormatUint8(n uint8) string {
return uint8Strs[n]
}
// FormatInt8 formats an int8 as a decimal string.
func FormatInt8(n int8) string {
return int8Strs[uint8(n)]
}
// pow10 holds the powers of ten representable in a uint64.
var pow10 = [20]uint64{
1, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9,
1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19,
}
// uintDigits returns the number of decimal digits in n. It is loop-free so it
// inlines into per-element sizing passes.
func uintDigits(n uint64) int {
if n < 10 {
return 1
}
// floor(log10(n)) approximated from floor(log2(n)): 1233/4096 ~= log10(2).
t := (bits.Len64(n) * 1233) >> 12
if n < pow10[t] {
return t
}
return t + 1
}
// intDigits returns the length of the decimal string representation of n,
// including the '-' sign for negative values.
func intDigits(n int64) int {
if n < 0 {
// uint64(-n) yields the absolute value for all negatives via two's
// complement, including math.MinInt64.
return 1 + uintDigits(uint64(-n))
}
return uintDigits(uint64(n))
}
// AppendUint appends the decimal string representation of n to dst.
func AppendUint(dst []byte, n uint64) []byte {
if n < 100 {
if n < 10 {
return append(dst, byte(n)+'0')
}
return append(dst, decimalPairs[2*n], decimalPairs[2*n+1])
}
if n < 10000 {
// Three or four digits, as a status code, a port or a year has: from the tables.
if n < 1000 {
d := digits3[3*n : 3*n+3]
return append(dst, d[0], d[1], d[2])
}
hi := n / 100
lo := n - hi*100
return append(dst, decimalPairs[2*hi], decimalPairs[2*hi+1], decimalPairs[2*lo], decimalPairs[2*lo+1])
}
return appendUintLong(dst, n)
}
// AppendInt appends the decimal string representation of n to dst.
func AppendInt(dst []byte, n int64) []byte {
if n >= 0 {
// AppendUint's tests once more, and not a call of it: a status code or a port is not
// worth one.
u := uint64(n)
if u < 100 {
if u < 10 {
return append(dst, byte(u)+'0')
}
return append(dst, decimalPairs[2*u], decimalPairs[2*u+1])
}
if u < 10000 {
if u < 1000 {
d := digits3[3*u : 3*u+3]
return append(dst, d[0], d[1], d[2])
}
hi := u / 100
lo := u - hi*100
return append(dst, decimalPairs[2*hi], decimalPairs[2*hi+1], decimalPairs[2*lo], decimalPairs[2*lo+1])
}
return appendUintLong(dst, u)
}
u := uint64(-n) // the magnitude, math.MinInt64 included
if u < 100 {
if u < 10 {
return append(dst, '-', byte(u)+'0')
}
return append(dst, '-', decimalPairs[2*u], decimalPairs[2*u+1])
}
if u < 10000 {
if u < 1000 {
d := digits3[3*u : 3*u+3]
return append(dst, '-', d[0], d[1], d[2])
}
hi := u / 100
lo := u - hi*100
return append(dst, '-', decimalPairs[2*hi], decimalPairs[2*hi+1], decimalPairs[2*lo], decimalPairs[2*lo+1])
}
l := len(dst)
if l >= cap(dst) {
// No room for the sign: appended on its own it would grow dst, to as little as 8 bytes,
// and the digits behind it would grow it a second time. The test is written as >= for
// the compiler, which then knows that the sign fits and appends it with no test of its
// own: a dst with room pays nothing for this one.
return appendIntGrow(dst, u)
}
// The sign's byte is reserved, not written, until it is known which array holds the result:
// a dst that has to grow keeps its spare capacity as it was.
dst = appendUintLong(dst[:l+1], u)
dst[l] = '-'
return dst
}
// appendUintLong is AppendUint from 10000 on, five digits and more. Where dst has room for them
// they are stored into it as the lane groups they come in, two or three words that overlap
// and that lie inside the result: no byte behind it is written, and nothing goes through a
// buffer and a copy of a length that is only known then. Where dst has to grow, appendUintGrow
// does that.
func appendUintLong(dst []byte, n uint64) []byte {
l := len(dst)
if n < digitsGroup {
// Five to eight digits: one group, shifted to start at its first digit, as two 4-byte
// windows.
z := digits8(uint32(n))
skip := uint(bits.TrailingZeros64(z)) >> 3 & 3 // the zero lanes in front: up to three
end := l + 8 - int(skip)
if uint(end) > uint(cap(dst)) {
return appendUintGrow(dst, n)
}
dst = dst[:end]
z = (z | digitZeros) >> (8 * skip)
binary.LittleEndian.PutUint32(dst[l:l+4:l+4], uint32(z))
binary.LittleEndian.PutUint32(dst[end-4:end:end], uint32(z>>(8*(4-skip))))
return dst
}
hi := n / digitsGroup
low := digits8(uint32(n-hi*digitsGroup)) | digitZeros
if hi < digitsGroup {
// Nine to sixteen digits: the digits of the high group and the first of the low one
// are the first word, and the low group is the last.
z := digits8(uint32(hi))
skip := uint(bits.TrailingZeros64(z)) >> 3 & 7
end := l + 16 - int(skip)
if uint(end) > uint(cap(dst)) {
return appendUintGrow(dst, n)
}
dst = dst[:end]
binary.LittleEndian.PutUint64(dst[l:l+8:l+8], (z|digitZeros)>>(8*skip)|low<<8<<(56-8*skip))
binary.LittleEndian.PutUint64(dst[end-8:end:end], low)
return dst
}
// Seventeen to twenty digits: a top group of one to four digits in front of two whole ones.
top := hi / digitsGroup // at most 1844
mid := digits8(uint32(hi-top*digitsGroup)) | digitZeros
z := digits8(uint32(top))
skip := uint(bits.TrailingZeros64(z)) >> 3 & 7
end := l + 24 - int(skip)
if uint(end) > uint(cap(dst)) {
return appendUintGrow(dst, n)
}
dst = dst[:end]
binary.LittleEndian.PutUint64(dst[l:l+8:l+8], (z|digitZeros)>>(8*skip)|mid<<8<<(56-8*skip))
binary.LittleEndian.PutUint64(dst[end-16:end-8:end-8], mid)
binary.LittleEndian.PutUint64(dst[end-8:end:end], low)
return dst
}
// appendUintGrow is appendUintLong for a dst without room: the digits go through a buffer, and
// append makes the room.
//
//go:noinline
func appendUintGrow(dst []byte, n uint64) []byte {
var buf digitsBuf
i := uintToBuf(&buf.b, n)
return append(dst, buf.b[i:]...)
}
// appendIntGrow is AppendInt for a negative value of five digits and more, of the magnitude n,
// and a dst that is full: the sign and the digits go through the buffer together, and one
// append makes the room for both.
//
//go:noinline
func appendIntGrow(dst []byte, n uint64) []byte {
var buf digitsBuf
i := uintToBuf(&buf.b, n) - 1
buf.b[i] = '-'
return append(dst, buf.b[i:]...)
}