You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

match.h 3.5 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 `absl::string_view`,
  22. // meaning that these functions can accept `std::string`, `absl::string_view` or
  23. // NUL-terminated C-style strings.
  24. //
  25. // Examples:
  26. // std::string s = "foo";
  27. // absl::string_view sv = "f";
  28. // assert(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)` ==> `absl::StrContains(s, x).
  33. #ifndef ABSL_STRINGS_MATCH_H_
  34. #define ABSL_STRINGS_MATCH_H_
  35. #include <cstring>
  36. #include "absl/strings/string_view.h"
  37. namespace absl
  38. {
  39. ABSL_NAMESPACE_BEGIN
  40. // StrContains()
  41. //
  42. // Returns whether a given string `haystack` contains the substring `needle`.
  43. inline bool StrContains(absl::string_view haystack, absl::string_view needle) noexcept
  44. {
  45. return haystack.find(needle, 0) != haystack.npos;
  46. }
  47. inline bool StrContains(absl::string_view haystack, char needle) noexcept
  48. {
  49. return haystack.find(needle) != haystack.npos;
  50. }
  51. // StartsWith()
  52. //
  53. // Returns whether a given string `text` begins with `prefix`.
  54. inline bool StartsWith(absl::string_view text, absl::string_view prefix) noexcept
  55. {
  56. return prefix.empty() ||
  57. (text.size() >= prefix.size() &&
  58. memcmp(text.data(), prefix.data(), prefix.size()) == 0);
  59. }
  60. // EndsWith()
  61. //
  62. // Returns whether a given string `text` ends with `suffix`.
  63. inline bool EndsWith(absl::string_view text, absl::string_view suffix) noexcept
  64. {
  65. return suffix.empty() ||
  66. (text.size() >= suffix.size() &&
  67. memcmp(text.data() + (text.size() - suffix.size()), suffix.data(), suffix.size()) == 0);
  68. }
  69. // EqualsIgnoreCase()
  70. //
  71. // Returns whether given ASCII strings `piece1` and `piece2` are equal, ignoring
  72. // case in the comparison.
  73. bool EqualsIgnoreCase(absl::string_view piece1, absl::string_view piece2) noexcept;
  74. // StartsWithIgnoreCase()
  75. //
  76. // Returns whether a given ASCII string `text` starts with `prefix`,
  77. // ignoring case in the comparison.
  78. bool StartsWithIgnoreCase(absl::string_view text, absl::string_view prefix) noexcept;
  79. // EndsWithIgnoreCase()
  80. //
  81. // Returns whether a given ASCII string `text` ends with `suffix`, ignoring
  82. // case in the comparison.
  83. bool EndsWithIgnoreCase(absl::string_view text, absl::string_view suffix) noexcept;
  84. ABSL_NAMESPACE_END
  85. } // namespace absl
  86. #endif // ABSL_STRINGS_MATCH_H_