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 <climits>
29 #include <locale>
30 #include <cmath>
31
32 #include "string-to-double.h"
33
34 #include "ieee.h"
35 #include "strtod.h"
36 #include "utils.h"
37
38 namespace double_conversion {
39
40 namespace {
41
ToLower(char ch)42 inline char ToLower(char ch) {
43 static const std::ctype<char>& cType =
44 std::use_facet<std::ctype<char> >(std::locale::classic());
45 return cType.tolower(ch);
46 }
47
Pass(char ch)48 inline char Pass(char ch) {
49 return ch;
50 }
51
52 template <class Iterator, class Converter>
ConsumeSubStringImpl(Iterator * current,Iterator end,const char * substring,Converter converter)53 static inline bool ConsumeSubStringImpl(Iterator* current,
54 Iterator end,
55 const char* substring,
56 Converter converter) {
57 DOUBLE_CONVERSION_ASSERT(converter(**current) == *substring);
58 for (substring++; *substring != '\0'; substring++) {
59 ++*current;
60 if (*current == end || converter(**current) != *substring) {
61 return false;
62 }
63 }
64 ++*current;
65 return true;
66 }
67
68 // Consumes the given substring from the iterator.
69 // Returns false, if the substring does not match.
70 template <class Iterator>
ConsumeSubString(Iterator * current,Iterator end,const char * substring,bool allow_case_insensitivity)71 static bool ConsumeSubString(Iterator* current,
72 Iterator end,
73 const char* substring,
74 bool allow_case_insensitivity) {
75 if (allow_case_insensitivity) {
76 return ConsumeSubStringImpl(current, end, substring, ToLower);
77 } else {
78 return ConsumeSubStringImpl(current, end, substring, Pass);
79 }
80 }
81
82 // Consumes first character of the str is equal to ch
ConsumeFirstCharacter(char ch,const char * str,bool case_insensitivity)83 inline bool ConsumeFirstCharacter(char ch,
84 const char* str,
85 bool case_insensitivity) {
86 return case_insensitivity ? ToLower(ch) == str[0] : ch == str[0];
87 }
88 } // namespace
89
90 // Maximum number of significant digits in decimal representation.
91 // The longest possible double in decimal representation is
92 // (2^53 - 1) * 2 ^ -1074 that is (2 ^ 53 - 1) * 5 ^ 1074 / 10 ^ 1074
93 // (768 digits). If we parse a number whose first digits are equal to a
94 // mean of 2 adjacent doubles (that could have up to 769 digits) the result
95 // must be rounded to the bigger one unless the tail consists of zeros, so
96 // we don't need to preserve all the digits.
97 const int kMaxSignificantDigits = 772;
98
99
100 static const char kWhitespaceTable7[] = { 32, 13, 10, 9, 11, 12 };
101 static const int kWhitespaceTable7Length = DOUBLE_CONVERSION_ARRAY_SIZE(kWhitespaceTable7);
102
103
104 static const uc16 kWhitespaceTable16[] = {
105 160, 8232, 8233, 5760, 6158, 8192, 8193, 8194, 8195,
106 8196, 8197, 8198, 8199, 8200, 8201, 8202, 8239, 8287, 12288, 65279
107 };
108 static const int kWhitespaceTable16Length = DOUBLE_CONVERSION_ARRAY_SIZE(kWhitespaceTable16);
109
110
isWhitespace(int x)111 static bool isWhitespace(int x) {
112 if (x < 128) {
113 for (int i = 0; i < kWhitespaceTable7Length; i++) {
114 if (kWhitespaceTable7[i] == x) return true;
115 }
116 } else {
117 for (int i = 0; i < kWhitespaceTable16Length; i++) {
118 if (kWhitespaceTable16[i] == x) return true;
119 }
120 }
121 return false;
122 }
123
124
125 // Returns true if a nonspace found and false if the end has reached.
126 template <class Iterator>
AdvanceToNonspace(Iterator * current,Iterator end)127 static inline bool AdvanceToNonspace(Iterator* current, Iterator end) {
128 while (*current != end) {
129 if (!isWhitespace(**current)) return true;
130 ++*current;
131 }
132 return false;
133 }
134
135
isDigit(int x,int radix)136 static bool isDigit(int x, int radix) {
137 return (x >= '0' && x <= '9' && x < '0' + radix)
138 || (radix > 10 && x >= 'a' && x < 'a' + radix - 10)
139 || (radix > 10 && x >= 'A' && x < 'A' + radix - 10);
140 }
141
142
SignedZero(bool sign)143 static double SignedZero(bool sign) {
144 return sign ? -0.0 : 0.0;
145 }
146
147
148 // Returns true if 'c' is a decimal digit that is valid for the given radix.
149 //
150 // The function is small and could be inlined, but VS2012 emitted a warning
151 // because it constant-propagated the radix and concluded that the last
152 // condition was always true. By moving it into a separate function the
153 // compiler wouldn't warn anymore.
154 #ifdef _MSC_VER
155 #pragma optimize("",off)
IsDecimalDigitForRadix(int c,int radix)156 static bool IsDecimalDigitForRadix(int c, int radix) {
157 return '0' <= c && c <= '9' && (c - '0') < radix;
158 }
159 #pragma optimize("",on)
160 #else
IsDecimalDigitForRadix(int c,int radix)161 static bool inline IsDecimalDigitForRadix(int c, int radix) {
162 return '0' <= c && c <= '9' && (c - '0') < radix;
163 }
164 #endif
165 // Returns true if 'c' is a character digit that is valid for the given radix.
166 // The 'a_character' should be 'a' or 'A'.
167 //
168 // The function is small and could be inlined, but VS2012 emitted a warning
169 // because it constant-propagated the radix and concluded that the first
170 // condition was always false. By moving it into a separate function the
171 // compiler wouldn't warn anymore.
IsCharacterDigitForRadix(int c,int radix,char a_character)172 static bool IsCharacterDigitForRadix(int c, int radix, char a_character) {
173 return radix > 10 && c >= a_character && c < a_character + radix - 10;
174 }
175
176 // Returns true, when the iterator is equal to end.
177 template<class Iterator>
Advance(Iterator * it,uc16 separator,int base,Iterator & end)178 static bool Advance (Iterator* it, uc16 separator, int base, Iterator& end) {
179 if (separator == StringToDoubleConverter::kNoSeparator) {
180 ++(*it);
181 return *it == end;
182 }
183 if (!isDigit(**it, base)) {
184 ++(*it);
185 return *it == end;
186 }
187 ++(*it);
188 if (*it == end) return true;
189 if (*it + 1 == end) return false;
190 if (**it == separator && isDigit(*(*it + 1), base)) {
191 ++(*it);
192 }
193 return *it == end;
194 }
195
196 // Checks whether the string in the range start-end is a hex-float string.
197 // This function assumes that the leading '0x'/'0X' is already consumed.
198 //
199 // Hex float strings are of one of the following forms:
200 // - hex_digits+ 'p' ('+'|'-')? exponent_digits+
201 // - hex_digits* '.' hex_digits+ 'p' ('+'|'-')? exponent_digits+
202 // - hex_digits+ '.' 'p' ('+'|'-')? exponent_digits+
203 template<class Iterator>
IsHexFloatString(Iterator start,Iterator end,uc16 separator,bool allow_trailing_junk)204 static bool IsHexFloatString(Iterator start,
205 Iterator end,
206 uc16 separator,
207 bool allow_trailing_junk) {
208 DOUBLE_CONVERSION_ASSERT(start != end);
209
210 Iterator current = start;
211
212 bool saw_digit = false;
213 while (isDigit(*current, 16)) {
214 saw_digit = true;
215 if (Advance(¤t, separator, 16, end)) return false;
216 }
217 if (*current == '.') {
218 if (Advance(¤t, separator, 16, end)) return false;
219 while (isDigit(*current, 16)) {
220 saw_digit = true;
221 if (Advance(¤t, separator, 16, end)) return false;
222 }
223 }
224 if (!saw_digit) return false;
225 if (*current != 'p' && *current != 'P') return false;
226 if (Advance(¤t, separator, 16, end)) return false;
227 if (*current == '+' || *current == '-') {
228 if (Advance(¤t, separator, 16, end)) return false;
229 }
230 if (!isDigit(*current, 10)) return false;
231 if (Advance(¤t, separator, 16, end)) return true;
232 while (isDigit(*current, 10)) {
233 if (Advance(¤t, separator, 16, end)) return true;
234 }
235 return allow_trailing_junk || !AdvanceToNonspace(¤t, end);
236 }
237
238
239 // Parsing integers with radix 2, 4, 8, 16, 32. Assumes current != end.
240 //
241 // If parse_as_hex_float is true, then the string must be a valid
242 // hex-float.
243 template <int radix_log_2, class Iterator>
RadixStringToIeee(Iterator * current,Iterator end,bool sign,uc16 separator,bool parse_as_hex_float,bool allow_trailing_junk,double junk_string_value,bool read_as_double,bool * result_is_junk)244 static double RadixStringToIeee(Iterator* current,
245 Iterator end,
246 bool sign,
247 uc16 separator,
248 bool parse_as_hex_float,
249 bool allow_trailing_junk,
250 double junk_string_value,
251 bool read_as_double,
252 bool* result_is_junk) {
253 DOUBLE_CONVERSION_ASSERT(*current != end);
254 DOUBLE_CONVERSION_ASSERT(!parse_as_hex_float ||
255 IsHexFloatString(*current, end, separator, allow_trailing_junk));
256
257 const int kDoubleSize = Double::kSignificandSize;
258 const int kSingleSize = Single::kSignificandSize;
259 const int kSignificandSize = read_as_double? kDoubleSize: kSingleSize;
260
261 *result_is_junk = true;
262
263 int64_t number = 0;
264 int exponent = 0;
265 const int radix = (1 << radix_log_2);
266 // Whether we have encountered a '.' and are parsing the decimal digits.
267 // Only relevant if parse_as_hex_float is true.
268 bool post_decimal = false;
269
270 // Skip leading 0s.
271 while (**current == '0') {
272 if (Advance(current, separator, radix, end)) {
273 *result_is_junk = false;
274 return SignedZero(sign);
275 }
276 }
277
278 while (true) {
279 int digit;
280 if (IsDecimalDigitForRadix(**current, radix)) {
281 digit = static_cast<char>(**current) - '0';
282 if (post_decimal) exponent -= radix_log_2;
283 } else if (IsCharacterDigitForRadix(**current, radix, 'a')) {
284 digit = static_cast<char>(**current) - 'a' + 10;
285 if (post_decimal) exponent -= radix_log_2;
286 } else if (IsCharacterDigitForRadix(**current, radix, 'A')) {
287 digit = static_cast<char>(**current) - 'A' + 10;
288 if (post_decimal) exponent -= radix_log_2;
289 } else if (parse_as_hex_float && **current == '.') {
290 post_decimal = true;
291 Advance(current, separator, radix, end);
292 DOUBLE_CONVERSION_ASSERT(*current != end);
293 continue;
294 } else if (parse_as_hex_float && (**current == 'p' || **current == 'P')) {
295 break;
296 } else {
297 if (allow_trailing_junk || !AdvanceToNonspace(current, end)) {
298 break;
299 } else {
300 return junk_string_value;
301 }
302 }
303
304 number = number * radix + digit;
305 int overflow = static_cast<int>(number >> kSignificandSize);
306 if (overflow != 0) {
307 // Overflow occurred. Need to determine which direction to round the
308 // result.
309 int overflow_bits_count = 1;
310 while (overflow > 1) {
311 overflow_bits_count++;
312 overflow >>= 1;
313 }
314
315 int dropped_bits_mask = ((1 << overflow_bits_count) - 1);
316 int dropped_bits = static_cast<int>(number) & dropped_bits_mask;
317 number >>= overflow_bits_count;
318 exponent += overflow_bits_count;
319
320 bool zero_tail = true;
321 for (;;) {
322 if (Advance(current, separator, radix, end)) break;
323 if (parse_as_hex_float && **current == '.') {
324 // Just run over the '.'. We are just trying to see whether there is
325 // a non-zero digit somewhere.
326 Advance(current, separator, radix, end);
327 DOUBLE_CONVERSION_ASSERT(*current != end);
328 post_decimal = true;
329 }
330 if (!isDigit(**current, radix)) break;
331 zero_tail = zero_tail && **current == '0';
332 if (!post_decimal) exponent += radix_log_2;
333 }
334
335 if (!parse_as_hex_float &&
336 !allow_trailing_junk &&
337 AdvanceToNonspace(current, end)) {
338 return junk_string_value;
339 }
340
341 int middle_value = (1 << (overflow_bits_count - 1));
342 if (dropped_bits > middle_value) {
343 number++; // Rounding up.
344 } else if (dropped_bits == middle_value) {
345 // Rounding to even to consistency with decimals: half-way case rounds
346 // up if significant part is odd and down otherwise.
347 if ((number & 1) != 0 || !zero_tail) {
348 number++; // Rounding up.
349 }
350 }
351
352 // Rounding up may cause overflow.
353 if ((number & ((int64_t)1 << kSignificandSize)) != 0) {
354 exponent++;
355 number >>= 1;
356 }
357 break;
358 }
359 if (Advance(current, separator, radix, end)) break;
360 }
361
362 DOUBLE_CONVERSION_ASSERT(number < ((int64_t)1 << kSignificandSize));
363 DOUBLE_CONVERSION_ASSERT(static_cast<int64_t>(static_cast<double>(number)) == number);
364
365 *result_is_junk = false;
366
367 if (parse_as_hex_float) {
368 DOUBLE_CONVERSION_ASSERT(**current == 'p' || **current == 'P');
369 Advance(current, separator, radix, end);
370 DOUBLE_CONVERSION_ASSERT(*current != end);
371 bool is_negative = false;
372 if (**current == '+') {
373 Advance(current, separator, radix, end);
374 DOUBLE_CONVERSION_ASSERT(*current != end);
375 } else if (**current == '-') {
376 is_negative = true;
377 Advance(current, separator, radix, end);
378 DOUBLE_CONVERSION_ASSERT(*current != end);
379 }
380 int written_exponent = 0;
381 while (IsDecimalDigitForRadix(**current, 10)) {
382 // No need to read exponents if they are too big. That could potentially overflow
383 // the `written_exponent` variable.
384 if (abs(written_exponent) <= 100 * Double::kMaxExponent) {
385 written_exponent = 10 * written_exponent + **current - '0';
386 }
387 if (Advance(current, separator, radix, end)) break;
388 }
389 if (is_negative) written_exponent = -written_exponent;
390 exponent += written_exponent;
391 }
392
393 if (exponent == 0 || number == 0) {
394 if (sign) {
395 if (number == 0) return -0.0;
396 number = -number;
397 }
398 return static_cast<double>(number);
399 }
400
401 DOUBLE_CONVERSION_ASSERT(number != 0);
402 double result = Double(DiyFp(number, exponent)).value();
403 return sign ? -result : result;
404 }
405
406 template <class Iterator>
StringToIeee(Iterator input,int length,bool read_as_double,int * processed_characters_count) const407 double StringToDoubleConverter::StringToIeee(
408 Iterator input,
409 int length,
410 bool read_as_double,
411 int* processed_characters_count) const {
412 Iterator current = input;
413 Iterator end = input + length;
414
415 *processed_characters_count = 0;
416
417 const bool allow_trailing_junk = (flags_ & ALLOW_TRAILING_JUNK) != 0;
418 const bool allow_leading_spaces = (flags_ & ALLOW_LEADING_SPACES) != 0;
419 const bool allow_trailing_spaces = (flags_ & ALLOW_TRAILING_SPACES) != 0;
420 const bool allow_spaces_after_sign = (flags_ & ALLOW_SPACES_AFTER_SIGN) != 0;
421 const bool allow_case_insensitivity = (flags_ & ALLOW_CASE_INSENSITIVITY) != 0;
422
423 // To make sure that iterator dereferencing is valid the following
424 // convention is used:
425 // 1. Each '++current' statement is followed by check for equality to 'end'.
426 // 2. If AdvanceToNonspace returned false then current == end.
427 // 3. If 'current' becomes equal to 'end' the function returns or goes to
428 // 'parsing_done'.
429 // 4. 'current' is not dereferenced after the 'parsing_done' label.
430 // 5. Code before 'parsing_done' may rely on 'current != end'.
431 if (current == end) return empty_string_value_;
432
433 if (allow_leading_spaces || allow_trailing_spaces) {
434 if (!AdvanceToNonspace(¤t, end)) {
435 *processed_characters_count = static_cast<int>(current - input);
436 return empty_string_value_;
437 }
438 if (!allow_leading_spaces && (input != current)) {
439 // No leading spaces allowed, but AdvanceToNonspace moved forward.
440 return junk_string_value_;
441 }
442 }
443
444 // The longest form of simplified number is: "-<significant digits>.1eXXX\0".
445 const int kBufferSize = kMaxSignificantDigits + 10;
446 char buffer[kBufferSize]; // NOLINT: size is known at compile time.
447 int buffer_pos = 0;
448
449 // Exponent will be adjusted if insignificant digits of the integer part
450 // or insignificant leading zeros of the fractional part are dropped.
451 int exponent = 0;
452 int significant_digits = 0;
453 int insignificant_digits = 0;
454 bool nonzero_digit_dropped = false;
455
456 bool sign = false;
457
458 if (*current == '+' || *current == '-') {
459 sign = (*current == '-');
460 ++current;
461 Iterator next_non_space = current;
462 // Skip following spaces (if allowed).
463 if (!AdvanceToNonspace(&next_non_space, end)) return junk_string_value_;
464 if (!allow_spaces_after_sign && (current != next_non_space)) {
465 return junk_string_value_;
466 }
467 current = next_non_space;
468 }
469
470 if (infinity_symbol_ != NULL) {
471 if (ConsumeFirstCharacter(*current, infinity_symbol_, allow_case_insensitivity)) {
472 if (!ConsumeSubString(¤t, end, infinity_symbol_, allow_case_insensitivity)) {
473 return junk_string_value_;
474 }
475
476 if (!(allow_trailing_spaces || allow_trailing_junk) && (current != end)) {
477 return junk_string_value_;
478 }
479 if (!allow_trailing_junk && AdvanceToNonspace(¤t, end)) {
480 return junk_string_value_;
481 }
482
483 DOUBLE_CONVERSION_ASSERT(buffer_pos == 0);
484 *processed_characters_count = static_cast<int>(current - input);
485 return sign ? -Double::Infinity() : Double::Infinity();
486 }
487 }
488
489 if (nan_symbol_ != NULL) {
490 if (ConsumeFirstCharacter(*current, nan_symbol_, allow_case_insensitivity)) {
491 if (!ConsumeSubString(¤t, end, nan_symbol_, allow_case_insensitivity)) {
492 return junk_string_value_;
493 }
494
495 if (!(allow_trailing_spaces || allow_trailing_junk) && (current != end)) {
496 return junk_string_value_;
497 }
498 if (!allow_trailing_junk && AdvanceToNonspace(¤t, end)) {
499 return junk_string_value_;
500 }
501
502 DOUBLE_CONVERSION_ASSERT(buffer_pos == 0);
503 *processed_characters_count = static_cast<int>(current - input);
504 return sign ? -Double::NaN() : Double::NaN();
505 }
506 }
507
508 bool leading_zero = false;
509 if (*current == '0') {
510 if (Advance(¤t, separator_, 10, end)) {
511 *processed_characters_count = static_cast<int>(current - input);
512 return SignedZero(sign);
513 }
514
515 leading_zero = true;
516
517 // It could be hexadecimal value.
518 if (((flags_ & ALLOW_HEX) || (flags_ & ALLOW_HEX_FLOATS)) &&
519 (*current == 'x' || *current == 'X')) {
520 ++current;
521
522 if (current == end) return junk_string_value_; // "0x"
523
524 bool parse_as_hex_float = (flags_ & ALLOW_HEX_FLOATS) &&
525 IsHexFloatString(current, end, separator_, allow_trailing_junk);
526
527 if (!parse_as_hex_float && !isDigit(*current, 16)) {
528 return junk_string_value_;
529 }
530
531 bool result_is_junk;
532 double result = RadixStringToIeee<4>(¤t,
533 end,
534 sign,
535 separator_,
536 parse_as_hex_float,
537 allow_trailing_junk,
538 junk_string_value_,
539 read_as_double,
540 &result_is_junk);
541 if (!result_is_junk) {
542 if (allow_trailing_spaces) AdvanceToNonspace(¤t, end);
543 *processed_characters_count = static_cast<int>(current - input);
544 }
545 return result;
546 }
547
548 // Ignore leading zeros in the integer part.
549 while (*current == '0') {
550 if (Advance(¤t, separator_, 10, end)) {
551 *processed_characters_count = static_cast<int>(current - input);
552 return SignedZero(sign);
553 }
554 }
555 }
556
557 bool octal = leading_zero && (flags_ & ALLOW_OCTALS) != 0;
558
559 // Copy significant digits of the integer part (if any) to the buffer.
560 while (*current >= '0' && *current <= '9') {
561 if (significant_digits < kMaxSignificantDigits) {
562 DOUBLE_CONVERSION_ASSERT(buffer_pos < kBufferSize);
563 buffer[buffer_pos++] = static_cast<char>(*current);
564 significant_digits++;
565 // Will later check if it's an octal in the buffer.
566 } else {
567 insignificant_digits++; // Move the digit into the exponential part.
568 nonzero_digit_dropped = nonzero_digit_dropped || *current != '0';
569 }
570 octal = octal && *current < '8';
571 if (Advance(¤t, separator_, 10, end)) goto parsing_done;
572 }
573
574 if (significant_digits == 0) {
575 octal = false;
576 }
577
578 if (*current == '.') {
579 if (octal && !allow_trailing_junk) return junk_string_value_;
580 if (octal) goto parsing_done;
581
582 if (Advance(¤t, separator_, 10, end)) {
583 if (significant_digits == 0 && !leading_zero) {
584 return junk_string_value_;
585 } else {
586 goto parsing_done;
587 }
588 }
589
590 if (significant_digits == 0) {
591 // octal = false;
592 // Integer part consists of 0 or is absent. Significant digits start after
593 // leading zeros (if any).
594 while (*current == '0') {
595 if (Advance(¤t, separator_, 10, end)) {
596 *processed_characters_count = static_cast<int>(current - input);
597 return SignedZero(sign);
598 }
599 exponent--; // Move this 0 into the exponent.
600 }
601 }
602
603 // There is a fractional part.
604 // We don't emit a '.', but adjust the exponent instead.
605 while (*current >= '0' && *current <= '9') {
606 if (significant_digits < kMaxSignificantDigits) {
607 DOUBLE_CONVERSION_ASSERT(buffer_pos < kBufferSize);
608 buffer[buffer_pos++] = static_cast<char>(*current);
609 significant_digits++;
610 exponent--;
611 } else {
612 // Ignore insignificant digits in the fractional part.
613 nonzero_digit_dropped = nonzero_digit_dropped || *current != '0';
614 }
615 if (Advance(¤t, separator_, 10, end)) goto parsing_done;
616 }
617 }
618
619 if (!leading_zero && exponent == 0 && significant_digits == 0) {
620 // If leading_zeros is true then the string contains zeros.
621 // If exponent < 0 then string was [+-]\.0*...
622 // If significant_digits != 0 the string is not equal to 0.
623 // Otherwise there are no digits in the string.
624 return junk_string_value_;
625 }
626
627 // Parse exponential part.
628 if (*current == 'e' || *current == 'E') {
629 if (octal && !allow_trailing_junk) return junk_string_value_;
630 if (octal) goto parsing_done;
631 Iterator junk_begin = current;
632 ++current;
633 if (current == end) {
634 if (allow_trailing_junk) {
635 current = junk_begin;
636 goto parsing_done;
637 } else {
638 return junk_string_value_;
639 }
640 }
641 char exponen_sign = '+';
642 if (*current == '+' || *current == '-') {
643 exponen_sign = static_cast<char>(*current);
644 ++current;
645 if (current == end) {
646 if (allow_trailing_junk) {
647 current = junk_begin;
648 goto parsing_done;
649 } else {
650 return junk_string_value_;
651 }
652 }
653 }
654
655 if (current == end || *current < '0' || *current > '9') {
656 if (allow_trailing_junk) {
657 current = junk_begin;
658 goto parsing_done;
659 } else {
660 return junk_string_value_;
661 }
662 }
663
664 const int max_exponent = INT_MAX / 2;
665 DOUBLE_CONVERSION_ASSERT(-max_exponent / 2 <= exponent && exponent <= max_exponent / 2);
666 int num = 0;
667 do {
668 // Check overflow.
669 int digit = *current - '0';
670 if (num >= max_exponent / 10
671 && !(num == max_exponent / 10 && digit <= max_exponent % 10)) {
672 num = max_exponent;
673 } else {
674 num = num * 10 + digit;
675 }
676 ++current;
677 } while (current != end && *current >= '0' && *current <= '9');
678
679 exponent += (exponen_sign == '-' ? -num : num);
680 }
681
682 if (!(allow_trailing_spaces || allow_trailing_junk) && (current != end)) {
683 return junk_string_value_;
684 }
685 if (!allow_trailing_junk && AdvanceToNonspace(¤t, end)) {
686 return junk_string_value_;
687 }
688 if (allow_trailing_spaces) {
689 AdvanceToNonspace(¤t, end);
690 }
691
692 parsing_done:
693 exponent += insignificant_digits;
694
695 if (octal) {
696 double result;
697 bool result_is_junk;
698 char* start = buffer;
699 result = RadixStringToIeee<3>(&start,
700 buffer + buffer_pos,
701 sign,
702 separator_,
703 false, // Don't parse as hex_float.
704 allow_trailing_junk,
705 junk_string_value_,
706 read_as_double,
707 &result_is_junk);
708 DOUBLE_CONVERSION_ASSERT(!result_is_junk);
709 *processed_characters_count = static_cast<int>(current - input);
710 return result;
711 }
712
713 if (nonzero_digit_dropped) {
714 buffer[buffer_pos++] = '1';
715 exponent--;
716 }
717
718 DOUBLE_CONVERSION_ASSERT(buffer_pos < kBufferSize);
719 buffer[buffer_pos] = '\0';
720
721 double converted;
722 if (read_as_double) {
723 converted = Strtod(Vector<const char>(buffer, buffer_pos), exponent);
724 } else {
725 converted = Strtof(Vector<const char>(buffer, buffer_pos), exponent);
726 }
727 *processed_characters_count = static_cast<int>(current - input);
728 return sign? -converted: converted;
729 }
730
731
StringToDouble(const char * buffer,int length,int * processed_characters_count) const732 double StringToDoubleConverter::StringToDouble(
733 const char* buffer,
734 int length,
735 int* processed_characters_count) const {
736 return StringToIeee(buffer, length, true, processed_characters_count);
737 }
738
739
StringToDouble(const uc16 * buffer,int length,int * processed_characters_count) const740 double StringToDoubleConverter::StringToDouble(
741 const uc16* buffer,
742 int length,
743 int* processed_characters_count) const {
744 return StringToIeee(buffer, length, true, processed_characters_count);
745 }
746
747
StringToFloat(const char * buffer,int length,int * processed_characters_count) const748 float StringToDoubleConverter::StringToFloat(
749 const char* buffer,
750 int length,
751 int* processed_characters_count) const {
752 return static_cast<float>(StringToIeee(buffer, length, false,
753 processed_characters_count));
754 }
755
756
StringToFloat(const uc16 * buffer,int length,int * processed_characters_count) const757 float StringToDoubleConverter::StringToFloat(
758 const uc16* buffer,
759 int length,
760 int* processed_characters_count) const {
761 return static_cast<float>(StringToIeee(buffer, length, false,
762 processed_characters_count));
763 }
764
765 } // namespace double_conversion
766