|
||||
File indexing completed on 2024-11-15 09:01:17
0001 // 0002 // Copyright 2017 The Abseil Authors. 0003 // 0004 // Licensed under the Apache License, Version 2.0 (the "License"); 0005 // you may not use this file except in compliance with the License. 0006 // You may obtain a copy of the License at 0007 // 0008 // https://www.apache.org/licenses/LICENSE-2.0 0009 // 0010 // Unless required by applicable law or agreed to in writing, software 0011 // distributed under the License is distributed on an "AS IS" BASIS, 0012 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 0013 // See the License for the specific language governing permissions and 0014 // limitations under the License. 0015 // 0016 // ----------------------------------------------------------------------------- 0017 // File: strip.h 0018 // ----------------------------------------------------------------------------- 0019 // 0020 // This file contains various functions for stripping substrings from a string. 0021 #ifndef ABSL_STRINGS_STRIP_H_ 0022 #define ABSL_STRINGS_STRIP_H_ 0023 0024 #include <cstddef> 0025 #include <string> 0026 0027 #include "absl/base/macros.h" 0028 #include "absl/base/nullability.h" 0029 #include "absl/strings/ascii.h" 0030 #include "absl/strings/match.h" 0031 #include "absl/strings/string_view.h" 0032 0033 namespace absl { 0034 ABSL_NAMESPACE_BEGIN 0035 0036 // ConsumePrefix() 0037 // 0038 // Strips the `expected` prefix, if found, from the start of `str`. 0039 // If the operation succeeded, `true` is returned. If not, `false` 0040 // is returned and `str` is not modified. 0041 // 0042 // Example: 0043 // 0044 // absl::string_view input("abc"); 0045 // EXPECT_TRUE(absl::ConsumePrefix(&input, "a")); 0046 // EXPECT_EQ(input, "bc"); 0047 inline bool ConsumePrefix(absl::Nonnull<absl::string_view*> str, 0048 absl::string_view expected) { 0049 if (!absl::StartsWith(*str, expected)) return false; 0050 str->remove_prefix(expected.size()); 0051 return true; 0052 } 0053 // ConsumeSuffix() 0054 // 0055 // Strips the `expected` suffix, if found, from the end of `str`. 0056 // If the operation succeeded, `true` is returned. If not, `false` 0057 // is returned and `str` is not modified. 0058 // 0059 // Example: 0060 // 0061 // absl::string_view input("abcdef"); 0062 // EXPECT_TRUE(absl::ConsumeSuffix(&input, "def")); 0063 // EXPECT_EQ(input, "abc"); 0064 inline bool ConsumeSuffix(absl::Nonnull<absl::string_view*> str, 0065 absl::string_view expected) { 0066 if (!absl::EndsWith(*str, expected)) return false; 0067 str->remove_suffix(expected.size()); 0068 return true; 0069 } 0070 0071 // StripPrefix() 0072 // 0073 // Returns a view into the input string `str` with the given `prefix` removed, 0074 // but leaving the original string intact. If the prefix does not match at the 0075 // start of the string, returns the original string instead. 0076 ABSL_MUST_USE_RESULT inline absl::string_view StripPrefix( 0077 absl::string_view str, absl::string_view prefix) { 0078 if (absl::StartsWith(str, prefix)) str.remove_prefix(prefix.size()); 0079 return str; 0080 } 0081 0082 // StripSuffix() 0083 // 0084 // Returns a view into the input string `str` with the given `suffix` removed, 0085 // but leaving the original string intact. If the suffix does not match at the 0086 // end of the string, returns the original string instead. 0087 ABSL_MUST_USE_RESULT inline absl::string_view StripSuffix( 0088 absl::string_view str, absl::string_view suffix) { 0089 if (absl::EndsWith(str, suffix)) str.remove_suffix(suffix.size()); 0090 return str; 0091 } 0092 0093 ABSL_NAMESPACE_END 0094 } // namespace absl 0095 0096 #endif // ABSL_STRINGS_STRIP_H_
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |