forked from ericlagergren/decimal
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscan.go
More file actions
483 lines (434 loc) · 9.47 KB
/
scan.go
File metadata and controls
483 lines (434 loc) · 9.47 KB
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
package decimal
import (
"errors"
"fmt"
"io"
"math/bits"
"strconv"
"unicode"
"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.
// Sign
neg, err := scanSign(r)
if err != nil {
return err
}
z.form, err = z.scanForm(r)
if err != nil {
if err == strconv.ErrSyntax {
z.Context.Conditions |= ConversionSyntax
}
return err
}
if z.form&special != 0 {
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 strconv.ErrSyntax:
z.form = qnan
z.Context.Conditions |= ConversionSyntax
}
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 strconv.ErrSyntax:
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()
}
// Likely infinity.
if ch == 'i' || ch == 'I' {
const (
inf1 = "infinity"
inf2 = "INFINITY"
)
for i := 1; i < len(inf1); i++ {
ch, err = r.ReadByte()
if err != nil {
if err == io.EOF {
// "inf"
if i == len("inf") {
return inf, nil
}
return 0, io.ErrUnexpectedEOF
}
return 0, err
}
if ch != inf1[i] && ch != inf2[i] {
return 0, strconv.ErrSyntax
}
}
return inf, nil
}
i := 0
signal := false
switch ch {
case 'q', 'Q':
// OK
case 's', 'S':
signal = true
case 'n', 'N':
i = 1 // or r.UnreadByte() and don't use i.
default:
return 0, strconv.ErrSyntax
}
const (
nan1 = "nan"
nan2 = "NAN"
)
for ; i < len(nan1); i++ {
ch, err = r.ReadByte()
if err != nil {
if err == io.EOF {
return 0, io.ErrUnexpectedEOF
}
return 0, err
}
if ch != nan1[i] && ch != nan2[i] {
return 0, strconv.ErrSyntax
}
}
// Parse payload
var buf [20]byte
for i = 0; i < 20; i++ {
ch, err := r.ReadByte()
if err != nil {
if err == io.EOF {
break
}
return 0, err
}
if ch >= '0' && ch <= '9' {
buf[i] = ch
}
}
if i > 0 {
z.compact, err = strconv.ParseUint(string(buf[:i]), 10, 64)
if err != nil {
return 0, err
}
}
if signal {
return snan, nil
}
return qnan, nil
}
// fakeState is implements fmt.ScanState so we can call big.Int.Scan.
type fakeState struct {
length int
scale int
i int // index into small
small []byte // buffer of first 20 or so characters.
r io.ByteScanner
}
func (f *fakeState) ReadRune() (rune, int, error) {
// small is guaranteed to be a valid numeric character.
if f.i < len(f.small) {
r := rune(f.small[f.i])
f.i++
f.length++
return r, 1, nil
}
ch, err := f.r.ReadByte()
if err != nil {
return 0, 0, err
}
if ch >= '0' && ch <= '9' {
f.length++
return rune(ch), 1, nil
}
if ch == '.' {
const noScale = -1
if f.scale > noScale {
return 0, 0, strconv.ErrSyntax
}
f.scale = f.length
return f.ReadRune() // skip to next raracter
}
if ch == 'e' || ch == 'E' {
// Can simply UnreadByte here since we're not using small anymore.
if err := f.r.UnreadByte(); err != nil {
return 0, 0, err
}
return 0, 0, io.EOF // end of mantissa
}
return 0, 0, strconv.ErrSyntax
}
func (f *fakeState) UnreadRune() error {
if f.i < len(f.small) {
if f.i == 0 {
return errors.New("attempted to read before start of buffer")
}
f.i--
return nil
}
return f.r.UnreadByte()
}
func (f *fakeState) SkipSpace() {
for {
ch, err := f.r.ReadByte()
if err != nil {
return
}
if !unicode.IsSpace(rune(ch)) {
f.r.UnreadByte()
return
}
}
}
func (f *fakeState) Token(skipSpace bool, fn func(rune) bool) (token []byte, err error) {
if skipSpace {
f.SkipSpace()
}
if fn == nil {
fn = func(r rune) bool { return !unicode.IsSpace(r) }
}
for {
r, _, err := f.ReadRune()
if err != nil {
if err == io.EOF {
break
}
return nil, err
}
if fn(r) {
token = append(token, byte(r))
}
}
return token, nil
}
func (f *fakeState) Width() (int, bool) { return 0, false }
func (f *fakeState) Read(_ []byte) (int, error) {
return 0, errors.New("bad scanning routine")
}
func (z *Big) scanMant(r io.ByteScanner) (err error) {
const noScale = -1
// Scan the first 21 or fewer bytes into a buffer. Should we hit io.EOF
// sooner, we know to try to parse it as a uint64. Otherwise, we read from
// small—followed by our io.ByteScanner—into z.uncaled.
var (
small [20 + 1]byte
scale = noScale
length int
i int
)
Loop:
for ; i < len(small); i++ {
ch, err := r.ReadByte()
if err != nil {
if err == io.EOF {
// Hit the end of our input: we're done here.
break
}
return err
}
// Common case.
if ch >= '0' && ch <= '9' {
small[i] = ch
length++
continue
}
switch ch {
case '.':
if scale != noScale { // found two '.'s
return strconv.ErrSyntax
}
scale = i
i--
case 'e', 'E':
// Hit the exponent: we're done here.
if err := r.UnreadByte(); err != nil {
return err
}
break Loop
default:
return strconv.ErrSyntax
}
}
// We can't use length to determine the precision since it'd require we
// count (and subtract) any leading zeros, but that's too much overhead for
// the general case (i.e., no leading zeros).
// We can tentatively fit into a uint64 if we didn't fill the buffer.
if i < len(small) {
z.compact, err = strconv.ParseUint(string(small[:i]), 10, 64)
if err == nil {
if scale != noScale {
z.exp = -int(length - scale)
} else {
z.exp = 0
}
z.precision = arith.Length(z.compact)
if z.compact == c.Inflated {
z.unscaled.SetUint64(c.Inflated)
}
return nil
}
err = err.(*strconv.NumError).Err
if err == strconv.ErrSyntax {
return err
}
// else err == strconv.ErrRange
}
// Either we filled the buffer or we hit the edge case where len(s) == 20
// but it's too large to fit into a uint64.
if i >= len(small) || (err == strconv.ErrRange && i == len(small)-1) {
err = nil
fs := fakeState{
small: small[:i],
r: r,
scale: scale,
length: -1,
}
if err := z.unscaled.Scan(&fs, 'd'); err != nil {
return err
}
// Technically we could have all zeros...
if z.unscaled.Sign() != 0 {
if m := z.unscaled.Uint64(); z.unscaled.IsUint64() && m != c.Inflated {
z.compact = m
z.precision = arith.Length(m)
} else {
z.compact = c.Inflated
z.precision = arith.BigLength(&z.unscaled)
}
} else {
z.compact = 0
z.precision = 1
}
if scale == noScale {
scale = fs.scale
}
length = fs.length
}
if scale != noScale {
z.exp = -int(length - scale)
}
return err
}
func (z *Big) scanExponent(r io.ByteScanner) (err error) {
// TODO(eric): there's actually an issue here if somebody has leading zeros
// in their exponent. But, I don't really think I want to support that.
// N is the max length of a signed N-bit int, including sign plus a leading
// 'e' or 'E'.
const N = ((((bits.UintSize + 1) * 1233) >> 12) + 1) + (64 / bits.UintSize)
var buf [N]byte
var i int
for ; i < len(buf); i++ {
if buf[i], err = r.ReadByte(); err != nil {
if err == io.EOF {
if i == 0 {
return nil
}
break
}
return err
}
}
if buf[0] != 'e' && buf[0] != 'E' {
return strconv.ErrSyntax
}
if ch, err := r.ReadByte(); err != io.EOF {
if ch < '0' || ch > '9' {
return err
}
if buf[0] == '-' {
return Underflow
}
return Overflow
}
exp, err := strconv.Atoi(string(buf[1:i]))
if err != nil {
if err = err.(*strconv.NumError).Err; err != strconv.ErrRange {
return err
}
// Atoi sets exp < 0 on underflow.
if exp < 0 {
return Underflow
}
return Overflow
}
z.exp += 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()
}