strip.h 3.1 KB

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