Slight optimization, not that it matters, but it makes the code easier to read, too.

This commit is contained in:
Jeremy Fincher 2004-04-09 15:30:09 +00:00
parent 4354a79f9f
commit f8b83f69f2

View File

@ -124,26 +124,27 @@ def isCtcp(msg):
msg.args[1].startswith('\x01') and \ msg.args[1].startswith('\x01') and \
msg.args[1].endswith('\x01') msg.args[1].endswith('\x01')
_match = fnmatch.fnmatchcase
_patternCache = {} _patternCache = {}
def _hostmaskPatternEqual(pattern, hostmask): def _hostmaskPatternEqual(pattern, hostmask):
"""Returns True if hostmask matches the hostmask pattern pattern.""" """Returns True if hostmask matches the hostmask pattern pattern."""
try: try:
return _patternCache[pattern](hostmask) is not None return _patternCache[pattern](hostmask) is not None
except KeyError: except KeyError:
# We make our own regexps, rather than use fnmatch, because fnmatch's
# case-insensitivity is not IRC's case-insensitity.
fd = sio() fd = sio()
for c in pattern: for c in pattern:
if c == '*': if c == '*':
fd.write('.*') fd.write('.*')
elif c == '?': elif c == '?':
fd.write('.') fd.write('.')
elif c == '[' or c == '{': elif c in '[{':
fd.write('[[{]') fd.write('[[{]')
elif c == '}' or c == ']': elif c in '}]':
fd.write(r'[}\]]') fd.write(r'[}\]]')
elif c == '|' or c == '\\': elif c in '|\\':
fd.write(r'[|\\]') fd.write(r'[|\\]')
elif c == '^' or c == '~': elif c in '^~':
fd.write('[~^]') fd.write('[~^]')
else: else:
fd.write(re.escape(c)) fd.write(re.escape(c))