strip.h
Go to the documentation of this file.
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"
31 
32 namespace absl {
33 
34 // ConsumePrefix()
35 //
36 // Strips the `expected` prefix from the start of the given string, returning
37 // `true` if the strip operation succeeded or false otherwise.
38 //
39 // Example:
40 //
41 // absl::string_view input("abc");
42 // EXPECT_TRUE(absl::ConsumePrefix(&input, "a"));
43 // EXPECT_EQ(input, "bc");
44 inline bool ConsumePrefix(absl::string_view* str, absl::string_view expected) {
45  if (!absl::StartsWith(*str, expected)) return false;
46  str->remove_prefix(expected.size());
47  return true;
48 }
49 // ConsumeSuffix()
50 //
51 // Strips the `expected` suffix from the end of the given string, returning
52 // `true` if the strip operation succeeded or false otherwise.
53 //
54 // Example:
55 //
56 // absl::string_view input("abcdef");
57 // EXPECT_TRUE(absl::ConsumeSuffix(&input, "def"));
58 // EXPECT_EQ(input, "abc");
59 inline bool ConsumeSuffix(absl::string_view* str, absl::string_view expected) {
60  if (!absl::EndsWith(*str, expected)) return false;
61  str->remove_suffix(expected.size());
62  return true;
63 }
64 
65 // StripPrefix()
66 //
67 // Returns a view into the input string 'str' with the given 'prefix' removed,
68 // but leaving the original string intact. If the prefix does not match at the
69 // start of the string, returns the original string instead.
72  if (absl::StartsWith(str, prefix)) str.remove_prefix(prefix.size());
73  return str;
74 }
75 
76 // StripSuffix()
77 //
78 // Returns a view into the input string 'str' with the given 'suffix' removed,
79 // but leaving the original string intact. If the suffix does not match at the
80 // end of the string, returns the original string instead.
83  if (absl::EndsWith(str, suffix)) str.remove_suffix(suffix.size());
84  return str;
85 }
86 
87 } // namespace absl
88 
89 #endif // ABSL_STRINGS_STRIP_H_
void remove_prefix(size_type n)
Definition: string_view.h:310
ABSL_MUST_USE_RESULT absl::string_view StripPrefix(absl::string_view str, absl::string_view prefix)
Definition: strip.h:70
Definition: algorithm.h:29
void remove_suffix(size_type n)
Definition: string_view.h:320
constexpr size_type size() const noexcept
Definition: string_view.h:260
bool EndsWith(absl::string_view text, absl::string_view suffix)
Definition: match.h:61
bool ConsumePrefix(absl::string_view *str, absl::string_view expected)
Definition: strip.h:44
#define ABSL_MUST_USE_RESULT
Definition: attributes.h:449
ABSL_MUST_USE_RESULT absl::string_view StripSuffix(absl::string_view str, absl::string_view suffix)
Definition: strip.h:81
bool StartsWith(absl::string_view text, absl::string_view prefix)
Definition: match.h:52
bool ConsumeSuffix(absl::string_view *str, absl::string_view expected)
Definition: strip.h:59


abseil_cpp
Author(s):
autogenerated on Wed Jun 19 2019 19:19:58