diff options
author | Sjoerd Job Postmus <sjoerdjob@sjec.nl> | 2016-06-02 08:30:27 +0200 |
---|---|---|
committer | Victor Uriarte <victor.m.uriarte@intel.com> | 2016-06-12 17:33:15 -0700 |
commit | d4cc0644c8348da5e49c58df5e26a3e969045249 (patch) | |
tree | a23fcaa75e313369194a27e0f7ac040b20b9e23c /sqlparse/sql.py | |
parent | 896774cb5298924abbcea81b9b90f1c7c10b3d6a (diff) | |
download | sqlparse-d4cc0644c8348da5e49c58df5e26a3e969045249.tar.gz |
Replace _group_matching with an inward-out grouping algorithm
All the matching between open/close was done all the time, first finding
the matching closing token, and then grouping the tokens in between, and
recurse over the newly created list.
Instead, it is more efficient to look for the previous open-token on
finding a closing-token, group these two together, and then continue on.
squashed: Handle token indices in group_tokens_between and find_matching.
Diffstat (limited to 'sqlparse/sql.py')
-rw-r--r-- | sqlparse/sql.py | 13 |
1 files changed, 9 insertions, 4 deletions
diff --git a/sqlparse/sql.py b/sqlparse/sql.py index 81cd8e9..dfe0430 100644 --- a/sqlparse/sql.py +++ b/sqlparse/sql.py @@ -331,9 +331,14 @@ class TokenList(Token): def group_tokens_between(self, grp_cls, start, end, include_end=True, extend=False): """Replace tokens by an instance of *grp_cls*.""" - start_idx = self.token_index(start) - end_idx = self.token_index(end) + include_end - tokens = self.tokens[start_idx:end_idx] + if isinstance(start, int): + start_idx = start + start = self.tokens[start_idx] + else: + start_idx = self.token_index(start) + + end_idx = self.token_index(end) if not isinstance(end, int) else end + end_idx += include_end if extend and isinstance(start, grp_cls): subtokens = self.tokens[start_idx+1:end_idx] @@ -344,7 +349,7 @@ class TokenList(Token): grp.value = start.__str__() else: subtokens = self.tokens[start_idx:end_idx] - grp = grp_cls(tokens) + grp = grp_cls(subtokens) self.tokens[start_idx:end_idx] = [grp] grp.parent = self |