Skip to content

Commit 1f08924

Browse files
committed
bugfix(posix,fnmatch): fixed failling tests
Implemented match_posix_class function that was needed to match the possible posix classes like alnum, alpha etc... Signed-off-by: Spago123 <harun.spago.code@gmail.com>
1 parent c9d3a01 commit 1f08924

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

lib/posix/options/fnmatch.c

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,39 @@ static inline int foldcase(int ch, int flags)
6060

6161
#define FOLDCASE(ch, flags) foldcase((unsigned char)(ch), (flags))
6262

63+
static bool match_posix_class(const char **pattern, int test) {
64+
static const struct {
65+
const char *name;
66+
int (*func)(int);
67+
} classes[] = {
68+
{ "alnum", isalnum },
69+
{ "alpha", isalpha },
70+
{ "digit", isdigit },
71+
{ "lower", islower },
72+
{ "upper", isupper },
73+
{ "space", isspace },
74+
{ "xdigit", isxdigit },
75+
{ "punct", ispunct },
76+
{ "cntrl", iscntrl },
77+
{ "graph", isgraph },
78+
{ "print", isprint },
79+
};
80+
81+
const char *p = *pattern;
82+
if (*p != ':' ) return false;
83+
p++;
84+
for (size_t i = 0; i < sizeof(classes)/sizeof(classes[0]); i++) {
85+
size_t len = strlen(classes[i].name);
86+
if (strncmp(p, classes[i].name, len) == 0 && p[len] == ':') {
87+
if (p[len+1] == ']') {
88+
*pattern = p + len + 2; // move past ":]"
89+
return classes[i].func(test);
90+
}
91+
}
92+
}
93+
return false;
94+
}
95+
6396
static const char *rangematch(const char *pattern, int test, int flags)
6497
{
6598
bool negate, ok, need;
@@ -99,6 +132,18 @@ static const char *rangematch(const char *pattern, int test, int flags)
99132
return NULL;
100133
}
101134

135+
if (c == '[' && *pattern == ':') {
136+
if (match_posix_class(&pattern, test)) {
137+
ok = true;
138+
continue;
139+
} else {
140+
// skip over class if unrecognized
141+
while (*pattern && !(*pattern == ':' && *(pattern+1) == ']')) pattern++;
142+
if (*pattern) pattern += 2;
143+
continue;
144+
}
145+
}
146+
102147
if (*pattern == '-') {
103148
c2 = FOLDCASE(*(pattern + 1), flags);
104149
if (c2 != EOS && c2 != ']') {

tests/posix/c_lib_ext/src/fnmatch.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ ZTEST(posix_c_lib_ext, test_fnmatch)
7373
zassert_equal(fnmatch("*/*", "a/.b", FNM_PATHNAME | FNM_PERIOD), FNM_NOMATCH);
7474
zassert_ok(fnmatch("*?*/*", "a/.b", FNM_PERIOD));
7575
zassert_ok(fnmatch("*[.]/b", "a./b", FNM_PATHNAME | FNM_PERIOD));
76-
/* zassert_ok(fnmatch("*[[:alpha:]]/""*[[:alnum:]]", "a/b", FNM_PATHNAME)); */
76+
/** Test cases for POSIC classes */
77+
zassert_ok(fnmatch("*[[:alpha:]]/""*[[:alnum:]]", "a/b", FNM_PATHNAME));
7778
zassert_not_equal(fnmatch("*[![:digit:]]*/[![:d-d]", "a/b", FNM_PATHNAME), 0);
7879
zassert_not_equal(fnmatch("*[![:digit:]]*/[[:d-d]", "a/[", FNM_PATHNAME), 0);
7980
zassert_not_equal(fnmatch("*[![:digit:]]*/[![:d-d]", "a/[", FNM_PATHNAME), 0);

0 commit comments

Comments
 (0)