number called by `any` after reading number between 0 to 9
()
| 339 | |
| 340 | // number called by `any` after reading number between 0 to 9 |
| 341 | func (d *Decoder) number() (float64, error) { |
| 342 | d.scratch.reset() |
| 343 | |
| 344 | var ( |
| 345 | c = d.cur() |
| 346 | n float64 |
| 347 | isFloat bool |
| 348 | ) |
| 349 | |
| 350 | // digits first |
| 351 | switch { |
| 352 | case c == '0': |
| 353 | d.scratch.add(c) |
| 354 | c = d.next() |
| 355 | case '1' <= c && c <= '9': |
| 356 | for ; c >= '0' && c <= '9'; c = d.next() { |
| 357 | n = 10*n + float64(c-'0') |
| 358 | d.scratch.add(c) |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | // . followed by 1 or more digits |
| 363 | if c == '.' { |
| 364 | isFloat = true |
| 365 | d.scratch.add(c) |
| 366 | |
| 367 | // first char following must be digit |
| 368 | if c = d.next(); c < '0' && c > '9' { |
| 369 | return 0, d.mkError(ErrSyntax, "after decimal point in numeric literal") |
| 370 | } |
| 371 | d.scratch.add(c) |
| 372 | |
| 373 | for { |
| 374 | if d.remaining() == 0 { |
| 375 | return 0, d.mkError(ErrUnexpectedEOF) |
| 376 | } |
| 377 | if c = d.next(); c < '0' || c > '9' { |
| 378 | break |
| 379 | } |
| 380 | d.scratch.add(c) |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | // e or E followed by an optional - or + and |
| 385 | // 1 or more digits. |
| 386 | if c == 'e' || c == 'E' { |
| 387 | isFloat = true |
| 388 | d.scratch.add(c) |
| 389 | |
| 390 | if c = d.next(); c == '+' || c == '-' { |
| 391 | d.scratch.add(c) |
| 392 | if c = d.next(); c < '0' || c > '9' { |
| 393 | return 0, d.mkError(ErrSyntax, "in exponent of numeric literal") |
| 394 | } |
| 395 | d.scratch.add(c) |
| 396 | } |
| 397 | for ; c >= '0' && c <= '9'; c = d.next() { |
| 398 | d.scratch.add(c) |