isValidJSONNumber reports whether s is a valid JSON number literal. From https://golang.org/src/encoding/json/encode.go#L652 isValidNumber Copyright 2010 The Go Authors.
(s string)
| 281 | // From https://golang.org/src/encoding/json/encode.go#L652 isValidNumber |
| 282 | // Copyright 2010 The Go Authors. |
| 283 | func isValidJSONNumber(s string) bool { |
| 284 | // This function implements the JSON numbers grammar. |
| 285 | // See https://tools.ietf.org/html/rfc7159#section-6 |
| 286 | // and https://www.json.org/img/number.png |
| 287 | |
| 288 | if s == "" { |
| 289 | return false |
| 290 | } |
| 291 | |
| 292 | // Optional - |
| 293 | if s[0] == '-' { |
| 294 | s = s[1:] |
| 295 | if s == "" { |
| 296 | return false |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | // Digits |
| 301 | switch { |
| 302 | default: |
| 303 | return false |
| 304 | |
| 305 | case s[0] == '0': |
| 306 | s = s[1:] |
| 307 | |
| 308 | case '1' <= s[0] && s[0] <= '9': |
| 309 | s = s[1:] |
| 310 | for len(s) > 0 && '0' <= s[0] && s[0] <= '9' { |
| 311 | s = s[1:] |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | // . followed by 1 or more digits. |
| 316 | if len(s) >= 2 && s[0] == '.' && '0' <= s[1] && s[1] <= '9' { |
| 317 | s = s[2:] |
| 318 | for len(s) > 0 && '0' <= s[0] && s[0] <= '9' { |
| 319 | s = s[1:] |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | // e or E followed by an optional - or + and |
| 324 | // 1 or more digits. |
| 325 | if len(s) >= 2 && (s[0] == 'e' || s[0] == 'E') { |
| 326 | s = s[1:] |
| 327 | if s[0] == '+' || s[0] == '-' { |
| 328 | s = s[1:] |
| 329 | if s == "" { |
| 330 | return false |
| 331 | } |
| 332 | } |
| 333 | for len(s) > 0 && '0' <= s[0] && s[0] <= '9' { |
| 334 | s = s[1:] |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | // Make sure we are at the end. |
| 339 | return s == "" |
| 340 | } |