]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/wildcard.cpp
Move this to another (yet more appropriate) place, saves some syscalls in an unlikely...
[user/henk/code/inspircd.git] / src / wildcard.cpp
index 3a91e8350ca5c51634216b6841d8622f399bd61a..b49ba943a6bc0d18f95fcd34c4417002fb6dd64f 100644 (file)
  * ---------------------------------------------------
  */
 
-/* $Core: libIRCDwildcard */
+/* $Core */
 
 #include "inspircd.h"
 #include "hashcomp.h"
 #include "inspstring.h"
 
-using irc::sockets::MatchCIDR;
-
-/* Rewritten to operate on more effective C++ std::string types
- * rather than char* to avoid data copies.
- * - Brain
+/*
+ * Wildcard matching!
+ *
+ *  Iteration 1)
+ *   Slow, horrible, etc.
+ *     Iteration 2)
+ *   The vastly available 'public domain' one
+ *     Iteration 3)
+ *   ZNC's, thought to be faster than ours, but it turned out that we could do better ;-)
+ *     Iteration 4)
+ *   Largely from work by peavey and myself (w00t) :)
+ *     Iteration 5)
+ *   peavey: Fix glob scan similar to 1.1, but scan ahead on glob in inner loop to retain speedup
+ *   this fixes another case which we forgot to test. Add early return for obvious fail condition.
  */
-
-CoreExport bool csmatch(const std::string &str, const std::string &mask)
+static bool match_internal(const unsigned char *string, const unsigned char *wild, unsigned const char *map)
 {
-       std::string::const_iterator cp, mp;
+       const unsigned char *s, *m; m = wild;
 
-       //unsigned char *cp = NULL, *mp = NULL;
-       //unsigned char* string = (unsigned char*)str;
-       //unsigned char* wild = (unsigned char*)mask;
-
-       std::string::const_iterator wild = mask.begin();
-       std::string::const_iterator string = str.begin();
-
-       if (mask.empty())
+       if (*string && !*wild)
                return false;
 
-       while ((string != str.end()) && (wild != mask.end()) && (*wild != '*'))
-       {
-               if ((*wild != *string) && (*wild != '?'))
-                       return 0;
-
-               wild++;
-               string++;
-       }
+       if (!map)
+               map = rfc_case_insensitive_map;
 
-       while (string != str.end())
+       while (*string)
        {
                if (*wild == '*')
                {
-                       if (++wild == mask.end())
-                               return 1;
-
-                       mp = wild;
-                       cp = string;
-                       cp++;
+                       while (*wild && *wild == '*')
+                               wild++;
+
+                       m = wild;
+
+                       if (!*wild)
+                               return true;
+                       else if (*wild != '?')
+                       {
+                               s = string;
+                               while (*s)
+                               {
+                                       if ((map[*wild] == map[*s]))
+                                       {
+                                               string = s;
+                                               if (*(wild+1) || !*(s+1))
+                                                       wild++;
+                                               break;
+                                       }
+                                       s++;
+                               }
+                       }
                }
-               else
-               if ((*wild == *string) || (*wild == '?'))
-               {
+               else if ( (map[*wild] == map[*string]) || (*wild == '?') )
                        wild++;
-                       string++;
-               }
                else
-               {
-                       wild = mp;
-                       string = cp++;
-               }
+                       wild = m;
 
+               string++;
        }
 
-       while ((wild != mask.end()) && (*wild == '*'))
+       while (*wild && *wild == '*')
                wild++;
 
-       return wild == mask.end();
+       return !*wild;
 }
 
-CoreExport bool match(const std::string &str, const std::string &mask)
-{
-       std::string::const_iterator cp, mp;
-       std::string::const_iterator wild = mask.begin();
-       std::string::const_iterator string = str.begin();
-
-       if (mask.empty())
-               return false;
-
-       while ((string != str.end()) && (wild != mask.end()) && (*wild != '*'))
-       {
-               if ((lowermap[(unsigned char)*wild] != lowermap[(unsigned char)*string]) && (*wild != '?'))
-                       return 0;
+/********************************************************************
+ * Below here is all wrappers around match_internal
+ ********************************************************************/
 
-               wild++;
-               string++;
-       }
-
-       while (string != str.end())
-       {
-               if (*wild == '*')
-               {
-                       if (++wild == mask.end())
-                               return 1;
-
-                       mp = wild;
-                       cp = string;
-                       cp++;
-               }
-               else
-               if ((lowermap[(unsigned char)*wild] == lowermap[(unsigned char)*string]) || (*wild == '?'))
-               {
-                       wild++;
-                       string++;
-               }
-               else
-               {
-                       wild = mp;
-                       string = cp++;
-               }
-
-       }
-
-       while ((wild != mask.end()) && (*wild == '*'))
-               wild++;
-
-       return wild == mask.end();
+CoreExport bool InspIRCd::Match(const std::string &str, const std::string &mask, unsigned const char *map)
+{
+       return match_internal((const unsigned char *)str.c_str(), (const unsigned char *)mask.c_str(), map);
 }
 
-/* Overloaded function that has the option of using cidr */
-CoreExport bool match(const std::string &str, const std::string &mask, bool use_cidr_match)
+CoreExport bool InspIRCd::Match(const  char *str, const char *mask, unsigned const char *map)
 {
-       if (use_cidr_match && MatchCIDR(str, mask, true))
-               return true;
-       return match(str, mask);
+       return match_internal((const unsigned char *)str, (const unsigned char *)mask, map);
 }
 
-CoreExport bool match(bool case_sensitive, const std::string &str, const std::string &mask, bool use_cidr_match)
+CoreExport bool InspIRCd::MatchCIDR(const std::string &str, const std::string &mask, unsigned const char *map)
 {
-       if (use_cidr_match && MatchCIDR(str, mask, true))
+       if (irc::sockets::MatchCIDR(str, mask, true))
                return true;
 
-       return case_sensitive ? csmatch(str, mask) : match(str, mask);
+       // Fall back to regular match
+       return InspIRCd::Match(str, mask, NULL);
 }
 
-CoreExport bool match(bool case_sensitive, const std::string &str, const std::string &mask)
+CoreExport bool InspIRCd::MatchCIDR(const  char *str, const char *mask, unsigned const char *map)
 {
-       return case_sensitive ? csmatch(str, mask) : match(str, mask);
+       if (irc::sockets::MatchCIDR(str, mask, true))
+               return true;
+
+       // Fall back to regular match
+       return InspIRCd::Match(str, mask, NULL);
 }