Decodes MetaString objects back into their original plain text form.
| 69 | |
| 70 | |
| 71 | class MetaStringDecoder: |
| 72 | """ |
| 73 | Decodes MetaString objects back into their original plain text form. |
| 74 | """ |
| 75 | |
| 76 | def __init__(self, special_char1: str, special_char2: str): |
| 77 | """ |
| 78 | Creates a MetaStringDecoder with specified special characters used for decoding. |
| 79 | |
| 80 | Args: |
| 81 | special_char1 (str): The first special character used for encoding. |
| 82 | special_char2 (str): The second special character used for encoding. |
| 83 | """ |
| 84 | self.special_char1 = special_char1 |
| 85 | self.special_char2 = special_char2 |
| 86 | |
| 87 | def decode(self, encoded_data: bytes, encoding: Encoding) -> str: |
| 88 | """ |
| 89 | Decodes the encoded data using the specified encoding. |
| 90 | |
| 91 | Args: |
| 92 | encoded_data (bytes): The data to decode. |
| 93 | encoding (Encoding): The encoding type used for decoding. |
| 94 | |
| 95 | Returns: |
| 96 | str: The decoded string. |
| 97 | """ |
| 98 | if len(encoded_data) == 0: |
| 99 | return "" |
| 100 | return self.decode_with_encoding(encoded_data, encoding) |
| 101 | |
| 102 | def decode_with_encoding(self, encoded_data: bytes, encoding: Encoding) -> str: |
| 103 | """ |
| 104 | Decodes the encoded data with the specified encoding. |
| 105 | |
| 106 | Args: |
| 107 | encoded_data (bytes): The data to decode. |
| 108 | encoding (Encoding): The encoding type. |
| 109 | |
| 110 | Returns: |
| 111 | str: The decoded string. |
| 112 | """ |
| 113 | if encoding == Encoding.LOWER_SPECIAL: |
| 114 | return self._decode_lower_special(encoded_data) |
| 115 | elif encoding == Encoding.LOWER_UPPER_DIGIT_SPECIAL: |
| 116 | return self._decode_lower_upper_digit_special(encoded_data) |
| 117 | elif encoding == Encoding.FIRST_TO_LOWER_SPECIAL: |
| 118 | return self._decode_rep_first_lower_special(encoded_data) |
| 119 | elif encoding == Encoding.ALL_TO_LOWER_SPECIAL: |
| 120 | return self._decode_rep_all_to_lower_special(encoded_data) |
| 121 | elif encoding == Encoding.UTF_8: |
| 122 | return encoded_data.decode("utf-8") |
| 123 | else: |
| 124 | raise ValueError(f"Unexpected encoding flag: {encoding}") |
| 125 | |
| 126 | def _decode_lower_special(self, data: bytes) -> str: |
| 127 | """ |
| 128 | Decodes data encoded with LOWER_SPECIAL encoding. |
no outgoing calls