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