Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(154)

Side by Side Diff: runtime/third_party/double-conversion/src/double-conversion.cc

Issue 8632010: double-conversion drop. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 9 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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
4 // met:
5 //
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.
15 //
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.
27
28 #include <limits.h>
29 #include <math.h>
30
31 #include "double-conversion.h"
32
33 #include "bignum-dtoa.h"
34 #include "double.h"
35 #include "fast-dtoa.h"
36 #include "fixed-dtoa.h"
37 #include "strtod.h"
38 #include "utils.h"
39
40 namespace double_conversion {
41
42 const DoubleToStringConverter& DoubleToStringConverter::EcmaScriptConverter() {
43 int flags = UNIQUE_ZERO | EMIT_POSITIVE_EXPONENT_SIGN;
44 static DoubleToStringConverter converter(flags,
45 "Infinity",
46 "NaN",
47 'e',
48 -6, 21,
49 6, 0);
50 return converter;
51 }
52
53
54 bool DoubleToStringConverter::HandleSpecialValues(
55 double value,
56 StringBuilder* result_builder) const {
57 Double double_inspect(value);
58 if (double_inspect.IsInfinite()) {
59 if (infinity_symbol_ == NULL) return false;
60 if (value < 0) {
61 result_builder->AddCharacter('-');
62 }
63 result_builder->AddString(infinity_symbol_);
64 return true;
65 }
66 if (double_inspect.IsNan()) {
67 if (nan_symbol_ == NULL) return false;
68 result_builder->AddString(nan_symbol_);
69 return true;
70 }
71 return false;
72 }
73
74
75 void DoubleToStringConverter::CreateExponentialRepresentation(
76 const char* decimal_digits,
77 int length,
78 int exponent,
79 StringBuilder* result_builder) const {
80 ASSERT(length != 0);
81 result_builder->AddCharacter(decimal_digits[0]);
82 if (length != 1) {
83 result_builder->AddCharacter('.');
84 result_builder->AddSubstring(&decimal_digits[1], length-1);
85 }
86 result_builder->AddCharacter(exponent_character_);
87 if (exponent < 0) {
88 result_builder->AddCharacter('-');
89 exponent = -exponent;
90 } else {
91 if ((flags_ & EMIT_POSITIVE_EXPONENT_SIGN) != 0) {
92 result_builder->AddCharacter('+');
93 }
94 }
95 if (exponent == 0) {
96 result_builder->AddCharacter('0');
97 return;
98 }
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);
105 exponent /= 10;
106 }
107 result_builder->AddSubstring(&buffer[first_char_pos],
108 kMaxExponentLength - first_char_pos);
109 }
110
111
112 void DoubleToStringConverter::CreateDecimalRepresentation(
113 const char* decimal_digits,
114 int length,
115 int decimal_point,
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);
129 }
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);
137 }
138 } else {
139 // "decima.l_rep000"
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);
148 }
149 if (digits_after_point == 0) {
150 if ((flags_ & EMIT_TRAILING_DECIMAL_POINT) != 0) {
151 result_builder->AddCharacter('.');
152 }
153 if ((flags_ & EMIT_TRAILING_ZERO_AFTER_POINT) != 0) {
154 result_builder->AddCharacter('0');
155 }
156 }
157 }
158
159
160 bool DoubleToStringConverter::ToShortest(double value,
161 StringBuilder* result_builder) const {
162 if (Double(value).IsSpecial()) {
163 return HandleSpecialValues(value, result_builder);
164 }
165
166 int decimal_point;
167 bool sign;
168 const int kDecimalRepCapacity = kBase10MaximalLength + 1;
169 char decimal_rep[kDecimalRepCapacity];
170 int decimal_rep_length;
171
172 DoubleToAscii(value, SHORTEST, 0, decimal_rep, kDecimalRepCapacity,
173 &sign, &decimal_rep_length, &decimal_point);
174
175 bool unique_zero = (flags_ & UNIQUE_ZERO) != 0;
176 if (sign && (value != 0.0 || !unique_zero)) {
177 result_builder->AddCharacter('-');
178 }
179
180 int exponent = decimal_point - 1;
181 if ((decimal_in_shortest_low_ <= exponent) &&
182 (exponent < decimal_in_shortest_high_)) {
183 CreateDecimalRepresentation(decimal_rep, decimal_rep_length,
184 decimal_point,
185 Max(0, decimal_rep_length - decimal_point),
186 result_builder);
187 } else {
188 CreateExponentialRepresentation(decimal_rep, decimal_rep_length, exponent,
189 result_builder);
190 }
191 return true;
192 }
193
194
195 bool DoubleToStringConverter::ToFixed(double value,
196 int requested_digits,
197 StringBuilder* result_builder) const {
198 ASSERT(kMaxFixedDigitsBeforePoint == 60);
199 const double kFirstNonFixed = 1e60;
200
201 if (Double(value).IsSpecial()) {
202 return HandleSpecialValues(value, result_builder);
203 }
204
205 if (requested_digits > kMaxFixedDigitsAfterPoint) return false;
206 if (value >= kFirstNonFixed || value <= -kFirstNonFixed) return false;
207
208 // Find a sufficiently precise decimal representation of n.
209 int decimal_point;
210 bool sign;
211 // Add space for the '\0' byte.
212 const int kDecimalRepCapacity =
213 kMaxFixedDigitsBeforePoint + kMaxFixedDigitsAfterPoint + 1;
214 char decimal_rep[kDecimalRepCapacity];
215 int decimal_rep_length;
216 DoubleToAscii(value, FIXED, requested_digits,
217 decimal_rep, kDecimalRepCapacity,
218 &sign, &decimal_rep_length, &decimal_point);
219
220 bool unique_zero = ((flags_ & UNIQUE_ZERO) != 0);
221 if (sign && (value != 0.0 || !unique_zero)) {
222 result_builder->AddCharacter('-');
223 }
224
225 CreateDecimalRepresentation(decimal_rep, decimal_rep_length, decimal_point,
226 requested_digits, result_builder);
227 return true;
228 }
229
230
231 bool DoubleToStringConverter::ToExponential(
232 double value,
233 int requested_digits,
234 StringBuilder* result_builder) const {
235 if (Double(value).IsSpecial()) {
236 return HandleSpecialValues(value, result_builder);
237 }
238
239 if (requested_digits < -1) return false;
240 if (requested_digits > kMaxExponentialDigits) return false;
241
242 int decimal_point;
243 bool sign;
244 // Add space for digit before the decimal point and the '\0' character.
245 const int kDecimalRepCapacity = kMaxExponentialDigits + 2;
246 ASSERT(kDecimalRepCapacity > kBase10MaximalLength);
247 char decimal_rep[kDecimalRepCapacity];
248 int decimal_rep_length;
249
250 if (requested_digits == -1) {
251 DoubleToAscii(value, SHORTEST, 0,
252 decimal_rep, kDecimalRepCapacity,
253 &sign, &decimal_rep_length, &decimal_point);
254 } else {
255 DoubleToAscii(value, PRECISION, requested_digits + 1,
256 decimal_rep, kDecimalRepCapacity,
257 &sign, &decimal_rep_length, &decimal_point);
258 ASSERT(decimal_rep_length <= requested_digits + 1);
259
260 for (int i = decimal_rep_length; i < requested_digits + 1; ++i) {
261 decimal_rep[i] = '0';
262 }
263 decimal_rep_length = requested_digits + 1;
264 }
265
266 bool unique_zero = ((flags_ & UNIQUE_ZERO) != 0);
267 if (sign && (value != 0.0 || !unique_zero)) {
268 result_builder->AddCharacter('-');
269 }
270
271 int exponent = decimal_point - 1;
272 CreateExponentialRepresentation(decimal_rep,
273 decimal_rep_length,
274 exponent,
275 result_builder);
276 return true;
277 }
278
279
280 bool DoubleToStringConverter::ToPrecision(double value,
281 int precision,
282 StringBuilder* result_builder) const {
283 if (Double(value).IsSpecial()) {
284 return HandleSpecialValues(value, result_builder);
285 }
286
287 if (precision < kMinPrecisionDigits || precision > kMaxPrecisionDigits) {
288 return false;
289 }
290
291 // Find a sufficiently precise decimal representation of n.
292 int decimal_point;
293 bool sign;
294 // Add one for the terminating null character.
295 const int kDecimalRepCapacity = kMaxPrecisionDigits + 1;
296 char decimal_rep[kDecimalRepCapacity];
297 int decimal_rep_length;
298
299 DoubleToAscii(value, PRECISION, precision,
300 decimal_rep, kDecimalRepCapacity,
301 &sign, &decimal_rep_length, &decimal_point);
302 ASSERT(decimal_rep_length <= precision);
303
304 bool unique_zero = ((flags_ & UNIQUE_ZERO) != 0);
305 if (sign && (value != 0.0 || !unique_zero)) {
306 result_builder->AddCharacter('-');
307 }
308
309 // The exponent if we print the number as x.xxeyyy. That is with the
310 // decimal point after the first digit.
311 int exponent = decimal_point - 1;
312
313 int extra_zero = ((flags_ & EMIT_TRAILING_ZERO_AFTER_POINT) != 0) ? 1 : 0;
314 if ((-decimal_point + 1 > max_leading_padding_zeroes_in_precision_mode_) ||
315 (decimal_point - precision + extra_zero >
316 max_trailing_padding_zeroes_in_precision_mode_)) {
317 // Fill buffer to contain 'precision' digits.
318 // Usually the buffer is already at the correct length, but 'DoubleToAscii'
319 // is allowed to return less characters.
320 for (int i = decimal_rep_length; i < precision; ++i) {
321 decimal_rep[i] = '0';
322 }
323
324 CreateExponentialRepresentation(decimal_rep,
325 precision,
326 exponent,
327 result_builder);
328 } else {
329 CreateDecimalRepresentation(decimal_rep, decimal_rep_length, decimal_point,
330 Max(0, precision - decimal_point),
331 result_builder);
332 }
333 return true;
334 }
335
336
337 static BignumDtoaMode DtoaToBignumDtoaMode(
338 DoubleToStringConverter::DtoaMode dtoa_mode) {
339 switch (dtoa_mode) {
340 case DoubleToStringConverter::SHORTEST: return BIGNUM_DTOA_SHORTEST;
341 case DoubleToStringConverter::FIXED: return BIGNUM_DTOA_FIXED;
342 case DoubleToStringConverter::PRECISION: return BIGNUM_DTOA_PRECISION;
343 default:
344 UNREACHABLE();
345 return BIGNUM_DTOA_SHORTEST; // To silence compiler.
346 }
347 }
348
349
350 void DoubleToStringConverter::DoubleToAscii(double v,
351 DtoaMode mode,
352 int requested_digits,
353 char* buffer,
354 int buffer_length,
355 bool* sign,
356 int* length,
357 int* point) {
358 Vector<char> vector(buffer, buffer_length);
359 ASSERT(!Double(v).IsSpecial());
360 ASSERT(mode == SHORTEST || requested_digits >= 0);
361
362 if (Double(v).Sign() < 0) {
363 *sign = true;
364 v = -v;
365 } else {
366 *sign = false;
367 }
368
369 if (mode == PRECISION && requested_digits == 0) {
370 vector[0] = '\0';
371 *length = 0;
372 return;
373 }
374
375 if (v == 0) {
376 vector[0] = '0';
377 vector[1] = '\0';
378 *length = 1;
379 *point = 1;
380 return;
381 }
382
383 bool fast_worked;
384 switch (mode) {
385 case SHORTEST:
386 fast_worked = FastDtoa(v, FAST_DTOA_SHORTEST, 0, vector, length, point);
387 break;
388 case FIXED:
389 fast_worked = FastFixedDtoa(v, requested_digits, vector, length, point);
390 break;
391 case PRECISION:
392 fast_worked = FastDtoa(v, FAST_DTOA_PRECISION, requested_digits,
393 vector, length, point);
394 break;
395 default:
396 UNREACHABLE();
397 fast_worked = false;
398 }
399 if (fast_worked) return;
400
401 // If the fast dtoa didn't succeed use the slower bignum version.
402 BignumDtoaMode bignum_mode = DtoaToBignumDtoaMode(mode);
403 BignumDtoa(v, bignum_mode, requested_digits, vector, length, point);
404 vector[*length] = '\0';
405 }
406
407
408 // Consumes the given substring from the iterator.
409 // Returns false, if the substring does not match.
410 static bool ConsumeSubString(const char** current,
411 const char* end,
412 const char* substring) {
413 ASSERT(**current == *substring);
414 for (substring++; *substring != '\0'; substring++) {
415 ++*current;
416 if (*current == end || **current != *substring) return false;
417 }
418 ++*current;
419 return true;
420 }
421
422
423 // Maximum number of significant digits in decimal representation.
424 // The longest possible double in decimal representation is
425 // (2^53 - 1) * 2 ^ -1074 that is (2 ^ 53 - 1) * 5 ^ 1074 / 10 ^ 1074
426 // (768 digits). If we parse a number whose first digits are equal to a
427 // mean of 2 adjacent doubles (that could have up to 769 digits) the result
428 // must be rounded to the bigger one unless the tail consists of zeros, so
429 // we don't need to preserve all the digits.
430 const int kMaxSignificantDigits = 772;
431
432
433 // Returns true if a nonspace found and false if the end has reached.
434 static inline bool AdvanceToNonspace(const char** current, const char* end) {
435 while (*current != end) {
436 if (**current != ' ') return true;
437 ++*current;
438 }
439 return false;
440 }
441
442
443 static bool isDigit(int x, int radix) {
444 return (x >= '0' && x <= '9' && x < '0' + radix)
445 || (radix > 10 && x >= 'a' && x < 'a' + radix - 10)
446 || (radix > 10 && x >= 'A' && x < 'A' + radix - 10);
447 }
448
449
450 static double SignedZero(bool sign) {
451 return sign ? -0.0 : 0.0;
452 }
453
454
455 // Parsing integers with radix 2, 4, 8, 16, 32. Assumes current != end.
456 template <int radix_log_2>
457 static double RadixStringToDouble(const char* current,
458 const char* end,
459 bool sign,
460 bool allow_trailing_junk,
461 double junk_string_value,
462 const char** trailing_pointer) {
463 ASSERT(current != end);
464
465 // Skip leading 0s.
466 while (*current == '0') {
467 ++current;
468 if (current == end) {
469 *trailing_pointer = end;
470 return SignedZero(sign);
471 }
472 }
473
474 int64_t number = 0;
475 int exponent = 0;
476 const int radix = (1 << radix_log_2);
477
478 do {
479 int digit;
480 if (*current >= '0' && *current <= '9' && *current < '0' + radix) {
481 digit = static_cast<char>(*current) - '0';
482 } else if (radix > 10 && *current >= 'a' && *current < 'a' + radix - 10) {
483 digit = static_cast<char>(*current) - 'a' + 10;
484 } else if (radix > 10 && *current >= 'A' && *current < 'A' + radix - 10) {
485 digit = static_cast<char>(*current) - 'A' + 10;
486 } else {
487 if (allow_trailing_junk || !AdvanceToNonspace(&current, end)) {
488 break;
489 } else {
490 return junk_string_value;
491 }
492 }
493
494 number = number * radix + digit;
495 int overflow = static_cast<int>(number >> 53);
496 if (overflow != 0) {
497 // Overflow occurred. Need to determine which direction to round the
498 // result.
499 int overflow_bits_count = 1;
500 while (overflow > 1) {
501 overflow_bits_count++;
502 overflow >>= 1;
503 }
504
505 int dropped_bits_mask = ((1 << overflow_bits_count) - 1);
506 int dropped_bits = static_cast<int>(number) & dropped_bits_mask;
507 number >>= overflow_bits_count;
508 exponent = overflow_bits_count;
509
510 bool zero_tail = true;
511 while (true) {
512 ++current;
513 if (current == end || !isDigit(*current, radix)) break;
514 zero_tail = zero_tail && *current == '0';
515 exponent += radix_log_2;
516 }
517
518 if (!allow_trailing_junk && AdvanceToNonspace(&current, end)) {
519 return junk_string_value;
520 }
521
522 int middle_value = (1 << (overflow_bits_count - 1));
523 if (dropped_bits > middle_value) {
524 number++; // Rounding up.
525 } else if (dropped_bits == middle_value) {
526 // Rounding to even to consistency with decimals: half-way case rounds
527 // up if significant part is odd and down otherwise.
528 if ((number & 1) != 0 || !zero_tail) {
529 number++; // Rounding up.
530 }
531 }
532
533 // Rounding up may cause overflow.
534 if ((number & ((int64_t)1 << 53)) != 0) {
535 exponent++;
536 number >>= 1;
537 }
538 break;
539 }
540 ++current;
541 } while (current != end);
542
543 ASSERT(number < ((int64_t)1 << 53));
544 ASSERT(static_cast<int64_t>(static_cast<double>(number)) == number);
545
546 *trailing_pointer = current;
547
548 if (exponent == 0) {
549 if (sign) {
550 if (number == 0) return -0.0;
551 number = -number;
552 }
553 return static_cast<double>(number);
554 }
555
556 ASSERT(number != 0);
557 return Double(DiyFp(number, exponent)).value();
558 }
559
560
561 double StringToDoubleConverter::StringToDouble(
562 const char* input,
563 int length,
564 int* processed_characters_count) {
565 const char* current = input;
566 const char* end = input + length;
567
568 *processed_characters_count = 0;
569
570 const bool allow_trailing_junk = (flags_ & ALLOW_TRAILING_JUNK) != 0;
571 const bool allow_leading_spaces = (flags_ & ALLOW_LEADING_SPACES) != 0;
572 const bool allow_trailing_spaces = (flags_ & ALLOW_TRAILING_SPACES) != 0;
573 const bool allow_spaces_after_sign = (flags_ & ALLOW_SPACES_AFTER_SIGN) != 0;
574
575 // To make sure that iterator dereferencing is valid the following
576 // convention is used:
577 // 1. Each '++current' statement is followed by check for equality to 'end'.
578 // 2. If AdvanceToNonspace returned false then current == end.
579 // 3. If 'current' becomes equal to 'end' the function returns or goes to
580 // 'parsing_done'.
581 // 4. 'current' is not dereferenced after the 'parsing_done' label.
582 // 5. Code before 'parsing_done' may rely on 'current != end'.
583 if (current == end) return empty_string_value_;
584
585 if (allow_leading_spaces || allow_trailing_spaces) {
586 if (!AdvanceToNonspace(&current, end)) {
587 *processed_characters_count = current - input;
588 return empty_string_value_;
589 }
590 if (!allow_leading_spaces && (input != current)) {
591 // No leading spaces allowed, but AdvanceToNonspace moved forward.
592 return junk_string_value_;
593 }
594 }
595
596 // The longest form of simplified number is: "-<significant digits>.1eXXX\0".
597 const int kBufferSize = kMaxSignificantDigits + 10;
598 char buffer[kBufferSize]; // NOLINT: size is known at compile time.
599 int buffer_pos = 0;
600
601 // Exponent will be adjusted if insignificant digits of the integer part
602 // or insignificant leading zeros of the fractional part are dropped.
603 int exponent = 0;
604 int significant_digits = 0;
605 int insignificant_digits = 0;
606 bool nonzero_digit_dropped = false;
607 bool fractional_part = false;
608
609 bool sign = false;
610
611 if (*current == '+' || *current == '-') {
612 sign = (*current == '-');
613 ++current;
614 const char* next_non_space = current;
615 // Skip following spaces (if allowed).
616 if (!AdvanceToNonspace(&next_non_space, end)) return junk_string_value_;
617 if (!allow_spaces_after_sign && (current != next_non_space)) {
618 return junk_string_value_;
619 }
620 current = next_non_space;
621 }
622
623 if (infinity_symbol_ != NULL) {
624 if (*current == infinity_symbol_[0]) {
625 if (!ConsumeSubString(&current, end, infinity_symbol_)) {
626 return junk_string_value_;
627 }
628
629 if (!(allow_trailing_spaces || allow_trailing_junk) && (current != end)) {
630 return junk_string_value_;
631 }
632 if (!allow_trailing_junk && AdvanceToNonspace(&current, end)) {
633 return junk_string_value_;
634 }
635
636 ASSERT(buffer_pos == 0);
637 *processed_characters_count = current - input;
638 return sign ? -Double::Infinity() : Double::Infinity();
639 }
640 }
641
642 if (nan_symbol_ != NULL) {
643 if (*current == nan_symbol_[0]) {
644 if (!ConsumeSubString(&current, end, nan_symbol_)) {
645 return junk_string_value_;
646 }
647
648 if (!(allow_trailing_spaces || allow_trailing_junk) && (current != end)) {
649 return junk_string_value_;
650 }
651 if (!allow_trailing_junk && AdvanceToNonspace(&current, end)) {
652 return junk_string_value_;
653 }
654
655 ASSERT(buffer_pos == 0);
656 *processed_characters_count = current - input;
657 return sign ? -Double::NaN() : Double::NaN();
658 }
659 }
660
661 bool leading_zero = false;
662 if (*current == '0') {
663 ++current;
664 if (current == end) {
665 *processed_characters_count = current - input;
666 return SignedZero(sign);
667 }
668
669 leading_zero = true;
670
671 // It could be hexadecimal value.
672 if ((flags_ & ALLOW_HEX) && (*current == 'x' || *current == 'X')) {
673 ++current;
674 if (current == end || !isDigit(*current, 16)) {
675 return junk_string_value_; // "0x".
676 }
677
678 const char* tail_pointer = NULL;
679 double result = RadixStringToDouble<4>(current,
680 end,
681 sign,
682 allow_trailing_junk,
683 junk_string_value_,
684 &tail_pointer);
685 if (tail_pointer != NULL) {
686 if (allow_trailing_spaces) AdvanceToNonspace(&tail_pointer, end);
687 *processed_characters_count = tail_pointer - input;
688 }
689 return result;
690 }
691
692 // Ignore leading zeros in the integer part.
693 while (*current == '0') {
694 ++current;
695 if (current == end) {
696 *processed_characters_count = current - input;
697 return SignedZero(sign);
698 }
699 }
700 }
701
702 bool octal = leading_zero && (flags_ & ALLOW_OCTALS) != 0;
703
704 // Copy significant digits of the integer part (if any) to the buffer.
705 while (*current >= '0' && *current <= '9') {
706 if (significant_digits < kMaxSignificantDigits) {
707 ASSERT(buffer_pos < kBufferSize);
708 buffer[buffer_pos++] = static_cast<char>(*current);
709 significant_digits++;
710 // Will later check if it's an octal in the buffer.
711 } else {
712 insignificant_digits++; // Move the digit into the exponential part.
713 nonzero_digit_dropped = nonzero_digit_dropped || *current != '0';
714 }
715 octal = octal && *current < '8';
716 ++current;
717 if (current == end) goto parsing_done;
718 }
719
720 if (significant_digits == 0) {
721 octal = false;
722 }
723
724 if (*current == '.') {
725 if (octal && !allow_trailing_junk) return junk_string_value_;
726 if (octal) goto parsing_done;
727
728 ++current;
729 if (current == end) {
730 if (significant_digits == 0 && !leading_zero) {
731 return junk_string_value_;
732 } else {
733 goto parsing_done;
734 }
735 }
736
737 if (significant_digits == 0) {
738 // octal = false;
739 // Integer part consists of 0 or is absent. Significant digits start after
740 // leading zeros (if any).
741 while (*current == '0') {
742 ++current;
743 if (current == end) {
744 *processed_characters_count = current - input;
745 return SignedZero(sign);
746 }
747 exponent--; // Move this 0 into the exponent.
748 }
749 }
750
751 // We don't emit a '.', but adjust the exponent instead.
752 fractional_part = true;
753
754 // There is a fractional part.
755 while (*current >= '0' && *current <= '9') {
756 if (significant_digits < kMaxSignificantDigits) {
757 ASSERT(buffer_pos < kBufferSize);
758 buffer[buffer_pos++] = static_cast<char>(*current);
759 significant_digits++;
760 exponent--;
761 } else {
762 // Ignore insignificant digits in the fractional part.
763 nonzero_digit_dropped = nonzero_digit_dropped || *current != '0';
764 }
765 ++current;
766 if (current == end) goto parsing_done;
767 }
768 }
769
770 if (!leading_zero && exponent == 0 && significant_digits == 0) {
771 // If leading_zeros is true then the string contains zeros.
772 // If exponent < 0 then string was [+-]\.0*...
773 // If significant_digits != 0 the string is not equal to 0.
774 // Otherwise there are no digits in the string.
775 return junk_string_value_;
776 }
777
778 // Parse exponential part.
779 if (*current == 'e' || *current == 'E') {
780 if (octal && !allow_trailing_junk) return junk_string_value_;
781 if (octal) goto parsing_done;
782 ++current;
783 if (current == end) {
784 if (allow_trailing_junk) {
785 goto parsing_done;
786 } else {
787 return junk_string_value_;
788 }
789 }
790 char sign = '+';
791 if (*current == '+' || *current == '-') {
792 sign = static_cast<char>(*current);
793 ++current;
794 if (current == end) {
795 if (allow_trailing_junk) {
796 goto parsing_done;
797 } else {
798 return junk_string_value_;
799 }
800 }
801 }
802
803 if (current == end || *current < '0' || *current > '9') {
804 if (allow_trailing_junk) {
805 goto parsing_done;
806 } else {
807 return junk_string_value_;
808 }
809 }
810
811 const int max_exponent = INT_MAX / 2;
812 ASSERT(-max_exponent / 2 <= exponent && exponent <= max_exponent / 2);
813 int num = 0;
814 do {
815 // Check overflow.
816 int digit = *current - '0';
817 if (num >= max_exponent / 10
818 && !(num == max_exponent / 10 && digit <= max_exponent % 10)) {
819 num = max_exponent;
820 } else {
821 num = num * 10 + digit;
822 }
823 ++current;
824 } while (current != end && *current >= '0' && *current <= '9');
825
826 exponent += (sign == '-' ? -num : num);
827 }
828
829 if (!(allow_trailing_spaces || allow_trailing_junk) && (current != end)) {
830 return junk_string_value_;
831 }
832 if (!allow_trailing_junk && AdvanceToNonspace(&current, end)) {
833 return junk_string_value_;
834 }
835 if (allow_trailing_spaces) {
836 AdvanceToNonspace(&current, end);
837 }
838
839 parsing_done:
840 exponent += insignificant_digits;
841
842 if (octal) {
843 double result;
844 const char* tail_pointer = NULL;
845 result = RadixStringToDouble<3>(buffer,
846 buffer + buffer_pos,
847 sign,
848 allow_trailing_junk,
849 junk_string_value_,
850 &tail_pointer);
851 ASSERT(tail_pointer != NULL);
852 *processed_characters_count = current - input;
853 return result;
854 }
855
856 if (nonzero_digit_dropped) {
857 buffer[buffer_pos++] = '1';
858 exponent--;
859 }
860
861 ASSERT(buffer_pos < kBufferSize);
862 buffer[buffer_pos] = '\0';
863
864 double converted = Strtod(Vector<const char>(buffer, buffer_pos), exponent);
865 *processed_characters_count = current - input;
866 return sign? -converted: converted;
867 }
868
869 } // namespace double_conversion
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698