Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 1 | // |
| 2 | // Copyright 2017 The Abseil Authors. |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | // |
| 16 | // ----------------------------------------------------------------------------- |
| 17 | // File: match.h |
| 18 | // ----------------------------------------------------------------------------- |
| 19 | // |
| 20 | // This file contains simple utilities for performing string matching checks. |
| 21 | // All of these function parameters are specified as `absl::string_view`, |
| 22 | // meaning that these functions can accept `std::string`, `absl::string_view` or |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame] | 23 | // NUL-terminated C-style strings. |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 24 | // |
| 25 | // Examples: |
| 26 | // std::string s = "foo"; |
| 27 | // absl::string_view sv = "f"; |
| 28 | // assert(absl::StrContains(s, sv)); |
| 29 | // |
| 30 | // Note: The order of parameters in these functions is designed to mimic the |
| 31 | // order an equivalent member function would exhibit; |
| 32 | // e.g. `s.Contains(x)` ==> `absl::StrContains(s, x). |
| 33 | #ifndef ABSL_STRINGS_MATCH_H_ |
| 34 | #define ABSL_STRINGS_MATCH_H_ |
| 35 | |
| 36 | #include <cstring> |
| 37 | |
| 38 | #include "absl/strings/string_view.h" |
| 39 | |
| 40 | namespace absl { |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame] | 41 | ABSL_NAMESPACE_BEGIN |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 42 | |
| 43 | // StrContains() |
| 44 | // |
| 45 | // Returns whether a given string `haystack` contains the substring `needle`. |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame] | 46 | inline bool StrContains(absl::string_view haystack, |
| 47 | absl::string_view needle) noexcept { |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 48 | return haystack.find(needle, 0) != haystack.npos; |
| 49 | } |
| 50 | |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame] | 51 | inline bool StrContains(absl::string_view haystack, char needle) noexcept { |
| 52 | return haystack.find(needle) != haystack.npos; |
| 53 | } |
| 54 | |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 55 | // StartsWith() |
| 56 | // |
| 57 | // Returns whether a given string `text` begins with `prefix`. |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame] | 58 | inline bool StartsWith(absl::string_view text, |
| 59 | absl::string_view prefix) noexcept { |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 60 | return prefix.empty() || |
| 61 | (text.size() >= prefix.size() && |
| 62 | memcmp(text.data(), prefix.data(), prefix.size()) == 0); |
| 63 | } |
| 64 | |
| 65 | // EndsWith() |
| 66 | // |
| 67 | // Returns whether a given string `text` ends with `suffix`. |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame] | 68 | inline bool EndsWith(absl::string_view text, |
| 69 | absl::string_view suffix) noexcept { |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 70 | return suffix.empty() || |
| 71 | (text.size() >= suffix.size() && |
| 72 | memcmp(text.data() + (text.size() - suffix.size()), suffix.data(), |
| 73 | suffix.size()) == 0); |
| 74 | } |
| 75 | |
| 76 | // EqualsIgnoreCase() |
| 77 | // |
| 78 | // Returns whether given ASCII strings `piece1` and `piece2` are equal, ignoring |
| 79 | // case in the comparison. |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame] | 80 | bool EqualsIgnoreCase(absl::string_view piece1, |
| 81 | absl::string_view piece2) noexcept; |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 82 | |
| 83 | // StartsWithIgnoreCase() |
| 84 | // |
| 85 | // Returns whether a given ASCII string `text` starts with `prefix`, |
| 86 | // ignoring case in the comparison. |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame] | 87 | bool StartsWithIgnoreCase(absl::string_view text, |
| 88 | absl::string_view prefix) noexcept; |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 89 | |
| 90 | // EndsWithIgnoreCase() |
| 91 | // |
| 92 | // Returns whether a given ASCII string `text` ends with `suffix`, ignoring |
| 93 | // case in the comparison. |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame] | 94 | bool EndsWithIgnoreCase(absl::string_view text, |
| 95 | absl::string_view suffix) noexcept; |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 96 | |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame] | 97 | ABSL_NAMESPACE_END |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 98 | } // namespace absl |
| 99 | |
| 100 | #endif // ABSL_STRINGS_MATCH_H_ |