Loading adb/bugreport.cpp +6 −3 Original line number Diff line number Diff line Loading @@ -21,6 +21,7 @@ #include <string> #include <vector> #include <android-base/parseint.h> #include <android-base/strings.h> #include "sysdeps.h" Loading Loading @@ -143,9 +144,11 @@ class BugreportStandardStreamsCallback : public StandardStreamsCallbackInterface // size_t idx1 = line.rfind(BUGZ_PROGRESS_PREFIX) + strlen(BUGZ_PROGRESS_PREFIX); size_t idx2 = line.rfind(BUGZ_PROGRESS_SEPARATOR); int progress = std::stoi(line.substr(idx1, (idx2 - idx1))); int total = std::stoi(line.substr(idx2 + 1)); int progress, total; if (android::base::ParseInt(line.substr(idx1, (idx2 - idx1)), &progress) && android::base::ParseInt(line.substr(idx2 + 1), &total)) { br_->UpdateProgress(line_message_, progress, total); } } else { invalid_lines_.push_back(line); } 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 Loading
adb/bugreport.cpp +6 −3 Original line number Diff line number Diff line Loading @@ -21,6 +21,7 @@ #include <string> #include <vector> #include <android-base/parseint.h> #include <android-base/strings.h> #include "sysdeps.h" Loading Loading @@ -143,9 +144,11 @@ class BugreportStandardStreamsCallback : public StandardStreamsCallbackInterface // size_t idx1 = line.rfind(BUGZ_PROGRESS_PREFIX) + strlen(BUGZ_PROGRESS_PREFIX); size_t idx2 = line.rfind(BUGZ_PROGRESS_SEPARATOR); int progress = std::stoi(line.substr(idx1, (idx2 - idx1))); int total = std::stoi(line.substr(idx2 + 1)); int progress, total; if (android::base::ParseInt(line.substr(idx1, (idx2 - idx1)), &progress) && android::base::ParseInt(line.substr(idx2 + 1), &total)) { br_->UpdateProgress(line_message_, progress, total); } } else { invalid_lines_.push_back(line); } 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