Mercurial > hg > th-libs
diff th_strmatch.c @ 414:77fd4f0327dc
Split th_str{case}match() into another file to reduce code duplication.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Wed, 09 Mar 2016 13:03:39 +0200 |
parents | |
children | 1b3472ba7b23 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/th_strmatch.c Wed Mar 09 13:03:39 2016 +0200 @@ -0,0 +1,108 @@ +/* + * String glob match implementation + * Programmed and designed by Matti 'ccr' Hamalainen + * (C) Copyright 2002-2016 Tecnic Software productions (TNSP) + * + * Please read file 'COPYING' for information on license and distribution. + */ + +BOOL TH_STRMATCH_FUNC (const char *haystack, const char *pattern) +{ + BOOL matched = TRUE, any = FALSE, end = FALSE; + const char *tmp = NULL; + + // Check given pattern and string + if (haystack == NULL || pattern == NULL) + return FALSE; + + // Start comparision + while (matched && !end) + switch (*pattern) + { + case '?': + // Any single character matches + if (*haystack) + { + pattern++; + haystack++; + } + else + matched = FALSE; + break; + + case '*': + pattern++; + if (!*pattern || *pattern == '?') + end = TRUE; + any = TRUE; + tmp = pattern; + break; + + case 0: + if (any) + { + if (*haystack) + haystack++; + else + end = TRUE; + } + else + if (*haystack) + { + if (tmp) + { + any = TRUE; + pattern = tmp; + } + else + matched = FALSE; + } + else + end = TRUE; + break; + + default: + if (any) + { + if (TH_STRMATCH_COLLATE(*pattern) == TH_STRMATCH_COLLATE(*haystack)) + { + any = FALSE; + } + else + if (*haystack) + haystack++; + else + matched = FALSE; + } + else + { + if (TH_STRMATCH_COLLATE(*pattern) == TH_STRMATCH_COLLATE(*haystack)) + { + if (*pattern) + pattern++; + if (*haystack) + haystack++; + } + else + if (tmp) + { + any = TRUE; + pattern = tmp; + } + else + matched = FALSE; + } + + if (!*haystack && !*pattern) + end = TRUE; + + break; + } + + return matched; +} + + +#undef TH_STRMATCH_FUNC +#undef TH_STRMATCH_COLLATE +