//字符串匹配,匹配返回 1,否则返回 0.
//pattern可以通过任意个 | 字符,组合match_one中pattern的功能
int
match(const char* pattern, const char* string)
{
const char* or;
for (;;)
{
or = strchr(pattern, '|');
if (or == (char*))
return match_one(pattern, strlen(pattern), string);
if (match_one(pattern, or - pattern, string))
return ;
pattern = or + ;
}
}
/* 对于pattern中的字符:
?:匹配任意单个字符
**:匹配任意长度字符串
*:匹配任意长度字符串,直到遇到 /
*/
static int
match_one(const char* pattern, int patternlen, const char* string)
{
const char* p;
for (p = pattern; p - pattern < patternlen; ++p, ++string)
{
if (*p == '?' && *string != '\0')
continue;
if (*p == '*')
{
int i, pl;
++p;
if (*p == '*')
{
/* Double-wildcard matches anything. */
++p;
i = strlen(string);
}
else
/* Single-wildcard matches anything but slash. */
i = strcspn(string, "/");
pl = patternlen - (p - pattern);
for (; i >= ; --i)
if (match_one(p, pl, &(string[i])))
return ;
return ;
}
if (*p != *string)
return ;
}
if (*string == '\0')
return ;
return ;
}