blob: a954d6cc694748e2629451fe816d400ac9c6459d [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// Copyright 2006-2008 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 <stdarg.h>
Steve Block6ded16b2010-05-10 14:33:55 +010029#include <limits.h>
Steve Blocka7e24c12009-10-30 11:49:00 +000030
31#include "v8.h"
32
33#include "conversions-inl.h"
Kristian Monsen25f61362010-05-21 11:50:48 +010034#include "dtoa.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000035#include "factory.h"
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -080036#include "scanner-base.h"
Ben Murdochf87a2032010-10-22 12:50:53 +010037#include "strtod.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000038
39namespace v8 {
40namespace internal {
41
Steve Block6ded16b2010-05-10 14:33:55 +010042namespace {
Steve Blocka7e24c12009-10-30 11:49:00 +000043
Steve Block6ded16b2010-05-10 14:33:55 +010044// C++-style iterator adaptor for StringInputBuffer
45// (unlike C++ iterators the end-marker has different type).
46class StringInputBufferIterator {
47 public:
48 class EndMarker {};
49
50 explicit StringInputBufferIterator(StringInputBuffer* buffer);
51
52 int operator*() const;
53 void operator++();
54 bool operator==(EndMarker const&) const { return end_; }
55 bool operator!=(EndMarker const& m) const { return !end_; }
56
57 private:
58 StringInputBuffer* const buffer_;
59 int current_;
60 bool end_;
61};
62
63
64StringInputBufferIterator::StringInputBufferIterator(
65 StringInputBuffer* buffer) : buffer_(buffer) {
66 ++(*this);
67}
68
69int StringInputBufferIterator::operator*() const {
70 return current_;
Steve Blocka7e24c12009-10-30 11:49:00 +000071}
72
73
Steve Block6ded16b2010-05-10 14:33:55 +010074void StringInputBufferIterator::operator++() {
75 end_ = !buffer_->has_more();
76 if (!end_) {
77 current_ = buffer_->GetNext();
Steve Blocka7e24c12009-10-30 11:49:00 +000078 }
Steve Block6ded16b2010-05-10 14:33:55 +010079}
Steve Blocka7e24c12009-10-30 11:49:00 +000080}
81
82
Steve Block6ded16b2010-05-10 14:33:55 +010083template <class Iterator, class EndMark>
84static bool SubStringEquals(Iterator* current,
85 EndMark end,
86 const char* substring) {
87 ASSERT(**current == *substring);
88 for (substring++; *substring != '\0'; substring++) {
89 ++*current;
90 if (*current == end || **current != *substring) return false;
Steve Blocka7e24c12009-10-30 11:49:00 +000091 }
Steve Block6ded16b2010-05-10 14:33:55 +010092 ++*current;
Steve Blocka7e24c12009-10-30 11:49:00 +000093 return true;
94}
95
96
Steve Block6ded16b2010-05-10 14:33:55 +010097// Maximum number of significant digits in decimal representation.
98// The longest possible double in decimal representation is
99// (2^53 - 1) * 2 ^ -1074 that is (2 ^ 53 - 1) * 5 ^ 1074 / 10 ^ 1074
100// (768 digits). If we parse a number whose first digits are equal to a
101// mean of 2 adjacent doubles (that could have up to 769 digits) the result
102// must be rounded to the bigger one unless the tail consists of zeros, so
103// we don't need to preserve all the digits.
104const int kMaxSignificantDigits = 772;
Steve Blocka7e24c12009-10-30 11:49:00 +0000105
Steve Blocka7e24c12009-10-30 11:49:00 +0000106
Steve Block6ded16b2010-05-10 14:33:55 +0100107static const double JUNK_STRING_VALUE = OS::nan_value();
108
109
110// Returns true if a nonspace found and false if the end has reached.
111template <class Iterator, class EndMark>
112static inline bool AdvanceToNonspace(Iterator* current, EndMark end) {
113 while (*current != end) {
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800114 if (!ScannerConstants::kIsWhiteSpace.get(**current)) return true;
Steve Block6ded16b2010-05-10 14:33:55 +0100115 ++*current;
116 }
117 return false;
118}
119
120
121static bool isDigit(int x, int radix) {
122 return (x >= '0' && x <= '9' && x < '0' + radix)
123 || (radix > 10 && x >= 'a' && x < 'a' + radix - 10)
124 || (radix > 10 && x >= 'A' && x < 'A' + radix - 10);
125}
126
127
128static double SignedZero(bool sign) {
129 return sign ? -0.0 : 0.0;
130}
131
132
133// Parsing integers with radix 2, 4, 8, 16, 32. Assumes current != end.
134template <int radix_log_2, class Iterator, class EndMark>
135static double InternalStringToIntDouble(Iterator current,
136 EndMark end,
137 bool sign,
138 bool allow_trailing_junk) {
139 ASSERT(current != end);
140
141 // Skip leading 0s.
142 while (*current == '0') {
143 ++current;
144 if (current == end) return SignedZero(sign);
145 }
146
147 int64_t number = 0;
148 int exponent = 0;
149 const int radix = (1 << radix_log_2);
150
151 do {
152 int digit;
153 if (*current >= '0' && *current <= '9' && *current < '0' + radix) {
154 digit = static_cast<char>(*current) - '0';
155 } else if (radix > 10 && *current >= 'a' && *current < 'a' + radix - 10) {
156 digit = static_cast<char>(*current) - 'a' + 10;
157 } else if (radix > 10 && *current >= 'A' && *current < 'A' + radix - 10) {
158 digit = static_cast<char>(*current) - 'A' + 10;
159 } else {
160 if (allow_trailing_junk || !AdvanceToNonspace(&current, end)) {
161 break;
162 } else {
163 return JUNK_STRING_VALUE;
164 }
165 }
166
167 number = number * radix + digit;
168 int overflow = static_cast<int>(number >> 53);
169 if (overflow != 0) {
170 // Overflow occurred. Need to determine which direction to round the
171 // result.
172 int overflow_bits_count = 1;
173 while (overflow > 1) {
174 overflow_bits_count++;
175 overflow >>= 1;
176 }
177
178 int dropped_bits_mask = ((1 << overflow_bits_count) - 1);
179 int dropped_bits = static_cast<int>(number) & dropped_bits_mask;
180 number >>= overflow_bits_count;
181 exponent = overflow_bits_count;
182
183 bool zero_tail = true;
184 while (true) {
185 ++current;
186 if (current == end || !isDigit(*current, radix)) break;
187 zero_tail = zero_tail && *current == '0';
188 exponent += radix_log_2;
189 }
190
191 if (!allow_trailing_junk && AdvanceToNonspace(&current, end)) {
192 return JUNK_STRING_VALUE;
193 }
194
195 int middle_value = (1 << (overflow_bits_count - 1));
196 if (dropped_bits > middle_value) {
197 number++; // Rounding up.
198 } else if (dropped_bits == middle_value) {
199 // Rounding to even to consistency with decimals: half-way case rounds
200 // up if significant part is odd and down otherwise.
201 if ((number & 1) != 0 || !zero_tail) {
202 number++; // Rounding up.
203 }
204 }
205
206 // Rounding up may cause overflow.
207 if ((number & ((int64_t)1 << 53)) != 0) {
208 exponent++;
209 number >>= 1;
210 }
211 break;
212 }
213 ++current;
214 } while (current != end);
215
216 ASSERT(number < ((int64_t)1 << 53));
217 ASSERT(static_cast<int64_t>(static_cast<double>(number)) == number);
218
219 if (exponent == 0) {
220 if (sign) {
221 if (number == 0) return -0.0;
222 number = -number;
223 }
224 return static_cast<double>(number);
225 }
226
227 ASSERT(number != 0);
228 // The double could be constructed faster from number (mantissa), exponent
229 // and sign. Assuming it's a rare case more simple code is used.
230 return static_cast<double>(sign ? -number : number) * pow(2.0, exponent);
231}
232
233
234template <class Iterator, class EndMark>
235static double InternalStringToInt(Iterator current, EndMark end, int radix) {
236 const bool allow_trailing_junk = true;
237 const double empty_string_val = JUNK_STRING_VALUE;
238
239 if (!AdvanceToNonspace(&current, end)) return empty_string_val;
240
241 bool sign = false;
242 bool leading_zero = false;
243
244 if (*current == '+') {
245 // Ignore leading sign; skip following spaces.
246 ++current;
247 if (!AdvanceToNonspace(&current, end)) return JUNK_STRING_VALUE;
248 } else if (*current == '-') {
249 ++current;
250 if (!AdvanceToNonspace(&current, end)) return JUNK_STRING_VALUE;
251 sign = true;
252 }
253
254 if (radix == 0) {
255 // Radix detection.
256 if (*current == '0') {
257 ++current;
258 if (current == end) return SignedZero(sign);
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(sign);
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(sign);
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 current, end, sign, allow_trailing_junk);
302 case 4:
303 return InternalStringToIntDouble<2>(
304 current, end, sign, allow_trailing_junk);
305 case 8:
306 return InternalStringToIntDouble<3>(
307 current, end, sign, allow_trailing_junk);
308
309 case 16:
310 return InternalStringToIntDouble<4>(
311 current, end, sign, allow_trailing_junk);
312
313 case 32:
314 return InternalStringToIntDouble<5>(
315 current, end, sign, 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 && AdvanceToNonspace(&current, end)) {
341 return JUNK_STRING_VALUE;
342 }
343
344 ASSERT(buffer_pos < kBufferSize);
Ben Murdochf87a2032010-10-22 12:50:53 +0100345 buffer[buffer_pos] = '\0';
346 Vector<const char> buffer_vector(buffer, buffer_pos);
347 return sign ? -Strtod(buffer_vector, 0) : Strtod(buffer_vector, 0);
Steve Block6ded16b2010-05-10 14:33:55 +0100348 }
349
350 // The following code causes accumulating rounding error for numbers greater
351 // than ~2^56. It's explicitly allowed in the spec: "if R is not 2, 4, 8, 10,
352 // 16, or 32, then mathInt may be an implementation-dependent approximation to
353 // the mathematical integer value" (15.1.2.2).
354
Steve Blocka7e24c12009-10-30 11:49:00 +0000355 int lim_0 = '0' + (radix < 10 ? radix : 10);
356 int lim_a = 'a' + (radix - 10);
357 int lim_A = 'A' + (radix - 10);
358
359 // NOTE: The code for computing the value may seem a bit complex at
360 // first glance. It is structured to use 32-bit multiply-and-add
361 // loops as long as possible to avoid loosing precision.
362
363 double v = 0.0;
Steve Block6ded16b2010-05-10 14:33:55 +0100364 bool done = false;
365 do {
Steve Blocka7e24c12009-10-30 11:49:00 +0000366 // Parse the longest part of the string starting at index j
367 // possible while keeping the multiplier, and thus the part
368 // itself, within 32 bits.
Steve Block6ded16b2010-05-10 14:33:55 +0100369 unsigned int part = 0, multiplier = 1;
370 while (true) {
371 int d;
372 if (*current >= '0' && *current < lim_0) {
373 d = *current - '0';
374 } else if (*current >= 'a' && *current < lim_a) {
375 d = *current - 'a' + 10;
376 } else if (*current >= 'A' && *current < lim_A) {
377 d = *current - 'A' + 10;
Steve Blocka7e24c12009-10-30 11:49:00 +0000378 } else {
Steve Block6ded16b2010-05-10 14:33:55 +0100379 done = true;
Steve Blocka7e24c12009-10-30 11:49:00 +0000380 break;
381 }
382
383 // Update the value of the part as long as the multiplier fits
384 // in 32 bits. When we can't guarantee that the next iteration
385 // will not overflow the multiplier, we stop parsing the part
386 // by leaving the loop.
Steve Block6ded16b2010-05-10 14:33:55 +0100387 const unsigned int kMaximumMultiplier = 0xffffffffU / 36;
Steve Blocka7e24c12009-10-30 11:49:00 +0000388 uint32_t m = multiplier * radix;
389 if (m > kMaximumMultiplier) break;
Steve Block6ded16b2010-05-10 14:33:55 +0100390 part = part * radix + d;
Steve Blocka7e24c12009-10-30 11:49:00 +0000391 multiplier = m;
392 ASSERT(multiplier > part);
Steve Block6ded16b2010-05-10 14:33:55 +0100393
394 ++current;
395 if (current == end) {
396 done = true;
397 break;
398 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000399 }
400
Steve Blocka7e24c12009-10-30 11:49:00 +0000401 // Update the value and skip the part in the string.
Steve Blocka7e24c12009-10-30 11:49:00 +0000402 v = v * multiplier + part;
Steve Block6ded16b2010-05-10 14:33:55 +0100403 } while (!done);
Steve Blocka7e24c12009-10-30 11:49:00 +0000404
Steve Block6ded16b2010-05-10 14:33:55 +0100405 if (!allow_trailing_junk && AdvanceToNonspace(&current, end)) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000406 return JUNK_STRING_VALUE;
407 }
408
Steve Block6ded16b2010-05-10 14:33:55 +0100409 return sign ? -v : v;
Steve Blocka7e24c12009-10-30 11:49:00 +0000410}
411
412
Steve Block6ded16b2010-05-10 14:33:55 +0100413// Converts a string to a double value. Assumes the Iterator supports
414// the following operations:
415// 1. current == end (other ops are not allowed), current != end.
416// 2. *current - gets the current character in the sequence.
417// 3. ++current (advances the position).
418template <class Iterator, class EndMark>
419static double InternalStringToDouble(Iterator current,
420 EndMark end,
421 int flags,
422 double empty_string_val) {
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 be 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 (!AdvanceToNonspace(&current, end)) return empty_string_val;
432
433 const bool allow_trailing_junk = (flags & ALLOW_TRAILING_JUNK) != 0;
434
435 // The longest form of simplified number is: "-<significant digits>'.1eXXX\0".
436 const int kBufferSize = kMaxSignificantDigits + 10;
437 char buffer[kBufferSize]; // NOLINT: size is known at compile time.
438 int buffer_pos = 0;
439
440 // Exponent will be adjusted if insignificant digits of the integer part
441 // or insignificant leading zeros of the fractional part are dropped.
442 int exponent = 0;
443 int significant_digits = 0;
444 int insignificant_digits = 0;
445 bool nonzero_digit_dropped = false;
446 bool fractional_part = false;
447
448 bool sign = false;
449
450 if (*current == '+') {
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800451 // Ignore leading sign.
Steve Block6ded16b2010-05-10 14:33:55 +0100452 ++current;
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800453 if (current == end) return JUNK_STRING_VALUE;
Steve Block6ded16b2010-05-10 14:33:55 +0100454 } else if (*current == '-') {
Steve Block6ded16b2010-05-10 14:33:55 +0100455 ++current;
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800456 if (current == end) return JUNK_STRING_VALUE;
Steve Block6ded16b2010-05-10 14:33:55 +0100457 sign = true;
458 }
459
460 static const char kInfinitySymbol[] = "Infinity";
461 if (*current == kInfinitySymbol[0]) {
462 if (!SubStringEquals(&current, end, kInfinitySymbol)) {
463 return JUNK_STRING_VALUE;
464 }
465
466 if (!allow_trailing_junk && AdvanceToNonspace(&current, end)) {
467 return JUNK_STRING_VALUE;
468 }
469
Ben Murdochf87a2032010-10-22 12:50:53 +0100470 ASSERT(buffer_pos == 0);
471 return sign ? -V8_INFINITY : V8_INFINITY;
Steve Block6ded16b2010-05-10 14:33:55 +0100472 }
473
474 bool leading_zero = false;
475 if (*current == '0') {
476 ++current;
477 if (current == end) return SignedZero(sign);
478
479 leading_zero = true;
480
481 // It could be hexadecimal value.
482 if ((flags & ALLOW_HEX) && (*current == 'x' || *current == 'X')) {
483 ++current;
484 if (current == end || !isDigit(*current, 16)) {
485 return JUNK_STRING_VALUE; // "0x".
486 }
487
Steve Block6ded16b2010-05-10 14:33:55 +0100488 return InternalStringToIntDouble<4>(current,
489 end,
490 sign,
491 allow_trailing_junk);
492 }
493
494 // Ignore leading zeros in the integer part.
495 while (*current == '0') {
496 ++current;
497 if (current == end) return SignedZero(sign);
498 }
499 }
500
501 bool octal = leading_zero && (flags & ALLOW_OCTALS) != 0;
502
503 // Copy significant digits of the integer part (if any) to the buffer.
504 while (*current >= '0' && *current <= '9') {
505 if (significant_digits < kMaxSignificantDigits) {
506 ASSERT(buffer_pos < kBufferSize);
507 buffer[buffer_pos++] = static_cast<char>(*current);
508 significant_digits++;
509 // Will later check if it's an octal in the buffer.
510 } else {
511 insignificant_digits++; // Move the digit into the exponential part.
512 nonzero_digit_dropped = nonzero_digit_dropped || *current != '0';
513 }
514 octal = octal && *current < '8';
515 ++current;
516 if (current == end) goto parsing_done;
517 }
518
519 if (significant_digits == 0) {
520 octal = false;
521 }
522
523 if (*current == '.') {
Ben Murdochf87a2032010-10-22 12:50:53 +0100524 if (octal && !allow_trailing_junk) return JUNK_STRING_VALUE;
525 if (octal) goto parsing_done;
526
Steve Block6ded16b2010-05-10 14:33:55 +0100527 ++current;
528 if (current == end) {
529 if (significant_digits == 0 && !leading_zero) {
530 return JUNK_STRING_VALUE;
531 } else {
532 goto parsing_done;
533 }
534 }
535
536 if (significant_digits == 0) {
537 // octal = false;
538 // Integer part consists of 0 or is absent. Significant digits start after
539 // leading zeros (if any).
540 while (*current == '0') {
541 ++current;
542 if (current == end) return SignedZero(sign);
543 exponent--; // Move this 0 into the exponent.
544 }
545 }
546
Ben Murdochf87a2032010-10-22 12:50:53 +0100547 // We don't emit a '.', but adjust the exponent instead.
Steve Block6ded16b2010-05-10 14:33:55 +0100548 fractional_part = true;
549
Ben Murdochf87a2032010-10-22 12:50:53 +0100550 // There is a fractional part.
Steve Block6ded16b2010-05-10 14:33:55 +0100551 while (*current >= '0' && *current <= '9') {
552 if (significant_digits < kMaxSignificantDigits) {
553 ASSERT(buffer_pos < kBufferSize);
554 buffer[buffer_pos++] = static_cast<char>(*current);
555 significant_digits++;
Ben Murdochf87a2032010-10-22 12:50:53 +0100556 exponent--;
Steve Block6ded16b2010-05-10 14:33:55 +0100557 } else {
558 // Ignore insignificant digits in the fractional part.
559 nonzero_digit_dropped = nonzero_digit_dropped || *current != '0';
560 }
561 ++current;
562 if (current == end) goto parsing_done;
563 }
564 }
565
566 if (!leading_zero && exponent == 0 && significant_digits == 0) {
567 // If leading_zeros is true then the string contains zeros.
568 // If exponent < 0 then string was [+-]\.0*...
569 // If significant_digits != 0 the string is not equal to 0.
570 // Otherwise there are no digits in the string.
571 return JUNK_STRING_VALUE;
572 }
573
574 // Parse exponential part.
575 if (*current == 'e' || *current == 'E') {
576 if (octal) return JUNK_STRING_VALUE;
577 ++current;
578 if (current == end) {
579 if (allow_trailing_junk) {
580 goto parsing_done;
581 } else {
582 return JUNK_STRING_VALUE;
583 }
584 }
585 char sign = '+';
586 if (*current == '+' || *current == '-') {
587 sign = static_cast<char>(*current);
588 ++current;
589 if (current == end) {
590 if (allow_trailing_junk) {
591 goto parsing_done;
592 } else {
593 return JUNK_STRING_VALUE;
594 }
595 }
596 }
597
598 if (current == end || *current < '0' || *current > '9') {
599 if (allow_trailing_junk) {
600 goto parsing_done;
601 } else {
602 return JUNK_STRING_VALUE;
603 }
604 }
605
606 const int max_exponent = INT_MAX / 2;
607 ASSERT(-max_exponent / 2 <= exponent && exponent <= max_exponent / 2);
608 int num = 0;
609 do {
610 // Check overflow.
611 int digit = *current - '0';
612 if (num >= max_exponent / 10
613 && !(num == max_exponent / 10 && digit <= max_exponent % 10)) {
614 num = max_exponent;
615 } else {
616 num = num * 10 + digit;
617 }
618 ++current;
619 } while (current != end && *current >= '0' && *current <= '9');
620
621 exponent += (sign == '-' ? -num : num);
622 }
623
624 if (!allow_trailing_junk && AdvanceToNonspace(&current, end)) {
625 return JUNK_STRING_VALUE;
626 }
627
628 parsing_done:
629 exponent += insignificant_digits;
630
631 if (octal) {
Ben Murdochf87a2032010-10-22 12:50:53 +0100632 return InternalStringToIntDouble<3>(buffer,
Steve Block6ded16b2010-05-10 14:33:55 +0100633 buffer + buffer_pos,
634 sign,
635 allow_trailing_junk);
636 }
637
638 if (nonzero_digit_dropped) {
Steve Block6ded16b2010-05-10 14:33:55 +0100639 buffer[buffer_pos++] = '1';
Ben Murdochf87a2032010-10-22 12:50:53 +0100640 exponent--;
Steve Block6ded16b2010-05-10 14:33:55 +0100641 }
642
643 ASSERT(buffer_pos < kBufferSize);
644 buffer[buffer_pos] = '\0';
645
Ben Murdochf87a2032010-10-22 12:50:53 +0100646 double converted = Strtod(Vector<const char>(buffer, buffer_pos), exponent);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800647 return sign ? -converted : converted;
Steve Block6ded16b2010-05-10 14:33:55 +0100648}
649
Ben Murdochf87a2032010-10-22 12:50:53 +0100650
Steve Blocka7e24c12009-10-30 11:49:00 +0000651double StringToDouble(String* str, int flags, double empty_string_val) {
Steve Block6ded16b2010-05-10 14:33:55 +0100652 StringShape shape(str);
653 if (shape.IsSequentialAscii()) {
654 const char* begin = SeqAsciiString::cast(str)->GetChars();
655 const char* end = begin + str->length();
656 return InternalStringToDouble(begin, end, flags, empty_string_val);
657 } else if (shape.IsSequentialTwoByte()) {
658 const uc16* begin = SeqTwoByteString::cast(str)->GetChars();
659 const uc16* end = begin + str->length();
660 return InternalStringToDouble(begin, end, flags, empty_string_val);
661 } else {
662 StringInputBuffer buffer(str);
663 return InternalStringToDouble(StringInputBufferIterator(&buffer),
664 StringInputBufferIterator::EndMarker(),
665 flags,
666 empty_string_val);
667 }
668}
669
670
671double StringToInt(String* str, int radix) {
672 StringShape shape(str);
673 if (shape.IsSequentialAscii()) {
674 const char* begin = SeqAsciiString::cast(str)->GetChars();
675 const char* end = begin + str->length();
676 return InternalStringToInt(begin, end, radix);
677 } else if (shape.IsSequentialTwoByte()) {
678 const uc16* begin = SeqTwoByteString::cast(str)->GetChars();
679 const uc16* end = begin + str->length();
680 return InternalStringToInt(begin, end, radix);
681 } else {
682 StringInputBuffer buffer(str);
683 return InternalStringToInt(StringInputBufferIterator(&buffer),
684 StringInputBufferIterator::EndMarker(),
685 radix);
686 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000687}
688
689
690double StringToDouble(const char* str, int flags, double empty_string_val) {
Steve Block6ded16b2010-05-10 14:33:55 +0100691 const char* end = str + StrLength(str);
Steve Block6ded16b2010-05-10 14:33:55 +0100692 return InternalStringToDouble(str, end, flags, empty_string_val);
Steve Blocka7e24c12009-10-30 11:49:00 +0000693}
694
695
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100696double StringToDouble(Vector<const char> str,
697 int flags,
698 double empty_string_val) {
699 const char* end = str.start() + str.length();
700 return InternalStringToDouble(str.start(), end, flags, empty_string_val);
701}
702
703
Steve Blocka7e24c12009-10-30 11:49:00 +0000704const char* DoubleToCString(double v, Vector<char> buffer) {
705 StringBuilder builder(buffer.start(), buffer.length());
706
707 switch (fpclassify(v)) {
708 case FP_NAN:
709 builder.AddString("NaN");
710 break;
711
712 case FP_INFINITE:
713 if (v < 0.0) {
714 builder.AddString("-Infinity");
715 } else {
716 builder.AddString("Infinity");
717 }
718 break;
719
720 case FP_ZERO:
721 builder.AddCharacter('0');
722 break;
723
724 default: {
725 int decimal_point;
726 int sign;
Kristian Monsen25f61362010-05-21 11:50:48 +0100727 const int kV8DtoaBufferCapacity = kBase10MaximalLength + 1;
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800728 char decimal_rep[kV8DtoaBufferCapacity];
Steve Block6ded16b2010-05-10 14:33:55 +0100729 int length;
Kristian Monsen25f61362010-05-21 11:50:48 +0100730
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800731 DoubleToAscii(v, DTOA_SHORTEST, 0,
732 Vector<char>(decimal_rep, kV8DtoaBufferCapacity),
733 &sign, &length, &decimal_point);
Steve Blocka7e24c12009-10-30 11:49:00 +0000734
735 if (sign) builder.AddCharacter('-');
736
737 if (length <= decimal_point && decimal_point <= 21) {
738 // ECMA-262 section 9.8.1 step 6.
739 builder.AddString(decimal_rep);
740 builder.AddPadding('0', decimal_point - length);
741
742 } else if (0 < decimal_point && decimal_point <= 21) {
743 // ECMA-262 section 9.8.1 step 7.
744 builder.AddSubstring(decimal_rep, decimal_point);
745 builder.AddCharacter('.');
746 builder.AddString(decimal_rep + decimal_point);
747
748 } else if (decimal_point <= 0 && decimal_point > -6) {
749 // ECMA-262 section 9.8.1 step 8.
750 builder.AddString("0.");
751 builder.AddPadding('0', -decimal_point);
752 builder.AddString(decimal_rep);
753
754 } else {
755 // ECMA-262 section 9.8.1 step 9 and 10 combined.
756 builder.AddCharacter(decimal_rep[0]);
757 if (length != 1) {
758 builder.AddCharacter('.');
759 builder.AddString(decimal_rep + 1);
760 }
761 builder.AddCharacter('e');
762 builder.AddCharacter((decimal_point >= 0) ? '+' : '-');
763 int exponent = decimal_point - 1;
764 if (exponent < 0) exponent = -exponent;
765 builder.AddFormatted("%d", exponent);
766 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000767 }
768 }
769 return builder.Finalize();
770}
771
772
773const char* IntToCString(int n, Vector<char> buffer) {
774 bool negative = false;
775 if (n < 0) {
776 // We must not negate the most negative int.
777 if (n == kMinInt) return DoubleToCString(n, buffer);
778 negative = true;
779 n = -n;
780 }
781 // Build the string backwards from the least significant digit.
782 int i = buffer.length();
783 buffer[--i] = '\0';
784 do {
785 buffer[--i] = '0' + (n % 10);
786 n /= 10;
787 } while (n);
788 if (negative) buffer[--i] = '-';
789 return buffer.start() + i;
790}
791
792
793char* DoubleToFixedCString(double value, int f) {
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800794 const int kMaxDigitsBeforePoint = 21;
Kristian Monsen25f61362010-05-21 11:50:48 +0100795 const double kFirstNonFixed = 1e21;
796 const int kMaxDigitsAfterPoint = 20;
Steve Blocka7e24c12009-10-30 11:49:00 +0000797 ASSERT(f >= 0);
Kristian Monsen25f61362010-05-21 11:50:48 +0100798 ASSERT(f <= kMaxDigitsAfterPoint);
Steve Blocka7e24c12009-10-30 11:49:00 +0000799
800 bool negative = false;
801 double abs_value = value;
802 if (value < 0) {
803 abs_value = -value;
804 negative = true;
805 }
806
Kristian Monsen25f61362010-05-21 11:50:48 +0100807 // If abs_value has more than kMaxDigitsBeforePoint digits before the point
808 // use the non-fixed conversion routine.
809 if (abs_value >= kFirstNonFixed) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000810 char arr[100];
811 Vector<char> buffer(arr, ARRAY_SIZE(arr));
812 return StrDup(DoubleToCString(value, buffer));
813 }
814
815 // Find a sufficiently precise decimal representation of n.
816 int decimal_point;
817 int sign;
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800818 // Add space for the '\0' byte.
Kristian Monsen25f61362010-05-21 11:50:48 +0100819 const int kDecimalRepCapacity =
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800820 kMaxDigitsBeforePoint + kMaxDigitsAfterPoint + 1;
Kristian Monsen25f61362010-05-21 11:50:48 +0100821 char decimal_rep[kDecimalRepCapacity];
822 int decimal_rep_length;
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800823 DoubleToAscii(value, DTOA_FIXED, f,
824 Vector<char>(decimal_rep, kDecimalRepCapacity),
825 &sign, &decimal_rep_length, &decimal_point);
Steve Blocka7e24c12009-10-30 11:49:00 +0000826
827 // Create a representation that is padded with zeros if needed.
828 int zero_prefix_length = 0;
829 int zero_postfix_length = 0;
830
831 if (decimal_point <= 0) {
832 zero_prefix_length = -decimal_point + 1;
833 decimal_point = 1;
834 }
835
836 if (zero_prefix_length + decimal_rep_length < decimal_point + f) {
837 zero_postfix_length = decimal_point + f - decimal_rep_length -
838 zero_prefix_length;
839 }
840
841 unsigned rep_length =
842 zero_prefix_length + decimal_rep_length + zero_postfix_length;
843 StringBuilder rep_builder(rep_length + 1);
844 rep_builder.AddPadding('0', zero_prefix_length);
845 rep_builder.AddString(decimal_rep);
846 rep_builder.AddPadding('0', zero_postfix_length);
847 char* rep = rep_builder.Finalize();
Steve Blocka7e24c12009-10-30 11:49:00 +0000848
849 // Create the result string by appending a minus and putting in a
850 // decimal point if needed.
851 unsigned result_size = decimal_point + f + 2;
852 StringBuilder builder(result_size + 1);
853 if (negative) builder.AddCharacter('-');
854 builder.AddSubstring(rep, decimal_point);
855 if (f > 0) {
856 builder.AddCharacter('.');
857 builder.AddSubstring(rep + decimal_point, f);
858 }
859 DeleteArray(rep);
860 return builder.Finalize();
861}
862
863
864static char* CreateExponentialRepresentation(char* decimal_rep,
865 int exponent,
866 bool negative,
867 int significant_digits) {
868 bool negative_exponent = false;
869 if (exponent < 0) {
870 negative_exponent = true;
871 exponent = -exponent;
872 }
873
874 // Leave room in the result for appending a minus, for a period, the
875 // letter 'e', a minus or a plus depending on the exponent, and a
876 // three digit exponent.
877 unsigned result_size = significant_digits + 7;
878 StringBuilder builder(result_size + 1);
879
880 if (negative) builder.AddCharacter('-');
881 builder.AddCharacter(decimal_rep[0]);
882 if (significant_digits != 1) {
883 builder.AddCharacter('.');
884 builder.AddString(decimal_rep + 1);
Steve Blockd0582a62009-12-15 09:54:21 +0000885 int rep_length = StrLength(decimal_rep);
886 builder.AddPadding('0', significant_digits - rep_length);
Steve Blocka7e24c12009-10-30 11:49:00 +0000887 }
888
889 builder.AddCharacter('e');
890 builder.AddCharacter(negative_exponent ? '-' : '+');
891 builder.AddFormatted("%d", exponent);
892 return builder.Finalize();
893}
894
895
896
897char* DoubleToExponentialCString(double value, int f) {
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100898 const int kMaxDigitsAfterPoint = 20;
Steve Blocka7e24c12009-10-30 11:49:00 +0000899 // f might be -1 to signal that f was undefined in JavaScript.
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100900 ASSERT(f >= -1 && f <= kMaxDigitsAfterPoint);
Steve Blocka7e24c12009-10-30 11:49:00 +0000901
902 bool negative = false;
903 if (value < 0) {
904 value = -value;
905 negative = true;
906 }
907
908 // Find a sufficiently precise decimal representation of n.
909 int decimal_point;
910 int sign;
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100911 // f corresponds to the digits after the point. There is always one digit
912 // before the point. The number of requested_digits equals hence f + 1.
913 // And we have to add one character for the null-terminator.
914 const int kV8DtoaBufferCapacity = kMaxDigitsAfterPoint + 1 + 1;
915 // Make sure that the buffer is big enough, even if we fall back to the
916 // shortest representation (which happens when f equals -1).
917 ASSERT(kBase10MaximalLength <= kMaxDigitsAfterPoint + 1);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800918 char decimal_rep[kV8DtoaBufferCapacity];
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100919 int decimal_rep_length;
920
Steve Blocka7e24c12009-10-30 11:49:00 +0000921 if (f == -1) {
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800922 DoubleToAscii(value, DTOA_SHORTEST, 0,
923 Vector<char>(decimal_rep, kV8DtoaBufferCapacity),
924 &sign, &decimal_rep_length, &decimal_point);
925 f = decimal_rep_length - 1;
Steve Blocka7e24c12009-10-30 11:49:00 +0000926 } else {
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800927 DoubleToAscii(value, DTOA_PRECISION, f + 1,
928 Vector<char>(decimal_rep, kV8DtoaBufferCapacity),
929 &sign, &decimal_rep_length, &decimal_point);
Steve Blocka7e24c12009-10-30 11:49:00 +0000930 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000931 ASSERT(decimal_rep_length > 0);
932 ASSERT(decimal_rep_length <= f + 1);
Steve Blocka7e24c12009-10-30 11:49:00 +0000933
934 int exponent = decimal_point - 1;
935 char* result =
936 CreateExponentialRepresentation(decimal_rep, exponent, negative, f+1);
937
Steve Blocka7e24c12009-10-30 11:49:00 +0000938 return result;
939}
940
941
942char* DoubleToPrecisionCString(double value, int p) {
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100943 const int kMinimalDigits = 1;
944 const int kMaximalDigits = 21;
945 ASSERT(p >= kMinimalDigits && p <= kMaximalDigits);
946 USE(kMinimalDigits);
Steve Blocka7e24c12009-10-30 11:49:00 +0000947
948 bool negative = false;
949 if (value < 0) {
950 value = -value;
951 negative = true;
952 }
953
954 // Find a sufficiently precise decimal representation of n.
955 int decimal_point;
956 int sign;
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100957 // Add one for the terminating null character.
958 const int kV8DtoaBufferCapacity = kMaximalDigits + 1;
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800959 char decimal_rep[kV8DtoaBufferCapacity];
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100960 int decimal_rep_length;
961
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800962 DoubleToAscii(value, DTOA_PRECISION, p,
963 Vector<char>(decimal_rep, kV8DtoaBufferCapacity),
964 &sign, &decimal_rep_length, &decimal_point);
Steve Blocka7e24c12009-10-30 11:49:00 +0000965 ASSERT(decimal_rep_length <= p);
966
967 int exponent = decimal_point - 1;
968
969 char* result = NULL;
970
971 if (exponent < -6 || exponent >= p) {
972 result =
973 CreateExponentialRepresentation(decimal_rep, exponent, negative, p);
974 } else {
975 // Use fixed notation.
976 //
977 // Leave room in the result for appending a minus, a period and in
978 // the case where decimal_point is not positive for a zero in
979 // front of the period.
980 unsigned result_size = (decimal_point <= 0)
981 ? -decimal_point + p + 3
982 : p + 2;
983 StringBuilder builder(result_size + 1);
984 if (negative) builder.AddCharacter('-');
985 if (decimal_point <= 0) {
986 builder.AddString("0.");
987 builder.AddPadding('0', -decimal_point);
988 builder.AddString(decimal_rep);
989 builder.AddPadding('0', p - decimal_rep_length);
990 } else {
991 const int m = Min(decimal_rep_length, decimal_point);
992 builder.AddSubstring(decimal_rep, m);
993 builder.AddPadding('0', decimal_point - decimal_rep_length);
994 if (decimal_point < p) {
995 builder.AddCharacter('.');
996 const int extra = negative ? 2 : 1;
997 if (decimal_rep_length > decimal_point) {
Steve Blockd0582a62009-12-15 09:54:21 +0000998 const int len = StrLength(decimal_rep + decimal_point);
Steve Blocka7e24c12009-10-30 11:49:00 +0000999 const int n = Min(len, p - (builder.position() - extra));
1000 builder.AddSubstring(decimal_rep + decimal_point, n);
1001 }
1002 builder.AddPadding('0', extra + (p - builder.position()));
1003 }
1004 }
1005 result = builder.Finalize();
1006 }
1007
Steve Blocka7e24c12009-10-30 11:49:00 +00001008 return result;
1009}
1010
1011
1012char* DoubleToRadixCString(double value, int radix) {
1013 ASSERT(radix >= 2 && radix <= 36);
1014
1015 // Character array used for conversion.
1016 static const char chars[] = "0123456789abcdefghijklmnopqrstuvwxyz";
1017
1018 // Buffer for the integer part of the result. 1024 chars is enough
1019 // for max integer value in radix 2. We need room for a sign too.
1020 static const int kBufferSize = 1100;
1021 char integer_buffer[kBufferSize];
1022 integer_buffer[kBufferSize - 1] = '\0';
1023
1024 // Buffer for the decimal part of the result. We only generate up
1025 // to kBufferSize - 1 chars for the decimal part.
1026 char decimal_buffer[kBufferSize];
1027 decimal_buffer[kBufferSize - 1] = '\0';
1028
1029 // Make sure the value is positive.
1030 bool is_negative = value < 0.0;
1031 if (is_negative) value = -value;
1032
1033 // Get the integer part and the decimal part.
1034 double integer_part = floor(value);
1035 double decimal_part = value - integer_part;
1036
1037 // Convert the integer part starting from the back. Always generate
1038 // at least one digit.
1039 int integer_pos = kBufferSize - 2;
1040 do {
1041 integer_buffer[integer_pos--] =
Steve Block3ce2e202009-11-05 08:53:23 +00001042 chars[static_cast<int>(modulo(integer_part, radix))];
Steve Blocka7e24c12009-10-30 11:49:00 +00001043 integer_part /= radix;
1044 } while (integer_part >= 1.0);
1045 // Sanity check.
1046 ASSERT(integer_pos > 0);
1047 // Add sign if needed.
1048 if (is_negative) integer_buffer[integer_pos--] = '-';
1049
1050 // Convert the decimal part. Repeatedly multiply by the radix to
1051 // generate the next char. Never generate more than kBufferSize - 1
1052 // chars.
1053 //
1054 // TODO(1093998): We will often generate a full decimal_buffer of
1055 // chars because hitting zero will often not happen. The right
1056 // solution would be to continue until the string representation can
1057 // be read back and yield the original value. To implement this
1058 // efficiently, we probably have to modify dtoa.
1059 int decimal_pos = 0;
1060 while ((decimal_part > 0.0) && (decimal_pos < kBufferSize - 1)) {
1061 decimal_part *= radix;
1062 decimal_buffer[decimal_pos++] =
1063 chars[static_cast<int>(floor(decimal_part))];
1064 decimal_part -= floor(decimal_part);
1065 }
1066 decimal_buffer[decimal_pos] = '\0';
1067
1068 // Compute the result size.
1069 int integer_part_size = kBufferSize - 2 - integer_pos;
1070 // Make room for zero termination.
1071 unsigned result_size = integer_part_size + decimal_pos;
1072 // If the number has a decimal part, leave room for the period.
1073 if (decimal_pos > 0) result_size++;
1074 // Allocate result and fill in the parts.
1075 StringBuilder builder(result_size + 1);
1076 builder.AddSubstring(integer_buffer + integer_pos + 1, integer_part_size);
1077 if (decimal_pos > 0) builder.AddCharacter('.');
1078 builder.AddSubstring(decimal_buffer, decimal_pos);
1079 return builder.Finalize();
1080}
1081
1082
1083} } // namespace v8::internal