LibC: Add FNM_CASEFOLD and FNM_IGNORECASE

These are part of POSIX issue 8
This commit is contained in:
Bananymous 2026-03-25 04:27:00 +02:00
parent e9c81477d7
commit d9ca25b796
2 changed files with 14 additions and 2 deletions

View File

@ -1,3 +1,4 @@
#include <ctype.h>
#include <fnmatch.h> #include <fnmatch.h>
#include <stddef.h> #include <stddef.h>
#include <stdint.h> #include <stdint.h>
@ -5,6 +6,8 @@
static int fnmatch_impl(const char* pattern, const char* string, int flags, bool leading) static int fnmatch_impl(const char* pattern, const char* string, int flags, bool leading)
{ {
const bool ignore_case = !!(flags & FNM_IGNORECASE);
while (*pattern) while (*pattern)
{ {
if ((flags & FNM_PERIOD) && leading && *string == '.' && *pattern != '.') if ((flags & FNM_PERIOD) && leading && *string == '.' && *pattern != '.')
@ -34,9 +37,13 @@ static int fnmatch_impl(const char* pattern, const char* string, int flags, bool
uint8_t ch; uint8_t ch;
uint32_t bitmap[0x100 / 8] {}; uint32_t bitmap[0x100 / 8] {};
while ((ch = *pattern++) != ']') while ((ch = *pattern++) != ']')
{
if (ignore_case)
ch = tolower(ch);
bitmap[ch / 32] |= 1 << (ch % 32); bitmap[ch / 32] |= 1 << (ch % 32);
}
ch = *string++; ch = ignore_case ? tolower(*string++) : *string++;
if (!!(bitmap[ch / 32] & (1 << (ch % 32))) == negate) if (!!(bitmap[ch / 32] & (1 << (ch % 32))) == negate)
return FNM_NOMATCH; return FNM_NOMATCH;
@ -63,7 +70,10 @@ static int fnmatch_impl(const char* pattern, const char* string, int flags, bool
if (*pattern == '\0') if (*pattern == '\0')
break; break;
if (*pattern != *string) const char lhs = ignore_case ? tolower(*pattern) : *pattern;
const char rhs = ignore_case ? tolower(*string) : *string;
if (lhs != rhs)
return FNM_NOMATCH; return FNM_NOMATCH;
if ((flags & FNM_PATHNAME) && *string == '/') if ((flags & FNM_PATHNAME) && *string == '/')
leading = true; leading = true;

View File

@ -11,6 +11,8 @@ __BEGIN_DECLS
#define FNM_PATHNAME 0x01 #define FNM_PATHNAME 0x01
#define FNM_PERIOD 0x02 #define FNM_PERIOD 0x02
#define FNM_NOESCAPE 0x04 #define FNM_NOESCAPE 0x04
#define FNM_CASEFOLD 0x08
#define FNM_IGNORECASE FNM_CASEFOLD
int fnmatch(const char* pattern, const char* string, int flags); int fnmatch(const char* pattern, const char* string, int flags);