Cache SafeRe patterns

This commit is contained in:
shortcutme 2017-07-15 01:30:35 +02:00
parent d281f112d9
commit 0e930efd95
No known key found for this signature in database
GPG key ID: 5B63BAE6CB9613AE

View file

@ -4,6 +4,8 @@ import re
class UnsafePatternError(Exception):
pass
cached_patterns = {}
def isSafePattern(pattern):
if len(pattern) > 255:
@ -16,5 +18,10 @@ def isSafePattern(pattern):
def match(pattern, *args, **kwargs):
if isSafePattern(pattern):
return re.match(pattern, *args, **kwargs)
cached_pattern = cached_patterns.get(pattern)
if cached_pattern:
return cached_pattern.match(*args, **kwargs)
else:
if isSafePattern(pattern):
cached_patterns[pattern] = re.compile(pattern)
return cached_patterns[pattern].match(*args, **kwargs)