Skip to content

Commit

Permalink
StringProcessing: Compile important pattern once
Browse files Browse the repository at this point in the history
By moving LAST_POSITION_UNESCAPED_PATTERN to the module level, it is compiled only once on module import. Before, it was compiled on every call of the position_escaped function
  • Loading branch information
Fabian Neuschmidt committed Mar 30, 2015
1 parent ce0e70a commit 1e8d624
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions coalib/parsing/StringProcessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,9 @@ def unescaped_search_in_between(begin,
elem.group(compiled_begin_pattern.groups + 2))


LAST_POSITION_UNESCAPED_PATTERN = re.compile(r"(?<!\\)(?:\\\\)*$")


def position_escaped(string, position):
"""
Checks whether a char at a specific position of the string is escaped by an
Expand All @@ -308,8 +311,7 @@ def position_escaped(string, position):
if not isinstance(position, int) or not 0 <= position < len(string):
raise ValueError("param position is not an int in the valid range")

pat = re.compile(r"(?<!\\)(?:\\\\)*$")
return not pat.search(string[:position])
return not LAST_POSITION_UNESCAPED_PATTERN.search(string[:position])


def unescaped_find(string, sub, start=None, end=None):
Expand Down

0 comments on commit 1e8d624

Please sign in to comment.