1
+ // Compile Time Evaluated Boyer Moore Horspool String Search Algorithm
2
+ // Author: David Kanekanian
3
+ //
4
+ // Requires C++17.
5
+
1
6
#include < string_view> // std::string_view
2
7
#include < optional> // std::optional
3
8
#include < array> // std::array
@@ -19,7 +24,6 @@ struct SearchPattern {
19
24
bad_char_table[i] = needle.length ();
20
25
}
21
26
22
-
23
27
// Rule 2
24
28
// Characters in the needle except last character jump:
25
29
// length of needle - last position of character - 1
@@ -30,7 +34,9 @@ struct SearchPattern {
30
34
};
31
35
32
36
/* * Returns index of first occurrance of pattern in haystack. */
33
- constexpr std::optional<std::size_t > boyer_moore_horspool (SearchPattern pattern, std::string_view haystack) {
37
+ [[nodiscard]] constexpr std::optional<std::size_t > boyer_moore_horspool (
38
+ const SearchPattern& pattern,
39
+ const std::string_view haystack) noexcept {
34
40
for (std::size_t h = 0 ; h < haystack.length () - pattern.needle .length () + 1 ;) {
35
41
// Search this placement of the needle.
36
42
bool found = true ;
@@ -63,8 +69,8 @@ int main() {
63
69
static_assert (index == 3 , " index is supposed to be 3!" );
64
70
65
71
if (index) {
66
- std::cout << " found at index " << index.value () << ' \n ' << std::flush ;
72
+ std::cout << " found at index " << index.value () << ' \n ' ;
67
73
} else {
68
- std::cout << " not found" << std::endl ;
74
+ std::cout << " not found" << ' \n ' ;
69
75
}
70
76
}
0 commit comments