summaryrefslogtreecommitdiffstats
path: root/base/include/android-base/parsedouble.h
diff options
context:
space:
mode:
Diffstat (limited to 'base/include/android-base/parsedouble.h')
-rw-r--r--base/include/android-base/parsedouble.h27
1 files changed, 22 insertions, 5 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