/src/abseil-cpp/absl/strings/match.cc
Line  | Count  | Source  | 
1  |  | // Copyright 2017 The Abseil Authors.  | 
2  |  | //  | 
3  |  | // Licensed under the Apache License, Version 2.0 (the "License");  | 
4  |  | // you may not use this file except in compliance with the License.  | 
5  |  | // You may obtain a copy of the License at  | 
6  |  | //  | 
7  |  | //      https://www.apache.org/licenses/LICENSE-2.0  | 
8  |  | //  | 
9  |  | // Unless required by applicable law or agreed to in writing, software  | 
10  |  | // distributed under the License is distributed on an "AS IS" BASIS,  | 
11  |  | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  | 
12  |  | // See the License for the specific language governing permissions and  | 
13  |  | // limitations under the License.  | 
14  |  |  | 
15  |  | #include "absl/strings/match.h"  | 
16  |  |  | 
17  |  | #include <algorithm>  | 
18  |  | #include <cstdint>  | 
19  |  |  | 
20  |  | #include "absl/base/config.h"  | 
21  |  | #include "absl/base/internal/endian.h"  | 
22  |  | #include "absl/base/optimization.h"  | 
23  |  | #include "absl/numeric/bits.h"  | 
24  |  | #include "absl/strings/ascii.h"  | 
25  |  | #include "absl/strings/internal/memutil.h"  | 
26  |  | #include "absl/strings/string_view.h"  | 
27  |  |  | 
28  |  | namespace absl { | 
29  |  | ABSL_NAMESPACE_BEGIN  | 
30  |  |  | 
31  |  | bool EqualsIgnoreCase(absl::string_view piece1,  | 
32  | 0  |                       absl::string_view piece2) noexcept { | 
33  | 0  |   return (piece1.size() == piece2.size() &&  | 
34  | 0  |           0 == absl::strings_internal::memcasecmp(piece1.data(), piece2.data(),  | 
35  | 0  |                                                   piece1.size()));  | 
36  |  |   // memcasecmp uses absl::ascii_tolower().  | 
37  | 0  | }  | 
38  |  |  | 
39  |  | bool StrContainsIgnoreCase(absl::string_view haystack,  | 
40  | 0  |                            absl::string_view needle) noexcept { | 
41  | 0  |   while (haystack.size() >= needle.size()) { | 
42  | 0  |     if (StartsWithIgnoreCase(haystack, needle)) return true;  | 
43  | 0  |     haystack.remove_prefix(1);  | 
44  | 0  |   }  | 
45  | 0  |   return false;  | 
46  | 0  | }  | 
47  |  |  | 
48  |  | bool StrContainsIgnoreCase(absl::string_view haystack,  | 
49  | 0  |                            char needle) noexcept { | 
50  | 0  |   char upper_needle = absl::ascii_toupper(static_cast<unsigned char>(needle));  | 
51  | 0  |   char lower_needle = absl::ascii_tolower(static_cast<unsigned char>(needle));  | 
52  | 0  |   if (upper_needle == lower_needle) { | 
53  | 0  |     return StrContains(haystack, needle);  | 
54  | 0  |   } else { | 
55  | 0  |     const char both_cstr[3] = {lower_needle, upper_needle, '\0'}; | 
56  | 0  |     return haystack.find_first_of(both_cstr) != absl::string_view::npos;  | 
57  | 0  |   }  | 
58  | 0  | }  | 
59  |  |  | 
60  |  | bool StartsWithIgnoreCase(absl::string_view text,  | 
61  | 0  |                           absl::string_view prefix) noexcept { | 
62  | 0  |   return (text.size() >= prefix.size()) &&  | 
63  | 0  |          EqualsIgnoreCase(text.substr(0, prefix.size()), prefix);  | 
64  | 0  | }  | 
65  |  |  | 
66  |  | bool EndsWithIgnoreCase(absl::string_view text,  | 
67  | 0  |                         absl::string_view suffix) noexcept { | 
68  | 0  |   return (text.size() >= suffix.size()) &&  | 
69  | 0  |          EqualsIgnoreCase(text.substr(text.size() - suffix.size()), suffix);  | 
70  | 0  | }  | 
71  |  |  | 
72  |  | absl::string_view FindLongestCommonPrefix(absl::string_view a,  | 
73  | 0  |                                           absl::string_view b) { | 
74  | 0  |   const absl::string_view::size_type limit = std::min(a.size(), b.size());  | 
75  | 0  |   const char* const pa = a.data();  | 
76  | 0  |   const char* const pb = b.data();  | 
77  | 0  |   absl::string_view::size_type count = (unsigned) 0;  | 
78  |  | 
  | 
79  | 0  |   if (ABSL_PREDICT_FALSE(limit < 8)) { | 
80  | 0  |     while (ABSL_PREDICT_TRUE(count + 2 <= limit)) { | 
81  | 0  |       uint16_t xor_bytes = absl::little_endian::Load16(pa + count) ^  | 
82  | 0  |                            absl::little_endian::Load16(pb + count);  | 
83  | 0  |       if (ABSL_PREDICT_FALSE(xor_bytes != 0)) { | 
84  | 0  |         if (ABSL_PREDICT_TRUE((xor_bytes & 0xff) == 0)) ++count;  | 
85  | 0  |         return absl::string_view(pa, count);  | 
86  | 0  |       }  | 
87  | 0  |       count += 2;  | 
88  | 0  |     }  | 
89  | 0  |     if (ABSL_PREDICT_TRUE(count != limit)) { | 
90  | 0  |       if (ABSL_PREDICT_TRUE(pa[count] == pb[count])) ++count;  | 
91  | 0  |     }  | 
92  | 0  |     return absl::string_view(pa, count);  | 
93  | 0  |   }  | 
94  |  |  | 
95  | 0  |   do { | 
96  | 0  |     uint64_t xor_bytes = absl::little_endian::Load64(pa + count) ^  | 
97  | 0  |                          absl::little_endian::Load64(pb + count);  | 
98  | 0  |     if (ABSL_PREDICT_FALSE(xor_bytes != 0)) { | 
99  | 0  |       count += static_cast<uint64_t>(absl::countr_zero(xor_bytes) >> 3);  | 
100  | 0  |       return absl::string_view(pa, count);  | 
101  | 0  |     }  | 
102  | 0  |     count += 8;  | 
103  | 0  |   } while (ABSL_PREDICT_TRUE(count + 8 < limit));  | 
104  |  |  | 
105  | 0  |   count = limit - 8;  | 
106  | 0  |   uint64_t xor_bytes = absl::little_endian::Load64(pa + count) ^  | 
107  | 0  |                        absl::little_endian::Load64(pb + count);  | 
108  | 0  |   if (ABSL_PREDICT_TRUE(xor_bytes != 0)) { | 
109  | 0  |     count += static_cast<uint64_t>(absl::countr_zero(xor_bytes) >> 3);  | 
110  | 0  |     return absl::string_view(pa, count);  | 
111  | 0  |   }  | 
112  | 0  |   return absl::string_view(pa, limit);  | 
113  | 0  | }  | 
114  |  |  | 
115  |  | absl::string_view FindLongestCommonSuffix(absl::string_view a,  | 
116  | 0  |                                           absl::string_view b) { | 
117  | 0  |   const absl::string_view::size_type limit = std::min(a.size(), b.size());  | 
118  | 0  |   if (limit == 0) return absl::string_view();  | 
119  |  |  | 
120  | 0  |   const char* pa = a.data() + a.size() - 1;  | 
121  | 0  |   const char* pb = b.data() + b.size() - 1;  | 
122  | 0  |   absl::string_view::size_type count = (unsigned) 0;  | 
123  | 0  |   while (count < limit && *pa == *pb) { | 
124  | 0  |     --pa;  | 
125  | 0  |     --pb;  | 
126  | 0  |     ++count;  | 
127  | 0  |   }  | 
128  |  | 
  | 
129  | 0  |   return absl::string_view(++pa, count);  | 
130  | 0  | }  | 
131  |  |  | 
132  |  | ABSL_NAMESPACE_END  | 
133  |  | }  // namespace absl  |