Skip to content

Instantly share code, notes, and snippets.

@klopp
Created September 25, 2018 09:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save klopp/2029c794bef737aeab7117b94d41e831 to your computer and use it in GitHub Desktop.
Save klopp/2029c794bef737aeab7117b94d41e831 to your computer and use it in GitHub Desktop.
static bool match_here(const char* mask, const char* text);
static inline bool match_star(int c, const char* mask, const char* text)
{
do {
if(match_here(mask, text))
return true;
} while(*text != '\0' && (*text++ == c || c == '?'));
return false;
}
static bool match_here(const char* mask, const char* text)
{
if(mask[0] == '\0')
return true;
int state = 0;
if(mask[0] == '\\') {
++mask;
/*
* Спорно. Проверка нужна, но что возвращать?
* Речь о том, что маска неожиданно закончилась вот так:
* "deafbeef\". Предполагаем, что заэскейпили концевой 0
* и всё нормально?
*/
if(mask[0] == '\0') return true;
}
else {
state = mask[0];
}
if(mask[1] == '*' && mask[0] != '\\')
return mask[2] == '\0' ? true : match_star(mask[0], mask + 2, text);
if(state == '$' && mask[1] == '\0')
return *text == '\0';
if(*text != '\0' && (state == '?' || mask[0] == *text))
return mask[1] == '\0' ? true : match_here(mask + 1, text + 1);
return false;
}
bool match(const char *text, const char *mask)
{
if(mask[0] == '^')
return match_here(mask + 1, text);
do {
if(match_here(mask, text))
return true;
} while(*text++ != '\0');
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment