diff options
author | Stefan Behnel <stefan_ml@behnel.de> | 2013-12-19 21:32:04 -0800 |
---|---|---|
committer | Stefan Behnel <stefan_ml@behnel.de> | 2013-12-19 21:32:04 -0800 |
commit | 202dc3f0f0bd57006550c8dbcaf66bcbd96e5ae8 (patch) | |
tree | 1a61809eee5f28cbe402feb5f0bbb43b8ca3b7cf | |
parent | 939b997c635f07bec52676c3e564e044db07f32f (diff) | |
download | tempita-202dc3f0f0bd57006550c8dbcaf66bcbd96e5ae8.tar.gz |
small performance patch for Tempita
-rw-r--r-- | tempita/__init__.py | 18 |
1 files changed, 12 insertions, 6 deletions
diff --git a/tempita/__init__.py b/tempita/__init__.py index 467bd5b..5928a0d 100644 --- a/tempita/__init__.py +++ b/tempita/__init__.py @@ -659,12 +659,12 @@ def lex(s, name=None, trim_whitespace=True, line_offset=0, delimiters=None): in_expr = False chunks = [] last = 0 - last_pos = (1, 1) + last_pos = (line_offset + 1, 1) token_re = re.compile(r'%s|%s' % (re.escape(delimiters[0]), re.escape(delimiters[1]))) for match in token_re.finditer(s): expr = match.group(0) - pos = find_position(s, match.end(), line_offset) + pos = find_position(s, match.end(), line_offset, last_pos) if expr == delimiters[0] and in_expr: raise TemplateError('%s inside expression' % delimiters[0], position=pos, @@ -758,10 +758,16 @@ def trim_lex(tokens): return tokens -def find_position(string, index, line_offset): - """Given a string and index, return (line, column)""" - leading = string[:index].splitlines() - return (len(leading) + line_offset, len(leading[-1]) + 1) +def find_position(string, index, last_index, last_pos=(1, 1)): + """ + Given a string and index, return (line, column) + """ + lines = string.count('\n', last_index, index) + if lines > 0: + column = index - string.rfind('\n', last_index, index) + else: + column = last_pos[1] + (index - last_index) + return (last_pos[0] + lines, column) def parse(s, name=None, line_offset=0, delimiters=None): |