Loading base/include/android-base/parseint.h +19 −2 Original line number Diff line number Diff line Loading @@ -21,13 +21,15 @@ #include <stdlib.h> #include <limits> #include <string> namespace android { namespace base { // Parses the unsigned decimal 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. // 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()) { Loading @@ -45,10 +47,17 @@ bool ParseUint(const char* s, T* out, return true; } // TODO: string_view template <typename T> bool ParseUint(const std::string& s, T* out, T max = std::numeric_limits<T>::max()) { return ParseUint(s.c_str(), out, max); } // Parses the signed decimal 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. // success; 'out' is untouched if parsing fails. template <typename T> bool ParseInt(const char* s, T* out, T min = std::numeric_limits<T>::min(), Loading @@ -67,6 +76,14 @@ bool ParseInt(const char* s, T* out, return true; } // TODO: string_view template <typename T> bool ParseInt(const std::string& s, T* out, T min = std::numeric_limits<T>::min(), T max = std::numeric_limits<T>::max()) { return ParseInt(s.c_str(), out, min, max); } } // namespace base } // namespace android Loading base/parseint_test.cpp +28 −8 Original line number Diff line number Diff line Loading @@ -19,7 +19,7 @@ #include <gtest/gtest.h> TEST(parseint, signed_smoke) { int i; int i = 0; ASSERT_FALSE(android::base::ParseInt("x", &i)); ASSERT_FALSE(android::base::ParseInt("123x", &i)); Loading @@ -28,7 +28,7 @@ TEST(parseint, signed_smoke) { ASSERT_TRUE(android::base::ParseInt("-123", &i)); ASSERT_EQ(-123, i); short s; short s = 0; ASSERT_TRUE(android::base::ParseInt("1234", &s)); ASSERT_EQ(1234, s); Loading @@ -39,7 +39,7 @@ TEST(parseint, signed_smoke) { } TEST(parseint, unsigned_smoke) { unsigned int i; unsigned int i = 0u; ASSERT_FALSE(android::base::ParseUint("x", &i)); ASSERT_FALSE(android::base::ParseUint("123x", &i)); Loading @@ -47,7 +47,7 @@ TEST(parseint, unsigned_smoke) { ASSERT_EQ(123u, i); ASSERT_FALSE(android::base::ParseUint("-123", &i)); unsigned short s; unsigned short s = 0u; ASSERT_TRUE(android::base::ParseUint("1234", &s)); ASSERT_EQ(1234u, s); Loading @@ -58,21 +58,41 @@ TEST(parseint, unsigned_smoke) { } TEST(parseint, no_implicit_octal) { int i; int i = 0; ASSERT_TRUE(android::base::ParseInt("0123", &i)); ASSERT_EQ(123, i); unsigned int u; unsigned int u = 0u; ASSERT_TRUE(android::base::ParseUint("0123", &u)); ASSERT_EQ(123u, u); } TEST(parseint, explicit_hex) { int i; int i = 0; ASSERT_TRUE(android::base::ParseInt("0x123", &i)); ASSERT_EQ(0x123, i); unsigned int u; unsigned int u = 0u; ASSERT_TRUE(android::base::ParseUint("0x123", &u)); ASSERT_EQ(0x123u, u); } TEST(parseint, string) { int i = 0; ASSERT_TRUE(android::base::ParseInt(std::string("123"), &i)); ASSERT_EQ(123, i); unsigned int u = 0u; ASSERT_TRUE(android::base::ParseUint(std::string("123"), &u)); ASSERT_EQ(123u, u); } TEST(parseint, untouched_on_failure) { int i = 123; ASSERT_FALSE(android::base::ParseInt("456x", &i)); ASSERT_EQ(123, i); unsigned int u = 123u; ASSERT_FALSE(android::base::ParseInt("456x", &u)); ASSERT_EQ(123u, u); } base/properties.cpp +2 −2 Original line number Diff line number Diff line Loading @@ -52,7 +52,7 @@ template <typename T> T GetIntProperty(const std::string& key, T default_value, T min, T max) { T result; std::string value = GetProperty(key, ""); if (!value.empty() && android::base::ParseInt(value.c_str(), &result, min, max)) return result; if (!value.empty() && android::base::ParseInt(value, &result, min, max)) return result; return default_value; } Loading @@ -60,7 +60,7 @@ template <typename T> T GetUintProperty(const std::string& key, T default_value, T max) { T result; std::string value = GetProperty(key, ""); if (!value.empty() && android::base::ParseUint(value.c_str(), &result, max)) return result; if (!value.empty() && android::base::ParseUint(value, &result, max)) return result; return default_value; } Loading bootstat/boot_event_record_store.cpp +1 −1 Original line number Diff line number Diff line Loading @@ -59,7 +59,7 @@ bool ParseRecordEventTime(const std::string& path, int32_t* uptime) { // Ignore existing bootstat records (which do not contain file content). if (!content.empty()) { int32_t value; if (android::base::ParseInt(content.c_str(), &value)) { if (android::base::ParseInt(content, &value)) { bootstat::LogHistogram("bootstat_mtime_matches_content", value == *uptime); } } Loading bootstat/bootstat.cpp +2 −2 Original line number Diff line number Diff line Loading @@ -60,7 +60,7 @@ void RecordBootEventFromCommandLine( BootEventRecordStore boot_event_store; if (!value_str.empty()) { int32_t value = 0; if (android::base::ParseInt(value_str.c_str(), &value)) { if (android::base::ParseInt(value_str, &value)) { boot_event_store.AddBootEventWithValue(event, value); } } else { Loading Loading @@ -193,7 +193,7 @@ std::string CalculateBootCompletePrefix() { std::string build_date_str = GetProperty("ro.build.date.utc"); int32_t build_date; if (!android::base::ParseInt(build_date_str.c_str(), &build_date)) { if (!android::base::ParseInt(build_date_str, &build_date)) { return std::string(); } Loading Loading
base/include/android-base/parseint.h +19 −2 Original line number Diff line number Diff line Loading @@ -21,13 +21,15 @@ #include <stdlib.h> #include <limits> #include <string> namespace android { namespace base { // Parses the unsigned decimal 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. // 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()) { Loading @@ -45,10 +47,17 @@ bool ParseUint(const char* s, T* out, return true; } // TODO: string_view template <typename T> bool ParseUint(const std::string& s, T* out, T max = std::numeric_limits<T>::max()) { return ParseUint(s.c_str(), out, max); } // Parses the signed decimal 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. // success; 'out' is untouched if parsing fails. template <typename T> bool ParseInt(const char* s, T* out, T min = std::numeric_limits<T>::min(), Loading @@ -67,6 +76,14 @@ bool ParseInt(const char* s, T* out, return true; } // TODO: string_view template <typename T> bool ParseInt(const std::string& s, T* out, T min = std::numeric_limits<T>::min(), T max = std::numeric_limits<T>::max()) { return ParseInt(s.c_str(), out, min, max); } } // namespace base } // namespace android Loading
base/parseint_test.cpp +28 −8 Original line number Diff line number Diff line Loading @@ -19,7 +19,7 @@ #include <gtest/gtest.h> TEST(parseint, signed_smoke) { int i; int i = 0; ASSERT_FALSE(android::base::ParseInt("x", &i)); ASSERT_FALSE(android::base::ParseInt("123x", &i)); Loading @@ -28,7 +28,7 @@ TEST(parseint, signed_smoke) { ASSERT_TRUE(android::base::ParseInt("-123", &i)); ASSERT_EQ(-123, i); short s; short s = 0; ASSERT_TRUE(android::base::ParseInt("1234", &s)); ASSERT_EQ(1234, s); Loading @@ -39,7 +39,7 @@ TEST(parseint, signed_smoke) { } TEST(parseint, unsigned_smoke) { unsigned int i; unsigned int i = 0u; ASSERT_FALSE(android::base::ParseUint("x", &i)); ASSERT_FALSE(android::base::ParseUint("123x", &i)); Loading @@ -47,7 +47,7 @@ TEST(parseint, unsigned_smoke) { ASSERT_EQ(123u, i); ASSERT_FALSE(android::base::ParseUint("-123", &i)); unsigned short s; unsigned short s = 0u; ASSERT_TRUE(android::base::ParseUint("1234", &s)); ASSERT_EQ(1234u, s); Loading @@ -58,21 +58,41 @@ TEST(parseint, unsigned_smoke) { } TEST(parseint, no_implicit_octal) { int i; int i = 0; ASSERT_TRUE(android::base::ParseInt("0123", &i)); ASSERT_EQ(123, i); unsigned int u; unsigned int u = 0u; ASSERT_TRUE(android::base::ParseUint("0123", &u)); ASSERT_EQ(123u, u); } TEST(parseint, explicit_hex) { int i; int i = 0; ASSERT_TRUE(android::base::ParseInt("0x123", &i)); ASSERT_EQ(0x123, i); unsigned int u; unsigned int u = 0u; ASSERT_TRUE(android::base::ParseUint("0x123", &u)); ASSERT_EQ(0x123u, u); } TEST(parseint, string) { int i = 0; ASSERT_TRUE(android::base::ParseInt(std::string("123"), &i)); ASSERT_EQ(123, i); unsigned int u = 0u; ASSERT_TRUE(android::base::ParseUint(std::string("123"), &u)); ASSERT_EQ(123u, u); } TEST(parseint, untouched_on_failure) { int i = 123; ASSERT_FALSE(android::base::ParseInt("456x", &i)); ASSERT_EQ(123, i); unsigned int u = 123u; ASSERT_FALSE(android::base::ParseInt("456x", &u)); ASSERT_EQ(123u, u); }
base/properties.cpp +2 −2 Original line number Diff line number Diff line Loading @@ -52,7 +52,7 @@ template <typename T> T GetIntProperty(const std::string& key, T default_value, T min, T max) { T result; std::string value = GetProperty(key, ""); if (!value.empty() && android::base::ParseInt(value.c_str(), &result, min, max)) return result; if (!value.empty() && android::base::ParseInt(value, &result, min, max)) return result; return default_value; } Loading @@ -60,7 +60,7 @@ template <typename T> T GetUintProperty(const std::string& key, T default_value, T max) { T result; std::string value = GetProperty(key, ""); if (!value.empty() && android::base::ParseUint(value.c_str(), &result, max)) return result; if (!value.empty() && android::base::ParseUint(value, &result, max)) return result; return default_value; } Loading
bootstat/boot_event_record_store.cpp +1 −1 Original line number Diff line number Diff line Loading @@ -59,7 +59,7 @@ bool ParseRecordEventTime(const std::string& path, int32_t* uptime) { // Ignore existing bootstat records (which do not contain file content). if (!content.empty()) { int32_t value; if (android::base::ParseInt(content.c_str(), &value)) { if (android::base::ParseInt(content, &value)) { bootstat::LogHistogram("bootstat_mtime_matches_content", value == *uptime); } } Loading
bootstat/bootstat.cpp +2 −2 Original line number Diff line number Diff line Loading @@ -60,7 +60,7 @@ void RecordBootEventFromCommandLine( BootEventRecordStore boot_event_store; if (!value_str.empty()) { int32_t value = 0; if (android::base::ParseInt(value_str.c_str(), &value)) { if (android::base::ParseInt(value_str, &value)) { boot_event_store.AddBootEventWithValue(event, value); } } else { Loading Loading @@ -193,7 +193,7 @@ std::string CalculateBootCompletePrefix() { std::string build_date_str = GetProperty("ro.build.date.utc"); int32_t build_date; if (!android::base::ParseInt(build_date_str.c_str(), &build_date)) { if (!android::base::ParseInt(build_date_str, &build_date)) { return std::string(); } Loading