39 lines
1.2 KiB
Python
39 lines
1.2 KiB
Python
from io import BytesIO
|
|
|
|
from bu4.codes import CODE_CALL, CODE_QOPN, CODE_MAKE, CODE_QCLS, CODE_NULL, CODE_SKIP, CODE_NAME, CODE_QUOT
|
|
|
|
|
|
def transply(source: str) -> bytes:
|
|
b = BytesIO()
|
|
for c in source:
|
|
if c.isspace():
|
|
pass
|
|
elif c in '/':
|
|
b.write(bytes([CODE_CALL]))
|
|
elif c in '{':
|
|
b.write(bytes([CODE_CALL, CODE_QOPN]))
|
|
elif c in '(':
|
|
b.write(bytes([CODE_MAKE]))
|
|
elif c in '|':
|
|
b.write(bytes([CODE_QCLS, CODE_MAKE]))
|
|
elif c in '})':
|
|
b.write(bytes([CODE_NULL]))
|
|
elif c in '#':
|
|
b.write(bytes([CODE_SKIP]))
|
|
elif c in '?':
|
|
b.write(bytes([CODE_QOPN, CODE_NULL, CODE_QCLS]))
|
|
elif c in ']':
|
|
b.write(bytes([CODE_NULL, CODE_QCLS]))
|
|
elif c in '[':
|
|
b.write(bytes([CODE_QOPN, CODE_NAME]))
|
|
elif c in '"':
|
|
b.write(bytes([CODE_QUOT]))
|
|
elif c in '<':
|
|
b.write(bytes([CODE_QOPN]))
|
|
elif c in '>':
|
|
b.write(bytes([CODE_QCLS]))
|
|
else:
|
|
value = c.encode()
|
|
b.write(value if len(value) == 1 else value.hex().encode())
|
|
return b.getvalue()
|