00001 // 00002 // Copyright 2017 The Abseil Authors. 00003 // 00004 // Licensed under the Apache License, Version 2.0 (the "License"); 00005 // you may not use this file except in compliance with the License. 00006 // You may obtain a copy of the License at 00007 // 00008 // https://www.apache.org/licenses/LICENSE-2.0 00009 // 00010 // Unless required by applicable law or agreed to in writing, software 00011 // distributed under the License is distributed on an "AS IS" BASIS, 00012 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 // See the License for the specific language governing permissions and 00014 // limitations under the License. 00015 // 00016 // ----------------------------------------------------------------------------- 00017 // File: match.h 00018 // ----------------------------------------------------------------------------- 00019 // 00020 // This file contains simple utilities for performing string matching checks. 00021 // All of these function parameters are specified as `absl::string_view`, 00022 // meaning that these functions can accept `std::string`, `absl::string_view` or 00023 // nul-terminated C-style strings. 00024 // 00025 // Examples: 00026 // std::string s = "foo"; 00027 // absl::string_view sv = "f"; 00028 // assert(absl::StrContains(s, sv)); 00029 // 00030 // Note: The order of parameters in these functions is designed to mimic the 00031 // order an equivalent member function would exhibit; 00032 // e.g. `s.Contains(x)` ==> `absl::StrContains(s, x). 00033 #ifndef ABSL_STRINGS_MATCH_H_ 00034 #define ABSL_STRINGS_MATCH_H_ 00035 00036 #include <cstring> 00037 00038 #include "absl/strings/string_view.h" 00039 00040 namespace absl { 00041 00042 // StrContains() 00043 // 00044 // Returns whether a given string `haystack` contains the substring `needle`. 00045 inline bool StrContains(absl::string_view haystack, absl::string_view needle) { 00046 return haystack.find(needle, 0) != haystack.npos; 00047 } 00048 00049 // StartsWith() 00050 // 00051 // Returns whether a given string `text` begins with `prefix`. 00052 inline bool StartsWith(absl::string_view text, absl::string_view prefix) { 00053 return prefix.empty() || 00054 (text.size() >= prefix.size() && 00055 memcmp(text.data(), prefix.data(), prefix.size()) == 0); 00056 } 00057 00058 // EndsWith() 00059 // 00060 // Returns whether a given string `text` ends with `suffix`. 00061 inline bool EndsWith(absl::string_view text, absl::string_view suffix) { 00062 return suffix.empty() || 00063 (text.size() >= suffix.size() && 00064 memcmp(text.data() + (text.size() - suffix.size()), suffix.data(), 00065 suffix.size()) == 0 00066 ); 00067 } 00068 00069 // EqualsIgnoreCase() 00070 // 00071 // Returns whether given ASCII strings `piece1` and `piece2` are equal, ignoring 00072 // case in the comparison. 00073 bool EqualsIgnoreCase(absl::string_view piece1, absl::string_view piece2); 00074 00075 // StartsWithIgnoreCase() 00076 // 00077 // Returns whether a given ASCII string `text` starts with `prefix`, 00078 // ignoring case in the comparison. 00079 bool StartsWithIgnoreCase(absl::string_view text, absl::string_view prefix); 00080 00081 // EndsWithIgnoreCase() 00082 // 00083 // Returns whether a given ASCII string `text` ends with `suffix`, ignoring 00084 // case in the comparison. 00085 bool EndsWithIgnoreCase(absl::string_view text, absl::string_view suffix); 00086 00087 } // namespace absl 00088 00089 #endif // ABSL_STRINGS_MATCH_H_