def _wildcard_key_match()

in chz/blueprint/_wildcard.py [0:0]


def _wildcard_key_match(key: str, target_str: str) -> bool:
    # This is what the regex does; currently unused (but is tested)
    if key.endswith("..."):
        raise ValueError("Wildcard not allowed at end of key")
    pattern = ["..."] if key.startswith("...") else []
    pattern += [x for x in re.split(r"(\.\.\.)|\.", key.removeprefix("...")) if x is not None]
    target = target_str.split(".")

    _grid: dict[tuple[int, int], bool] = {}

    def _match(i: int, j: int) -> bool:
        if i == len(pattern):
            return j == len(target)
        if j == len(target):
            return False
        if (i, j) in _grid:
            return _grid[i, j]
        if pattern[i] == "...":
            ret = _match(i, j + 1) or _match(i + 1, j)
            _grid[i, j] = ret
            return ret
        ret = pattern[i] == target[j] and _match(i + 1, j + 1)
        _grid[i, j] = ret
        return ret

    return _match(0, 0)