""" Utility for parsing chapter range expressions like "1-100", "1,5,9", "1-50,70-90". """ from __future__ import annotations import re def parse_chapter_range(expr: str, max_chapter: int = 99999) -> list[int]: """ Parse expressions like: "" -> all chapters (empty list = no filter) "1-100" -> 1 through 100 "50" -> chapter 50 only "1,5,9,20" -> specific chapters "1-50,70-90" -> ranges combined Returns sorted list of chapter indices, or empty list meaning "all". """ expr = expr.strip() if not expr or expr.lower() in ("all", "*"): return [] indices: set[int] = set() parts = re.split(r",\s*", expr) for part in parts: part = part.strip() if not part: continue m = re.match(r"^(\d+)\s*-\s*(\d+)$", part) if m: start, end = int(m.group(1)), int(m.group(2)) indices.update(range(min(start, end), max(start, end) + 1)) elif re.match(r"^\d+$", part): indices.add(int(part)) # ignore invalid parts return sorted(indices) def format_range_hint(total: int) -> str: if total == 0: return "No chapters found yet" return f"e.g. all, 1-{total}, 1-50,70-90, 5,10,15"