| 1 |
// Copyright (C) 2014-2025 Free Software Foundation, Inc. |
| 2 |
// |
| 3 |
// This file is part of the GNU ISO C++ Library. This library is free |
| 4 |
// software; you can redistribute it and/or modify it under the |
| 5 |
// terms of the GNU General Public License as published by the |
| 6 |
// Free Software Foundation; either version 3, or (at your option) |
| 7 |
// any later version. |
| 8 |
|
| 9 |
// This library is distributed in the hope that it will be useful, |
| 10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
// GNU General Public License for more details. |
| 13 |
|
| 14 |
// You should have received a copy of the GNU General Public License along |
| 15 |
// with this library; see the file COPYING3. If not see |
| 16 |
// <http://www.gnu.org/licenses/>. |
| 17 |
|
| 18 |
// { dg-do run { target c++17 } } |
| 19 |
// { dg-add-options no_pch } |
| 20 |
|
| 21 |
#include <functional> |
| 22 |
|
| 23 |
#if __STDC_HOSTED__ |
| 24 |
# ifndef __cpp_lib_boyer_moore_searcher |
| 25 |
# error "Feature-test macro for searchers missing in <functional>" |
| 26 |
# elif __cpp_lib_boyer_moore_searcher < 201603 |
| 27 |
# error "Feature-test macro for searchers has wrong value in <functional>" |
| 28 |
# endif |
| 29 |
#endif // HOSTED |
| 30 |
|
| 31 |
#include <string_view> |
| 32 |
#include <cstring> |
| 33 |
#include <cctype> |
| 34 |
#include <algorithm> |
| 35 |
#include <testsuite_hooks.h> |
| 36 |
|
| 37 |
using std::default_searcher; |
| 38 |
#if __STDC_HOSTED__ |
| 39 |
using std::boyer_moore_searcher; |
| 40 |
using std::boyer_moore_horspool_searcher; |
| 41 |
#endif // HOSTED |
| 42 |
|
| 43 |
void |
| 44 |
test01() |
| 45 |
{ |
| 46 |
const char s[] = { 'a', (char)-97, 'a', '\0' }; |
| 47 |
const char* needles[] = { |
| 48 |
s, "", "a", "aa", "aaa", "ab", "cd", "abcd", "abcdabcd", "abcabcd" |
| 49 |
}; |
| 50 |
const char* haystacks[] = { |
| 51 |
s, "", "a", "aa", "aaa", "ab", "cd", "abcd", "abcdabcd", "abcabcd", |
| 52 |
"aaaaaaa", "aabaa", "aaacab", "cdabcdab", "abcdabcd", "xyzabcdxyz" |
| 53 |
}; |
| 54 |
|
| 55 |
for (auto n : needles) |
| 56 |
{ |
| 57 |
auto nlen = std::strlen(n); |
| 58 |
auto ne = n + nlen; |
| 59 |
default_searcher d(n, ne); |
| 60 |
#if __STDC_HOSTED__ |
| 61 |
boyer_moore_searcher bm(n, ne); |
| 62 |
boyer_moore_horspool_searcher bmh(n, ne); |
| 63 |
#endif // HOSTED |
| 64 |
for (auto h : haystacks) |
| 65 |
{ |
| 66 |
auto he = h + std::strlen(h); |
| 67 |
auto res = std::search(h, he, n, ne); |
| 68 |
auto d_res = d(h, he); |
| 69 |
VERIFY( d_res.first == res ); |
| 70 |
if (res == he) |
| 71 |
VERIFY( d_res.second == d_res.first ); |
| 72 |
else |
| 73 |
VERIFY( d_res.second == (d_res.first + nlen) ); |
| 74 |
|
| 75 |
#if __STDC_HOSTED__ |
| 76 |
auto bm_res = bm(h, he); |
| 77 |
VERIFY( bm_res.first == res ); |
| 78 |
if (res == he) |
| 79 |
VERIFY( bm_res.second == bm_res.first ); |
| 80 |
else |
| 81 |
VERIFY( bm_res.second == (bm_res.first + nlen) ); |
| 82 |
auto bmh_res = bmh(h, he); |
| 83 |
VERIFY( bmh_res.first == res ); |
| 84 |
if (res == he) |
| 85 |
VERIFY( bmh_res.second == bmh_res.first ); |
| 86 |
else |
| 87 |
VERIFY( bmh_res.second == (bmh_res.first + nlen) ); |
| 88 |
#endif |
| 89 |
} |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
void |
| 94 |
test02() |
| 95 |
{ |
| 96 |
const wchar_t s[] = { L'a', (wchar_t)-97, L'a', L'\0' }; |
| 97 |
const wchar_t* needles[] = { |
| 98 |
s, L"", L"a", L"aa", L"aaa", L"ab", L"cd", L"abcd", L"abcdabcd", L"abcabcd" |
| 99 |
}; |
| 100 |
const wchar_t* haystacks[] = { |
| 101 |
s, L"", L"a", L"aa", L"aaa", L"ab", L"cd", L"abcd", L"abcdabcd", L"abcabcd", |
| 102 |
L"aaaaaaa", L"aabaa", L"aaacab", L"cdabcdab", L"abcdabcd", L"xyzabcdxyz" |
| 103 |
}; |
| 104 |
|
| 105 |
for (auto n : needles) |
| 106 |
{ |
| 107 |
auto nlen = std::char_traits<wchar_t>::length(n); |
| 108 |
auto ne = n + nlen; |
| 109 |
default_searcher d(n, ne); |
| 110 |
#if __STDC_HOSTED__ |
| 111 |
boyer_moore_searcher bm(n, ne); |
| 112 |
boyer_moore_horspool_searcher bmh(n, ne); |
| 113 |
#endif // HOSTED |
| 114 |
for (auto h : haystacks) |
| 115 |
{ |
| 116 |
auto he = h + std::char_traits<wchar_t>::length(h); |
| 117 |
auto res = std::search(h, he, n, ne); |
| 118 |
auto d_res = d(h, he); |
| 119 |
VERIFY( d_res.first == res ); |
| 120 |
if (res == he) |
| 121 |
VERIFY( d_res.second == d_res.first ); |
| 122 |
else |
| 123 |
VERIFY( d_res.second == (d_res.first + nlen) ); |
| 124 |
#if __STDC_HOSTED__ |
| 125 |
auto bm_res = bm(h, he); |
| 126 |
VERIFY( bm_res.first == res ); |
| 127 |
if (res == he) |
| 128 |
VERIFY( bm_res.second == bm_res.first ); |
| 129 |
else |
| 130 |
VERIFY( bm_res.second == (bm_res.first + nlen) ); |
| 131 |
auto bmh_res = bmh(h, he); |
| 132 |
VERIFY( bmh_res.first == res ); |
| 133 |
if (res == he) |
| 134 |
VERIFY( bmh_res.second == bmh_res.first ); |
| 135 |
else |
| 136 |
VERIFY( bmh_res.second == (bmh_res.first + nlen) ); |
| 137 |
#endif // HOSTED |
| 138 |
} |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
void |
| 143 |
test03() |
| 144 |
{ |
| 145 |
// custom predicate |
| 146 |
struct |
| 147 |
{ |
| 148 |
static unsigned char |
| 149 |
norm(unsigned char c) { return std::isalnum(c) ? c : '#'; } |
| 150 |
|
| 151 |
// equality |
| 152 |
bool operator()(char l, char r) const { return norm(l) == norm(r); } |
| 153 |
|
| 154 |
// hash |
| 155 |
std::size_t operator()(char c) const { return std::hash<char>{}(norm(c)); } |
| 156 |
} eq; |
| 157 |
|
| 158 |
const char* needle = " foo 123 "; |
| 159 |
const char* haystack = "*****foo*123******"; |
| 160 |
auto nlen = std::strlen(needle); |
| 161 |
const char* ne = needle + nlen; |
| 162 |
const char* he = haystack + std::strlen(haystack); |
| 163 |
|
| 164 |
default_searcher d(needle, ne, eq); |
| 165 |
#if __STDC_HOSTED__ |
| 166 |
boyer_moore_searcher bm(needle, ne, eq, eq); |
| 167 |
boyer_moore_horspool_searcher bmh(needle, ne, eq, eq); |
| 168 |
#endif |
| 169 |
|
| 170 |
auto res = std::search(haystack, he, needle, ne, eq); |
| 171 |
auto d_res = d(haystack, he); |
| 172 |
VERIFY( d_res.first == res ); |
| 173 |
if (res == he) |
| 174 |
VERIFY( d_res.second == d_res.first ); |
| 175 |
else |
| 176 |
VERIFY( d_res.second == (d_res.first + nlen) ); |
| 177 |
#if __STDC_HOSTED__ |
| 178 |
auto bm_res = bm(haystack, he); |
| 179 |
VERIFY( bm_res.first == res ); |
| 180 |
if (res == he) |
| 181 |
VERIFY( bm_res.second == bm_res.first ); |
| 182 |
else |
| 183 |
VERIFY( bm_res.second == (bm_res.first + nlen) ); |
| 184 |
auto bmh_res = bmh(haystack, he); |
| 185 |
VERIFY( bmh_res.first == res ); |
| 186 |
if (res == he) |
| 187 |
VERIFY( bmh_res.second == bmh_res.first ); |
| 188 |
else |
| 189 |
VERIFY( bmh_res.second == (bmh_res.first + nlen) ); |
| 190 |
#endif // HOSTED |
| 191 |
} |
| 192 |
|
| 193 |
int |
| 194 |
main() |
| 195 |
{ |
| 196 |
test01(); |
| 197 |
test02(); |
| 198 |
test03(); |
| 199 |
} |