Skip to content

Commit

Permalink
Added regex to speedup things (-100ms).
Browse files Browse the repository at this point in the history
  • Loading branch information
esseks authored and Einar Lielmanis committed Sep 5, 2011
1 parent 5112b1c commit 67a3922
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions python/jsbeautifier/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -879,11 +879,12 @@ def handle_string(self, token_text):

# Try to replace readable \x-encoded characters with their equivalent,
# if it is possible (e.g. '\x41\x42\x43\x01' becomes 'ABC\x01').
if r'\x' in token_text:
for char in string.printable:
token_text = token_text.replace(r'\x%x' % ord(char), char)
token_text = token_text.replace(r'\x%X' % ord(char), char)
def unescape(match):
block, literal = match.group(0, 1)
char = chr(int(literal, 16))
return char if char in string.printable else block

token_text = re.sub(r'\\x([a-f0-9]{2})', unescape, token_text, flags=re.I)

self.append(token_text)

Expand Down

0 comments on commit 67a3922

Please sign in to comment.