From: oes Date: Fri, 1 Jun 2001 10:31:51 +0000 (+0000) Subject: Added character class matching to trivimatch; renamed to simplematch X-Git-Tag: v_2_9_9~431 X-Git-Url: http://www.privoxy.org/gitweb/?p=privoxy.git;a=commitdiff_plain;h=f10e5df2fa4a1a5d7e53a354bf9196560e1bf5b2 Added character class matching to trivimatch; renamed to simplematch --- diff --git a/miscutil.c b/miscutil.c index c3c270c5..8e169fa6 100644 --- a/miscutil.c +++ b/miscutil.c @@ -1,4 +1,4 @@ -const char miscutil_rcs[] = "$Id: miscutil.c,v 1.3 2001/05/29 23:10:09 oes Exp $"; +const char miscutil_rcs[] = "$Id: miscutil.c,v 1.4 2001/05/31 17:32:31 oes Exp $"; /********************************************************************* * * File : $Source: /cvsroot/ijbswa/current/miscutil.c,v $ @@ -36,6 +36,11 @@ const char miscutil_rcs[] = "$Id: miscutil.c,v 1.3 2001/05/29 23:10:09 oes Exp $ * * Revisions : * $Log: miscutil.c,v $ + * Revision 1.4 2001/05/31 17:32:31 oes + * + * - Enhanced domain part globbing with infix and prefix asterisk + * matching and optional unanchored operation + * * Revision 1.3 2001/05/29 23:10:09 oes * * @@ -394,14 +399,12 @@ char *strsav(char *old, const char *text_to_append) /********************************************************************* * - * Function : trivimatch - * - * Description : Trivial string matching, with only one metacharacter, - * namely '*', which stands for zero or more arbitrary - * characters. + * Function : simplematch * - * Note: The * is greedy, i.e. it will try to match as - * much text es possible. + * Description : String matching, with a (greedy) '*' wildcard that + * stands for zero or more arbitrary characters and + * character classes in [], which take both enumerations + * and ranges. * * Parameters : * 1 : pattern = pattern for matching @@ -410,15 +413,21 @@ char *strsav(char *old, const char *text_to_append) * Returns : 0 if match, else nonzero * *********************************************************************/ -int trivimatch(char *pattern, char *text) +int simplematch(char *pattern, char *text) { - char *fallback; - char *pat = pattern; - char *txt = text; - int wildcard = 0; + char *fallback; + char *pat = pattern; + char *txt = text; + int wildcard = 0; + + char lastchar = 'a'; + unsigned i; + unsigned char charmap[32]; + while (*txt) { + /* EOF pattern but !EOF text? */ if (*pat == '\0') { @@ -428,46 +437,78 @@ int trivimatch(char *pattern, char *text) /* '*' in the pattern? */ if (*pat == '*') { - + /* The pattern ends afterwards? Speed up the return. */ if (*++pat == '\0') { return 0; } - + /* Else, set wildcard mode and remember position after '*' */ wildcard = 1; fallback = pat; } - /* Compare: */ - if (*pat != *txt) + /* Character range specification? */ + if (*pat == '[') { - /* In wildcard mode, just try again */ + memset(charmap, '\0', sizeof(charmap)); + + while (*++pat != ']') + { + if (!*pat) + { + return 1; + } + else if (*pat == '-') + { + if ((*++pat == ']') || *pat == '\0') + { + return(1); + } + for(i = lastchar; i <= *pat; i++) + { + charmap[i / 8] |= (1 << (i % 8)); + } + } + else + { + charmap[*pat / 8] |= (1 << (*pat % 8)); + lastchar = *pat; + } + } + } /* -END- if Character range specification */ + + + /* Compare: Char match, or char range match*/ + if ((*pat == *txt) + || ((*pat == ']') && (charmap[*txt / 8] & (1 << (*txt % 8)))) ) + { + /* Sucess, go ahead */ + pat++; + } + else + { + /* In wildcard mode, just try again after failiure */ if(wildcard) { - /* Without wildcard mode, this is fatal! */ pat = fallback; } - /* Bad luck otherwise */ + /* Else, bad luck */ else { return 1; } } - /* We had a match, advance */ - else - { - pat++; - } txt++; - } + } - if(*pat == '*') pat++; + /* Cut off extra '*'s */ + if(*pat == '*') pat++; - /* Hey, we've made it all the way through! */ - return(*pat); + /* If this is the pattern's end, fine! */ + return(*pat); } diff --git a/miscutil.h b/miscutil.h index 02a54d22..21424e8c 100644 --- a/miscutil.h +++ b/miscutil.h @@ -1,6 +1,6 @@ #ifndef _MISCUTIL_H #define _MISCUTIL_H -#define MISCUTIL_H_VERSION "$Id: miscutil.h,v 1.3 2001/05/29 23:10:09 oes Exp $" +#define MISCUTIL_H_VERSION "$Id: miscutil.h,v 1.4 2001/05/31 17:32:31 oes Exp $" /********************************************************************* * * File : $Source: /cvsroot/ijbswa/current/miscutil.h,v $ @@ -37,6 +37,11 @@ * * Revisions : * $Log: miscutil.h,v $ + * Revision 1.4 2001/05/31 17:32:31 oes + * + * - Enhanced domain part globbing with infix and prefix asterisk + * matching and optional unanchored operation + * * Revision 1.3 2001/05/29 23:10:09 oes * * @@ -89,7 +94,7 @@ extern int strncmpic(const char *s1, const char *s2, size_t n); extern char *strsav(char *old, const char *text_to_append); extern char *chomp(char *string); -extern int trivimatch(char *pattern, char *text); +extern int simplematch(char *pattern, char *text); #ifdef __MINGW32__ extern char *strdup(const char *s);