Finds the encoding token for the first character in the text. Tests: >>> lz77_compressor = LZ77Compressor() >>> lz77_compressor._find_encoding_token("abrarrarrad", "abracad").offset 7 >>> lz77_compressor._find_encoding_token("adabrarrarrad", "
(self, text: str, search_buffer: str)
| 142 | return output |
| 143 | |
| 144 | def _find_encoding_token(self, text: str, search_buffer: str) -> Token: |
| 145 | """Finds the encoding token for the first character in the text. |
| 146 | |
| 147 | Tests: |
| 148 | >>> lz77_compressor = LZ77Compressor() |
| 149 | >>> lz77_compressor._find_encoding_token("abrarrarrad", "abracad").offset |
| 150 | 7 |
| 151 | >>> lz77_compressor._find_encoding_token("adabrarrarrad", "cabrac").length |
| 152 | 1 |
| 153 | >>> lz77_compressor._find_encoding_token("abc", "xyz").offset |
| 154 | 0 |
| 155 | >>> lz77_compressor._find_encoding_token("", "xyz").offset |
| 156 | Traceback (most recent call last): |
| 157 | ... |
| 158 | ValueError: We need some text to work with. |
| 159 | >>> lz77_compressor._find_encoding_token("abc", "").offset |
| 160 | 0 |
| 161 | """ |
| 162 | |
| 163 | if not text: |
| 164 | raise ValueError("We need some text to work with.") |
| 165 | |
| 166 | # Initialise result parameters to default values |
| 167 | length, offset = 0, 0 |
| 168 | |
| 169 | if not search_buffer: |
| 170 | return Token(offset, length, text[length]) |
| 171 | |
| 172 | for i, character in enumerate(search_buffer): |
| 173 | found_offset = len(search_buffer) - i |
| 174 | if character == text[0]: |
| 175 | found_length = self._match_length_from_index(text, search_buffer, 0, i) |
| 176 | # if the found length is bigger than the current or if it's equal, |
| 177 | # which means it's offset is smaller: update offset and length |
| 178 | if found_length >= length: |
| 179 | offset, length = found_offset, found_length |
| 180 | |
| 181 | return Token(offset, length, text[length]) |
| 182 | |
| 183 | def _match_length_from_index( |
| 184 | self, text: str, window: str, text_index: int, window_index: int |
no test coverage detected