/
int.go
342 lines (311 loc) · 8.36 KB
/
int.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
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
package reader
import (
"math"
)
// intSeq extracts int values recursively until the closing bracket
// is found, then assigns the elements to the allocated slice.
func (r *Reader) intSeq(idx int) (res []int, err error) {
var n int
if n, err = r.Int(); err == nil {
if r.Next() {
res, err = r.intSeq(idx + 1)
} else {
res = make([]int, idx+1)
}
if err == nil {
res[idx] = n
}
}
return
}
// Ints extracts an array of int values from the data and skips all whitespace
// after it. The values must be enclosed in square brackets "[...]" and the
// values must be separated by commas.
func (r *Reader) Ints() (res []int, err error) {
if err = r.OpenArray(); err == nil {
if r.Token() == TerminatorToken {
res = []int{}
err = r.CloseArray()
} else if res, err = r.intSeq(0); err == nil {
err = r.CloseArray()
}
}
return
}
// Int extracts the next int value from the data and skips all
// whitespace after it.
func (r *Reader) Int() (n int, err error) {
dat := r.dat[r.pos:]
num, neg, pos, ok := readInteger(dat)
if !ok {
if num == 0 && neg {
return 0, newNumberOutOfRangeError(dat[:pos], r.pos)
} else if pos == len(dat) {
return 0, newEndOfFileError()
} else {
return 0, newInvalidCharacterError(dat[pos], r.pos+pos)
}
} else if !neg && num <= math.MaxInt32 {
n = int(num)
} else if neg && num <= math.MaxInt32+1 {
n = int(-int64(num))
} else {
return 0, newNumberOutOfRangeError(dat[:pos], r.pos)
}
r.pos += pos
r.SkipWhiteSpace()
return
}
// Intp extracts the next int value and returns a pointer variable.
func (r *Reader) Intp() (res *int, err error) {
if v, err := r.Int(); err == nil {
res = &v
}
return
}
// int8Seq extracts int8 values recursively until the closing bracket
// is found, then assigns the elements to the allocated slice.
func (r *Reader) int8Seq(idx int) (res []int8, err error) {
var n int8
if n, err = r.Int8(); err == nil {
if r.Next() {
res, err = r.int8Seq(idx + 1)
} else {
res = make([]int8, idx+1)
}
if err == nil {
res[idx] = n
}
}
return
}
// Int8s extracts an array of int8 values from the data and skips all
// whitespace after it. The values must be enclosed in square brackets "[...]"
// and the values must be separated by commas.
func (r *Reader) Int8s() (res []int8, err error) {
if err = r.OpenArray(); err == nil {
if r.Token() == TerminatorToken {
res = []int8{}
err = r.CloseArray()
} else if res, err = r.int8Seq(0); err == nil {
err = r.CloseArray()
}
}
return
}
// Int8 extracts the next int8 value from the data and skips all
// whitespace after it.
func (r *Reader) Int8() (n int8, err error) {
dat := r.dat[r.pos:]
num, neg, pos, ok := readInteger(dat)
if !ok {
if num == 0 && neg {
return 0, newNumberOutOfRangeError(dat[:pos], r.pos)
} else if pos == len(dat) {
return 0, newEndOfFileError()
} else {
return 0, newInvalidCharacterError(dat[pos], r.pos+pos)
}
} else if !neg && num <= math.MaxInt8 {
n = int8(num)
} else if neg && num <= math.MaxInt8+1 {
n = int8(-int64(num))
} else {
return 0, newNumberOutOfRangeError(dat[:pos], r.pos)
}
r.pos += pos
r.SkipWhiteSpace()
return
}
// Int8p extracts the next int8 value and returns a pointer variable.
func (r *Reader) Int8p() (res *int8, err error) {
if v, err := r.Int8(); err == nil {
res = &v
}
return
}
// int16Seq extracts int16 values recursively until the closing bracket
// is found, then assigns the elements to the allocated slice.
func (r *Reader) int16Seq(idx int) (res []int16, err error) {
var n int16
if n, err = r.Int16(); err == nil {
if r.Next() {
res, err = r.int16Seq(idx + 1)
} else {
res = make([]int16, idx+1)
}
if err == nil {
res[idx] = n
}
}
return
}
// Int16s extracts an array of int16 values from the data and skips all
// whitespace after it. The values must be enclosed in square brackets "[...]"
// and the values must be separated by commas.
func (r *Reader) Int16s() (res []int16, err error) {
if err = r.OpenArray(); err == nil {
if r.Token() == TerminatorToken {
res = []int16{}
err = r.CloseArray()
} else if res, err = r.int16Seq(0); err == nil {
err = r.CloseArray()
}
}
return
}
// Int16 extracts the next int16 value from the data and skips all
// whitespace after it.
func (r *Reader) Int16() (n int16, err error) {
dat := r.dat[r.pos:]
num, neg, pos, ok := readInteger(dat)
if !ok {
if num == 0 && neg {
return 0, newNumberOutOfRangeError(dat[:pos], r.pos)
} else if pos == len(dat) {
return 0, newEndOfFileError()
} else {
return 0, newInvalidCharacterError(dat[pos], r.pos+pos)
}
} else if !neg && num <= math.MaxInt16 {
n = int16(num)
} else if neg && num <= math.MaxInt16+1 {
n = int16(-int64(num))
} else {
return 0, newNumberOutOfRangeError(dat[:pos], r.pos)
}
r.pos += pos
r.SkipWhiteSpace()
return
}
// Int16p extracts the next int16 value and returns a pointer variable.
func (r *Reader) Int16p() (res *int16, err error) {
if v, err := r.Int16(); err == nil {
res = &v
}
return
}
// int32Seq extracts int32 values recursively until the closing bracket
// is found, then assigns the elements to the allocated slice.
func (r *Reader) int32Seq(idx int) (res []int32, err error) {
var n int32
if n, err = r.Int32(); err == nil {
if r.Next() {
res, err = r.int32Seq(idx + 1)
} else {
res = make([]int32, idx+1)
}
if err == nil {
res[idx] = n
}
}
return
}
// Int32s extracts an array of int32 values from the data and skips all
// whitespace after it. The values must be enclosed in square brackets "[...]"
// and the values must be separated by commas.
func (r *Reader) Int32s() (res []int32, err error) {
if err = r.OpenArray(); err == nil {
if r.Token() == TerminatorToken {
res = []int32{}
err = r.CloseArray()
} else if res, err = r.int32Seq(0); err == nil {
err = r.CloseArray()
}
}
return
}
// Int32 extracts the next int32 value from the data and skips all
// whitespace after it.
func (r *Reader) Int32() (n int32, err error) {
dat := r.dat[r.pos:]
num, neg, pos, ok := readInteger(dat)
if !ok {
if num == 0 && neg {
return 0, newNumberOutOfRangeError(dat[:pos], r.pos)
} else if pos == len(dat) {
return 0, newEndOfFileError()
} else {
return 0, newInvalidCharacterError(dat[pos], r.pos+pos)
}
} else if !neg && num <= math.MaxInt32 {
n = int32(num)
} else if neg && num <= math.MaxInt32+1 {
n = int32(-int64(num))
} else {
return 0, newNumberOutOfRangeError(dat[:pos], r.pos)
}
r.pos += pos
r.SkipWhiteSpace()
return
}
// Int32p extracts the next int32 value and returns a pointer variable.
func (r *Reader) Int32p() (res *int32, err error) {
if v, err := r.Int32(); err == nil {
res = &v
}
return
}
// int64Seq extracts int64 values recursively until the closing bracket
// is found, then assigns the elements to the allocated slice.
func (r *Reader) int64Seq(idx int) (res []int64, err error) {
var n int64
if n, err = r.Int64(); err == nil {
if r.Next() {
res, err = r.int64Seq(idx + 1)
} else {
res = make([]int64, idx+1)
}
if err == nil {
res[idx] = n
}
}
return
}
// Int64s extracts an array of int64 values from the data and skips all
// whitespace after it. The values must be enclosed in square brackets "[...]"
// and the values must be separated by commas.
func (r *Reader) Int64s() (res []int64, err error) {
if err = r.OpenArray(); err == nil {
if r.Token() == TerminatorToken {
res = []int64{}
err = r.CloseArray()
} else if res, err = r.int64Seq(0); err == nil {
err = r.CloseArray()
}
}
return
}
// Int64 extracts the next int64 value from the data and skips all
// whitespace after it.
func (r *Reader) Int64() (n int64, err error) {
dat := r.dat[r.pos:]
num, neg, pos, ok := readInteger(dat)
if !ok {
if num == 0 && neg {
return 0, newNumberOutOfRangeError(dat[:pos], r.pos)
} else if pos == len(dat) {
return 0, newEndOfFileError()
} else {
return 0, newInvalidCharacterError(dat[pos], r.pos+pos)
}
} else if !neg && num <= math.MaxInt64 {
n = int64(num)
} else if neg && num <= math.MaxInt64 {
n = -int64(num)
} else if neg && num == math.MaxInt64+1 {
n = math.MinInt64
} else {
return 0, newNumberOutOfRangeError(dat[:pos], r.pos)
}
r.pos += pos
r.SkipWhiteSpace()
return
}
// Int64p extracts the next int64 value and returns a pointer variable.
func (r *Reader) Int64p() (res *int64, err error) {
if v, err := r.Int64(); err == nil {
res = &v
}
return
}