# HG changeset patch # User Matti Hamalainen # Date 1457521419 -7200 # Node ID 77fd4f0327dc8b2defe8d5f8b6bbe5a5456b9955 # Parent bb7120c11f8e66761e33aa16cece72f1c5cf018d Split th_str{case}match() into another file to reduce code duplication. diff -r bb7120c11f8e -r 77fd4f0327dc th_string.c --- a/th_string.c Wed Mar 09 13:03:05 2016 +0200 +++ b/th_string.c Wed Mar 09 13:03:39 2016 +0200 @@ -931,217 +931,16 @@ * wildcards ? and *. "?" matches any character and "*" matches * any number of characters. */ -BOOL th_strmatch(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 - do { - matched = FALSE; - switch (*pattern) - { - case '?': - // Any single character matches - if (*haystack) - { - matched = TRUE; - pattern++; - haystack++; - } - break; - - case '*': - matched = TRUE; - pattern++; - if (!*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 (*pattern == *haystack) - { - any = FALSE; - matched = TRUE; - } - else - { - if (*haystack) - { - matched = TRUE; - haystack++; - } - } - } - else - { - if (*pattern == *haystack) - { - matched = TRUE; - if (*pattern) - pattern++; - if (*haystack) - haystack++; - } - else - { - if (tmp) - { - matched = TRUE; - any = TRUE; - pattern = tmp; - } - } - } - - if (!*haystack && !*pattern) - end = TRUE; - break; - - } // switch - } while (matched && !end); - - return matched; -} +#define TH_STRMATCH_FUNC th_strmatch +#define TH_STRMATCH_COLLATE(px) (px) +#include "th_strmatch.c" /* Compare a string to a pattern. Case-INSENSITIVE version. */ -BOOL th_strcasematch(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 - do { - 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_tolower(*pattern) == th_tolower(*haystack)) - { - any = FALSE; - } - else - { - if (*haystack) - haystack++; - else - matched = FALSE; - } - } - else - { - if (th_tolower(*pattern) == th_tolower(*haystack)) - { - if (*pattern) - pattern++; - if (*haystack) - haystack++; - } - else - { - if (tmp) - { - any = TRUE; - pattern = tmp; - } - else - matched = FALSE; - } - } - - if (!*haystack && !*pattern) - end = TRUE; - - break; - - } // switch - } while (matched && !end); - - return matched; -} +#define TH_STRMATCH_FUNC th_strcasematch +#define TH_STRMATCH_COLLATE(px) th_tolower(px) +#include "th_strmatch.c" int th_get_hex_triplet(const char *str) diff -r bb7120c11f8e -r 77fd4f0327dc th_strmatch.c --- /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 +