Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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>

authored by

Du, Changbin and committed by
Linus Torvalds
aace0509 0d9dfc23

+52
+1
include/linux/parser.h
··· 29 29 int match_int(substring_t *, int *result); 30 30 int match_octal(substring_t *, int *result); 31 31 int match_hex(substring_t *, int *result); 32 + bool match_wildcard(const char *pattern, const char *str); 32 33 size_t match_strlcpy(char *, const substring_t *, size_t); 33 34 char *match_strdup(const substring_t *);
+51
lib/parser.c
··· 193 193 } 194 194 195 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 + 245 + /** 196 246 * match_strlcpy: - Copy the characters from a substring_t to a sized buffer 197 247 * @dest: where to copy to 198 248 * @src: &substring_t to copy ··· 285 235 EXPORT_SYMBOL(match_int); 286 236 EXPORT_SYMBOL(match_octal); 287 237 EXPORT_SYMBOL(match_hex); 238 + EXPORT_SYMBOL(match_wildcard); 288 239 EXPORT_SYMBOL(match_strlcpy); 289 240 EXPORT_SYMBOL(match_strdup);