Last active
March 30, 2023 21:34
-
-
Save mnixry/3608d0207196b847887d34ace8feeb87 to your computer and use it in GitHub Desktop.
Implentment https://github.com/fumiama/base16384 in Python by only one line
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
encoding, decoding = ( | |
lambda input_bytes: ( | |
"".join( | |
chr(0x4E00 + sum(1 << i for i, bit in enumerate(reversed(row)) if bit)) | |
for row in ( | |
lambda x, length: ( | |
tuple(next(it, None) for it in x) for _ in range(length) | |
) | |
)( | |
[((char >> i) & 1 for char in input_bytes for i in reversed(range(8)))] | |
* 14, | |
len(input_bytes) * 4 // 7 + (1 if len(input_bytes) * 4 % 7 else 0), | |
) | |
) | |
+ (chr(0x3D00 + len(input_bytes) % 7) if len(input_bytes) % 7 else "") | |
), | |
lambda input_string: bytes( | |
sum(1 << i for i, bit in enumerate(reversed(row)) if bit) | |
for row in ( | |
lambda raw_string, residue: ( | |
lambda x, length: ( | |
tuple(next(it, None) for it in x) for _ in range(length) | |
) | |
)( | |
[ | |
( | |
(char >> i) & 1 | |
for char in (ord(i) - 0x4E00 for i in raw_string) | |
for i in reversed(range(14)) | |
) | |
] | |
* 8, | |
(len(raw_string) - 1) // 4 * 7 + (residue or 7), | |
) | |
)( | |
input_string[ | |
: None | |
if input_string and 0x4E00 <= ord(input_string[-1]) <= 0x8DFF | |
else -1 | |
], | |
( | |
ord(input_string[-1]) - 0x3D00 | |
if input_string and 0 <= ord(input_string[-1]) - 0x3D00 < 8 | |
else 0 | |
), | |
) | |
), | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# flake8:noqa:E371 | |
# input: [[1,2,3],[1,2],[1]] | |
# output: [[1,1,1],[2,2,None],[3,None,None]] | |
zip_longest = lambda x, length: ( | |
tuple(next(it, None) for it in x) for _ in range(length) | |
) | |
iterate_bits = lambda x, bit_length=8: ( | |
(char >> i) & 1 for char in x for i in reversed(range(bit_length)) | |
) | |
data = b"Hello World!12345678901234\0" | |
print( | |
result := "".join( | |
chr(0x4E00 + sum(1 << i for i, bit in enumerate(reversed(row)) if bit)) | |
for row in zip_longest( | |
[iterate_bits(data)] * 14, | |
len(data) * 4 // 7 + (1 if len(data) * 4 % 7 else 0), | |
) | |
) | |
+ (chr(0x3D00 + len(data) % 7) if len(data) % 7 else "") | |
) | |
print([format(ord(i) - 0x4E00, "0>14b") for i in result]) | |
iterate_chars = lambda x: iterate_bits((ord(i) - 0x4E00 for i in x), 14) | |
print( | |
bytes( | |
sum(1 << i for i, bit in enumerate(reversed(row)) if bit) | |
for row in ( | |
lambda input_string, residue: zip_longest( | |
[iterate_chars(input_string)] * 8, | |
(len(input_string) - 1) // 4 * 7 + (residue or 7), | |
) | |
)( | |
result[: None if result and 0x4E00 <= ord(result[-1]) <= 0x8DFF else -1], | |
( | |
ord(result[-1]) - 0x3D00 | |
if result and 0 <= ord(result[-1]) - 0x3D00 < 8 | |
else 0 | |
), | |
) | |
), | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment