Debian Codegraph @

internal/decoder/number_parse.go

raw
1 package decoder
2
3 import (
4 "fmt"
5
6 "github.com/goccy/go-json/internal/errors"
7 )
8
9 // The numbers of the input are parsed in one pass: the digits are accumulated as they are read.
10
11 // maxUint64Digits is the number of the digits which a uint64 holds whatever they are: 19 nines are less than 1<<64.
12 const maxUint64Digits = 19
13
14 // parseDigits reads the digits at cursor, which starts a number after its sign, and returns their value, the
15 // position after them and their number. A number which starts with 0 has no other digit: the digit after it,
16 // if any, is not read. The value is exact for up to maxUint64Digits digits.
17 func parseDigits(buf []byte, cursor int64) (uint64, int64, int) {
18 if buf[cursor] == '0' {
19 return 0, cursor + 1, 1
20 }
21 start := cursor
22 var u uint64
23 for {
24 d := buf[cursor] - '0'
25 if d > 9 {
26 break
27 }
28 u = u*10 + uint64(d)
29 cursor++
30 }
31 return u, cursor, int(cursor - start)
32 }
33
34 // isFloatContinuation reports whether c continues an integer as a number which is not an integer.
35 func isFloatContinuation(c byte) bool {
36 return c == '.' || c == 'e' || c == 'E'
37 }
38
39 // float64pow10 are the powers of ten which a float64 holds exactly.
40 var float64pow10 = [...]float64{
41 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 1e10, 1e11,
42 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19, 1e20, 1e21, 1e22,
43 }
44
45 // parseFloatFast parses the number at cursor, as the JSON grammar has it, when its mantissa has at most 19
46 // digits: the float64 of a mantissa of at most 53 bits times or divided by a power of ten which a float64 holds
47 // exactly is correctly rounded as it is ( Clinger's fast path ), and any other is multiplied by the power of ten
48 // from a table ( mulPow10 ). The result is the one of strconv.ParseFloat. It returns false for a number which
49 // is not decided so, or anything which is not a number by the grammar, which the caller parses as it did before.
50 func parseFloatFast(buf []byte, cursor int64) (float64, int64, bool) {
51 neg := buf[cursor] == '-'
52 if neg {
53 cursor++
54 }
55 if buf[cursor]-'0' > 9 {
56 return 0, 0, false
57 }
58 mantissa, cursor, digits := parseDigits(buf, cursor)
59 exp := 0
60 if buf[cursor] == '.' {
61 cursor++
62 start := cursor
63 if mantissa == 0 {
64 // the zeros before the first digit which is not zero are not digits of the mantissa
65 digits = 0
66 }
67 for {
68 d := buf[cursor] - '0'
69 if d > 9 {
70 break
71 }
72 mantissa = mantissa*10 + uint64(d)
73 if mantissa != 0 {
74 digits++
75 }
76 cursor++
77 }
78 fraction := int(cursor - start)
79 if fraction == 0 {
80 return 0, 0, false
81 }
82 exp = -fraction
83 }
84 if c := buf[cursor]; c == 'e' || c == 'E' {
85 cursor++
86 expNeg := false
87 switch buf[cursor] {
88 case '-':
89 expNeg = true
90 cursor++
91 case '+':
92 cursor++
93 }
94 start := cursor
95 e := 0
96 for {
97 d := buf[cursor] - '0'
98 if d > 9 {
99 break
100 }
101 if e < 10000 {
102 e = e*10 + int(d)
103 }
104 cursor++
105 }
106 if cursor == start {
107 return 0, 0, false
108 }
109 if expNeg {
110 e = -e
111 }
112 exp += e
113 }
114 if digits > maxUint64Digits {
115 return 0, 0, false
116 }
117 var f float64
118 if mantissa <= 1<<53 && exp >= -22 && exp <= 22 {
119 f = float64(mantissa)
120 if exp < 0 {
121 f /= float64pow10[-exp]
122 } else {
123 f *= float64pow10[exp]
124 }
125 } else {
126 var ok bool
127 if f, ok = mulPow10(mantissa, exp); !ok {
128 return 0, 0, false
129 }
130 }
131 if neg {
132 f = -f
133 }
134 return f, cursor, true
135 }
136
137 // numberEnd returns the position after the number which starts at start, validated by the grammar of the JSON
138 // numbers, which is followed by a byte which may end a value.
139 func numberEnd(buf []byte, start int64) (int64, error) {
140 c := start
141 if buf[c] == '-' {
142 c++
143 }
144 switch {
145 case buf[c] == '0':
146 c++
147 case '1' <= buf[c] && buf[c] <= '9':
148 c = skipDigits(buf, c+1)
149 default:
150 return 0, errors.ErrSyntax(fmt.Sprintf("invalid character %s in numeric literal", quoteChar(buf[c])), c+1)
151 }
152 if buf[c] == '.' {
153 c++
154 if buf[c]-'0' > 9 {
155 return 0, errors.ErrSyntax(fmt.Sprintf("invalid character %s after decimal point in numeric literal", quoteChar(buf[c])), c+1)
156 }
157 c = skipDigits(buf, c)
158 }
159 if buf[c] == 'e' || buf[c] == 'E' {
160 c++
161 if buf[c] == '+' || buf[c] == '-' {
162 c++
163 }
164 if buf[c]-'0' > 9 {
165 return 0, errors.ErrSyntax(fmt.Sprintf("invalid character %s in exponent of numeric literal", quoteChar(buf[c])), c+1)
166 }
167 c = skipDigits(buf, c)
168 }
169 if !validEndNumberChar[buf[c]] {
170 return 0, errors.ErrSyntax(fmt.Sprintf("invalid character %s after top-level value", quoteChar(buf[c])), c+1)
171 }
172 return c, nil
173 }
174
175 // skipDigits returns the position of the first byte from c which is not a digit.
176 func skipDigits(buf []byte, c int64) int64 {
177 for buf[c]-'0' <= 9 {
178 c++
179 }
180 return c
181 }