00001 // 00002 // Copyright 2017 The Abseil Authors. 00003 // 00004 // Licensed under the Apache License, Version 2.0 (the "License"); 00005 // you may not use this file except in compliance with the License. 00006 // You may obtain a copy of the License at 00007 // 00008 // https://www.apache.org/licenses/LICENSE-2.0 00009 // 00010 // Unless required by applicable law or agreed to in writing, software 00011 // distributed under the License is distributed on an "AS IS" BASIS, 00012 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 // See the License for the specific language governing permissions and 00014 // limitations under the License. 00015 // 00016 // ----------------------------------------------------------------------------- 00017 // File: strip.h 00018 // ----------------------------------------------------------------------------- 00019 // 00020 // This file contains various functions for stripping substrings from a string. 00021 #ifndef ABSL_STRINGS_STRIP_H_ 00022 #define ABSL_STRINGS_STRIP_H_ 00023 00024 #include <cstddef> 00025 #include <string> 00026 00027 #include "absl/base/macros.h" 00028 #include "absl/strings/ascii.h" 00029 #include "absl/strings/match.h" 00030 #include "absl/strings/string_view.h" 00031 00032 namespace absl { 00033 00034 // ConsumePrefix() 00035 // 00036 // Strips the `expected` prefix from the start of the given string, returning 00037 // `true` if the strip operation succeeded or false otherwise. 00038 // 00039 // Example: 00040 // 00041 // absl::string_view input("abc"); 00042 // EXPECT_TRUE(absl::ConsumePrefix(&input, "a")); 00043 // EXPECT_EQ(input, "bc"); 00044 inline bool ConsumePrefix(absl::string_view* str, absl::string_view expected) { 00045 if (!absl::StartsWith(*str, expected)) return false; 00046 str->remove_prefix(expected.size()); 00047 return true; 00048 } 00049 // ConsumeSuffix() 00050 // 00051 // Strips the `expected` suffix from the end of the given string, returning 00052 // `true` if the strip operation succeeded or false otherwise. 00053 // 00054 // Example: 00055 // 00056 // absl::string_view input("abcdef"); 00057 // EXPECT_TRUE(absl::ConsumeSuffix(&input, "def")); 00058 // EXPECT_EQ(input, "abc"); 00059 inline bool ConsumeSuffix(absl::string_view* str, absl::string_view expected) { 00060 if (!absl::EndsWith(*str, expected)) return false; 00061 str->remove_suffix(expected.size()); 00062 return true; 00063 } 00064 00065 // StripPrefix() 00066 // 00067 // Returns a view into the input string 'str' with the given 'prefix' removed, 00068 // but leaving the original string intact. If the prefix does not match at the 00069 // start of the string, returns the original string instead. 00070 ABSL_MUST_USE_RESULT inline absl::string_view StripPrefix( 00071 absl::string_view str, absl::string_view prefix) { 00072 if (absl::StartsWith(str, prefix)) str.remove_prefix(prefix.size()); 00073 return str; 00074 } 00075 00076 // StripSuffix() 00077 // 00078 // Returns a view into the input string 'str' with the given 'suffix' removed, 00079 // but leaving the original string intact. If the suffix does not match at the 00080 // end of the string, returns the original string instead. 00081 ABSL_MUST_USE_RESULT inline absl::string_view StripSuffix( 00082 absl::string_view str, absl::string_view suffix) { 00083 if (absl::EndsWith(str, suffix)) str.remove_suffix(suffix.size()); 00084 return str; 00085 } 00086 00087 } // namespace absl 00088 00089 #endif // ABSL_STRINGS_STRIP_H_