lib/parser.c: add match_wildcard() function
authorDu, Changbin <changbin.du@gmail.com>
Thu, 23 Jan 2014 23:54:12 +0000 (15:54 -0800)
committerLinus Torvalds <torvalds@linux-foundation.org>
Fri, 24 Jan 2014 00:36:55 +0000 (16:36 -0800)
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>
include/linux/parser.h
lib/parser.c

index ea2281e726f65f5f2968eb571adc874843530959..39d5b7955b23f9a02c80904804c996180de060ac 100644 (file)
@@ -29,5 +29,6 @@ int match_token(char *, const match_table_t table, substring_t args[]);
 int match_int(substring_t *, int *result);
 int match_octal(substring_t *, int *result);
 int match_hex(substring_t *, int *result);
+bool match_wildcard(const char *pattern, const char *str);
 size_t match_strlcpy(char *, const substring_t *, size_t);
 char *match_strdup(const substring_t *);
index 807b2aaa33fa42488c88e1adff522fa17b4368f3..ee5295541cea8f4435ef24dbd431d70f78722d1f 100644 (file)
@@ -192,6 +192,56 @@ int match_hex(substring_t *s, int *result)
        return match_number(s, result, 16);
 }
 
+/**
+ * match_wildcard: - parse if a string matches given wildcard pattern
+ * @pattern: wildcard pattern
+ * @str: the string to be parsed
+ *
+ * Description: Parse the string @str to check if matches wildcard
+ * pattern @pattern. The pattern may contain two type wildcardes:
+ *   '*' - matches zero or more characters
+ *   '?' - matches one character
+ * If it's matched, return true, else return false.
+ */
+bool match_wildcard(const char *pattern, const char *str)
+{
+       const char *s = str;
+       const char *p = pattern;
+       bool star = false;
+
+       while (*s) {
+               switch (*p) {
+               case '?':
+                       s++;
+                       p++;
+                       break;
+               case '*':
+                       star = true;
+                       str = s;
+                       if (!*++p)
+                               return true;
+                       pattern = p;
+                       break;
+               default:
+                       if (*s == *p) {
+                               s++;
+                               p++;
+                       } else {
+                               if (!star)
+                                       return false;
+                               str++;
+                               s = str;
+                               p = pattern;
+                       }
+                       break;
+               }
+       }
+
+       if (*p == '*')
+               ++p;
+       return !*p;
+}
+
 /**
  * match_strlcpy: - Copy the characters from a substring_t to a sized buffer
  * @dest: where to copy to
@@ -235,5 +285,6 @@ EXPORT_SYMBOL(match_token);
 EXPORT_SYMBOL(match_int);
 EXPORT_SYMBOL(match_octal);
 EXPORT_SYMBOL(match_hex);
+EXPORT_SYMBOL(match_wildcard);
 EXPORT_SYMBOL(match_strlcpy);
 EXPORT_SYMBOL(match_strdup);