Author: netski (By Spike)
authorJochen Meesters <spike@undernet.org>
Wed, 13 Aug 2003 15:01:44 +0000 (15:01 +0000)
committerJochen Meesters <spike@undernet.org>
Wed, 13 Aug 2003 15:01:44 +0000 (15:01 +0000)
Log message: Rewrite of ircd/match.c

git-svn-id: file:///home/klmitch/undernet-ircu/undernet-ircu-svn/ircu2/trunk@967 c9e4aea6-c8fd-4c43-8297-357d70d61c8c

ChangeLog
ircd/match.c

index a2d82f9f0db035b36a1ce6036dca1f08daeecdf7..363eda4de2220e58a49fbb5b512053129eaacc86 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2003-08-12 Timothy Vogelsang <net@astrolink.org>
+
+        * ircd/match.c: (match) rewrote function based on existing
+          code from the hybrid ircd -- death to goto
+
 2003-07-07  Bas Steendijk <steendijk@xs4all.nl>
 
         * ircd/s_user.c: invalidate ban cache for user on host hiding/account
index 0d43b574124d1117a30807b59cbbf0efb77af392..f4b808121c1bfa9db398399f93fe47d0329c1a91 100644 (file)
@@ -151,96 +151,68 @@ int mmatch(const char *old_mask, const char *new_mask)
  *
  * return  0, if match
  *         1, if no match
- */
-
-/*
- * match
- *
- * Rewritten by Andrea Cocito (Nemesi), November 1998.
  *
+ *  Originally by Douglas A Lewis (dalewis@acsu.buffalo.edu)
+ *  Rewritten by Timothy Vogelsang (netski), net@astrolink.org
  */
 
-/****************** Nemesi's match() ***************/
-
-int match(const char *mask, const char *string)
+int match(const char *mask, const char *name)
 {
-  const char *m = mask, *s = string;
-  char ch;
-  const char *bm, *bs;          /* Will be reg anyway on a decent CPU/compiler */
-
-  /* Process the "head" of the mask, if any */
-  while ((ch = *m++) && (ch != '*'))
-    switch (ch)
-    {
-      case '\\':
-        if (*m == '?' || *m == '*')
-          ch = *m++;
-      default:
-        if (ToLower(*s) != ToLower(ch))
-          return 1;
-      case '?':
-        if (!*s++)
-          return 1;
-    };
-  if (!ch)
-    return *s;
-
-  /* We got a star: quickly find if/where we match the next char */
-got_star:
-  bm = m;                       /* Next try rollback here */
-  while ((ch = *m++))
-    switch (ch)
-    {
-      case '?':
-        if (!*s++)
-          return 1;
-      case '*':
-        bm = m;
-        continue;               /* while */
-      case '\\':
-        if (*m == '?' || *m == '*')
-          ch = *m++;
-      default:
-        goto break_while;       /* C is structured ? */
-    };
-break_while:
-  if (!ch)
-    return 0;                   /* mask ends with '*', we got it */
-  ch = ToLower(ch);
-  while (ToLower(*s++) != ch)
-    if (!*s)
-      return 1;
-  bs = s;                       /* Next try start from here */
+  const char *m = mask, *n = name;
+  const char *m_tmp = mask, *n_tmp = name;
+  int wild = 0;
 
-  /* Check the rest of the "chunk" */
-  while ((ch = *m++))
-  {
-    switch (ch)
-    {
-      case '*':
-        goto got_star;
-      case '\\':
-        if (*m == '?' || *m == '*')
-          ch = *m++;
-      default:
-        if (ToLower(*s) != ToLower(ch))
-        {
-          m = bm;
-          s = bs;
-          goto got_star;
-        };
-      case '?':
-        if (!*s++)
-          return 1;
-    };
-  };
-  if (*s)
+  for (;;)
   {
-    m = bm;
-    s = bs;
-    goto got_star;
-  };
-  return 0;
+    if (*m == '*') {
+      while (*m == '*')  /* clean up any additional wildcards */
+        m++;
+
+      m_tmp = m;
+      n_tmp = n;
+      wild = 1;
+    }
+    if (*m == '\\')  /* next wildcard is disregarded */
+      *m++;
+
+    if (!*m) {
+      if (!*n)
+        return 0;  /* match */
+
+      for (m--; (m > mask) && (*m == '?'); m--);
+        ;
+
+      if (*m == '*' && (m > mask))
+        return 0;  /* match */
+
+      if (!wild)
+        return 1;
+
+      m = m_tmp;
+      n = ++n_tmp;
+    }
+    else if (!*n) {
+      while (*m == '*')  /* clean up any additional wildcards */
+        m++;
+
+      return (*m != 0);
+    }
+    if (tolower(*m) != tolower(*n) && *m != '?') {
+      if (!wild)
+        return 1;  /* failure! */
+
+      m = m_tmp;
+      n = ++n_tmp;
+    }
+    else {
+      if (*m)
+        m++;
+      if (*n)
+        n++;
+    }
+  }
+
+  return 1;  /* no match! */
 }
 
 /*