Skip to content

Commit aace050

Browse files
changbindutorvalds
authored andcommitted
lib/parser.c: add match_wildcard() function
match_wildcard function is a simple implementation of wildcard matching algorithm. It only supports two usual wildcardes: '*' - matches zero or more characters '?' - matches one character This algorithm is safe since it is non-recursive. Signed-off-by: Du, Changbin <changbin.du@gmail.com> Cc: Jason Baron <jbaron@akamai.com> Cc: Joe Perches <joe@perches.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 0d9dfc2 commit aace050

2 files changed

Lines changed: 52 additions & 0 deletions

File tree

‎include/linux/parser.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,6 @@ int match_token(char *, const match_table_t table, substring_t args[]);
2929
int match_int(substring_t *, int *result);
3030
int match_octal(substring_t *, int *result);
3131
int match_hex(substring_t *, int *result);
32+
bool match_wildcard(const char *pattern, const char *str);
3233
size_t match_strlcpy(char *, const substring_t *, size_t);
3334
char *match_strdup(const substring_t *);

‎lib/parser.c‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,56 @@ int match_hex(substring_t *s, int *result)
192192
return match_number(s, result, 16);
193193
}
194194

195+
/**
196+
* match_wildcard: - parse if a string matches given wildcard pattern
197+
* @pattern: wildcard pattern
198+
* @str: the string to be parsed
199+
*
200+
* Description: Parse the string @str to check if matches wildcard
201+
* pattern @pattern. The pattern may contain two type wildcardes:
202+
* '*' - matches zero or more characters
203+
* '?' - matches one character
204+
* If it's matched, return true, else return false.
205+
*/
206+
bool match_wildcard(const char *pattern, const char *str)
207+
{
208+
const char *s = str;
209+
const char *p = pattern;
210+
bool star = false;
211+
212+
while (*s) {
213+
switch (*p) {
214+
case '?':
215+
s++;
216+
p++;
217+
break;
218+
case '*':
219+
star = true;
220+
str = s;
221+
if (!*++p)
222+
return true;
223+
pattern = p;
224+
break;
225+
default:
226+
if (*s == *p) {
227+
s++;
228+
p++;
229+
} else {
230+
if (!star)
231+
return false;
232+
str++;
233+
s = str;
234+
p = pattern;
235+
}
236+
break;
237+
}
238+
}
239+
240+
if (*p == '*')
241+
++p;
242+
return !*p;
243+
}
244+
195245
/**
196246
* match_strlcpy: - Copy the characters from a substring_t to a sized buffer
197247
* @dest: where to copy to
@@ -235,5 +285,6 @@ EXPORT_SYMBOL(match_token);
235285
EXPORT_SYMBOL(match_int);
236286
EXPORT_SYMBOL(match_octal);
237287
EXPORT_SYMBOL(match_hex);
288+
EXPORT_SYMBOL(match_wildcard);
238289
EXPORT_SYMBOL(match_strlcpy);
239290
EXPORT_SYMBOL(match_strdup);

0 commit comments

Comments
 (0)