aboutsummaryrefslogtreecommitdiff
path: root/wildcard_cmp.c
diff options
context:
space:
mode:
authorXavier Del Campo Romero <xavi.dcr@tutanota.com>2023-06-06 01:59:46 +0200
committerXavier Del Campo Romero <xavi.dcr@tutanota.com>2023-07-20 23:52:55 +0200
commit35ced95c363640c3f8cd86b60330fb68338101b8 (patch)
treef06c46b023dcdf0d7c3b401a0ad75890508e6976 /wildcard_cmp.c
parent0759122e4c7dd17af9f981040e44c747579478b6 (diff)
downloadlibweb-35ced95c363640c3f8cd86b60330fb68338101b8.tar.gz
wildcard_cmp: Allow case-insensitive searches
The new search feature will require them.
Diffstat (limited to 'wildcard_cmp.c')
-rw-r--r--wildcard_cmp.c37
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;
+ }
}
}