ext/include/iv/conversions.h in iv-phonic-0.0.7 vs ext/include/iv/conversions.h in iv-phonic-0.0.8
- old
+ new
@@ -1,14 +1,15 @@
-#ifndef IV_CONVERSIONS_H_
-#define IV_CONVERSIONS_H_
+#ifndef _IV_CONVERSIONS_H_
+#define _IV_CONVERSIONS_H_
#include <cstdio>
#include <cmath>
#include <string>
#include <limits>
#include <tr1/array>
#include <tr1/cstdint>
-#include "chars.h"
+#include <tr1/cmath>
+#include "character.h"
#include "dtoa.h"
#include "ustringpiece.h"
#include "none.h"
namespace iv {
namespace core {
@@ -58,11 +59,11 @@
if (it == last) {
return (parse_float) ? Conversions::kNaN : 0;
}
while (it != last &&
- (Chars::IsWhiteSpace(*it) || Chars::IsLineTerminator(*it))) {
+ (character::IsWhiteSpace(*it) || character::IsLineTerminator(*it))) {
++it;
}
// white space only " "
if (it == last) {
@@ -81,11 +82,11 @@
if (it == last) {
return Conversions::kNaN;
}
- if (Chars::IsDecimalDigit(*it)) {
+ if (character::IsDecimalDigit(*it)) {
if (*it == '0') {
is_found_zero = true;
++it;
if (it == last) {
return (is_signed) ? -0.0 : 0.0;
@@ -97,18 +98,18 @@
is_decimal = false;
buffer[pos++] = '0';
buffer[pos++] = *it;
++it;
++significant_digits;
- if (it == last || !Chars::IsHexDigit(*it)) {
+ if (it == last || !character::IsHexDigit(*it)) {
return Conversions::kNaN;
}
// waste leading zero
while (it != last && *it == '0') {
++it;
}
- while (it != last && Chars::IsHexDigit(*it)) {
+ while (it != last && character::IsHexDigit(*it)) {
if (significant_digits < Conversions::kMaxSignificantDigits) {
buffer[pos++] = *it;
++it;
++significant_digits;
} else {
@@ -122,11 +123,11 @@
}
}
}
if (is_decimal) {
while (it != last &&
- Chars::IsDecimalDigit(*it)) {
+ character::IsDecimalDigit(*it)) {
if (significant_digits < Conversions::kMaxSignificantDigits) {
buffer[pos++] = *it;
++significant_digits;
} else {
++insignificant_digits;
@@ -135,11 +136,11 @@
}
if (it != last && *it == '.') {
buffer[pos++] = '.';
++it;
while (it != last &&
- Chars::IsDecimalDigit(*it)) {
+ character::IsDecimalDigit(*it)) {
if (significant_digits < Conversions::kMaxSignificantDigits) {
buffer[pos++] = *it;
++significant_digits;
}
++it;
@@ -150,11 +151,11 @@
if (*it == '.') {
buffer[pos++] = '.';
++it;
const Iter start = it;
while (it != last &&
- Chars::IsDecimalDigit(*it)) {
+ character::IsDecimalDigit(*it)) {
if (significant_digits < Conversions::kMaxSignificantDigits) {
buffer[pos++] = *it;
++significant_digits;
}
++it;
@@ -171,11 +172,12 @@
return Conversions::kNaN;
}
}
// infinity
while (it != last &&
- (Chars::IsWhiteSpace(*it) || Chars::IsLineTerminator(*it))) {
+ (character::IsWhiteSpace(*it) ||
+ character::IsLineTerminator(*it))) {
++it;
}
if (it == last || parse_float) {
if (is_signed) {
return -std::numeric_limits<double>::infinity();
@@ -207,11 +209,11 @@
if (*it == '+' || *it == '-') {
buffer[pos++] = *it;
++it;
is_signed_exp = true;
}
- if (it == last || !Chars::IsDecimalDigit(*it)) {
+ if (it == last || !character::IsDecimalDigit(*it)) {
if (parse_float) {
--it;
--pos;
if (is_signed_exp) {
--it;
@@ -227,11 +229,11 @@
exponent = 9999;
} else {
exponent = exponent * 10 + (*it - '0');
}
++it;
- } while (it != last && Chars::IsDecimalDigit(*it));
+ } while (it != last && character::IsDecimalDigit(*it));
exponent+=insignificant_digits;
if (exponent > 9999) {
exponent = 9999;
}
std::snprintf(buffer.data()+pos, 5, "%d", exponent); // NOLINT
@@ -240,11 +242,11 @@
// exponent_pasing_done label
exponent_pasing_done:
while (it != last &&
- (Chars::IsWhiteSpace(*it) || Chars::IsLineTerminator(*it))) {
+ (character::IsWhiteSpace(*it) || character::IsLineTerminator(*it))) {
++it;
}
if (it == last || parse_float) {
if (pos == 0) {
@@ -303,11 +305,11 @@
template<typename Iter>
inline double StringToIntegerWithRadix(Iter it, Iter last,
int radix, bool strip_prefix) {
// remove leading white space
while (it != last &&
- (Chars::IsWhiteSpace(*it) || Chars::IsLineTerminator(*it))) {
+ (character::IsWhiteSpace(*it) || character::IsLineTerminator(*it))) {
++it;
}
// empty string ""
if (it == last) {
@@ -409,10 +411,31 @@
inline uint32_t DoubleToUInt32(double d) {
return static_cast<uint32_t>(DoubleToInt32(d));
}
+inline int64_t DoubleToInt64(double d) {
+ int64_t i = static_cast<int64_t>(d);
+ if (static_cast<double>(i) == d) {
+ return i;
+ }
+ if (!std::isfinite(d) || d == 0) {
+ return 0;
+ }
+ if (Conversions::DoubleToInt32_Two32 >= d) {
+ return static_cast<int64_t>(DoubleToInt32(d));
+ }
+ const int32_t lo = DoubleToInt32(
+ std::fmod(d, Conversions::DoubleToInt32_Two32));
+ const int32_t hi = DoubleToInt32(d / Conversions::DoubleToInt32_Two32);
+ return hi * 4294967296ULL + lo;
+}
+
+inline uint64_t DoubleToUInt64(double d) {
+ return static_cast<uint64_t>(DoubleToInt64(d));
+}
+
inline double DoubleToInteger(double d) {
if (std::isnan(d)) {
return 0;
}
if (!std::isfinite(d) || d == 0) {
@@ -432,21 +455,21 @@
} else {
*value = 0;
return true;
}
}
- if (it != last && Chars::IsDecimalDigit(*it)) {
+ if (it != last && character::IsDecimalDigit(*it)) {
ch = *it - '0';
*value = ch;
} else {
return false;
}
++it;
uint32_t prev = *value;
for (;it != last; ++it) {
prev = *value;
- if (Chars::IsDecimalDigit(*it)) {
+ if (character::IsDecimalDigit(*it)) {
ch = *it - '0';
*value = ch + (prev * 10);
} else {
return false;
}
@@ -510,6 +533,6 @@
DoubleToStringWithRadix(v, radix, &str);
return str;
}
} } // namespace iv::core
-#endif // IV_CONVERSIONS_H_
+#endif // _IV_CONVERSIONS_H_