summaryrefslogtreecommitdiff
path: root/sqlparse
diff options
context:
space:
mode:
authorAndi Albrecht <albrecht.andi@gmail.com>2013-05-10 10:16:17 +0200
committerAndi Albrecht <albrecht.andi@gmail.com>2013-05-10 10:16:17 +0200
commitbac3c5156f5da7a766f1384321c6d485c163b1e5 (patch)
tree7907e0a4e8a941ff203746455c7caa06a7b7acf2 /sqlparse
parentc7dd2658da9a0a5871a532133d11ca6c308ba29b (diff)
downloadsqlparse-bac3c5156f5da7a766f1384321c6d485c163b1e5.tar.gz
Improve performance of reindent engine a bit (targets issue41).
Diffstat (limited to 'sqlparse')
-rw-r--r--sqlparse/filters.py13
-rw-r--r--sqlparse/lexer.py2
2 files changed, 11 insertions, 4 deletions
diff --git a/sqlparse/filters.py b/sqlparse/filters.py
index c6506eb..70f1051 100644
--- a/sqlparse/filters.py
+++ b/sqlparse/filters.py
@@ -250,10 +250,17 @@ class ReindentFilter:
self._curr_stmt = None
self._last_stmt = None
+ def _flatten_up_to_token(self, token):
+ """Yields all tokens up to token plus the next one."""
+ # helper for _get_offset
+ iterator = self._curr_stmt.flatten()
+ for t in iterator:
+ yield t
+ if t == token:
+ raise StopIteration
+
def _get_offset(self, token):
- all_ = list(self._curr_stmt.flatten())
- idx = all_.index(token)
- raw = ''.join(unicode(x) for x in all_[:idx + 1])
+ raw = ''.join(map(unicode, self._flatten_up_to_token(token)))
line = raw.splitlines()[-1]
# Now take current offset into account and return relative offset.
full_offset = len(line) - len(self.char * (self.width * self.indent))
diff --git a/sqlparse/lexer.py b/sqlparse/lexer.py
index dd6ecc0..b981c0e 100644
--- a/sqlparse/lexer.py
+++ b/sqlparse/lexer.py
@@ -171,7 +171,7 @@ class Lexer(object):
# $ matches *before* newline, therefore we have two patterns
# to match Comment.Single
(r'--.*?$', tokens.Comment.Single),
- (r'(\r|\n|\r\n)', tokens.Newline),
+ (r'(\r\n|\r|\n)', tokens.Newline),
(r'\s+', tokens.Whitespace),
(r'/\*', tokens.Comment.Multiline, 'multiline-comments'),
(r':=', tokens.Assignment),