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