/
json_decode_number.go
154 lines (141 loc) · 4.5 KB
/
json_decode_number.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
package jsontoken
import (
"fmt"
"io"
"strconv"
"github.com/ipld/go-ipld-prime/codec/codectools"
"github.com/ipld/go-ipld-prime/codec/codectools/scratch"
)
// License note: the string and numeric parsers here borrow
// heavily from the golang stdlib json parser scanner.
// That code is originally Copyright 2010 The Go Authors,
// and is governed by a BSD-style license.
// DecodeNumber will attempt to decode data in the format of a JSON numer from the reader.
// JSON is somewhat ambiguous about numbers: we'll return an int if we can, and a float if there's any decimal point involved.
// The boolean return indicates which kind of number we have:
// if true, we have an int (and the float return is invalid);
// if false, we have a float (and the int return is invalid).
func DecodeNumber(r *scratch.Reader) (codectools.TokenKind, int64, float64, error) {
r.Track()
// Scan until scanner tells us end of numeric.
// Pick the first scanner stepfunc based on the leading byte.
majorByte, err := r.Readn1()
if err != nil {
return codectools.TokenKind_Null, 0, 0, err
}
var step numscanStep
switch majorByte {
case '-':
step = numscan_neg
case '0':
step = numscan_0
case '1', '2', '3', '4', '5', '6', '7', '8', '9':
step = numscan_1
default:
panic("unreachable") // FIXME not anymore it ain't, this is exported
}
for {
b, err := r.Readn1()
if err == io.EOF {
break
}
if err != nil {
return 0, 0, 0, err
}
step, err = step(b)
if step == nil {
// Unread one. The scan loop consumed one char beyond the end (this is unavoidable in json!),
// and that might be part of what whatever is going to be decoded from this stream next.
r.Unreadn1()
break
}
if err != nil {
return 0, 0, 0, err
}
}
// Parse!
// *This is not a fast parse*.
// Try int first; if it fails try float; if that fails return the float error.
s := string(r.StopTrack())
if i, err := strconv.ParseInt(s, 10, 64); err == nil {
return codectools.TokenKind_Int, i, 0, nil
}
f, err := strconv.ParseFloat(s, 64)
return codectools.TokenKind_Float, 0, f, err
}
// Scan steps are looped over the stream to find how long the number is.
// A nil step func is returned to indicate the string is done.
// Actually parsing the string is done by 'parseString()'.
type numscanStep func(c byte) (numscanStep, error)
// numscan_neg is the state after reading `-` during a number.
func numscan_neg(c byte) (numscanStep, error) {
if c == '0' {
return numscan_0, nil
}
if '1' <= c && c <= '9' {
return numscan_1, nil
}
return nil, fmt.Errorf("invalid byte in numeric literal: 0x%x", c)
}
// numscan_1 is the state after reading a non-zero integer during a number,
// such as after reading `1` or `100` but not `0`.
func numscan_1(c byte) (numscanStep, error) {
if '0' <= c && c <= '9' {
return numscan_1, nil
}
return numscan_0(c)
}
// numscan_0 is the state after reading `0` during a number.
func numscan_0(c byte) (numscanStep, error) {
if c == '.' {
return numscan_dot, nil
}
if c == 'e' || c == 'E' {
return numscan_e, nil
}
return nil, nil
}
// numscan_dot is the state after reading the integer and decimal point in a number,
// such as after reading `1.`.
func numscan_dot(c byte) (numscanStep, error) {
if '0' <= c && c <= '9' {
return numscan_dot0, nil
}
return nil, fmt.Errorf("invalid byte after decimal in numeric literal: 0x%x", c)
}
// numscan_dot0 is the state after reading the integer, decimal point, and subsequent
// digits of a number, such as after reading `3.14`.
func numscan_dot0(c byte) (numscanStep, error) {
if '0' <= c && c <= '9' {
return numscan_dot0, nil
}
if c == 'e' || c == 'E' {
return numscan_e, nil
}
return nil, nil
}
// numscan_e is the state after reading the mantissa and e in a number,
// such as after reading `314e` or `0.314e`.
func numscan_e(c byte) (numscanStep, error) {
if c == '+' || c == '-' {
return numscan_eSign, nil
}
return numscan_eSign(c)
}
// numscan_eSign is the state after reading the mantissa, e, and sign in a number,
// such as after reading `314e-` or `0.314e+`.
func numscan_eSign(c byte) (numscanStep, error) {
if '0' <= c && c <= '9' {
return numscan_e0, nil
}
return nil, fmt.Errorf("invalid byte in exponent of numeric literal: 0x%x", c)
}
// numscan_e0 is the state after reading the mantissa, e, optional sign,
// and at least one digit of the exponent in a number,
// such as after reading `314e-2` or `0.314e+1` or `3.14e0`.
func numscan_e0(c byte) (numscanStep, error) {
if '0' <= c && c <= '9' {
return numscan_e0, nil
}
return nil, nil
}