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/match.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: match.h
18
// -----------------------------------------------------------------------------
19
//
20
// This file contains simple utilities for performing string matching checks.
21
// All of these function parameters are specified as `absl::string_view`,
22
// meaning that these functions can accept `std::string`, `absl::string_view` or
23
// NUL-terminated C-style strings.
24
//
25
// Examples:
26
//   std::string s = "foo";
27
//   absl::string_view sv = "f";
28
//   assert(absl::StrContains(s, sv));
29
//
30
// Note: The order of parameters in these functions is designed to mimic the
31
// order an equivalent member function would exhibit;
32
// e.g. `s.Contains(x)` ==> `absl::StrContains(s, x).
33
#ifndef ABSL_STRINGS_MATCH_H_
34
#define ABSL_STRINGS_MATCH_H_
35
36
#include <cstring>
37
38
#include "absl/base/config.h"
39
#include "absl/strings/string_view.h"
40
41
namespace absl {
42
ABSL_NAMESPACE_BEGIN
43
44
// StrContains()
45
//
46
// Returns whether a given string `haystack` contains the substring `needle`.
47
inline bool StrContains(absl::string_view haystack,
48
0
                        absl::string_view needle) noexcept {
49
0
  return haystack.find(needle, 0) != haystack.npos;
50
0
}
51
52
0
inline bool StrContains(absl::string_view haystack, char needle) noexcept {
53
0
  return haystack.find(needle) != haystack.npos;
54
0
}
55
56
// StartsWith()
57
//
58
// Returns whether a given string `text` begins with `prefix`.
59
inline constexpr bool StartsWith(absl::string_view text,
60
0
                                 absl::string_view prefix) noexcept {
61
0
  if (prefix.empty()) {
62
0
    return true;
63
0
  }
64
0
  if (text.size() < prefix.size()) {
65
0
    return false;
66
0
  }
67
0
  absl::string_view possible_match = text.substr(0, prefix.size());
68
69
0
  return possible_match == prefix;
70
0
}
71
72
// EndsWith()
73
//
74
// Returns whether a given string `text` ends with `suffix`.
75
inline constexpr bool EndsWith(absl::string_view text,
76
0
                               absl::string_view suffix) noexcept {
77
0
  if (suffix.empty()) {
78
0
    return true;
79
0
  }
80
0
  if (text.size() < suffix.size()) {
81
0
    return false;
82
0
  }
83
0
  absl::string_view possible_match = text.substr(text.size() - suffix.size());
84
0
  return possible_match == suffix;
85
0
}
86
// StrContainsIgnoreCase()
87
//
88
// Returns whether a given ASCII string `haystack` contains the ASCII substring
89
// `needle`, ignoring case in the comparison.
90
bool StrContainsIgnoreCase(absl::string_view haystack,
91
                           absl::string_view needle) noexcept;
92
93
bool StrContainsIgnoreCase(absl::string_view haystack,
94
                           char needle) noexcept;
95
96
// EqualsIgnoreCase()
97
//
98
// Returns whether given ASCII strings `piece1` and `piece2` are equal, ignoring
99
// case in the comparison.
100
bool EqualsIgnoreCase(absl::string_view piece1,
101
                      absl::string_view piece2) noexcept;
102
103
// StartsWithIgnoreCase()
104
//
105
// Returns whether a given ASCII string `text` starts with `prefix`,
106
// ignoring case in the comparison.
107
bool StartsWithIgnoreCase(absl::string_view text,
108
                          absl::string_view prefix) noexcept;
109
110
// EndsWithIgnoreCase()
111
//
112
// Returns whether a given ASCII string `text` ends with `suffix`, ignoring
113
// case in the comparison.
114
bool EndsWithIgnoreCase(absl::string_view text,
115
                        absl::string_view suffix) noexcept;
116
117
// Yields the longest prefix in common between both input strings.
118
// Pointer-wise, the returned result is a subset of input "a".
119
absl::string_view FindLongestCommonPrefix(absl::string_view a,
120
                                          absl::string_view b);
121
122
// Yields the longest suffix in common between both input strings.
123
// Pointer-wise, the returned result is a subset of input "a".
124
absl::string_view FindLongestCommonSuffix(absl::string_view a,
125
                                          absl::string_view b);
126
127
ABSL_NAMESPACE_END
128
}  // namespace absl
129
130
#endif  // ABSL_STRINGS_MATCH_H_