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
This commit is contained in:
parent
8cf0bd75f7
commit
42937492c8
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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"));
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue