Loading base/include/android-base/strings.h +2 −0 Original line number Diff line number Diff line Loading @@ -61,6 +61,7 @@ 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); bool StartsWith(const std::string& s, char prefix); // Tests whether 's' ends with 'suffix'. // TODO: string_view Loading @@ -68,6 +69,7 @@ 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& suffix); bool EndsWithIgnoreCase(const std::string& s, const std::string& suffix); bool EndsWith(const std::string& s, char suffix); // Tests whether 'lhs' equals 'rhs', ignoring case. bool EqualsIgnoreCase(const std::string& lhs, const std::string& rhs); Loading base/strings.cpp +8 −0 Original line number Diff line number Diff line Loading @@ -95,6 +95,10 @@ bool StartsWith(const std::string& s, const std::string& prefix) { return strncmp(s.c_str(), prefix.c_str(), prefix.size()) == 0; } bool StartsWith(const std::string& s, char prefix) { return *s.c_str() == prefix; // Use c_str() to guarantee there is at least a '\0'. } bool StartsWithIgnoreCase(const std::string& s, const char* prefix) { return strncasecmp(s.c_str(), prefix, strlen(prefix)) == 0; } Loading @@ -121,6 +125,10 @@ bool EndsWith(const std::string& s, const std::string& suffix) { return EndsWith(s, suffix.c_str(), suffix.size(), true); } bool EndsWith(const std::string& s, char suffix) { return EndsWith(s, &suffix, 1, true); } bool EndsWithIgnoreCase(const std::string& s, const char* suffix) { return EndsWith(s, suffix, strlen(suffix), false); } Loading base/strings_test.cpp +12 −0 Original line number Diff line number Diff line Loading @@ -198,6 +198,12 @@ TEST(strings, StartsWithIgnoreCase_contains_prefix) { ASSERT_FALSE(android::base::StartsWithIgnoreCase("foobar", "BAR")); } TEST(strings, StartsWith_char) { ASSERT_FALSE(android::base::StartsWith("", 'f')); ASSERT_TRUE(android::base::StartsWith("foo", 'f')); ASSERT_FALSE(android::base::StartsWith("foo", 'o')); } TEST(strings, EndsWith_empty) { ASSERT_FALSE(android::base::EndsWith("", "foo")); ASSERT_TRUE(android::base::EndsWith("", "")); Loading Loading @@ -273,6 +279,12 @@ TEST(strings, EndsWithIgnoreCase_std_string) { ASSERT_FALSE(android::base::EndsWithIgnoreCase("GoOdByE", std::string{"lo"})); } TEST(strings, EndsWith_char) { ASSERT_FALSE(android::base::EndsWith("", 'o')); ASSERT_TRUE(android::base::EndsWith("foo", 'o')); ASSERT_FALSE(android::base::EndsWith("foo", "f")); } TEST(strings, EqualsIgnoreCase) { ASSERT_TRUE(android::base::EqualsIgnoreCase("foo", "FOO")); ASSERT_TRUE(android::base::EqualsIgnoreCase("FOO", "foo")); Loading Loading
base/include/android-base/strings.h +2 −0 Original line number Diff line number Diff line Loading @@ -61,6 +61,7 @@ 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); bool StartsWith(const std::string& s, char prefix); // Tests whether 's' ends with 'suffix'. // TODO: string_view Loading @@ -68,6 +69,7 @@ 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& suffix); bool EndsWithIgnoreCase(const std::string& s, const std::string& suffix); bool EndsWith(const std::string& s, char suffix); // Tests whether 'lhs' equals 'rhs', ignoring case. bool EqualsIgnoreCase(const std::string& lhs, const std::string& rhs); Loading
base/strings.cpp +8 −0 Original line number Diff line number Diff line Loading @@ -95,6 +95,10 @@ bool StartsWith(const std::string& s, const std::string& prefix) { return strncmp(s.c_str(), prefix.c_str(), prefix.size()) == 0; } bool StartsWith(const std::string& s, char prefix) { return *s.c_str() == prefix; // Use c_str() to guarantee there is at least a '\0'. } bool StartsWithIgnoreCase(const std::string& s, const char* prefix) { return strncasecmp(s.c_str(), prefix, strlen(prefix)) == 0; } Loading @@ -121,6 +125,10 @@ bool EndsWith(const std::string& s, const std::string& suffix) { return EndsWith(s, suffix.c_str(), suffix.size(), true); } bool EndsWith(const std::string& s, char suffix) { return EndsWith(s, &suffix, 1, true); } bool EndsWithIgnoreCase(const std::string& s, const char* suffix) { return EndsWith(s, suffix, strlen(suffix), false); } Loading
base/strings_test.cpp +12 −0 Original line number Diff line number Diff line Loading @@ -198,6 +198,12 @@ TEST(strings, StartsWithIgnoreCase_contains_prefix) { ASSERT_FALSE(android::base::StartsWithIgnoreCase("foobar", "BAR")); } TEST(strings, StartsWith_char) { ASSERT_FALSE(android::base::StartsWith("", 'f')); ASSERT_TRUE(android::base::StartsWith("foo", 'f')); ASSERT_FALSE(android::base::StartsWith("foo", 'o')); } TEST(strings, EndsWith_empty) { ASSERT_FALSE(android::base::EndsWith("", "foo")); ASSERT_TRUE(android::base::EndsWith("", "")); Loading Loading @@ -273,6 +279,12 @@ TEST(strings, EndsWithIgnoreCase_std_string) { ASSERT_FALSE(android::base::EndsWithIgnoreCase("GoOdByE", std::string{"lo"})); } TEST(strings, EndsWith_char) { ASSERT_FALSE(android::base::EndsWith("", 'o')); ASSERT_TRUE(android::base::EndsWith("foo", 'o')); ASSERT_FALSE(android::base::EndsWith("foo", "f")); } TEST(strings, EqualsIgnoreCase) { ASSERT_TRUE(android::base::EqualsIgnoreCase("foo", "FOO")); ASSERT_TRUE(android::base::EqualsIgnoreCase("FOO", "foo")); Loading