Joe Tsai | 879b18d | 2018-08-03 17:22:24 -0700 | [diff] [blame] | 1 | // Copyright 2018 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | package json |
| 6 | |
| 7 | import ( |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 8 | "bytes" |
Joe Tsai | 879b18d | 2018-08-03 17:22:24 -0700 | [diff] [blame] | 9 | "math" |
Joe Tsai | 879b18d | 2018-08-03 17:22:24 -0700 | [diff] [blame] | 10 | "strconv" |
Joe Tsai | 879b18d | 2018-08-03 17:22:24 -0700 | [diff] [blame] | 11 | ) |
| 12 | |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 13 | // appendFloat formats given float in bitSize, and appends to the given []byte. |
| 14 | func appendFloat(out []byte, n float64, bitSize int) []byte { |
| 15 | switch { |
| 16 | case math.IsNaN(n): |
| 17 | return append(out, `"NaN"`...) |
| 18 | case math.IsInf(n, +1): |
| 19 | return append(out, `"Infinity"`...) |
| 20 | case math.IsInf(n, -1): |
| 21 | return append(out, `"-Infinity"`...) |
Joe Tsai | 879b18d | 2018-08-03 17:22:24 -0700 | [diff] [blame] | 22 | } |
| 23 | |
| 24 | // JSON number formatting logic based on encoding/json. |
| 25 | // See floatEncoder.encode for reference. |
Joe Tsai | 879b18d | 2018-08-03 17:22:24 -0700 | [diff] [blame] | 26 | fmt := byte('f') |
| 27 | if abs := math.Abs(n); abs != 0 { |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 28 | if bitSize == 64 && (abs < 1e-6 || abs >= 1e21) || |
| 29 | bitSize == 32 && (float32(abs) < 1e-6 || float32(abs) >= 1e21) { |
Joe Tsai | 879b18d | 2018-08-03 17:22:24 -0700 | [diff] [blame] | 30 | fmt = 'e' |
| 31 | } |
| 32 | } |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 33 | out = strconv.AppendFloat(out, n, fmt, -1, bitSize) |
Joe Tsai | 879b18d | 2018-08-03 17:22:24 -0700 | [diff] [blame] | 34 | if fmt == 'e' { |
| 35 | n := len(out) |
| 36 | if n >= 4 && out[n-4] == 'e' && out[n-3] == '-' && out[n-2] == '0' { |
| 37 | out[n-2] = out[n-1] |
| 38 | out = out[:n-1] |
| 39 | } |
| 40 | } |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 41 | return out |
Joe Tsai | 879b18d | 2018-08-03 17:22:24 -0700 | [diff] [blame] | 42 | } |
| 43 | |
Herbie Ong | a342195 | 2019-03-21 18:12:26 -0700 | [diff] [blame] | 44 | // consumeNumber reads the given []byte for a valid JSON number. If it is valid, |
| 45 | // it returns the number of bytes. Parsing logic follows the definition in |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 46 | // https://tools.ietf.org/html/rfc7159#section-6, and is based off |
| 47 | // encoding/json.isValidNumber function. |
Herbie Ong | a342195 | 2019-03-21 18:12:26 -0700 | [diff] [blame] | 48 | func consumeNumber(input []byte) (int, bool) { |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 49 | var n int |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 50 | |
| 51 | s := input |
| 52 | if len(s) == 0 { |
Herbie Ong | a342195 | 2019-03-21 18:12:26 -0700 | [diff] [blame] | 53 | return 0, false |
Joe Tsai | 879b18d | 2018-08-03 17:22:24 -0700 | [diff] [blame] | 54 | } |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 55 | |
| 56 | // Optional - |
| 57 | if s[0] == '-' { |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 58 | s = s[1:] |
| 59 | n++ |
| 60 | if len(s) == 0 { |
Herbie Ong | a342195 | 2019-03-21 18:12:26 -0700 | [diff] [blame] | 61 | return 0, false |
Joe Tsai | 879b18d | 2018-08-03 17:22:24 -0700 | [diff] [blame] | 62 | } |
Joe Tsai | 879b18d | 2018-08-03 17:22:24 -0700 | [diff] [blame] | 63 | } |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 64 | |
| 65 | // Digits |
| 66 | switch { |
| 67 | case s[0] == '0': |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 68 | s = s[1:] |
| 69 | n++ |
| 70 | |
| 71 | case '1' <= s[0] && s[0] <= '9': |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 72 | s = s[1:] |
| 73 | n++ |
| 74 | for len(s) > 0 && '0' <= s[0] && s[0] <= '9' { |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 75 | s = s[1:] |
| 76 | n++ |
| 77 | } |
| 78 | |
| 79 | default: |
Herbie Ong | a342195 | 2019-03-21 18:12:26 -0700 | [diff] [blame] | 80 | return 0, false |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | // . followed by 1 or more digits. |
| 84 | if len(s) >= 2 && s[0] == '.' && '0' <= s[1] && s[1] <= '9' { |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 85 | s = s[2:] |
| 86 | n += 2 |
| 87 | for len(s) > 0 && '0' <= s[0] && s[0] <= '9' { |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 88 | s = s[1:] |
| 89 | n++ |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | // e or E followed by an optional - or + and |
| 94 | // 1 or more digits. |
| 95 | if len(s) >= 2 && (s[0] == 'e' || s[0] == 'E') { |
| 96 | s = s[1:] |
| 97 | n++ |
| 98 | if s[0] == '+' || s[0] == '-' { |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 99 | s = s[1:] |
| 100 | n++ |
| 101 | if len(s) == 0 { |
Herbie Ong | a342195 | 2019-03-21 18:12:26 -0700 | [diff] [blame] | 102 | return 0, false |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 103 | } |
| 104 | } |
| 105 | for len(s) > 0 && '0' <= s[0] && s[0] <= '9' { |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 106 | s = s[1:] |
| 107 | n++ |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | // Check that next byte is a delimiter or it is at the end. |
| 112 | if n < len(input) && isNotDelim(input[n]) { |
Herbie Ong | a342195 | 2019-03-21 18:12:26 -0700 | [diff] [blame] | 113 | return 0, false |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 114 | } |
| 115 | |
Herbie Ong | a342195 | 2019-03-21 18:12:26 -0700 | [diff] [blame] | 116 | return n, true |
| 117 | } |
| 118 | |
| 119 | // numberParts is the result of parsing out a valid JSON number. It contains |
| 120 | // the parts of a number. The parts are used for integer conversion. |
| 121 | type numberParts struct { |
| 122 | neg bool |
| 123 | intp []byte |
| 124 | frac []byte |
| 125 | exp []byte |
| 126 | } |
| 127 | |
| 128 | // parseNumber constructs numberParts from given []byte. The logic here is |
| 129 | // similar to consumeNumber above with the difference of having to construct |
| 130 | // numberParts. |
| 131 | func parseNumber(input []byte) (numberParts, bool) { |
| 132 | var neg bool |
| 133 | var intp []byte |
| 134 | var frac []byte |
| 135 | var exp []byte |
| 136 | |
| 137 | s := input |
| 138 | if len(s) == 0 { |
| 139 | return numberParts{}, false |
| 140 | } |
| 141 | |
| 142 | // Optional - |
| 143 | if s[0] == '-' { |
| 144 | neg = true |
| 145 | s = s[1:] |
| 146 | if len(s) == 0 { |
| 147 | return numberParts{}, false |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | // Digits |
| 152 | switch { |
| 153 | case s[0] == '0': |
| 154 | // Skip first 0 and no need to store. |
| 155 | s = s[1:] |
| 156 | |
| 157 | case '1' <= s[0] && s[0] <= '9': |
| 158 | intp = append(intp, s[0]) |
| 159 | s = s[1:] |
| 160 | for len(s) > 0 && '0' <= s[0] && s[0] <= '9' { |
| 161 | intp = append(intp, s[0]) |
| 162 | s = s[1:] |
| 163 | } |
| 164 | |
| 165 | default: |
| 166 | return numberParts{}, false |
| 167 | } |
| 168 | |
| 169 | // . followed by 1 or more digits. |
| 170 | if len(s) >= 2 && s[0] == '.' && '0' <= s[1] && s[1] <= '9' { |
| 171 | frac = append(frac, s[1]) |
| 172 | s = s[2:] |
| 173 | for len(s) > 0 && '0' <= s[0] && s[0] <= '9' { |
| 174 | frac = append(frac, s[0]) |
| 175 | s = s[1:] |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | // e or E followed by an optional - or + and |
| 180 | // 1 or more digits. |
| 181 | if len(s) >= 2 && (s[0] == 'e' || s[0] == 'E') { |
| 182 | s = s[1:] |
| 183 | if s[0] == '+' || s[0] == '-' { |
| 184 | exp = append(exp, s[0]) |
| 185 | s = s[1:] |
| 186 | if len(s) == 0 { |
| 187 | return numberParts{}, false |
| 188 | } |
| 189 | } |
| 190 | for len(s) > 0 && '0' <= s[0] && s[0] <= '9' { |
| 191 | exp = append(exp, s[0]) |
| 192 | s = s[1:] |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | return numberParts{ |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 197 | neg: neg, |
| 198 | intp: intp, |
| 199 | frac: bytes.TrimRight(frac, "0"), // Remove unnecessary 0s to the right. |
| 200 | exp: exp, |
Herbie Ong | a342195 | 2019-03-21 18:12:26 -0700 | [diff] [blame] | 201 | }, true |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | // normalizeToIntString returns an integer string in normal form without the |
| 205 | // E-notation for given numberParts. It will return false if it is not an |
| 206 | // integer or if the exponent exceeds than max/min int value. |
Herbie Ong | a342195 | 2019-03-21 18:12:26 -0700 | [diff] [blame] | 207 | func normalizeToIntString(n numberParts) (string, bool) { |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 208 | num := n.intp |
| 209 | intpSize := len(num) |
| 210 | fracSize := len(n.frac) |
| 211 | |
| 212 | if intpSize == 0 && fracSize == 0 { |
| 213 | return "0", true |
| 214 | } |
| 215 | |
| 216 | var exp int |
| 217 | if len(n.exp) > 0 { |
| 218 | i, err := strconv.ParseInt(string(n.exp), 10, 32) |
| 219 | if err != nil { |
| 220 | return "", false |
| 221 | } |
| 222 | exp = int(i) |
| 223 | } |
| 224 | |
| 225 | if exp >= 0 { |
| 226 | // For positive E, shift fraction digits into integer part and also pad |
| 227 | // with zeroes as needed. |
| 228 | |
| 229 | // If there are more digits in fraction than the E value, then number is |
| 230 | // not an integer. |
| 231 | if fracSize > exp { |
| 232 | return "", false |
| 233 | } |
| 234 | |
| 235 | num = append(num, n.frac...) |
| 236 | for i := 0; i < exp-fracSize; i++ { |
| 237 | num = append(num, '0') |
| 238 | } |
| 239 | |
| 240 | } else { |
| 241 | // For negative E, shift digits in integer part out. |
| 242 | |
| 243 | // If there are any fractions to begin with, then number is not an |
| 244 | // integer. |
| 245 | if fracSize > 0 { |
| 246 | return "", false |
| 247 | } |
| 248 | |
| 249 | index := intpSize + exp |
| 250 | if index < 0 { |
| 251 | return "", false |
| 252 | } |
| 253 | // If any of the digits being shifted out is non-zero, then number is |
| 254 | // not an integer. |
| 255 | for i := index; i < intpSize; i++ { |
| 256 | if num[i] != '0' { |
| 257 | return "", false |
| 258 | } |
| 259 | } |
| 260 | num = num[:index] |
| 261 | } |
| 262 | |
| 263 | if n.neg { |
| 264 | return "-" + string(num), true |
| 265 | } |
| 266 | return string(num), true |
Joe Tsai | 879b18d | 2018-08-03 17:22:24 -0700 | [diff] [blame] | 267 | } |