-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathdecimal64scan.go
306 lines (275 loc) · 6.16 KB
/
decimal64scan.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
package decimal
import (
"fmt"
"io"
"strings"
)
var DefaultScanContext64 = DefaultFormatContext64
// Parse64 parses a string representation of a number as a Decimal64.
// It uses [DefaultScanContext64].
func Parse64(s string) (Decimal64, error) {
return DefaultScanContext64.Parse(s)
}
// Parse64 parses a string representation of a number as a Decimal64.
func (ctx Context64) Parse(s string) (Decimal64, error) {
state := &scanner{reader: strings.NewReader(s)}
var d Decimal64
if err := ctx.Scan(&d, state, 'e'); err != nil {
return d, err
}
// entire string must have been consumed
r, _, err := state.ReadRune()
if err == nil {
return QNaN64, fmt.Errorf("expected end of string, found %c", r)
}
return d, nil
}
// MustParse64 parses a string as a Decimal64 and returns the value or
// panics if the string doesn't represent a valid Decimal64.
// It uses [DefaultScanContext64].
func MustParse64(s string) Decimal64 {
return DefaultScanContext64.MustParse(s)
}
// MustParse64 parses a string as a Decimal64 and returns the value or
// panics if the string doesn't represent a valid Decimal64.
func (ctx Context64) MustParse(s string) Decimal64 {
d, err := ctx.Parse(s)
if err != nil {
panic(err)
}
return d
}
// Scan implements fmt.Scanner.
// It uses [DefaultScanContext64].
func (d *Decimal64) Scan(state fmt.ScanState, verb rune) error {
return DefaultScanContext64.Scan(d, state, verb)
}
// Scan scans a string into a Decimal64, applying context rounding.
func (ctx Context64) Scan(d *Decimal64, state fmt.ScanState, verb rune) error {
*d = SNaN64
sign, err := eatRune(state, '+', '-')
if err != nil {
return err
}
if sign < 0 {
sign = 0
}
// Word-number: [Ii]nf(inity)?|∞|[qs]?(nan|NaN)
kw, err := keywords.Match(state)
if err != nil {
return err
}
switch kw {
case 0:
case 1:
if sign == 0 {
*d = Infinity64
} else {
*d = NegInfinity64
}
return nil
case 3:
payload, _ := eatBytes(state, isDigit)
payloadInt, _, err := ctx.parseUint(payload)
if err != nil {
return err
}
*d = newPayloadNan(sign, flQNaN, uint64(payloadInt))
return nil
case 2:
payload, _ := eatBytes(state, isDigit)
payloadInt, _, err := ctx.parseUint(payload)
if err != nil {
return err
}
*d = newPayloadNan(sign, flSNaN, uint64(payloadInt))
return nil
default:
return errNotDecimal64
}
whole, err := eatBytes(state, isDigit)
if err != nil {
return err
}
var buf [64]byte
mantissa := append(buf[:0], whole...)
if _, err := eatRune(state, '.', -1); err != nil {
return err
}
frac, err := eatBytes(state, isDigit)
if err != nil {
return err
}
mantissa = append(mantissa, frac...)
if len(mantissa) == 0 {
return fmt.Errorf("mantissa missing")
}
e, err := eatRune(state, 'e', 'E')
if err != nil {
return err
}
var expSign int
var exp []byte
if e != -1 {
expSign, err = eatRune(state, '+', '-')
if err != nil {
return err
}
if expSign < 0 {
expSign = 0
}
exp, err = eatBytes(state, isDigit)
if err != nil {
return err
}
if len(exp) == 0 {
return fmt.Errorf("exponent value missing")
}
}
significand, sExp, err := ctx.parseUint(mantissa)
if err != nil {
return err
}
if significand == 0 {
*d = zeroes64[sign]
return nil
}
uexponent, _, err := ctx.parseUint(exp)
if err != nil {
return err
}
exponent := int64(uexponent)
exponent *= int64(1 - 2*expSign)
if exponent > 1000 {
*d = infinities64[sign]
return nil
} else if exponent < -1000 {
*d = zeroes64[sign]
return nil
}
exponent += int64(sExp - len(frac))
partExp, partSignificand := renormalize(int16(exponent), uint64(significand))
*d = newFromParts(int8(sign), partExp, partSignificand)
return nil
}
func isDigit(r rune) bool {
return '0' <= r && r <= '9'
}
var errNotDecimal64 error = Error("not a valid Decimal64")
func allZeros(s []byte) bool {
for _, c := range s {
if c != '0' {
return false
}
}
return true
}
func (ctx Context64) parseUint(s []byte) (uint64, int, error) {
var a uint64
var exp int
for i, c := range s {
if a >= decimal64Base {
switch ctx.Rounding {
case HalfUp:
if c >= '5' {
a++
}
case HalfEven:
if c > '5' || c == '5' && (a%2 == 1 || !allZeros(s[i+1:])) {
a++
}
case Down:
default:
return 0, 0, fmt.Errorf("unsupported rounding mode: %v", ctx.Rounding)
}
exp = len(s) - i
break
}
a = 10*a + uint64(c-'0')
}
return a, exp, nil
}
type trie struct {
heads []string
tails tries
result int
}
func trieBranch(heads string, tails ...trie) trie {
return trie{heads: strings.Split(heads, "|"), tails: tails}
}
func trieLeaf(heads string, result int) trie {
return trie{heads: strings.Split(heads, "|"), result: result}
}
type tries []trie
func (tt tries) Match(state fmt.ScanState) (int, error) {
for _, t := range tt {
heads:
for _, head := range t.heads {
for i, c := range head {
// Try to eat the head.
r, _, err := state.ReadRune()
if err != nil {
if err != io.EOF {
return 0, err
}
continue heads
}
if r != c {
if i > 0 {
return 0, errNotDecimal64
}
if err := state.UnreadRune(); err != nil {
return 0, err
}
continue heads
}
}
if len(t.tails) == 0 {
return t.result, nil
}
return t.tails.Match(state)
}
}
return 0, nil
}
var keywords = tries{
trieBranch("inf|Inf", trieLeaf("inity|", 1)),
trieLeaf("∞", 1),
trieBranch("s", trieLeaf("nan|NaN", 2)),
trieBranch("q|", trieLeaf("nan|NaN", 3)),
}
func eatBytes(state fmt.ScanState, f func(r rune) bool) ([]byte, error) {
token, err := state.Token(false, f)
if err != nil {
return nil, err
}
return token, err
}
// eatRune returns 0 if it reads a, 1 if it reads b, -1 otherwise.
func eatRune(state fmt.ScanState, a, b rune) (int, error) {
r, _, err := state.ReadRune()
if err != nil {
if err != io.EOF {
return 0, err
}
return -1, nil
}
if r == a {
return 0, nil
}
if r == b {
return 1, nil
}
return -1, state.UnreadRune()
}
func newPayloadNan(sign int, fl flavor, weight uint64) Decimal64 {
s := uint64(sign) << 63
switch fl {
case flQNaN:
return new64(s | QNaN64.bits | weight)
case flSNaN:
return new64(s | SNaN64.bits | weight)
default:
return QNaN64
}
}