match.cc 4.6 KB

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