Skip to content

Instantly share code, notes, and snippets.

@yaoxinghuo
Created January 15, 2014 01:31
Show Gist options
  • Save yaoxinghuo/8429225 to your computer and use it in GitHub Desktop.
Save yaoxinghuo/8429225 to your computer and use it in GitHub Desktop.
C通配符字符串比较
bool matchWildcards(const char *pattern, const char *content) {
// if we reatch both end of two string, we are done
if ('\0' == *pattern && '\0' == *content)
return true;
/* make sure that the characters after '*' are present in second string.
this function assumes that the first string will not contain two
consecutive '*'*/
if ('*' == *pattern && '\0' != *(pattern + 1) && '\0' == *content)
return false;
// if the first string contains '?', or current characters of both
// strings match
if ('?' == *pattern || *pattern == *content)
return matchWildcards(pattern + 1, content + 1);
/* if there is *, then there are two possibilities
a) We consider current character of second string
b) We ignore current character of second string.*/
if ('*' == *pattern)
return matchWildcards(pattern + 1, content) || matchWildcards(pattern, content + 1);
return false;
}
bool compareWildcards(const char *pattern,const char *content) {
if (NULL == pattern || NULL == content)
return NO;
return matchWildcards(pattern, content);
}
-(bool) compareWildcards:(NSString *)pattern pattern:(NSString *)content{
return compareWildcards([pattern UTF8String], [content UTF8String]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment