Skip to content

Commit

Permalink
Cache SafeRe patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
shortcutme committed Jul 14, 2017
1 parent d281f11 commit 0e930ef
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/util/SafeRe.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
class UnsafePatternError(Exception):
pass

cached_patterns = {}


def isSafePattern(pattern):
if len(pattern) > 255:
Expand All @@ -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)

0 comments on commit 0e930ef

Please sign in to comment.