Skip to content

Commit 210e6cd

Browse files
committed
refactor(fnmatch.c): removed duplicated code and simplified
Some Ifs had the same condition so insted of duplicating that work simple helper functions have been created Signed-off-by: Spago123 <harun.spago.code@gmail.com>
1 parent a7172d8 commit 210e6cd

File tree

1 file changed

+27
-18
lines changed

1 file changed

+27
-18
lines changed

lib/posix/options/fnmatch.c

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

61+
static int check_fnm_period(const char* string, const int flags)
62+
{
63+
return *string == '.' && (flags & FNM_PERIOD);
64+
}
65+
66+
static int check_for_pathname(const char letter, const int flags)
67+
{
68+
return letter == '/' && (flags & FNM_PATHNAME);
69+
}
70+
6171
#define FOLDCASE(ch, flags) foldcase((unsigned char)(ch), (flags))
6272

6373
static bool match_posix_class(const char **pattern, int test) {
64-
static const struct {
74+
const struct {
6575
const char *name;
6676
int (*func)(int);
6777
} classes[] = {
@@ -118,7 +128,7 @@ static const char *rangematch(const char *pattern, int test, int flags)
118128
c = FOLDCASE(*pattern++, flags)) {
119129
need = false;
120130

121-
if (c == '/' && (flags & FNM_PATHNAME)) {
131+
if (check_for_pathname(c, flags)) {
122132
return (void *)-1;
123133
}
124134

@@ -195,13 +205,13 @@ static int fnmatchx(const char *pattern, const char *string, int flags, size_t r
195205
return FNM_NOMATCH;
196206
}
197207

198-
if (*string == '/' && (flags & FNM_PATHNAME)) {
208+
if (check_for_pathname(*string, flags)) {
199209
return FNM_NOMATCH;
200210
}
201211

202-
if (*string == '.' && (flags & FNM_PERIOD) &&
212+
if (check_fnm_period(string, flags) &&
203213
(string == stringstart ||
204-
((flags & FNM_PATHNAME) && *(string - 1) == '/'))) {
214+
check_for_pathname(*(string - 1), flags))) {
205215
return FNM_NOMATCH;
206216
}
207217

@@ -214,23 +224,22 @@ static int fnmatchx(const char *pattern, const char *string, int flags, size_t r
214224
c = FOLDCASE(*++pattern, flags);
215225
}
216226

217-
if (*string == '.' && (flags & FNM_PERIOD) &&
227+
if (check_fnm_period(string, flags) &&
218228
(string == stringstart ||
219-
((flags & FNM_PATHNAME) && *(string - 1) == '/'))) {
229+
check_for_pathname(*(string - 1), flags))) {
220230
return FNM_NOMATCH;
221231
}
222232

223233
/* Optimize for pattern with * at end or before /. */
224234
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 {
235+
if(!(flags & FNM_PATHNAME)) {
231236
return 0;
232237
}
233-
} else if (c == '/' && flags & FNM_PATHNAME) {
238+
return (flags & FNM_LEADING_DIR) ||
239+
strchr(string, '/') == NULL
240+
? 0
241+
: FNM_NOMATCH;
242+
} else if (check_for_pathname(c, flags)) {
234243
string = strchr(string, '/');
235244
if (string == NULL) {
236245
return FNM_NOMATCH;
@@ -252,7 +261,7 @@ static int fnmatchx(const char *pattern, const char *string, int flags, size_t r
252261
return e;
253262
}
254263

255-
if (test == '/' && flags & FNM_PATHNAME) {
264+
if (check_for_pathname(test, flags)) {
256265
break;
257266
}
258267

@@ -265,13 +274,13 @@ static int fnmatchx(const char *pattern, const char *string, int flags, size_t r
265274
return FNM_NOMATCH;
266275
}
267276

268-
if (*string == '/' && flags & FNM_PATHNAME) {
277+
if (check_for_pathname(*string, flags)) {
269278
return FNM_NOMATCH;
270279
}
271280

272-
if (*string == '.' && (flags & FNM_PERIOD) &&
281+
if (check_fnm_period(string, flags) &&
273282
(string == stringstart ||
274-
((flags & FNM_PATHNAME) && *(string - 1) == '/'))) {
283+
check_for_pathname(*(string - 1), flags))) {
275284
return FNM_NOMATCH;
276285
}
277286

0 commit comments

Comments
 (0)