blob: 7867719968d2f89e279dc9632d1c0341d02202fb [file] [log] [blame]
Ben Murdoch8b112d22011-06-08 16:22:53 +01001// Copyright 2011 the V8 project authors. All rights reserved.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
Steve Blocka7e24c12009-10-30 11:49:00 +00004
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005#include "src/conversions.h"
6
Steve Block6ded16b2010-05-10 14:33:55 +01007#include <limits.h>
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008#include <stdarg.h>
9#include <cmath>
Steve Blocka7e24c12009-10-30 11:49:00 +000010
Ben Murdochb8a8cc12014-11-26 15:28:44 +000011#include "src/assert-scope.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000012#include "src/char-predicates-inl.h"
13#include "src/codegen.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000014#include "src/conversions-inl.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000015#include "src/dtoa.h"
16#include "src/factory.h"
17#include "src/list-inl.h"
18#include "src/strtod.h"
19#include "src/utils.h"
20
21#ifndef _STLP_VENDOR_CSTD
22// STLPort doesn't import fpclassify into the std namespace.
23using std::fpclassify;
24#endif
Steve Blocka7e24c12009-10-30 11:49:00 +000025
26namespace v8 {
27namespace internal {
28
Steve Blocka7e24c12009-10-30 11:49:00 +000029
Ben Murdochb8a8cc12014-11-26 15:28:44 +000030namespace {
31
32// C++-style iterator adaptor for StringCharacterStream
33// (unlike C++ iterators the end-marker has different type).
34class StringCharacterStreamIterator {
35 public:
36 class EndMarker {};
37
38 explicit StringCharacterStreamIterator(StringCharacterStream* stream);
39
40 uint16_t operator*() const;
41 void operator++();
42 bool operator==(EndMarker const&) const { return end_; }
43 bool operator!=(EndMarker const& m) const { return !end_; }
44
45 private:
46 StringCharacterStream* const stream_;
47 uint16_t current_;
48 bool end_;
49};
50
51
52StringCharacterStreamIterator::StringCharacterStreamIterator(
53 StringCharacterStream* stream) : stream_(stream) {
54 ++(*this);
55}
56
57uint16_t StringCharacterStreamIterator::operator*() const {
58 return current_;
59}
60
61
62void StringCharacterStreamIterator::operator++() {
63 end_ = !stream_->HasMore();
64 if (!end_) {
65 current_ = stream_->GetNext();
66 }
67}
68} // End anonymous namespace.
69
70
Ben Murdoch8b112d22011-06-08 16:22:53 +010071double StringToDouble(UnicodeCache* unicode_cache,
72 const char* str, int flags, double empty_string_val) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000073 // We cast to const uint8_t* here to avoid instantiating the
74 // InternalStringToDouble() template for const char* as well.
75 const uint8_t* start = reinterpret_cast<const uint8_t*>(str);
76 const uint8_t* end = start + StrLength(str);
77 return InternalStringToDouble(unicode_cache, start, end, flags,
Steve Block44f0eee2011-05-26 01:26:41 +010078 empty_string_val);
Steve Blocka7e24c12009-10-30 11:49:00 +000079}
80
81
Ben Murdoch8b112d22011-06-08 16:22:53 +010082double StringToDouble(UnicodeCache* unicode_cache,
Ben Murdochb8a8cc12014-11-26 15:28:44 +000083 Vector<const uint8_t> str,
Kristian Monsen80d68ea2010-09-08 11:05:35 +010084 int flags,
85 double empty_string_val) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000086 // We cast to const uint8_t* here to avoid instantiating the
87 // InternalStringToDouble() template for const char* as well.
88 const uint8_t* start = reinterpret_cast<const uint8_t*>(str.start());
89 const uint8_t* end = start + str.length();
90 return InternalStringToDouble(unicode_cache, start, end, flags,
Steve Block44f0eee2011-05-26 01:26:41 +010091 empty_string_val);
Kristian Monsen80d68ea2010-09-08 11:05:35 +010092}
93
Ben Murdochb8a8cc12014-11-26 15:28:44 +000094
Ben Murdoch257744e2011-11-30 15:57:28 +000095double StringToDouble(UnicodeCache* unicode_cache,
96 Vector<const uc16> str,
97 int flags,
98 double empty_string_val) {
99 const uc16* end = str.start() + str.length();
100 return InternalStringToDouble(unicode_cache, str.start(), end, flags,
101 empty_string_val);
102}
103
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100104
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000105// Converts a string into an integer.
106double StringToInt(UnicodeCache* unicode_cache,
107 Vector<const uint8_t> vector,
108 int radix) {
109 return InternalStringToInt(
110 unicode_cache, vector.start(), vector.start() + vector.length(), radix);
111}
112
113
114double StringToInt(UnicodeCache* unicode_cache,
115 Vector<const uc16> vector,
116 int radix) {
117 return InternalStringToInt(
118 unicode_cache, vector.start(), vector.start() + vector.length(), radix);
119}
120
121
Steve Blocka7e24c12009-10-30 11:49:00 +0000122const char* DoubleToCString(double v, Vector<char> buffer) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000123 switch (fpclassify(v)) {
Steve Block1e0659c2011-05-24 12:43:12 +0100124 case FP_NAN: return "NaN";
125 case FP_INFINITE: return (v < 0.0 ? "-Infinity" : "Infinity");
126 case FP_ZERO: return "0";
Steve Blocka7e24c12009-10-30 11:49:00 +0000127 default: {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000128 SimpleStringBuilder builder(buffer.start(), buffer.length());
Steve Blocka7e24c12009-10-30 11:49:00 +0000129 int decimal_point;
130 int sign;
Kristian Monsen25f61362010-05-21 11:50:48 +0100131 const int kV8DtoaBufferCapacity = kBase10MaximalLength + 1;
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800132 char decimal_rep[kV8DtoaBufferCapacity];
Steve Block6ded16b2010-05-10 14:33:55 +0100133 int length;
Kristian Monsen25f61362010-05-21 11:50:48 +0100134
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800135 DoubleToAscii(v, DTOA_SHORTEST, 0,
136 Vector<char>(decimal_rep, kV8DtoaBufferCapacity),
137 &sign, &length, &decimal_point);
Steve Blocka7e24c12009-10-30 11:49:00 +0000138
139 if (sign) builder.AddCharacter('-');
140
141 if (length <= decimal_point && decimal_point <= 21) {
142 // ECMA-262 section 9.8.1 step 6.
143 builder.AddString(decimal_rep);
144 builder.AddPadding('0', decimal_point - length);
145
146 } else if (0 < decimal_point && decimal_point <= 21) {
147 // ECMA-262 section 9.8.1 step 7.
148 builder.AddSubstring(decimal_rep, decimal_point);
149 builder.AddCharacter('.');
150 builder.AddString(decimal_rep + decimal_point);
151
152 } else if (decimal_point <= 0 && decimal_point > -6) {
153 // ECMA-262 section 9.8.1 step 8.
154 builder.AddString("0.");
155 builder.AddPadding('0', -decimal_point);
156 builder.AddString(decimal_rep);
157
158 } else {
159 // ECMA-262 section 9.8.1 step 9 and 10 combined.
160 builder.AddCharacter(decimal_rep[0]);
161 if (length != 1) {
162 builder.AddCharacter('.');
163 builder.AddString(decimal_rep + 1);
164 }
165 builder.AddCharacter('e');
166 builder.AddCharacter((decimal_point >= 0) ? '+' : '-');
167 int exponent = decimal_point - 1;
168 if (exponent < 0) exponent = -exponent;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000169 builder.AddDecimalInteger(exponent);
Steve Blocka7e24c12009-10-30 11:49:00 +0000170 }
Steve Block1e0659c2011-05-24 12:43:12 +0100171 return builder.Finalize();
Steve Blocka7e24c12009-10-30 11:49:00 +0000172 }
173 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000174}
175
176
177const char* IntToCString(int n, Vector<char> buffer) {
178 bool negative = false;
179 if (n < 0) {
180 // We must not negate the most negative int.
181 if (n == kMinInt) return DoubleToCString(n, buffer);
182 negative = true;
183 n = -n;
184 }
185 // Build the string backwards from the least significant digit.
186 int i = buffer.length();
187 buffer[--i] = '\0';
188 do {
189 buffer[--i] = '0' + (n % 10);
190 n /= 10;
191 } while (n);
192 if (negative) buffer[--i] = '-';
193 return buffer.start() + i;
194}
195
196
197char* DoubleToFixedCString(double value, int f) {
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800198 const int kMaxDigitsBeforePoint = 21;
Kristian Monsen25f61362010-05-21 11:50:48 +0100199 const double kFirstNonFixed = 1e21;
200 const int kMaxDigitsAfterPoint = 20;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000201 DCHECK(f >= 0);
202 DCHECK(f <= kMaxDigitsAfterPoint);
Steve Blocka7e24c12009-10-30 11:49:00 +0000203
204 bool negative = false;
205 double abs_value = value;
206 if (value < 0) {
207 abs_value = -value;
208 negative = true;
209 }
210
Kristian Monsen25f61362010-05-21 11:50:48 +0100211 // If abs_value has more than kMaxDigitsBeforePoint digits before the point
212 // use the non-fixed conversion routine.
213 if (abs_value >= kFirstNonFixed) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000214 char arr[100];
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000215 Vector<char> buffer(arr, arraysize(arr));
Steve Blocka7e24c12009-10-30 11:49:00 +0000216 return StrDup(DoubleToCString(value, buffer));
217 }
218
219 // Find a sufficiently precise decimal representation of n.
220 int decimal_point;
221 int sign;
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800222 // Add space for the '\0' byte.
Kristian Monsen25f61362010-05-21 11:50:48 +0100223 const int kDecimalRepCapacity =
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800224 kMaxDigitsBeforePoint + kMaxDigitsAfterPoint + 1;
Kristian Monsen25f61362010-05-21 11:50:48 +0100225 char decimal_rep[kDecimalRepCapacity];
226 int decimal_rep_length;
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800227 DoubleToAscii(value, DTOA_FIXED, f,
228 Vector<char>(decimal_rep, kDecimalRepCapacity),
229 &sign, &decimal_rep_length, &decimal_point);
Steve Blocka7e24c12009-10-30 11:49:00 +0000230
231 // Create a representation that is padded with zeros if needed.
232 int zero_prefix_length = 0;
233 int zero_postfix_length = 0;
234
235 if (decimal_point <= 0) {
236 zero_prefix_length = -decimal_point + 1;
237 decimal_point = 1;
238 }
239
240 if (zero_prefix_length + decimal_rep_length < decimal_point + f) {
241 zero_postfix_length = decimal_point + f - decimal_rep_length -
242 zero_prefix_length;
243 }
244
245 unsigned rep_length =
246 zero_prefix_length + decimal_rep_length + zero_postfix_length;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000247 SimpleStringBuilder rep_builder(rep_length + 1);
Steve Blocka7e24c12009-10-30 11:49:00 +0000248 rep_builder.AddPadding('0', zero_prefix_length);
249 rep_builder.AddString(decimal_rep);
250 rep_builder.AddPadding('0', zero_postfix_length);
251 char* rep = rep_builder.Finalize();
Steve Blocka7e24c12009-10-30 11:49:00 +0000252
253 // Create the result string by appending a minus and putting in a
254 // decimal point if needed.
255 unsigned result_size = decimal_point + f + 2;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000256 SimpleStringBuilder builder(result_size + 1);
Steve Blocka7e24c12009-10-30 11:49:00 +0000257 if (negative) builder.AddCharacter('-');
258 builder.AddSubstring(rep, decimal_point);
259 if (f > 0) {
260 builder.AddCharacter('.');
261 builder.AddSubstring(rep + decimal_point, f);
262 }
263 DeleteArray(rep);
264 return builder.Finalize();
265}
266
267
268static char* CreateExponentialRepresentation(char* decimal_rep,
269 int exponent,
270 bool negative,
271 int significant_digits) {
272 bool negative_exponent = false;
273 if (exponent < 0) {
274 negative_exponent = true;
275 exponent = -exponent;
276 }
277
278 // Leave room in the result for appending a minus, for a period, the
279 // letter 'e', a minus or a plus depending on the exponent, and a
280 // three digit exponent.
281 unsigned result_size = significant_digits + 7;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000282 SimpleStringBuilder builder(result_size + 1);
Steve Blocka7e24c12009-10-30 11:49:00 +0000283
284 if (negative) builder.AddCharacter('-');
285 builder.AddCharacter(decimal_rep[0]);
286 if (significant_digits != 1) {
287 builder.AddCharacter('.');
288 builder.AddString(decimal_rep + 1);
Steve Blockd0582a62009-12-15 09:54:21 +0000289 int rep_length = StrLength(decimal_rep);
290 builder.AddPadding('0', significant_digits - rep_length);
Steve Blocka7e24c12009-10-30 11:49:00 +0000291 }
292
293 builder.AddCharacter('e');
294 builder.AddCharacter(negative_exponent ? '-' : '+');
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000295 builder.AddDecimalInteger(exponent);
Steve Blocka7e24c12009-10-30 11:49:00 +0000296 return builder.Finalize();
297}
298
299
Steve Blocka7e24c12009-10-30 11:49:00 +0000300char* DoubleToExponentialCString(double value, int f) {
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100301 const int kMaxDigitsAfterPoint = 20;
Steve Blocka7e24c12009-10-30 11:49:00 +0000302 // f might be -1 to signal that f was undefined in JavaScript.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000303 DCHECK(f >= -1 && f <= kMaxDigitsAfterPoint);
Steve Blocka7e24c12009-10-30 11:49:00 +0000304
305 bool negative = false;
306 if (value < 0) {
307 value = -value;
308 negative = true;
309 }
310
311 // Find a sufficiently precise decimal representation of n.
312 int decimal_point;
313 int sign;
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100314 // f corresponds to the digits after the point. There is always one digit
315 // before the point. The number of requested_digits equals hence f + 1.
316 // And we have to add one character for the null-terminator.
317 const int kV8DtoaBufferCapacity = kMaxDigitsAfterPoint + 1 + 1;
318 // Make sure that the buffer is big enough, even if we fall back to the
319 // shortest representation (which happens when f equals -1).
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000320 DCHECK(kBase10MaximalLength <= kMaxDigitsAfterPoint + 1);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800321 char decimal_rep[kV8DtoaBufferCapacity];
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100322 int decimal_rep_length;
323
Steve Blocka7e24c12009-10-30 11:49:00 +0000324 if (f == -1) {
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800325 DoubleToAscii(value, DTOA_SHORTEST, 0,
326 Vector<char>(decimal_rep, kV8DtoaBufferCapacity),
327 &sign, &decimal_rep_length, &decimal_point);
328 f = decimal_rep_length - 1;
Steve Blocka7e24c12009-10-30 11:49:00 +0000329 } else {
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800330 DoubleToAscii(value, DTOA_PRECISION, f + 1,
331 Vector<char>(decimal_rep, kV8DtoaBufferCapacity),
332 &sign, &decimal_rep_length, &decimal_point);
Steve Blocka7e24c12009-10-30 11:49:00 +0000333 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000334 DCHECK(decimal_rep_length > 0);
335 DCHECK(decimal_rep_length <= f + 1);
Steve Blocka7e24c12009-10-30 11:49:00 +0000336
337 int exponent = decimal_point - 1;
338 char* result =
339 CreateExponentialRepresentation(decimal_rep, exponent, negative, f+1);
340
Steve Blocka7e24c12009-10-30 11:49:00 +0000341 return result;
342}
343
344
345char* DoubleToPrecisionCString(double value, int p) {
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100346 const int kMinimalDigits = 1;
347 const int kMaximalDigits = 21;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000348 DCHECK(p >= kMinimalDigits && p <= kMaximalDigits);
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100349 USE(kMinimalDigits);
Steve Blocka7e24c12009-10-30 11:49:00 +0000350
351 bool negative = false;
352 if (value < 0) {
353 value = -value;
354 negative = true;
355 }
356
357 // Find a sufficiently precise decimal representation of n.
358 int decimal_point;
359 int sign;
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100360 // Add one for the terminating null character.
361 const int kV8DtoaBufferCapacity = kMaximalDigits + 1;
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800362 char decimal_rep[kV8DtoaBufferCapacity];
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100363 int decimal_rep_length;
364
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800365 DoubleToAscii(value, DTOA_PRECISION, p,
366 Vector<char>(decimal_rep, kV8DtoaBufferCapacity),
367 &sign, &decimal_rep_length, &decimal_point);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000368 DCHECK(decimal_rep_length <= p);
Steve Blocka7e24c12009-10-30 11:49:00 +0000369
370 int exponent = decimal_point - 1;
371
372 char* result = NULL;
373
374 if (exponent < -6 || exponent >= p) {
375 result =
376 CreateExponentialRepresentation(decimal_rep, exponent, negative, p);
377 } else {
378 // Use fixed notation.
379 //
380 // Leave room in the result for appending a minus, a period and in
381 // the case where decimal_point is not positive for a zero in
382 // front of the period.
383 unsigned result_size = (decimal_point <= 0)
384 ? -decimal_point + p + 3
385 : p + 2;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000386 SimpleStringBuilder builder(result_size + 1);
Steve Blocka7e24c12009-10-30 11:49:00 +0000387 if (negative) builder.AddCharacter('-');
388 if (decimal_point <= 0) {
389 builder.AddString("0.");
390 builder.AddPadding('0', -decimal_point);
391 builder.AddString(decimal_rep);
392 builder.AddPadding('0', p - decimal_rep_length);
393 } else {
394 const int m = Min(decimal_rep_length, decimal_point);
395 builder.AddSubstring(decimal_rep, m);
396 builder.AddPadding('0', decimal_point - decimal_rep_length);
397 if (decimal_point < p) {
398 builder.AddCharacter('.');
399 const int extra = negative ? 2 : 1;
400 if (decimal_rep_length > decimal_point) {
Steve Blockd0582a62009-12-15 09:54:21 +0000401 const int len = StrLength(decimal_rep + decimal_point);
Steve Blocka7e24c12009-10-30 11:49:00 +0000402 const int n = Min(len, p - (builder.position() - extra));
403 builder.AddSubstring(decimal_rep + decimal_point, n);
404 }
405 builder.AddPadding('0', extra + (p - builder.position()));
406 }
407 }
408 result = builder.Finalize();
409 }
410
Steve Blocka7e24c12009-10-30 11:49:00 +0000411 return result;
412}
413
414
415char* DoubleToRadixCString(double value, int radix) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000416 DCHECK(radix >= 2 && radix <= 36);
Steve Blocka7e24c12009-10-30 11:49:00 +0000417
418 // Character array used for conversion.
419 static const char chars[] = "0123456789abcdefghijklmnopqrstuvwxyz";
420
421 // Buffer for the integer part of the result. 1024 chars is enough
422 // for max integer value in radix 2. We need room for a sign too.
423 static const int kBufferSize = 1100;
424 char integer_buffer[kBufferSize];
425 integer_buffer[kBufferSize - 1] = '\0';
426
427 // Buffer for the decimal part of the result. We only generate up
428 // to kBufferSize - 1 chars for the decimal part.
429 char decimal_buffer[kBufferSize];
430 decimal_buffer[kBufferSize - 1] = '\0';
431
432 // Make sure the value is positive.
433 bool is_negative = value < 0.0;
434 if (is_negative) value = -value;
435
436 // Get the integer part and the decimal part.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000437 double integer_part = std::floor(value);
Steve Blocka7e24c12009-10-30 11:49:00 +0000438 double decimal_part = value - integer_part;
439
440 // Convert the integer part starting from the back. Always generate
441 // at least one digit.
442 int integer_pos = kBufferSize - 2;
443 do {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000444 double remainder = modulo(integer_part, radix);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000445 integer_buffer[integer_pos--] = chars[static_cast<int>(remainder)];
446 integer_part -= remainder;
Steve Blocka7e24c12009-10-30 11:49:00 +0000447 integer_part /= radix;
448 } while (integer_part >= 1.0);
449 // Sanity check.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000450 DCHECK(integer_pos > 0);
Steve Blocka7e24c12009-10-30 11:49:00 +0000451 // Add sign if needed.
452 if (is_negative) integer_buffer[integer_pos--] = '-';
453
454 // Convert the decimal part. Repeatedly multiply by the radix to
455 // generate the next char. Never generate more than kBufferSize - 1
456 // chars.
457 //
458 // TODO(1093998): We will often generate a full decimal_buffer of
459 // chars because hitting zero will often not happen. The right
460 // solution would be to continue until the string representation can
461 // be read back and yield the original value. To implement this
462 // efficiently, we probably have to modify dtoa.
463 int decimal_pos = 0;
464 while ((decimal_part > 0.0) && (decimal_pos < kBufferSize - 1)) {
465 decimal_part *= radix;
466 decimal_buffer[decimal_pos++] =
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000467 chars[static_cast<int>(std::floor(decimal_part))];
468 decimal_part -= std::floor(decimal_part);
Steve Blocka7e24c12009-10-30 11:49:00 +0000469 }
470 decimal_buffer[decimal_pos] = '\0';
471
472 // Compute the result size.
473 int integer_part_size = kBufferSize - 2 - integer_pos;
474 // Make room for zero termination.
475 unsigned result_size = integer_part_size + decimal_pos;
476 // If the number has a decimal part, leave room for the period.
477 if (decimal_pos > 0) result_size++;
478 // Allocate result and fill in the parts.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000479 SimpleStringBuilder builder(result_size + 1);
Steve Blocka7e24c12009-10-30 11:49:00 +0000480 builder.AddSubstring(integer_buffer + integer_pos + 1, integer_part_size);
481 if (decimal_pos > 0) builder.AddCharacter('.');
482 builder.AddSubstring(decimal_buffer, decimal_pos);
483 return builder.Finalize();
484}
485
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000486
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000487// ES6 18.2.4 parseFloat(string)
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400488double StringToDouble(UnicodeCache* unicode_cache, Handle<String> string,
489 int flags, double empty_string_val) {
490 Handle<String> flattened = String::Flatten(string);
491 {
492 DisallowHeapAllocation no_gc;
493 String::FlatContent flat = flattened->GetFlatContent();
494 DCHECK(flat.IsFlat());
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400495 if (flat.IsOneByte()) {
496 return StringToDouble(unicode_cache, flat.ToOneByteVector(), flags,
497 empty_string_val);
498 } else {
499 return StringToDouble(unicode_cache, flat.ToUC16Vector(), flags,
500 empty_string_val);
501 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000502 }
503}
504
505
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000506bool IsSpecialIndex(UnicodeCache* unicode_cache, String* string) {
507 // Max length of canonical double: -X.XXXXXXXXXXXXXXXXX-eXXX
508 const int kBufferSize = 24;
509 const int length = string->length();
510 if (length == 0 || length > kBufferSize) return false;
511 uint16_t buffer[kBufferSize];
512 String::WriteToFlat(string, buffer, 0, length);
513 // If the first char is not a digit or a '-' or we can't match 'NaN' or
514 // '(-)Infinity', bailout immediately.
515 int offset = 0;
516 if (!IsDecimalDigit(buffer[0])) {
517 if (buffer[0] == '-') {
518 if (length == 1) return false; // Just '-' is bad.
519 if (!IsDecimalDigit(buffer[1])) {
520 if (buffer[1] == 'I' && length == 9) {
521 // Allow matching of '-Infinity' below.
522 } else {
523 return false;
524 }
525 }
526 offset++;
527 } else if (buffer[0] == 'I' && length == 8) {
528 // Allow matching of 'Infinity' below.
529 } else if (buffer[0] == 'N' && length == 3) {
530 // Match NaN.
531 return buffer[1] == 'a' && buffer[2] == 'N';
532 } else {
533 return false;
534 }
535 }
536 // Expected fast path: key is an integer.
537 static const int kRepresentableIntegerLength = 15; // (-)XXXXXXXXXXXXXXX
538 if (length - offset <= kRepresentableIntegerLength) {
539 const int initial_offset = offset;
540 bool matches = true;
541 for (; offset < length; offset++) {
542 matches &= IsDecimalDigit(buffer[offset]);
543 }
544 if (matches) {
545 // Match 0 and -0.
546 if (buffer[initial_offset] == '0') return initial_offset == length - 1;
547 return true;
548 }
549 }
550 // Slow path: test DoubleToString(StringToDouble(string)) == string.
551 Vector<const uint16_t> vector(buffer, length);
552 double d = StringToDouble(unicode_cache, vector, NO_FLAGS);
553 if (std::isnan(d)) return false;
554 // Compute reverse string.
555 char reverse_buffer[kBufferSize + 1]; // Result will be /0 terminated.
556 Vector<char> reverse_vector(reverse_buffer, arraysize(reverse_buffer));
557 const char* reverse_string = DoubleToCString(d, reverse_vector);
558 for (int i = 0; i < length; ++i) {
559 if (static_cast<uint16_t>(reverse_string[i]) != buffer[i]) return false;
560 }
561 return true;
562}
563} // namespace internal
564} // namespace v8