1
// Copyright 2010 the V8 project authors. All rights reserved.
2
// Redistribution and use in source and binary forms, with or without
3
// modification, are permitted provided that the following conditions are
6
// * Redistributions of source code must retain the above copyright
7
// notice, this list of conditions and the following disclaimer.
8
// * Redistributions in binary form must reproduce the above
9
// copyright notice, this list of conditions and the following
10
// disclaimer in the documentation and/or other materials provided
11
// with the distribution.
12
// * Neither the name of Google Inc. nor the names of its
13
// contributors may be used to endorse or promote products derived
14
// from this software without specific prior written permission.
16
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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.
31
#include "double-conversion.h"
33
#include "bignum-dtoa.h"
34
#include "fast-dtoa.h"
35
#include "fixed-dtoa.h"
40
namespace double_conversion {
42
const DoubleToStringConverter& DoubleToStringConverter::EcmaScriptConverter() {
43
int flags = UNIQUE_ZERO | EMIT_POSITIVE_EXPONENT_SIGN;
44
static DoubleToStringConverter converter(flags,
54
bool DoubleToStringConverter::HandleSpecialValues(
56
StringBuilder* result_builder) const {
57
Double double_inspect(value);
58
if (double_inspect.IsInfinite()) {
59
if (infinity_symbol_ == NULL) return false;
61
result_builder->AddCharacter('-');
63
result_builder->AddString(infinity_symbol_);
66
if (double_inspect.IsNan()) {
67
if (nan_symbol_ == NULL) return false;
68
result_builder->AddString(nan_symbol_);
75
void DoubleToStringConverter::CreateExponentialRepresentation(
76
const char* decimal_digits,
79
StringBuilder* result_builder) const {
81
result_builder->AddCharacter(decimal_digits[0]);
83
result_builder->AddCharacter('.');
84
result_builder->AddSubstring(&decimal_digits[1], length-1);
86
result_builder->AddCharacter(exponent_character_);
88
result_builder->AddCharacter('-');
91
if ((flags_ & EMIT_POSITIVE_EXPONENT_SIGN) != 0) {
92
result_builder->AddCharacter('+');
96
result_builder->AddCharacter('0');
99
ASSERT(exponent < 1e4);
100
const int kMaxExponentLength = 5;
101
char buffer[kMaxExponentLength];
102
int first_char_pos = kMaxExponentLength;
103
while (exponent > 0) {
104
buffer[--first_char_pos] = '0' + (exponent % 10);
107
result_builder->AddSubstring(&buffer[first_char_pos],
108
kMaxExponentLength - first_char_pos);
112
void DoubleToStringConverter::CreateDecimalRepresentation(
113
const char* decimal_digits,
116
int digits_after_point,
117
StringBuilder* result_builder) const {
118
// Create a representation that is padded with zeros if needed.
119
if (decimal_point <= 0) {
120
// "0.00000decimal_rep".
121
result_builder->AddCharacter('0');
122
if (digits_after_point > 0) {
123
result_builder->AddCharacter('.');
124
result_builder->AddPadding('0', -decimal_point);
125
ASSERT(length <= digits_after_point - (-decimal_point));
126
result_builder->AddSubstring(decimal_digits, length);
127
int remaining_digits = digits_after_point - (-decimal_point) - length;
128
result_builder->AddPadding('0', remaining_digits);
130
} else if (decimal_point >= length) {
131
// "decimal_rep0000.00000" or "decimal_rep.0000"
132
result_builder->AddSubstring(decimal_digits, length);
133
result_builder->AddPadding('0', decimal_point - length);
134
if (digits_after_point > 0) {
135
result_builder->AddCharacter('.');
136
result_builder->AddPadding('0', digits_after_point);
140
ASSERT(digits_after_point > 0);
141
result_builder->AddSubstring(decimal_digits, decimal_point);
142
result_builder->AddCharacter('.');
143
ASSERT(length - decimal_point <= digits_after_point);
144
result_builder->AddSubstring(&decimal_digits[decimal_point],
145
length - decimal_point);
146
int remaining_digits = digits_after_point - (length - decimal_point);
147
result_builder->AddPadding('0', remaining_digits);
149
if (digits_after_point == 0) {
150
if ((flags_ & EMIT_TRAILING_DECIMAL_POINT) != 0) {
151
result_builder->AddCharacter('.');
153
if ((flags_ & EMIT_TRAILING_ZERO_AFTER_POINT) != 0) {
154
result_builder->AddCharacter('0');
160
bool DoubleToStringConverter::ToShortestIeeeNumber(
162
StringBuilder* result_builder,
163
DoubleToStringConverter::DtoaMode mode) const {
164
assert(mode == SHORTEST || mode == SHORTEST_SINGLE);
165
if (Double(value).IsSpecial()) {
166
return HandleSpecialValues(value, result_builder);
171
const int kDecimalRepCapacity = kBase10MaximalLength + 1;
172
char decimal_rep[kDecimalRepCapacity];
173
int decimal_rep_length;
175
DoubleToAscii(value, mode, 0, decimal_rep, kDecimalRepCapacity,
176
&sign, &decimal_rep_length, &decimal_point);
178
bool unique_zero = (flags_ & UNIQUE_ZERO) != 0;
179
if (sign && (value != 0.0 || !unique_zero)) {
180
result_builder->AddCharacter('-');
183
int exponent = decimal_point - 1;
184
if ((decimal_in_shortest_low_ <= exponent) &&
185
(exponent < decimal_in_shortest_high_)) {
186
CreateDecimalRepresentation(decimal_rep, decimal_rep_length,
188
Max(0, decimal_rep_length - decimal_point),
191
CreateExponentialRepresentation(decimal_rep, decimal_rep_length, exponent,
198
bool DoubleToStringConverter::ToFixed(double value,
199
int requested_digits,
200
StringBuilder* result_builder) const {
201
ASSERT(kMaxFixedDigitsBeforePoint == 60);
202
const double kFirstNonFixed = 1e60;
204
if (Double(value).IsSpecial()) {
205
return HandleSpecialValues(value, result_builder);
208
if (requested_digits > kMaxFixedDigitsAfterPoint) return false;
209
if (value >= kFirstNonFixed || value <= -kFirstNonFixed) return false;
211
// Find a sufficiently precise decimal representation of n.
214
// Add space for the '\0' byte.
215
const int kDecimalRepCapacity =
216
kMaxFixedDigitsBeforePoint + kMaxFixedDigitsAfterPoint + 1;
217
char decimal_rep[kDecimalRepCapacity];
218
int decimal_rep_length;
219
DoubleToAscii(value, FIXED, requested_digits,
220
decimal_rep, kDecimalRepCapacity,
221
&sign, &decimal_rep_length, &decimal_point);
223
bool unique_zero = ((flags_ & UNIQUE_ZERO) != 0);
224
if (sign && (value != 0.0 || !unique_zero)) {
225
result_builder->AddCharacter('-');
228
CreateDecimalRepresentation(decimal_rep, decimal_rep_length, decimal_point,
229
requested_digits, result_builder);
234
bool DoubleToStringConverter::ToExponential(
236
int requested_digits,
237
StringBuilder* result_builder) const {
238
if (Double(value).IsSpecial()) {
239
return HandleSpecialValues(value, result_builder);
242
if (requested_digits < -1) return false;
243
if (requested_digits > kMaxExponentialDigits) return false;
247
// Add space for digit before the decimal point and the '\0' character.
248
const int kDecimalRepCapacity = kMaxExponentialDigits + 2;
249
ASSERT(kDecimalRepCapacity > kBase10MaximalLength);
250
char decimal_rep[kDecimalRepCapacity];
251
int decimal_rep_length;
253
if (requested_digits == -1) {
254
DoubleToAscii(value, SHORTEST, 0,
255
decimal_rep, kDecimalRepCapacity,
256
&sign, &decimal_rep_length, &decimal_point);
258
DoubleToAscii(value, PRECISION, requested_digits + 1,
259
decimal_rep, kDecimalRepCapacity,
260
&sign, &decimal_rep_length, &decimal_point);
261
ASSERT(decimal_rep_length <= requested_digits + 1);
263
for (int i = decimal_rep_length; i < requested_digits + 1; ++i) {
264
decimal_rep[i] = '0';
266
decimal_rep_length = requested_digits + 1;
269
bool unique_zero = ((flags_ & UNIQUE_ZERO) != 0);
270
if (sign && (value != 0.0 || !unique_zero)) {
271
result_builder->AddCharacter('-');
274
int exponent = decimal_point - 1;
275
CreateExponentialRepresentation(decimal_rep,
283
bool DoubleToStringConverter::ToPrecision(double value,
285
StringBuilder* result_builder) const {
286
if (Double(value).IsSpecial()) {
287
return HandleSpecialValues(value, result_builder);
290
if (precision < kMinPrecisionDigits || precision > kMaxPrecisionDigits) {
294
// Find a sufficiently precise decimal representation of n.
297
// Add one for the terminating null character.
298
const int kDecimalRepCapacity = kMaxPrecisionDigits + 1;
299
char decimal_rep[kDecimalRepCapacity];
300
int decimal_rep_length;
302
DoubleToAscii(value, PRECISION, precision,
303
decimal_rep, kDecimalRepCapacity,
304
&sign, &decimal_rep_length, &decimal_point);
305
ASSERT(decimal_rep_length <= precision);
307
bool unique_zero = ((flags_ & UNIQUE_ZERO) != 0);
308
if (sign && (value != 0.0 || !unique_zero)) {
309
result_builder->AddCharacter('-');
312
// The exponent if we print the number as x.xxeyyy. That is with the
313
// decimal point after the first digit.
314
int exponent = decimal_point - 1;
316
int extra_zero = ((flags_ & EMIT_TRAILING_ZERO_AFTER_POINT) != 0) ? 1 : 0;
317
if ((-decimal_point + 1 > max_leading_padding_zeroes_in_precision_mode_) ||
318
(decimal_point - precision + extra_zero >
319
max_trailing_padding_zeroes_in_precision_mode_)) {
320
// Fill buffer to contain 'precision' digits.
321
// Usually the buffer is already at the correct length, but 'DoubleToAscii'
322
// is allowed to return less characters.
323
for (int i = decimal_rep_length; i < precision; ++i) {
324
decimal_rep[i] = '0';
327
CreateExponentialRepresentation(decimal_rep,
332
CreateDecimalRepresentation(decimal_rep, decimal_rep_length, decimal_point,
333
Max(0, precision - decimal_point),
340
static BignumDtoaMode DtoaToBignumDtoaMode(
341
DoubleToStringConverter::DtoaMode dtoa_mode) {
343
case DoubleToStringConverter::SHORTEST: return BIGNUM_DTOA_SHORTEST;
344
case DoubleToStringConverter::SHORTEST_SINGLE:
345
return BIGNUM_DTOA_SHORTEST_SINGLE;
346
case DoubleToStringConverter::FIXED: return BIGNUM_DTOA_FIXED;
347
case DoubleToStringConverter::PRECISION: return BIGNUM_DTOA_PRECISION;
350
return BIGNUM_DTOA_SHORTEST; // To silence compiler.
355
void DoubleToStringConverter::DoubleToAscii(double v,
357
int requested_digits,
363
Vector<char> vector(buffer, buffer_length);
364
ASSERT(!Double(v).IsSpecial());
365
ASSERT(mode == SHORTEST || mode == SHORTEST_SINGLE || requested_digits >= 0);
367
if (Double(v).Sign() < 0) {
374
if (mode == PRECISION && requested_digits == 0) {
391
fast_worked = FastDtoa(v, FAST_DTOA_SHORTEST, 0, vector, length, point);
393
case SHORTEST_SINGLE:
394
fast_worked = FastDtoa(v, FAST_DTOA_SHORTEST_SINGLE, 0,
395
vector, length, point);
398
fast_worked = FastFixedDtoa(v, requested_digits, vector, length, point);
401
fast_worked = FastDtoa(v, FAST_DTOA_PRECISION, requested_digits,
402
vector, length, point);
408
if (fast_worked) return;
410
// If the fast dtoa didn't succeed use the slower bignum version.
411
BignumDtoaMode bignum_mode = DtoaToBignumDtoaMode(mode);
412
BignumDtoa(v, bignum_mode, requested_digits, vector, length, point);
413
vector[*length] = '\0';
417
// Consumes the given substring from the iterator.
418
// Returns false, if the substring does not match.
419
static bool ConsumeSubString(const char** current,
421
const char* substring) {
422
ASSERT(**current == *substring);
423
for (substring++; *substring != '\0'; substring++) {
425
if (*current == end || **current != *substring) return false;
432
// Maximum number of significant digits in decimal representation.
433
// The longest possible double in decimal representation is
434
// (2^53 - 1) * 2 ^ -1074 that is (2 ^ 53 - 1) * 5 ^ 1074 / 10 ^ 1074
435
// (768 digits). If we parse a number whose first digits are equal to a
436
// mean of 2 adjacent doubles (that could have up to 769 digits) the result
437
// must be rounded to the bigger one unless the tail consists of zeros, so
438
// we don't need to preserve all the digits.
439
const int kMaxSignificantDigits = 772;
442
// Returns true if a nonspace found and false if the end has reached.
443
static inline bool AdvanceToNonspace(const char** current, const char* end) {
444
while (*current != end) {
445
if (**current != ' ') return true;
452
static bool isDigit(int x, int radix) {
453
return (x >= '0' && x <= '9' && x < '0' + radix)
454
|| (radix > 10 && x >= 'a' && x < 'a' + radix - 10)
455
|| (radix > 10 && x >= 'A' && x < 'A' + radix - 10);
459
static double SignedZero(bool sign) {
460
return sign ? -0.0 : 0.0;
464
// Parsing integers with radix 2, 4, 8, 16, 32. Assumes current != end.
465
template <int radix_log_2>
466
static double RadixStringToIeee(const char* current,
469
bool allow_trailing_junk,
470
double junk_string_value,
472
const char** trailing_pointer) {
473
ASSERT(current != end);
475
const int kDoubleSize = Double::kSignificandSize;
476
const int kSingleSize = Single::kSignificandSize;
477
const int kSignificandSize = read_as_double? kDoubleSize: kSingleSize;
480
while (*current == '0') {
482
if (current == end) {
483
*trailing_pointer = end;
484
return SignedZero(sign);
490
const int radix = (1 << radix_log_2);
494
if (*current >= '0' && *current <= '9' && *current < '0' + radix) {
495
digit = static_cast<char>(*current) - '0';
496
} else if (radix > 10 && *current >= 'a' && *current < 'a' + radix - 10) {
497
digit = static_cast<char>(*current) - 'a' + 10;
498
} else if (radix > 10 && *current >= 'A' && *current < 'A' + radix - 10) {
499
digit = static_cast<char>(*current) - 'A' + 10;
501
if (allow_trailing_junk || !AdvanceToNonspace(¤t, end)) {
504
return junk_string_value;
508
number = number * radix + digit;
509
int overflow = static_cast<int>(number >> kSignificandSize);
511
// Overflow occurred. Need to determine which direction to round the
513
int overflow_bits_count = 1;
514
while (overflow > 1) {
515
overflow_bits_count++;
519
int dropped_bits_mask = ((1 << overflow_bits_count) - 1);
520
int dropped_bits = static_cast<int>(number) & dropped_bits_mask;
521
number >>= overflow_bits_count;
522
exponent = overflow_bits_count;
524
bool zero_tail = true;
527
if (current == end || !isDigit(*current, radix)) break;
528
zero_tail = zero_tail && *current == '0';
529
exponent += radix_log_2;
532
if (!allow_trailing_junk && AdvanceToNonspace(¤t, end)) {
533
return junk_string_value;
536
int middle_value = (1 << (overflow_bits_count - 1));
537
if (dropped_bits > middle_value) {
538
number++; // Rounding up.
539
} else if (dropped_bits == middle_value) {
540
// Rounding to even to consistency with decimals: half-way case rounds
541
// up if significant part is odd and down otherwise.
542
if ((number & 1) != 0 || !zero_tail) {
543
number++; // Rounding up.
547
// Rounding up may cause overflow.
548
if ((number & ((int64_t)1 << kSignificandSize)) != 0) {
555
} while (current != end);
557
ASSERT(number < ((int64_t)1 << kSignificandSize));
558
ASSERT(static_cast<int64_t>(static_cast<double>(number)) == number);
560
*trailing_pointer = current;
564
if (number == 0) return -0.0;
567
return static_cast<double>(number);
571
return Double(DiyFp(number, exponent)).value();
575
double StringToDoubleConverter::StringToIeee(
578
int* processed_characters_count,
579
bool read_as_double) {
580
const char* current = input;
581
const char* end = input + length;
583
*processed_characters_count = 0;
585
const bool allow_trailing_junk = (flags_ & ALLOW_TRAILING_JUNK) != 0;
586
const bool allow_leading_spaces = (flags_ & ALLOW_LEADING_SPACES) != 0;
587
const bool allow_trailing_spaces = (flags_ & ALLOW_TRAILING_SPACES) != 0;
588
const bool allow_spaces_after_sign = (flags_ & ALLOW_SPACES_AFTER_SIGN) != 0;
590
// To make sure that iterator dereferencing is valid the following
591
// convention is used:
592
// 1. Each '++current' statement is followed by check for equality to 'end'.
593
// 2. If AdvanceToNonspace returned false then current == end.
594
// 3. If 'current' becomes equal to 'end' the function returns or goes to
596
// 4. 'current' is not dereferenced after the 'parsing_done' label.
597
// 5. Code before 'parsing_done' may rely on 'current != end'.
598
if (current == end) return empty_string_value_;
600
if (allow_leading_spaces || allow_trailing_spaces) {
601
if (!AdvanceToNonspace(¤t, end)) {
602
*processed_characters_count = current - input;
603
return empty_string_value_;
605
if (!allow_leading_spaces && (input != current)) {
606
// No leading spaces allowed, but AdvanceToNonspace moved forward.
607
return junk_string_value_;
611
// The longest form of simplified number is: "-<significant digits>.1eXXX\0".
612
const int kBufferSize = kMaxSignificantDigits + 10;
613
char buffer[kBufferSize]; // NOLINT: size is known at compile time.
616
// Exponent will be adjusted if insignificant digits of the integer part
617
// or insignificant leading zeros of the fractional part are dropped.
619
int significant_digits = 0;
620
int insignificant_digits = 0;
621
bool nonzero_digit_dropped = false;
625
if (*current == '+' || *current == '-') {
626
sign = (*current == '-');
628
const char* next_non_space = current;
629
// Skip following spaces (if allowed).
630
if (!AdvanceToNonspace(&next_non_space, end)) return junk_string_value_;
631
if (!allow_spaces_after_sign && (current != next_non_space)) {
632
return junk_string_value_;
634
current = next_non_space;
637
if (infinity_symbol_ != NULL) {
638
if (*current == infinity_symbol_[0]) {
639
if (!ConsumeSubString(¤t, end, infinity_symbol_)) {
640
return junk_string_value_;
643
if (!(allow_trailing_spaces || allow_trailing_junk) && (current != end)) {
644
return junk_string_value_;
646
if (!allow_trailing_junk && AdvanceToNonspace(¤t, end)) {
647
return junk_string_value_;
650
ASSERT(buffer_pos == 0);
651
*processed_characters_count = current - input;
652
return sign ? -Double::Infinity() : Double::Infinity();
656
if (nan_symbol_ != NULL) {
657
if (*current == nan_symbol_[0]) {
658
if (!ConsumeSubString(¤t, end, nan_symbol_)) {
659
return junk_string_value_;
662
if (!(allow_trailing_spaces || allow_trailing_junk) && (current != end)) {
663
return junk_string_value_;
665
if (!allow_trailing_junk && AdvanceToNonspace(¤t, end)) {
666
return junk_string_value_;
669
ASSERT(buffer_pos == 0);
670
*processed_characters_count = current - input;
671
return sign ? -Double::NaN() : Double::NaN();
675
bool leading_zero = false;
676
if (*current == '0') {
678
if (current == end) {
679
*processed_characters_count = current - input;
680
return SignedZero(sign);
685
// It could be hexadecimal value.
686
if ((flags_ & ALLOW_HEX) && (*current == 'x' || *current == 'X')) {
688
if (current == end || !isDigit(*current, 16)) {
689
return junk_string_value_; // "0x".
692
const char* tail_pointer = NULL;
693
double result = RadixStringToIeee<4>(current,
700
if (tail_pointer != NULL) {
701
if (allow_trailing_spaces) AdvanceToNonspace(&tail_pointer, end);
702
*processed_characters_count = tail_pointer - input;
707
// Ignore leading zeros in the integer part.
708
while (*current == '0') {
710
if (current == end) {
711
*processed_characters_count = current - input;
712
return SignedZero(sign);
717
bool octal = leading_zero && (flags_ & ALLOW_OCTALS) != 0;
719
// Copy significant digits of the integer part (if any) to the buffer.
720
while (*current >= '0' && *current <= '9') {
721
if (significant_digits < kMaxSignificantDigits) {
722
ASSERT(buffer_pos < kBufferSize);
723
buffer[buffer_pos++] = static_cast<char>(*current);
724
significant_digits++;
725
// Will later check if it's an octal in the buffer.
727
insignificant_digits++; // Move the digit into the exponential part.
728
nonzero_digit_dropped = nonzero_digit_dropped || *current != '0';
730
octal = octal && *current < '8';
732
if (current == end) goto parsing_done;
735
if (significant_digits == 0) {
739
if (*current == '.') {
740
if (octal && !allow_trailing_junk) return junk_string_value_;
741
if (octal) goto parsing_done;
744
if (current == end) {
745
if (significant_digits == 0 && !leading_zero) {
746
return junk_string_value_;
752
if (significant_digits == 0) {
754
// Integer part consists of 0 or is absent. Significant digits start after
755
// leading zeros (if any).
756
while (*current == '0') {
758
if (current == end) {
759
*processed_characters_count = current - input;
760
return SignedZero(sign);
762
exponent--; // Move this 0 into the exponent.
766
// There is a fractional part.
767
// We don't emit a '.', but adjust the exponent instead.
768
while (*current >= '0' && *current <= '9') {
769
if (significant_digits < kMaxSignificantDigits) {
770
ASSERT(buffer_pos < kBufferSize);
771
buffer[buffer_pos++] = static_cast<char>(*current);
772
significant_digits++;
775
// Ignore insignificant digits in the fractional part.
776
nonzero_digit_dropped = nonzero_digit_dropped || *current != '0';
779
if (current == end) goto parsing_done;
783
if (!leading_zero && exponent == 0 && significant_digits == 0) {
784
// If leading_zeros is true then the string contains zeros.
785
// If exponent < 0 then string was [+-]\.0*...
786
// If significant_digits != 0 the string is not equal to 0.
787
// Otherwise there are no digits in the string.
788
return junk_string_value_;
791
// Parse exponential part.
792
if (*current == 'e' || *current == 'E') {
793
if (octal && !allow_trailing_junk) return junk_string_value_;
794
if (octal) goto parsing_done;
796
if (current == end) {
797
if (allow_trailing_junk) {
800
return junk_string_value_;
804
if (*current == '+' || *current == '-') {
805
sign = static_cast<char>(*current);
807
if (current == end) {
808
if (allow_trailing_junk) {
811
return junk_string_value_;
816
if (current == end || *current < '0' || *current > '9') {
817
if (allow_trailing_junk) {
820
return junk_string_value_;
824
const int max_exponent = INT_MAX / 2;
825
ASSERT(-max_exponent / 2 <= exponent && exponent <= max_exponent / 2);
829
int digit = *current - '0';
830
if (num >= max_exponent / 10
831
&& !(num == max_exponent / 10 && digit <= max_exponent % 10)) {
834
num = num * 10 + digit;
837
} while (current != end && *current >= '0' && *current <= '9');
839
exponent += (sign == '-' ? -num : num);
842
if (!(allow_trailing_spaces || allow_trailing_junk) && (current != end)) {
843
return junk_string_value_;
845
if (!allow_trailing_junk && AdvanceToNonspace(¤t, end)) {
846
return junk_string_value_;
848
if (allow_trailing_spaces) {
849
AdvanceToNonspace(¤t, end);
853
exponent += insignificant_digits;
857
const char* tail_pointer = NULL;
858
result = RadixStringToIeee<3>(buffer,
865
ASSERT(tail_pointer != NULL);
866
*processed_characters_count = current - input;
870
if (nonzero_digit_dropped) {
871
buffer[buffer_pos++] = '1';
875
ASSERT(buffer_pos < kBufferSize);
876
buffer[buffer_pos] = '\0';
879
if (read_as_double) {
880
converted = Strtod(Vector<const char>(buffer, buffer_pos), exponent);
882
converted = Strtof(Vector<const char>(buffer, buffer_pos), exponent);
884
*processed_characters_count = current - input;
885
return sign? -converted: converted;
888
} // namespace double_conversion