Commit 85015726 authored by Kent Tamura's avatar Kent Tamura Committed by Commit Bot

Add tests and comments for WTF::CharactersToDouble() and WTF::CharactersToFloat().

This CL has no behavior changes.

Bug: 746157
Change-Id: Ia40822c341d827fccebb3968961205a0dd69a188
Reviewed-on: https://chromium-review.googlesource.com/593369Reviewed-by: default avatarTakayoshi Kochi <kochi@chromium.org>
Commit-Queue: Kent Tamura <tkent@chromium.org>
Cr-Commit-Position: refs/heads/master@{#490740}
parent 14b298ca
......@@ -84,22 +84,68 @@ WTF_EXPORT uint64_t CharactersToUInt64(const UChar*,
// is trailing garbage. Like the non-strict functions above, these return the
// value when there is trailing garbage. It would be better if these were more
// consistent with the above functions instead.
//
// string -> double.
//
// These functions accepts:
// - leading '+'
// - numbers without leading zeros such as ".5"
// - numbers ending with "." such as "3."
// - scientific notation
// - leading whitespace (IsASCIISpace, not IsHTMLSpace)
// - no trailing whitespace
// - no trailing garbage
// - no numbers such as "NaN" "Infinity"
//
// A huge absolute number which a double can't represent is accepted, and
// +Infinity or -Infinity is returned.
//
// A small absolute numbers which a double can't represent is accepted, and
// 0 is returned
WTF_EXPORT double CharactersToDouble(const LChar*, size_t, bool* ok);
WTF_EXPORT double CharactersToDouble(const UChar*, size_t, bool* ok);
// |parsed_length| will have the length of characters which was parsed as a
// double number. It will be 0 if the input string isn't a number. It will be
// smaller than |length| if the input string contains trailing
// whiespace/garbage.
WTF_EXPORT double CharactersToDouble(const LChar*,
size_t,
size_t length,
size_t& parsed_length);
WTF_EXPORT double CharactersToDouble(const UChar*,
size_t,
size_t length,
size_t& parsed_length);
// string -> float.
//
// These functions accepts:
// - leading '+'
// - numbers without leading zeros such as ".5"
// - numbers ending with "." such as "3."
// - scientific notation
// - leading whitespace (IsASCIISpace, not IsHTMLSpace)
// - no trailing whitespace
// - no trailing garbage
// - no numbers such as "NaN" "Infinity"
//
// A huge absolute number which a float can't represent is accepted, and
// +Infinity or -Infinity is returned.
//
// A small absolute numbers which a float can't represent is accepted, and
// 0 is returned
WTF_EXPORT float CharactersToFloat(const LChar*, size_t, bool* ok);
WTF_EXPORT float CharactersToFloat(const UChar*, size_t, bool* ok);
WTF_EXPORT float CharactersToFloat(const LChar*, size_t, size_t& parsed_length);
WTF_EXPORT float CharactersToFloat(const UChar*, size_t, size_t& parsed_length);
// |parsed_length| will have the length of characters which was parsed as a
// flaot number. It will be 0 if the input string isn't a number. It will be
// smaller than |length| if the input string contains trailing
// whiespace/garbage.
WTF_EXPORT float CharactersToFloat(const LChar*,
size_t length,
size_t& parsed_length);
WTF_EXPORT float CharactersToFloat(const UChar*,
size_t length,
size_t& parsed_length);
} // namespace WTF
......
......@@ -241,4 +241,145 @@ TEST(StringToNumberTest, NumberParsingState) {
EXPECT_EQ(NumberParsingResult::kSuccess, ParseUInt("10", &value));
}
void ParseDouble(const char* str, double expected_value) {
bool ok;
double value = CharactersToDouble(reinterpret_cast<const LChar*>(str),
std::strlen(str), &ok);
EXPECT_TRUE(ok) << "\"" << str << "\"";
EXPECT_EQ(expected_value, value);
}
void FailToParseDouble(const char* str) {
bool ok;
CharactersToDouble(reinterpret_cast<const LChar*>(str), std::strlen(str),
&ok);
EXPECT_FALSE(ok) << "\"" << str << "\"";
}
TEST(StringToNumberTest, CharactersToDouble) {
FailToParseDouble("");
ParseDouble("0", 0.0);
ParseDouble("-0", 0.0);
ParseDouble("1.5", 1.5);
ParseDouble("+1.5", 1.5);
FailToParseDouble("+");
FailToParseDouble("-");
ParseDouble(".5", 0.5);
ParseDouble("1.", 1);
FailToParseDouble(".");
ParseDouble("1e-100", 1e-100);
ParseDouble("1e100", 1e+100);
ParseDouble(" 1.5", 1.5);
FailToParseDouble("1.5 ");
FailToParseDouble("1.5px");
FailToParseDouble("NaN");
FailToParseDouble("nan");
FailToParseDouble("Infinity");
FailToParseDouble("infinity");
FailToParseDouble("Inf");
FailToParseDouble("inf");
ParseDouble("1e+4000", std::numeric_limits<double>::infinity());
ParseDouble("-1e+4000", -std::numeric_limits<double>::infinity());
ParseDouble("1e-4000", 0);
FailToParseDouble("1e");
FailToParseDouble("1e-");
FailToParseDouble("1e+");
FailToParseDouble("1e3.");
FailToParseDouble("1e3.5");
FailToParseDouble("1e.3");
}
size_t ParseDouble(const char* str) {
size_t parsed;
CharactersToDouble(reinterpret_cast<const LChar*>(str), std::strlen(str),
parsed);
return parsed;
}
TEST(StringToNumberTest, CharactersToDoubleParsedLength) {
EXPECT_EQ(0u, ParseDouble(""));
EXPECT_EQ(0u, ParseDouble(" "));
EXPECT_EQ(0u, ParseDouble("+"));
EXPECT_EQ(0u, ParseDouble("-"));
EXPECT_EQ(0u, ParseDouble("."));
EXPECT_EQ(0u, ParseDouble(" "));
EXPECT_EQ(4u, ParseDouble(" 123"));
EXPECT_EQ(4u, ParseDouble(" 123 "));
EXPECT_EQ(4u, ParseDouble(" 123px"));
EXPECT_EQ(5u, ParseDouble("1.234"));
EXPECT_EQ(5u, ParseDouble("1.234e"));
EXPECT_EQ(7u, ParseDouble("1.234e1"));
}
void ParseFloat(const char* str, float expected_value) {
bool ok;
float value = CharactersToFloat(reinterpret_cast<const LChar*>(str),
std::strlen(str), &ok);
EXPECT_TRUE(ok) << "\"" << str << "\"";
EXPECT_EQ(expected_value, value);
}
void FailToParseFloat(const char* str) {
bool ok;
CharactersToFloat(reinterpret_cast<const LChar*>(str), std::strlen(str), &ok);
EXPECT_FALSE(ok) << "\"" << str << "\"";
}
TEST(StringToNumberTest, CharactersToFloat) {
FailToParseFloat("");
ParseFloat("0", 0.0f);
ParseFloat("-0", 0.0f);
ParseFloat("1.5", 1.5f);
ParseFloat("+1.5", 1.5f);
FailToParseFloat("+");
FailToParseFloat("-");
ParseFloat(".5", 0.5f);
ParseFloat("1.", 1.0f);
FailToParseFloat(".");
ParseFloat("1e-40", 1e-40f);
ParseFloat("1e30", 1e+30f);
ParseFloat(" 1.5", 1.5f);
FailToParseFloat("1.5 ");
FailToParseFloat("1.5px");
FailToParseFloat("NaN");
FailToParseFloat("nan");
FailToParseFloat("Infinity");
FailToParseFloat("infinity");
FailToParseFloat("Inf");
FailToParseFloat("inf");
ParseFloat("1e+4000", std::numeric_limits<float>::infinity());
ParseFloat("-1e+4000", -std::numeric_limits<float>::infinity());
ParseFloat("1e+100", std::numeric_limits<float>::infinity());
ParseFloat("-1e+100", -std::numeric_limits<float>::infinity());
ParseFloat("1e-4000", 0);
FailToParseFloat("1e");
FailToParseFloat("1e-");
FailToParseFloat("1e+");
FailToParseFloat("1e3.");
FailToParseFloat("1e3.5");
FailToParseFloat("1e.3");
}
size_t ParseFloat(const char* str) {
size_t parsed;
CharactersToFloat(reinterpret_cast<const LChar*>(str), std::strlen(str),
parsed);
return parsed;
}
TEST(StringToNumberTest, CharactersToFloatParsedLength) {
EXPECT_EQ(0u, ParseFloat(""));
EXPECT_EQ(0u, ParseFloat(" "));
EXPECT_EQ(0u, ParseFloat("+"));
EXPECT_EQ(0u, ParseFloat("-"));
EXPECT_EQ(0u, ParseFloat("."));
EXPECT_EQ(0u, ParseFloat(" "));
EXPECT_EQ(4u, ParseFloat(" 123"));
EXPECT_EQ(4u, ParseFloat(" 123 "));
EXPECT_EQ(4u, ParseFloat(" 123px"));
EXPECT_EQ(5u, ParseFloat("1.234"));
EXPECT_EQ(5u, ParseFloat("1.234e"));
EXPECT_EQ(7u, ParseFloat("1.234e1"));
}
} // namespace WTF
......@@ -384,6 +384,22 @@ class WTF_EXPORT String {
int64_t ToInt64(bool* ok = 0) const;
uint64_t ToUInt64(bool* ok = 0) const;
// These functions accepts:
// - leading '+'
// - numbers without leading zeros such as ".5"
// - numbers ending with "." such as "3."
// - scientific notation
// - leading whitespace (IsASCIISpace, not IsHTMLSpace)
// - no trailing whitespace
// - no trailing garbage
// - no numbers such as "NaN" "Infinity"
//
// A huge absolute number which a double/float can't represent is accepted,
// and +Infinity or -Infinity is returned.
//
// A small absolute numbers which a double/float can't represent is accepted,
// and 0 is returned
//
// FIXME: Like the strict functions above, these give false for "ok" when
// there is trailing garbage. Like the non-strict functions above, these
// return the value when there is trailing garbage. It would be better if
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment