wildcard_cmp: Allow case-insensitive searches

The new search feature will require them.
This commit is contained in:
Xavier Del Campo Romero 2023-06-06 01:59:46 +02:00
parent d4c8a8062f
commit 6e5c001c28
Signed by: xavi
GPG Key ID: 84FF3612A9BF43F2
3 changed files with 29 additions and 14 deletions

View File

@ -58,7 +58,7 @@ static int on_payload(const struct http_payload *const p,
{
const struct elem *const e = &h->elem[i];
if (e->op == p->op && !wildcard_cmp(p->resource, e->url))
if (e->op == p->op && !wildcard_cmp(p->resource, e->url, true))
return e->f(p, r, e->user);
}

View File

@ -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;
}
}
}

View File

@ -1,6 +1,8 @@
#ifndef WILDCARD_CMP_H
#define WILDCARD_CMP_H
int wildcard_cmp(const char *s, const char *p);
#include <stdbool.h>
int wildcard_cmp(const char *s, const char *p, bool casecmp);
#endif /* WILDCARD_CMP_H */