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.

strip.h 3.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. {
  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::string_view* str, absl::string_view expected)
  44. {
  45. if (!absl::StartsWith(*str, expected))
  46. 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 bool ConsumeSuffix(absl::string_view* str, absl::string_view expected)
  62. {
  63. if (!absl::EndsWith(*str, expected))
  64. return false;
  65. str->remove_suffix(expected.size());
  66. return true;
  67. }
  68. // StripPrefix()
  69. //
  70. // Returns a view into the input string `str` with the given `prefix` removed,
  71. // but leaving the original string intact. If the prefix does not match at the
  72. // start of the string, returns the original string instead.
  73. ABSL_MUST_USE_RESULT inline absl::string_view StripPrefix(
  74. absl::string_view str, absl::string_view prefix
  75. )
  76. {
  77. if (absl::StartsWith(str, prefix))
  78. str.remove_prefix(prefix.size());
  79. return str;
  80. }
  81. // StripSuffix()
  82. //
  83. // Returns a view into the input string `str` with the given `suffix` removed,
  84. // but leaving the original string intact. If the suffix does not match at the
  85. // end of the string, returns the original string instead.
  86. ABSL_MUST_USE_RESULT inline absl::string_view StripSuffix(
  87. absl::string_view str, absl::string_view suffix
  88. )
  89. {
  90. if (absl::EndsWith(str, suffix))
  91. str.remove_suffix(suffix.size());
  92. return str;
  93. }
  94. ABSL_NAMESPACE_END
  95. } // namespace absl
  96. #endif // ABSL_STRINGS_STRIP_H_