Added character class matching to trivimatch; renamed to simplematch
authoroes <oes@users.sourceforge.net>
Fri, 1 Jun 2001 10:31:51 +0000 (10:31 +0000)
committeroes <oes@users.sourceforge.net>
Fri, 1 Jun 2001 10:31:51 +0000 (10:31 +0000)
miscutil.c
miscutil.h

index c3c270c..8e169fa 100644 (file)
@@ -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 $
 /*********************************************************************
  *
  * 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 $
  *
  * 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
  *
  *
  *    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
  *
  * 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
  *
  *********************************************************************/
  * 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)
    {
   
    while (*txt)
    {
+
       /* EOF pattern but !EOF text? */
       if (*pat == '\0')
       {
       /* EOF pattern but !EOF text? */
       if (*pat == '\0')
       {
@@ -428,46 +437,78 @@ int trivimatch(char *pattern, char *text)
       /* '*' in the pattern?  */
       if (*pat == '*') 
       {
       /* '*' in the pattern?  */
       if (*pat == '*') 
       {
-
+     
          /* The pattern ends afterwards? Speed up the return. */
          if (*++pat == '\0')
          {
             return 0;
          }
          /* 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;
       }
 
          /* 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)
          {
          if(wildcard)
          {
-            /* Without wildcard mode, this is fatal! */
             pat = fallback;
          }
 
             pat = fallback;
          }
 
-         /* Bad luck otherwise */
+         /* Else, bad luck */
          else
          {
             return 1;
          }
       }
          else
          {
             return 1;
          }
       }
-      /* We had a match, advance */
-      else
-      {
-         pat++;
-      }
       txt++;
       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);
 
 }
 
 
 }
 
index 02a54d2..21424e8 100644 (file)
@@ -1,6 +1,6 @@
 #ifndef _MISCUTIL_H
 #define _MISCUTIL_H
 #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 $
 /*********************************************************************
  *
  * File        :  $Source: /cvsroot/ijbswa/current/miscutil.h,v $
  *
  * Revisions   :
  *    $Log: miscutil.h,v $
  *
  * 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
  *
  *
  *    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 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);
 
 #ifdef __MINGW32__
 extern char *strdup(const char *s);