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

Side by Side Diff: src/conversions-inl.h

Issue 7308004: Extract string->double and double->string conversions for use in the preparser. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Created 9 years, 5 months 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
1 // Copyright 2006-2008 the V8 project authors. All rights reserved. 1 // Copyright 2011 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
11 // with the distribution. 11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its 12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived 13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission. 14 // from this software without specific prior written permission.
15 // 15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
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 #ifndef V8_CONVERSIONS_INL_H_ 28 #ifndef V8_CONVERSIONS_INL_H_
29 #define V8_CONVERSIONS_INL_H_ 29 #define V8_CONVERSIONS_INL_H_
30 30
31 #include <limits.h> // Required for INT_MAX etc.
31 #include <math.h> 32 #include <math.h>
32 #include <float.h> // required for DBL_MAX and on Win32 for finite() 33 #include <float.h> // Required for DBL_MAX and on Win32 for finite()
33 #include <stdarg.h> 34 #include <stdarg.h>
34 35
35 // ---------------------------------------------------------------------------- 36 // ----------------------------------------------------------------------------
36 // Extra POSIX/ANSI functions for Win32/MSVC. 37 // Extra POSIX/ANSI functions for Win32/MSVC.
37 38
38 #include "conversions.h" 39 #include "conversions.h"
40 #include "strtod.h"
39 #include "platform.h" 41 #include "platform.h"
40 42
41 namespace v8 { 43 namespace v8 {
42 namespace internal { 44 namespace internal {
43 45
44 // The fast double-to-unsigned-int conversion routine does not guarantee 46 // The fast double-to-unsigned-int conversion routine does not guarantee
45 // rounding towards zero, or any reasonable value if the argument is larger 47 // rounding towards zero, or any reasonable value if the argument is larger
46 // than what fits in an unsigned 32-bit integer. 48 // than what fits in an unsigned 32-bit integer.
47 static inline unsigned int FastD2UI(double x) { 49 static inline unsigned int FastD2UI(double x) {
48 // There is no unsigned version of lrint, so there is no fast path 50 // There is no unsigned version of lrint, so there is no fast path
(...skipping 21 matching lines...) Expand all
70 } 72 }
71 73
72 74
73 static inline double DoubleToInteger(double x) { 75 static inline double DoubleToInteger(double x) {
74 if (isnan(x)) return 0; 76 if (isnan(x)) return 0;
75 if (!isfinite(x) || x == 0) return x; 77 if (!isfinite(x) || x == 0) return x;
76 return (x >= 0) ? floor(x) : ceil(x); 78 return (x >= 0) ? floor(x) : ceil(x);
77 } 79 }
78 80
79 81
80 int32_t NumberToInt32(Object* number) {
81 if (number->IsSmi()) return Smi::cast(number)->value();
82 return DoubleToInt32(number->Number());
83 }
84
85
86 uint32_t NumberToUint32(Object* number) {
87 if (number->IsSmi()) return Smi::cast(number)->value();
88 return DoubleToUint32(number->Number());
89 }
90
91
92 int32_t DoubleToInt32(double x) { 82 int32_t DoubleToInt32(double x) {
93 int32_t i = FastD2I(x); 83 int32_t i = FastD2I(x);
94 if (FastI2D(i) == x) return i; 84 if (FastI2D(i) == x) return i;
95 static const double two32 = 4294967296.0; 85 static const double two32 = 4294967296.0;
96 static const double two31 = 2147483648.0; 86 static const double two31 = 2147483648.0;
97 if (!isfinite(x) || x == 0) return 0; 87 if (!isfinite(x) || x == 0) return 0;
98 if (x < 0 || x >= two32) x = modulo(x, two32); 88 if (x < 0 || x >= two32) x = modulo(x, two32);
99 x = (x >= 0) ? floor(x) : ceil(x) + two32; 89 x = (x >= 0) ? floor(x) : ceil(x) + two32;
100 return (int32_t) ((x >= two31) ? x - two32 : x); 90 return (int32_t) ((x >= two31) ? x - two32 : x);
101 } 91 }
102 92
103 93
94 template <class Iterator, class EndMark>
95 static bool SubStringEquals(Iterator* current,
96 EndMark end,
97 const char* substring) {
98 ASSERT(**current == *substring);
99 for (substring++; *substring != '\0'; substring++) {
100 ++*current;
101 if (*current == end || **current != *substring) return false;
102 }
103 ++*current;
104 return true;
105 }
106
107
108 // Returns true if a nonspace found and false if the end has reached.
Rico 2011/07/05 08:54:57 nonspace found -> nonspace is found end has reache
Lasse Reichstein 2011/07/05 10:43:18 rewritten.
109 template <class Iterator, class EndMark>
110 static inline bool AdvanceToNonspace(UnicodeCache* unicode_cache,
111 Iterator* current,
112 EndMark end) {
113 while (*current != end) {
114 if (!unicode_cache->IsWhiteSpace(**current)) return true;
115 ++*current;
116 }
117 return false;
118 }
119
120
121 // Parsing integers with radix 2, 4, 8, 16, 32. Assumes current != end.
122 template <int radix_log_2, class Iterator, class EndMark>
123 static double InternalStringToIntDouble(UnicodeCache* unicode_cache,
124 Iterator current,
125 EndMark end,
126 bool negative,
127 bool allow_trailing_junk) {
128 ASSERT(current != end);
129
130 // Skip leading 0s.
131 while (*current == '0') {
132 ++current;
133 if (current == end) return SignedZero(negative);
134 }
135
136 int64_t number = 0;
137 int exponent = 0;
138 const int radix = (1 << radix_log_2);
139
140 do {
141 int digit;
142 if (*current >= '0' && *current <= '9' && *current < '0' + radix) {
143 digit = static_cast<char>(*current) - '0';
144 } else if (radix > 10 && *current >= 'a' && *current < 'a' + radix - 10) {
145 digit = static_cast<char>(*current) - 'a' + 10;
146 } else if (radix > 10 && *current >= 'A' && *current < 'A' + radix - 10) {
147 digit = static_cast<char>(*current) - 'A' + 10;
148 } else {
149 if (allow_trailing_junk ||
150 !AdvanceToNonspace(unicode_cache, &current, end)) {
151 break;
152 } else {
153 return JUNK_STRING_VALUE;
154 }
155 }
156
157 number = number * radix + digit;
158 int overflow = static_cast<int>(number >> 53);
159 if (overflow != 0) {
160 // Overflow occurred. Need to determine which direction to round the
161 // result.
162 int overflow_bits_count = 1;
163 while (overflow > 1) {
164 overflow_bits_count++;
165 overflow >>= 1;
166 }
167
168 int dropped_bits_mask = ((1 << overflow_bits_count) - 1);
169 int dropped_bits = static_cast<int>(number) & dropped_bits_mask;
170 number >>= overflow_bits_count;
171 exponent = overflow_bits_count;
172
173 bool zero_tail = true;
174 while (true) {
175 ++current;
176 if (current == end || !isDigit(*current, radix)) break;
177 zero_tail = zero_tail && *current == '0';
178 exponent += radix_log_2;
179 }
180
181 if (!allow_trailing_junk &&
182 AdvanceToNonspace(unicode_cache, &current, end)) {
183 return JUNK_STRING_VALUE;
184 }
185
186 int middle_value = (1 << (overflow_bits_count - 1));
187 if (dropped_bits > middle_value) {
188 number++; // Rounding up.
189 } else if (dropped_bits == middle_value) {
190 // Rounding to even to consistency with decimals: half-way case rounds
191 // up if significant part is odd and down otherwise.
192 if ((number & 1) != 0 || !zero_tail) {
193 number++; // Rounding up.
194 }
195 }
196
197 // Rounding up may cause overflow.
198 if ((number & ((int64_t)1 << 53)) != 0) {
199 exponent++;
200 number >>= 1;
201 }
202 break;
203 }
204 ++current;
205 } while (current != end);
206
207 ASSERT(number < ((int64_t)1 << 53));
208 ASSERT(static_cast<int64_t>(static_cast<double>(number)) == number);
209
210 if (exponent == 0) {
211 if (negative) {
212 if (number == 0) return -0.0;
213 number = -number;
214 }
215 return static_cast<double>(number);
216 }
217
218 ASSERT(number != 0);
219 // The double could be constructed faster from number (mantissa), exponent
220 // and sign. Assuming it's a rare case more simple code is used.
221 return static_cast<double>(negative ? -number : number) * pow(2.0, exponent);
222 }
223
224
225 template <class Iterator, class EndMark>
226 static double InternalStringToInt(UnicodeCache* unicode_cache,
227 Iterator current,
228 EndMark end,
229 int radix) {
230 const bool allow_trailing_junk = true;
231 const double empty_string_val = JUNK_STRING_VALUE;
232
233 if (!AdvanceToNonspace(unicode_cache, &current, end)) {
234 return empty_string_val;
235 }
236
237 bool negative = false;
238 bool leading_zero = false;
239
240 if (*current == '+') {
241 // Ignore leading sign; skip following spaces.
242 ++current;
243 if (current == end) {
244 return JUNK_STRING_VALUE;
245 }
246 } else if (*current == '-') {
247 ++current;
248 if (current == end) {
249 return JUNK_STRING_VALUE;
250 }
251 negative = true;
252 }
253
254 if (radix == 0) {
255 // Radix detection.
256 if (*current == '0') {
257 ++current;
258 if (current == end) return SignedZero(negative);
259 if (*current == 'x' || *current == 'X') {
260 radix = 16;
261 ++current;
262 if (current == end) return JUNK_STRING_VALUE;
263 } else {
264 radix = 8;
265 leading_zero = true;
266 }
267 } else {
268 radix = 10;
269 }
270 } else if (radix == 16) {
271 if (*current == '0') {
272 // Allow "0x" prefix.
273 ++current;
274 if (current == end) return SignedZero(negative);
275 if (*current == 'x' || *current == 'X') {
276 ++current;
277 if (current == end) return JUNK_STRING_VALUE;
278 } else {
279 leading_zero = true;
280 }
281 }
282 }
283
284 if (radix < 2 || radix > 36) return JUNK_STRING_VALUE;
285
286 // Skip leading zeros.
287 while (*current == '0') {
288 leading_zero = true;
289 ++current;
290 if (current == end) return SignedZero(negative);
291 }
292
293 if (!leading_zero && !isDigit(*current, radix)) {
294 return JUNK_STRING_VALUE;
295 }
296
297 if (IsPowerOf2(radix)) {
298 switch (radix) {
299 case 2:
300 return InternalStringToIntDouble<1>(
301 unicode_cache, current, end, negative, allow_trailing_junk);
302 case 4:
303 return InternalStringToIntDouble<2>(
304 unicode_cache, current, end, negative, allow_trailing_junk);
305 case 8:
306 return InternalStringToIntDouble<3>(
307 unicode_cache, current, end, negative, allow_trailing_junk);
308
309 case 16:
310 return InternalStringToIntDouble<4>(
311 unicode_cache, current, end, negative, allow_trailing_junk);
312
313 case 32:
314 return InternalStringToIntDouble<5>(
315 unicode_cache, current, end, negative, allow_trailing_junk);
316 default:
317 UNREACHABLE();
318 }
319 }
320
321 if (radix == 10) {
322 // Parsing with strtod.
323 const int kMaxSignificantDigits = 309; // Doubles are less than 1.8e308.
324 // The buffer may contain up to kMaxSignificantDigits + 1 digits and a zero
325 // end.
326 const int kBufferSize = kMaxSignificantDigits + 2;
327 char buffer[kBufferSize];
328 int buffer_pos = 0;
329 while (*current >= '0' && *current <= '9') {
330 if (buffer_pos <= kMaxSignificantDigits) {
331 // If the number has more than kMaxSignificantDigits it will be parsed
332 // as infinity.
333 ASSERT(buffer_pos < kBufferSize);
334 buffer[buffer_pos++] = static_cast<char>(*current);
335 }
336 ++current;
337 if (current == end) break;
338 }
339
340 if (!allow_trailing_junk &&
341 AdvanceToNonspace(unicode_cache, &current, end)) {
342 return JUNK_STRING_VALUE;
343 }
344
345 ASSERT(buffer_pos < kBufferSize);
346 buffer[buffer_pos] = '\0';
347 Vector<const char> buffer_vector(buffer, buffer_pos);
348 return negative ? -Strtod(buffer_vector, 0) : Strtod(buffer_vector, 0);
349 }
350
351 // The following code causes accumulating rounding error for numbers greater
352 // than ~2^56. It's explicitly allowed in the spec: "if R is not 2, 4, 8, 10,
353 // 16, or 32, then mathInt may be an implementation-dependent approximation to
354 // the mathematical integer value" (15.1.2.2).
355
356 int lim_0 = '0' + (radix < 10 ? radix : 10);
357 int lim_a = 'a' + (radix - 10);
358 int lim_A = 'A' + (radix - 10);
359
360 // NOTE: The code for computing the value may seem a bit complex at
361 // first glance. It is structured to use 32-bit multiply-and-add
362 // loops as long as possible to avoid loosing precision.
363
364 double v = 0.0;
365 bool done = false;
366 do {
367 // Parse the longest part of the string starting at index j
368 // possible while keeping the multiplier, and thus the part
369 // itself, within 32 bits.
370 unsigned int part = 0, multiplier = 1;
371 while (true) {
372 int d;
373 if (*current >= '0' && *current < lim_0) {
374 d = *current - '0';
375 } else if (*current >= 'a' && *current < lim_a) {
376 d = *current - 'a' + 10;
377 } else if (*current >= 'A' && *current < lim_A) {
378 d = *current - 'A' + 10;
379 } else {
380 done = true;
381 break;
382 }
383
384 // Update the value of the part as long as the multiplier fits
385 // in 32 bits. When we can't guarantee that the next iteration
386 // will not overflow the multiplier, we stop parsing the part
387 // by leaving the loop.
388 const unsigned int kMaximumMultiplier = 0xffffffffU / 36;
389 uint32_t m = multiplier * radix;
390 if (m > kMaximumMultiplier) break;
391 part = part * radix + d;
392 multiplier = m;
393 ASSERT(multiplier > part);
394
395 ++current;
396 if (current == end) {
397 done = true;
398 break;
399 }
400 }
401
402 // Update the value and skip the part in the string.
403 v = v * multiplier + part;
404 } while (!done);
405
406 if (!allow_trailing_junk &&
407 AdvanceToNonspace(unicode_cache, &current, end)) {
408 return JUNK_STRING_VALUE;
409 }
410
411 return negative ? -v : v;
412 }
413
414
415 // Converts a string to a double value. Assumes the Iterator supports
416 // the following operations:
417 // 1. current == end (other ops are not allowed), current != end.
418 // 2. *current - gets the current character in the sequence.
419 // 3. ++current (advances the position).
420 template <class Iterator, class EndMark>
421 static double InternalStringToDouble(UnicodeCache* unicode_cache,
422 Iterator current,
423 EndMark end,
424 int flags,
425 double empty_string_val) {
426 // To make sure that iterator dereferencing is valid the following
427 // convention is used:
428 // 1. Each '++current' statement is followed by check for equality to 'end'.
429 // 2. If AdvanceToNonspace returned false then current == end.
430 // 3. If 'current' becomes be equal to 'end' the function returns or goes to
431 // 'parsing_done'.
432 // 4. 'current' is not dereferenced after the 'parsing_done' label.
433 // 5. Code before 'parsing_done' may rely on 'current != end'.
434 if (!AdvanceToNonspace(unicode_cache, &current, end)) {
435 return empty_string_val;
436 }
437
438 const bool allow_trailing_junk = (flags & ALLOW_TRAILING_JUNK) != 0;
439
440 // The longest form of simplified number is: "-<significant digits>'.1eXXX\0".
441 const int kBufferSize = kMaxSignificantDigits + 10;
442 char buffer[kBufferSize]; // NOLINT: size is known at compile time.
443 int buffer_pos = 0;
444
445 // Exponent will be adjusted if insignificant digits of the integer part
446 // or insignificant leading zeros of the fractional part are dropped.
447 int exponent = 0;
448 int significant_digits = 0;
449 int insignificant_digits = 0;
450 bool nonzero_digit_dropped = false;
451 bool fractional_part = false;
452
453 bool negative = false;
454
455 if (*current == '+') {
456 // Ignore leading sign.
457 ++current;
458 if (current == end) return JUNK_STRING_VALUE;
459 } else if (*current == '-') {
460 ++current;
461 if (current == end) return JUNK_STRING_VALUE;
462 negative = true;
463 }
464
465 static const char kInfinitySymbol[] = "Infinity";
466 if (*current == kInfinitySymbol[0]) {
467 if (!SubStringEquals(&current, end, kInfinitySymbol)) {
468 return JUNK_STRING_VALUE;
469 }
470
471 if (!allow_trailing_junk &&
472 AdvanceToNonspace(unicode_cache, &current, end)) {
473 return JUNK_STRING_VALUE;
474 }
475
476 ASSERT(buffer_pos == 0);
477 return negative ? -V8_INFINITY : V8_INFINITY;
478 }
479
480 bool leading_zero = false;
481 if (*current == '0') {
482 ++current;
483 if (current == end) return SignedZero(negative);
484
485 leading_zero = true;
486
487 // It could be hexadecimal value.
488 if ((flags & ALLOW_HEX) && (*current == 'x' || *current == 'X')) {
489 ++current;
490 if (current == end || !isDigit(*current, 16)) {
491 return JUNK_STRING_VALUE; // "0x".
492 }
493
494 return InternalStringToIntDouble<4>(unicode_cache,
495 current,
496 end,
497 negative,
498 allow_trailing_junk);
499 }
500
501 // Ignore leading zeros in the integer part.
502 while (*current == '0') {
503 ++current;
504 if (current == end) return SignedZero(negative);
505 }
506 }
507
508 bool octal = leading_zero && (flags & ALLOW_OCTALS) != 0;
509
510 // Copy significant digits of the integer part (if any) to the buffer.
511 while (*current >= '0' && *current <= '9') {
512 if (significant_digits < kMaxSignificantDigits) {
513 ASSERT(buffer_pos < kBufferSize);
514 buffer[buffer_pos++] = static_cast<char>(*current);
515 significant_digits++;
516 // Will later check if it's an octal in the buffer.
517 } else {
518 insignificant_digits++; // Move the digit into the exponential part.
519 nonzero_digit_dropped = nonzero_digit_dropped || *current != '0';
520 }
521 octal = octal && *current < '8';
522 ++current;
523 if (current == end) goto parsing_done;
524 }
525
526 if (significant_digits == 0) {
527 octal = false;
528 }
529
530 if (*current == '.') {
531 if (octal && !allow_trailing_junk) return JUNK_STRING_VALUE;
532 if (octal) goto parsing_done;
533
534 ++current;
535 if (current == end) {
536 if (significant_digits == 0 && !leading_zero) {
537 return JUNK_STRING_VALUE;
538 } else {
539 goto parsing_done;
540 }
541 }
542
543 if (significant_digits == 0) {
544 // octal = false;
545 // Integer part consists of 0 or is absent. Significant digits start after
546 // leading zeros (if any).
547 while (*current == '0') {
548 ++current;
549 if (current == end) return SignedZero(negative);
550 exponent--; // Move this 0 into the exponent.
551 }
552 }
553
554 // We don't emit a '.', but adjust the exponent instead.
555 fractional_part = true;
556
557 // There is a fractional part.
558 while (*current >= '0' && *current <= '9') {
559 if (significant_digits < kMaxSignificantDigits) {
560 ASSERT(buffer_pos < kBufferSize);
561 buffer[buffer_pos++] = static_cast<char>(*current);
562 significant_digits++;
563 exponent--;
564 } else {
565 // Ignore insignificant digits in the fractional part.
566 nonzero_digit_dropped = nonzero_digit_dropped || *current != '0';
567 }
568 ++current;
569 if (current == end) goto parsing_done;
570 }
571 }
572
573 if (!leading_zero && exponent == 0 && significant_digits == 0) {
574 // If leading_zeros is true then the string contains zeros.
575 // If exponent < 0 then string was [+-]\.0*...
576 // If significant_digits != 0 the string is not equal to 0.
577 // Otherwise there are no digits in the string.
578 return JUNK_STRING_VALUE;
579 }
580
581 // Parse exponential part.
582 if (*current == 'e' || *current == 'E') {
583 if (octal) return JUNK_STRING_VALUE;
584 ++current;
585 if (current == end) {
586 if (allow_trailing_junk) {
587 goto parsing_done;
588 } else {
589 return JUNK_STRING_VALUE;
590 }
591 }
592 char sign = '+';
593 if (*current == '+' || *current == '-') {
594 sign = static_cast<char>(*current);
595 ++current;
596 if (current == end) {
597 if (allow_trailing_junk) {
598 goto parsing_done;
599 } else {
600 return JUNK_STRING_VALUE;
601 }
602 }
603 }
604
605 if (current == end || *current < '0' || *current > '9') {
606 if (allow_trailing_junk) {
607 goto parsing_done;
608 } else {
609 return JUNK_STRING_VALUE;
610 }
611 }
612
613 const int max_exponent = INT_MAX / 2;
614 ASSERT(-max_exponent / 2 <= exponent && exponent <= max_exponent / 2);
615 int num = 0;
616 do {
617 // Check overflow.
618 int digit = *current - '0';
619 if (num >= max_exponent / 10
620 && !(num == max_exponent / 10 && digit <= max_exponent % 10)) {
621 num = max_exponent;
622 } else {
623 num = num * 10 + digit;
624 }
625 ++current;
626 } while (current != end && *current >= '0' && *current <= '9');
627
628 exponent += (sign == '-' ? -num : num);
629 }
630
631 if (!allow_trailing_junk &&
632 AdvanceToNonspace(unicode_cache, &current, end)) {
633 return JUNK_STRING_VALUE;
634 }
635
636 parsing_done:
637 exponent += insignificant_digits;
638
639 if (octal) {
640 return InternalStringToIntDouble<3>(unicode_cache,
641 buffer,
642 buffer + buffer_pos,
643 negative,
644 allow_trailing_junk);
645 }
646
647 if (nonzero_digit_dropped) {
648 buffer[buffer_pos++] = '1';
649 exponent--;
650 }
651
652 ASSERT(buffer_pos < kBufferSize);
653 buffer[buffer_pos] = '\0';
654
655 double converted = Strtod(Vector<const char>(buffer, buffer_pos), exponent);
656 return negative ? -converted : converted;
657 }
658
104 } } // namespace v8::internal 659 } } // namespace v8::internal
105 660
106 #endif // V8_CONVERSIONS_INL_H_ 661 #endif // V8_CONVERSIONS_INL_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698