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