Commit 7e9771cc authored by Kirill Smelkov's avatar Kirill Smelkov

.

parent fd1a95da
...@@ -52,24 +52,38 @@ def encode(s: str): # -> str ...@@ -52,24 +52,38 @@ def encode(s: str): # -> str
# decode provides reverse operation for encode. # decode provides reverse operation for encode.
def decode(s: str): # -> str | ValueError | UnicodeDecodeError def decode(s: str): # -> str | ValueError
try:
return _decode(s)
except Exception as e:
raise ValueError("decoding %r" % s) from e
def _decode(s):
s = s.encode('utf-8') s = s.encode('utf-8')
outv = [] outv = []
emit = outv.append emit = outv.append
def bad(reason):
raise ValueError(reason)
while len(s) > 0: while len(s) > 0:
c = s[0:1] c = s[:1]
s = s[1:] s = s[1:]
if c != b'_': if c != b'_':
emit(c) emit(c)
continue continue
if len(s) < 2: if len(s) < 1:
bad("truncated escape sequence") raise ValueError("truncated escape sequence")
x = s[:2] x = s[:1]
s = s[2:] s = s[1:]
if x == b'_':
emit(b'_')
continue
if len(s) < 1:
raise ValueError("truncated escape sequence")
x += s[:1]
s = s[1:]
i = int(x, 16) # raises ValueError if not ok i = int(x, 16) # raises ValueError if not ok
c = bytes([i]) c = bytes([i])
emit(c) emit(c)
...@@ -106,7 +120,7 @@ def test_encode(): ...@@ -106,7 +120,7 @@ def test_encode():
('_', '__'), ('_', '__'),
(' ', '_20'), (' ', '_20'),
('αβγ', '_ce_b1_ce_b2_ce_b3'), ('αβγ', '_ce_b1_ce_b2_ce_b3'),
('a b+c_d' 'a_20b_43c__d'), ('a b+c_d', 'a_20b_2bc__d'),
] ]
for (s, encok) in testv: for (s, encok) in testv:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment