Skip to content

Commit bac3c51

Browse files
committed
Improve performance of reindent engine a bit (targets issue41).
1 parent c7dd265 commit bac3c51

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

sqlparse/filters.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -250,10 +250,17 @@ def __init__(self, width=2, char=' ', line_width=None):
250250
self._curr_stmt = None
251251
self._last_stmt = None
252252

253+
def _flatten_up_to_token(self, token):
254+
"""Yields all tokens up to token plus the next one."""
255+
# helper for _get_offset
256+
iterator = self._curr_stmt.flatten()
257+
for t in iterator:
258+
yield t
259+
if t == token:
260+
raise StopIteration
261+
253262
def _get_offset(self, token):
254-
all_ = list(self._curr_stmt.flatten())
255-
idx = all_.index(token)
256-
raw = ''.join(unicode(x) for x in all_[:idx + 1])
263+
raw = ''.join(map(unicode, self._flatten_up_to_token(token)))
257264
line = raw.splitlines()[-1]
258265
# Now take current offset into account and return relative offset.
259266
full_offset = len(line) - len(self.char * (self.width * self.indent))

sqlparse/lexer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ class Lexer(object):
171171
# $ matches *before* newline, therefore we have two patterns
172172
# to match Comment.Single
173173
(r'--.*?$', tokens.Comment.Single),
174-
(r'(\r|\n|\r\n)', tokens.Newline),
174+
(r'(\r\n|\r|\n)', tokens.Newline),
175175
(r'\s+', tokens.Whitespace),
176176
(r'/\*', tokens.Comment.Multiline, 'multiline-comments'),
177177
(r':=', tokens.Assignment),

0 commit comments

Comments
 (0)