diff options
author | Greg Falcon <gfalcon@google.com> | 2023-03-15 14:41:08 -0700 |
---|---|---|
committer | Copybara-Service <copybara-worker@google.com> | 2023-03-15 14:41:46 -0700 |
commit | ab0e3e8e1fe0d71c2675b9e8c5aa48f19a1f716a (patch) | |
tree | cc626bc95cc23664449900d5023b0fd2e0e2ac14 /absl/strings/match.cc | |
parent | a8f3b9d6119e8ff1e56a67661cf5a94f41c7866e (diff) |
Add StrContainsIgnoreCase() to strings/match.h; all the other case-sensitive methods in this file have corresponding case-insensitive ones.
PiperOrigin-RevId: 516933773
Change-Id: Iaec41afd923b10bc493ad864c0ecfe85a1fe2db8
Diffstat (limited to 'absl/strings/match.cc')
-rw-r--r-- | absl/strings/match.cc | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/absl/strings/match.cc b/absl/strings/match.cc index 2d672509..b65cbc67 100644 --- a/absl/strings/match.cc +++ b/absl/strings/match.cc @@ -13,6 +13,7 @@ // limitations under the License. #include "absl/strings/match.h" +#include "absl/strings/ascii.h" #include "absl/strings/internal/memutil.h" @@ -27,6 +28,27 @@ bool EqualsIgnoreCase(absl::string_view piece1, // memcasecmp uses absl::ascii_tolower(). } +bool StrContainsIgnoreCase(absl::string_view haystack, + absl::string_view needle) noexcept { + while (haystack.size() >= needle.size()) { + if (StartsWithIgnoreCase(haystack, needle)) return true; + haystack.remove_prefix(1); + } + return false; +} + +bool StrContainsIgnoreCase(absl::string_view haystack, + char needle) noexcept { + char upper_needle = absl::ascii_toupper(static_cast<unsigned char>(needle)); + char lower_needle = absl::ascii_tolower(static_cast<unsigned char>(needle)); + if (upper_needle == lower_needle) { + return StrContains(haystack, needle); + } else { + const char both_cstr[3] = {lower_needle, upper_needle, '\0'}; + return haystack.find_first_of(both_cstr) != absl::string_view::npos; + } +} + bool StartsWithIgnoreCase(absl::string_view text, absl::string_view prefix) noexcept { return (text.size() >= prefix.size()) && |