-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathatoi.go
305 lines (281 loc) · 6.31 KB
/
atoi.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
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
package fcnv
import (
"strconv"
)
func sign(i string, o *string) (s int) {
s = 1
if len(i) > 0 {
switch i[0] {
case '-':
s = -1
fallthrough
case '+':
*o = i[1:]
default:
*o = i
}
}
return
}
func isdigit(b byte) bool {
return b^0x30 < 0xA
}
// atoi8 returns the result of ParseInt(s, 10, 8) converted to type int8.
func atoi8(s string) (ret int8, err error) {
const (
fnName = "Atoi8"
cutoff = 1 << (8 - 1)
limit = 4 // int8 = [-128, 127]
)
s0 := s
mp := sign(s, &s)
var buf uint
for i := range s {
if i >= limit {
goto syntaxError
}
if isdigit(s[i]) {
buf = buf*10 + uint(s[i]^0x30)
} else {
goto syntaxError
}
}
if buf < cutoff || mp < 0 && buf <= cutoff {
return int8(buf) * int8(mp), nil
} else {
goto syntaxError
}
syntaxError:
return 0, &strconv.NumError{Func: fnName, Num: s0, Err: strconv.ErrSyntax}
}
// atoi16 returns the result of ParseInt(s, 10, 16) converted to type int16.
func atoi16(s string) (ret int16, err error) {
const (
fnName = "Atoi16"
cutoff = 1 << (16 - 1)
limit = 5 // int16 = [-32768, 32767]
)
s0 := s
mp := sign(s, &s)
var buf uint
for i := range s {
if i > limit {
goto syntaxError
}
if isdigit(s[i]) {
buf = buf*10 + uint(s[i]^0x30)
} else {
goto syntaxError
}
}
if buf < cutoff || mp < 0 && buf <= cutoff {
return int16(buf) * int16(mp), nil
} else {
goto syntaxError
}
syntaxError:
return 0, &strconv.NumError{Func: fnName, Num: s0, Err: strconv.ErrSyntax}
}
// atoi32 returns the result of ParseInt(s, 10, 32) converted to type int32.
func atoi32(s string) (ret int32, err error) {
const (
fnName = "Atoi32"
cutoff = 1 << (32 - 1)
limit = 10 // int32 = [-2147483648, 2147483647]
)
s0 := s
mp := sign(s, &s)
var buf uint
for i := range s {
if i > limit {
goto syntaxError
}
if isdigit(s[i]) {
buf = buf*10 + uint(s[i]^0x30)
} else {
goto syntaxError
}
}
if buf < cutoff || mp < 0 && buf <= cutoff {
return int32(buf) * int32(mp), nil
} else {
goto syntaxError
}
syntaxError:
return 0, &strconv.NumError{Func: fnName, Num: s0, Err: strconv.ErrSyntax}
}
// atoi64 returns the result of ParseInt(s, 10, 64) converted to type int64.
func atoi64(s string) (ret int64, err error) {
const (
fnName = "Atoi64"
cutoff = 1 << (64 - 1)
limit = 19 // int64 = [-9223372036854775808, 9223372036854775807]
)
s0 := s
mp := sign(s, &s)
var buf uint
for i := range s {
if i > limit {
goto syntaxError
}
if isdigit(s[i]) {
buf = buf*10 + uint(s[i]^0x30)
} else {
goto syntaxError
}
}
if buf < cutoff || mp < 0 && buf <= cutoff {
return int64(buf) * int64(mp), nil
} else {
goto syntaxError
}
syntaxError:
return 0, &strconv.NumError{Func: fnName, Num: s0, Err: strconv.ErrSyntax}
}
// atoui returns the result of ParseUint(s, 10, 0) converted to type uint.
func atoui(s string) (ret uint, err error) {
const (
fnName = "Atoui"
// uint32 = [0, 4294967295]
// uint64 = [0, 18446744073709551615]
limit = 10 * strconv.IntSize / 32
)
// Fast path for small integers that fit int type.
if sLen := len(s); 0 < sLen && sLen < limit {
s0 := s
if sign(s, &s) < 0 {
return 0, &strconv.NumError{Func: fnName, Num: s0, Err: strconv.ErrSyntax}
}
for _, ch := range []byte(s) {
ch ^= '0'
if ch > 9 {
return 0, &strconv.NumError{Func: fnName, Num: s0, Err: strconv.ErrSyntax}
}
ret = ret*10 + uint(ch)
}
return ret, nil
}
// Slow path for invalid or big integers.
ui64, err := strconv.ParseUint(s, 10, 0)
if nerr, ok := err.(*strconv.NumError); ok {
nerr.Func = fnName
ui64 = 0
}
return uint(ui64), err
}
// atoui8 returns the result of ParseUint(s, 10, 8) converted to type uint8.
func atoui8(s string) (ret uint8, err error) {
const (
fnName = "Atoui8"
cutoff = 1<<8 - 1
limit = 3 // uint8 = [0, 255]
)
s0 := s
if sign(s, &s) < 0 {
return 0, &strconv.NumError{Func: fnName, Num: s0, Err: strconv.ErrSyntax}
}
buf := uint(0)
for i := range s {
if i >= limit {
goto syntaxError
}
if isdigit(s[i]) {
buf = buf*10 + uint(s[i]^0x30)
} else {
goto syntaxError
}
}
if buf > cutoff {
goto syntaxError
}
return uint8(buf), nil
syntaxError:
return 0, &strconv.NumError{Func: fnName, Num: s0, Err: strconv.ErrSyntax}
}
// atoui16 returns the result of ParseUint(s, 10, 16) converted to type uint16.
func atoui16(s string) (ret uint16, err error) {
const (
fnName = "Atoui16"
cutoff = 1<<16 - 1
limit = 5 // uint16 = [0, 65535]
)
s0 := s
if sign(s, &s) < 0 {
return 0, &strconv.NumError{Func: fnName, Num: s0, Err: strconv.ErrSyntax}
}
buf := uint(0)
for i := range s {
if i >= limit {
goto syntaxError
}
if isdigit(s[i]) {
buf = buf*10 + uint(s[i]^0x30)
} else {
goto syntaxError
}
}
if buf > cutoff {
goto syntaxError
}
return uint16(buf), nil
syntaxError:
return 0, &strconv.NumError{Func: fnName, Num: s0, Err: strconv.ErrSyntax}
}
// atoui32 returns the result of ParseUint(s, 10, 32) converted to type uint32.
func atoui32(s string) (ret uint32, err error) {
const (
fnName = "Atoui32"
cutoff = uint64(1<<32 - 1)
limit = 10 // uint32 = [0, 4294967295]
)
s0 := s
if sign(s, &s) < 0 {
return 0, &strconv.NumError{Func: fnName, Num: s0, Err: strconv.ErrSyntax}
}
buf := uint64(0)
for i := range s {
if i >= limit {
goto syntaxError
}
if isdigit(s[i]) {
buf = buf*10 + uint64(s[i]^0x30)
} else {
goto syntaxError
}
}
if buf > cutoff {
goto syntaxError
}
return uint32(buf), nil
syntaxError:
return 0, &strconv.NumError{Func: fnName, Num: s0, Err: strconv.ErrSyntax}
}
// atoui64 returns the result of ParseUint(s, 10, 64) converted to type uint64.
func atoui64(s string) (ret uint64, err error) {
const (
fnName = "Atoui64"
limit = 20 // uint64 = [0, 18446744073709551615]
)
// Fast path for small integers that fit int type.
if sLen := len(s); 0 < sLen && sLen < limit {
s0 := s
if sign(s, &s) < 0 {
return 0, &strconv.NumError{Func: fnName, Num: s0, Err: strconv.ErrSyntax}
}
for _, ch := range []byte(s) {
ch ^= '0'
if ch > 9 {
return 0, &strconv.NumError{Func: fnName, Num: s0, Err: strconv.ErrSyntax}
}
ret = ret*10 + uint64(ch)
}
return ret, nil
}
// Slow path for invalid or big integers.
ui64, err := strconv.ParseUint(s, 10, 0)
if nerr, ok := err.(*strconv.NumError); ok {
nerr.Func = fnName
ui64 = 0
}
return ui64, err
}