-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathitoa.go
65 lines (56 loc) · 1.35 KB
/
itoa.go
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
package decimal
const smallsString = "00010203040506070809" +
"10111213141516171819" +
"20212223242526272829" +
"30313233343536373839" +
"40414243444546474849" +
"50515253545556575859" +
"60616263646566676869" +
"70717273747576777879" +
"80818283848586878889" +
"90919293949596979899"
const host32bit = ^uint(0)>>32 == 0
// Adapted from standard library strconv/itoa.go.
func formatBits10(buf []byte, u uint64, w int) []byte {
// Probably only needs 17, but let's play it safe.
var a [32]byte
i := len(a)
if host32bit {
// convert the lower digits using 32bit operations
for u >= 1e9 {
// Avoid using r = a%b in addition to q = a/b
// since 64bit division and modulo operations
// are calculated by runtime functions on 32bit machines.
q := u / 1e9
us := uint(u - q*1e9) // u % 1e9 fits into a uint
for j := 4; j > 0; j-- {
is := us % 100 * 2
us /= 100
i -= 2
w -= 2
a[i+1] = smallsString[is+1]
a[i+0] = smallsString[is+0]
}
// us < 10, since it contains the last digit
// from the initial 9-digit us.
i--
w--
a[i] = smallsString[us*2+1]
u = q
}
// u < 1e9
}
// u guaranteed to fit into a uint
us := uint(u)
for ; w > 0; w -= 2 {
is := us % 100 * 2
us /= 100
i -= 2
a[i+1] = smallsString[is+1]
a[i+0] = smallsString[is+0]
}
if w < 0 {
i++
}
return append(buf, a[i:]...)
}