Skip to content

Commit ea2a731

Browse files
committed
bail early
1 parent 4b10591 commit ea2a731

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

src/textual/fuzzy.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from __future__ import annotations
99

1010
from operator import itemgetter
11-
from re import finditer
11+
from re import IGNORECASE, escape, finditer, search
1212
from typing import Iterable, NamedTuple
1313

1414
import rich.repr
@@ -75,6 +75,14 @@ def match(self, query: str, candidate: str) -> tuple[float, tuple[int, ...]]:
7575
Returns:
7676
A pair of (score, tuple of offsets). `(0, ())` for no result.
7777
"""
78+
79+
query_regex = ".*?".join(f"({escape(character)})" for character in query)
80+
if not search(
81+
query_regex, candidate, flags=0 if self.case_sensitive else IGNORECASE
82+
):
83+
# Bail out early if there is no possibility of a match
84+
return (0.0, ())
85+
7886
cache_key = (query, candidate, self.case_sensitive)
7987
if cache_key in self.cache:
8088
return self.cache[cache_key]
@@ -131,7 +139,7 @@ def score(search: _Search) -> float:
131139
find = candidate.find
132140
# Limit the number of loops out of an abundance of caution.
133141
# This would be hard to reach without contrived data.
134-
remaining_loops = 500
142+
remaining_loops = 200
135143

136144
while stack and (remaining_loops := remaining_loops - 1):
137145
search = pop()

0 commit comments

Comments
 (0)