Skip to content

Commit 8a52e6b

Browse files
committed
Regex tree precompute logic
1 parent 6d2e616 commit 8a52e6b

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

regex_enumerator/regex_tree.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,10 +249,31 @@ def __init__(self, alternatives: list[Alternative], min_len: int, max_len: int |
249249
self.done = self._base == 0 or self._max_len == 0
250250
self._gen_charset = False
251251
self._index_charset = 0
252-
self._index_repetition = 0
252+
if precompute and max_len is not None:
253+
self._index_repetition = max_len - min_len
254+
else:
255+
self._index_repetition = 0
253256
self._done_repetition = False
254257
self._current_chars: set[str] = self._calculate_chars()
255-
self.current: set[str] = self._calculate() if not self.done else set()
258+
self.current: set[str] = self._calculate_first() if not self.done else set()
259+
260+
def _calculate_first(self) -> set[str]:
261+
if self._max_len is not None and self._index_repetition + self._min_len >= self._max_len:
262+
self._done_repetition = True
263+
if self._done_charset:
264+
self.done = True
265+
266+
if self._index_repetition + self._min_len == 0:
267+
return {''}
268+
269+
result = {''}
270+
for _ in range(self._min_len):
271+
result = {pfx + sfx for pfx in result for sfx in self._current_chars}
272+
273+
for _ in range(self._index_repetition):
274+
result.update({pfx + sfx for pfx in result for sfx in self._current_chars})
275+
276+
return result
256277

257278
def add_reference(self, reference: BackReference):
258279
if reference.done and len(reference.current) == 0:

0 commit comments

Comments
 (0)