Coverage Report

Created: 2023-09-25 06:27

/src/abseil-cpp/absl/strings/strip.h
Line
Count
Source (jump to first uncovered line)
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 {
33
ABSL_NAMESPACE_BEGIN
34
35
// ConsumePrefix()
36
//
37
// Strips the `expected` prefix, if found, from the start of `str`.
38
// If the operation succeeded, `true` is returned.  If not, `false`
39
// is returned and `str` is not modified.
40
//
41
// Example:
42
//
43
//   absl::string_view input("abc");
44
//   EXPECT_TRUE(absl::ConsumePrefix(&input, "a"));
45
//   EXPECT_EQ(input, "bc");
46
0
inline bool ConsumePrefix(absl::string_view* str, absl::string_view expected) {
47
0
  if (!absl::StartsWith(*str, expected)) return false;
48
0
  str->remove_prefix(expected.size());
49
0
  return true;
50
0
}
51
// ConsumeSuffix()
52
//
53
// Strips the `expected` suffix, if found, from the end of `str`.
54
// If the operation succeeded, `true` is returned.  If not, `false`
55
// is returned and `str` is not modified.
56
//
57
// Example:
58
//
59
//   absl::string_view input("abcdef");
60
//   EXPECT_TRUE(absl::ConsumeSuffix(&input, "def"));
61
//   EXPECT_EQ(input, "abc");
62
0
inline bool ConsumeSuffix(absl::string_view* str, absl::string_view expected) {
63
0
  if (!absl::EndsWith(*str, expected)) return false;
64
0
  str->remove_suffix(expected.size());
65
0
  return true;
66
0
}
67
68
// StripPrefix()
69
//
70
// Returns a view into the input string `str` with the given `prefix` removed,
71
// but leaving the original string intact. If the prefix does not match at the
72
// start of the string, returns the original string instead.
73
ABSL_MUST_USE_RESULT inline absl::string_view StripPrefix(
74
0
    absl::string_view str, absl::string_view prefix) {
75
0
  if (absl::StartsWith(str, prefix)) str.remove_prefix(prefix.size());
76
0
  return str;
77
0
}
78
79
// StripSuffix()
80
//
81
// Returns a view into the input string `str` with the given `suffix` removed,
82
// but leaving the original string intact. If the suffix does not match at the
83
// end of the string, returns the original string instead.
84
ABSL_MUST_USE_RESULT inline absl::string_view StripSuffix(
85
0
    absl::string_view str, absl::string_view suffix) {
86
0
  if (absl::EndsWith(str, suffix)) str.remove_suffix(suffix.size());
87
0
  return str;
88
0
}
89
90
ABSL_NAMESPACE_END
91
}  // namespace absl
92
93
#endif  // ABSL_STRINGS_STRIP_H_