Loading adb/socket_spec.cpp +4 −4 Original line number Diff line number Diff line Loading @@ -118,7 +118,7 @@ static bool tcp_host_is_local(const std::string& hostname) { bool is_socket_spec(const std::string& spec) { for (const auto& it : kLocalSocketTypes) { std::string prefix = it.first + ":"; if (StartsWith(spec, prefix.c_str())) { if (StartsWith(spec, prefix)) { return true; } } Loading @@ -128,7 +128,7 @@ bool is_socket_spec(const std::string& spec) { bool is_local_socket_spec(const std::string& spec) { for (const auto& it : kLocalSocketTypes) { std::string prefix = it.first + ":"; if (StartsWith(spec, prefix.c_str())) { if (StartsWith(spec, prefix)) { return true; } } Loading Loading @@ -170,7 +170,7 @@ int socket_spec_connect(const std::string& spec, std::string* error) { for (const auto& it : kLocalSocketTypes) { std::string prefix = it.first + ":"; if (StartsWith(spec, prefix.c_str())) { if (StartsWith(spec, prefix)) { if (!it.second.available) { *error = StringPrintf("socket type %s is unavailable on this platform", it.first.c_str()); Loading Loading @@ -213,7 +213,7 @@ int socket_spec_listen(const std::string& spec, std::string* error, int* resolve for (const auto& it : kLocalSocketTypes) { std::string prefix = it.first + ":"; if (StartsWith(spec, prefix.c_str())) { if (StartsWith(spec, prefix)) { if (!it.second.available) { *error = StringPrintf("attempted to listen on unavailable socket type: '%s'", spec.c_str()); Loading base/include/android-base/strings.h +6 −0 Original line number Diff line number Diff line Loading @@ -57,12 +57,18 @@ extern template std::string Join(const std::vector<std::string>&, const std::str extern template std::string Join(const std::vector<const char*>&, const std::string&); // Tests whether 's' starts with 'prefix'. // TODO: string_view bool StartsWith(const std::string& s, const char* prefix); bool StartsWithIgnoreCase(const std::string& s, const char* prefix); bool StartsWith(const std::string& s, const std::string& prefix); bool StartsWithIgnoreCase(const std::string& s, const std::string& prefix); // Tests whether 's' ends with 'suffix'. // TODO: string_view bool EndsWith(const std::string& s, const char* suffix); bool EndsWithIgnoreCase(const std::string& s, const char* suffix); bool EndsWith(const std::string& s, const std::string& prefix); bool EndsWithIgnoreCase(const std::string& s, const std::string& prefix); // Tests whether 'lhs' equals 'rhs', ignoring case. bool EqualsIgnoreCase(const std::string& lhs, const std::string& rhs); Loading base/strings.cpp +20 −4 Original line number Diff line number Diff line Loading @@ -91,12 +91,20 @@ bool StartsWith(const std::string& s, const char* prefix) { return strncmp(s.c_str(), prefix, strlen(prefix)) == 0; } bool StartsWith(const std::string& s, const std::string& prefix) { return strncmp(s.c_str(), prefix.c_str(), prefix.size()) == 0; } bool StartsWithIgnoreCase(const std::string& s, const char* prefix) { return strncasecmp(s.c_str(), prefix, strlen(prefix)) == 0; } static bool EndsWith(const std::string& s, const char* suffix, bool case_sensitive) { size_t suffix_length = strlen(suffix); bool StartsWithIgnoreCase(const std::string& s, const std::string& prefix) { return strncasecmp(s.c_str(), prefix.c_str(), prefix.size()) == 0; } static bool EndsWith(const std::string& s, const char* suffix, size_t suffix_length, bool case_sensitive) { size_t string_length = s.size(); if (suffix_length > string_length) { return false; Loading @@ -106,11 +114,19 @@ static bool EndsWith(const std::string& s, const char* suffix, bool case_sensiti } bool EndsWith(const std::string& s, const char* suffix) { return EndsWith(s, suffix, true); return EndsWith(s, suffix, strlen(suffix), true); } bool EndsWith(const std::string& s, const std::string& suffix) { return EndsWith(s, suffix.c_str(), suffix.size(), true); } bool EndsWithIgnoreCase(const std::string& s, const char* suffix) { return EndsWith(s, suffix, false); return EndsWith(s, suffix, strlen(suffix), false); } bool EndsWithIgnoreCase(const std::string& s, const std::string& suffix) { return EndsWith(s, suffix.c_str(), suffix.size(), false); } bool EqualsIgnoreCase(const std::string& lhs, const std::string& rhs) { Loading base/strings_test.cpp +20 −0 Original line number Diff line number Diff line Loading @@ -253,6 +253,26 @@ TEST(strings, EndsWithIgnoreCase_contains_prefix) { ASSERT_FALSE(android::base::EndsWithIgnoreCase("foobar", "FOO")); } TEST(strings, StartsWith_std_string) { ASSERT_TRUE(android::base::StartsWith("hello", std::string{"hell"})); ASSERT_FALSE(android::base::StartsWith("goodbye", std::string{"hell"})); } TEST(strings, StartsWithIgnoreCase_std_string) { ASSERT_TRUE(android::base::StartsWithIgnoreCase("HeLlO", std::string{"hell"})); ASSERT_FALSE(android::base::StartsWithIgnoreCase("GoOdByE", std::string{"hell"})); } TEST(strings, EndsWith_std_string) { ASSERT_TRUE(android::base::EndsWith("hello", std::string{"lo"})); ASSERT_FALSE(android::base::EndsWith("goodbye", std::string{"lo"})); } TEST(strings, EndsWithIgnoreCase_std_string) { ASSERT_TRUE(android::base::EndsWithIgnoreCase("HeLlO", std::string{"lo"})); ASSERT_FALSE(android::base::EndsWithIgnoreCase("GoOdByE", std::string{"lo"})); } TEST(strings, EqualsIgnoreCase) { ASSERT_TRUE(android::base::EqualsIgnoreCase("foo", "FOO")); ASSERT_TRUE(android::base::EqualsIgnoreCase("FOO", "foo")); Loading bootstat/bootstat.cpp +3 −6 Original line number Diff line number Diff line Loading @@ -315,8 +315,7 @@ bool isStrongRebootReason(const std::string& r) { for (auto& s : knownReasons) { if (s == "cold") break; // Prefix defined as terminated by a nul or comma (,). if (android::base::StartsWith(r, s.c_str()) && ((r.length() == s.length()) || (r[s.length()] == ','))) { if (android::base::StartsWith(r, s) && ((r.length() == s.length()) || (r[s.length()] == ','))) { return true; } } Loading @@ -328,8 +327,7 @@ bool isKernelRebootReason(const std::string& r) { for (auto& s : knownReasons) { if (s == "recovery") break; // Prefix defined as terminated by a nul or comma (,). if (android::base::StartsWith(r, s.c_str()) && ((r.length() == s.length()) || (r[s.length()] == ','))) { if (android::base::StartsWith(r, s) && ((r.length() == s.length()) || (r[s.length()] == ','))) { return true; } } Loading @@ -340,8 +338,7 @@ bool isKernelRebootReason(const std::string& r) { bool isKnownRebootReason(const std::string& r) { for (auto& s : knownReasons) { // Prefix defined as terminated by a nul or comma (,). if (android::base::StartsWith(r, s.c_str()) && ((r.length() == s.length()) || (r[s.length()] == ','))) { if (android::base::StartsWith(r, s) && ((r.length() == s.length()) || (r[s.length()] == ','))) { return true; } } Loading Loading
adb/socket_spec.cpp +4 −4 Original line number Diff line number Diff line Loading @@ -118,7 +118,7 @@ static bool tcp_host_is_local(const std::string& hostname) { bool is_socket_spec(const std::string& spec) { for (const auto& it : kLocalSocketTypes) { std::string prefix = it.first + ":"; if (StartsWith(spec, prefix.c_str())) { if (StartsWith(spec, prefix)) { return true; } } Loading @@ -128,7 +128,7 @@ bool is_socket_spec(const std::string& spec) { bool is_local_socket_spec(const std::string& spec) { for (const auto& it : kLocalSocketTypes) { std::string prefix = it.first + ":"; if (StartsWith(spec, prefix.c_str())) { if (StartsWith(spec, prefix)) { return true; } } Loading Loading @@ -170,7 +170,7 @@ int socket_spec_connect(const std::string& spec, std::string* error) { for (const auto& it : kLocalSocketTypes) { std::string prefix = it.first + ":"; if (StartsWith(spec, prefix.c_str())) { if (StartsWith(spec, prefix)) { if (!it.second.available) { *error = StringPrintf("socket type %s is unavailable on this platform", it.first.c_str()); Loading Loading @@ -213,7 +213,7 @@ int socket_spec_listen(const std::string& spec, std::string* error, int* resolve for (const auto& it : kLocalSocketTypes) { std::string prefix = it.first + ":"; if (StartsWith(spec, prefix.c_str())) { if (StartsWith(spec, prefix)) { if (!it.second.available) { *error = StringPrintf("attempted to listen on unavailable socket type: '%s'", spec.c_str()); Loading
base/include/android-base/strings.h +6 −0 Original line number Diff line number Diff line Loading @@ -57,12 +57,18 @@ extern template std::string Join(const std::vector<std::string>&, const std::str extern template std::string Join(const std::vector<const char*>&, const std::string&); // Tests whether 's' starts with 'prefix'. // TODO: string_view bool StartsWith(const std::string& s, const char* prefix); bool StartsWithIgnoreCase(const std::string& s, const char* prefix); bool StartsWith(const std::string& s, const std::string& prefix); bool StartsWithIgnoreCase(const std::string& s, const std::string& prefix); // Tests whether 's' ends with 'suffix'. // TODO: string_view bool EndsWith(const std::string& s, const char* suffix); bool EndsWithIgnoreCase(const std::string& s, const char* suffix); bool EndsWith(const std::string& s, const std::string& prefix); bool EndsWithIgnoreCase(const std::string& s, const std::string& prefix); // Tests whether 'lhs' equals 'rhs', ignoring case. bool EqualsIgnoreCase(const std::string& lhs, const std::string& rhs); Loading
base/strings.cpp +20 −4 Original line number Diff line number Diff line Loading @@ -91,12 +91,20 @@ bool StartsWith(const std::string& s, const char* prefix) { return strncmp(s.c_str(), prefix, strlen(prefix)) == 0; } bool StartsWith(const std::string& s, const std::string& prefix) { return strncmp(s.c_str(), prefix.c_str(), prefix.size()) == 0; } bool StartsWithIgnoreCase(const std::string& s, const char* prefix) { return strncasecmp(s.c_str(), prefix, strlen(prefix)) == 0; } static bool EndsWith(const std::string& s, const char* suffix, bool case_sensitive) { size_t suffix_length = strlen(suffix); bool StartsWithIgnoreCase(const std::string& s, const std::string& prefix) { return strncasecmp(s.c_str(), prefix.c_str(), prefix.size()) == 0; } static bool EndsWith(const std::string& s, const char* suffix, size_t suffix_length, bool case_sensitive) { size_t string_length = s.size(); if (suffix_length > string_length) { return false; Loading @@ -106,11 +114,19 @@ static bool EndsWith(const std::string& s, const char* suffix, bool case_sensiti } bool EndsWith(const std::string& s, const char* suffix) { return EndsWith(s, suffix, true); return EndsWith(s, suffix, strlen(suffix), true); } bool EndsWith(const std::string& s, const std::string& suffix) { return EndsWith(s, suffix.c_str(), suffix.size(), true); } bool EndsWithIgnoreCase(const std::string& s, const char* suffix) { return EndsWith(s, suffix, false); return EndsWith(s, suffix, strlen(suffix), false); } bool EndsWithIgnoreCase(const std::string& s, const std::string& suffix) { return EndsWith(s, suffix.c_str(), suffix.size(), false); } bool EqualsIgnoreCase(const std::string& lhs, const std::string& rhs) { Loading
base/strings_test.cpp +20 −0 Original line number Diff line number Diff line Loading @@ -253,6 +253,26 @@ TEST(strings, EndsWithIgnoreCase_contains_prefix) { ASSERT_FALSE(android::base::EndsWithIgnoreCase("foobar", "FOO")); } TEST(strings, StartsWith_std_string) { ASSERT_TRUE(android::base::StartsWith("hello", std::string{"hell"})); ASSERT_FALSE(android::base::StartsWith("goodbye", std::string{"hell"})); } TEST(strings, StartsWithIgnoreCase_std_string) { ASSERT_TRUE(android::base::StartsWithIgnoreCase("HeLlO", std::string{"hell"})); ASSERT_FALSE(android::base::StartsWithIgnoreCase("GoOdByE", std::string{"hell"})); } TEST(strings, EndsWith_std_string) { ASSERT_TRUE(android::base::EndsWith("hello", std::string{"lo"})); ASSERT_FALSE(android::base::EndsWith("goodbye", std::string{"lo"})); } TEST(strings, EndsWithIgnoreCase_std_string) { ASSERT_TRUE(android::base::EndsWithIgnoreCase("HeLlO", std::string{"lo"})); ASSERT_FALSE(android::base::EndsWithIgnoreCase("GoOdByE", std::string{"lo"})); } TEST(strings, EqualsIgnoreCase) { ASSERT_TRUE(android::base::EqualsIgnoreCase("foo", "FOO")); ASSERT_TRUE(android::base::EqualsIgnoreCase("FOO", "foo")); Loading
bootstat/bootstat.cpp +3 −6 Original line number Diff line number Diff line Loading @@ -315,8 +315,7 @@ bool isStrongRebootReason(const std::string& r) { for (auto& s : knownReasons) { if (s == "cold") break; // Prefix defined as terminated by a nul or comma (,). if (android::base::StartsWith(r, s.c_str()) && ((r.length() == s.length()) || (r[s.length()] == ','))) { if (android::base::StartsWith(r, s) && ((r.length() == s.length()) || (r[s.length()] == ','))) { return true; } } Loading @@ -328,8 +327,7 @@ bool isKernelRebootReason(const std::string& r) { for (auto& s : knownReasons) { if (s == "recovery") break; // Prefix defined as terminated by a nul or comma (,). if (android::base::StartsWith(r, s.c_str()) && ((r.length() == s.length()) || (r[s.length()] == ','))) { if (android::base::StartsWith(r, s) && ((r.length() == s.length()) || (r[s.length()] == ','))) { return true; } } Loading @@ -340,8 +338,7 @@ bool isKernelRebootReason(const std::string& r) { bool isKnownRebootReason(const std::string& r) { for (auto& s : knownReasons) { // Prefix defined as terminated by a nul or comma (,). if (android::base::StartsWith(r, s.c_str()) && ((r.length() == s.length()) || (r[s.length()] == ','))) { if (android::base::StartsWith(r, s) && ((r.length() == s.length()) || (r[s.length()] == ','))) { return true; } } Loading