Skip to content

Commit

Permalink
Simplify expansion - kill sliding window.
Browse files Browse the repository at this point in the history
  • Loading branch information
jsirois committed Jan 16, 2025
1 parent 03669f3 commit 10b474c
Showing 1 changed file with 4 additions and 14 deletions.
18 changes: 4 additions & 14 deletions dev_cmd/expansion.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,13 @@

from __future__ import annotations

import itertools
import math
import re
from collections import deque
from typing import Iterable, Iterator, List, Tuple, TypeVar, Union
from typing import List, Tuple, Union

from dev_cmd import brace_substitution
from dev_cmd.brace_substitution import Substituter

T = TypeVar("T")


def sliding_window(items: Iterable[T], size: int) -> Iterator[Tuple[T, ...]]:
iterator = iter(items)
window = deque(itertools.islice(iterator, size - 1), maxlen=size)
for x in iterator:
window.append(x)
yield tuple(window)


class ExpandBraces(Substituter[List[Union[str, List[str]]], Tuple[str, ...]]):
def raw_text(self, text: str, *, state: list[str | list[str]]) -> None:
Expand Down Expand Up @@ -52,7 +40,8 @@ def substitution(self, text: str, section: slice, *, state: list[str | list[str]
work.append(item)

expanded: list[str] = []
for prefix, item in sliding_window(["", *work], 2):
prefix: str | list[str] = ""
for item in work:
if isinstance(prefix, str):
if isinstance(item, str):
expanded.append(item)
Expand All @@ -68,6 +57,7 @@ def substitution(self, text: str, section: slice, *, state: list[str | list[str]
expanded[-len(prefix) :] = [
f"{p}{atom}" for p in expanded[-len(prefix) :] for atom in item
]
prefix = item

state.append(expanded)
else:
Expand Down

0 comments on commit 10b474c

Please sign in to comment.