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

Side by Side Diff: src/conversions.cc

Issue 1096002: StringToDouble rewritten not using String::Get and memory allocations.... (Closed) Base URL: http://v8.googlecode.com/svn/branches/bleeding_edge/
Patch Set: '' Created 10 years, 9 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 | « no previous file | test/cctest/test-conversions.cc » ('j') | test/cctest/test-conversions.cc » ('J')
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 2006-2008 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 #include <stdarg.h> 28 #include <stdarg.h>
29 #include <limits.h>
29 30
30 #include "v8.h" 31 #include "v8.h"
31 32
32 #include "conversions-inl.h" 33 #include "conversions-inl.h"
33 #include "factory.h" 34 #include "factory.h"
34 #include "fast-dtoa.h" 35 #include "fast-dtoa.h"
35 #include "scanner.h" 36 #include "scanner.h"
36 37
37 namespace v8 { 38 namespace v8 {
38 namespace internal { 39 namespace internal {
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
85 result[i - index] = static_cast<char>(c); 86 result[i - index] = static_cast<char>(c);
86 } else { 87 } else {
87 result[i - index] = 127; // Force number parsing to fail. 88 result[i - index] = 127; // Force number parsing to fail.
88 } 89 }
89 } 90 }
90 result[length - index] = '\0'; 91 result[length - index] = '\0';
91 return result; 92 return result;
92 } 93 }
93 94
94 95
96 namespace {
97
98 // C++-style iterator adaptor for StringInputBuffer
99 // (unlike C++ iterators the end-marker has different type).
100 class StringInputBufferIterator {
101 public:
102 class EndMarker {};
103
104 explicit StringInputBufferIterator(StringInputBuffer* buffer);
105
106 int operator*() const;
107 void operator++();
108 bool operator==(EndMarker const&) const { return end_; }
109 bool operator!=(EndMarker const& m) const { return !end_; }
110
111 private:
112 StringInputBuffer* const buffer_;
113 int current_;
114 bool end_;
115 };
116
117
118 StringInputBufferIterator::StringInputBufferIterator(
119 StringInputBuffer* buffer) : buffer_(buffer) {
120 ++(*this);
121 }
122
123 int StringInputBufferIterator::operator*() const {
124 return current_;
125 }
126
127
128 void StringInputBufferIterator::operator++() {
129 end_ = !buffer_->has_more();
130 if (!end_) {
131 current_ = buffer_->GetNext();
132 }
133 }
134 }
135
136
95 static inline void ReleaseCString(const char* original, const char* str) { 137 static inline void ReleaseCString(const char* original, const char* str) {
96 } 138 }
97 139
98 140
99 static inline void ReleaseCString(String* original, const char* str) { 141 static inline void ReleaseCString(String* original, const char* str) {
100 DeleteArray(const_cast<char *>(str)); 142 DeleteArray(const_cast<char *>(str));
101 } 143 }
102 144
103 145
104 static inline bool IsSpace(const char* str, int index) { 146 template <class Iterator, class EndMark>
105 ASSERT(index >= 0 && index < StrLength(str)); 147 static bool SubStringEquals(Iterator* current,
106 return Scanner::kIsWhiteSpace.get(str[index]); 148 EndMark end,
107 } 149 const char* substring) {
108 150 ASSERT(**current == *substring);
109 151 for (substring++; *substring != '\0'; substring++) {
110 static inline bool IsSpace(String* str, int index) { 152 ++*current;
111 return Scanner::kIsWhiteSpace.get(str->Get(index)); 153 if (*current == end || **current != *substring) return false;
112 }
113
114
115 static inline bool SubStringEquals(const char* str,
116 int index,
117 const char* other) {
118 return strncmp(str + index, other, strlen(other)) != 0;
119 }
120
121
122 static inline bool SubStringEquals(String* str, int index, const char* other) {
123 HandleScope scope;
124 int str_length = str->length();
125 int other_length = StrLength(other);
126 int end = index + other_length < str_length ?
127 index + other_length :
128 str_length;
129 Handle<String> substring =
130 Factory::NewSubString(Handle<String>(str), index, end);
131 return substring->IsEqualTo(Vector<const char>(other, other_length));
132 }
133
134
135 // Check if a string should be parsed as an octal number. The string
136 // can be either a char* or a String*.
137 template<class S>
138 static bool ShouldParseOctal(S* s, int i) {
139 int index = i;
140 int len = GetLength(s);
141 if (index < len && GetChar(s, index) != '0') return false;
142
143 // If the first real character (following '0') is not an octal
144 // digit, bail out early. This also takes care of numbers of the
145 // forms 0.xxx and 0exxx by not allowing the first 0 to be
146 // interpreted as an octal.
147 index++;
148 if (index < len) {
149 int d = GetChar(s, index) - '0';
150 if (d < 0 || d > 7) return false;
151 } else {
152 return false;
153 } 154 }
154 155 ++*current;
155 // Traverse all digits (including the first). If there is an octal
156 // prefix which is not a part of a longer decimal prefix, we return
157 // true. Otherwise, false is returned.
158 while (index < len) {
159 int d = GetChar(s, index++) - '0';
160 if (d == 8 || d == 9) return false;
161 if (d < 0 || d > 7) return true;
162 }
163 return true; 156 return true;
164 } 157 }
165 158
166 159
167 extern "C" double gay_strtod(const char* s00, const char** se); 160 extern "C" double gay_strtod(const char* s00, const char** se);
168 161
169 162
170 // Parse an int from a string starting a given index and in a given 163 // Parse an int from a string starting a given index and in a given
171 // radix. The string can be either a char* or a String*. 164 // radix. The string can be either a char* or a String*.
172 template <class S> 165 template <class S>
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
255 248
256 249
257 int StringToInt(const char* str, int index, int radix, double* value) { 250 int StringToInt(const char* str, int index, int radix, double* value) {
258 return InternalStringToInt(const_cast<char*>(str), index, radix, value); 251 return InternalStringToInt(const_cast<char*>(str), index, radix, value);
259 } 252 }
260 253
261 254
262 static const double JUNK_STRING_VALUE = OS::nan_value(); 255 static const double JUNK_STRING_VALUE = OS::nan_value();
263 256
264 257
265 // Convert a string to a double value. The string can be either a 258 // Returns true if a nonspace found and false if the end has reached.
266 // char* or a String*. 259 template <class Iterator, class EndMark>
267 template<class S> 260 static inline bool AdvanceToNonspace(Iterator* current, EndMark end) {
268 static double InternalStringToDouble(S* str, 261 while (*current != end) {
262 if (!Scanner::kIsWhiteSpace.get(**current)) return true;
263 ++*current;
264 }
265 return false;
266 }
267
268
269 template <class Iterator, class EndMark>
270 static double InternalHexidecimalStringToDouble(Iterator current,
271 EndMark end,
272 char* buffer,
273 bool allow_trailing_junk) {
274 ASSERT(current != end);
275 // We reuse the buffer of InternalStringToDouble. Since hexidecimal
276 // numbers may have much less digits than decimal the buffer won't overflow.
277 int significant_digits = 0;
278 int insignificant_digits = 0;
279 bool leading_zero = false;
280 // Hexidecomal may have (52) / 4 + 1 significant digit. Mean of 2
Erik Corry 2010/03/25 12:26:16 Hexidecomal -> Hexadecimal digit -> digits I can't
Florian Loitsch 2010/03/25 13:51:35 Maybe the following explanation is easier to under
281 // hexidecimal may have n + 1.
Erik Corry 2010/03/25 12:26:16 hexidecimal -> hexadecimal
282 const int max_significant_digits = (52) / 4 + 2;
Erik Corry 2010/03/25 12:26:16 I don't see the point in putting 52 in brackets he
283 int buffer_pos = 0;
284 bool nonzero_digit_dropped = false;
285
286 // Skip leading 0s.
287 while (*current == '0') {
288 leading_zero = true;
289 ++current;
290 if (current == end) return 0;
291 }
292
293 int begin_pos = buffer_pos;
294 while ((*current >= '0' && *current <= '9')
295 || (*current >= 'a' && *current <= 'f')
296 || (*current >= 'A' && *current <= 'F')) {
297 if (significant_digits <= max_significant_digits) {
298 ASSERT(buffer_pos < buffer_size);
299 buffer[buffer_pos++] = *current;
300 significant_digits++;
301 } else {
302 insignificant_digits++;
303 nonzero_digit_dropped = nonzero_digit_dropped || *current != '0';
304 }
305 ++current;
306 if (current == end) break;
307 }
308
309 if (!allow_trailing_junk && AdvanceToNonspace(&current, end)) {
310 return JUNK_STRING_VALUE;
311 }
312
313 if (significant_digits == 0) {
314 return leading_zero ? 0 : JUNK_STRING_VALUE;
315 }
316
317 if (nonzero_digit_dropped) {
318 ASSERT(insignificant_digits > 0);
319 insignificant_digits--;
320 buffer[buffer_pos++] = '1';
321 }
322
323 buffer[buffer_pos] = '\0';
324
325 double result;
326 StringToInt(buffer, begin_pos, 16, &result);
327 if (insignificant_digits > 0) {
328 // Multiplying by power of 2 doesn't cause a loss of precision.
Erik Corry 2010/03/25 12:26:16 power -> a power
329 result *= pow(16.0, insignificant_digits);
330 }
331 return result;
332 }
333
334
335 // Converts a string to a double value. Assumes the Iterator supports
336 // the following operations:
337 // 1. current == end (other ops are not allowed), current != end.
338 // 2. *current - gets the current character in the sequence.
339 // 3. ++current (advances the position).
340 template <class Iterator, class EndMark>
341 static double InternalStringToDouble(Iterator current,
342 EndMark end,
269 int flags, 343 int flags,
270 double empty_string_val) { 344 double empty_string_val) {
271 double result = 0.0; 345 // To make sure that iterator unreferencing is valid the following
Erik Corry 2010/03/25 12:26:16 unreferencing -> dereferencing
272 int index = 0; 346 // convention is used:
273 347 // 1. Each '++current' statement is followed by check for equality to 'end'.
274 int len = GetLength(str); 348 // 2. If AdvanceToNonspace returned false then current == end.
275 349 // 3. If 'current' becomes be equal to 'end' the function returns or goes to
276 // Skip leading spaces. 350 // 'parsing_done'.
277 while ((index < len) && IsSpace(str, index)) index++; 351 // 4. 'current' is not unreferenced after the 'parsing_done' label.
Erik Corry 2010/03/25 12:26:16 and here
278 352 // 5. Code before 'parsing_done' may rely on 'current != end'.
279 // Is the string empty? 353 if (!AdvanceToNonspace(&current, end)) return empty_string_val;
280 if (index >= len) return empty_string_val; 354
281 355 const bool allow_trailing_junk = (flags & ALLOW_TRAILING_JUNK) != 0;
282 // Get the first character. 356
283 uint16_t first = GetChar(str, index); 357 // Insignificant digits will be removed.
284 358 const int max_significant_digits = 772;
Erik Corry 2010/03/25 12:26:16 Name as a constant. Is it possible to provide a o
285 // Numbers can only start with '-', '+', '.', 'I' (Infinity), or a digit. 359 // The longest form of simplified number is: "-<significant digits>'.1eXXX\0".
286 if (first != '-' && first != '+' && first != '.' && first != 'I' && 360 const int buffer_size = max_significant_digits + 10;
Erik Corry 2010/03/25 12:26:16 Name as a constant.
287 (first > '9' || first < '0')) { 361 char buffer[buffer_size]; // NOLINT: size is known at compile time.
362 int buffer_pos = 0;
363
364 // Exponent will be adjusted if insignificant digits of the integer part
365 // or insignificant leading zeros of the fractional part are dropped.
366 int exponent = 0;
367 int significant_digits = 0;
368 int insignificant_digits = 0;
369 bool nonzero_digit_dropped = false;
370
371 double signed_zero = 0.0;
372
373 if (*current == '+') {
374 // Ignore leading sign; Skip following spaces.
Erik Corry 2010/03/25 12:26:16 Skip -> skip
375 ++current;
376 if (!AdvanceToNonspace(&current, end)) return JUNK_STRING_VALUE;
377 } else if (*current == '-') {
378 buffer[buffer_pos++] = '-';
379 ++current;
380 if (!AdvanceToNonspace(&current, end)) return JUNK_STRING_VALUE;
381 signed_zero = -0.0;
382 }
383
384 static const char infinity_symbol[] = "Infinity";
Erik Corry 2010/03/25 12:26:16 Name as a constant.
385 if (*current == infinity_symbol[0]) {
386 if (!SubStringEquals(&current, end, infinity_symbol)) {
387 return JUNK_STRING_VALUE;
388 }
389
390 if (!allow_trailing_junk && AdvanceToNonspace(&current, end)) {
391 return JUNK_STRING_VALUE;
392 }
393
394 return (buffer_pos > 0 && buffer[0] == '-') ? -V8_INFINITY : V8_INFINITY;
395 }
396
397 bool leading_zero = false;
398 if (*current == '0') {
399 ++current;
400 if (current == end) return signed_zero;
401
402 leading_zero = true;
403
404 // It could be hexadecimal value.
405 if ((flags & ALLOW_HEX) && (*current == 'x' || *current == 'X')) {
406 ++current;
407 if (current == end) return JUNK_STRING_VALUE; // "0x".
408
409 double result = InternalHexidecimalStringToDouble(current,
Erik Corry 2010/03/25 12:26:16 Hexi -> Hexa
410 end,
411 buffer + buffer_pos,
412 allow_trailing_junk);
413 return (buffer_pos > 0 && buffer[0] == '-') ? -result : result;
Erik Corry 2010/03/25 12:26:16 Should this be buffer[buffer_pos - 1]?
Florian Loitsch 2010/03/25 13:51:35 lgtm.
414 }
415
416 // Ignore leading zeros in the integer part.
417 while (*current == '0') {
418 ++current;
419 if (current == end) return signed_zero;
420 }
421 }
422
423 bool octal = leading_zero && (flags & ALLOW_OCTALS) != 0;
424
425 // Copy significant digits of the integer part (if any) to the buffer.
426 while (*current >= '0' && *current <= '9') {
427 if (significant_digits < max_significant_digits) {
428 ASSERT(buffer_pos < buffer_size);
429 buffer[buffer_pos++] = *current;
430 significant_digits++;
431 // Will later check if it's an octal in the buffer.
432 } else {
433 insignificant_digits++; // Move the digit into the exponential part.
434 nonzero_digit_dropped = nonzero_digit_dropped || *current != '0';
435 }
436 octal = octal && *current < '8';
437 ++current;
438 if (current == end) goto parsing_done;
439 }
440
441 if (*current == '.') {
442 ASSERT(buffer_pos < buffer_size);
443 buffer[buffer_pos++] = '.';
444 ++current;
445 if (current == end) {
446 if (significant_digits == 0 && !leading_zero) {
447 return JUNK_STRING_VALUE;
448 } else {
449 goto parsing_done;
450 }
451 }
452
453 if (significant_digits == 0) {
454 octal = false;
455 // Integer part consists of 0 or is absent. Significant digits start after
456 // leading zeros (if any).
457 while (*current == '0') {
458 ++current;
459 if (current == end) return signed_zero;
460 exponent--; // Move this 0 into the exponent.
461 }
462 }
463
464 // There is the fractional part.
465 while (*current >= '0' && *current <= '9') {
466 if (significant_digits < max_significant_digits) {
467 ASSERT(buffer_pos < buffer_size);
468 buffer[buffer_pos++] = *current;
469 significant_digits++;
470 } else {
471 // Ignore insignificant digits in the fractional part.
472 nonzero_digit_dropped = nonzero_digit_dropped || *current != '0';
473 }
474 ++current;
475 if (current == end) goto parsing_done;
476 }
477 }
478
479 if (!leading_zero && exponent == 0 && significant_digits == 0) {
480 // If leading_zeros is true then the string contains zeros.
481 // If exponent < 0 then string was [+-]\.0*...
482 // If significant_digits != 0 the string is not equal to 0.
483 // Otherwise there is no digits in the string.
288 return JUNK_STRING_VALUE; 484 return JUNK_STRING_VALUE;
289 } 485 }
290 486
291 // Compute sign of result based on first character. 487 // Parse exponential part.
292 int sign = 1; 488 if (*current == 'e' || *current == 'E') {
293 if (first == '-') { 489 if (octal) return JUNK_STRING_VALUE;
294 sign = -1; 490 ++current;
295 index++; 491 if (current == end) {
296 // String only containing a '-' are junk chars. 492 if (allow_trailing_junk) {
297 if (index == len) return JUNK_STRING_VALUE; 493 goto parsing_done;
298 } 494 } else {
299 495 return JUNK_STRING_VALUE;
300 // do we have a hex number? 496 }
301 // (since the string is 0-terminated, it's ok to look one char beyond the end) 497 }
302 if ((flags & ALLOW_HEX) != 0 && 498 char sign = '+';
303 (index + 1) < len && 499 if (*current == '+' || *current == '-') {
304 GetChar(str, index) == '0' && 500 sign = *current;
305 (GetChar(str, index + 1) == 'x' || GetChar(str, index + 1) == 'X')) { 501 ++current;
306 index += 2; 502 if (current == end) {
307 index = StringToInt(str, index, 16, &result); 503 if (allow_trailing_junk) {
308 } else if ((flags & ALLOW_OCTALS) != 0 && ShouldParseOctal(str, index)) { 504 goto parsing_done;
309 // NOTE: We optimistically try to parse the number as an octal (if 505 } else {
310 // we're allowed to), even though this is not as dictated by 506 return JUNK_STRING_VALUE;
311 // ECMA-262. The reason for doing this is compatibility with IE and 507 }
312 // Firefox. 508 }
313 index = StringToInt(str, index, 8, &result); 509 }
510
511 if (current == end || *current < '0' || *current > '9') {
512 if (allow_trailing_junk) {
513 goto parsing_done;
514 } else {
515 return JUNK_STRING_VALUE;
516 }
517 }
518
519 const int max_exponent = INT_MAX / 2;
520 ASSERT(-max_exponent / 2 <= exponent && exponent <= max_exponent / 2);
521 int num = 0;
522 do {
523 // Check overflow.
524 int digit = *current - '0';
525 if (num >= max_exponent / 10
526 && !(num == max_exponent / 10 && digit <= max_exponent % 10)) {
527 num = max_exponent;
528 } else {
529 num = num * 10 + digit;
530 }
531 ++current;
532 } while (current != end && *current >= '0' && *current <= '9');
533
534 exponent += (sign == '-' ? -num : num);
535 }
536
537 if (!allow_trailing_junk && AdvanceToNonspace(&current, end)) {
538 return JUNK_STRING_VALUE;
539 }
540
541 parsing_done:
542 exponent += insignificant_digits;
543
544 if (octal) {
545 buffer[buffer_pos] = '\0';
546 // ALLOW_OCTALS has set and there is no '8' and '9' in insignificant
Erik Corry 2010/03/25 12:26:16 '8' and '9' -> '8' or '9'
Florian Loitsch 2010/03/25 13:51:35 ALLOW_OCTALS is set ...
547 // digits. Check significant digits now.
548 char sign = '+';
549 const char* s = buffer;
550 if (*s == '-' || *s == '+') sign = *s++;
551
552 double result;
553 s += StringToInt(s, 0, 8, &result);
554 if (!allow_trailing_junk && *s != '\0') return JUNK_STRING_VALUE;
555
556 if (sign == '-') result = -result;
557 if (insignificant_digits > 0) {
558 result *= pow(8.0, insignificant_digits);
559 }
560 return result;
561 }
562
563 if (nonzero_digit_dropped) {
564 if (insignificant_digits) buffer[buffer_pos++] = '.';
565 buffer[buffer_pos++] = '1';
566 }
567
568 if (exponent != 0) {
569 ASSERT(buffer_pos < buffer_size);
570 buffer[buffer_pos++] = 'e';
571 if (exponent < 0) {
572 ASSERT(buffer_pos < buffer_size);
573 buffer[buffer_pos++] = '-';
574 exponent = -exponent;
575 }
576 if (exponent > 999) exponent = 999; // Result will be Infinity or 0 or -0.
577
578 const int exp_digits = 3;
579 for (int i = 0; i < exp_digits; i++) {
580 buffer[buffer_pos + exp_digits - 1 - i] = '0' + exponent % 10;
581 exponent /= 10;
582 }
583 ASSERT(exponent == 0);
584 buffer_pos += exp_digits;
585 }
586
587 ASSERT(buffer_pos < buffer_size);
588 buffer[buffer_pos] = '\0';
589
590 return gay_strtod(buffer, NULL);
591 }
592
593 double StringToDouble(String* str, int flags, double empty_string_val) {
594 StringShape shape(str);
595 if (shape.IsSequentialAscii()) {
596 const char* begin = SeqAsciiString::cast(str)->GetChars();
597 const char* end = begin + str->length();
598 return InternalStringToDouble(begin, end, flags, empty_string_val);
599 } else if (shape.IsSequentialTwoByte()) {
600 const uc16* begin = SeqTwoByteString::cast(str)->GetChars();
601 const uc16* end = begin + str->length();
602 return InternalStringToDouble(begin, end, flags, empty_string_val);
314 } else { 603 } else {
315 const char* cstr = GetCString(str, index); 604 StringInputBuffer buffer(str);
316 const char* end; 605 return InternalStringToDouble(StringInputBufferIterator(&buffer),
317 // Optimistically parse the number and then, if that fails, 606 StringInputBufferIterator::EndMarker(),
318 // check if it might have been {+,-,}Infinity. 607 flags,
319 result = gay_strtod(cstr, &end); 608 empty_string_val);
320 ReleaseCString(str, cstr); 609 }
321 if (result != 0.0 || end != cstr) {
322 // It appears that strtod worked
323 index += static_cast<int>(end - cstr);
324 } else {
325 // Check for {+,-,}Infinity
326 bool is_negative = (GetChar(str, index) == '-');
327 if (GetChar(str, index) == '+' || GetChar(str, index) == '-')
328 index++;
329 if (!SubStringEquals(str, index, "Infinity"))
330 return JUNK_STRING_VALUE;
331 result = is_negative ? -V8_INFINITY : V8_INFINITY;
332 index += 8;
333 }
334 }
335
336 if ((flags & ALLOW_TRAILING_JUNK) == 0) {
337 // skip trailing spaces
338 while ((index < len) && IsSpace(str, index)) index++;
339 // string ending with junk?
340 if (index < len) return JUNK_STRING_VALUE;
341 }
342
343 return sign * result;
344 }
345
346
347 double StringToDouble(String* str, int flags, double empty_string_val) {
348 return InternalStringToDouble(str, flags, empty_string_val);
349 } 610 }
350 611
351 612
352 double StringToDouble(const char* str, int flags, double empty_string_val) { 613 double StringToDouble(const char* str, int flags, double empty_string_val) {
353 return InternalStringToDouble(str, flags, empty_string_val); 614 const char* end = str + StrLength(str);
354 } 615
355 616 return InternalStringToDouble(str, end, flags, empty_string_val);
356 617 }
618
619
357 extern "C" char* dtoa(double d, int mode, int ndigits, 620 extern "C" char* dtoa(double d, int mode, int ndigits,
358 int* decpt, int* sign, char** rve); 621 int* decpt, int* sign, char** rve);
359 622
360 extern "C" void freedtoa(char* s); 623 extern "C" void freedtoa(char* s);
361 624
362 const char* DoubleToCString(double v, Vector<char> buffer) { 625 const char* DoubleToCString(double v, Vector<char> buffer) {
363 StringBuilder builder(buffer.start(), buffer.length()); 626 StringBuilder builder(buffer.start(), buffer.length());
364 627
365 switch (fpclassify(v)) { 628 switch (fpclassify(v)) {
366 case FP_NAN: 629 case FP_NAN:
(...skipping 343 matching lines...) Expand 10 before | Expand all | Expand 10 after
710 // Allocate result and fill in the parts. 973 // Allocate result and fill in the parts.
711 StringBuilder builder(result_size + 1); 974 StringBuilder builder(result_size + 1);
712 builder.AddSubstring(integer_buffer + integer_pos + 1, integer_part_size); 975 builder.AddSubstring(integer_buffer + integer_pos + 1, integer_part_size);
713 if (decimal_pos > 0) builder.AddCharacter('.'); 976 if (decimal_pos > 0) builder.AddCharacter('.');
714 builder.AddSubstring(decimal_buffer, decimal_pos); 977 builder.AddSubstring(decimal_buffer, decimal_pos);
715 return builder.Finalize(); 978 return builder.Finalize();
716 } 979 }
717 980
718 981
719 } } // namespace v8::internal 982 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « no previous file | test/cctest/test-conversions.cc » ('j') | test/cctest/test-conversions.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698