diff options
| author | Xavier Del Campo Romero <xavi.dcr@tutanota.com> | 2023-06-06 01:59:46 +0200 |
|---|---|---|
| committer | Xavier Del Campo Romero <xavi.dcr@tutanota.com> | 2023-06-06 03:48:50 +0200 |
| commit | 6e5c001c287c51c558f4dc255d7a130937bd4777 (patch) | |
| tree | f8ea7edb5fcc3a273f1097e81754ad81fc615e58 /wildcard_cmp.c | |
| parent | d4c8a8062ff722d7165f4b5faefe0f9b73d3738a (diff) | |
wildcard_cmp: Allow case-insensitive searches
The new search feature will require them.
Diffstat (limited to 'wildcard_cmp.c')
| -rw-r--r-- | wildcard_cmp.c | 37 |
1 files changed, 25 insertions, 12 deletions
diff --git a/wildcard_cmp.c b/wildcard_cmp.c index 8f7c7c1..c4743d3 100644 --- a/wildcard_cmp.c +++ b/wildcard_cmp.c @@ -1,20 +1,28 @@ #include "wildcard_cmp.h" +#include <stdbool.h> +#include <stddef.h> #include <string.h> +#include <strings.h> -static int wildcard_cmp(const char *s, const char *p) +int wildcard_cmp(const char *s, const char *p, const bool casecmp) { + int (*const cmp)(const char *, const char *) = + casecmp ? strcmp : strcasecmp; + int (*const ncmp)(const char *, const char *, size_t) = + casecmp ? strncmp : strncasecmp; + while (*p && *s) { const char *const wc = strchr(p, '*'); if (!wc) - return strcmp(s, p); + return cmp(s, p); const size_t n = wc - p; if (n) { - const int r = strncmp(s, p, n); + const int r = ncmp(s, p, n); if (r) return r; @@ -22,17 +30,22 @@ static int wildcard_cmp(const char *s, const char *p) p += n; s += n; } - else if (*(wc + 1) == *s) - { - p = wc + 1; - s += n; - } - else if (*(wc + 1) == '*') - p++; else { - s++; - p += n; + const char next = *(wc + 1), wca[2] = {next}, sa[sizeof wca] = {*s}; + + if (!cmp(wca, sa)) + { + p = wc + 1; + s += n; + } + else if (next == '*') + p++; + else + { + s++; + p += n; + } } } |
