From 0acaa4c57d7169f3903af3c3df4faf95d2cbea84 Mon Sep 17 00:00:00 2001 From: Victor Uriarte Date: Wed, 15 Jun 2016 02:39:54 -0700 Subject: Reduce calls by _group to get tk idx --- sqlparse/engine/grouping.py | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) (limited to 'sqlparse/engine') diff --git a/sqlparse/engine/grouping.py b/sqlparse/engine/grouping.py index ae214c2..a74f6f8 100644 --- a/sqlparse/engine/grouping.py +++ b/sqlparse/engine/grouping.py @@ -313,17 +313,28 @@ def _group(tlist, cls, match, post=None, extend=True): """Groups together tokens that are joined by a middle token. ie. x < y""" - for token in list(tlist): + + tidx_offset = 0 + pidx, prev_ = None, None + for idx, token in enumerate(list(tlist)): + tidx = idx - tidx_offset + + if token.is_whitespace(): + continue if token.is_group() and not isinstance(token, cls): _group(token, cls, match, valid_left, valid_right, post, extend) + pidx, prev_ = tidx, token continue if not match(token): + pidx, prev_ = tidx, token continue - tidx = tlist.token_index(token) - pidx, prev_ = tlist.token_prev(tidx) nidx, next_ = tlist.token_next(tidx) if valid_left(prev_) and valid_right(next_): from_idx, to_idx = post(tlist, pidx, tidx, nidx) - tlist.group_tokens(cls, from_idx, to_idx, extend=extend) + grp = tlist.group_tokens(cls, from_idx, to_idx, extend=extend) + tidx_offset += to_idx - from_idx + pidx, prev_ = from_idx, grp + else: + pidx, prev_ = tidx, token -- cgit v1.2.1