4 #include "parse_trigrams.h"
16 bool matches(const Needle &needle, const char *haystack)
18 if (needle.type == Needle::STRSTR) {
19 return strstr(haystack, needle.str.c_str()) != nullptr;
20 } else if (needle.type == Needle::GLOB) {
21 int flags = ignore_case ? FNM_CASEFOLD : 0;
22 return fnmatch(needle.str.c_str(), haystack, flags) == 0;
24 assert(needle.type == Needle::REGEX);
25 return regexec(&needle.re, haystack, /*nmatch=*/0, /*pmatch=*/nullptr, /*flags=*/0) == 0;
29 string unescape_glob_to_plain_string(const string &needle)
32 for (size_t i = 0; i < needle.size(); i += read_unigram(needle, i).second) {
33 uint32_t ch = read_unigram(needle, i).first;
34 assert(ch != WILDCARD_UNIGRAM);
35 if (ch == PREMATURE_END_UNIGRAM) {
36 fprintf(stderr, "Pattern '%s' ended prematurely\n", needle.c_str());
39 unescaped.push_back(ch);
44 regex_t compile_regex(const string &needle)
47 int flags = REG_NOSUB;
51 if (use_extended_regex) {
52 flags |= REG_EXTENDED;
54 int err = regcomp(&re, needle.c_str(), flags);
57 regerror(err, &re, errbuf, sizeof(errbuf));
58 fprintf(stderr, "Error when compiling regex '%s': %s\n", needle.c_str(), errbuf);