Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-16 09:41:02

0001 //
0002 // Copyright 2017 The Abseil Authors.
0003 //
0004 // Licensed under the Apache License, Version 2.0 (the "License");
0005 // you may not use this file except in compliance with the License.
0006 // You may obtain a copy of the License at
0007 //
0008 //      https://www.apache.org/licenses/LICENSE-2.0
0009 //
0010 // Unless required by applicable law or agreed to in writing, software
0011 // distributed under the License is distributed on an "AS IS" BASIS,
0012 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0013 // See the License for the specific language governing permissions and
0014 // limitations under the License.
0015 //
0016 // -----------------------------------------------------------------------------
0017 // File: match.h
0018 // -----------------------------------------------------------------------------
0019 //
0020 // This file contains simple utilities for performing string matching checks.
0021 // All of these function parameters are specified as `absl::string_view`,
0022 // meaning that these functions can accept `std::string`, `absl::string_view` or
0023 // NUL-terminated C-style strings.
0024 //
0025 // Examples:
0026 //   std::string s = "foo";
0027 //   absl::string_view sv = "f";
0028 //   assert(absl::StrContains(s, sv));
0029 //
0030 // Note: The order of parameters in these functions is designed to mimic the
0031 // order an equivalent member function would exhibit;
0032 // e.g. `s.Contains(x)` ==> `absl::StrContains(s, x).
0033 #ifndef ABSL_STRINGS_MATCH_H_
0034 #define ABSL_STRINGS_MATCH_H_
0035 
0036 #include <cstring>
0037 
0038 #include "absl/strings/string_view.h"
0039 
0040 namespace absl {
0041 ABSL_NAMESPACE_BEGIN
0042 
0043 // StrContains()
0044 //
0045 // Returns whether a given string `haystack` contains the substring `needle`.
0046 inline bool StrContains(absl::string_view haystack,
0047                         absl::string_view needle) noexcept {
0048   return haystack.find(needle, 0) != haystack.npos;
0049 }
0050 
0051 inline bool StrContains(absl::string_view haystack, char needle) noexcept {
0052   return haystack.find(needle) != haystack.npos;
0053 }
0054 
0055 // StartsWith()
0056 //
0057 // Returns whether a given string `text` begins with `prefix`.
0058 inline bool StartsWith(absl::string_view text,
0059                        absl::string_view prefix) noexcept {
0060   return prefix.empty() ||
0061          (text.size() >= prefix.size() &&
0062           memcmp(text.data(), prefix.data(), prefix.size()) == 0);
0063 }
0064 
0065 // EndsWith()
0066 //
0067 // Returns whether a given string `text` ends with `suffix`.
0068 inline bool EndsWith(absl::string_view text,
0069                      absl::string_view suffix) noexcept {
0070   return suffix.empty() ||
0071          (text.size() >= suffix.size() &&
0072           memcmp(text.data() + (text.size() - suffix.size()), suffix.data(),
0073                  suffix.size()) == 0);
0074 }
0075 // StrContainsIgnoreCase()
0076 //
0077 // Returns whether a given ASCII string `haystack` contains the ASCII substring
0078 // `needle`, ignoring case in the comparison.
0079 bool StrContainsIgnoreCase(absl::string_view haystack,
0080                            absl::string_view needle) noexcept;
0081 
0082 bool StrContainsIgnoreCase(absl::string_view haystack,
0083                            char needle) noexcept;
0084 
0085 // EqualsIgnoreCase()
0086 //
0087 // Returns whether given ASCII strings `piece1` and `piece2` are equal, ignoring
0088 // case in the comparison.
0089 bool EqualsIgnoreCase(absl::string_view piece1,
0090                       absl::string_view piece2) noexcept;
0091 
0092 // StartsWithIgnoreCase()
0093 //
0094 // Returns whether a given ASCII string `text` starts with `prefix`,
0095 // ignoring case in the comparison.
0096 bool StartsWithIgnoreCase(absl::string_view text,
0097                           absl::string_view prefix) noexcept;
0098 
0099 // EndsWithIgnoreCase()
0100 //
0101 // Returns whether a given ASCII string `text` ends with `suffix`, ignoring
0102 // case in the comparison.
0103 bool EndsWithIgnoreCase(absl::string_view text,
0104                         absl::string_view suffix) noexcept;
0105 
0106 // Yields the longest prefix in common between both input strings.
0107 // Pointer-wise, the returned result is a subset of input "a".
0108 absl::string_view FindLongestCommonPrefix(absl::string_view a,
0109                                           absl::string_view b);
0110 
0111 // Yields the longest suffix in common between both input strings.
0112 // Pointer-wise, the returned result is a subset of input "a".
0113 absl::string_view FindLongestCommonSuffix(absl::string_view a,
0114                                           absl::string_view b);
0115 
0116 ABSL_NAMESPACE_END
0117 }  // namespace absl
0118 
0119 #endif  // ABSL_STRINGS_MATCH_H_