2011-07-05 11:54:11 +00:00
|
|
|
// Copyright 2011 the V8 project authors. All rights reserved.
|
2014-04-29 06:42:26 +00:00
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
#ifndef V8_CONVERSIONS_INL_H_
|
|
|
|
#define V8_CONVERSIONS_INL_H_
|
|
|
|
|
2014-12-20 13:17:20 +00:00
|
|
|
#include <float.h> // Required for DBL_MAX and on Win32 for finite()
|
|
|
|
#include <limits.h> // Required for INT_MAX etc.
|
|
|
|
#include <stdarg.h>
|
2013-04-19 13:26:47 +00:00
|
|
|
#include <cmath>
|
2014-12-20 13:17:20 +00:00
|
|
|
#include "src/globals.h" // Required for V8_INFINITY
|
2015-08-20 11:46:54 +00:00
|
|
|
#include "src/unicode-cache-inl.h"
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Extra POSIX/ANSI functions for Win32/MSVC.
|
|
|
|
|
2014-09-02 13:36:35 +00:00
|
|
|
#include "src/base/bits.h"
|
2014-06-30 13:25:46 +00:00
|
|
|
#include "src/base/platform/platform.h"
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "src/conversions.h"
|
|
|
|
#include "src/double.h"
|
2015-08-13 14:02:22 +00:00
|
|
|
#include "src/objects-inl.h"
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "src/strtod.h"
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2009-05-25 10:05:56 +00:00
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2011-11-29 10:56:11 +00:00
|
|
|
inline double JunkStringValue() {
|
2014-09-08 09:11:11 +00:00
|
|
|
return bit_cast<double, uint64_t>(kQuietNaNMask);
|
2011-07-07 13:57:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-30 09:55:26 +00:00
|
|
|
inline double SignedZero(bool negative) {
|
|
|
|
return negative ? uint64_to_double(Double::kSignMask) : 0.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-03-03 13:44:20 +00:00
|
|
|
// The fast double-to-unsigned-int conversion routine does not guarantee
|
2010-03-16 12:20:52 +00:00
|
|
|
// rounding towards zero, or any reasonable value if the argument is larger
|
|
|
|
// than what fits in an unsigned 32-bit integer.
|
2011-11-29 10:56:11 +00:00
|
|
|
inline unsigned int FastD2UI(double x) {
|
2010-03-03 13:44:20 +00:00
|
|
|
// There is no unsigned version of lrint, so there is no fast path
|
|
|
|
// in this function as there is in FastD2I. Using lrint doesn't work
|
|
|
|
// for values of 2^31 and above.
|
|
|
|
|
|
|
|
// Convert "small enough" doubles to uint32_t by fixing the 32
|
|
|
|
// least significant non-fractional bits in the low 32 bits of the
|
|
|
|
// double, and reading them from there.
|
|
|
|
const double k2Pow52 = 4503599627370496.0;
|
|
|
|
bool negative = x < 0;
|
|
|
|
if (negative) {
|
|
|
|
x = -x;
|
|
|
|
}
|
|
|
|
if (x < k2Pow52) {
|
|
|
|
x += k2Pow52;
|
|
|
|
uint32_t result;
|
2014-04-15 16:39:21 +00:00
|
|
|
#ifndef V8_TARGET_BIG_ENDIAN
|
2010-03-16 12:20:52 +00:00
|
|
|
Address mantissa_ptr = reinterpret_cast<Address>(&x);
|
2014-04-15 16:39:21 +00:00
|
|
|
#else
|
|
|
|
Address mantissa_ptr = reinterpret_cast<Address>(&x) + kIntSize;
|
|
|
|
#endif
|
2010-03-16 12:20:52 +00:00
|
|
|
// Copy least significant 32 bits of mantissa.
|
2014-05-27 12:02:44 +00:00
|
|
|
memcpy(&result, mantissa_ptr, sizeof(result));
|
2010-03-03 13:44:20 +00:00
|
|
|
return negative ? ~result + 1 : result;
|
|
|
|
}
|
|
|
|
// Large number (outside uint32 range), Infinity or NaN.
|
|
|
|
return 0x80000000u; // Return integer indefinite.
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-09-22 11:42:10 +00:00
|
|
|
inline float DoubleToFloat32(double x) {
|
2015-10-15 16:03:20 +00:00
|
|
|
// TODO(yangguo): This static_cast is implementation-defined behaviour in C++,
|
2014-09-22 11:42:10 +00:00
|
|
|
// so we may need to do the conversion manually instead to match the spec.
|
|
|
|
volatile float f = static_cast<float>(x);
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-29 10:56:11 +00:00
|
|
|
inline double DoubleToInteger(double x) {
|
2013-04-19 13:26:47 +00:00
|
|
|
if (std::isnan(x)) return 0;
|
|
|
|
if (!std::isfinite(x) || x == 0) return x;
|
2014-01-14 09:57:05 +00:00
|
|
|
return (x >= 0) ? std::floor(x) : std::ceil(x);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int32_t DoubleToInt32(double x) {
|
|
|
|
int32_t i = FastD2I(x);
|
|
|
|
if (FastI2D(i) == x) return i;
|
2011-09-07 12:39:53 +00:00
|
|
|
Double d(x);
|
|
|
|
int exponent = d.Exponent();
|
|
|
|
if (exponent < 0) {
|
|
|
|
if (exponent <= -Double::kSignificandSize) return 0;
|
|
|
|
return d.Sign() * static_cast<int32_t>(d.Significand() >> -exponent);
|
|
|
|
} else {
|
|
|
|
if (exponent > 31) return 0;
|
|
|
|
return d.Sign() * static_cast<int32_t>(d.Significand() << exponent);
|
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-08-13 14:02:22 +00:00
|
|
|
bool IsSmiDouble(double value) {
|
|
|
|
return !IsMinusZero(value) && value >= Smi::kMinValue &&
|
|
|
|
value <= Smi::kMaxValue && value == FastI2D(FastD2I(value));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool IsInt32Double(double value) {
|
|
|
|
return !IsMinusZero(value) && value >= kMinInt && value <= kMaxInt &&
|
|
|
|
value == FastI2D(FastD2I(value));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool IsUint32Double(double value) {
|
|
|
|
return !IsMinusZero(value) && value >= 0 && value <= kMaxUInt32 &&
|
|
|
|
value == FastUI2D(FastD2UI(value));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int32_t NumberToInt32(Object* number) {
|
|
|
|
if (number->IsSmi()) return Smi::cast(number)->value();
|
|
|
|
return DoubleToInt32(number->Number());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
uint32_t NumberToUint32(Object* number) {
|
|
|
|
if (number->IsSmi()) return Smi::cast(number)->value();
|
|
|
|
return DoubleToUint32(number->Number());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool TryNumberToSize(Isolate* isolate, Object* number, size_t* result) {
|
|
|
|
SealHandleScope shs(isolate);
|
|
|
|
if (number->IsSmi()) {
|
|
|
|
int value = Smi::cast(number)->value();
|
|
|
|
DCHECK(static_cast<unsigned>(Smi::kMaxValue) <=
|
|
|
|
std::numeric_limits<size_t>::max());
|
|
|
|
if (value >= 0) {
|
|
|
|
*result = static_cast<size_t>(value);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
DCHECK(number->IsHeapNumber());
|
|
|
|
double value = HeapNumber::cast(number)->value();
|
|
|
|
if (value >= 0 && value <= std::numeric_limits<size_t>::max()) {
|
|
|
|
*result = static_cast<size_t>(value);
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
size_t NumberToSize(Isolate* isolate, Object* number) {
|
|
|
|
size_t result = 0;
|
|
|
|
bool is_valid = TryNumberToSize(isolate, number, &result);
|
|
|
|
CHECK(is_valid);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-08-13 15:30:04 +00:00
|
|
|
uint32_t DoubleToUint32(double x) {
|
|
|
|
return static_cast<uint32_t>(DoubleToInt32(x));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-07-05 11:54:11 +00:00
|
|
|
template <class Iterator, class EndMark>
|
2011-11-29 10:56:11 +00:00
|
|
|
bool SubStringEquals(Iterator* current,
|
|
|
|
EndMark end,
|
|
|
|
const char* substring) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(**current == *substring);
|
2011-07-05 11:54:11 +00:00
|
|
|
for (substring++; *substring != '\0'; substring++) {
|
|
|
|
++*current;
|
|
|
|
if (*current == end || **current != *substring) return false;
|
|
|
|
}
|
|
|
|
++*current;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Returns true if a nonspace character has been found and false if the
|
|
|
|
// end was been reached before finding a nonspace character.
|
|
|
|
template <class Iterator, class EndMark>
|
2011-11-29 10:56:11 +00:00
|
|
|
inline bool AdvanceToNonspace(UnicodeCache* unicode_cache,
|
|
|
|
Iterator* current,
|
|
|
|
EndMark end) {
|
2011-07-05 11:54:11 +00:00
|
|
|
while (*current != end) {
|
2014-02-10 12:43:10 +00:00
|
|
|
if (!unicode_cache->IsWhiteSpaceOrLineTerminator(**current)) return true;
|
2011-07-05 11:54:11 +00:00
|
|
|
++*current;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Parsing integers with radix 2, 4, 8, 16, 32. Assumes current != end.
|
|
|
|
template <int radix_log_2, class Iterator, class EndMark>
|
2011-11-29 10:56:11 +00:00
|
|
|
double InternalStringToIntDouble(UnicodeCache* unicode_cache,
|
|
|
|
Iterator current,
|
|
|
|
EndMark end,
|
|
|
|
bool negative,
|
|
|
|
bool allow_trailing_junk) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(current != end);
|
2011-07-05 11:54:11 +00:00
|
|
|
|
|
|
|
// Skip leading 0s.
|
|
|
|
while (*current == '0') {
|
|
|
|
++current;
|
|
|
|
if (current == end) return SignedZero(negative);
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t number = 0;
|
|
|
|
int exponent = 0;
|
|
|
|
const int radix = (1 << radix_log_2);
|
|
|
|
|
|
|
|
do {
|
|
|
|
int digit;
|
|
|
|
if (*current >= '0' && *current <= '9' && *current < '0' + radix) {
|
|
|
|
digit = static_cast<char>(*current) - '0';
|
|
|
|
} else if (radix > 10 && *current >= 'a' && *current < 'a' + radix - 10) {
|
|
|
|
digit = static_cast<char>(*current) - 'a' + 10;
|
|
|
|
} else if (radix > 10 && *current >= 'A' && *current < 'A' + radix - 10) {
|
|
|
|
digit = static_cast<char>(*current) - 'A' + 10;
|
|
|
|
} else {
|
|
|
|
if (allow_trailing_junk ||
|
|
|
|
!AdvanceToNonspace(unicode_cache, ¤t, end)) {
|
|
|
|
break;
|
|
|
|
} else {
|
2011-07-07 13:57:58 +00:00
|
|
|
return JunkStringValue();
|
2011-07-05 11:54:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
number = number * radix + digit;
|
|
|
|
int overflow = static_cast<int>(number >> 53);
|
|
|
|
if (overflow != 0) {
|
|
|
|
// Overflow occurred. Need to determine which direction to round the
|
|
|
|
// result.
|
|
|
|
int overflow_bits_count = 1;
|
|
|
|
while (overflow > 1) {
|
|
|
|
overflow_bits_count++;
|
|
|
|
overflow >>= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int dropped_bits_mask = ((1 << overflow_bits_count) - 1);
|
|
|
|
int dropped_bits = static_cast<int>(number) & dropped_bits_mask;
|
|
|
|
number >>= overflow_bits_count;
|
|
|
|
exponent = overflow_bits_count;
|
|
|
|
|
|
|
|
bool zero_tail = true;
|
|
|
|
while (true) {
|
|
|
|
++current;
|
|
|
|
if (current == end || !isDigit(*current, radix)) break;
|
|
|
|
zero_tail = zero_tail && *current == '0';
|
|
|
|
exponent += radix_log_2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!allow_trailing_junk &&
|
|
|
|
AdvanceToNonspace(unicode_cache, ¤t, end)) {
|
2011-07-07 13:57:58 +00:00
|
|
|
return JunkStringValue();
|
2011-07-05 11:54:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int middle_value = (1 << (overflow_bits_count - 1));
|
|
|
|
if (dropped_bits > middle_value) {
|
|
|
|
number++; // Rounding up.
|
|
|
|
} else if (dropped_bits == middle_value) {
|
|
|
|
// Rounding to even to consistency with decimals: half-way case rounds
|
|
|
|
// up if significant part is odd and down otherwise.
|
|
|
|
if ((number & 1) != 0 || !zero_tail) {
|
|
|
|
number++; // Rounding up.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rounding up may cause overflow.
|
2013-01-07 15:02:56 +00:00
|
|
|
if ((number & (static_cast<int64_t>(1) << 53)) != 0) {
|
2011-07-05 11:54:11 +00:00
|
|
|
exponent++;
|
|
|
|
number >>= 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
++current;
|
|
|
|
} while (current != end);
|
|
|
|
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(number < ((int64_t)1 << 53));
|
|
|
|
DCHECK(static_cast<int64_t>(static_cast<double>(number)) == number);
|
2011-07-05 11:54:11 +00:00
|
|
|
|
|
|
|
if (exponent == 0) {
|
|
|
|
if (negative) {
|
|
|
|
if (number == 0) return -0.0;
|
|
|
|
number = -number;
|
|
|
|
}
|
|
|
|
return static_cast<double>(number);
|
|
|
|
}
|
|
|
|
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(number != 0);
|
2014-01-14 09:57:05 +00:00
|
|
|
return std::ldexp(static_cast<double>(negative ? -number : number), exponent);
|
2011-07-05 11:54:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <class Iterator, class EndMark>
|
2011-11-29 10:56:11 +00:00
|
|
|
double InternalStringToInt(UnicodeCache* unicode_cache,
|
|
|
|
Iterator current,
|
|
|
|
EndMark end,
|
|
|
|
int radix) {
|
2011-07-05 11:54:11 +00:00
|
|
|
const bool allow_trailing_junk = true;
|
2011-07-07 13:57:58 +00:00
|
|
|
const double empty_string_val = JunkStringValue();
|
2011-07-05 11:54:11 +00:00
|
|
|
|
|
|
|
if (!AdvanceToNonspace(unicode_cache, ¤t, end)) {
|
|
|
|
return empty_string_val;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool negative = false;
|
|
|
|
bool leading_zero = false;
|
|
|
|
|
|
|
|
if (*current == '+') {
|
|
|
|
// Ignore leading sign; skip following spaces.
|
|
|
|
++current;
|
|
|
|
if (current == end) {
|
2011-07-07 13:57:58 +00:00
|
|
|
return JunkStringValue();
|
2011-07-05 11:54:11 +00:00
|
|
|
}
|
|
|
|
} else if (*current == '-') {
|
|
|
|
++current;
|
|
|
|
if (current == end) {
|
2011-07-07 13:57:58 +00:00
|
|
|
return JunkStringValue();
|
2011-07-05 11:54:11 +00:00
|
|
|
}
|
|
|
|
negative = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (radix == 0) {
|
|
|
|
// Radix detection.
|
2012-08-08 07:44:17 +00:00
|
|
|
radix = 10;
|
2011-07-05 11:54:11 +00:00
|
|
|
if (*current == '0') {
|
|
|
|
++current;
|
|
|
|
if (current == end) return SignedZero(negative);
|
|
|
|
if (*current == 'x' || *current == 'X') {
|
|
|
|
radix = 16;
|
|
|
|
++current;
|
2011-07-07 13:57:58 +00:00
|
|
|
if (current == end) return JunkStringValue();
|
2011-07-05 11:54:11 +00:00
|
|
|
} else {
|
|
|
|
leading_zero = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (radix == 16) {
|
|
|
|
if (*current == '0') {
|
|
|
|
// Allow "0x" prefix.
|
|
|
|
++current;
|
|
|
|
if (current == end) return SignedZero(negative);
|
|
|
|
if (*current == 'x' || *current == 'X') {
|
|
|
|
++current;
|
2011-07-07 13:57:58 +00:00
|
|
|
if (current == end) return JunkStringValue();
|
2011-07-05 11:54:11 +00:00
|
|
|
} else {
|
|
|
|
leading_zero = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-07 13:57:58 +00:00
|
|
|
if (radix < 2 || radix > 36) return JunkStringValue();
|
2011-07-05 11:54:11 +00:00
|
|
|
|
|
|
|
// Skip leading zeros.
|
|
|
|
while (*current == '0') {
|
|
|
|
leading_zero = true;
|
|
|
|
++current;
|
|
|
|
if (current == end) return SignedZero(negative);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!leading_zero && !isDigit(*current, radix)) {
|
2011-07-07 13:57:58 +00:00
|
|
|
return JunkStringValue();
|
2011-07-05 11:54:11 +00:00
|
|
|
}
|
|
|
|
|
2014-09-02 13:36:35 +00:00
|
|
|
if (base::bits::IsPowerOfTwo32(radix)) {
|
2011-07-05 11:54:11 +00:00
|
|
|
switch (radix) {
|
|
|
|
case 2:
|
|
|
|
return InternalStringToIntDouble<1>(
|
|
|
|
unicode_cache, current, end, negative, allow_trailing_junk);
|
|
|
|
case 4:
|
|
|
|
return InternalStringToIntDouble<2>(
|
|
|
|
unicode_cache, current, end, negative, allow_trailing_junk);
|
|
|
|
case 8:
|
|
|
|
return InternalStringToIntDouble<3>(
|
|
|
|
unicode_cache, current, end, negative, allow_trailing_junk);
|
|
|
|
|
|
|
|
case 16:
|
|
|
|
return InternalStringToIntDouble<4>(
|
|
|
|
unicode_cache, current, end, negative, allow_trailing_junk);
|
|
|
|
|
|
|
|
case 32:
|
|
|
|
return InternalStringToIntDouble<5>(
|
|
|
|
unicode_cache, current, end, negative, allow_trailing_junk);
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (radix == 10) {
|
|
|
|
// Parsing with strtod.
|
|
|
|
const int kMaxSignificantDigits = 309; // Doubles are less than 1.8e308.
|
|
|
|
// The buffer may contain up to kMaxSignificantDigits + 1 digits and a zero
|
|
|
|
// end.
|
|
|
|
const int kBufferSize = kMaxSignificantDigits + 2;
|
|
|
|
char buffer[kBufferSize];
|
|
|
|
int buffer_pos = 0;
|
|
|
|
while (*current >= '0' && *current <= '9') {
|
|
|
|
if (buffer_pos <= kMaxSignificantDigits) {
|
|
|
|
// If the number has more than kMaxSignificantDigits it will be parsed
|
|
|
|
// as infinity.
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(buffer_pos < kBufferSize);
|
2011-07-05 11:54:11 +00:00
|
|
|
buffer[buffer_pos++] = static_cast<char>(*current);
|
|
|
|
}
|
|
|
|
++current;
|
|
|
|
if (current == end) break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!allow_trailing_junk &&
|
|
|
|
AdvanceToNonspace(unicode_cache, ¤t, end)) {
|
2011-07-07 13:57:58 +00:00
|
|
|
return JunkStringValue();
|
2011-07-05 11:54:11 +00:00
|
|
|
}
|
|
|
|
|
2014-08-04 11:34:54 +00:00
|
|
|
SLOW_DCHECK(buffer_pos < kBufferSize);
|
2011-07-05 11:54:11 +00:00
|
|
|
buffer[buffer_pos] = '\0';
|
|
|
|
Vector<const char> buffer_vector(buffer, buffer_pos);
|
|
|
|
return negative ? -Strtod(buffer_vector, 0) : Strtod(buffer_vector, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// The following code causes accumulating rounding error for numbers greater
|
|
|
|
// than ~2^56. It's explicitly allowed in the spec: "if R is not 2, 4, 8, 10,
|
|
|
|
// 16, or 32, then mathInt may be an implementation-dependent approximation to
|
|
|
|
// the mathematical integer value" (15.1.2.2).
|
|
|
|
|
|
|
|
int lim_0 = '0' + (radix < 10 ? radix : 10);
|
|
|
|
int lim_a = 'a' + (radix - 10);
|
|
|
|
int lim_A = 'A' + (radix - 10);
|
|
|
|
|
|
|
|
// NOTE: The code for computing the value may seem a bit complex at
|
|
|
|
// first glance. It is structured to use 32-bit multiply-and-add
|
|
|
|
// loops as long as possible to avoid loosing precision.
|
|
|
|
|
|
|
|
double v = 0.0;
|
|
|
|
bool done = false;
|
|
|
|
do {
|
|
|
|
// Parse the longest part of the string starting at index j
|
|
|
|
// possible while keeping the multiplier, and thus the part
|
|
|
|
// itself, within 32 bits.
|
|
|
|
unsigned int part = 0, multiplier = 1;
|
|
|
|
while (true) {
|
|
|
|
int d;
|
|
|
|
if (*current >= '0' && *current < lim_0) {
|
|
|
|
d = *current - '0';
|
|
|
|
} else if (*current >= 'a' && *current < lim_a) {
|
|
|
|
d = *current - 'a' + 10;
|
|
|
|
} else if (*current >= 'A' && *current < lim_A) {
|
|
|
|
d = *current - 'A' + 10;
|
|
|
|
} else {
|
|
|
|
done = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the value of the part as long as the multiplier fits
|
|
|
|
// in 32 bits. When we can't guarantee that the next iteration
|
|
|
|
// will not overflow the multiplier, we stop parsing the part
|
|
|
|
// by leaving the loop.
|
|
|
|
const unsigned int kMaximumMultiplier = 0xffffffffU / 36;
|
|
|
|
uint32_t m = multiplier * radix;
|
|
|
|
if (m > kMaximumMultiplier) break;
|
|
|
|
part = part * radix + d;
|
|
|
|
multiplier = m;
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(multiplier > part);
|
2011-07-05 11:54:11 +00:00
|
|
|
|
|
|
|
++current;
|
|
|
|
if (current == end) {
|
|
|
|
done = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the value and skip the part in the string.
|
|
|
|
v = v * multiplier + part;
|
|
|
|
} while (!done);
|
|
|
|
|
|
|
|
if (!allow_trailing_junk &&
|
|
|
|
AdvanceToNonspace(unicode_cache, ¤t, end)) {
|
2011-07-07 13:57:58 +00:00
|
|
|
return JunkStringValue();
|
2011-07-05 11:54:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return negative ? -v : v;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Converts a string to a double value. Assumes the Iterator supports
|
|
|
|
// the following operations:
|
|
|
|
// 1. current == end (other ops are not allowed), current != end.
|
|
|
|
// 2. *current - gets the current character in the sequence.
|
|
|
|
// 3. ++current (advances the position).
|
|
|
|
template <class Iterator, class EndMark>
|
2011-11-29 10:56:11 +00:00
|
|
|
double InternalStringToDouble(UnicodeCache* unicode_cache,
|
|
|
|
Iterator current,
|
|
|
|
EndMark end,
|
|
|
|
int flags,
|
|
|
|
double empty_string_val) {
|
2011-07-05 11:54:11 +00:00
|
|
|
// To make sure that iterator dereferencing is valid the following
|
|
|
|
// convention is used:
|
|
|
|
// 1. Each '++current' statement is followed by check for equality to 'end'.
|
|
|
|
// 2. If AdvanceToNonspace returned false then current == end.
|
|
|
|
// 3. If 'current' becomes be equal to 'end' the function returns or goes to
|
|
|
|
// 'parsing_done'.
|
|
|
|
// 4. 'current' is not dereferenced after the 'parsing_done' label.
|
|
|
|
// 5. Code before 'parsing_done' may rely on 'current != end'.
|
|
|
|
if (!AdvanceToNonspace(unicode_cache, ¤t, end)) {
|
|
|
|
return empty_string_val;
|
|
|
|
}
|
|
|
|
|
|
|
|
const bool allow_trailing_junk = (flags & ALLOW_TRAILING_JUNK) != 0;
|
|
|
|
|
|
|
|
// The longest form of simplified number is: "-<significant digits>'.1eXXX\0".
|
|
|
|
const int kBufferSize = kMaxSignificantDigits + 10;
|
|
|
|
char buffer[kBufferSize]; // NOLINT: size is known at compile time.
|
|
|
|
int buffer_pos = 0;
|
|
|
|
|
|
|
|
// Exponent will be adjusted if insignificant digits of the integer part
|
|
|
|
// or insignificant leading zeros of the fractional part are dropped.
|
|
|
|
int exponent = 0;
|
|
|
|
int significant_digits = 0;
|
|
|
|
int insignificant_digits = 0;
|
|
|
|
bool nonzero_digit_dropped = false;
|
|
|
|
|
2012-07-23 10:33:41 +00:00
|
|
|
enum Sign {
|
|
|
|
NONE,
|
|
|
|
NEGATIVE,
|
|
|
|
POSITIVE
|
|
|
|
};
|
|
|
|
|
|
|
|
Sign sign = NONE;
|
2011-07-05 11:54:11 +00:00
|
|
|
|
|
|
|
if (*current == '+') {
|
|
|
|
// Ignore leading sign.
|
|
|
|
++current;
|
2011-07-07 13:57:58 +00:00
|
|
|
if (current == end) return JunkStringValue();
|
2012-07-23 10:33:41 +00:00
|
|
|
sign = POSITIVE;
|
2011-07-05 11:54:11 +00:00
|
|
|
} else if (*current == '-') {
|
|
|
|
++current;
|
2011-07-07 13:57:58 +00:00
|
|
|
if (current == end) return JunkStringValue();
|
2012-07-23 10:33:41 +00:00
|
|
|
sign = NEGATIVE;
|
2011-07-05 11:54:11 +00:00
|
|
|
}
|
|
|
|
|
2013-02-28 17:03:34 +00:00
|
|
|
static const char kInfinityString[] = "Infinity";
|
|
|
|
if (*current == kInfinityString[0]) {
|
|
|
|
if (!SubStringEquals(¤t, end, kInfinityString)) {
|
2011-07-07 13:57:58 +00:00
|
|
|
return JunkStringValue();
|
2011-07-05 11:54:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!allow_trailing_junk &&
|
|
|
|
AdvanceToNonspace(unicode_cache, ¤t, end)) {
|
2011-07-07 13:57:58 +00:00
|
|
|
return JunkStringValue();
|
2011-07-05 11:54:11 +00:00
|
|
|
}
|
|
|
|
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(buffer_pos == 0);
|
2014-12-20 13:17:20 +00:00
|
|
|
return (sign == NEGATIVE) ? -V8_INFINITY : V8_INFINITY;
|
2011-07-05 11:54:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool leading_zero = false;
|
|
|
|
if (*current == '0') {
|
|
|
|
++current;
|
2012-07-23 10:33:41 +00:00
|
|
|
if (current == end) return SignedZero(sign == NEGATIVE);
|
2011-07-05 11:54:11 +00:00
|
|
|
|
|
|
|
leading_zero = true;
|
|
|
|
|
|
|
|
// It could be hexadecimal value.
|
|
|
|
if ((flags & ALLOW_HEX) && (*current == 'x' || *current == 'X')) {
|
|
|
|
++current;
|
2012-07-23 10:33:41 +00:00
|
|
|
if (current == end || !isDigit(*current, 16) || sign != NONE) {
|
2011-07-07 13:57:58 +00:00
|
|
|
return JunkStringValue(); // "0x".
|
2011-07-05 11:54:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return InternalStringToIntDouble<4>(unicode_cache,
|
|
|
|
current,
|
|
|
|
end,
|
2012-07-23 10:33:41 +00:00
|
|
|
false,
|
2011-07-05 11:54:11 +00:00
|
|
|
allow_trailing_junk);
|
2013-07-19 09:57:35 +00:00
|
|
|
|
|
|
|
// It could be an explicit octal value.
|
|
|
|
} else if ((flags & ALLOW_OCTAL) && (*current == 'o' || *current == 'O')) {
|
|
|
|
++current;
|
|
|
|
if (current == end || !isDigit(*current, 8) || sign != NONE) {
|
|
|
|
return JunkStringValue(); // "0o".
|
|
|
|
}
|
|
|
|
|
|
|
|
return InternalStringToIntDouble<3>(unicode_cache,
|
|
|
|
current,
|
|
|
|
end,
|
|
|
|
false,
|
|
|
|
allow_trailing_junk);
|
|
|
|
|
|
|
|
// It could be a binary value.
|
|
|
|
} else if ((flags & ALLOW_BINARY) && (*current == 'b' || *current == 'B')) {
|
|
|
|
++current;
|
|
|
|
if (current == end || !isBinaryDigit(*current) || sign != NONE) {
|
|
|
|
return JunkStringValue(); // "0b".
|
|
|
|
}
|
|
|
|
|
|
|
|
return InternalStringToIntDouble<1>(unicode_cache,
|
|
|
|
current,
|
|
|
|
end,
|
|
|
|
false,
|
|
|
|
allow_trailing_junk);
|
2011-07-05 11:54:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ignore leading zeros in the integer part.
|
|
|
|
while (*current == '0') {
|
|
|
|
++current;
|
2012-07-23 10:33:41 +00:00
|
|
|
if (current == end) return SignedZero(sign == NEGATIVE);
|
2011-07-05 11:54:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-19 09:57:35 +00:00
|
|
|
bool octal = leading_zero && (flags & ALLOW_IMPLICIT_OCTAL) != 0;
|
2011-07-05 11:54:11 +00:00
|
|
|
|
|
|
|
// Copy significant digits of the integer part (if any) to the buffer.
|
|
|
|
while (*current >= '0' && *current <= '9') {
|
|
|
|
if (significant_digits < kMaxSignificantDigits) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(buffer_pos < kBufferSize);
|
2011-07-05 11:54:11 +00:00
|
|
|
buffer[buffer_pos++] = static_cast<char>(*current);
|
|
|
|
significant_digits++;
|
|
|
|
// Will later check if it's an octal in the buffer.
|
|
|
|
} else {
|
|
|
|
insignificant_digits++; // Move the digit into the exponential part.
|
|
|
|
nonzero_digit_dropped = nonzero_digit_dropped || *current != '0';
|
|
|
|
}
|
|
|
|
octal = octal && *current < '8';
|
|
|
|
++current;
|
|
|
|
if (current == end) goto parsing_done;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (significant_digits == 0) {
|
|
|
|
octal = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*current == '.') {
|
2011-07-07 13:57:58 +00:00
|
|
|
if (octal && !allow_trailing_junk) return JunkStringValue();
|
2011-07-05 11:54:11 +00:00
|
|
|
if (octal) goto parsing_done;
|
|
|
|
|
|
|
|
++current;
|
|
|
|
if (current == end) {
|
|
|
|
if (significant_digits == 0 && !leading_zero) {
|
2011-07-07 13:57:58 +00:00
|
|
|
return JunkStringValue();
|
2011-07-05 11:54:11 +00:00
|
|
|
} else {
|
|
|
|
goto parsing_done;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (significant_digits == 0) {
|
|
|
|
// octal = false;
|
|
|
|
// Integer part consists of 0 or is absent. Significant digits start after
|
|
|
|
// leading zeros (if any).
|
|
|
|
while (*current == '0') {
|
|
|
|
++current;
|
2012-07-23 10:33:41 +00:00
|
|
|
if (current == end) return SignedZero(sign == NEGATIVE);
|
2011-07-05 11:54:11 +00:00
|
|
|
exponent--; // Move this 0 into the exponent.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-20 08:09:58 +00:00
|
|
|
// There is a fractional part. We don't emit a '.', but adjust the exponent
|
|
|
|
// instead.
|
2011-07-05 11:54:11 +00:00
|
|
|
while (*current >= '0' && *current <= '9') {
|
|
|
|
if (significant_digits < kMaxSignificantDigits) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(buffer_pos < kBufferSize);
|
2011-07-05 11:54:11 +00:00
|
|
|
buffer[buffer_pos++] = static_cast<char>(*current);
|
|
|
|
significant_digits++;
|
|
|
|
exponent--;
|
|
|
|
} else {
|
|
|
|
// Ignore insignificant digits in the fractional part.
|
|
|
|
nonzero_digit_dropped = nonzero_digit_dropped || *current != '0';
|
|
|
|
}
|
|
|
|
++current;
|
|
|
|
if (current == end) goto parsing_done;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!leading_zero && exponent == 0 && significant_digits == 0) {
|
|
|
|
// If leading_zeros is true then the string contains zeros.
|
|
|
|
// If exponent < 0 then string was [+-]\.0*...
|
|
|
|
// If significant_digits != 0 the string is not equal to 0.
|
|
|
|
// Otherwise there are no digits in the string.
|
2011-07-07 13:57:58 +00:00
|
|
|
return JunkStringValue();
|
2011-07-05 11:54:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Parse exponential part.
|
|
|
|
if (*current == 'e' || *current == 'E') {
|
2011-07-07 13:57:58 +00:00
|
|
|
if (octal) return JunkStringValue();
|
2011-07-05 11:54:11 +00:00
|
|
|
++current;
|
|
|
|
if (current == end) {
|
|
|
|
if (allow_trailing_junk) {
|
|
|
|
goto parsing_done;
|
|
|
|
} else {
|
2011-07-07 13:57:58 +00:00
|
|
|
return JunkStringValue();
|
2011-07-05 11:54:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
char sign = '+';
|
|
|
|
if (*current == '+' || *current == '-') {
|
|
|
|
sign = static_cast<char>(*current);
|
|
|
|
++current;
|
|
|
|
if (current == end) {
|
|
|
|
if (allow_trailing_junk) {
|
|
|
|
goto parsing_done;
|
|
|
|
} else {
|
2011-07-07 13:57:58 +00:00
|
|
|
return JunkStringValue();
|
2011-07-05 11:54:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (current == end || *current < '0' || *current > '9') {
|
|
|
|
if (allow_trailing_junk) {
|
|
|
|
goto parsing_done;
|
|
|
|
} else {
|
2011-07-07 13:57:58 +00:00
|
|
|
return JunkStringValue();
|
2011-07-05 11:54:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-20 13:17:20 +00:00
|
|
|
const int max_exponent = INT_MAX / 2;
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(-max_exponent / 2 <= exponent && exponent <= max_exponent / 2);
|
2011-07-05 11:54:11 +00:00
|
|
|
int num = 0;
|
|
|
|
do {
|
|
|
|
// Check overflow.
|
|
|
|
int digit = *current - '0';
|
|
|
|
if (num >= max_exponent / 10
|
|
|
|
&& !(num == max_exponent / 10 && digit <= max_exponent % 10)) {
|
|
|
|
num = max_exponent;
|
|
|
|
} else {
|
|
|
|
num = num * 10 + digit;
|
|
|
|
}
|
|
|
|
++current;
|
|
|
|
} while (current != end && *current >= '0' && *current <= '9');
|
|
|
|
|
|
|
|
exponent += (sign == '-' ? -num : num);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!allow_trailing_junk &&
|
|
|
|
AdvanceToNonspace(unicode_cache, ¤t, end)) {
|
2011-07-07 13:57:58 +00:00
|
|
|
return JunkStringValue();
|
2011-07-05 11:54:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
parsing_done:
|
|
|
|
exponent += insignificant_digits;
|
|
|
|
|
|
|
|
if (octal) {
|
|
|
|
return InternalStringToIntDouble<3>(unicode_cache,
|
|
|
|
buffer,
|
|
|
|
buffer + buffer_pos,
|
2012-07-23 10:33:41 +00:00
|
|
|
sign == NEGATIVE,
|
2011-07-05 11:54:11 +00:00
|
|
|
allow_trailing_junk);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nonzero_digit_dropped) {
|
|
|
|
buffer[buffer_pos++] = '1';
|
|
|
|
exponent--;
|
|
|
|
}
|
|
|
|
|
2014-08-04 11:34:54 +00:00
|
|
|
SLOW_DCHECK(buffer_pos < kBufferSize);
|
2011-07-05 11:54:11 +00:00
|
|
|
buffer[buffer_pos] = '\0';
|
|
|
|
|
|
|
|
double converted = Strtod(Vector<const char>(buffer, buffer_pos), exponent);
|
2012-07-23 10:33:41 +00:00
|
|
|
return (sign == NEGATIVE) ? -converted : converted;
|
2011-07-05 11:54:11 +00:00
|
|
|
}
|
|
|
|
|
2015-09-30 13:46:56 +00:00
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
#endif // V8_CONVERSIONS_INL_H_
|