match.cc 4.6 KB

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