diff options
author | Elliott Hughes <enh@google.com> | 2016-10-25 14:56:04 -0700 |
---|---|---|
committer | Elliott Hughes <enh@google.com> | 2016-10-25 14:56:04 -0700 |
commit | 42937492c8ca790676af6372e67971043de0711b (patch) | |
tree | a584c4bbb2e348dbdd4c56a1a42bec049565e278 | |
parent | 8cf0bd75f79ed641b5841dea1d34af18c6b4d40d (diff) | |
download | system_core-42937492c8ca790676af6372e67971043de0711b.tar.gz system_core-42937492c8ca790676af6372e67971043de0711b.tar.bz2 system_core-42937492c8ca790676af6372e67971043de0711b.zip |
Add StartsWithIgnoreCase/EndsWithIgnoreCase.
This has come up a couple of times now.
Bug: wanted as part of http://b/32094640
Test: ran tests
Change-Id: I51b67074b7ddeedd771d7be9651ba33e05491b33
-rw-r--r-- | base/include/android-base/strings.h | 2 | ||||
-rw-r--r-- | base/strings.cpp | 18 | ||||
-rw-r--r-- | base/strings_test.cpp | 85 |
3 files changed, 94 insertions, 11 deletions
diff --git a/base/include/android-base/strings.h b/base/include/android-base/strings.h index 69781cd40..b8a92896a 100644 --- a/base/include/android-base/strings.h +++ b/base/include/android-base/strings.h @@ -58,9 +58,11 @@ extern template std::string Join(const std::vector<const char*>&, const std::str // Tests whether 's' starts with 'prefix'. bool StartsWith(const std::string& s, const char* prefix); +bool StartsWithIgnoreCase(const std::string& s, const char* prefix); // Tests whether 's' ends with 'suffix'. bool EndsWith(const std::string& s, const char* suffix); +bool EndsWithIgnoreCase(const std::string& s, const char* suffix); } // namespace base } // namespace android diff --git a/base/strings.cpp b/base/strings.cpp index b8775df21..7a94ad78c 100644 --- a/base/strings.cpp +++ b/base/strings.cpp @@ -87,17 +87,29 @@ template std::string Join(const std::vector<std::string>&, const std::string&); template std::string Join(const std::vector<const char*>&, const std::string&); bool StartsWith(const std::string& s, const char* prefix) { - return s.compare(0, strlen(prefix), prefix) == 0; + return strncmp(s.c_str(), prefix, strlen(prefix)) == 0; } -bool EndsWith(const std::string& s, const char* suffix) { +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); size_t string_length = s.size(); if (suffix_length > string_length) { return false; } size_t offset = string_length - suffix_length; - return s.compare(offset, suffix_length, suffix) == 0; + return (case_sensitive ? strncmp : strncasecmp)(s.c_str() + offset, suffix, suffix_length) == 0; +} + +bool EndsWith(const std::string& s, const char* suffix) { + return EndsWith(s, suffix, true); +} + +bool EndsWithIgnoreCase(const std::string& s, const char* suffix) { + return EndsWith(s, suffix, false); } } // namespace base diff --git a/base/strings_test.cpp b/base/strings_test.cpp index 30ae29e71..5fb21dde6 100644 --- a/base/strings_test.cpp +++ b/base/strings_test.cpp @@ -134,44 +134,113 @@ TEST(strings, join_unordered_set) { "2,1" == android::base::Join(list, ',')); } -TEST(strings, startswith_empty) { +TEST(strings, StartsWith_empty) { ASSERT_FALSE(android::base::StartsWith("", "foo")); ASSERT_TRUE(android::base::StartsWith("", "")); } -TEST(strings, startswith_simple) { +TEST(strings, StartsWithIgnoreCase_empty) { + ASSERT_FALSE(android::base::StartsWithIgnoreCase("", "foo")); + ASSERT_TRUE(android::base::StartsWithIgnoreCase("", "")); +} + +TEST(strings, StartsWith_simple) { ASSERT_TRUE(android::base::StartsWith("foo", "")); ASSERT_TRUE(android::base::StartsWith("foo", "f")); ASSERT_TRUE(android::base::StartsWith("foo", "fo")); ASSERT_TRUE(android::base::StartsWith("foo", "foo")); } -TEST(strings, startswith_prefix_too_long) { +TEST(strings, StartsWithIgnoreCase_simple) { + ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "")); + ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "f")); + ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "F")); + ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "fo")); + ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "fO")); + ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "Fo")); + ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "FO")); + ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "foo")); + ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "foO")); + ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "fOo")); + ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "fOO")); + ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "Foo")); + ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "FoO")); + ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "FOo")); + ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "FOO")); +} + +TEST(strings, StartsWith_prefix_too_long) { ASSERT_FALSE(android::base::StartsWith("foo", "foobar")); } -TEST(strings, startswith_contains_prefix) { +TEST(strings, StartsWithIgnoreCase_prefix_too_long) { + ASSERT_FALSE(android::base::StartsWithIgnoreCase("foo", "foobar")); + ASSERT_FALSE(android::base::StartsWithIgnoreCase("foo", "FOOBAR")); +} + +TEST(strings, StartsWith_contains_prefix) { ASSERT_FALSE(android::base::StartsWith("foobar", "oba")); ASSERT_FALSE(android::base::StartsWith("foobar", "bar")); } -TEST(strings, endswith_empty) { +TEST(strings, StartsWithIgnoreCase_contains_prefix) { + ASSERT_FALSE(android::base::StartsWithIgnoreCase("foobar", "oba")); + ASSERT_FALSE(android::base::StartsWithIgnoreCase("foobar", "OBA")); + ASSERT_FALSE(android::base::StartsWithIgnoreCase("foobar", "bar")); + ASSERT_FALSE(android::base::StartsWithIgnoreCase("foobar", "BAR")); +} + +TEST(strings, EndsWith_empty) { ASSERT_FALSE(android::base::EndsWith("", "foo")); ASSERT_TRUE(android::base::EndsWith("", "")); } -TEST(strings, endswith_simple) { +TEST(strings, EndsWithIgnoreCase_empty) { + ASSERT_FALSE(android::base::EndsWithIgnoreCase("", "foo")); + ASSERT_FALSE(android::base::EndsWithIgnoreCase("", "FOO")); + ASSERT_TRUE(android::base::EndsWithIgnoreCase("", "")); +} + +TEST(strings, EndsWith_simple) { ASSERT_TRUE(android::base::EndsWith("foo", "")); ASSERT_TRUE(android::base::EndsWith("foo", "o")); ASSERT_TRUE(android::base::EndsWith("foo", "oo")); ASSERT_TRUE(android::base::EndsWith("foo", "foo")); } -TEST(strings, endswith_prefix_too_long) { +TEST(strings, EndsWithIgnoreCase_simple) { + ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "")); + ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "o")); + ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "O")); + ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "oo")); + ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "oO")); + ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "Oo")); + ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "OO")); + ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "foo")); + ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "foO")); + ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "fOo")); + ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "fOO")); + ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "Foo")); + ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "FoO")); + ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "FOo")); + ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "FOO")); +} + +TEST(strings, EndsWith_prefix_too_long) { ASSERT_FALSE(android::base::EndsWith("foo", "foobar")); } -TEST(strings, endswith_contains_prefix) { +TEST(strings, EndsWithIgnoreCase_prefix_too_long) { + ASSERT_FALSE(android::base::EndsWithIgnoreCase("foo", "foobar")); + ASSERT_FALSE(android::base::EndsWithIgnoreCase("foo", "FOOBAR")); +} + +TEST(strings, EndsWith_contains_prefix) { ASSERT_FALSE(android::base::EndsWith("foobar", "oba")); ASSERT_FALSE(android::base::EndsWith("foobar", "foo")); } + +TEST(strings, EndsWithIgnoreCase_contains_prefix) { + ASSERT_FALSE(android::base::EndsWithIgnoreCase("foobar", "OBA")); + ASSERT_FALSE(android::base::EndsWithIgnoreCase("foobar", "FOO")); +} |