Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 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 | |
| 24 | #include <cstddef> |
| 25 | #include <string> |
| 26 | |
| 27 | #include "absl/base/macros.h" |
| 28 | #include "absl/strings/ascii.h" |
| 29 | #include "absl/strings/match.h" |
| 30 | #include "absl/strings/string_view.h" |
| 31 | |
| 32 | namespace absl { |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame] | 33 | ABSL_NAMESPACE_BEGIN |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 34 | |
| 35 | // ConsumePrefix() |
| 36 | // |
| 37 | // Strips the `expected` prefix from the start of the given string, returning |
| 38 | // `true` if the strip operation succeeded or false otherwise. |
| 39 | // |
| 40 | // Example: |
| 41 | // |
| 42 | // absl::string_view input("abc"); |
| 43 | // EXPECT_TRUE(absl::ConsumePrefix(&input, "a")); |
| 44 | // EXPECT_EQ(input, "bc"); |
| 45 | inline bool ConsumePrefix(absl::string_view* str, 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 from the end of the given string, returning |
| 53 | // `true` if the strip operation succeeded or false otherwise. |
| 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::string_view* str, absl::string_view expected) { |
| 61 | if (!absl::EndsWith(*str, expected)) return false; |
| 62 | str->remove_suffix(expected.size()); |
| 63 | return true; |
| 64 | } |
| 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 | |
| 77 | // StripSuffix() |
| 78 | // |
| 79 | // Returns a view into the input string 'str' with the given 'suffix' removed, |
| 80 | // but leaving the original string intact. If the suffix does not match at the |
| 81 | // end of the string, returns the original string instead. |
| 82 | ABSL_MUST_USE_RESULT inline absl::string_view StripSuffix( |
| 83 | absl::string_view str, absl::string_view suffix) { |
| 84 | if (absl::EndsWith(str, suffix)) str.remove_suffix(suffix.size()); |
| 85 | return str; |
| 86 | } |
| 87 | |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame] | 88 | ABSL_NAMESPACE_END |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 89 | } // namespace absl |
| 90 | |
| 91 | #endif // ABSL_STRINGS_STRIP_H_ |