OLD | NEW |
---|---|
1 // Copyright 2011 the V8 project authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <limits.h> | 5 #include <limits.h> |
6 #include <stdarg.h> | 6 #include <stdarg.h> |
7 #include <cmath> | 7 #include <cmath> |
8 | 8 |
9 #include "src/v8.h" | 9 #include "src/v8.h" |
10 | 10 |
(...skipping 485 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
496 return StringToDouble(unicode_cache, flat.ToOneByteVector(), flags, | 496 return StringToDouble(unicode_cache, flat.ToOneByteVector(), flags, |
497 empty_string_val); | 497 empty_string_val); |
498 } else { | 498 } else { |
499 return StringToDouble(unicode_cache, flat.ToUC16Vector(), flags, | 499 return StringToDouble(unicode_cache, flat.ToUC16Vector(), flags, |
500 empty_string_val); | 500 empty_string_val); |
501 } | 501 } |
502 } | 502 } |
503 } | 503 } |
504 | 504 |
505 | 505 |
506 bool IsNonArrayIndexInteger(String* string) { | 506 bool IsSpecialIndex(UnicodeCache* unicode_cache, String* string) { |
507 const int kBufferSize = 64; | 507 // Max length of canonical double: -X.XXXXXXXXXXXXXXXXX-eXXX |
508 const int kUint32MaxChars = 11; | 508 const int kBufferSize = 24; |
509 const int length = string->length(); | |
510 if (length == 0 || length > kBufferSize) return false; | |
509 uint16_t buffer[kBufferSize]; | 511 uint16_t buffer[kBufferSize]; |
512 String::WriteToFlat(string, buffer, 0, length); | |
513 // If the first char is not a digit or a '-', bailout immediately. | |
510 int offset = 0; | 514 int offset = 0; |
511 const int length = string->length(); | 515 if (!IsDecimalDigit(buffer[0])) { |
512 if (length == 0) return false; | 516 if (buffer[0] != '-') return false; |
513 // First iteration, check for minus, 0 followed by anything else, etc. | 517 if (length == 1) return false; // Just '-' is bad. |
514 int to = std::min(offset + kUint32MaxChars, length); | 518 offset++; |
515 { | 519 } |
516 String::WriteToFlat(string, buffer, offset, to); | 520 // Match 0 and -0. |
517 bool negative = false; | 521 if (buffer[offset] == '0') return offset == length - 1; |
Toon Verwaest
2015/03/27 14:30:51
(-)0.XXX...
| |
518 if (buffer[offset] == '-') { | 522 // Expected fast path: key is an integer. |
519 negative = true; | 523 static const int kRepresentableIntegerLength = 15; // (-)XXXXXXXXXXXXXXX |
520 ++offset; | 524 if (length - offset <= kRepresentableIntegerLength) { |
521 if (offset == to) return false; // Just '-' is bad. | 525 bool matches = true; |
526 for (; offset < length; offset++) { | |
527 matches &= IsDecimalDigit(buffer[offset]); | |
522 } | 528 } |
523 if (buffer[offset] == '0') { | 529 if (matches) return true; |
524 return to == 2 && negative; // Match just '-0'. | |
525 } | |
526 // Process positive integers. | |
527 if (!negative) { | |
528 uint64_t acc = 0; | |
529 for (; offset < to; ++offset) { | |
530 uint64_t digit = buffer[offset] - '0'; | |
531 if (digit > 9) return false; | |
532 acc = 10 * acc + digit; | |
533 } | |
534 // String is consumed. Evaluate what we have. | |
535 if (offset == length) { | |
536 return acc > | |
537 static_cast<uint64_t>(std::numeric_limits<uint32_t>::max()); | |
538 } | |
539 } | |
540 } | 530 } |
541 // Consume rest of string. If we get here, we're way out of uint32_t bounds | 531 // Slow path: test DoubleToString(StringToDouble(string)) == string. |
542 // or negative. | 532 Vector<const uint16_t> vector(buffer, length); |
543 int i = offset; | 533 double d = StringToDouble(unicode_cache, vector, NO_FLAGS); |
544 while (true) { | 534 if (std::isnan(d)) return false; |
545 for (; offset < to; ++offset, ++i) { | 535 // Compute reverse string. |
546 if (!IsDecimalDigit(buffer[i])) return false; | 536 char reverse_buffer[kBufferSize + 1]; // Result will be /0 terminated. |
547 } | 537 Vector<char> reverse_vector(reverse_buffer, arraysize(reverse_buffer)); |
548 if (offset == length) break; | 538 const char* reverse_string = DoubleToCString(d, reverse_vector); |
549 // Read next chunk. | 539 for (int i = 0; i < length; ++i) { |
550 to = std::min(offset + kBufferSize, length); | 540 if (static_cast<uint16_t>(reverse_string[i]) != buffer[i]) return false; |
551 String::WriteToFlat(string, buffer, offset, to); | |
552 i = 0; | |
553 } | 541 } |
554 return true; | 542 return true; |
555 } | 543 } |
556 } } // namespace v8::internal | 544 } } // namespace v8::internal |
OLD | NEW |