OLD | NEW |
1 // Copyright 2010 the V8 project authors. All rights reserved. | 1 // Copyright 2010 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 13 matching lines...) Expand all Loading... |
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | 27 |
28 #include <limits.h> | 28 #include <limits.h> |
29 #include <math.h> | 29 #include <math.h> |
30 | 30 |
31 #include "double-conversion.h" | 31 #include "double-conversion.h" |
32 | 32 |
33 #include "bignum-dtoa.h" | 33 #include "bignum-dtoa.h" |
34 #include "double.h" | |
35 #include "fast-dtoa.h" | 34 #include "fast-dtoa.h" |
36 #include "fixed-dtoa.h" | 35 #include "fixed-dtoa.h" |
| 36 #include "ieee.h" |
37 #include "strtod.h" | 37 #include "strtod.h" |
38 #include "utils.h" | 38 #include "utils.h" |
39 | 39 |
40 namespace double_conversion { | 40 namespace double_conversion { |
41 | 41 |
42 const DoubleToStringConverter& DoubleToStringConverter::EcmaScriptConverter() { | 42 const DoubleToStringConverter& DoubleToStringConverter::EcmaScriptConverter() { |
43 int flags = UNIQUE_ZERO | EMIT_POSITIVE_EXPONENT_SIGN; | 43 int flags = UNIQUE_ZERO | EMIT_POSITIVE_EXPONENT_SIGN; |
44 static DoubleToStringConverter converter(flags, | 44 static DoubleToStringConverter converter(flags, |
45 "Infinity", | 45 "Infinity", |
46 "NaN", | 46 "NaN", |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
91 if ((flags_ & EMIT_POSITIVE_EXPONENT_SIGN) != 0) { | 91 if ((flags_ & EMIT_POSITIVE_EXPONENT_SIGN) != 0) { |
92 result_builder->AddCharacter('+'); | 92 result_builder->AddCharacter('+'); |
93 } | 93 } |
94 } | 94 } |
95 if (exponent == 0) { | 95 if (exponent == 0) { |
96 result_builder->AddCharacter('0'); | 96 result_builder->AddCharacter('0'); |
97 return; | 97 return; |
98 } | 98 } |
99 ASSERT(exponent < 1e4); | 99 ASSERT(exponent < 1e4); |
100 const int kMaxExponentLength = 5; | 100 const int kMaxExponentLength = 5; |
101 char buffer[kMaxExponentLength]; | 101 char buffer[kMaxExponentLength + 1]; |
| 102 buffer[kMaxExponentLength] = '\0'; |
102 int first_char_pos = kMaxExponentLength; | 103 int first_char_pos = kMaxExponentLength; |
103 while (exponent > 0) { | 104 while (exponent > 0) { |
104 buffer[--first_char_pos] = '0' + (exponent % 10); | 105 buffer[--first_char_pos] = '0' + (exponent % 10); |
105 exponent /= 10; | 106 exponent /= 10; |
106 } | 107 } |
107 result_builder->AddSubstring(&buffer[first_char_pos], | 108 result_builder->AddSubstring(&buffer[first_char_pos], |
108 kMaxExponentLength - first_char_pos); | 109 kMaxExponentLength - first_char_pos); |
109 } | 110 } |
110 | 111 |
111 | 112 |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
150 if ((flags_ & EMIT_TRAILING_DECIMAL_POINT) != 0) { | 151 if ((flags_ & EMIT_TRAILING_DECIMAL_POINT) != 0) { |
151 result_builder->AddCharacter('.'); | 152 result_builder->AddCharacter('.'); |
152 } | 153 } |
153 if ((flags_ & EMIT_TRAILING_ZERO_AFTER_POINT) != 0) { | 154 if ((flags_ & EMIT_TRAILING_ZERO_AFTER_POINT) != 0) { |
154 result_builder->AddCharacter('0'); | 155 result_builder->AddCharacter('0'); |
155 } | 156 } |
156 } | 157 } |
157 } | 158 } |
158 | 159 |
159 | 160 |
160 bool DoubleToStringConverter::ToShortest(double value, | 161 bool DoubleToStringConverter::ToShortestIeeeNumber( |
161 StringBuilder* result_builder) const { | 162 double value, |
| 163 StringBuilder* result_builder, |
| 164 DoubleToStringConverter::DtoaMode mode) const { |
| 165 ASSERT(mode == SHORTEST || mode == SHORTEST_SINGLE); |
162 if (Double(value).IsSpecial()) { | 166 if (Double(value).IsSpecial()) { |
163 return HandleSpecialValues(value, result_builder); | 167 return HandleSpecialValues(value, result_builder); |
164 } | 168 } |
165 | 169 |
166 int decimal_point; | 170 int decimal_point; |
167 bool sign; | 171 bool sign; |
168 const int kDecimalRepCapacity = kBase10MaximalLength + 1; | 172 const int kDecimalRepCapacity = kBase10MaximalLength + 1; |
169 char decimal_rep[kDecimalRepCapacity]; | 173 char decimal_rep[kDecimalRepCapacity]; |
170 int decimal_rep_length; | 174 int decimal_rep_length; |
171 | 175 |
172 DoubleToAscii(value, SHORTEST, 0, decimal_rep, kDecimalRepCapacity, | 176 DoubleToAscii(value, mode, 0, decimal_rep, kDecimalRepCapacity, |
173 &sign, &decimal_rep_length, &decimal_point); | 177 &sign, &decimal_rep_length, &decimal_point); |
174 | 178 |
175 bool unique_zero = (flags_ & UNIQUE_ZERO) != 0; | 179 bool unique_zero = (flags_ & UNIQUE_ZERO) != 0; |
176 if (sign && (value != 0.0 || !unique_zero)) { | 180 if (sign && (value != 0.0 || !unique_zero)) { |
177 result_builder->AddCharacter('-'); | 181 result_builder->AddCharacter('-'); |
178 } | 182 } |
179 | 183 |
180 int exponent = decimal_point - 1; | 184 int exponent = decimal_point - 1; |
181 if ((decimal_in_shortest_low_ <= exponent) && | 185 if ((decimal_in_shortest_low_ <= exponent) && |
182 (exponent < decimal_in_shortest_high_)) { | 186 (exponent < decimal_in_shortest_high_)) { |
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
331 result_builder); | 335 result_builder); |
332 } | 336 } |
333 return true; | 337 return true; |
334 } | 338 } |
335 | 339 |
336 | 340 |
337 static BignumDtoaMode DtoaToBignumDtoaMode( | 341 static BignumDtoaMode DtoaToBignumDtoaMode( |
338 DoubleToStringConverter::DtoaMode dtoa_mode) { | 342 DoubleToStringConverter::DtoaMode dtoa_mode) { |
339 switch (dtoa_mode) { | 343 switch (dtoa_mode) { |
340 case DoubleToStringConverter::SHORTEST: return BIGNUM_DTOA_SHORTEST; | 344 case DoubleToStringConverter::SHORTEST: return BIGNUM_DTOA_SHORTEST; |
| 345 case DoubleToStringConverter::SHORTEST_SINGLE: |
| 346 return BIGNUM_DTOA_SHORTEST_SINGLE; |
341 case DoubleToStringConverter::FIXED: return BIGNUM_DTOA_FIXED; | 347 case DoubleToStringConverter::FIXED: return BIGNUM_DTOA_FIXED; |
342 case DoubleToStringConverter::PRECISION: return BIGNUM_DTOA_PRECISION; | 348 case DoubleToStringConverter::PRECISION: return BIGNUM_DTOA_PRECISION; |
343 default: | 349 default: |
344 UNREACHABLE(); | 350 UNREACHABLE(); |
345 return BIGNUM_DTOA_SHORTEST; // To silence compiler. | |
346 } | 351 } |
347 } | 352 } |
348 | 353 |
349 | 354 |
350 void DoubleToStringConverter::DoubleToAscii(double v, | 355 void DoubleToStringConverter::DoubleToAscii(double v, |
351 DtoaMode mode, | 356 DtoaMode mode, |
352 int requested_digits, | 357 int requested_digits, |
353 char* buffer, | 358 char* buffer, |
354 int buffer_length, | 359 int buffer_length, |
355 bool* sign, | 360 bool* sign, |
356 int* length, | 361 int* length, |
357 int* point) { | 362 int* point) { |
358 Vector<char> vector(buffer, buffer_length); | 363 Vector<char> vector(buffer, buffer_length); |
359 ASSERT(!Double(v).IsSpecial()); | 364 ASSERT(!Double(v).IsSpecial()); |
360 ASSERT(mode == SHORTEST || requested_digits >= 0); | 365 ASSERT(mode == SHORTEST || mode == SHORTEST_SINGLE || requested_digits >= 0); |
361 | 366 |
362 if (Double(v).Sign() < 0) { | 367 if (Double(v).Sign() < 0) { |
363 *sign = true; | 368 *sign = true; |
364 v = -v; | 369 v = -v; |
365 } else { | 370 } else { |
366 *sign = false; | 371 *sign = false; |
367 } | 372 } |
368 | 373 |
369 if (mode == PRECISION && requested_digits == 0) { | 374 if (mode == PRECISION && requested_digits == 0) { |
370 vector[0] = '\0'; | 375 vector[0] = '\0'; |
371 *length = 0; | 376 *length = 0; |
372 return; | 377 return; |
373 } | 378 } |
374 | 379 |
375 if (v == 0) { | 380 if (v == 0) { |
376 vector[0] = '0'; | 381 vector[0] = '0'; |
377 vector[1] = '\0'; | 382 vector[1] = '\0'; |
378 *length = 1; | 383 *length = 1; |
379 *point = 1; | 384 *point = 1; |
380 return; | 385 return; |
381 } | 386 } |
382 | 387 |
383 bool fast_worked; | 388 bool fast_worked; |
384 switch (mode) { | 389 switch (mode) { |
385 case SHORTEST: | 390 case SHORTEST: |
386 fast_worked = FastDtoa(v, FAST_DTOA_SHORTEST, 0, vector, length, point); | 391 fast_worked = FastDtoa(v, FAST_DTOA_SHORTEST, 0, vector, length, point); |
387 break; | 392 break; |
| 393 case SHORTEST_SINGLE: |
| 394 fast_worked = FastDtoa(v, FAST_DTOA_SHORTEST_SINGLE, 0, |
| 395 vector, length, point); |
| 396 break; |
388 case FIXED: | 397 case FIXED: |
389 fast_worked = FastFixedDtoa(v, requested_digits, vector, length, point); | 398 fast_worked = FastFixedDtoa(v, requested_digits, vector, length, point); |
390 break; | 399 break; |
391 case PRECISION: | 400 case PRECISION: |
392 fast_worked = FastDtoa(v, FAST_DTOA_PRECISION, requested_digits, | 401 fast_worked = FastDtoa(v, FAST_DTOA_PRECISION, requested_digits, |
393 vector, length, point); | 402 vector, length, point); |
394 break; | 403 break; |
395 default: | 404 default: |
| 405 fast_worked = false; |
396 UNREACHABLE(); | 406 UNREACHABLE(); |
397 fast_worked = false; | |
398 } | 407 } |
399 if (fast_worked) return; | 408 if (fast_worked) return; |
400 | 409 |
401 // If the fast dtoa didn't succeed use the slower bignum version. | 410 // If the fast dtoa didn't succeed use the slower bignum version. |
402 BignumDtoaMode bignum_mode = DtoaToBignumDtoaMode(mode); | 411 BignumDtoaMode bignum_mode = DtoaToBignumDtoaMode(mode); |
403 BignumDtoa(v, bignum_mode, requested_digits, vector, length, point); | 412 BignumDtoa(v, bignum_mode, requested_digits, vector, length, point); |
404 vector[*length] = '\0'; | 413 vector[*length] = '\0'; |
405 } | 414 } |
406 | 415 |
407 | 416 |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
445 || (radix > 10 && x >= 'a' && x < 'a' + radix - 10) | 454 || (radix > 10 && x >= 'a' && x < 'a' + radix - 10) |
446 || (radix > 10 && x >= 'A' && x < 'A' + radix - 10); | 455 || (radix > 10 && x >= 'A' && x < 'A' + radix - 10); |
447 } | 456 } |
448 | 457 |
449 | 458 |
450 static double SignedZero(bool sign) { | 459 static double SignedZero(bool sign) { |
451 return sign ? -0.0 : 0.0; | 460 return sign ? -0.0 : 0.0; |
452 } | 461 } |
453 | 462 |
454 | 463 |
| 464 // Returns true if 'c' is a decimal digit that is valid for the given radix. |
| 465 // |
| 466 // The function is small and could be inlined, but VS2012 emitted a warning |
| 467 // because it constant-propagated the radix and concluded that the last |
| 468 // condition was always true. By moving it into a separate function the |
| 469 // compiler wouldn't warn anymore. |
| 470 static bool IsDecimalDigitForRadix(int c, int radix) { |
| 471 return '0' <= c && c <= '9' && (c - '0') < radix; |
| 472 } |
| 473 |
| 474 // Returns true if 'c' is a character digit that is valid for the given radix. |
| 475 // The 'a_character' should be 'a' or 'A'. |
| 476 // |
| 477 // The function is small and could be inlined, but VS2012 emitted a warning |
| 478 // because it constant-propagated the radix and concluded that the first |
| 479 // condition was always false. By moving it into a separate function the |
| 480 // compiler wouldn't warn anymore. |
| 481 static bool IsCharacterDigitForRadix(int c, int radix, char a_character) { |
| 482 return radix > 10 && c >= a_character && c < a_character + radix - 10; |
| 483 } |
| 484 |
| 485 |
455 // Parsing integers with radix 2, 4, 8, 16, 32. Assumes current != end. | 486 // Parsing integers with radix 2, 4, 8, 16, 32. Assumes current != end. |
456 template <int radix_log_2> | 487 template <int radix_log_2> |
457 static double RadixStringToDouble(const char* current, | 488 static double RadixStringToIeee(const char* current, |
458 const char* end, | 489 const char* end, |
459 bool sign, | 490 bool sign, |
460 bool allow_trailing_junk, | 491 bool allow_trailing_junk, |
461 double junk_string_value, | 492 double junk_string_value, |
462 const char** trailing_pointer) { | 493 bool read_as_double, |
| 494 const char** trailing_pointer) { |
463 ASSERT(current != end); | 495 ASSERT(current != end); |
464 | 496 |
| 497 const int kDoubleSize = Double::kSignificandSize; |
| 498 const int kSingleSize = Single::kSignificandSize; |
| 499 const int kSignificandSize = read_as_double? kDoubleSize: kSingleSize; |
| 500 |
465 // Skip leading 0s. | 501 // Skip leading 0s. |
466 while (*current == '0') { | 502 while (*current == '0') { |
467 ++current; | 503 ++current; |
468 if (current == end) { | 504 if (current == end) { |
469 *trailing_pointer = end; | 505 *trailing_pointer = end; |
470 return SignedZero(sign); | 506 return SignedZero(sign); |
471 } | 507 } |
472 } | 508 } |
473 | 509 |
474 int64_t number = 0; | 510 int64_t number = 0; |
475 int exponent = 0; | 511 int exponent = 0; |
476 const int radix = (1 << radix_log_2); | 512 const int radix = (1 << radix_log_2); |
477 | 513 |
478 do { | 514 do { |
479 int digit; | 515 int digit; |
480 if (*current >= '0' && *current <= '9' && *current < '0' + radix) { | 516 if (IsDecimalDigitForRadix(*current, radix)) { |
481 digit = static_cast<char>(*current) - '0'; | 517 digit = static_cast<char>(*current) - '0'; |
482 } else if (radix > 10 && *current >= 'a' && *current < 'a' + radix - 10) { | 518 } else if (IsCharacterDigitForRadix(*current, radix, 'a')) { |
483 digit = static_cast<char>(*current) - 'a' + 10; | 519 digit = static_cast<char>(*current) - 'a' + 10; |
484 } else if (radix > 10 && *current >= 'A' && *current < 'A' + radix - 10) { | 520 } else if (IsCharacterDigitForRadix(*current, radix, 'A')) { |
485 digit = static_cast<char>(*current) - 'A' + 10; | 521 digit = static_cast<char>(*current) - 'A' + 10; |
486 } else { | 522 } else { |
487 if (allow_trailing_junk || !AdvanceToNonspace(¤t, end)) { | 523 if (allow_trailing_junk || !AdvanceToNonspace(¤t, end)) { |
488 break; | 524 break; |
489 } else { | 525 } else { |
490 return junk_string_value; | 526 return junk_string_value; |
491 } | 527 } |
492 } | 528 } |
493 | 529 |
494 number = number * radix + digit; | 530 number = number * radix + digit; |
495 int overflow = static_cast<int>(number >> 53); | 531 int overflow = static_cast<int>(number >> kSignificandSize); |
496 if (overflow != 0) { | 532 if (overflow != 0) { |
497 // Overflow occurred. Need to determine which direction to round the | 533 // Overflow occurred. Need to determine which direction to round the |
498 // result. | 534 // result. |
499 int overflow_bits_count = 1; | 535 int overflow_bits_count = 1; |
500 while (overflow > 1) { | 536 while (overflow > 1) { |
501 overflow_bits_count++; | 537 overflow_bits_count++; |
502 overflow >>= 1; | 538 overflow >>= 1; |
503 } | 539 } |
504 | 540 |
505 int dropped_bits_mask = ((1 << overflow_bits_count) - 1); | 541 int dropped_bits_mask = ((1 << overflow_bits_count) - 1); |
506 int dropped_bits = static_cast<int>(number) & dropped_bits_mask; | 542 int dropped_bits = static_cast<int>(number) & dropped_bits_mask; |
507 number >>= overflow_bits_count; | 543 number >>= overflow_bits_count; |
508 exponent = overflow_bits_count; | 544 exponent = overflow_bits_count; |
509 | 545 |
510 bool zero_tail = true; | 546 bool zero_tail = true; |
511 while (true) { | 547 for (;;) { |
512 ++current; | 548 ++current; |
513 if (current == end || !isDigit(*current, radix)) break; | 549 if (current == end || !isDigit(*current, radix)) break; |
514 zero_tail = zero_tail && *current == '0'; | 550 zero_tail = zero_tail && *current == '0'; |
515 exponent += radix_log_2; | 551 exponent += radix_log_2; |
516 } | 552 } |
517 | 553 |
518 if (!allow_trailing_junk && AdvanceToNonspace(¤t, end)) { | 554 if (!allow_trailing_junk && AdvanceToNonspace(¤t, end)) { |
519 return junk_string_value; | 555 return junk_string_value; |
520 } | 556 } |
521 | 557 |
522 int middle_value = (1 << (overflow_bits_count - 1)); | 558 int middle_value = (1 << (overflow_bits_count - 1)); |
523 if (dropped_bits > middle_value) { | 559 if (dropped_bits > middle_value) { |
524 number++; // Rounding up. | 560 number++; // Rounding up. |
525 } else if (dropped_bits == middle_value) { | 561 } else if (dropped_bits == middle_value) { |
526 // Rounding to even to consistency with decimals: half-way case rounds | 562 // Rounding to even to consistency with decimals: half-way case rounds |
527 // up if significant part is odd and down otherwise. | 563 // up if significant part is odd and down otherwise. |
528 if ((number & 1) != 0 || !zero_tail) { | 564 if ((number & 1) != 0 || !zero_tail) { |
529 number++; // Rounding up. | 565 number++; // Rounding up. |
530 } | 566 } |
531 } | 567 } |
532 | 568 |
533 // Rounding up may cause overflow. | 569 // Rounding up may cause overflow. |
534 if ((number & ((int64_t)1 << 53)) != 0) { | 570 if ((number & ((int64_t)1 << kSignificandSize)) != 0) { |
535 exponent++; | 571 exponent++; |
536 number >>= 1; | 572 number >>= 1; |
537 } | 573 } |
538 break; | 574 break; |
539 } | 575 } |
540 ++current; | 576 ++current; |
541 } while (current != end); | 577 } while (current != end); |
542 | 578 |
543 ASSERT(number < ((int64_t)1 << 53)); | 579 ASSERT(number < ((int64_t)1 << kSignificandSize)); |
544 ASSERT(static_cast<int64_t>(static_cast<double>(number)) == number); | 580 ASSERT(static_cast<int64_t>(static_cast<double>(number)) == number); |
545 | 581 |
546 *trailing_pointer = current; | 582 *trailing_pointer = current; |
547 | 583 |
548 if (exponent == 0) { | 584 if (exponent == 0) { |
549 if (sign) { | 585 if (sign) { |
550 if (number == 0) return -0.0; | 586 if (number == 0) return -0.0; |
551 number = -number; | 587 number = -number; |
552 } | 588 } |
553 return static_cast<double>(number); | 589 return static_cast<double>(number); |
554 } | 590 } |
555 | 591 |
556 ASSERT(number != 0); | 592 ASSERT(number != 0); |
557 return Double(DiyFp(number, exponent)).value(); | 593 return Double(DiyFp(number, exponent)).value(); |
558 } | 594 } |
559 | 595 |
560 | 596 |
561 double StringToDoubleConverter::StringToDouble( | 597 double StringToDoubleConverter::StringToIeee( |
562 const char* input, | 598 const char* input, |
563 int length, | 599 int length, |
564 int* processed_characters_count) { | 600 int* processed_characters_count, |
| 601 bool read_as_double) const { |
565 const char* current = input; | 602 const char* current = input; |
566 const char* end = input + length; | 603 const char* end = input + length; |
567 | 604 |
568 *processed_characters_count = 0; | 605 *processed_characters_count = 0; |
569 | 606 |
570 const bool allow_trailing_junk = (flags_ & ALLOW_TRAILING_JUNK) != 0; | 607 const bool allow_trailing_junk = (flags_ & ALLOW_TRAILING_JUNK) != 0; |
571 const bool allow_leading_spaces = (flags_ & ALLOW_LEADING_SPACES) != 0; | 608 const bool allow_leading_spaces = (flags_ & ALLOW_LEADING_SPACES) != 0; |
572 const bool allow_trailing_spaces = (flags_ & ALLOW_TRAILING_SPACES) != 0; | 609 const bool allow_trailing_spaces = (flags_ & ALLOW_TRAILING_SPACES) != 0; |
573 const bool allow_spaces_after_sign = (flags_ & ALLOW_SPACES_AFTER_SIGN) != 0; | 610 const bool allow_spaces_after_sign = (flags_ & ALLOW_SPACES_AFTER_SIGN) != 0; |
574 | 611 |
575 // To make sure that iterator dereferencing is valid the following | 612 // To make sure that iterator dereferencing is valid the following |
576 // convention is used: | 613 // convention is used: |
577 // 1. Each '++current' statement is followed by check for equality to 'end'. | 614 // 1. Each '++current' statement is followed by check for equality to 'end'. |
578 // 2. If AdvanceToNonspace returned false then current == end. | 615 // 2. If AdvanceToNonspace returned false then current == end. |
579 // 3. If 'current' becomes equal to 'end' the function returns or goes to | 616 // 3. If 'current' becomes equal to 'end' the function returns or goes to |
580 // 'parsing_done'. | 617 // 'parsing_done'. |
581 // 4. 'current' is not dereferenced after the 'parsing_done' label. | 618 // 4. 'current' is not dereferenced after the 'parsing_done' label. |
582 // 5. Code before 'parsing_done' may rely on 'current != end'. | 619 // 5. Code before 'parsing_done' may rely on 'current != end'. |
583 if (current == end) return empty_string_value_; | 620 if (current == end) return empty_string_value_; |
584 | 621 |
585 if (allow_leading_spaces || allow_trailing_spaces) { | 622 if (allow_leading_spaces || allow_trailing_spaces) { |
586 if (!AdvanceToNonspace(¤t, end)) { | 623 if (!AdvanceToNonspace(¤t, end)) { |
587 *processed_characters_count = current - input; | 624 *processed_characters_count = static_cast<int>(current - input); |
588 return empty_string_value_; | 625 return empty_string_value_; |
589 } | 626 } |
590 if (!allow_leading_spaces && (input != current)) { | 627 if (!allow_leading_spaces && (input != current)) { |
591 // No leading spaces allowed, but AdvanceToNonspace moved forward. | 628 // No leading spaces allowed, but AdvanceToNonspace moved forward. |
592 return junk_string_value_; | 629 return junk_string_value_; |
593 } | 630 } |
594 } | 631 } |
595 | 632 |
596 // The longest form of simplified number is: "-<significant digits>.1eXXX\0". | 633 // The longest form of simplified number is: "-<significant digits>.1eXXX\0". |
597 const int kBufferSize = kMaxSignificantDigits + 10; | 634 const int kBufferSize = kMaxSignificantDigits + 10; |
(...skipping 28 matching lines...) Expand all Loading... |
626 } | 663 } |
627 | 664 |
628 if (!(allow_trailing_spaces || allow_trailing_junk) && (current != end)) { | 665 if (!(allow_trailing_spaces || allow_trailing_junk) && (current != end)) { |
629 return junk_string_value_; | 666 return junk_string_value_; |
630 } | 667 } |
631 if (!allow_trailing_junk && AdvanceToNonspace(¤t, end)) { | 668 if (!allow_trailing_junk && AdvanceToNonspace(¤t, end)) { |
632 return junk_string_value_; | 669 return junk_string_value_; |
633 } | 670 } |
634 | 671 |
635 ASSERT(buffer_pos == 0); | 672 ASSERT(buffer_pos == 0); |
636 *processed_characters_count = current - input; | 673 *processed_characters_count = static_cast<int>(current - input); |
637 return sign ? -Double::Infinity() : Double::Infinity(); | 674 return sign ? -Double::Infinity() : Double::Infinity(); |
638 } | 675 } |
639 } | 676 } |
640 | 677 |
641 if (nan_symbol_ != NULL) { | 678 if (nan_symbol_ != NULL) { |
642 if (*current == nan_symbol_[0]) { | 679 if (*current == nan_symbol_[0]) { |
643 if (!ConsumeSubString(¤t, end, nan_symbol_)) { | 680 if (!ConsumeSubString(¤t, end, nan_symbol_)) { |
644 return junk_string_value_; | 681 return junk_string_value_; |
645 } | 682 } |
646 | 683 |
647 if (!(allow_trailing_spaces || allow_trailing_junk) && (current != end)) { | 684 if (!(allow_trailing_spaces || allow_trailing_junk) && (current != end)) { |
648 return junk_string_value_; | 685 return junk_string_value_; |
649 } | 686 } |
650 if (!allow_trailing_junk && AdvanceToNonspace(¤t, end)) { | 687 if (!allow_trailing_junk && AdvanceToNonspace(¤t, end)) { |
651 return junk_string_value_; | 688 return junk_string_value_; |
652 } | 689 } |
653 | 690 |
654 ASSERT(buffer_pos == 0); | 691 ASSERT(buffer_pos == 0); |
655 *processed_characters_count = current - input; | 692 *processed_characters_count = static_cast<int>(current - input); |
656 return sign ? -Double::NaN() : Double::NaN(); | 693 return sign ? -Double::NaN() : Double::NaN(); |
657 } | 694 } |
658 } | 695 } |
659 | 696 |
660 bool leading_zero = false; | 697 bool leading_zero = false; |
661 if (*current == '0') { | 698 if (*current == '0') { |
662 ++current; | 699 ++current; |
663 if (current == end) { | 700 if (current == end) { |
664 *processed_characters_count = current - input; | 701 *processed_characters_count = static_cast<int>(current - input); |
665 return SignedZero(sign); | 702 return SignedZero(sign); |
666 } | 703 } |
667 | 704 |
668 leading_zero = true; | 705 leading_zero = true; |
669 | 706 |
670 // It could be hexadecimal value. | 707 // It could be hexadecimal value. |
671 if ((flags_ & ALLOW_HEX) && (*current == 'x' || *current == 'X')) { | 708 if ((flags_ & ALLOW_HEX) && (*current == 'x' || *current == 'X')) { |
672 ++current; | 709 ++current; |
673 if (current == end || !isDigit(*current, 16)) { | 710 if (current == end || !isDigit(*current, 16)) { |
674 return junk_string_value_; // "0x". | 711 return junk_string_value_; // "0x". |
675 } | 712 } |
676 | 713 |
677 const char* tail_pointer = NULL; | 714 const char* tail_pointer = NULL; |
678 double result = RadixStringToDouble<4>(current, | 715 double result = RadixStringToIeee<4>(current, |
679 end, | 716 end, |
680 sign, | 717 sign, |
681 allow_trailing_junk, | 718 allow_trailing_junk, |
682 junk_string_value_, | 719 junk_string_value_, |
683 &tail_pointer); | 720 read_as_double, |
| 721 &tail_pointer); |
684 if (tail_pointer != NULL) { | 722 if (tail_pointer != NULL) { |
685 if (allow_trailing_spaces) AdvanceToNonspace(&tail_pointer, end); | 723 if (allow_trailing_spaces) AdvanceToNonspace(&tail_pointer, end); |
686 *processed_characters_count = tail_pointer - input; | 724 *processed_characters_count = static_cast<int>(tail_pointer - input); |
687 } | 725 } |
688 return result; | 726 return result; |
689 } | 727 } |
690 | 728 |
691 // Ignore leading zeros in the integer part. | 729 // Ignore leading zeros in the integer part. |
692 while (*current == '0') { | 730 while (*current == '0') { |
693 ++current; | 731 ++current; |
694 if (current == end) { | 732 if (current == end) { |
695 *processed_characters_count = current - input; | 733 *processed_characters_count = static_cast<int>(current - input); |
696 return SignedZero(sign); | 734 return SignedZero(sign); |
697 } | 735 } |
698 } | 736 } |
699 } | 737 } |
700 | 738 |
701 bool octal = leading_zero && (flags_ & ALLOW_OCTALS) != 0; | 739 bool octal = leading_zero && (flags_ & ALLOW_OCTALS) != 0; |
702 | 740 |
703 // Copy significant digits of the integer part (if any) to the buffer. | 741 // Copy significant digits of the integer part (if any) to the buffer. |
704 while (*current >= '0' && *current <= '9') { | 742 while (*current >= '0' && *current <= '9') { |
705 if (significant_digits < kMaxSignificantDigits) { | 743 if (significant_digits < kMaxSignificantDigits) { |
(...skipping 27 matching lines...) Expand all Loading... |
733 } | 771 } |
734 } | 772 } |
735 | 773 |
736 if (significant_digits == 0) { | 774 if (significant_digits == 0) { |
737 // octal = false; | 775 // octal = false; |
738 // Integer part consists of 0 or is absent. Significant digits start after | 776 // Integer part consists of 0 or is absent. Significant digits start after |
739 // leading zeros (if any). | 777 // leading zeros (if any). |
740 while (*current == '0') { | 778 while (*current == '0') { |
741 ++current; | 779 ++current; |
742 if (current == end) { | 780 if (current == end) { |
743 *processed_characters_count = current - input; | 781 *processed_characters_count = static_cast<int>(current - input); |
744 return SignedZero(sign); | 782 return SignedZero(sign); |
745 } | 783 } |
746 exponent--; // Move this 0 into the exponent. | 784 exponent--; // Move this 0 into the exponent. |
747 } | 785 } |
748 } | 786 } |
749 | 787 |
750 // There is a fractional part. | 788 // There is a fractional part. |
751 // We don't emit a '.', but adjust the exponent instead. | 789 // We don't emit a '.', but adjust the exponent instead. |
752 while (*current >= '0' && *current <= '9') { | 790 while (*current >= '0' && *current <= '9') { |
753 if (significant_digits < kMaxSignificantDigits) { | 791 if (significant_digits < kMaxSignificantDigits) { |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
832 if (allow_trailing_spaces) { | 870 if (allow_trailing_spaces) { |
833 AdvanceToNonspace(¤t, end); | 871 AdvanceToNonspace(¤t, end); |
834 } | 872 } |
835 | 873 |
836 parsing_done: | 874 parsing_done: |
837 exponent += insignificant_digits; | 875 exponent += insignificant_digits; |
838 | 876 |
839 if (octal) { | 877 if (octal) { |
840 double result; | 878 double result; |
841 const char* tail_pointer = NULL; | 879 const char* tail_pointer = NULL; |
842 result = RadixStringToDouble<3>(buffer, | 880 result = RadixStringToIeee<3>(buffer, |
843 buffer + buffer_pos, | 881 buffer + buffer_pos, |
844 sign, | 882 sign, |
845 allow_trailing_junk, | 883 allow_trailing_junk, |
846 junk_string_value_, | 884 junk_string_value_, |
847 &tail_pointer); | 885 read_as_double, |
| 886 &tail_pointer); |
848 ASSERT(tail_pointer != NULL); | 887 ASSERT(tail_pointer != NULL); |
849 *processed_characters_count = current - input; | 888 *processed_characters_count = static_cast<int>(current - input); |
850 return result; | 889 return result; |
851 } | 890 } |
852 | 891 |
853 if (nonzero_digit_dropped) { | 892 if (nonzero_digit_dropped) { |
854 buffer[buffer_pos++] = '1'; | 893 buffer[buffer_pos++] = '1'; |
855 exponent--; | 894 exponent--; |
856 } | 895 } |
857 | 896 |
858 ASSERT(buffer_pos < kBufferSize); | 897 ASSERT(buffer_pos < kBufferSize); |
859 buffer[buffer_pos] = '\0'; | 898 buffer[buffer_pos] = '\0'; |
860 | 899 |
861 double converted = Strtod(Vector<const char>(buffer, buffer_pos), exponent); | 900 double converted; |
862 *processed_characters_count = current - input; | 901 if (read_as_double) { |
| 902 converted = Strtod(Vector<const char>(buffer, buffer_pos), exponent); |
| 903 } else { |
| 904 converted = Strtof(Vector<const char>(buffer, buffer_pos), exponent); |
| 905 } |
| 906 *processed_characters_count = static_cast<int>(current - input); |
863 return sign? -converted: converted; | 907 return sign? -converted: converted; |
864 } | 908 } |
865 | 909 |
866 } // namespace double_conversion | 910 } // namespace double_conversion |
OLD | NEW |