Source file
src/strconv/itoa.go
1
2
3
4
5 package strconv
6
7 import "math/bits"
8
9 const fastSmalls = true
10
11
12
13
14 func FormatUint(i uint64, base int) string {
15 if fastSmalls && i < nSmalls && base == 10 {
16 return small(int(i))
17 }
18 _, s := formatBits(nil, i, base, false, false)
19 return s
20 }
21
22
23
24
25 func FormatInt(i int64, base int) string {
26 if fastSmalls && 0 <= i && i < nSmalls && base == 10 {
27 return small(int(i))
28 }
29 _, s := formatBits(nil, uint64(i), base, i < 0, false)
30 return s
31 }
32
33
34 func Itoa(i int) string {
35 return FormatInt(int64(i), 10)
36 }
37
38
39
40 func AppendInt(dst []byte, i int64, base int) []byte {
41 if fastSmalls && 0 <= i && i < nSmalls && base == 10 {
42 return append(dst, small(int(i))...)
43 }
44 dst, _ = formatBits(dst, uint64(i), base, i < 0, true)
45 return dst
46 }
47
48
49
50 func AppendUint(dst []byte, i uint64, base int) []byte {
51 if fastSmalls && i < nSmalls && base == 10 {
52 return append(dst, small(int(i))...)
53 }
54 dst, _ = formatBits(dst, i, base, false, true)
55 return dst
56 }
57
58
59 func small(i int) string {
60 if i < 10 {
61 return digits[i : i+1]
62 }
63 return smallsString[i*2 : i*2+2]
64 }
65
66 const nSmalls = 100
67
68 const smallsString = "00010203040506070809" +
69 "10111213141516171819" +
70 "20212223242526272829" +
71 "30313233343536373839" +
72 "40414243444546474849" +
73 "50515253545556575859" +
74 "60616263646566676869" +
75 "70717273747576777879" +
76 "80818283848586878889" +
77 "90919293949596979899"
78
79 const host32bit = ^uint(0)>>32 == 0
80
81 const digits = "0123456789abcdefghijklmnopqrstuvwxyz"
82
83
84
85
86
87
88 func formatBits(dst []byte, u uint64, base int, neg, append_ bool) (d []byte, s string) {
89 if base < 2 || base > len(digits) {
90 panic("strconv: illegal AppendInt/FormatInt base")
91 }
92
93
94 var a [64 + 1]byte
95 i := len(a)
96
97 if neg {
98 u = -u
99 }
100
101
102
103
104 if base == 10 {
105
106
107
108 if host32bit {
109
110 for u >= 1e9 {
111
112
113
114 q := u / 1e9
115 us := uint(u - q*1e9)
116 for j := 4; j > 0; j-- {
117 is := us % 100 * 2
118 us /= 100
119 i -= 2
120 a[i+1] = smallsString[is+1]
121 a[i+0] = smallsString[is+0]
122 }
123
124
125
126 i--
127 a[i] = smallsString[us*2+1]
128
129 u = q
130 }
131
132 }
133
134
135 us := uint(u)
136 for us >= 100 {
137 is := us % 100 * 2
138 us /= 100
139 i -= 2
140 a[i+1] = smallsString[is+1]
141 a[i+0] = smallsString[is+0]
142 }
143
144
145 is := us * 2
146 i--
147 a[i] = smallsString[is+1]
148 if us >= 10 {
149 i--
150 a[i] = smallsString[is]
151 }
152
153 } else if isPowerOfTwo(base) {
154
155
156
157
158
159
160
161 shift := uint(bits.TrailingZeros(uint(base))) & 7
162 b := uint64(base)
163 m := uint(base) - 1
164 for u >= b {
165 i--
166 a[i] = digits[uint(u)&m]
167 u >>= shift
168 }
169
170 i--
171 a[i] = digits[uint(u)]
172 } else {
173
174 b := uint64(base)
175 for u >= b {
176 i--
177
178
179
180 q := u / b
181 a[i] = digits[uint(u-q*b)]
182 u = q
183 }
184
185 i--
186 a[i] = digits[uint(u)]
187 }
188
189
190 if neg {
191 i--
192 a[i] = '-'
193 }
194
195 if append_ {
196 d = append(dst, a[i:]...)
197 return
198 }
199 s = string(a[i:])
200 return
201 }
202
203 func isPowerOfTwo(x int) bool {
204 return x&(x-1) == 0
205 }
206
View as plain text