huffman_encode returns the bitstring and the bitlength of the bitstring representing the string provided as a parameter :param str s: the string to encode :return: (int, int): the bitstring of s and its bitlength :raises: AssertionError
(cls, s)
| 1015 | |
| 1016 | @classmethod |
| 1017 | def huffman_encode(cls, s): |
| 1018 | # type: (str) -> Tuple[int, int] |
| 1019 | """ huffman_encode returns the bitstring and the bitlength of the |
| 1020 | bitstring representing the string provided as a parameter |
| 1021 | |
| 1022 | :param str s: the string to encode |
| 1023 | :return: (int, int): the bitstring of s and its bitlength |
| 1024 | :raises: AssertionError |
| 1025 | """ |
| 1026 | i = 0 |
| 1027 | ibl = 0 |
| 1028 | for c in s: |
| 1029 | val, bl = cls._huffman_encode_char(c) |
| 1030 | i = (i << bl) + val |
| 1031 | ibl += bl |
| 1032 | |
| 1033 | padlen = 8 - (ibl % 8) |
| 1034 | if padlen != 8: |
| 1035 | val, bl = cls._huffman_encode_char(EOS()) |
| 1036 | i = (i << padlen) + (val >> (bl - padlen)) |
| 1037 | ibl += padlen |
| 1038 | |
| 1039 | ret = i, ibl |
| 1040 | assert ret[0] >= 0 |
| 1041 | assert (ret[1] >= 0) |
| 1042 | return ret |
| 1043 | |
| 1044 | @classmethod |
| 1045 | def huffman_decode(cls, i, ibl): |
no test coverage detected