diff options
author | Steven Moreland <smoreland@google.com> | 2018-08-08 16:39:35 -0700 |
---|---|---|
committer | Steven Moreland <smoreland@google.com> | 2018-08-08 16:50:27 -0700 |
commit | 1b8981e972895d69eda3d6f96adf4581a450ee30 (patch) | |
tree | a156c6ee6e023967b965c3a818c5e6490af81e2d /base | |
parent | f41897a66e4e99cdda38c8150cd1cec543e2c393 (diff) | |
download | system_core-1b8981e972895d69eda3d6f96adf4581a450ee30.tar.gz system_core-1b8981e972895d69eda3d6f96adf4581a450ee30.tar.bz2 system_core-1b8981e972895d69eda3d6f96adf4581a450ee30.zip |
ParseDouble: allow validation only.
This change also updates documentation for Parse(Ui|I)nt functions
which recently had a corresponding change applied.
Bug: 110758329
Test: libbase_test
Change-Id: I4842c0500a6e49498eeb8a63d1117c06727fffdf
Diffstat (limited to 'base')
-rw-r--r-- | base/include/android-base/parsedouble.h | 6 | ||||
-rw-r--r-- | base/include/android-base/parseint.h | 12 | ||||
-rw-r--r-- | base/parsedouble_test.cpp | 5 |
3 files changed, 15 insertions, 8 deletions
diff --git a/base/include/android-base/parsedouble.h b/base/include/android-base/parsedouble.h index c273c61d3..9a20eb194 100644 --- a/base/include/android-base/parsedouble.h +++ b/base/include/android-base/parsedouble.h @@ -24,7 +24,7 @@ namespace android { namespace base { -// Parse double value in the string 's' and sets 'out' to that value. +// 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, @@ -39,7 +39,9 @@ static inline bool ParseDouble(const char* s, double* out, if (result < min || max < result) { return false; } - *out = result; + if (out != nullptr) { + *out = result; + } return true; } diff --git a/base/include/android-base/parseint.h b/base/include/android-base/parseint.h index bb54c99b8..55f1ed3ea 100644 --- a/base/include/android-base/parseint.h +++ b/base/include/android-base/parseint.h @@ -27,9 +27,9 @@ namespace android { namespace base { // Parses the unsigned decimal or hexadecimal integer in the string 's' and sets -// 'out' to that value. Optionally allows the caller to define a 'max' beyond -// which otherwise valid values will be rejected. Returns boolean success; 'out' -// is untouched if parsing fails. +// 'out' to that value if it is specified. Optionally allows the caller to define +// a 'max' beyond which otherwise valid values will be rejected. Returns boolean +// success; 'out' is untouched if parsing fails. template <typename T> bool ParseUint(const char* s, T* out, T max = std::numeric_limits<T>::max(), bool allow_suffixes = false) { @@ -72,9 +72,9 @@ bool ParseByteCount(const std::string& s, T* out, T max = std::numeric_limits<T> } // Parses the signed decimal or hexadecimal integer in the string 's' and sets -// 'out' to that value. Optionally allows the caller to define a 'min' and 'max' -// beyond which otherwise valid values will be rejected. Returns boolean -// success; 'out' is untouched if parsing fails. +// 'out' to that value if it is specified. Optionally allows the caller to define +// a 'min' and 'max' beyond which otherwise valid values will be rejected. Returns +// boolean success; 'out' is untouched if parsing fails. template <typename T> bool ParseInt(const char* s, T* out, T min = std::numeric_limits<T>::min(), diff --git a/base/parsedouble_test.cpp b/base/parsedouble_test.cpp index 8734c4257..797a370bc 100644 --- a/base/parsedouble_test.cpp +++ b/base/parsedouble_test.cpp @@ -35,4 +35,9 @@ TEST(parsedouble, smoke) { ASSERT_FALSE(android::base::ParseDouble("3.0", &d, -1.0, 2.0)); ASSERT_TRUE(android::base::ParseDouble("1.0", &d, 0.0, 2.0)); ASSERT_DOUBLE_EQ(1.0, d); + + ASSERT_FALSE(android::base::ParseDouble("123.4x", nullptr)); + ASSERT_TRUE(android::base::ParseDouble("-123.4", nullptr)); + ASSERT_FALSE(android::base::ParseDouble("3.0", nullptr, -1.0, 2.0)); + ASSERT_TRUE(android::base::ParseDouble("1.0", nullptr, 0.0, 2.0)); } |