match.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. //
  2. // Copyright 2017 The Abseil Authors.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // https://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. //
  16. // -----------------------------------------------------------------------------
  17. // File: match.h
  18. // -----------------------------------------------------------------------------
  19. //
  20. // This file contains simple utilities for performing string matching checks.
  21. // All of these function parameters are specified as `y_absl::string_view`,
  22. // meaning that these functions can accept `TString`, `y_absl::string_view` or
  23. // NUL-terminated C-style strings.
  24. //
  25. // Examples:
  26. // TString s = "foo";
  27. // y_absl::string_view sv = "f";
  28. // assert(y_absl::StrContains(s, sv));
  29. //
  30. // Note: The order of parameters in these functions is designed to mimic the
  31. // order an equivalent member function would exhibit;
  32. // e.g. `s.Contains(x)` ==> `y_absl::StrContains(s, x).
  33. #ifndef Y_ABSL_STRINGS_MATCH_H_
  34. #define Y_ABSL_STRINGS_MATCH_H_
  35. #include <cstring>
  36. #include "y_absl/strings/string_view.h"
  37. namespace y_absl {
  38. Y_ABSL_NAMESPACE_BEGIN
  39. // StrContains()
  40. //
  41. // Returns whether a given string `haystack` contains the substring `needle`.
  42. inline bool StrContains(y_absl::string_view haystack,
  43. y_absl::string_view needle) noexcept {
  44. return haystack.find(needle, 0) != haystack.npos;
  45. }
  46. inline bool StrContains(y_absl::string_view haystack, char needle) noexcept {
  47. return haystack.find(needle) != haystack.npos;
  48. }
  49. // StartsWith()
  50. //
  51. // Returns whether a given string `text` begins with `prefix`.
  52. inline bool StartsWith(y_absl::string_view text,
  53. y_absl::string_view prefix) noexcept {
  54. return prefix.empty() ||
  55. (text.size() >= prefix.size() &&
  56. memcmp(text.data(), prefix.data(), prefix.size()) == 0);
  57. }
  58. // EndsWith()
  59. //
  60. // Returns whether a given string `text` ends with `suffix`.
  61. inline bool EndsWith(y_absl::string_view text,
  62. y_absl::string_view suffix) noexcept {
  63. return suffix.empty() ||
  64. (text.size() >= suffix.size() &&
  65. memcmp(text.data() + (text.size() - suffix.size()), suffix.data(),
  66. suffix.size()) == 0);
  67. }
  68. // StrContainsIgnoreCase()
  69. //
  70. // Returns whether a given ASCII string `haystack` contains the ASCII substring
  71. // `needle`, ignoring case in the comparison.
  72. bool StrContainsIgnoreCase(y_absl::string_view haystack,
  73. y_absl::string_view needle) noexcept;
  74. bool StrContainsIgnoreCase(y_absl::string_view haystack,
  75. char needle) noexcept;
  76. // EqualsIgnoreCase()
  77. //
  78. // Returns whether given ASCII strings `piece1` and `piece2` are equal, ignoring
  79. // case in the comparison.
  80. bool EqualsIgnoreCase(y_absl::string_view piece1,
  81. y_absl::string_view piece2) noexcept;
  82. // StartsWithIgnoreCase()
  83. //
  84. // Returns whether a given ASCII string `text` starts with `prefix`,
  85. // ignoring case in the comparison.
  86. bool StartsWithIgnoreCase(y_absl::string_view text,
  87. y_absl::string_view prefix) noexcept;
  88. // EndsWithIgnoreCase()
  89. //
  90. // Returns whether a given ASCII string `text` ends with `suffix`, ignoring
  91. // case in the comparison.
  92. bool EndsWithIgnoreCase(y_absl::string_view text,
  93. y_absl::string_view suffix) noexcept;
  94. // Yields the longest prefix in common between both input strings.
  95. // Pointer-wise, the returned result is a subset of input "a".
  96. y_absl::string_view FindLongestCommonPrefix(y_absl::string_view a,
  97. y_absl::string_view b);
  98. // Yields the longest suffix in common between both input strings.
  99. // Pointer-wise, the returned result is a subset of input "a".
  100. y_absl::string_view FindLongestCommonSuffix(y_absl::string_view a,
  101. y_absl::string_view b);
  102. Y_ABSL_NAMESPACE_END
  103. } // namespace y_absl
  104. #endif // Y_ABSL_STRINGS_MATCH_H_