strip.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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: strip.h
  18. // -----------------------------------------------------------------------------
  19. //
  20. // This file contains various functions for stripping substrings from a string.
  21. #ifndef ABSL_STRINGS_STRIP_H_
  22. #define ABSL_STRINGS_STRIP_H_
  23. #include <cstddef>
  24. #include <string>
  25. #include "absl/base/attributes.h"
  26. #include "absl/base/macros.h"
  27. #include "absl/base/nullability.h"
  28. #include "absl/strings/ascii.h"
  29. #include "absl/strings/match.h"
  30. #include "absl/strings/string_view.h"
  31. namespace absl {
  32. ABSL_NAMESPACE_BEGIN
  33. // ConsumePrefix()
  34. //
  35. // Strips the `expected` prefix, if found, from the start of `str`.
  36. // If the operation succeeded, `true` is returned. If not, `false`
  37. // is returned and `str` is not modified.
  38. //
  39. // Example:
  40. //
  41. // absl::string_view input("abc");
  42. // EXPECT_TRUE(absl::ConsumePrefix(&input, "a"));
  43. // EXPECT_EQ(input, "bc");
  44. inline constexpr bool ConsumePrefix(absl::Nonnull<absl::string_view*> str,
  45. absl::string_view expected) {
  46. if (!absl::StartsWith(*str, expected)) return false;
  47. str->remove_prefix(expected.size());
  48. return true;
  49. }
  50. // ConsumeSuffix()
  51. //
  52. // Strips the `expected` suffix, if found, from the end of `str`.
  53. // If the operation succeeded, `true` is returned. If not, `false`
  54. // is returned and `str` is not modified.
  55. //
  56. // Example:
  57. //
  58. // absl::string_view input("abcdef");
  59. // EXPECT_TRUE(absl::ConsumeSuffix(&input, "def"));
  60. // EXPECT_EQ(input, "abc");
  61. inline constexpr bool ConsumeSuffix(absl::Nonnull<absl::string_view*> str,
  62. absl::string_view expected) {
  63. if (!absl::EndsWith(*str, expected)) return false;
  64. str->remove_suffix(expected.size());
  65. return true;
  66. }
  67. // StripPrefix()
  68. //
  69. // Returns a view into the input string `str` with the given `prefix` removed,
  70. // but leaving the original string intact. If the prefix does not match at the
  71. // start of the string, returns the original string instead.
  72. ABSL_MUST_USE_RESULT inline constexpr absl::string_view StripPrefix(
  73. absl::string_view str ABSL_ATTRIBUTE_LIFETIME_BOUND,
  74. absl::string_view prefix) {
  75. if (absl::StartsWith(str, prefix)) str.remove_prefix(prefix.size());
  76. return str;
  77. }
  78. // StripSuffix()
  79. //
  80. // Returns a view into the input string `str` with the given `suffix` removed,
  81. // but leaving the original string intact. If the suffix does not match at the
  82. // end of the string, returns the original string instead.
  83. ABSL_MUST_USE_RESULT inline constexpr absl::string_view StripSuffix(
  84. absl::string_view str ABSL_ATTRIBUTE_LIFETIME_BOUND,
  85. absl::string_view suffix) {
  86. if (absl::EndsWith(str, suffix)) str.remove_suffix(suffix.size());
  87. return str;
  88. }
  89. ABSL_NAMESPACE_END
  90. } // namespace absl
  91. #endif // ABSL_STRINGS_STRIP_H_