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/internal/endian.h"
21 #include "absl/numeric/bits.h"
22 #include "absl/strings/ascii.h"
23 #include "absl/strings/internal/memutil.h"
24
25 namespace absl {
26 ABSL_NAMESPACE_BEGIN
27
EqualsIgnoreCase(absl::string_view piece1,absl::string_view piece2)28 bool EqualsIgnoreCase(absl::string_view piece1,
29 absl::string_view piece2) noexcept {
30 return (piece1.size() == piece2.size() &&
31 0 == absl::strings_internal::memcasecmp(piece1.data(), piece2.data(),
32 piece1.size()));
33 // memcasecmp uses absl::ascii_tolower().
34 }
35
StrContainsIgnoreCase(absl::string_view haystack,absl::string_view needle)36 bool StrContainsIgnoreCase(absl::string_view haystack,
37 absl::string_view needle) noexcept {
38 while (haystack.size() >= needle.size()) {
39 if (StartsWithIgnoreCase(haystack, needle)) return true;
40 haystack.remove_prefix(1);
41 }
42 return false;
43 }
44
StrContainsIgnoreCase(absl::string_view haystack,char needle)45 bool StrContainsIgnoreCase(absl::string_view haystack,
46 char needle) noexcept {
47 char upper_needle = absl::ascii_toupper(static_cast<unsigned char>(needle));
48 char lower_needle = absl::ascii_tolower(static_cast<unsigned char>(needle));
49 if (upper_needle == lower_needle) {
50 return StrContains(haystack, needle);
51 } else {
52 const char both_cstr[3] = {lower_needle, upper_needle, '\0'};
53 return haystack.find_first_of(both_cstr) != absl::string_view::npos;
54 }
55 }
56
StartsWithIgnoreCase(absl::string_view text,absl::string_view prefix)57 bool StartsWithIgnoreCase(absl::string_view text,
58 absl::string_view prefix) noexcept {
59 return (text.size() >= prefix.size()) &&
60 EqualsIgnoreCase(text.substr(0, prefix.size()), prefix);
61 }
62
EndsWithIgnoreCase(absl::string_view text,absl::string_view suffix)63 bool EndsWithIgnoreCase(absl::string_view text,
64 absl::string_view suffix) noexcept {
65 return (text.size() >= suffix.size()) &&
66 EqualsIgnoreCase(text.substr(text.size() - suffix.size()), suffix);
67 }
68
FindLongestCommonPrefix(absl::string_view a,absl::string_view b)69 absl::string_view FindLongestCommonPrefix(absl::string_view a,
70 absl::string_view b) {
71 const absl::string_view::size_type limit = std::min(a.size(), b.size());
72 const char* const pa = a.data();
73 const char* const pb = b.data();
74 absl::string_view::size_type count = (unsigned) 0;
75
76 if (ABSL_PREDICT_FALSE(limit < 8)) {
77 while (ABSL_PREDICT_TRUE(count + 2 <= limit)) {
78 uint16_t xor_bytes = absl::little_endian::Load16(pa + count) ^
79 absl::little_endian::Load16(pb + count);
80 if (ABSL_PREDICT_FALSE(xor_bytes != 0)) {
81 if (ABSL_PREDICT_TRUE((xor_bytes & 0xff) == 0)) ++count;
82 return absl::string_view(pa, count);
83 }
84 count += 2;
85 }
86 if (ABSL_PREDICT_TRUE(count != limit)) {
87 if (ABSL_PREDICT_TRUE(pa[count] == pb[count])) ++count;
88 }
89 return absl::string_view(pa, count);
90 }
91
92 do {
93 uint64_t xor_bytes = absl::little_endian::Load64(pa + count) ^
94 absl::little_endian::Load64(pb + count);
95 if (ABSL_PREDICT_FALSE(xor_bytes != 0)) {
96 count += static_cast<uint64_t>(absl::countr_zero(xor_bytes) >> 3);
97 return absl::string_view(pa, count);
98 }
99 count += 8;
100 } while (ABSL_PREDICT_TRUE(count + 8 < limit));
101
102 count = limit - 8;
103 uint64_t xor_bytes = absl::little_endian::Load64(pa + count) ^
104 absl::little_endian::Load64(pb + count);
105 if (ABSL_PREDICT_TRUE(xor_bytes != 0)) {
106 count += static_cast<uint64_t>(absl::countr_zero(xor_bytes) >> 3);
107 return absl::string_view(pa, count);
108 }
109 return absl::string_view(pa, limit);
110 }
111
FindLongestCommonSuffix(absl::string_view a,absl::string_view b)112 absl::string_view FindLongestCommonSuffix(absl::string_view a,
113 absl::string_view b) {
114 const absl::string_view::size_type limit = std::min(a.size(), b.size());
115 if (limit == 0) return absl::string_view();
116
117 const char* pa = a.data() + a.size() - 1;
118 const char* pb = b.data() + b.size() - 1;
119 absl::string_view::size_type count = (unsigned) 0;
120 while (count < limit && *pa == *pb) {
121 --pa;
122 --pb;
123 ++count;
124 }
125
126 return absl::string_view(++pa, count);
127 }
128
129 ABSL_NAMESPACE_END
130 } // namespace absl
131