Skip to content

Commit d32fd8f

Browse files
committed
Simplify gen_fuzz_strings using itertools
1 parent 76450dd commit d32fd8f

File tree

1 file changed

+3
-17
lines changed

1 file changed

+3
-17
lines changed

tests/utils/gen_fuzz_strings.py

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,10 @@
1+
from itertools import product
12
from typing import Generator
23

34
__all__ = ["gen_fuzz_strings"]
45

56

67
def gen_fuzz_strings(allowed_chars: str, max_length: int) -> Generator[str, None, None]:
78
"""Generator that produces all possible combinations of allowed characters."""
8-
num_allowed_chars = len(allowed_chars)
9-
10-
num_combinations = 0
11-
for length in range(1, max_length + 1):
12-
num_combinations += num_allowed_chars ** length
13-
14-
yield "" # special case for empty string
15-
for combination in range(num_combinations):
16-
permutation = ""
17-
18-
left_over = combination
19-
while left_over >= 0:
20-
reminder = left_over % num_allowed_chars
21-
permutation = allowed_chars[reminder] + permutation
22-
left_over = (left_over - reminder) // num_allowed_chars - 1
23-
24-
yield permutation
9+
for length in range(max_length + 1):
10+
yield from map("".join, product(allowed_chars, repeat=length))

0 commit comments

Comments
 (0)