diff options
| author | Steven Moreland <smoreland@google.com> | 2018-08-09 16:14:18 +0000 |
|---|---|---|
| committer | Gerrit Code Review <noreply-gerritcodereview@google.com> | 2018-08-09 16:14:18 +0000 |
| commit | 9ce0de0ed1a10ef862c795f7479558d6654314b9 (patch) | |
| tree | b9e5409840d439f5517dfc352e661f5703735a68 /base | |
| parent | 2d3f234f2000dcf5a642af6db41167e277afcb68 (diff) | |
| parent | 586ef26f746108617703f4e863b8b271666e38b5 (diff) | |
| download | system_core-9ce0de0ed1a10ef862c795f7479558d6654314b9.tar.gz system_core-9ce0de0ed1a10ef862c795f7479558d6654314b9.tar.bz2 system_core-9ce0de0ed1a10ef862c795f7479558d6654314b9.zip | |
Merge "Add ParseFloat (in parsedouble.h)."
Diffstat (limited to 'base')
| -rw-r--r-- | base/include/android-base/parsedouble.h | 27 | ||||
| -rw-r--r-- | base/parsedouble_test.cpp | 26 |
2 files changed, 47 insertions, 6 deletions
diff --git a/base/include/android-base/parsedouble.h b/base/include/android-base/parsedouble.h index 9a20eb194..7b1c648b7 100644 --- a/base/include/android-base/parsedouble.h +++ b/base/include/android-base/parsedouble.h @@ -24,15 +24,14 @@ namespace android { namespace base { -// Parse double value in the string 's' and sets 'out' to that value if it exists. +// Parse floating value in the string 's' and sets 'out' to that value if it exists. // Optionally allows the caller to define a 'min' and 'max' beyond which // otherwise valid values will be rejected. Returns boolean success. -static inline bool ParseDouble(const char* s, double* out, - double min = std::numeric_limits<double>::lowest(), - double max = std::numeric_limits<double>::max()) { +template <typename T, T (*strtox)(const char* str, char** endptr)> +static inline bool ParseFloatingPoint(const char* s, T* out, T min, T max) { errno = 0; char* end; - double result = strtod(s, &end); + T result = strtox(s, &end); if (errno != 0 || s == end || *end != '\0') { return false; } @@ -45,5 +44,23 @@ static inline bool ParseDouble(const char* s, double* out, return true; } +// Parse double value in the string 's' and sets 'out' to that value if it exists. +// Optionally allows the caller to define a 'min' and 'max' beyond which +// otherwise valid values will be rejected. Returns boolean success. +static inline bool ParseDouble(const char* s, double* out, + double min = std::numeric_limits<double>::lowest(), + double max = std::numeric_limits<double>::max()) { + return ParseFloatingPoint<double, strtod>(s, out, min, max); +} + +// Parse float value in the string 's' and sets 'out' to that value if it exists. +// Optionally allows the caller to define a 'min' and 'max' beyond which +// otherwise valid values will be rejected. Returns boolean success. +static inline bool ParseFloat(const char* s, float* out, + float min = std::numeric_limits<float>::lowest(), + float max = std::numeric_limits<float>::max()) { + return ParseFloatingPoint<float, strtof>(s, out, min, max); +} + } // namespace base } // namespace android diff --git a/base/parsedouble_test.cpp b/base/parsedouble_test.cpp index 797a370bc..ec3c10c74 100644 --- a/base/parsedouble_test.cpp +++ b/base/parsedouble_test.cpp @@ -18,7 +18,7 @@ #include <gtest/gtest.h> -TEST(parsedouble, smoke) { +TEST(parsedouble, double_smoke) { double d; ASSERT_FALSE(android::base::ParseDouble("", &d)); ASSERT_FALSE(android::base::ParseDouble("x", &d)); @@ -41,3 +41,27 @@ TEST(parsedouble, smoke) { ASSERT_FALSE(android::base::ParseDouble("3.0", nullptr, -1.0, 2.0)); ASSERT_TRUE(android::base::ParseDouble("1.0", nullptr, 0.0, 2.0)); } + +TEST(parsedouble, float_smoke) { + float f; + ASSERT_FALSE(android::base::ParseFloat("", &f)); + ASSERT_FALSE(android::base::ParseFloat("x", &f)); + ASSERT_FALSE(android::base::ParseFloat("123.4x", &f)); + + ASSERT_TRUE(android::base::ParseFloat("123.4", &f)); + ASSERT_FLOAT_EQ(123.4, f); + ASSERT_TRUE(android::base::ParseFloat("-123.4", &f)); + ASSERT_FLOAT_EQ(-123.4, f); + + ASSERT_TRUE(android::base::ParseFloat("0", &f, 0.0)); + ASSERT_FLOAT_EQ(0.0, f); + ASSERT_FALSE(android::base::ParseFloat("0", &f, 1e-9)); + ASSERT_FALSE(android::base::ParseFloat("3.0", &f, -1.0, 2.0)); + ASSERT_TRUE(android::base::ParseFloat("1.0", &f, 0.0, 2.0)); + ASSERT_FLOAT_EQ(1.0, f); + + ASSERT_FALSE(android::base::ParseFloat("123.4x", nullptr)); + ASSERT_TRUE(android::base::ParseFloat("-123.4", nullptr)); + ASSERT_FALSE(android::base::ParseFloat("3.0", nullptr, -1.0, 2.0)); + ASSERT_TRUE(android::base::ParseFloat("1.0", nullptr, 0.0, 2.0)); +} |
