Spaces:
Sleeping
Sleeping
Download utils/range_parser.py from Krixter/application: direct link, hf CLI and curl.
- Browser
- Download file 1.28 kB
-
https://huggingface.co/spaces/Krixter/application/resolve/main/utils/range_parser.py
- Command line
-
hf download hf://spaces/Krixter/application/utils/range_parser.py
-
curl -L -o range_parser.py https://huggingface.co/spaces/Krixter/application/resolve/main/utils/range_parser.py
1.28 kB
| """ | |
| 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" | |