Coverage Report

Created: 2026-09-14 06:45

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