1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2008 InspIRCd Development Team
6 * See: http://www.inspircd.org/wiki/index.php/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
18 #include "inspstring.h"
20 using irc::sockets::MatchCIDR;
22 /* Rewritten to operate on more effective C++ std::string types
23 * rather than char* to avoid data copies.
27 CoreExport bool csmatch(const std::string &str, const std::string &mask)
29 std::string::const_iterator cp, mp;
31 //unsigned char *cp = NULL, *mp = NULL;
32 //unsigned char* string = (unsigned char*)str;
33 //unsigned char* wild = (unsigned char*)mask;
35 std::string::const_iterator wild = mask.begin();
36 std::string::const_iterator string = str.begin();
41 while ((string != str.end()) && (wild != mask.end()) && (*wild != '*'))
43 if ((*wild != *string) && (*wild != '?'))
50 if (wild == mask.end() && string != str.end())
53 while (string != str.end())
55 if (wild != mask.end() && *wild == '*')
57 if (++wild == mask.end())
67 if ((string != str.end() && wild != mask.end()) && ((*wild == *string) || (*wild == '?')))
83 while ((wild != mask.end()) && (*wild == '*'))
86 return wild == mask.end();
89 CoreExport bool match(const std::string &str, const std::string &mask)
91 std::string::const_iterator cp, mp;
92 std::string::const_iterator wild = mask.begin();
93 std::string::const_iterator string = str.begin();
98 while ((string != str.end()) && (wild != mask.end()) && (*wild != '*'))
100 if ((lowermap[(unsigned char)*wild] != lowermap[(unsigned char)*string]) && (*wild != '?'))
105 //printf("Iterate first loop\n");
108 if (wild == mask.end() && string != str.end())
111 while (string != str.end())
113 //printf("outer\n %c", *string);
114 if (wild != mask.end() && *wild == '*')
117 //printf("inner %c\n", *wild);
118 if (++wild == mask.end())
129 if ((string != str.end() && wild != mask.end()) && ((lowermap[(unsigned char)*wild] == lowermap[(unsigned char)*string]) || (*wild == '?')))
131 if (wild != mask.end())
134 if (string != str.end())
148 while ((wild != mask.end()) && (*wild == '*'))
151 return wild == mask.end();
154 /* Overloaded function that has the option of using cidr */
155 CoreExport bool match(const std::string &str, const std::string &mask, bool use_cidr_match)
157 if (use_cidr_match && MatchCIDR(str, mask, true))
159 return match(str, mask);
162 CoreExport bool match(bool case_sensitive, const std::string &str, const std::string &mask, bool use_cidr_match)
164 if (use_cidr_match && MatchCIDR(str, mask, true))
167 return case_sensitive ? csmatch(str, mask) : match(str, mask);
170 CoreExport bool match(bool case_sensitive, const std::string &str, const std::string &mask)
172 return case_sensitive ? csmatch(str, mask) : match(str, mask);