HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
pattern_match.hpp
Go to the documentation of this file.
1// Copyright Take Vos 2022.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
8#pragma once
9
10namespace hi { inline namespace v1 {
11
19template<char Wildcard = '*'>
20[[nodiscard]] constexpr bool pattern_match(std::string_view needle, std::string_view haystack) noexcept
21{
22 auto needle_index = 0_uz;
23 auto haystack_index = 0_uz;
24
25 while (needle_index != needle.size()) {
26 // Find the next glob.
27 hilet i = needle.find(Wildcard, needle_index);
28 hilet pattern = needle.substr(needle_index, i);
29 needle_index = i != needle.npos ? i + 1 : needle.size();
30
31 // Check if the patterns between two globs is found in the haystack in-sequence.
32 if (not pattern.empty()) {
33 haystack_index = haystack.find(pattern, haystack_index);
34 if (haystack_index == haystack.npos) {
35 return false;
36 }
37 haystack_index += pattern.size();
38 }
39 }
40
41 if (needle.empty() or needle.back() != Wildcard) {
42 return haystack_index == haystack.size();
43 }
44 return true;
45}
46
47}} // namespace hi::v1
#define hilet
Invariant should be the default for variables.
Definition utility.hpp:23
DOXYGEN BUG.
Definition algorithm.hpp:13
geometry/margins.hpp
Definition cache.hpp:11
constexpr bool pattern_match(std::string_view needle, std::string_view haystack) noexcept
Patern match.
Definition pattern_match.hpp:20