Skip to content

Commit 1191ddf

Browse files
committed
🗓 Oct 3, 2025 10:48:05 PM
✨ to/from_base65536 🐙 plugins updated 🧪 tests added/updated
1 parent 0ad979b commit 1191ddf

File tree

6 files changed

+578
-4
lines changed

6 files changed

+578
-4
lines changed

TODO

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ New ideas:
2828
☐ beaufort
2929
☐ 🔥 update python_requires in setup.py on python version change
3030
☐ update base85 https://github.com/gorakhargosh/mom/blob/master/mom/codec/base85.py#L343
31+
☐ remove new lines method
3132

3233
Bug:
3334

chepy/modules/dataformat.py

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@
1212
import quopri
1313
import io
1414
import csv
15+
from six import indexbytes, int2byte, unichr
1516
import sqlite3
1617
import collections
1718
from random import randint
1819
import regex as re
19-
from .internal.constants import Encoding
20+
from .internal.constants import Encoding, Base65536
2021
from .internal.helpers import (
2122
detect_delimiter,
2223
Rotate,
@@ -751,11 +752,11 @@ def str_to_hex(self, delimiter: Union[str, bytes] = b"") -> DataFormatT:
751752
return self
752753

753754
@ChepyDecorators.call_stack
754-
def int_to_bytes(self, length: Union[int, None]=None, byteorder: str = 'big'):
755+
def int_to_bytes(self, length: Union[int, None] = None, byteorder: str = "big"):
755756
"""Int to bytes conversion
756757
757758
Args:
758-
length (int, optional): Length of bytes. If not specified, byteorder is ignored and long_to_bytes is used.
759+
length (int, optional): Length of bytes. If not specified, byteorder is ignored and long_to_bytes is used.
759760
byteorder (str, optional): Endianness. Defaults to 'big'.
760761
761762
Returns:
@@ -2165,6 +2166,50 @@ def to_base62(
21652166
self.state = "".join(base62)
21662167
return self
21672168

2169+
@ChepyDecorators.call_stack
2170+
def from_base65536(self) -> DataFormatT:
2171+
"""Decode Base 65536 for unicode
2172+
2173+
Returns:
2174+
Chepy: The Chepy object.
2175+
"""
2176+
data = self._convert_to_str()
2177+
stream = io.BytesIO()
2178+
done = False
2179+
for ch in data:
2180+
code_point = ord(ch)
2181+
b1 = code_point & ((1 << 8) - 1)
2182+
try:
2183+
b2 = Base65536.B2[code_point - b1]
2184+
except KeyError: # pragma: no cover
2185+
raise ValueError("Invalid base65536 code point: %d" % code_point)
2186+
b = int2byte(b1) if b2 == -1 else int2byte(b1) + int2byte(b2)
2187+
if len(b) == 1:
2188+
if done: # pragma: no cover
2189+
raise ValueError("base65536 sequence continued after final byte")
2190+
done = True
2191+
stream.write(b)
2192+
self.state = stream.getvalue()
2193+
return self
2194+
2195+
@ChepyDecorators.call_stack
2196+
def to_base65536(self) -> DataFormatT:
2197+
"""Encode to Base 65536. Base64 for unicode
2198+
2199+
Returns:
2200+
Chepy: The Chepy object.
2201+
"""
2202+
data = self._convert_to_bytes()
2203+
stream = io.StringIO()
2204+
length = len(data)
2205+
for x in range(0, length, 2):
2206+
b1 = indexbytes(data, x)
2207+
b2 = indexbytes(data, x + 1) if x + 1 < length else -1
2208+
code_point = Base65536.BLOCK_START[b2] + b1
2209+
stream.write(unichr(code_point))
2210+
self.state = stream.getvalue()
2211+
return self
2212+
21682213
@ChepyDecorators.call_stack
21692214
def from_base62(
21702215
self,

chepy/modules/dataformat.pyi

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ class DataFormat(ChepyCore):
111111
def rotate_left(self: DataFormatT, radix: int=1, carry: bool=False) -> DataFormatT: ...
112112
def to_base62(self: DataFormatT, alphabet: str="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz") -> DataFormatT: ...
113113
def from_base62(self: DataFormatT, alphabet: str="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz") -> DataFormatT: ...
114+
def to_base65536(self: DataFormatT) -> DataFormatT: ...
115+
def from_base65536(self: DataFormatT) -> DataFormatT: ...
114116
def cut(self: DataFormatT, start: int, end: int) -> DataFormatT: ...
115117
def flatten(self: DataFormatT) -> DataFormatT: ...
116118
def to_utf21(self: DataFormatT) -> DataFormatT: ...

0 commit comments

Comments
 (0)