MCPcopy Index your code
hub / github.com/bpython/bpython / decode

Function decode

bpython/test/test_line_properties.py:30–48  ·  view source on GitHub ↗

a d' -> ((3, 'abcd'), (1, 3, 'bdc'))

(s: str)

Source from the content-addressed store, hash-verified

28
29
30def 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
51def line_with_cursor(cursor_offset: int, line: str) -> str:

Callers 2

assertAccessMethod · 0.85
test_decodeMethod · 0.85

Calls 3

LinePartClass · 0.90
finditerMethod · 0.80
startMethod · 0.80

Tested by

no test coverage detected