Skip to content

Commit

Permalink
Fix 16-bit separators.
Browse files Browse the repository at this point in the history
Fixes #89.
  • Loading branch information
floitsch committed Mar 4, 2019
1 parent 4b2a7f3 commit 8751aaf
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 4 deletions.
6 changes: 3 additions & 3 deletions double-conversion/double-conversion.cc
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,7 @@ static bool IsCharacterDigitForRadix(int c, int radix, char a_character) {

// Returns true, when the iterator is equal to end.
template<class Iterator>
static bool Advance (Iterator* it, char separator, int base, Iterator& end) {
static bool Advance (Iterator* it, uc16 separator, int base, Iterator& end) {
if (separator == StringToDoubleConverter::kNoSeparator) {
++(*it);
return *it == end;
Expand Down Expand Up @@ -581,7 +581,7 @@ static bool Advance (Iterator* it, char separator, int base, Iterator& end) {
template<class Iterator>
static bool IsHexFloatString(Iterator start,
Iterator end,
char separator,
uc16 separator,
bool allow_trailing_junk) {
ASSERT(start != end);

Expand Down Expand Up @@ -622,7 +622,7 @@ template <int radix_log_2, class Iterator>
static double RadixStringToIeee(Iterator* current,
Iterator end,
bool sign,
char separator,
uc16 separator,
bool parse_as_hex_float,
bool allow_trailing_junk,
double junk_string_value,
Expand Down
94 changes: 93 additions & 1 deletion test/cctest/test-conversions.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1735,6 +1735,33 @@ static double StrToD16(const uc16* str16, int length, int flags,
}


static double StrToD16(const char* str, int flags,
double empty_string_value,
int* processed_characters_count, bool* processed_all,
char char_separator, uc16 separator) {
uc16 str16[256];
int length = -1;
for (int i = 0;; i++) {
if (str[i] == char_separator) {
str16[i] = separator;
} else {
str16[i] = str[i];
}
if (str[i] == '\0') {
length = i;
break;
}
}
ASSERT(length < 256);
StringToDoubleConverter converter(flags, empty_string_value, Double::NaN(),
NULL, NULL, separator);
double result =
converter.StringToDouble(str16, length, processed_characters_count);
*processed_all = (length == *processed_characters_count);
return result;
}


static double StrToD(const char* str, int flags, double empty_string_value,
int* processed_characters_count, bool* processed_all,
uc16 separator = StringToDoubleConverter::kNoSeparator) {
Expand Down Expand Up @@ -3207,7 +3234,7 @@ TEST(StringToDoubleSeparator) {
int flags;
int processed;
bool all_used;
char separator;
uc16 separator;

separator = '\'';
flags = StringToDoubleConverter::NO_FLAGS;
Expand Down Expand Up @@ -3518,6 +3545,71 @@ TEST(StringToDoubleSeparator) {
CHECK_EQ(Double::NaN(),
StrToD("0x0 3.p -0", flags, 0.0, &processed, &all_used));
CHECK_EQ(0, processed);

separator = 0x202F;
char char_separator = '@';
flags = StringToDoubleConverter::ALLOW_HEX |
StringToDoubleConverter::ALLOW_HEX_FLOATS |
StringToDoubleConverter::ALLOW_LEADING_SPACES |
StringToDoubleConverter::ALLOW_TRAILING_SPACES;

CHECK_EQ(18.0,
StrToD16("0x1@2", flags, 0.0, &processed, &all_used,
char_separator, separator));
CHECK(all_used);

CHECK_EQ(0.0, StrToD16("0x0@0", flags, 1.0, &processed, &all_used,
char_separator, separator));
CHECK(all_used);

CHECK_EQ(static_cast<double>(0x123456789),
StrToD16("0x1@2@3@4@5@6@7@8@9", flags, Double::NaN(),
&processed, &all_used, char_separator, separator));
CHECK(all_used);

CHECK_EQ(18.0, StrToD16(" 0x1@2 ", flags, 0.0,
&processed, &all_used, char_separator, separator));
CHECK(all_used);

CHECK_EQ(static_cast<double>(0xabcdef),
StrToD16("0xa@b@c@d@e@f", flags, 0.0,
&processed, &all_used, char_separator, separator));
CHECK(all_used);

CHECK_EQ(Double::NaN(),
StrToD16("0x@1@2", flags, 0.0,
&processed, &all_used, char_separator, separator));
CHECK_EQ(0, processed);

CHECK_EQ(Double::NaN(),
StrToD16("0@x0", flags, 1.0,
&processed, &all_used, char_separator, separator));
CHECK_EQ(0, processed);

CHECK_EQ(Double::NaN(),
StrToD16("0x1@2@@3@4@5@6@7@8@9", flags, Double::NaN(),
&processed, &all_used, char_separator, separator));
CHECK_EQ(0, processed);

CHECK_EQ(3.0,
StrToD16("0x0@3p0", flags, 0.0, &processed, &all_used,
char_separator, separator));
CHECK(all_used);

CHECK_EQ(0.0,
StrToD16("0x.0@0p0", flags, 0.0, &processed, &all_used,
char_separator, separator));
CHECK(all_used);

CHECK_EQ(3.0,
StrToD16("0x3.0@0p0", flags, 0.0, &processed, &all_used,
char_separator, separator));
CHECK(all_used);

CHECK_EQ(3.0,
StrToD16("0x0@23.p0", flags, 0.0, &processed, &all_used,
char_separator, separator));
CHECK(all_used);
}

TEST(StringToDoubleSpecialValues) {
Expand Down

0 comments on commit 8751aaf

Please sign in to comment.