From 3a388df6688d884955be154980655620001d2af0 Mon Sep 17 00:00:00 2001 From: av27-lgtm Date: Fri, 9 Oct 2026 19:08:37 +0400 Subject: [PATCH] perf: add fast-path in token_next_by and eliminate list copy in token_index --- sqlparse/sql.py | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/sqlparse/sql.py b/sqlparse/sql.py index ec44a6da..9603b245 100644 --- a/sqlparse/sql.py +++ b/sqlparse/sql.py @@ -271,6 +271,27 @@ def matcher(tk): def token_next_by(self, i=None, m=None, t=None, idx=-1, end=None): idx += 1 + tokens = self.tokens + end = len(tokens) if end is None else end + if idx < end: + if t is not None and i is None and m is None and not isinstance(t, list): + for k in range(idx, end): + tk = tokens[k] + if tk is not None and tk.ttype in t: + return k, tk + return None, None + if m is not None and i is None and t is None and not isinstance(m, list): + for k in range(idx, end): + tk = tokens[k] + if tk is not None and tk.match(*m): + return k, tk + return None, None + if i is not None and m is None and t is None: + for k in range(idx, end): + tk = tokens[k] + if tk is not None and isinstance(tk, i): + return k, tk + return None, None return self._token_matching(lambda tk: imt(tk, i, m, t), idx, end) def token_not_matching(self, funcs, idx): @@ -310,7 +331,7 @@ def matcher(tk): def token_index(self, token, start=0): """Return list index of token.""" start = start if isinstance(start, int) else self.token_index(start) - return start + self.tokens[start:].index(token) + return self.tokens.index(token, start) def group_tokens(self, grp_cls, start, end, include_end=True, extend=False):