blob: a348235d6689e4467ea293d01c78543ed327b36f [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
Steve Block1e0659c2011-05-24 12:43:12 +0100128static double SignedZero(bool negative) {
129 return negative ? -0.0 : 0.0;
Steve Block6ded16b2010-05-10 14:33:55 +0100130}
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,
Steve Block1e0659c2011-05-24 12:43:12 +0100137 bool negative,
Steve Block6ded16b2010-05-10 14:33:55 +0100138 bool allow_trailing_junk) {
139 ASSERT(current != end);
140
141 // Skip leading 0s.
142 while (*current == '0') {
143 ++current;
Steve Block1e0659c2011-05-24 12:43:12 +0100144 if (current == end) return SignedZero(negative);
Steve Block6ded16b2010-05-10 14:33:55 +0100145 }
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) {
Steve Block1e0659c2011-05-24 12:43:12 +0100220 if (negative) {
Steve Block6ded16b2010-05-10 14:33:55 +0100221 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.
Steve Block1e0659c2011-05-24 12:43:12 +0100230 return static_cast<double>(negative ? -number : number) * pow(2.0, exponent);
Steve Block6ded16b2010-05-10 14:33:55 +0100231}
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
Steve Block1e0659c2011-05-24 12:43:12 +0100241 bool negative = false;
Steve Block6ded16b2010-05-10 14:33:55 +0100242 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;
Steve Block1e0659c2011-05-24 12:43:12 +0100251 negative = true;
Steve Block6ded16b2010-05-10 14:33:55 +0100252 }
253
254 if (radix == 0) {
255 // Radix detection.
256 if (*current == '0') {
257 ++current;
Steve Block1e0659c2011-05-24 12:43:12 +0100258 if (current == end) return SignedZero(negative);
Steve Block6ded16b2010-05-10 14:33:55 +0100259 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;
Steve Block1e0659c2011-05-24 12:43:12 +0100274 if (current == end) return SignedZero(negative);
Steve Block6ded16b2010-05-10 14:33:55 +0100275 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;
Steve Block1e0659c2011-05-24 12:43:12 +0100290 if (current == end) return SignedZero(negative);
Steve Block6ded16b2010-05-10 14:33:55 +0100291 }
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>(
Steve Block1e0659c2011-05-24 12:43:12 +0100301 current, end, negative, allow_trailing_junk);
Steve Block6ded16b2010-05-10 14:33:55 +0100302 case 4:
303 return InternalStringToIntDouble<2>(
Steve Block1e0659c2011-05-24 12:43:12 +0100304 current, end, negative, allow_trailing_junk);
Steve Block6ded16b2010-05-10 14:33:55 +0100305 case 8:
306 return InternalStringToIntDouble<3>(
Steve Block1e0659c2011-05-24 12:43:12 +0100307 current, end, negative, allow_trailing_junk);
Steve Block6ded16b2010-05-10 14:33:55 +0100308
309 case 16:
310 return InternalStringToIntDouble<4>(
Steve Block1e0659c2011-05-24 12:43:12 +0100311 current, end, negative, allow_trailing_junk);
Steve Block6ded16b2010-05-10 14:33:55 +0100312
313 case 32:
314 return InternalStringToIntDouble<5>(
Steve Block1e0659c2011-05-24 12:43:12 +0100315 current, end, negative, allow_trailing_junk);
Steve Block6ded16b2010-05-10 14:33:55 +0100316 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);
Steve Block1e0659c2011-05-24 12:43:12 +0100347 return negative ? -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 Block1e0659c2011-05-24 12:43:12 +0100409 return negative ? -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
Steve Block1e0659c2011-05-24 12:43:12 +0100448 bool negative = false;
Steve Block6ded16b2010-05-10 14:33:55 +0100449
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 Block1e0659c2011-05-24 12:43:12 +0100457 negative = true;
Steve Block6ded16b2010-05-10 14:33:55 +0100458 }
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);
Steve Block1e0659c2011-05-24 12:43:12 +0100471 return negative ? -V8_INFINITY : V8_INFINITY;
Steve Block6ded16b2010-05-10 14:33:55 +0100472 }
473
474 bool leading_zero = false;
475 if (*current == '0') {
476 ++current;
Steve Block1e0659c2011-05-24 12:43:12 +0100477 if (current == end) return SignedZero(negative);
Steve Block6ded16b2010-05-10 14:33:55 +0100478
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,
Steve Block1e0659c2011-05-24 12:43:12 +0100490 negative,
Steve Block6ded16b2010-05-10 14:33:55 +0100491 allow_trailing_junk);
492 }
493
494 // Ignore leading zeros in the integer part.
495 while (*current == '0') {
496 ++current;
Steve Block1e0659c2011-05-24 12:43:12 +0100497 if (current == end) return SignedZero(negative);
Steve Block6ded16b2010-05-10 14:33:55 +0100498 }
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;
Steve Block1e0659c2011-05-24 12:43:12 +0100542 if (current == end) return SignedZero(negative);
Steve Block6ded16b2010-05-10 14:33:55 +0100543 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,
Steve Block1e0659c2011-05-24 12:43:12 +0100634 negative,
Steve Block6ded16b2010-05-10 14:33:55 +0100635 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);
Steve Block1e0659c2011-05-24 12:43:12 +0100647 return negative ? -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) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000705 switch (fpclassify(v)) {
Steve Block1e0659c2011-05-24 12:43:12 +0100706 case FP_NAN: return "NaN";
707 case FP_INFINITE: return (v < 0.0 ? "-Infinity" : "Infinity");
708 case FP_ZERO: return "0";
Steve Blocka7e24c12009-10-30 11:49:00 +0000709 default: {
Steve Block1e0659c2011-05-24 12:43:12 +0100710 StringBuilder builder(buffer.start(), buffer.length());
Steve Blocka7e24c12009-10-30 11:49:00 +0000711 int decimal_point;
712 int sign;
Kristian Monsen25f61362010-05-21 11:50:48 +0100713 const int kV8DtoaBufferCapacity = kBase10MaximalLength + 1;
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800714 char decimal_rep[kV8DtoaBufferCapacity];
Steve Block6ded16b2010-05-10 14:33:55 +0100715 int length;
Kristian Monsen25f61362010-05-21 11:50:48 +0100716
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800717 DoubleToAscii(v, DTOA_SHORTEST, 0,
718 Vector<char>(decimal_rep, kV8DtoaBufferCapacity),
719 &sign, &length, &decimal_point);
Steve Blocka7e24c12009-10-30 11:49:00 +0000720
721 if (sign) builder.AddCharacter('-');
722
723 if (length <= decimal_point && decimal_point <= 21) {
724 // ECMA-262 section 9.8.1 step 6.
725 builder.AddString(decimal_rep);
726 builder.AddPadding('0', decimal_point - length);
727
728 } else if (0 < decimal_point && decimal_point <= 21) {
729 // ECMA-262 section 9.8.1 step 7.
730 builder.AddSubstring(decimal_rep, decimal_point);
731 builder.AddCharacter('.');
732 builder.AddString(decimal_rep + decimal_point);
733
734 } else if (decimal_point <= 0 && decimal_point > -6) {
735 // ECMA-262 section 9.8.1 step 8.
736 builder.AddString("0.");
737 builder.AddPadding('0', -decimal_point);
738 builder.AddString(decimal_rep);
739
740 } else {
741 // ECMA-262 section 9.8.1 step 9 and 10 combined.
742 builder.AddCharacter(decimal_rep[0]);
743 if (length != 1) {
744 builder.AddCharacter('.');
745 builder.AddString(decimal_rep + 1);
746 }
747 builder.AddCharacter('e');
748 builder.AddCharacter((decimal_point >= 0) ? '+' : '-');
749 int exponent = decimal_point - 1;
750 if (exponent < 0) exponent = -exponent;
751 builder.AddFormatted("%d", exponent);
752 }
Steve Block1e0659c2011-05-24 12:43:12 +0100753 return builder.Finalize();
Steve Blocka7e24c12009-10-30 11:49:00 +0000754 }
755 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000756}
757
758
759const char* IntToCString(int n, Vector<char> buffer) {
760 bool negative = false;
761 if (n < 0) {
762 // We must not negate the most negative int.
763 if (n == kMinInt) return DoubleToCString(n, buffer);
764 negative = true;
765 n = -n;
766 }
767 // Build the string backwards from the least significant digit.
768 int i = buffer.length();
769 buffer[--i] = '\0';
770 do {
771 buffer[--i] = '0' + (n % 10);
772 n /= 10;
773 } while (n);
774 if (negative) buffer[--i] = '-';
775 return buffer.start() + i;
776}
777
778
779char* DoubleToFixedCString(double value, int f) {
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800780 const int kMaxDigitsBeforePoint = 21;
Kristian Monsen25f61362010-05-21 11:50:48 +0100781 const double kFirstNonFixed = 1e21;
782 const int kMaxDigitsAfterPoint = 20;
Steve Blocka7e24c12009-10-30 11:49:00 +0000783 ASSERT(f >= 0);
Kristian Monsen25f61362010-05-21 11:50:48 +0100784 ASSERT(f <= kMaxDigitsAfterPoint);
Steve Blocka7e24c12009-10-30 11:49:00 +0000785
786 bool negative = false;
787 double abs_value = value;
788 if (value < 0) {
789 abs_value = -value;
790 negative = true;
791 }
792
Kristian Monsen25f61362010-05-21 11:50:48 +0100793 // If abs_value has more than kMaxDigitsBeforePoint digits before the point
794 // use the non-fixed conversion routine.
795 if (abs_value >= kFirstNonFixed) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000796 char arr[100];
797 Vector<char> buffer(arr, ARRAY_SIZE(arr));
798 return StrDup(DoubleToCString(value, buffer));
799 }
800
801 // Find a sufficiently precise decimal representation of n.
802 int decimal_point;
803 int sign;
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800804 // Add space for the '\0' byte.
Kristian Monsen25f61362010-05-21 11:50:48 +0100805 const int kDecimalRepCapacity =
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800806 kMaxDigitsBeforePoint + kMaxDigitsAfterPoint + 1;
Kristian Monsen25f61362010-05-21 11:50:48 +0100807 char decimal_rep[kDecimalRepCapacity];
808 int decimal_rep_length;
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800809 DoubleToAscii(value, DTOA_FIXED, f,
810 Vector<char>(decimal_rep, kDecimalRepCapacity),
811 &sign, &decimal_rep_length, &decimal_point);
Steve Blocka7e24c12009-10-30 11:49:00 +0000812
813 // Create a representation that is padded with zeros if needed.
814 int zero_prefix_length = 0;
815 int zero_postfix_length = 0;
816
817 if (decimal_point <= 0) {
818 zero_prefix_length = -decimal_point + 1;
819 decimal_point = 1;
820 }
821
822 if (zero_prefix_length + decimal_rep_length < decimal_point + f) {
823 zero_postfix_length = decimal_point + f - decimal_rep_length -
824 zero_prefix_length;
825 }
826
827 unsigned rep_length =
828 zero_prefix_length + decimal_rep_length + zero_postfix_length;
829 StringBuilder rep_builder(rep_length + 1);
830 rep_builder.AddPadding('0', zero_prefix_length);
831 rep_builder.AddString(decimal_rep);
832 rep_builder.AddPadding('0', zero_postfix_length);
833 char* rep = rep_builder.Finalize();
Steve Blocka7e24c12009-10-30 11:49:00 +0000834
835 // Create the result string by appending a minus and putting in a
836 // decimal point if needed.
837 unsigned result_size = decimal_point + f + 2;
838 StringBuilder builder(result_size + 1);
839 if (negative) builder.AddCharacter('-');
840 builder.AddSubstring(rep, decimal_point);
841 if (f > 0) {
842 builder.AddCharacter('.');
843 builder.AddSubstring(rep + decimal_point, f);
844 }
845 DeleteArray(rep);
846 return builder.Finalize();
847}
848
849
850static char* CreateExponentialRepresentation(char* decimal_rep,
851 int exponent,
852 bool negative,
853 int significant_digits) {
854 bool negative_exponent = false;
855 if (exponent < 0) {
856 negative_exponent = true;
857 exponent = -exponent;
858 }
859
860 // Leave room in the result for appending a minus, for a period, the
861 // letter 'e', a minus or a plus depending on the exponent, and a
862 // three digit exponent.
863 unsigned result_size = significant_digits + 7;
864 StringBuilder builder(result_size + 1);
865
866 if (negative) builder.AddCharacter('-');
867 builder.AddCharacter(decimal_rep[0]);
868 if (significant_digits != 1) {
869 builder.AddCharacter('.');
870 builder.AddString(decimal_rep + 1);
Steve Blockd0582a62009-12-15 09:54:21 +0000871 int rep_length = StrLength(decimal_rep);
872 builder.AddPadding('0', significant_digits - rep_length);
Steve Blocka7e24c12009-10-30 11:49:00 +0000873 }
874
875 builder.AddCharacter('e');
876 builder.AddCharacter(negative_exponent ? '-' : '+');
877 builder.AddFormatted("%d", exponent);
878 return builder.Finalize();
879}
880
881
882
883char* DoubleToExponentialCString(double value, int f) {
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100884 const int kMaxDigitsAfterPoint = 20;
Steve Blocka7e24c12009-10-30 11:49:00 +0000885 // f might be -1 to signal that f was undefined in JavaScript.
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100886 ASSERT(f >= -1 && f <= kMaxDigitsAfterPoint);
Steve Blocka7e24c12009-10-30 11:49:00 +0000887
888 bool negative = false;
889 if (value < 0) {
890 value = -value;
891 negative = true;
892 }
893
894 // Find a sufficiently precise decimal representation of n.
895 int decimal_point;
896 int sign;
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100897 // f corresponds to the digits after the point. There is always one digit
898 // before the point. The number of requested_digits equals hence f + 1.
899 // And we have to add one character for the null-terminator.
900 const int kV8DtoaBufferCapacity = kMaxDigitsAfterPoint + 1 + 1;
901 // Make sure that the buffer is big enough, even if we fall back to the
902 // shortest representation (which happens when f equals -1).
903 ASSERT(kBase10MaximalLength <= kMaxDigitsAfterPoint + 1);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800904 char decimal_rep[kV8DtoaBufferCapacity];
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100905 int decimal_rep_length;
906
Steve Blocka7e24c12009-10-30 11:49:00 +0000907 if (f == -1) {
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800908 DoubleToAscii(value, DTOA_SHORTEST, 0,
909 Vector<char>(decimal_rep, kV8DtoaBufferCapacity),
910 &sign, &decimal_rep_length, &decimal_point);
911 f = decimal_rep_length - 1;
Steve Blocka7e24c12009-10-30 11:49:00 +0000912 } else {
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800913 DoubleToAscii(value, DTOA_PRECISION, f + 1,
914 Vector<char>(decimal_rep, kV8DtoaBufferCapacity),
915 &sign, &decimal_rep_length, &decimal_point);
Steve Blocka7e24c12009-10-30 11:49:00 +0000916 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000917 ASSERT(decimal_rep_length > 0);
918 ASSERT(decimal_rep_length <= f + 1);
Steve Blocka7e24c12009-10-30 11:49:00 +0000919
920 int exponent = decimal_point - 1;
921 char* result =
922 CreateExponentialRepresentation(decimal_rep, exponent, negative, f+1);
923
Steve Blocka7e24c12009-10-30 11:49:00 +0000924 return result;
925}
926
927
928char* DoubleToPrecisionCString(double value, int p) {
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100929 const int kMinimalDigits = 1;
930 const int kMaximalDigits = 21;
931 ASSERT(p >= kMinimalDigits && p <= kMaximalDigits);
932 USE(kMinimalDigits);
Steve Blocka7e24c12009-10-30 11:49:00 +0000933
934 bool negative = false;
935 if (value < 0) {
936 value = -value;
937 negative = true;
938 }
939
940 // Find a sufficiently precise decimal representation of n.
941 int decimal_point;
942 int sign;
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100943 // Add one for the terminating null character.
944 const int kV8DtoaBufferCapacity = kMaximalDigits + 1;
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800945 char decimal_rep[kV8DtoaBufferCapacity];
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100946 int decimal_rep_length;
947
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800948 DoubleToAscii(value, DTOA_PRECISION, p,
949 Vector<char>(decimal_rep, kV8DtoaBufferCapacity),
950 &sign, &decimal_rep_length, &decimal_point);
Steve Blocka7e24c12009-10-30 11:49:00 +0000951 ASSERT(decimal_rep_length <= p);
952
953 int exponent = decimal_point - 1;
954
955 char* result = NULL;
956
957 if (exponent < -6 || exponent >= p) {
958 result =
959 CreateExponentialRepresentation(decimal_rep, exponent, negative, p);
960 } else {
961 // Use fixed notation.
962 //
963 // Leave room in the result for appending a minus, a period and in
964 // the case where decimal_point is not positive for a zero in
965 // front of the period.
966 unsigned result_size = (decimal_point <= 0)
967 ? -decimal_point + p + 3
968 : p + 2;
969 StringBuilder builder(result_size + 1);
970 if (negative) builder.AddCharacter('-');
971 if (decimal_point <= 0) {
972 builder.AddString("0.");
973 builder.AddPadding('0', -decimal_point);
974 builder.AddString(decimal_rep);
975 builder.AddPadding('0', p - decimal_rep_length);
976 } else {
977 const int m = Min(decimal_rep_length, decimal_point);
978 builder.AddSubstring(decimal_rep, m);
979 builder.AddPadding('0', decimal_point - decimal_rep_length);
980 if (decimal_point < p) {
981 builder.AddCharacter('.');
982 const int extra = negative ? 2 : 1;
983 if (decimal_rep_length > decimal_point) {
Steve Blockd0582a62009-12-15 09:54:21 +0000984 const int len = StrLength(decimal_rep + decimal_point);
Steve Blocka7e24c12009-10-30 11:49:00 +0000985 const int n = Min(len, p - (builder.position() - extra));
986 builder.AddSubstring(decimal_rep + decimal_point, n);
987 }
988 builder.AddPadding('0', extra + (p - builder.position()));
989 }
990 }
991 result = builder.Finalize();
992 }
993
Steve Blocka7e24c12009-10-30 11:49:00 +0000994 return result;
995}
996
997
998char* DoubleToRadixCString(double value, int radix) {
999 ASSERT(radix >= 2 && radix <= 36);
1000
1001 // Character array used for conversion.
1002 static const char chars[] = "0123456789abcdefghijklmnopqrstuvwxyz";
1003
1004 // Buffer for the integer part of the result. 1024 chars is enough
1005 // for max integer value in radix 2. We need room for a sign too.
1006 static const int kBufferSize = 1100;
1007 char integer_buffer[kBufferSize];
1008 integer_buffer[kBufferSize - 1] = '\0';
1009
1010 // Buffer for the decimal part of the result. We only generate up
1011 // to kBufferSize - 1 chars for the decimal part.
1012 char decimal_buffer[kBufferSize];
1013 decimal_buffer[kBufferSize - 1] = '\0';
1014
1015 // Make sure the value is positive.
1016 bool is_negative = value < 0.0;
1017 if (is_negative) value = -value;
1018
1019 // Get the integer part and the decimal part.
1020 double integer_part = floor(value);
1021 double decimal_part = value - integer_part;
1022
1023 // Convert the integer part starting from the back. Always generate
1024 // at least one digit.
1025 int integer_pos = kBufferSize - 2;
1026 do {
1027 integer_buffer[integer_pos--] =
Steve Block3ce2e202009-11-05 08:53:23 +00001028 chars[static_cast<int>(modulo(integer_part, radix))];
Steve Blocka7e24c12009-10-30 11:49:00 +00001029 integer_part /= radix;
1030 } while (integer_part >= 1.0);
1031 // Sanity check.
1032 ASSERT(integer_pos > 0);
1033 // Add sign if needed.
1034 if (is_negative) integer_buffer[integer_pos--] = '-';
1035
1036 // Convert the decimal part. Repeatedly multiply by the radix to
1037 // generate the next char. Never generate more than kBufferSize - 1
1038 // chars.
1039 //
1040 // TODO(1093998): We will often generate a full decimal_buffer of
1041 // chars because hitting zero will often not happen. The right
1042 // solution would be to continue until the string representation can
1043 // be read back and yield the original value. To implement this
1044 // efficiently, we probably have to modify dtoa.
1045 int decimal_pos = 0;
1046 while ((decimal_part > 0.0) && (decimal_pos < kBufferSize - 1)) {
1047 decimal_part *= radix;
1048 decimal_buffer[decimal_pos++] =
1049 chars[static_cast<int>(floor(decimal_part))];
1050 decimal_part -= floor(decimal_part);
1051 }
1052 decimal_buffer[decimal_pos] = '\0';
1053
1054 // Compute the result size.
1055 int integer_part_size = kBufferSize - 2 - integer_pos;
1056 // Make room for zero termination.
1057 unsigned result_size = integer_part_size + decimal_pos;
1058 // If the number has a decimal part, leave room for the period.
1059 if (decimal_pos > 0) result_size++;
1060 // Allocate result and fill in the parts.
1061 StringBuilder builder(result_size + 1);
1062 builder.AddSubstring(integer_buffer + integer_pos + 1, integer_part_size);
1063 if (decimal_pos > 0) builder.AddCharacter('.');
1064 builder.AddSubstring(decimal_buffer, decimal_pos);
1065 return builder.Finalize();
1066}
1067
1068
1069} } // namespace v8::internal