-
Notifications
You must be signed in to change notification settings - Fork 62
/
scan.go
378 lines (339 loc) · 6.43 KB
/
scan.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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
package decimal
import (
"fmt"
"io"
"math"
"github.com/ericlagergren/decimal/internal/arith"
"github.com/ericlagergren/decimal/internal/c"
)
func (z *Big) scan(r io.ByteScanner) error {
if debug {
defer func() { z.validate() }()
}
// http://speleotrove.com/decimal/daconvs.html#refnumsyn
//
// sign ::= '+' | '-'
// digit ::= '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' |
// '8' | '9'
// indicator ::= 'e' | 'E'
// digits ::= digit [digit]...
// decimal-part ::= digits '.' [digits] | ['.'] digits
// exponent-part ::= indicator [sign] digits
// infinity ::= 'Infinity' | 'Inf'
// nan ::= 'NaN' [digits] | 'sNaN' [digits]
// numeric-value ::= decimal-part [exponent-part] | infinity
// numeric-string ::= [sign] numeric-value | [sign] nan
//
// We deviate a little by being a tad bit more forgiving. For instance,
// we allow case-insensitive nan and infinity values.
// Before scanning, reset it to zero
z.SetUint64(0)
// Sign
neg, err := scanSign(r)
if err != nil {
return err
}
z.form, err = z.scanForm(r)
if err != nil {
switch err {
case ConversionSyntax:
z.form = qnan
z.Context.Conditions |= ConversionSyntax
default:
return err
}
return nil
}
if z.isSpecial() {
if neg {
z.form |= signbit
}
return nil
}
// Mantissa (as a unsigned integer)
if err := z.scanMant(r); err != nil {
switch err {
case io.EOF:
z.form = qnan
return io.ErrUnexpectedEOF
case ConversionSyntax:
z.form = qnan
z.Context.Conditions |= ConversionSyntax
default:
return err
}
return nil
}
// Exponent
if err := z.scanExponent(r); err != nil && err != io.EOF {
switch err {
case Underflow:
z.xflow(MinScale, false, neg)
case Overflow:
z.xflow(MinScale, true, neg)
case ConversionSyntax:
z.form = qnan
z.Context.Conditions |= ConversionSyntax
default:
return err
}
return nil
}
// Adjust for negative values.
if neg {
z.form |= signbit
}
return nil
}
func scanSign(r io.ByteScanner) (bool, error) {
ch, err := r.ReadByte()
if err != nil {
return false, err
}
switch ch {
case '+':
return false, nil
case '-':
return true, nil
default:
return false, r.UnreadByte()
}
}
func (z *Big) scanForm(r io.ByteScanner) (form, error) {
ch, err := r.ReadByte()
if err != nil {
return 0, err
}
if (ch >= '0' && ch <= '9') || ch == '.' {
return finite, r.UnreadByte()
}
signal := false
switch ch {
case 'i', 'I':
return z.scanInfinity(r)
case 'q', 'Q':
// OK
case 's', 'S':
signal = true
case 'n', 'N':
r.UnreadByte()
default:
return 0, ConversionSyntax
}
const (
s = "nan"
)
for i := 0; i < len(s); i++ {
ch, err := r.ReadByte()
if err != nil {
if err == io.EOF {
return 0, io.ErrUnexpectedEOF
}
return 0, err
}
if ch != s[i] && ch != s[i]-('a'-'A') {
return 0, ConversionSyntax
}
}
// Parse payload
for {
ch, err := r.ReadByte()
if err != nil {
if err == io.EOF {
break
}
return 0, err
}
if ch < '0' || ch > '9' {
return 0, ConversionSyntax
}
d := ch - '0'
if d >= 10 {
return 0, ConversionSyntax
}
const cutoff = math.MaxUint64/10 + 1
if z.compact >= cutoff {
return 0, ConversionSyntax
}
z.compact *= 10
if z.compact+uint64(d) < z.compact {
return 0, ConversionSyntax
}
z.compact += uint64(d)
}
if signal {
return snan, nil
}
return qnan, nil
}
func (z *Big) scanInfinity(r io.ByteScanner) (form, error) {
const (
s = "infinity"
)
for i := 1; i < len(s); i++ {
ch, err := r.ReadByte()
if err != nil {
if err != io.EOF {
return 0, err
}
if i == len("inf") {
return inf, nil
}
return 0, io.ErrUnexpectedEOF
}
if ch != s[i] && ch != s[i]-('a'-'A') {
return 0, ConversionSyntax
}
}
return inf, nil
}
func (z *Big) scanMant(r io.ByteScanner) (err error) {
buf := make([]byte, 0, 20)
seenDot := false
dot := 0
big := false
Loop:
for {
ch, err := r.ReadByte()
if err != nil {
if err == io.EOF {
break Loop
}
return err
}
// Common case.
if ch >= '0' && ch <= '9' {
buf = append(buf, ch)
if !big {
d := ch - '0'
if d >= 10 {
return ConversionSyntax
}
const cutoff = math.MaxUint64/10 + 1
if z.compact >= cutoff {
big = true
continue
}
z.compact *= 10
if z.compact+uint64(d) < z.compact {
big = true
continue
}
z.compact += uint64(d)
}
continue
}
switch ch {
case '.':
if seenDot {
// Found two dots.
return ConversionSyntax
}
seenDot = true
dot = len(buf)
case 'e', 'E':
// Hit the exponent: we're done here.
if err := r.UnreadByte(); err != nil {
return err
}
break Loop
default:
return ConversionSyntax
}
}
if big || z.compact == c.Inflated {
z.unscaled.SetString(string(buf), 10)
z.compact = c.Inflated
z.precision = arith.BigLength(&z.unscaled)
} else {
z.precision = arith.Length(z.compact)
}
if seenDot {
z.exp = -(len(buf) - dot)
}
return nil
}
func (z *Big) scanExponent(r io.ByteScanner) error {
ch, err := r.ReadByte()
if err != nil {
if err == io.EOF {
return nil
}
return err
}
switch ch {
case 'e', 'E':
// OK
default:
return ConversionSyntax
}
ch, err = r.ReadByte()
if err != nil {
return err
}
var neg bool
switch ch {
case '+':
// OK
case '-':
neg = true
default:
r.UnreadByte()
}
max := uint64(arith.MaxInt)
if neg {
max++ // -math.MinInt
}
var exp uint64
for {
ch, err := r.ReadByte()
if err != nil {
if err == io.EOF {
break
}
return err
}
if ch < '0' || ch > '9' {
return ConversionSyntax
}
d := ch - '0'
if d >= 10 {
return ConversionSyntax
}
const cutoff = math.MaxUint64/10 + 1
if exp >= cutoff {
return ConversionSyntax
}
exp *= 10
v := exp + uint64(d)
if v < exp || v > max {
if neg {
return Underflow
}
return Overflow
}
exp = v
}
if neg {
z.exp -= int(exp)
} else {
z.exp += int(exp)
}
return nil
}
// byteReader implementation borrowed from math/big/intconv.go
// byteReader is a local wrapper around fmt.ScanState; it implements the
// io.ByteReader interface.
type byteReader struct {
fmt.ScanState
}
func (r byteReader) ReadByte() (byte, error) {
ch, size, err := r.ReadRune()
if size != 1 && err == nil {
err = fmt.Errorf("invalid rune %#U", ch)
}
return byte(ch), err
}
func (r byteReader) UnreadByte() error {
return r.UnreadRune()
}