Compared against declared_pairs · told 12 of 12 deliberately broken copies apart · 2144 ms
def kind_of(ch):
kind = 0
if ch == '&':
kind = 1
if ch == '=':
kind = 2
if ch == '+':
kind = 3
if ch == '%':
kind = 4
if ch == '"':
kind = 5
if ch == '\\':
kind = 6
return kind
def low(ch):
i = 0
for u in 'ABCDEF':
if ch == u:
return 'abcdef'[i]
i = i + 1
return ch
def pct(h):
if h == '0a':
return '\n'
if h == '0d':
return '\r'
if h == '09':
return '\t'
hexes = '2021222325262728' + '292a2b2c2d2e2f30' + '3132333435363738' + '393a3b3c3d3e3f40' + '4142434445464748' + '494a4b4c4d4e4f50' + '5152535455565758' + '595a5b5c5d5e5f60' + '6162636465666768' + '696a6b6c6d6e6f70' + '7172737475767778' + '797a7b7c7d7e'
chars = ' !"#' + "%&'()*+,-./0" + '123456789:;' + '<' + '=' + '>' + '?@' + 'ABCDEFGHIJKLMNOP' + 'QRSTUVWXYZ[\\]^_`' + 'abcdefghijklmnop' + 'qrstuvwxyz{|}~'
i = 0
n = len(chars)
while i < n:
if hexes[i * 2:i * 2 + 2] == h:
return chars[i]
i = i + 1
return ''
def esc(raw):
out = ''
for ch in raw:
if ch == '\\':
out = out + '\\\\'
else:
if ch == '"':
out = out + '\\"'
else:
if ch == '\n':
out = out + '\\n'
else:
if ch == '\r':
out = out + '\\r'
else:
if ch == '\t':
out = out + '\\t'
else:
out = out + ch
return out
def keep(keys, vals, key, val, seen):
found = 0
for old in keys:
if old == key:
found = 1
if found == 0:
keys = keys + [key]
if seen == 0:
val = ''
vals[key] = val
return keys
def solve(s):
if len(s) == 0:
return '{}'
keys = []
vals = dict()
key = ''
val = ''
seen = 0
i = 0
n = len(s)
while i < n:
ch = s[i]
kind = kind_of(ch)
step = 1
piece = ''
take = 0
if kind == 1:
keys = keep(keys, vals, key, val, seen)
key = ''
val = ''
seen = 0
else:
if kind == 2:
if seen == 0:
seen = 1
else:
piece = '='
take = 1
else:
if kind == 3:
piece = ' '
take = 1
else:
if kind == 4:
if i + 2 < n:
got = pct(low(s[i + 1]) + low(s[i + 2]))
if got == '':
piece = '%'
take = 1
else:
piece = got
take = 1
step = 3
else:
piece = '%'
take = 1
else:
if kind == 6:
piece = '\\'
take = 1
else:
if kind == 5:
piece = '"'
take = 1
else:
piece = ch
take = 1
if take == 1:
if seen == 0:
key = key + piece
else:
val = val + piece
i = i + step
keys = keep(keys, vals, key, val, seen)
out = '{'
j = 0
for k in keys:
if j > 0:
out = out + ','
out = out + '"' + esc(k) + '":"' + esc(vals[k]) + '"'
j = j + 1
return out + '}'
12 test cases ship with it; the first three:
| in | out |
|---|---|
xwcm xrb xwcm mzztdb 0 | {"xwcm xrb\nxwcm mzztdb 0":""} |
hlky jjrrbf hlky nylw 1 | {"hlky jjrrbf\nhlky nylw 1":""} |
lqgs vtgkjw lqgs rkvqhh 2 | {"lqgs vtgkjw\nlqgs rkvqhh 2":""} |
GET https://research.zaiba.cloud/api/v1/programs/form_urlencoded_to_json
POST https://research.zaiba.cloud/api/v1/programs/form_urlencoded_to_json/run
{"input": "…"}
# 200 · X-DeAI-Rail: grant (free within the allowance) · output + signed receipt
GET https://research.zaiba.cloud/api/v1/programs/form_urlencoded_to_json/code
# 200 · text/plain · the tool as one Python file, written by the library's authors