From 6925ecf760bc42914eb62c223507ccfff2ec71f5 Mon Sep 17 00:00:00 2001 From: Jeremy Fincher Date: Wed, 3 Dec 2003 23:13:07 +0000 Subject: [PATCH] New hostmaskPatternEqual that's a bit more correct and perhaps slightly faster. --- src/ircutils.py | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/src/ircutils.py b/src/ircutils.py index 3dc0643ec..a95f1060e 100644 --- a/src/ircutils.py +++ b/src/ircutils.py @@ -49,6 +49,7 @@ import string import fnmatch import operator from itertools import imap +from cStringIO import StringIO as sio def isUserHostmask(s): """Returns whether or not the string s is a valid User hostmask.""" @@ -116,9 +117,33 @@ def isChannel(s): '\x07' not in s and ',' not in s and ' ' not in s) _match = fnmatch.fnmatchcase + +_patternCache = {} def hostmaskPatternEqual(pattern, hostmask): """Returns True if hostmask matches the hostmask pattern pattern.""" - return _match(toLower(hostmask), toLower(pattern)) + try: + return bool(_patternCache[pattern](hostmask)) + except KeyError: + fd = sio() + for c in pattern: + if c == '*': + fd.write('.*') + elif c == '?': + fd.write('.') + elif c == '[' or c == '{': + fd.write('[[{]') + elif c == '}' or c == ']': + fd.write(r'[}\]]') + elif c == '|' or c == '\\': + fd.write(r'[|\\]') + elif c == '^' or c == '~': + fd.write('[~^]') + else: + fd.write(re.escape(c)) + fd.write('$') + f = re.compile(fd.getvalue(), re.I).match + _patternCache[pattern] = f + return bool(f(hostmask)) _ipchars = string.digits + '.' def isIP(s):