Skip to content

Instantly share code, notes, and snippets.

@b4284
Created May 31, 2016 06:57
Show Gist options
  • Select an option

  • Save b4284/20833d58ec0d0b31583f1f578cd93e6c to your computer and use it in GitHub Desktop.

Select an option

Save b4284/20833d58ec0d0b31583f1f578cd93e6c to your computer and use it in GitHub Desktop.
bool StringMatch(const char *str, size_t strn,
const char *pat, size_t patn)
{
size_t stri = 0;
for (size_t i = 0; i < patn; ++i) {
switch (pat[i]) {
case '?':
if (stri < strn) {
stri += 1;
continue;
}
break;
case '*':
if (i == patn - 1) {
return true;
} else {
const char *strtmp = strchr(str + stri, pat[i + 1]);
while (strtmp != NULL) {
if (StringMatch(strtmp, strn - (strtmp - str),
pat + i + 1, patn - i - 1))
{
return true;
} else {
strtmp = strchr(strtmp + 1, pat[i + 1]);
continue;
}
}
return false;
}
break;
default:
if (pat[i] == str[stri]) {
stri += 1;
continue;
} else {
return false;
}
break;
}
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment