diff options
| author | Xavier Del Campo Romero <xavi.dcr@tutanota.com> | 2023-06-06 03:35:20 +0200 |
|---|---|---|
| committer | Xavier Del Campo Romero <xavi.dcr@tutanota.com> | 2023-06-06 03:48:51 +0200 |
| commit | 6adf1c44adb61a04bf37eb78eb5df5c4302bd768 (patch) | |
| tree | 4fd4ecf3fa874ed19645a5c29ce77ec3a83c38fb /wildcard_cmp.c | |
| parent | 0d7ead46381da27f3b79e646434109ca4324ece8 (diff) | |
wildcard_cmp.c: Fix a couple of bugs
wildcard_cmp would otherwise fail with the following use cases:
s = "mymi", p = "*mi*"
s = "mymi", p = "*mi"
Diffstat (limited to 'wildcard_cmp.c')
| -rw-r--r-- | wildcard_cmp.c | 25 |
1 files changed, 22 insertions, 3 deletions
diff --git a/wildcard_cmp.c b/wildcard_cmp.c index dd9ff7a..dd19d6c 100644 --- a/wildcard_cmp.c +++ b/wildcard_cmp.c @@ -6,6 +6,7 @@ int wildcard_cmp(const char *s, const char *p, const bool casecmp) { + const char *last = NULL; int (*const cmp)(const char *, const char *) = casecmp ? strcmp : strcasecmp; int (*const ncmp)(const char *, const char *, size_t) = @@ -16,7 +17,18 @@ int wildcard_cmp(const char *s, const char *p, const bool casecmp) const char *const wc = strchr(p, '*'); if (!wc) - return cmp(s, p); + { + const int r = cmp(s, p); + + if (r && last) + { + p = last; + s += strlen(p); + continue; + } + else + return r; + } const size_t n = wc - p; @@ -25,9 +37,15 @@ int wildcard_cmp(const char *s, const char *p, const bool casecmp) const int r = ncmp(s, p, n); if (r) - return r; + { + if (last) + p = last; + else + return r; + } + else + p += n; - p += n; s += n; } else @@ -36,6 +54,7 @@ int wildcard_cmp(const char *s, const char *p, const bool casecmp) if (!cmp(wca, sa)) { + last = p; p = wc + 1; } else if (next == '*') |
