Skip to content

Commit 75bab66

Browse files
committed
refactor(fnmatch.c): removed duplicate code and added simplifications
Signed-off-by: Spago123 <harun.spago.code@gmail.com>
1 parent 1f08924 commit 75bab66

File tree

2 files changed

+73
-19
lines changed

2 files changed

+73
-19
lines changed

include/zephyr/posix/fnmatch.h

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,26 @@
5050
extern "C" {
5151
#endif
5252

53-
int fnmatch(const char *, const char *, int);
53+
/**
54+
* Function used to check if the given pattern is in the string, using the given flags
55+
*
56+
* @param pattern pattern that is matched against the string
57+
* @param string string that is checked for the pattern
58+
* @param flags used to signal special matching conditions
59+
* FNM_NOESCAPE 0x01 Disable backslash escaping.
60+
* FNM_PATHNAME 0x02 Slash must be matched by slash.
61+
* FNM_PERIOD 0x04 Period must be matched by period.
62+
* FNM_CASEFOLD 0x08 Pattern is matched case-insensitive
63+
* FNM_LEADING_DIR 0x10 Ignore /<tail> after Imatch.
64+
*
65+
*
66+
* @return int
67+
* @retval 0 pattern found in string
68+
* @retval FNM_NOMATCH pattern not found in string
69+
* @retval FNM_NORES recursion limit reached
70+
* @retval FNM_NOSYS function not implemented
71+
*/
72+
int fnmatch(const char *pattern, const char *string, int flags);
5473

5574
#ifdef __cplusplus
5675
}

lib/posix/options/fnmatch.c

Lines changed: 53 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,46 @@ static inline int foldcase(int ch, int flags)
5858
return ch;
5959
}
6060

61+
/**
62+
* @brief Function to check for FNM_PERIOD condition
63+
*
64+
* @param string string whose first character is checked if it is a period
65+
* @param flags passed flags to check for FNM_PERIOD
66+
* @return int
67+
* @retval 1 if first character is period and FNM_PERIOD flag is set
68+
* @retval 0 otherwise
69+
*/
70+
static int check_fnm_period(const char* string, const int flags)
71+
{
72+
return *string == '.' && (flags & FNM_PERIOD);
73+
}
74+
75+
/**
76+
* @brief Function to check for FNM_PATHNAME condition
77+
*
78+
* @param letter letter to be checked if it is a '/'
79+
* @param flags flags passed to check for FNM_PATHNAME
80+
* @return int
81+
* @retval 1 if letter is '/' and FNM_PATHNAME flag is set
82+
* @retval 0 otherwise
83+
*/
84+
static int check_for_pathname(const char letter, const int flags)
85+
{
86+
return letter == '/' && (flags & FNM_PATHNAME);
87+
}
88+
6189
#define FOLDCASE(ch, flags) foldcase((unsigned char)(ch), (flags))
6290

91+
/**
92+
* @brief Function to match POSIX character classes
93+
*
94+
* @param pattern start of the pattern, should point to the character after '['
95+
* @param test next character in the string to be tested against the class
96+
* @return true
97+
* @return false
98+
*/
6399
static bool match_posix_class(const char **pattern, int test) {
64-
static const struct {
100+
const struct {
65101
const char *name;
66102
int (*func)(int);
67103
} classes[] = {
@@ -118,7 +154,7 @@ static const char *rangematch(const char *pattern, int test, int flags)
118154
c = FOLDCASE(*pattern++, flags)) {
119155
need = false;
120156

121-
if (c == '/' && (flags & FNM_PATHNAME)) {
157+
if (check_for_pathname(c, flags)) {
122158
return (void *)-1;
123159
}
124160

@@ -195,13 +231,13 @@ static int fnmatchx(const char *pattern, const char *string, int flags, size_t r
195231
return FNM_NOMATCH;
196232
}
197233

198-
if (*string == '/' && (flags & FNM_PATHNAME)) {
234+
if (check_for_pathname(*string, flags)) {
199235
return FNM_NOMATCH;
200236
}
201237

202-
if (*string == '.' && (flags & FNM_PERIOD) &&
238+
if (check_fnm_period(string, flags) &&
203239
(string == stringstart ||
204-
((flags & FNM_PATHNAME) && *(string - 1) == '/'))) {
240+
check_for_pathname(*(string - 1), flags))) {
205241
return FNM_NOMATCH;
206242
}
207243

@@ -214,23 +250,22 @@ static int fnmatchx(const char *pattern, const char *string, int flags, size_t r
214250
c = FOLDCASE(*++pattern, flags);
215251
}
216252

217-
if (*string == '.' && (flags & FNM_PERIOD) &&
253+
if (check_fnm_period(string, flags) &&
218254
(string == stringstart ||
219-
((flags & FNM_PATHNAME) && *(string - 1) == '/'))) {
255+
check_for_pathname(*(string - 1), flags))) {
220256
return FNM_NOMATCH;
221257
}
222258

223259
/* Optimize for pattern with * at end or before /. */
224260
if (c == EOS) {
225-
if (flags & FNM_PATHNAME) {
226-
return (flags & FNM_LEADING_DIR) ||
227-
strchr(string, '/') == NULL
228-
? 0
229-
: FNM_NOMATCH;
230-
} else {
261+
if(!(flags & FNM_PATHNAME)) {
231262
return 0;
232263
}
233-
} else if (c == '/' && flags & FNM_PATHNAME) {
264+
return (flags & FNM_LEADING_DIR) ||
265+
strchr(string, '/') == NULL
266+
? 0
267+
: FNM_NOMATCH;
268+
} else if (check_for_pathname(c, flags)) {
234269
string = strchr(string, '/');
235270
if (string == NULL) {
236271
return FNM_NOMATCH;
@@ -252,7 +287,7 @@ static int fnmatchx(const char *pattern, const char *string, int flags, size_t r
252287
return e;
253288
}
254289

255-
if (test == '/' && flags & FNM_PATHNAME) {
290+
if (check_for_pathname(test, flags)) {
256291
break;
257292
}
258293

@@ -265,13 +300,13 @@ static int fnmatchx(const char *pattern, const char *string, int flags, size_t r
265300
return FNM_NOMATCH;
266301
}
267302

268-
if (*string == '/' && flags & FNM_PATHNAME) {
303+
if (check_for_pathname(*string, flags)) {
269304
return FNM_NOMATCH;
270305
}
271306

272-
if (*string == '.' && (flags & FNM_PERIOD) &&
307+
if (check_fnm_period(string, flags) &&
273308
(string == stringstart ||
274-
((flags & FNM_PATHNAME) && *(string - 1) == '/'))) {
309+
check_for_pathname(*(string - 1), flags))) {
275310
return FNM_NOMATCH;
276311
}
277312

0 commit comments

Comments
 (0)