strip.h 3.2 KB

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