Parse a JSON number that contains a fraction or exponent, as lexed into raw by #nextNumber. Uses Clinger's fast path where possible: when the significand has at most 15 digits it is exactly representable as a double, as are powers of ten up to 10^22, so a single floating-point mu
(StringBuilder raw)
| 398 | * are always bit-for-bit identical to the JDK. |
| 399 | */ |
| 400 | private static double parseDouble(StringBuilder raw) { |
| 401 | int length = raw.length(); |
| 402 | int index = 0; |
| 403 | boolean negative = false; |
| 404 | if (raw.charAt(0) == '-') { |
| 405 | negative = true; |
| 406 | index = 1; |
| 407 | } |
| 408 | |
| 409 | long significand = 0; |
| 410 | int digits = 0; |
| 411 | int fractionDigits = 0; |
| 412 | |
| 413 | while (index < length) { |
| 414 | char c = raw.charAt(index); |
| 415 | if (c < '0' || c > '9') { |
| 416 | break; |
| 417 | } |
| 418 | significand = significand * 10 + (c - '0'); |
| 419 | digits++; |
| 420 | index++; |
| 421 | } |
| 422 | |
| 423 | if (index < length && raw.charAt(index) == '.') { |
| 424 | index++; |
| 425 | while (index < length) { |
| 426 | char c = raw.charAt(index); |
| 427 | if (c < '0' || c > '9') { |
| 428 | break; |
| 429 | } |
| 430 | significand = significand * 10 + (c - '0'); |
| 431 | digits++; |
| 432 | fractionDigits++; |
| 433 | index++; |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | int exponent = 0; |
| 438 | if (index < length) { |
| 439 | // By construction the remainder is ('e' | 'E') ('+' | '-')? 1*DIGIT. |
| 440 | index++; |
| 441 | boolean exponentNegative = false; |
| 442 | char sign = raw.charAt(index); |
| 443 | if (sign == '+' || sign == '-') { |
| 444 | exponentNegative = sign == '-'; |
| 445 | index++; |
| 446 | } |
| 447 | while (index < length) { |
| 448 | // Clamp rather than overflow; anything this large falls back below anyway. |
| 449 | if (exponent < 100_000) { |
| 450 | exponent = exponent * 10 + (raw.charAt(index) - '0'); |
| 451 | } |
| 452 | index++; |
| 453 | } |
| 454 | if (exponentNegative) { |
| 455 | exponent = -exponent; |
| 456 | } |
| 457 | } |