]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/wildcard.cpp
Our optimizations missed a possibility :p
[user/henk/code/inspircd.git] / src / wildcard.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
6  *                       E-mail:
7  *                <brain@chatspike.net>
8  *                <Craig@chatspike.net>
9  *     
10  * Written by Craig Edwards, Craig McLure, and others.
11  * This program is free but copyrighted software; see
12  *            the file COPYING for details.
13  *
14  * ---------------------------------------------------
15  */
16
17 using namespace std;
18
19 #include <string>
20 #include "inspircd_config.h"
21 #include "inspircd.h"
22 #include "helperfuncs.h"
23 #include "inspstring.h"
24
25 extern char lowermap[255];
26
27 // Wed 27 Apr 2005 - Brain
28 // I've taken our our old wildcard routine -
29 // although comprehensive, it was topheavy and very
30 // slow, and ate masses of cpu when doing lots of
31 // comparisons. This is the 'de-facto' routine used
32 // by many, nobody really knows who wrote it first
33 // or what license its under, i've seen examples of it
34 // (unattributed to any author) all over the 'net.
35 // For now, we'll just consider this public domain.
36
37 int wildcmp(char *wild, char *string)
38 {
39         char *cp, *mp;
40         while ((*string) && (*wild != '*'))
41         {
42                 if ((lowermap[(unsigned)*wild] != lowermap[(unsigned)*string]) && (*wild != '?'))
43                 {
44                         return 0;
45                 }
46                 wild++;
47                 string++;
48         }
49
50         while (*string)
51         {
52                 if (*wild == '*')
53                 {
54                         if (!*++wild)
55                         {
56                                 return 1;
57                         }
58                         mp = wild;
59                         cp = string+1;
60                 }
61                 else
62                 if ((lowermap[(unsigned)*wild] == lowermap[(unsigned)*string]) || (*wild == '?'))
63                 {
64                         wild++;
65                         string++;
66                 }
67                 else
68                 {
69                         wild = mp;
70                         string = cp++;
71                 }
72
73         }
74
75         while (*wild == '*')
76         {
77                 wild++;
78         }
79
80         return !*wild;
81 }
82
83 // This wrapper function is required to convert both
84 // strings to 'scandanavian lowercase' and make copies
85 // of them to a safe location. It also ensures we don't
86 // bite off more than we can chew with the length of
87 // the string.
88
89 bool match(const char* literal, const char* mask)
90 {
91         return wildcmp((char*)mask, (char*)literal);
92 }
93