diff options
Diffstat (limited to 'absl/strings')
-rw-r--r-- | absl/strings/match.cc | 7 | ||||
-rw-r--r-- | absl/strings/match.h | 14 | ||||
-rw-r--r-- | absl/strings/match_test.cc | 11 |
3 files changed, 28 insertions, 4 deletions
diff --git a/absl/strings/match.cc b/absl/strings/match.cc index 25bd7f0b828b..3d10c57784eb 100644 --- a/absl/strings/match.cc +++ b/absl/strings/match.cc @@ -27,6 +27,13 @@ bool CaseEqual(absl::string_view piece1, absl::string_view piece2) { } } // namespace +bool EqualsIgnoreCase(absl::string_view piece1, absl::string_view piece2) { + return (piece1.size() == piece2.size() && + 0 == absl::strings_internal::memcasecmp(piece1.data(), piece2.data(), + piece1.size())); + // memcasecmp uses absl::ascii_tolower(). +} + bool StartsWithIgnoreCase(absl::string_view text, absl::string_view prefix) { return (text.size() >= prefix.size()) && CaseEqual(text.substr(0, prefix.size()), prefix); diff --git a/absl/strings/match.h b/absl/strings/match.h index 942a196b8853..6e8ed10fc50f 100644 --- a/absl/strings/match.h +++ b/absl/strings/match.h @@ -66,16 +66,22 @@ inline bool EndsWith(absl::string_view text, absl::string_view suffix) { ); } -// StartsWithIgnoreCase() +// EqualsIgnoreCase() // -// Returns whether a given string `text` starts with `starts_with`, ignoring +// Returns whether given ASCII strings `piece1` and `piece2` are equal, ignoring // case in the comparison. +bool EqualsIgnoreCase(absl::string_view piece1, absl::string_view piece2); + +// StartsWithIgnoreCase() +// +// Returns whether a given ASCII string `text` starts with `starts_with`, +// ignoring case in the comparison. bool StartsWithIgnoreCase(absl::string_view text, absl::string_view prefix); // EndsWithIgnoreCase() // -// Returns whether a given string `text` ends with `ends_with`, ignoring case -// in the comparison. +// Returns whether a given ASCII string `text` ends with `ends_with`, ignoring +// case in the comparison. bool EndsWithIgnoreCase(absl::string_view text, absl::string_view suffix); } // namespace absl diff --git a/absl/strings/match_test.cc b/absl/strings/match_test.cc index d194f0e689bf..c21e00bf807c 100644 --- a/absl/strings/match_test.cc +++ b/absl/strings/match_test.cc @@ -80,6 +80,17 @@ TEST(MatchTest, ContainsNull) { EXPECT_FALSE(absl::StrContains(cs, sv2)); } +TEST(MatchTest, EqualsIgnoreCase) { + std::string text = "the"; + absl::string_view data(text); + + EXPECT_TRUE(absl::EqualsIgnoreCase(data, "The")); + EXPECT_TRUE(absl::EqualsIgnoreCase(data, "THE")); + EXPECT_TRUE(absl::EqualsIgnoreCase(data, "the")); + EXPECT_FALSE(absl::EqualsIgnoreCase(data, "Quick")); + EXPECT_FALSE(absl::EqualsIgnoreCase(data, "then")); +} + TEST(MatchTest, StartsWithIgnoreCase) { EXPECT_TRUE(absl::StartsWithIgnoreCase("foo", "foo")); EXPECT_TRUE(absl::StartsWithIgnoreCase("foo", "Fo")); |