-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathread_nums.go
299 lines (269 loc) · 5.91 KB
/
read_nums.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
package dsvreader
import (
"errors"
"math"
"strconv"
)
var (
errOutOfRange = errors.New("out of range")
errInvalidSyntex = errors.New("invalid syntax")
)
// Int returns the next int column value from the current row.
func (tr *Reader) Int() int {
if tr.err != nil {
return 0
}
b, err := tr.nextCol()
if err != nil {
tr.setColError("cannot read `int`", err)
return 0
}
n, err := strconv.Atoi(b2s(b))
if err != nil {
tr.setColError("cannot parse `int`", err)
return 0
}
return n
}
// Uint returns the next uint column value from the current row.
func (tr *Reader) Uint() uint {
if tr.err != nil {
return 0
}
b, err := tr.nextCol()
if err != nil {
tr.setColError("cannot read `uint`", err)
return 0
}
s := b2s(b)
// Fast path - attempt to use Atoi
n, err := strconv.Atoi(s)
if err == nil && n >= 0 {
return uint(n)
}
// Slow path - use ParseUint
nu, err := strconv.ParseUint(s, 10, strconv.IntSize)
if err != nil {
tr.setColError("cannot parse `uint`", err)
return 0
}
return uint(nu)
}
// Int32 returns the next int32 column value from the current row.
func (tr *Reader) Int32() int32 {
if tr.err != nil {
return 0
}
b, err := tr.nextCol()
if err != nil {
tr.setColError("cannot read `int32`", err)
return 0
}
s := b2s(b)
// Fast path - attempt to use Atoi
n, err := strconv.Atoi(s)
if err == nil && n >= math.MinInt32 && n <= math.MaxInt32 {
return int32(n)
}
// Slow path - use ParseInt
n32, err := strconv.ParseInt(s, 10, 32)
if err != nil {
tr.setColError("cannot parse `int32`", err)
return 0
}
return int32(n32)
}
// Uint32 returns the next uint32 column value from the current row.
func (tr *Reader) Uint32() uint32 {
if tr.err != nil {
return 0
}
b, err := tr.nextCol()
if err != nil {
tr.setColError("cannot read `uint32`", err)
return 0
}
s := b2s(b)
// Fast path - attempt to use Atoi
n, err := strconv.Atoi(s)
if err == nil && n >= 0 && n <= math.MaxUint32 {
return uint32(n)
}
// Slow path - use ParseUint
n32, err := strconv.ParseUint(s, 10, 32)
if err != nil {
tr.setColError("cannot parse `uint32`", err)
return 0
}
return uint32(n32)
}
// Int16 returns the next int16 column value from the current row.
func (tr *Reader) Int16() int16 {
if tr.err != nil {
return 0
}
b, err := tr.nextCol()
if err != nil {
tr.setColError("cannot read `int16`", err)
return 0
}
n, err := strconv.Atoi(b2s(b))
switch {
case err != nil:
tr.setColError("cannot parse `int16`", err)
case n < math.MinInt16 || n > math.MaxInt16:
tr.setColError("cannot parse `int16`", errOutOfRange)
default:
return int16(n)
}
return 0
}
// Uint16 returns the next uint16 column value from the current row.
func (tr *Reader) Uint16() uint16 {
if tr.err != nil {
return 0
}
b, err := tr.nextCol()
if err != nil {
tr.setColError("cannot read `uint16`", err)
return 0
}
n, err := strconv.Atoi(b2s(b))
switch {
case err != nil:
tr.setColError("cannot parse `uint16`", err)
case n < 0:
tr.setColError("cannot parse `uint16`", errInvalidSyntex)
case n > math.MaxUint16:
tr.setColError("cannot parse `uint16`", errOutOfRange)
default:
return uint16(n)
}
return 0
}
// Int8 returns the next int8 column value from the current row.
func (tr *Reader) Int8() int8 {
if tr.err != nil {
return 0
}
b, err := tr.nextCol()
if err != nil {
tr.setColError("cannot read `int8`", err)
return 0
}
n, err := strconv.Atoi(b2s(b))
switch {
case err != nil:
tr.setColError("cannot parse `int8`", err)
case n < math.MinInt8 || n > math.MaxInt8:
tr.setColError("cannot parse `int8`", errOutOfRange)
default:
return int8(n)
}
return 0
}
// Uint8 returns the next uint8 column value from the current row.
func (tr *Reader) Uint8() uint8 {
if tr.err != nil {
return 0
}
b, err := tr.nextCol()
if err != nil {
tr.setColError("cannot read `uint8`", err)
return 0
}
n, err := strconv.Atoi(b2s(b))
switch {
case err != nil:
tr.setColError("cannot parse `uint8`", err)
case n < 0:
tr.setColError("cannot parse `uint8`", errInvalidSyntex)
case n > math.MaxUint8:
tr.setColError("cannot parse `uint8`", errOutOfRange)
default:
return uint8(n)
}
return 0
}
// Int64 returns the next int64 column value from the current row.
func (tr *Reader) Int64() int64 {
if tr.err != nil {
return 0
}
b, err := tr.nextCol()
if err != nil {
tr.setColError("cannot read `int64`", err)
return 0
}
s := b2s(b)
// Fast path - attempt to use Atoi
n, err := strconv.Atoi(s)
if err == nil {
return int64(n)
}
// Slow path - use ParseInt
n64, err := strconv.ParseInt(s, 10, 64)
if err != nil {
tr.setColError("cannot parse `int64`", err)
return 0
}
return n64
}
// Uint64 returns the next uint64 column value from the current row.
func (tr *Reader) Uint64() uint64 {
if tr.err != nil {
return 0
}
b, err := tr.nextCol()
if err != nil {
tr.setColError("cannot read `uint64`", err)
return 0
}
s := b2s(b)
// Fast path - attempt to use Atoi
n, err := strconv.Atoi(s)
if err == nil && n >= 0 {
return uint64(n)
}
// Slow path - use ParseUint
n64, err := strconv.ParseUint(s, 10, 64)
if err != nil {
tr.setColError("cannot parse `uint64`", err)
return 0
}
return n64
}
// Float32 returns the next float32 column value from the current row.
func (tr *Reader) Float32() float32 {
if tr.err != nil {
return 0
}
b, err := tr.nextCol()
if err != nil {
tr.setColError("cannot read `float32`", err)
return 0
}
f32, err := strconv.ParseFloat(b2s(b), 32)
if err != nil {
tr.setColError("cannot parse `float32`", err)
return 0
}
return float32(f32)
}
// Float64 returns the next float64 column value from the current row.
func (tr *Reader) Float64() float64 {
if tr.err != nil {
return 0
}
b, err := tr.nextCol()
if err != nil {
tr.setColError("cannot read `float64`", err)
return 0
}
f64, err := strconv.ParseFloat(b2s(b), 64)
if err != nil {
tr.setColError("cannot parse `float64`", err)
return 0
}
return f64
}