Skip to content

Commit

Permalink
---
Browse files Browse the repository at this point in the history
yaml
---
r: 182354
b: refs/heads/master
c: 6964cd2
h: refs/heads/master
v: v3
  • Loading branch information
Masami Hiramatsu authored and Ingo Molnar committed Jan 13, 2010
1 parent 04115d5 commit 6e23c78
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 5 deletions.
2 changes: 1 addition & 1 deletion [refs]
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
---
refs/heads/master: fb1d2edf7ee25a26ad0b238d0ee335a3b28b7aa3
refs/heads/master: 6964cd2c8efe6e048401f1fe3952a06c563c34c1
65 changes: 61 additions & 4 deletions trunk/tools/perf/util/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -227,16 +227,73 @@ char **argv_split(const char *str, int *argcp)
return NULL;
}

/* Glob expression pattern matching */
/* Character class matching */
static bool __match_charclass(const char *pat, char c, const char **npat)
{
bool complement = false, ret = true;

if (*pat == '!') {
complement = true;
pat++;
}
if (*pat++ == c) /* First character is special */
goto end;

while (*pat && *pat != ']') { /* Matching */
if (*pat == '-' && *(pat + 1) != ']') { /* Range */
if (*(pat - 1) <= c && c <= *(pat + 1))
goto end;
if (*(pat - 1) > *(pat + 1))
goto error;
pat += 2;
} else if (*pat++ == c)
goto end;
}
if (!*pat)
goto error;
ret = false;

end:
while (*pat && *pat != ']') /* Searching closing */
pat++;
if (!*pat)
goto error;
*npat = pat + 1;
return complement ? !ret : ret;

error:
return false;
}

/**
* strglobmatch - glob expression pattern matching
* @str: the target string to match
* @pat: the pattern string to match
*
* This returns true if the @str matches @pat. @pat can includes wildcards
* ('*','?') and character classes ([CHARS], complementation and ranges are
* also supported). Also, this supports escape character ('\') to use special
* characters as normal character.
*
* Note: if @pat syntax is broken, this always returns false.
*/
bool strglobmatch(const char *str, const char *pat)
{
while (*str && *pat && *pat != '*') {
if (*pat == '?') {
if (*pat == '?') { /* Matches any single character */
str++;
pat++;
} else
if (*str++ != *pat++)
continue;
} else if (*pat == '[') /* Character classes/Ranges */
if (__match_charclass(pat + 1, *str, &pat)) {
str++;
continue;
} else
return false;
else if (*pat == '\\') /* Escaped char match as normal char */
pat++;
if (*str++ != *pat++)
return false;
}
/* Check wild card */
if (*pat == '*') {
Expand Down

0 comments on commit 6e23c78

Please sign in to comment.