a d' -> ((3, 'abcd'), (1, 3, 'bdc'))
(s: str)
| 28 | |
| 29 | |
| 30 | def decode(s: str) -> tuple[tuple[int, str], LinePart | None]: |
| 31 | """'a<bd|c>d' -> ((3, 'abcd'), (1, 3, 'bdc'))""" |
| 32 | |
| 33 | if not s.count("|") == 1: |
| 34 | raise ValueError("match helper needs | to occur once") |
| 35 | if s.count("<") != s.count(">") or s.count("<") not in (0, 1): |
| 36 | raise ValueError("match helper needs <, and > to occur just once") |
| 37 | matches = list(re.finditer(r"[<>|]", s)) |
| 38 | assert len(matches) in [1, 3], [m.group() for m in matches] |
| 39 | d = {} |
| 40 | for i, m in enumerate(matches): |
| 41 | d[m.group(0)] = m.start() - i |
| 42 | s = s[: m.start() - i] + s[m.end() - i :] |
| 43 | assert len(d) in [1, 3], "need all the parts just once! %r" % d |
| 44 | |
| 45 | if "<" in d: |
| 46 | return (d["|"], s), LinePart(d["<"], d[">"], s[d["<"] : d[">"]]) |
| 47 | else: |
| 48 | return (d["|"], s), None |
| 49 | |
| 50 | |
| 51 | def line_with_cursor(cursor_offset: int, line: str) -> str: |
no test coverage detected