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

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: git utd 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
« no previous file with comments | « src/conversions.cc ('k') | src/diy-fp.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 character has been found and false if the
109 // end was been reached before finding a nonspace character.
110 template <class Iterator, class EndMark>
111 static inline bool AdvanceToNonspace(UnicodeCache* unicode_cache,
112 Iterator* current,
113 EndMark end) {
114 while (*current != end) {
115 if (!unicode_cache->IsWhiteSpace(**current)) return true;
116 ++*current;
117 }
118 return false;
119 }
120
121
122 // Parsing integers with radix 2, 4, 8, 16, 32. Assumes current != end.
123 template <int radix_log_2, class Iterator, class EndMark>
124 static double InternalStringToIntDouble(UnicodeCache* unicode_cache,
125 Iterator current,
126 EndMark end,
127 bool negative,
128 bool allow_trailing_junk) {
129 ASSERT(current != end);
130
131 // Skip leading 0s.
132 while (*current == '0') {
133 ++current;
134 if (current == end) return SignedZero(negative);
135 }
136
137 int64_t number = 0;
138 int exponent = 0;
139 const int radix = (1 << radix_log_2);
140
141 do {
142 int digit;
143 if (*current >= '0' && *current <= '9' && *current < '0' + radix) {
144 digit = static_cast<char>(*current) - '0';
145 } else if (radix > 10 && *current >= 'a' && *current < 'a' + radix - 10) {
146 digit = static_cast<char>(*current) - 'a' + 10;
147 } else if (radix > 10 && *current >= 'A' && *current < 'A' + radix - 10) {
148 digit = static_cast<char>(*current) - 'A' + 10;
149 } else {
150 if (allow_trailing_junk ||
151 !AdvanceToNonspace(unicode_cache, &current, end)) {
152 break;
153 } else {
154 return JUNK_STRING_VALUE;
155 }
156 }
157
158 number = number * radix + digit;
159 int overflow = static_cast<int>(number >> 53);
160 if (overflow != 0) {
161 // Overflow occurred. Need to determine which direction to round the
162 // result.
163 int overflow_bits_count = 1;
164 while (overflow > 1) {
165 overflow_bits_count++;
166 overflow >>= 1;
167 }
168
169 int dropped_bits_mask = ((1 << overflow_bits_count) - 1);
170 int dropped_bits = static_cast<int>(number) & dropped_bits_mask;
171 number >>= overflow_bits_count;
172 exponent = overflow_bits_count;
173
174 bool zero_tail = true;
175 while (true) {
176 ++current;
177 if (current == end || !isDigit(*current, radix)) break;
178 zero_tail = zero_tail && *current == '0';
179 exponent += radix_log_2;
180 }
181
182 if (!allow_trailing_junk &&
183 AdvanceToNonspace(unicode_cache, &current, end)) {
184 return JUNK_STRING_VALUE;
185 }
186
187 int middle_value = (1 << (overflow_bits_count - 1));
188 if (dropped_bits > middle_value) {
189 number++; // Rounding up.
190 } else if (dropped_bits == middle_value) {
191 // Rounding to even to consistency with decimals: half-way case rounds
192 // up if significant part is odd and down otherwise.
193 if ((number & 1) != 0 || !zero_tail) {
194 number++; // Rounding up.
195 }
196 }
197
198 // Rounding up may cause overflow.
199 if ((number & ((int64_t)1 << 53)) != 0) {
200 exponent++;
201 number >>= 1;
202 }
203 break;
204 }
205 ++current;
206 } while (current != end);
207
208 ASSERT(number < ((int64_t)1 << 53));
209 ASSERT(static_cast<int64_t>(static_cast<double>(number)) == number);
210
211 if (exponent == 0) {
212 if (negative) {
213 if (number == 0) return -0.0;
214 number = -number;
215 }
216 return static_cast<double>(number);
217 }
218
219 ASSERT(number != 0);
220 // The double could be constructed faster from number (mantissa), exponent
221 // and sign. Assuming it's a rare case more simple code is used.
222 return static_cast<double>(negative ? -number : number) * pow(2.0, exponent);
223 }
224
225
226 template <class Iterator, class EndMark>
227 static double InternalStringToInt(UnicodeCache* unicode_cache,
228 Iterator current,
229 EndMark end,
230 int radix) {
231 const bool allow_trailing_junk = true;
232 const double empty_string_val = JUNK_STRING_VALUE;
233
234 if (!AdvanceToNonspace(unicode_cache, &current, end)) {
235 return empty_string_val;
236 }
237
238 bool negative = false;
239 bool leading_zero = false;
240
241 if (*current == '+') {
242 // Ignore leading sign; skip following spaces.
243 ++current;
244 if (current == end) {
245 return JUNK_STRING_VALUE;
246 }
247 } else if (*current == '-') {
248 ++current;
249 if (current == end) {
250 return JUNK_STRING_VALUE;
251 }
252 negative = true;
253 }
254
255 if (radix == 0) {
256 // Radix detection.
257 if (*current == '0') {
258 ++current;
259 if (current == end) return SignedZero(negative);
260 if (*current == 'x' || *current == 'X') {
261 radix = 16;
262 ++current;
263 if (current == end) return JUNK_STRING_VALUE;
264 } else {
265 radix = 8;
266 leading_zero = true;
267 }
268 } else {
269 radix = 10;
270 }
271 } else if (radix == 16) {
272 if (*current == '0') {
273 // Allow "0x" prefix.
274 ++current;
275 if (current == end) return SignedZero(negative);
276 if (*current == 'x' || *current == 'X') {
277 ++current;
278 if (current == end) return JUNK_STRING_VALUE;
279 } else {
280 leading_zero = true;
281 }
282 }
283 }
284
285 if (radix < 2 || radix > 36) return JUNK_STRING_VALUE;
286
287 // Skip leading zeros.
288 while (*current == '0') {
289 leading_zero = true;
290 ++current;
291 if (current == end) return SignedZero(negative);
292 }
293
294 if (!leading_zero && !isDigit(*current, radix)) {
295 return JUNK_STRING_VALUE;
296 }
297
298 if (IsPowerOf2(radix)) {
299 switch (radix) {
300 case 2:
301 return InternalStringToIntDouble<1>(
302 unicode_cache, current, end, negative, allow_trailing_junk);
303 case 4:
304 return InternalStringToIntDouble<2>(
305 unicode_cache, current, end, negative, allow_trailing_junk);
306 case 8:
307 return InternalStringToIntDouble<3>(
308 unicode_cache, current, end, negative, allow_trailing_junk);
309
310 case 16:
311 return InternalStringToIntDouble<4>(
312 unicode_cache, current, end, negative, allow_trailing_junk);
313
314 case 32:
315 return InternalStringToIntDouble<5>(
316 unicode_cache, current, end, negative, allow_trailing_junk);
317 default:
318 UNREACHABLE();
319 }
320 }
321
322 if (radix == 10) {
323 // Parsing with strtod.
324 const int kMaxSignificantDigits = 309; // Doubles are less than 1.8e308.
325 // The buffer may contain up to kMaxSignificantDigits + 1 digits and a zero
326 // end.
327 const int kBufferSize = kMaxSignificantDigits + 2;
328 char buffer[kBufferSize];
329 int buffer_pos = 0;
330 while (*current >= '0' && *current <= '9') {
331 if (buffer_pos <= kMaxSignificantDigits) {
332 // If the number has more than kMaxSignificantDigits it will be parsed
333 // as infinity.
334 ASSERT(buffer_pos < kBufferSize);
335 buffer[buffer_pos++] = static_cast<char>(*current);
336 }
337 ++current;
338 if (current == end) break;
339 }
340
341 if (!allow_trailing_junk &&
342 AdvanceToNonspace(unicode_cache, &current, end)) {
343 return JUNK_STRING_VALUE;
344 }
345
346 ASSERT(buffer_pos < kBufferSize);
347 buffer[buffer_pos] = '\0';
348 Vector<const char> buffer_vector(buffer, buffer_pos);
349 return negative ? -Strtod(buffer_vector, 0) : Strtod(buffer_vector, 0);
350 }
351
352 // The following code causes accumulating rounding error for numbers greater
353 // than ~2^56. It's explicitly allowed in the spec: "if R is not 2, 4, 8, 10,
354 // 16, or 32, then mathInt may be an implementation-dependent approximation to
355 // the mathematical integer value" (15.1.2.2).
356
357 int lim_0 = '0' + (radix < 10 ? radix : 10);
358 int lim_a = 'a' + (radix - 10);
359 int lim_A = 'A' + (radix - 10);
360
361 // NOTE: The code for computing the value may seem a bit complex at
362 // first glance. It is structured to use 32-bit multiply-and-add
363 // loops as long as possible to avoid loosing precision.
364
365 double v = 0.0;
366 bool done = false;
367 do {
368 // Parse the longest part of the string starting at index j
369 // possible while keeping the multiplier, and thus the part
370 // itself, within 32 bits.
371 unsigned int part = 0, multiplier = 1;
372 while (true) {
373 int d;
374 if (*current >= '0' && *current < lim_0) {
375 d = *current - '0';
376 } else if (*current >= 'a' && *current < lim_a) {
377 d = *current - 'a' + 10;
378 } else if (*current >= 'A' && *current < lim_A) {
379 d = *current - 'A' + 10;
380 } else {
381 done = true;
382 break;
383 }
384
385 // Update the value of the part as long as the multiplier fits
386 // in 32 bits. When we can't guarantee that the next iteration
387 // will not overflow the multiplier, we stop parsing the part
388 // by leaving the loop.
389 const unsigned int kMaximumMultiplier = 0xffffffffU / 36;
390 uint32_t m = multiplier * radix;
391 if (m > kMaximumMultiplier) break;
392 part = part * radix + d;
393 multiplier = m;
394 ASSERT(multiplier > part);
395
396 ++current;
397 if (current == end) {
398 done = true;
399 break;
400 }
401 }
402
403 // Update the value and skip the part in the string.
404 v = v * multiplier + part;
405 } while (!done);
406
407 if (!allow_trailing_junk &&
408 AdvanceToNonspace(unicode_cache, &current, end)) {
409 return JUNK_STRING_VALUE;
410 }
411
412 return negative ? -v : v;
413 }
414
415
416 // Converts a string to a double value. Assumes the Iterator supports
417 // the following operations:
418 // 1. current == end (other ops are not allowed), current != end.
419 // 2. *current - gets the current character in the sequence.
420 // 3. ++current (advances the position).
421 template <class Iterator, class EndMark>
422 static double InternalStringToDouble(UnicodeCache* unicode_cache,
423 Iterator current,
424 EndMark end,
425 int flags,
426 double empty_string_val) {
427 // To make sure that iterator dereferencing is valid the following
428 // convention is used:
429 // 1. Each '++current' statement is followed by check for equality to 'end'.
430 // 2. If AdvanceToNonspace returned false then current == end.
431 // 3. If 'current' becomes be equal to 'end' the function returns or goes to
432 // 'parsing_done'.
433 // 4. 'current' is not dereferenced after the 'parsing_done' label.
434 // 5. Code before 'parsing_done' may rely on 'current != end'.
435 if (!AdvanceToNonspace(unicode_cache, &current, end)) {
436 return empty_string_val;
437 }
438
439 const bool allow_trailing_junk = (flags & ALLOW_TRAILING_JUNK) != 0;
440
441 // The longest form of simplified number is: "-<significant digits>'.1eXXX\0".
442 const int kBufferSize = kMaxSignificantDigits + 10;
443 char buffer[kBufferSize]; // NOLINT: size is known at compile time.
444 int buffer_pos = 0;
445
446 // Exponent will be adjusted if insignificant digits of the integer part
447 // or insignificant leading zeros of the fractional part are dropped.
448 int exponent = 0;
449 int significant_digits = 0;
450 int insignificant_digits = 0;
451 bool nonzero_digit_dropped = false;
452 bool fractional_part = false;
453
454 bool negative = false;
455
456 if (*current == '+') {
457 // Ignore leading sign.
458 ++current;
459 if (current == end) return JUNK_STRING_VALUE;
460 } else if (*current == '-') {
461 ++current;
462 if (current == end) return JUNK_STRING_VALUE;
463 negative = true;
464 }
465
466 static const char kInfinitySymbol[] = "Infinity";
467 if (*current == kInfinitySymbol[0]) {
468 if (!SubStringEquals(&current, end, kInfinitySymbol)) {
469 return JUNK_STRING_VALUE;
470 }
471
472 if (!allow_trailing_junk &&
473 AdvanceToNonspace(unicode_cache, &current, end)) {
474 return JUNK_STRING_VALUE;
475 }
476
477 ASSERT(buffer_pos == 0);
478 return negative ? -V8_INFINITY : V8_INFINITY;
479 }
480
481 bool leading_zero = false;
482 if (*current == '0') {
483 ++current;
484 if (current == end) return SignedZero(negative);
485
486 leading_zero = true;
487
488 // It could be hexadecimal value.
489 if ((flags & ALLOW_HEX) && (*current == 'x' || *current == 'X')) {
490 ++current;
491 if (current == end || !isDigit(*current, 16)) {
492 return JUNK_STRING_VALUE; // "0x".
493 }
494
495 return InternalStringToIntDouble<4>(unicode_cache,
496 current,
497 end,
498 negative,
499 allow_trailing_junk);
500 }
501
502 // Ignore leading zeros in the integer part.
503 while (*current == '0') {
504 ++current;
505 if (current == end) return SignedZero(negative);
506 }
507 }
508
509 bool octal = leading_zero && (flags & ALLOW_OCTALS) != 0;
510
511 // Copy significant digits of the integer part (if any) to the buffer.
512 while (*current >= '0' && *current <= '9') {
513 if (significant_digits < kMaxSignificantDigits) {
514 ASSERT(buffer_pos < kBufferSize);
515 buffer[buffer_pos++] = static_cast<char>(*current);
516 significant_digits++;
517 // Will later check if it's an octal in the buffer.
518 } else {
519 insignificant_digits++; // Move the digit into the exponential part.
520 nonzero_digit_dropped = nonzero_digit_dropped || *current != '0';
521 }
522 octal = octal && *current < '8';
523 ++current;
524 if (current == end) goto parsing_done;
525 }
526
527 if (significant_digits == 0) {
528 octal = false;
529 }
530
531 if (*current == '.') {
532 if (octal && !allow_trailing_junk) return JUNK_STRING_VALUE;
533 if (octal) goto parsing_done;
534
535 ++current;
536 if (current == end) {
537 if (significant_digits == 0 && !leading_zero) {
538 return JUNK_STRING_VALUE;
539 } else {
540 goto parsing_done;
541 }
542 }
543
544 if (significant_digits == 0) {
545 // octal = false;
546 // Integer part consists of 0 or is absent. Significant digits start after
547 // leading zeros (if any).
548 while (*current == '0') {
549 ++current;
550 if (current == end) return SignedZero(negative);
551 exponent--; // Move this 0 into the exponent.
552 }
553 }
554
555 // We don't emit a '.', but adjust the exponent instead.
556 fractional_part = true;
557
558 // There is a fractional part.
559 while (*current >= '0' && *current <= '9') {
560 if (significant_digits < kMaxSignificantDigits) {
561 ASSERT(buffer_pos < kBufferSize);
562 buffer[buffer_pos++] = static_cast<char>(*current);
563 significant_digits++;
564 exponent--;
565 } else {
566 // Ignore insignificant digits in the fractional part.
567 nonzero_digit_dropped = nonzero_digit_dropped || *current != '0';
568 }
569 ++current;
570 if (current == end) goto parsing_done;
571 }
572 }
573
574 if (!leading_zero && exponent == 0 && significant_digits == 0) {
575 // If leading_zeros is true then the string contains zeros.
576 // If exponent < 0 then string was [+-]\.0*...
577 // If significant_digits != 0 the string is not equal to 0.
578 // Otherwise there are no digits in the string.
579 return JUNK_STRING_VALUE;
580 }
581
582 // Parse exponential part.
583 if (*current == 'e' || *current == 'E') {
584 if (octal) return JUNK_STRING_VALUE;
585 ++current;
586 if (current == end) {
587 if (allow_trailing_junk) {
588 goto parsing_done;
589 } else {
590 return JUNK_STRING_VALUE;
591 }
592 }
593 char sign = '+';
594 if (*current == '+' || *current == '-') {
595 sign = static_cast<char>(*current);
596 ++current;
597 if (current == end) {
598 if (allow_trailing_junk) {
599 goto parsing_done;
600 } else {
601 return JUNK_STRING_VALUE;
602 }
603 }
604 }
605
606 if (current == end || *current < '0' || *current > '9') {
607 if (allow_trailing_junk) {
608 goto parsing_done;
609 } else {
610 return JUNK_STRING_VALUE;
611 }
612 }
613
614 const int max_exponent = INT_MAX / 2;
615 ASSERT(-max_exponent / 2 <= exponent && exponent <= max_exponent / 2);
616 int num = 0;
617 do {
618 // Check overflow.
619 int digit = *current - '0';
620 if (num >= max_exponent / 10
621 && !(num == max_exponent / 10 && digit <= max_exponent % 10)) {
622 num = max_exponent;
623 } else {
624 num = num * 10 + digit;
625 }
626 ++current;
627 } while (current != end && *current >= '0' && *current <= '9');
628
629 exponent += (sign == '-' ? -num : num);
630 }
631
632 if (!allow_trailing_junk &&
633 AdvanceToNonspace(unicode_cache, &current, end)) {
634 return JUNK_STRING_VALUE;
635 }
636
637 parsing_done:
638 exponent += insignificant_digits;
639
640 if (octal) {
641 return InternalStringToIntDouble<3>(unicode_cache,
642 buffer,
643 buffer + buffer_pos,
644 negative,
645 allow_trailing_junk);
646 }
647
648 if (nonzero_digit_dropped) {
649 buffer[buffer_pos++] = '1';
650 exponent--;
651 }
652
653 ASSERT(buffer_pos < kBufferSize);
654 buffer[buffer_pos] = '\0';
655
656 double converted = Strtod(Vector<const char>(buffer, buffer_pos), exponent);
657 return negative ? -converted : converted;
658 }
659
104 } } // namespace v8::internal 660 } } // namespace v8::internal
105 661
106 #endif // V8_CONVERSIONS_INL_H_ 662 #endif // V8_CONVERSIONS_INL_H_
OLDNEW
« no previous file with comments | « src/conversions.cc ('k') | src/diy-fp.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698