General Utility Library for C++17 25.4.1
substring_checks.h
Go to the documentation of this file.
1
23#ifndef GUL17_SUBSTRING_CHECKS_H_
24#define GUL17_SUBSTRING_CHECKS_H_
25
26#include <string_view>
27
28#include "gul17/case_ascii.h"
29#include "gul17/internal.h"
30
31namespace gul17 {
32
52constexpr inline bool contains(std::string_view haystack, std::string_view needle) noexcept
53{
54 return haystack.find(needle) != std::string_view::npos;
55}
56
69constexpr inline bool contains(std::string_view haystack, char needle) noexcept
70{
71 return haystack.find(needle) != std::string_view::npos;
72}
73
86constexpr inline bool ends_with(std::string_view str, std::string_view suffix) noexcept
87{
88 const auto hsl = str.length();
89 const auto sl = suffix.length();
90
91 return hsl >= sl && str.compare(hsl - sl, sl, suffix) == 0;
92}
93
105constexpr inline bool ends_with(std::string_view str, char c) noexcept
106{
107 return !str.empty() && str.back() == c;
108}
109
122constexpr inline bool starts_with(std::string_view str, std::string_view prefix) noexcept
123{
124 const auto hsl = str.length();
125 const auto hl = prefix.length();
126
127 return hsl >= hl && std::string_view{ str.data(), hl }.compare(prefix) == 0;
128}
129
141constexpr inline bool starts_with(std::string_view str, char c) noexcept
142{
143 return !str.empty() && str.front() == c;
144}
145
146//
147// Case insensitive std::variants following
148//
149
166constexpr inline bool equals_nocase(std::string_view str1, std::string_view str2) noexcept
167{
168 if (str1.size() != str2.size())
169 return false;
170
171 // Hand-rolled version of std::equal() to maintain constexprness prior to C++20
172 auto it1 = str1.cbegin();
173 auto it2 = str2.cbegin();
174
175 while (it1 != str1.cend())
176 {
178 return false;
179
180 ++it1;
181 ++it2;
182 }
183
184 return true;
185}
186
208constexpr inline bool contains_nocase(std::string_view haystack, std::string_view needle) noexcept
209{
210 auto const haysize = haystack.size();
211 auto const needlesize = needle.size();
212 if (haysize < needlesize)
213 return false;
214 for (std::size_t i = 0; i <= haysize - needlesize; ++i) {
215 auto j = needlesize;
216 while (j-- > 0) {
218 break;
219 }
220 if (j == std::size_t(-1)) {
221 return true;
222 }
223 haystack.remove_prefix(1);
224 }
225 return false;
226}
227
242constexpr inline bool contains_nocase(std::string_view haystack, char needle) noexcept
243{
245 while (not haystack.empty()) {
246 if (lowercase_ascii(haystack.front()) == needle)
247 return true;
248 haystack.remove_prefix(1);
249 }
250 return false;
251}
252
268constexpr inline bool ends_with_nocase(std::string_view str, std::string_view suffix) noexcept
269{
270 while (not str.empty()) {
271 if (suffix.empty())
272 return true;
273 if (lowercase_ascii(str.back()) != lowercase_ascii(suffix.back()))
274 return false;
275 str.remove_suffix(1);
276 suffix.remove_suffix(1);
277 }
278 return suffix.empty();
279}
280
294constexpr inline bool ends_with_nocase(std::string_view str, char c) noexcept
295{
296 return !str.empty() && lowercase_ascii(str.back()) == lowercase_ascii(c);
297}
298
314constexpr inline bool starts_with_nocase(std::string_view str, std::string_view prefix) noexcept
315{
316 while (not str.empty()) {
317 if (prefix.empty())
318 return true;
319 if (lowercase_ascii(str.front()) != lowercase_ascii(prefix.front()))
320 return false;
321 str.remove_prefix(1);
322 prefix.remove_prefix(1);
323 }
324 return prefix.empty();
325}
326
340constexpr inline bool starts_with_nocase(std::string_view str, char c) noexcept
341{
342 return !str.empty() && lowercase_ascii(str.front()) == lowercase_ascii(c);
343}
344
346
347} // namespace gul17
348
349#endif
350
351/* vim:set expandtab softtabstop=4 tabstop=4 shiftwidth=4 textwidth=90 cindent: */
Declarations of lowercase_ascii(), lowercase_ascii_inplace(), uppercase_ascii(), and uppercase_ascii_...
auto constexpr bit_set(unsigned bit) noexcept -> ReturnT
Set a bit in an integral type.
Definition bit_manip.h:121
constexpr char lowercase_ascii(char c) noexcept
Return the ASCII lowercase equivalent of the given character (or the unchanged character,...
Definition case_ascii.h:48
constexpr bool ends_with(std::string_view str, std::string_view suffix) noexcept
Determine whether a string ends with another string.
Definition substring_checks.h:86
constexpr bool contains_nocase(std::string_view haystack, std::string_view needle) noexcept
Determine whether a string contains another string.
Definition substring_checks.h:208
constexpr bool starts_with(std::string_view str, std::string_view prefix) noexcept
Determine whether a string starts with another string.
Definition substring_checks.h:122
constexpr bool equals_nocase(std::string_view str1, std::string_view str2) noexcept
Determine whether a string is equal to another one, making no distinction between upper and lower cas...
Definition substring_checks.h:166
constexpr bool ends_with_nocase(std::string_view str, std::string_view suffix) noexcept
Determine whether a string ends with another string.
Definition substring_checks.h:268
constexpr bool starts_with_nocase(std::string_view str, std::string_view prefix) noexcept
Determine whether a string starts with another string.
Definition substring_checks.h:314
constexpr bool contains(std::string_view haystack, std::string_view needle) noexcept
Determine whether a string contains another string.
Definition substring_checks.h:52
Definition of macros used internally by GUL.
Namespace gul17 contains all functions and classes of the General Utility Library.
Definition doxygen.h:26