strip.h 3.0 KB

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