1// Copyright 2011 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "src/conversions.h"
6
7#include <limits.h>
8#include <stdarg.h>
9#include <cmath>
10
11#include "src/allocation.h"
12#include "src/assert-scope.h"
13#include "src/char-predicates-inl.h"
14#include "src/dtoa.h"
15#include "src/handles.h"
16#include "src/heap/factory.h"
17#include "src/objects-inl.h"
18#include "src/objects/bigint.h"
19#include "src/strtod.h"
20#include "src/utils.h"
21
22#if defined(_STLP_VENDOR_CSTD)
23// STLPort doesn't import fpclassify into the std namespace.
24#define FPCLASSIFY_NAMESPACE
25#else
26#define FPCLASSIFY_NAMESPACE std
27#endif
28
29namespace v8 {
30namespace internal {
31
32inline double JunkStringValue() {
33 return bit_cast<double, uint64_t>(kQuietNaNMask);
34}
35
36inline double SignedZero(bool negative) {
37 return negative ? uint64_to_double(Double::kSignMask) : 0.0;
38}
39
40inline bool isDigit(int x, int radix) {
41 return (x >= '0' && x <= '9' && x < '0' + radix) ||
42 (radix > 10 && x >= 'a' && x < 'a' + radix - 10) ||
43 (radix > 10 && x >= 'A' && x < 'A' + radix - 10);
44}
45
46inline bool isBinaryDigit(int x) { return x == '0' || x == '1'; }
47
48template <class Iterator, class EndMark>
49bool SubStringEquals(Iterator* current, EndMark end, const char* substring) {
50 DCHECK(**current == *substring);
51 for (substring++; *substring != '\0'; substring++) {
52 ++*current;
53 if (*current == end || **current != *substring) return false;
54 }
55 ++*current;
56 return true;
57}
58
59// Returns true if a nonspace character has been found and false if the
60// end was been reached before finding a nonspace character.
61template <class Iterator, class EndMark>
62inline bool AdvanceToNonspace(Iterator* current, EndMark end) {
63 while (*current != end) {
64 if (!IsWhiteSpaceOrLineTerminator(**current)) return true;
65 ++*current;
66 }
67 return false;
68}
69
70// Parsing integers with radix 2, 4, 8, 16, 32. Assumes current != end.
71template <int radix_log_2, class Iterator, class EndMark>
72double InternalStringToIntDouble(Iterator current, EndMark end, bool negative,
73 bool allow_trailing_junk) {
74 DCHECK(current != end);
75
76 // Skip leading 0s.
77 while (*current == '0') {
78 ++current;
79 if (current == end) return SignedZero(negative);
80 }
81
82 int64_t number = 0;
83 int exponent = 0;
84 const int radix = (1 << radix_log_2);
85
86 int lim_0 = '0' + (radix < 10 ? radix : 10);
87 int lim_a = 'a' + (radix - 10);
88 int lim_A = 'A' + (radix - 10);
89
90 do {
91 int digit;
92 if (*current >= '0' && *current < lim_0) {
93 digit = static_cast<char>(*current) - '0';
94 } else if (*current >= 'a' && *current < lim_a) {
95 digit = static_cast<char>(*current) - 'a' + 10;
96 } else if (*current >= 'A' && *current < lim_A) {
97 digit = static_cast<char>(*current) - 'A' + 10;
98 } else {
99 if (allow_trailing_junk || !AdvanceToNonspace(&current, end)) {
100 break;
101 } else {
102 return JunkStringValue();
103 }
104 }
105
106 number = number * radix + digit;
107 int overflow = static_cast<int>(number >> 53);
108 if (overflow != 0) {
109 // Overflow occurred. Need to determine which direction to round the
110 // result.
111 int overflow_bits_count = 1;
112 while (overflow > 1) {
113 overflow_bits_count++;
114 overflow >>= 1;
115 }
116
117 int dropped_bits_mask = ((1 << overflow_bits_count) - 1);
118 int dropped_bits = static_cast<int>(number) & dropped_bits_mask;
119 number >>= overflow_bits_count;
120 exponent = overflow_bits_count;
121
122 bool zero_tail = true;
123 while (true) {
124 ++current;
125 if (current == end || !isDigit(*current, radix)) break;
126 zero_tail = zero_tail && *current == '0';
127 exponent += radix_log_2;
128 }
129
130 if (!allow_trailing_junk && AdvanceToNonspace(&current, end)) {
131 return JunkStringValue();
132 }
133
134 int middle_value = (1 << (overflow_bits_count - 1));
135 if (dropped_bits > middle_value) {
136 number++; // Rounding up.
137 } else if (dropped_bits == middle_value) {
138 // Rounding to even to consistency with decimals: half-way case rounds
139 // up if significant part is odd and down otherwise.
140 if ((number & 1) != 0 || !zero_tail) {
141 number++; // Rounding up.
142 }
143 }
144
145 // Rounding up may cause overflow.
146 if ((number & (static_cast<int64_t>(1) << 53)) != 0) {
147 exponent++;
148 number >>= 1;
149 }
150 break;
151 }
152 ++current;
153 } while (current != end);
154
155 DCHECK(number < ((int64_t)1 << 53));
156 DCHECK(static_cast<int64_t>(static_cast<double>(number)) == number);
157
158 if (exponent == 0) {
159 if (negative) {
160 if (number == 0) return -0.0;
161 number = -number;
162 }
163 return static_cast<double>(number);
164 }
165
166 DCHECK_NE(number, 0);
167 return std::ldexp(static_cast<double>(negative ? -number : number), exponent);
168}
169
170// ES6 18.2.5 parseInt(string, radix) (with NumberParseIntHelper subclass);
171// and BigInt parsing cases from https://tc39.github.io/proposal-bigint/
172// (with StringToBigIntHelper subclass).
173class StringToIntHelper {
174 public:
175 StringToIntHelper(Isolate* isolate, Handle<String> subject, int radix)
176 : isolate_(isolate), subject_(subject), radix_(radix) {
177 DCHECK(subject->IsFlat());
178 }
179
180 // Used for the StringToBigInt operation.
181 StringToIntHelper(Isolate* isolate, Handle<String> subject)
182 : isolate_(isolate), subject_(subject) {
183 DCHECK(subject->IsFlat());
184 }
185
186 // Used for parsing BigInt literals, where the input is a Zone-allocated
187 // buffer of one-byte digits, along with an optional radix prefix.
188 StringToIntHelper(Isolate* isolate, const uint8_t* subject, int length)
189 : isolate_(isolate), raw_one_byte_subject_(subject), length_(length) {}
190 virtual ~StringToIntHelper() = default;
191
192 protected:
193 // Subclasses must implement these:
194 virtual void AllocateResult() = 0;
195 virtual void ResultMultiplyAdd(uint32_t multiplier, uint32_t part) = 0;
196
197 // Subclasses must call this to do all the work.
198 void ParseInt();
199
200 // Subclasses may override this.
201 virtual void HandleSpecialCases() {}
202
203 // Subclass constructors should call these for configuration before calling
204 // ParseInt().
205 void set_allow_binary_and_octal_prefixes() {
206 allow_binary_and_octal_prefixes_ = true;
207 }
208 void set_disallow_trailing_junk() { allow_trailing_junk_ = false; }
209
210 bool IsOneByte() const {
211 return raw_one_byte_subject_ != nullptr ||
212 String::IsOneByteRepresentationUnderneath(*subject_);
213 }
214
215 Vector<const uint8_t> GetOneByteVector() {
216 if (raw_one_byte_subject_ != nullptr) {
217 return Vector<const uint8_t>(raw_one_byte_subject_, length_);
218 }
219 DisallowHeapAllocation no_gc;
220 return subject_->GetFlatContent(no_gc).ToOneByteVector();
221 }
222
223 Vector<const uc16> GetTwoByteVector() {
224 DisallowHeapAllocation no_gc;
225 return subject_->GetFlatContent(no_gc).ToUC16Vector();
226 }
227
228 // Subclasses get access to internal state:
229 enum State { kRunning, kError, kJunk, kEmpty, kZero, kDone };
230
231 enum class Sign { kNegative, kPositive, kNone };
232
233 Isolate* isolate() { return isolate_; }
234 int radix() { return radix_; }
235 int cursor() { return cursor_; }
236 int length() { return length_; }
237 bool negative() { return sign_ == Sign::kNegative; }
238 Sign sign() { return sign_; }
239 State state() { return state_; }
240 void set_state(State state) { state_ = state; }
241
242 private:
243 template <class Char>
244 void DetectRadixInternal(Char current, int length);
245 template <class Char>
246 void ParseInternal(Char start);
247
248 Isolate* isolate_;
249 Handle<String> subject_;
250 const uint8_t* raw_one_byte_subject_ = nullptr;
251 int radix_ = 0;
252 int cursor_ = 0;
253 int length_ = 0;
254 Sign sign_ = Sign::kNone;
255 bool leading_zero_ = false;
256 bool allow_binary_and_octal_prefixes_ = false;
257 bool allow_trailing_junk_ = true;
258 State state_ = kRunning;
259};
260
261void StringToIntHelper::ParseInt() {
262 {
263 DisallowHeapAllocation no_gc;
264 if (IsOneByte()) {
265 Vector<const uint8_t> vector = GetOneByteVector();
266 DetectRadixInternal(vector.start(), vector.length());
267 } else {
268 Vector<const uc16> vector = GetTwoByteVector();
269 DetectRadixInternal(vector.start(), vector.length());
270 }
271 }
272 if (state_ != kRunning) return;
273 AllocateResult();
274 HandleSpecialCases();
275 if (state_ != kRunning) return;
276 {
277 DisallowHeapAllocation no_gc;
278 if (IsOneByte()) {
279 Vector<const uint8_t> vector = GetOneByteVector();
280 DCHECK_EQ(length_, vector.length());
281 ParseInternal(vector.start());
282 } else {
283 Vector<const uc16> vector = GetTwoByteVector();
284 DCHECK_EQ(length_, vector.length());
285 ParseInternal(vector.start());
286 }
287 }
288 DCHECK_NE(state_, kRunning);
289}
290
291template <class Char>
292void StringToIntHelper::DetectRadixInternal(Char current, int length) {
293 Char start = current;
294 length_ = length;
295 Char end = start + length;
296
297 if (!AdvanceToNonspace(&current, end)) {
298 return set_state(kEmpty);
299 }
300
301 if (*current == '+') {
302 // Ignore leading sign; skip following spaces.
303 ++current;
304 if (current == end) {
305 return set_state(kJunk);
306 }
307 sign_ = Sign::kPositive;
308 } else if (*current == '-') {
309 ++current;
310 if (current == end) {
311 return set_state(kJunk);
312 }
313 sign_ = Sign::kNegative;
314 }
315
316 if (radix_ == 0) {
317 // Radix detection.
318 radix_ = 10;
319 if (*current == '0') {
320 ++current;
321 if (current == end) return set_state(kZero);
322 if (*current == 'x' || *current == 'X') {
323 radix_ = 16;
324 ++current;
325 if (current == end) return set_state(kJunk);
326 } else if (allow_binary_and_octal_prefixes_ &&
327 (*current == 'o' || *current == 'O')) {
328 radix_ = 8;
329 ++current;
330 if (current == end) return set_state(kJunk);
331 } else if (allow_binary_and_octal_prefixes_ &&
332 (*current == 'b' || *current == 'B')) {
333 radix_ = 2;
334 ++current;
335 if (current == end) return set_state(kJunk);
336 } else {
337 leading_zero_ = true;
338 }
339 }
340 } else if (radix_ == 16) {
341 if (*current == '0') {
342 // Allow "0x" prefix.
343 ++current;
344 if (current == end) return set_state(kZero);
345 if (*current == 'x' || *current == 'X') {
346 ++current;
347 if (current == end) return set_state(kJunk);
348 } else {
349 leading_zero_ = true;
350 }
351 }
352 }
353 // Skip leading zeros.
354 while (*current == '0') {
355 leading_zero_ = true;
356 ++current;
357 if (current == end) return set_state(kZero);
358 }
359
360 if (!leading_zero_ && !isDigit(*current, radix_)) {
361 return set_state(kJunk);
362 }
363
364 DCHECK(radix_ >= 2 && radix_ <= 36);
365 STATIC_ASSERT(String::kMaxLength <= INT_MAX);
366 cursor_ = static_cast<int>(current - start);
367}
368
369template <class Char>
370void StringToIntHelper::ParseInternal(Char start) {
371 Char current = start + cursor_;
372 Char end = start + length_;
373
374 // The following code causes accumulating rounding error for numbers greater
375 // than ~2^56. It's explicitly allowed in the spec: "if R is not 2, 4, 8, 10,
376 // 16, or 32, then mathInt may be an implementation-dependent approximation to
377 // the mathematical integer value" (15.1.2.2).
378
379 int lim_0 = '0' + (radix_ < 10 ? radix_ : 10);
380 int lim_a = 'a' + (radix_ - 10);
381 int lim_A = 'A' + (radix_ - 10);
382
383 // NOTE: The code for computing the value may seem a bit complex at
384 // first glance. It is structured to use 32-bit multiply-and-add
385 // loops as long as possible to avoid losing precision.
386
387 bool done = false;
388 do {
389 // Parse the longest part of the string starting at {current}
390 // possible while keeping the multiplier, and thus the part
391 // itself, within 32 bits.
392 uint32_t part = 0, multiplier = 1;
393 while (true) {
394 uint32_t d;
395 if (*current >= '0' && *current < lim_0) {
396 d = *current - '0';
397 } else if (*current >= 'a' && *current < lim_a) {
398 d = *current - 'a' + 10;
399 } else if (*current >= 'A' && *current < lim_A) {
400 d = *current - 'A' + 10;
401 } else {
402 done = true;
403 break;
404 }
405
406 // Update the value of the part as long as the multiplier fits
407 // in 32 bits. When we can't guarantee that the next iteration
408 // will not overflow the multiplier, we stop parsing the part
409 // by leaving the loop.
410 const uint32_t kMaximumMultiplier = 0xFFFFFFFFU / 36;
411 uint32_t m = multiplier * static_cast<uint32_t>(radix_);
412 if (m > kMaximumMultiplier) break;
413 part = part * radix_ + d;
414 multiplier = m;
415 DCHECK(multiplier > part);
416
417 ++current;
418 if (current == end) {
419 done = true;
420 break;
421 }
422 }
423
424 // Update the value and skip the part in the string.
425 ResultMultiplyAdd(multiplier, part);
426 } while (!done);
427
428 if (!allow_trailing_junk_ && AdvanceToNonspace(&current, end)) {
429 return set_state(kJunk);
430 }
431
432 return set_state(kDone);
433}
434
435class NumberParseIntHelper : public StringToIntHelper {
436 public:
437 NumberParseIntHelper(Isolate* isolate, Handle<String> string, int radix)
438 : StringToIntHelper(isolate, string, radix) {}
439
440 double GetResult() {
441 ParseInt();
442 switch (state()) {
443 case kJunk:
444 case kEmpty:
445 return JunkStringValue();
446 case kZero:
447 return SignedZero(negative());
448 case kDone:
449 return negative() ? -result_ : result_;
450 case kError:
451 case kRunning:
452 break;
453 }
454 UNREACHABLE();
455 }
456
457 protected:
458 void AllocateResult() override {}
459 void ResultMultiplyAdd(uint32_t multiplier, uint32_t part) override {
460 result_ = result_ * multiplier + part;
461 }
462
463 private:
464 void HandleSpecialCases() override {
465 bool is_power_of_two = base::bits::IsPowerOfTwo(radix());
466 if (!is_power_of_two && radix() != 10) return;
467 DisallowHeapAllocation no_gc;
468 if (IsOneByte()) {
469 Vector<const uint8_t> vector = GetOneByteVector();
470 DCHECK_EQ(length(), vector.length());
471 result_ = is_power_of_two ? HandlePowerOfTwoCase(vector.start())
472 : HandleBaseTenCase(vector.start());
473 } else {
474 Vector<const uc16> vector = GetTwoByteVector();
475 DCHECK_EQ(length(), vector.length());
476 result_ = is_power_of_two ? HandlePowerOfTwoCase(vector.start())
477 : HandleBaseTenCase(vector.start());
478 }
479 set_state(kDone);
480 }
481
482 template <class Char>
483 double HandlePowerOfTwoCase(Char start) {
484 Char current = start + cursor();
485 Char end = start + length();
486 const bool allow_trailing_junk = true;
487 // GetResult() will take care of the sign bit, so ignore it for now.
488 const bool negative = false;
489 switch (radix()) {
490 case 2:
491 return InternalStringToIntDouble<1>(current, end, negative,
492 allow_trailing_junk);
493 case 4:
494 return InternalStringToIntDouble<2>(current, end, negative,
495 allow_trailing_junk);
496 case 8:
497 return InternalStringToIntDouble<3>(current, end, negative,
498 allow_trailing_junk);
499
500 case 16:
501 return InternalStringToIntDouble<4>(current, end, negative,
502 allow_trailing_junk);
503
504 case 32:
505 return InternalStringToIntDouble<5>(current, end, negative,
506 allow_trailing_junk);
507 default:
508 UNREACHABLE();
509 }
510 }
511
512 template <class Char>
513 double HandleBaseTenCase(Char start) {
514 // Parsing with strtod.
515 Char current = start + cursor();
516 Char end = start + length();
517 const int kMaxSignificantDigits = 309; // Doubles are less than 1.8e308.
518 // The buffer may contain up to kMaxSignificantDigits + 1 digits and a zero
519 // end.
520 const int kBufferSize = kMaxSignificantDigits + 2;
521 char buffer[kBufferSize];
522 int buffer_pos = 0;
523 while (*current >= '0' && *current <= '9') {
524 if (buffer_pos <= kMaxSignificantDigits) {
525 // If the number has more than kMaxSignificantDigits it will be parsed
526 // as infinity.
527 DCHECK_LT(buffer_pos, kBufferSize);
528 buffer[buffer_pos++] = static_cast<char>(*current);
529 }
530 ++current;
531 if (current == end) break;
532 }
533
534 SLOW_DCHECK(buffer_pos < kBufferSize);
535 buffer[buffer_pos] = '\0';
536 Vector<const char> buffer_vector(buffer, buffer_pos);
537 return Strtod(buffer_vector, 0);
538 }
539
540 double result_ = 0;
541};
542
543// Converts a string to a double value. Assumes the Iterator supports
544// the following operations:
545// 1. current == end (other ops are not allowed), current != end.
546// 2. *current - gets the current character in the sequence.
547// 3. ++current (advances the position).
548template <class Iterator, class EndMark>
549double InternalStringToDouble(Iterator current, EndMark end, int flags,
550 double empty_string_val) {
551 // To make sure that iterator dereferencing is valid the following
552 // convention is used:
553 // 1. Each '++current' statement is followed by check for equality to 'end'.
554 // 2. If AdvanceToNonspace returned false then current == end.
555 // 3. If 'current' becomes be equal to 'end' the function returns or goes to
556 // 'parsing_done'.
557 // 4. 'current' is not dereferenced after the 'parsing_done' label.
558 // 5. Code before 'parsing_done' may rely on 'current != end'.
559 if (!AdvanceToNonspace(&current, end)) {
560 return empty_string_val;
561 }
562
563 const bool allow_trailing_junk = (flags & ALLOW_TRAILING_JUNK) != 0;
564
565 // Maximum number of significant digits in decimal representation.
566 // The longest possible double in decimal representation is
567 // (2^53 - 1) * 2 ^ -1074 that is (2 ^ 53 - 1) * 5 ^ 1074 / 10 ^ 1074
568 // (768 digits). If we parse a number whose first digits are equal to a
569 // mean of 2 adjacent doubles (that could have up to 769 digits) the result
570 // must be rounded to the bigger one unless the tail consists of zeros, so
571 // we don't need to preserve all the digits.
572 const int kMaxSignificantDigits = 772;
573
574 // The longest form of simplified number is: "-<significant digits>'.1eXXX\0".
575 const int kBufferSize = kMaxSignificantDigits + 10;
576 char buffer[kBufferSize]; // NOLINT: size is known at compile time.
577 int buffer_pos = 0;
578
579 // Exponent will be adjusted if insignificant digits of the integer part
580 // or insignificant leading zeros of the fractional part are dropped.
581 int exponent = 0;
582 int significant_digits = 0;
583 int insignificant_digits = 0;
584 bool nonzero_digit_dropped = false;
585
586 enum Sign { NONE, NEGATIVE, POSITIVE };
587
588 Sign sign = NONE;
589
590 if (*current == '+') {
591 // Ignore leading sign.
592 ++current;
593 if (current == end) return JunkStringValue();
594 sign = POSITIVE;
595 } else if (*current == '-') {
596 ++current;
597 if (current == end) return JunkStringValue();
598 sign = NEGATIVE;
599 }
600
601 static const char kInfinityString[] = "Infinity";
602 if (*current == kInfinityString[0]) {
603 if (!SubStringEquals(&current, end, kInfinityString)) {
604 return JunkStringValue();
605 }
606
607 if (!allow_trailing_junk && AdvanceToNonspace(&current, end)) {
608 return JunkStringValue();
609 }
610
611 DCHECK_EQ(buffer_pos, 0);
612 return (sign == NEGATIVE) ? -V8_INFINITY : V8_INFINITY;
613 }
614
615 bool leading_zero = false;
616 if (*current == '0') {
617 ++current;
618 if (current == end) return SignedZero(sign == NEGATIVE);
619
620 leading_zero = true;
621
622 // It could be hexadecimal value.
623 if ((flags & ALLOW_HEX) && (*current == 'x' || *current == 'X')) {
624 ++current;
625 if (current == end || !isDigit(*current, 16) || sign != NONE) {
626 return JunkStringValue(); // "0x".
627 }
628
629 return InternalStringToIntDouble<4>(current, end, false,
630 allow_trailing_junk);
631
632 // It could be an explicit octal value.
633 } else if ((flags & ALLOW_OCTAL) && (*current == 'o' || *current == 'O')) {
634 ++current;
635 if (current == end || !isDigit(*current, 8) || sign != NONE) {
636 return JunkStringValue(); // "0o".
637 }
638
639 return InternalStringToIntDouble<3>(current, end, false,
640 allow_trailing_junk);
641
642 // It could be a binary value.
643 } else if ((flags & ALLOW_BINARY) && (*current == 'b' || *current == 'B')) {
644 ++current;
645 if (current == end || !isBinaryDigit(*current) || sign != NONE) {
646 return JunkStringValue(); // "0b".
647 }
648
649 return InternalStringToIntDouble<1>(current, end, false,
650 allow_trailing_junk);
651 }
652
653 // Ignore leading zeros in the integer part.
654 while (*current == '0') {
655 ++current;
656 if (current == end) return SignedZero(sign == NEGATIVE);
657 }
658 }
659
660 bool octal = leading_zero && (flags & ALLOW_IMPLICIT_OCTAL) != 0;
661
662 // Copy significant digits of the integer part (if any) to the buffer.
663 while (*current >= '0' && *current <= '9') {
664 if (significant_digits < kMaxSignificantDigits) {
665 DCHECK_LT(buffer_pos, kBufferSize);
666 buffer[buffer_pos++] = static_cast<char>(*current);
667 significant_digits++;
668 // Will later check if it's an octal in the buffer.
669 } else {
670 insignificant_digits++; // Move the digit into the exponential part.
671 nonzero_digit_dropped = nonzero_digit_dropped || *current != '0';
672 }
673 octal = octal && *current < '8';
674 ++current;
675 if (current == end) goto parsing_done;
676 }
677
678 if (significant_digits == 0) {
679 octal = false;
680 }
681
682 if (*current == '.') {
683 if (octal && !allow_trailing_junk) return JunkStringValue();
684 if (octal) goto parsing_done;
685
686 ++current;
687 if (current == end) {
688 if (significant_digits == 0 && !leading_zero) {
689 return JunkStringValue();
690 } else {
691 goto parsing_done;
692 }
693 }
694
695 if (significant_digits == 0) {
696 // octal = false;
697 // Integer part consists of 0 or is absent. Significant digits start after
698 // leading zeros (if any).
699 while (*current == '0') {
700 ++current;
701 if (current == end) return SignedZero(sign == NEGATIVE);
702 exponent--; // Move this 0 into the exponent.
703 }
704 }
705
706 // There is a fractional part. We don't emit a '.', but adjust the exponent
707 // instead.
708 while (*current >= '0' && *current <= '9') {
709 if (significant_digits < kMaxSignificantDigits) {
710 DCHECK_LT(buffer_pos, kBufferSize);
711 buffer[buffer_pos++] = static_cast<char>(*current);
712 significant_digits++;
713 exponent--;
714 } else {
715 // Ignore insignificant digits in the fractional part.
716 nonzero_digit_dropped = nonzero_digit_dropped || *current != '0';
717 }
718 ++current;
719 if (current == end) goto parsing_done;
720 }
721 }
722
723 if (!leading_zero && exponent == 0 && significant_digits == 0) {
724 // If leading_zeros is true then the string contains zeros.
725 // If exponent < 0 then string was [+-]\.0*...
726 // If significant_digits != 0 the string is not equal to 0.
727 // Otherwise there are no digits in the string.
728 return JunkStringValue();
729 }
730
731 // Parse exponential part.
732 if (*current == 'e' || *current == 'E') {
733 if (octal) return JunkStringValue();
734 ++current;
735 if (current == end) {
736 if (allow_trailing_junk) {
737 goto parsing_done;
738 } else {
739 return JunkStringValue();
740 }
741 }
742 char sign = '+';
743 if (*current == '+' || *current == '-') {
744 sign = static_cast<char>(*current);
745 ++current;
746 if (current == end) {
747 if (allow_trailing_junk) {
748 goto parsing_done;
749 } else {
750 return JunkStringValue();
751 }
752 }
753 }
754
755 if (current == end || *current < '0' || *current > '9') {
756 if (allow_trailing_junk) {
757 goto parsing_done;
758 } else {
759 return JunkStringValue();
760 }
761 }
762
763 const int max_exponent = INT_MAX / 2;
764 DCHECK(-max_exponent / 2 <= exponent && exponent <= max_exponent / 2);
765 int num = 0;
766 do {
767 // Check overflow.
768 int digit = *current - '0';
769 if (num >= max_exponent / 10 &&
770 !(num == max_exponent / 10 && digit <= max_exponent % 10)) {
771 num = max_exponent;
772 } else {
773 num = num * 10 + digit;
774 }
775 ++current;
776 } while (current != end && *current >= '0' && *current <= '9');
777
778 exponent += (sign == '-' ? -num : num);
779 }
780
781 if (!allow_trailing_junk && AdvanceToNonspace(&current, end)) {
782 return JunkStringValue();
783 }
784
785parsing_done:
786 exponent += insignificant_digits;
787
788 if (octal) {
789 return InternalStringToIntDouble<3>(buffer, buffer + buffer_pos,
790 sign == NEGATIVE, allow_trailing_junk);
791 }
792
793 if (nonzero_digit_dropped) {
794 buffer[buffer_pos++] = '1';
795 exponent--;
796 }
797
798 SLOW_DCHECK(buffer_pos < kBufferSize);
799 buffer[buffer_pos] = '\0';
800
801 double converted = Strtod(Vector<const char>(buffer, buffer_pos), exponent);
802 return (sign == NEGATIVE) ? -converted : converted;
803}
804
805double StringToDouble(const char* str, int flags, double empty_string_val) {
806 // We cast to const uint8_t* here to avoid instantiating the
807 // InternalStringToDouble() template for const char* as well.
808 const uint8_t* start = reinterpret_cast<const uint8_t*>(str);
809 const uint8_t* end = start + StrLength(str);
810 return InternalStringToDouble(start, end, flags, empty_string_val);
811}
812
813double StringToDouble(Vector<const uint8_t> str, int flags,
814 double empty_string_val) {
815 // We cast to const uint8_t* here to avoid instantiating the
816 // InternalStringToDouble() template for const char* as well.
817 const uint8_t* start = reinterpret_cast<const uint8_t*>(str.start());
818 const uint8_t* end = start + str.length();
819 return InternalStringToDouble(start, end, flags, empty_string_val);
820}
821
822double StringToDouble(Vector<const uc16> str, int flags,
823 double empty_string_val) {
824 const uc16* end = str.start() + str.length();
825 return InternalStringToDouble(str.start(), end, flags, empty_string_val);
826}
827
828double StringToInt(Isolate* isolate, Handle<String> string, int radix) {
829 NumberParseIntHelper helper(isolate, string, radix);
830 return helper.GetResult();
831}
832
833class StringToBigIntHelper : public StringToIntHelper {
834 public:
835 enum class Behavior { kStringToBigInt, kLiteral };
836
837 // Used for StringToBigInt operation (BigInt constructor and == operator).
838 StringToBigIntHelper(Isolate* isolate, Handle<String> string)
839 : StringToIntHelper(isolate, string),
840 behavior_(Behavior::kStringToBigInt) {
841 set_allow_binary_and_octal_prefixes();
842 set_disallow_trailing_junk();
843 }
844
845 // Used for parsing BigInt literals, where the input is a buffer of
846 // one-byte ASCII digits, along with an optional radix prefix.
847 StringToBigIntHelper(Isolate* isolate, const uint8_t* string, int length)
848 : StringToIntHelper(isolate, string, length),
849 behavior_(Behavior::kLiteral) {
850 set_allow_binary_and_octal_prefixes();
851 }
852
853 MaybeHandle<BigInt> GetResult() {
854 ParseInt();
855 if (behavior_ == Behavior::kStringToBigInt && sign() != Sign::kNone &&
856 radix() != 10) {
857 return MaybeHandle<BigInt>();
858 }
859 if (state() == kEmpty) {
860 if (behavior_ == Behavior::kStringToBigInt) {
861 set_state(kZero);
862 } else {
863 UNREACHABLE();
864 }
865 }
866 switch (state()) {
867 case kJunk:
868 if (should_throw() == kThrowOnError) {
869 THROW_NEW_ERROR(isolate(),
870 NewSyntaxError(MessageTemplate::kBigIntInvalidString),
871 BigInt);
872 } else {
873 DCHECK_EQ(should_throw(), kDontThrow);
874 return MaybeHandle<BigInt>();
875 }
876 case kZero:
877 return BigInt::Zero(isolate());
878 case kError:
879 DCHECK_EQ(should_throw() == kThrowOnError,
880 isolate()->has_pending_exception());
881 return MaybeHandle<BigInt>();
882 case kDone:
883 return BigInt::Finalize(result_, negative());
884 case kEmpty:
885 case kRunning:
886 break;
887 }
888 UNREACHABLE();
889 }
890
891 protected:
892 void AllocateResult() override {
893 // We have to allocate a BigInt that's big enough to fit the result.
894 // Conseratively assume that all remaining digits are significant.
895 // Optimization opportunity: Would it makes sense to scan for trailing
896 // junk before allocating the result?
897 int charcount = length() - cursor();
898 // For literals, we pretenure the allocated BigInt, since it's about
899 // to be stored in the interpreter's constants array.
900 AllocationType allocation = behavior_ == Behavior::kLiteral
901 ? AllocationType::kOld
902 : AllocationType::kYoung;
903 MaybeHandle<FreshlyAllocatedBigInt> maybe = BigInt::AllocateFor(
904 isolate(), radix(), charcount, should_throw(), allocation);
905 if (!maybe.ToHandle(&result_)) {
906 set_state(kError);
907 }
908 }
909
910 void ResultMultiplyAdd(uint32_t multiplier, uint32_t part) override {
911 BigInt::InplaceMultiplyAdd(result_, static_cast<uintptr_t>(multiplier),
912 static_cast<uintptr_t>(part));
913 }
914
915 private:
916 ShouldThrow should_throw() const { return kDontThrow; }
917
918 Handle<FreshlyAllocatedBigInt> result_;
919 Behavior behavior_;
920};
921
922MaybeHandle<BigInt> StringToBigInt(Isolate* isolate, Handle<String> string) {
923 string = String::Flatten(isolate, string);
924 StringToBigIntHelper helper(isolate, string);
925 return helper.GetResult();
926}
927
928MaybeHandle<BigInt> BigIntLiteral(Isolate* isolate, const char* string) {
929 StringToBigIntHelper helper(isolate, reinterpret_cast<const uint8_t*>(string),
930 static_cast<int>(strlen(string)));
931 return helper.GetResult();
932}
933
934const char* DoubleToCString(double v, Vector<char> buffer) {
935 switch (FPCLASSIFY_NAMESPACE::fpclassify(v)) {
936 case FP_NAN: return "NaN";
937 case FP_INFINITE: return (v < 0.0 ? "-Infinity" : "Infinity");
938 case FP_ZERO: return "0";
939 default: {
940 if (IsInt32Double(v)) {
941 // This will trigger if v is -0 and -0.0 is stringified to "0".
942 // (see ES section 7.1.12.1 #sec-tostring-applied-to-the-number-type)
943 return IntToCString(FastD2I(v), buffer);
944 }
945 SimpleStringBuilder builder(buffer.start(), buffer.length());
946 int decimal_point;
947 int sign;
948 const int kV8DtoaBufferCapacity = kBase10MaximalLength + 1;
949 char decimal_rep[kV8DtoaBufferCapacity];
950 int length;
951
952 DoubleToAscii(v, DTOA_SHORTEST, 0,
953 Vector<char>(decimal_rep, kV8DtoaBufferCapacity),
954 &sign, &length, &decimal_point);
955
956 if (sign) builder.AddCharacter('-');
957
958 if (length <= decimal_point && decimal_point <= 21) {
959 // ECMA-262 section 9.8.1 step 6.
960 builder.AddString(decimal_rep);
961 builder.AddPadding('0', decimal_point - length);
962
963 } else if (0 < decimal_point && decimal_point <= 21) {
964 // ECMA-262 section 9.8.1 step 7.
965 builder.AddSubstring(decimal_rep, decimal_point);
966 builder.AddCharacter('.');
967 builder.AddString(decimal_rep + decimal_point);
968
969 } else if (decimal_point <= 0 && decimal_point > -6) {
970 // ECMA-262 section 9.8.1 step 8.
971 builder.AddString("0.");
972 builder.AddPadding('0', -decimal_point);
973 builder.AddString(decimal_rep);
974
975 } else {
976 // ECMA-262 section 9.8.1 step 9 and 10 combined.
977 builder.AddCharacter(decimal_rep[0]);
978 if (length != 1) {
979 builder.AddCharacter('.');
980 builder.AddString(decimal_rep + 1);
981 }
982 builder.AddCharacter('e');
983 builder.AddCharacter((decimal_point >= 0) ? '+' : '-');
984 int exponent = decimal_point - 1;
985 if (exponent < 0) exponent = -exponent;
986 builder.AddDecimalInteger(exponent);
987 }
988 return builder.Finalize();
989 }
990 }
991}
992
993
994const char* IntToCString(int n, Vector<char> buffer) {
995 bool negative = true;
996 if (n >= 0) {
997 n = -n;
998 negative = false;
999 }
1000 // Build the string backwards from the least significant digit.
1001 int i = buffer.length();
1002 buffer[--i] = '\0';
1003 do {
1004 // We ensured n <= 0, so the subtraction does the right addition.
1005 buffer[--i] = '0' - (n % 10);
1006 n /= 10;
1007 } while (n);
1008 if (negative) buffer[--i] = '-';
1009 return buffer.start() + i;
1010}
1011
1012
1013char* DoubleToFixedCString(double value, int f) {
1014 const int kMaxDigitsBeforePoint = 21;
1015 const double kFirstNonFixed = 1e21;
1016 DCHECK_GE(f, 0);
1017 DCHECK_LE(f, kMaxFractionDigits);
1018
1019 bool negative = false;
1020 double abs_value = value;
1021 if (value < 0) {
1022 abs_value = -value;
1023 negative = true;
1024 }
1025
1026 // If abs_value has more than kMaxDigitsBeforePoint digits before the point
1027 // use the non-fixed conversion routine.
1028 if (abs_value >= kFirstNonFixed) {
1029 char arr[kMaxFractionDigits];
1030 Vector<char> buffer(arr, arraysize(arr));
1031 return StrDup(DoubleToCString(value, buffer));
1032 }
1033
1034 // Find a sufficiently precise decimal representation of n.
1035 int decimal_point;
1036 int sign;
1037 // Add space for the '\0' byte.
1038 const int kDecimalRepCapacity =
1039 kMaxDigitsBeforePoint + kMaxFractionDigits + 1;
1040 char decimal_rep[kDecimalRepCapacity];
1041 int decimal_rep_length;
1042 DoubleToAscii(value, DTOA_FIXED, f,
1043 Vector<char>(decimal_rep, kDecimalRepCapacity),
1044 &sign, &decimal_rep_length, &decimal_point);
1045
1046 // Create a representation that is padded with zeros if needed.
1047 int zero_prefix_length = 0;
1048 int zero_postfix_length = 0;
1049
1050 if (decimal_point <= 0) {
1051 zero_prefix_length = -decimal_point + 1;
1052 decimal_point = 1;
1053 }
1054
1055 if (zero_prefix_length + decimal_rep_length < decimal_point + f) {
1056 zero_postfix_length = decimal_point + f - decimal_rep_length -
1057 zero_prefix_length;
1058 }
1059
1060 unsigned rep_length =
1061 zero_prefix_length + decimal_rep_length + zero_postfix_length;
1062 SimpleStringBuilder rep_builder(rep_length + 1);
1063 rep_builder.AddPadding('0', zero_prefix_length);
1064 rep_builder.AddString(decimal_rep);
1065 rep_builder.AddPadding('0', zero_postfix_length);
1066 char* rep = rep_builder.Finalize();
1067
1068 // Create the result string by appending a minus and putting in a
1069 // decimal point if needed.
1070 unsigned result_size = decimal_point + f + 2;
1071 SimpleStringBuilder builder(result_size + 1);
1072 if (negative) builder.AddCharacter('-');
1073 builder.AddSubstring(rep, decimal_point);
1074 if (f > 0) {
1075 builder.AddCharacter('.');
1076 builder.AddSubstring(rep + decimal_point, f);
1077 }
1078 DeleteArray(rep);
1079 return builder.Finalize();
1080}
1081
1082
1083static char* CreateExponentialRepresentation(char* decimal_rep,
1084 int exponent,
1085 bool negative,
1086 int significant_digits) {
1087 bool negative_exponent = false;
1088 if (exponent < 0) {
1089 negative_exponent = true;
1090 exponent = -exponent;
1091 }
1092
1093 // Leave room in the result for appending a minus, for a period, the
1094 // letter 'e', a minus or a plus depending on the exponent, and a
1095 // three digit exponent.
1096 unsigned result_size = significant_digits + 7;
1097 SimpleStringBuilder builder(result_size + 1);
1098
1099 if (negative) builder.AddCharacter('-');
1100 builder.AddCharacter(decimal_rep[0]);
1101 if (significant_digits != 1) {
1102 builder.AddCharacter('.');
1103 builder.AddString(decimal_rep + 1);
1104 int rep_length = StrLength(decimal_rep);
1105 builder.AddPadding('0', significant_digits - rep_length);
1106 }
1107
1108 builder.AddCharacter('e');
1109 builder.AddCharacter(negative_exponent ? '-' : '+');
1110 builder.AddDecimalInteger(exponent);
1111 return builder.Finalize();
1112}
1113
1114
1115char* DoubleToExponentialCString(double value, int f) {
1116 // f might be -1 to signal that f was undefined in JavaScript.
1117 DCHECK(f >= -1 && f <= kMaxFractionDigits);
1118
1119 bool negative = false;
1120 if (value < 0) {
1121 value = -value;
1122 negative = true;
1123 }
1124
1125 // Find a sufficiently precise decimal representation of n.
1126 int decimal_point;
1127 int sign;
1128 // f corresponds to the digits after the point. There is always one digit
1129 // before the point. The number of requested_digits equals hence f + 1.
1130 // And we have to add one character for the null-terminator.
1131 const int kV8DtoaBufferCapacity = kMaxFractionDigits + 1 + 1;
1132 // Make sure that the buffer is big enough, even if we fall back to the
1133 // shortest representation (which happens when f equals -1).
1134 DCHECK_LE(kBase10MaximalLength, kMaxFractionDigits + 1);
1135 char decimal_rep[kV8DtoaBufferCapacity];
1136 int decimal_rep_length;
1137
1138 if (f == -1) {
1139 DoubleToAscii(value, DTOA_SHORTEST, 0,
1140 Vector<char>(decimal_rep, kV8DtoaBufferCapacity),
1141 &sign, &decimal_rep_length, &decimal_point);
1142 f = decimal_rep_length - 1;
1143 } else {
1144 DoubleToAscii(value, DTOA_PRECISION, f + 1,
1145 Vector<char>(decimal_rep, kV8DtoaBufferCapacity),
1146 &sign, &decimal_rep_length, &decimal_point);
1147 }
1148 DCHECK_GT(decimal_rep_length, 0);
1149 DCHECK(decimal_rep_length <= f + 1);
1150
1151 int exponent = decimal_point - 1;
1152 char* result =
1153 CreateExponentialRepresentation(decimal_rep, exponent, negative, f+1);
1154
1155 return result;
1156}
1157
1158
1159char* DoubleToPrecisionCString(double value, int p) {
1160 const int kMinimalDigits = 1;
1161 DCHECK(p >= kMinimalDigits && p <= kMaxFractionDigits);
1162 USE(kMinimalDigits);
1163
1164 bool negative = false;
1165 if (value < 0) {
1166 value = -value;
1167 negative = true;
1168 }
1169
1170 // Find a sufficiently precise decimal representation of n.
1171 int decimal_point;
1172 int sign;
1173 // Add one for the terminating null character.
1174 const int kV8DtoaBufferCapacity = kMaxFractionDigits + 1;
1175 char decimal_rep[kV8DtoaBufferCapacity];
1176 int decimal_rep_length;
1177
1178 DoubleToAscii(value, DTOA_PRECISION, p,
1179 Vector<char>(decimal_rep, kV8DtoaBufferCapacity),
1180 &sign, &decimal_rep_length, &decimal_point);
1181 DCHECK(decimal_rep_length <= p);
1182
1183 int exponent = decimal_point - 1;
1184
1185 char* result = nullptr;
1186
1187 if (exponent < -6 || exponent >= p) {
1188 result =
1189 CreateExponentialRepresentation(decimal_rep, exponent, negative, p);
1190 } else {
1191 // Use fixed notation.
1192 //
1193 // Leave room in the result for appending a minus, a period and in
1194 // the case where decimal_point is not positive for a zero in
1195 // front of the period.
1196 unsigned result_size = (decimal_point <= 0)
1197 ? -decimal_point + p + 3
1198 : p + 2;
1199 SimpleStringBuilder builder(result_size + 1);
1200 if (negative) builder.AddCharacter('-');
1201 if (decimal_point <= 0) {
1202 builder.AddString("0.");
1203 builder.AddPadding('0', -decimal_point);
1204 builder.AddString(decimal_rep);
1205 builder.AddPadding('0', p - decimal_rep_length);
1206 } else {
1207 const int m = Min(decimal_rep_length, decimal_point);
1208 builder.AddSubstring(decimal_rep, m);
1209 builder.AddPadding('0', decimal_point - decimal_rep_length);
1210 if (decimal_point < p) {
1211 builder.AddCharacter('.');
1212 const int extra = negative ? 2 : 1;
1213 if (decimal_rep_length > decimal_point) {
1214 const int len = StrLength(decimal_rep + decimal_point);
1215 const int n = Min(len, p - (builder.position() - extra));
1216 builder.AddSubstring(decimal_rep + decimal_point, n);
1217 }
1218 builder.AddPadding('0', extra + (p - builder.position()));
1219 }
1220 }
1221 result = builder.Finalize();
1222 }
1223
1224 return result;
1225}
1226
1227char* DoubleToRadixCString(double value, int radix) {
1228 DCHECK(radix >= 2 && radix <= 36);
1229 DCHECK(std::isfinite(value));
1230 DCHECK_NE(0.0, value);
1231 // Character array used for conversion.
1232 static const char chars[] = "0123456789abcdefghijklmnopqrstuvwxyz";
1233
1234 // Temporary buffer for the result. We start with the decimal point in the
1235 // middle and write to the left for the integer part and to the right for the
1236 // fractional part. 1024 characters for the exponent and 52 for the mantissa
1237 // either way, with additional space for sign, decimal point and string
1238 // termination should be sufficient.
1239 static const int kBufferSize = 2200;
1240 char buffer[kBufferSize];
1241 int integer_cursor = kBufferSize / 2;
1242 int fraction_cursor = integer_cursor;
1243
1244 bool negative = value < 0;
1245 if (negative) value = -value;
1246
1247 // Split the value into an integer part and a fractional part.
1248 double integer = std::floor(value);
1249 double fraction = value - integer;
1250 // We only compute fractional digits up to the input double's precision.
1251 double delta = 0.5 * (Double(value).NextDouble() - value);
1252 delta = std::max(Double(0.0).NextDouble(), delta);
1253 DCHECK_GT(delta, 0.0);
1254 if (fraction > delta) {
1255 // Insert decimal point.
1256 buffer[fraction_cursor++] = '.';
1257 do {
1258 // Shift up by one digit.
1259 fraction *= radix;
1260 delta *= radix;
1261 // Write digit.
1262 int digit = static_cast<int>(fraction);
1263 buffer[fraction_cursor++] = chars[digit];
1264 // Calculate remainder.
1265 fraction -= digit;
1266 // Round to even.
1267 if (fraction > 0.5 || (fraction == 0.5 && (digit & 1))) {
1268 if (fraction + delta > 1) {
1269 // We need to back trace already written digits in case of carry-over.
1270 while (true) {
1271 fraction_cursor--;
1272 if (fraction_cursor == kBufferSize / 2) {
1273 CHECK_EQ('.', buffer[fraction_cursor]);
1274 // Carry over to the integer part.
1275 integer += 1;
1276 break;
1277 }
1278 char c = buffer[fraction_cursor];
1279 // Reconstruct digit.
1280 int digit = c > '9' ? (c - 'a' + 10) : (c - '0');
1281 if (digit + 1 < radix) {
1282 buffer[fraction_cursor++] = chars[digit + 1];
1283 break;
1284 }
1285 }
1286 break;
1287 }
1288 }
1289 } while (fraction > delta);
1290 }
1291
1292 // Compute integer digits. Fill unrepresented digits with zero.
1293 while (Double(integer / radix).Exponent() > 0) {
1294 integer /= radix;
1295 buffer[--integer_cursor] = '0';
1296 }
1297 do {
1298 double remainder = Modulo(integer, radix);
1299 buffer[--integer_cursor] = chars[static_cast<int>(remainder)];
1300 integer = (integer - remainder) / radix;
1301 } while (integer > 0);
1302
1303 // Add sign and terminate string.
1304 if (negative) buffer[--integer_cursor] = '-';
1305 buffer[fraction_cursor++] = '\0';
1306 DCHECK_LT(fraction_cursor, kBufferSize);
1307 DCHECK_LE(0, integer_cursor);
1308 // Allocate new string as return value.
1309 char* result = NewArray<char>(fraction_cursor - integer_cursor);
1310 memcpy(result, buffer + integer_cursor, fraction_cursor - integer_cursor);
1311 return result;
1312}
1313
1314
1315// ES6 18.2.4 parseFloat(string)
1316double StringToDouble(Isolate* isolate, Handle<String> string, int flags,
1317 double empty_string_val) {
1318 Handle<String> flattened = String::Flatten(isolate, string);
1319 {
1320 DisallowHeapAllocation no_gc;
1321 String::FlatContent flat = flattened->GetFlatContent(no_gc);
1322 DCHECK(flat.IsFlat());
1323 if (flat.IsOneByte()) {
1324 return StringToDouble(flat.ToOneByteVector(), flags, empty_string_val);
1325 } else {
1326 return StringToDouble(flat.ToUC16Vector(), flags, empty_string_val);
1327 }
1328 }
1329}
1330
1331bool IsSpecialIndex(String string) {
1332 // Max length of canonical double: -X.XXXXXXXXXXXXXXXXX-eXXX
1333 const int kBufferSize = 24;
1334 const int length = string->length();
1335 if (length == 0 || length > kBufferSize) return false;
1336 uint16_t buffer[kBufferSize];
1337 String::WriteToFlat(string, buffer, 0, length);
1338 // If the first char is not a digit or a '-' or we can't match 'NaN' or
1339 // '(-)Infinity', bailout immediately.
1340 int offset = 0;
1341 if (!IsDecimalDigit(buffer[0])) {
1342 if (buffer[0] == '-') {
1343 if (length == 1) return false; // Just '-' is bad.
1344 if (!IsDecimalDigit(buffer[1])) {
1345 if (buffer[1] == 'I' && length == 9) {
1346 // Allow matching of '-Infinity' below.
1347 } else {
1348 return false;
1349 }
1350 }
1351 offset++;
1352 } else if (buffer[0] == 'I' && length == 8) {
1353 // Allow matching of 'Infinity' below.
1354 } else if (buffer[0] == 'N' && length == 3) {
1355 // Match NaN.
1356 return buffer[1] == 'a' && buffer[2] == 'N';
1357 } else {
1358 return false;
1359 }
1360 }
1361 // Expected fast path: key is an integer.
1362 static const int kRepresentableIntegerLength = 15; // (-)XXXXXXXXXXXXXXX
1363 if (length - offset <= kRepresentableIntegerLength) {
1364 const int initial_offset = offset;
1365 bool matches = true;
1366 for (; offset < length; offset++) {
1367 matches &= IsDecimalDigit(buffer[offset]);
1368 }
1369 if (matches) {
1370 // Match 0 and -0.
1371 if (buffer[initial_offset] == '0') return initial_offset == length - 1;
1372 return true;
1373 }
1374 }
1375 // Slow path: test DoubleToString(StringToDouble(string)) == string.
1376 Vector<const uint16_t> vector(buffer, length);
1377 double d = StringToDouble(vector, NO_FLAGS);
1378 if (std::isnan(d)) return false;
1379 // Compute reverse string.
1380 char reverse_buffer[kBufferSize + 1]; // Result will be /0 terminated.
1381 Vector<char> reverse_vector(reverse_buffer, arraysize(reverse_buffer));
1382 const char* reverse_string = DoubleToCString(d, reverse_vector);
1383 for (int i = 0; i < length; ++i) {
1384 if (static_cast<uint16_t>(reverse_string[i]) != buffer[i]) return false;
1385 }
1386 return true;
1387}
1388} // namespace internal
1389} // namespace v8
1390