|
12 | 12 | import quopri |
13 | 13 | import io |
14 | 14 | import csv |
| 15 | +from six import indexbytes, int2byte, unichr |
15 | 16 | import sqlite3 |
16 | 17 | import collections |
17 | 18 | from random import randint |
18 | 19 | import regex as re |
19 | | -from .internal.constants import Encoding |
| 20 | +from .internal.constants import Encoding, Base65536 |
20 | 21 | from .internal.helpers import ( |
21 | 22 | detect_delimiter, |
22 | 23 | Rotate, |
@@ -751,11 +752,11 @@ def str_to_hex(self, delimiter: Union[str, bytes] = b"") -> DataFormatT: |
751 | 752 | return self |
752 | 753 |
|
753 | 754 | @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"): |
755 | 756 | """Int to bytes conversion |
756 | 757 |
|
757 | 758 | 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. |
759 | 760 | byteorder (str, optional): Endianness. Defaults to 'big'. |
760 | 761 |
|
761 | 762 | Returns: |
@@ -2165,6 +2166,50 @@ def to_base62( |
2165 | 2166 | self.state = "".join(base62) |
2166 | 2167 | return self |
2167 | 2168 |
|
| 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 | + |
2168 | 2213 | @ChepyDecorators.call_stack |
2169 | 2214 | def from_base62( |
2170 | 2215 | self, |
|
0 commit comments