Skip to content

Commit

Permalink
Regex tree precompute logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Buba98 committed Dec 27, 2024
1 parent 6d2e616 commit 8a52e6b
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions regex_enumerator/regex_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,10 +249,31 @@ def __init__(self, alternatives: list[Alternative], min_len: int, max_len: int |
self.done = self._base == 0 or self._max_len == 0
self._gen_charset = False
self._index_charset = 0
self._index_repetition = 0
if precompute and max_len is not None:
self._index_repetition = max_len - min_len
else:
self._index_repetition = 0
self._done_repetition = False
self._current_chars: set[str] = self._calculate_chars()
self.current: set[str] = self._calculate() if not self.done else set()
self.current: set[str] = self._calculate_first() if not self.done else set()

def _calculate_first(self) -> set[str]:
if self._max_len is not None and self._index_repetition + self._min_len >= self._max_len:
self._done_repetition = True
if self._done_charset:
self.done = True

if self._index_repetition + self._min_len == 0:
return {''}

result = {''}
for _ in range(self._min_len):
result = {pfx + sfx for pfx in result for sfx in self._current_chars}

for _ in range(self._index_repetition):
result.update({pfx + sfx for pfx in result for sfx in self._current_chars})

return result

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

0 comments on commit 8a52e6b

Please sign in to comment.