bloaty/third_party/abseil-cpp/absl/strings/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"
30 #include "absl/strings/string_view.h"
31 
32 namespace absl {
34 
35 // ConsumePrefix()
36 //
37 // Strips the `expected` prefix from the start of the given string, returning
38 // `true` if the strip operation succeeded or false otherwise.
39 //
40 // Example:
41 //
42 // absl::string_view input("abc");
43 // EXPECT_TRUE(absl::ConsumePrefix(&input, "a"));
44 // EXPECT_EQ(input, "bc");
45 inline bool ConsumePrefix(absl::string_view* str, absl::string_view expected) {
46  if (!absl::StartsWith(*str, expected)) return false;
47  str->remove_prefix(expected.size());
48  return true;
49 }
50 // ConsumeSuffix()
51 //
52 // Strips the `expected` suffix from the end of the given string, returning
53 // `true` if the strip operation succeeded or false otherwise.
54 //
55 // Example:
56 //
57 // absl::string_view input("abcdef");
58 // EXPECT_TRUE(absl::ConsumeSuffix(&input, "def"));
59 // EXPECT_EQ(input, "abc");
60 inline bool ConsumeSuffix(absl::string_view* str, absl::string_view expected) {
61  if (!absl::EndsWith(*str, expected)) return false;
62  str->remove_suffix(expected.size());
63  return true;
64 }
65 
66 // StripPrefix()
67 //
68 // Returns a view into the input string 'str' with the given 'prefix' removed,
69 // but leaving the original string intact. If the prefix does not match at the
70 // start of the string, returns the original string instead.
73  if (absl::StartsWith(str, prefix)) str.remove_prefix(prefix.size());
74  return str;
75 }
76 
77 // StripSuffix()
78 //
79 // Returns a view into the input string 'str' with the given 'suffix' removed,
80 // but leaving the original string intact. If the suffix does not match at the
81 // end of the string, returns the original string instead.
84  if (absl::EndsWith(str, suffix)) str.remove_suffix(suffix.size());
85  return str;
86 }
87 
89 } // namespace absl
90 
91 #endif // ABSL_STRINGS_STRIP_H_
xds_interop_client.str
str
Definition: xds_interop_client.py:487
absl::ConsumeSuffix
bool ConsumeSuffix(absl::string_view *str, absl::string_view expected)
Definition: abseil-cpp/absl/strings/strip.h:62
absl::StartsWith
bool StartsWith(absl::string_view text, absl::string_view prefix) noexcept
Definition: third_party/abseil-cpp/absl/strings/match.h:58
absl::string_view
Definition: abseil-cpp/absl/strings/string_view.h:167
absl::StripPrefix
ABSL_MUST_USE_RESULT absl::string_view StripPrefix(absl::string_view str, absl::string_view prefix)
Definition: abseil-cpp/absl/strings/strip.h:73
ABSL_NAMESPACE_END
#define ABSL_NAMESPACE_END
Definition: third_party/abseil-cpp/absl/base/config.h:171
absl::StripSuffix
ABSL_MUST_USE_RESULT absl::string_view StripSuffix(absl::string_view str, absl::string_view suffix)
Definition: abseil-cpp/absl/strings/strip.h:84
ABSL_MUST_USE_RESULT
#define ABSL_MUST_USE_RESULT
Definition: abseil-cpp/absl/base/attributes.h:441
ABSL_NAMESPACE_BEGIN
#define ABSL_NAMESPACE_BEGIN
Definition: third_party/abseil-cpp/absl/base/config.h:170
absl::string_view::size
constexpr size_type size() const noexcept
Definition: abseil-cpp/absl/strings/string_view.h:277
suffix
unsigned char suffix[65536]
Definition: bloaty/third_party/zlib/examples/gun.c:164
prefix
static const char prefix[]
Definition: head_of_line_blocking.cc:28
absl
Definition: abseil-cpp/absl/algorithm/algorithm.h:31
absl::EndsWith
bool EndsWith(absl::string_view text, absl::string_view suffix) noexcept
Definition: third_party/abseil-cpp/absl/strings/match.h:68
absl::ConsumePrefix
ABSL_NAMESPACE_BEGIN bool ConsumePrefix(absl::string_view *str, absl::string_view expected)
Definition: abseil-cpp/absl/strings/strip.h:46


grpc
Author(s):
autogenerated on Fri May 16 2025 03:00:22