Determines the encoding type of the input string. Args: input_string (str): The string to be encoded. Returns: Encoding: The encoding type.
(self, input_string: str, encodings: List[Encoding] = None)
| 373 | ) |
| 374 | |
| 375 | def compute_encoding(self, input_string: str, encodings: List[Encoding] = None) -> Encoding: |
| 376 | """ |
| 377 | Determines the encoding type of the input string. |
| 378 | |
| 379 | Args: |
| 380 | input_string (str): The string to be encoded. |
| 381 | |
| 382 | Returns: |
| 383 | Encoding: The encoding type. |
| 384 | """ |
| 385 | if not input_string: |
| 386 | return Encoding.LOWER_SPECIAL |
| 387 | if encodings is None: |
| 388 | encodings = list(Encoding.__members__.values()) |
| 389 | |
| 390 | chars = list(input_string) |
| 391 | statistics = self._compute_statistics(chars) |
| 392 | if statistics.can_lower_special_encoded and Encoding.LOWER_SPECIAL in encodings: |
| 393 | return Encoding.LOWER_SPECIAL |
| 394 | elif statistics.can_lower_upper_digit_special_encoded and Encoding.LOWER_UPPER_DIGIT_SPECIAL in encodings: |
| 395 | if statistics.digit_count != 0: |
| 396 | return Encoding.LOWER_UPPER_DIGIT_SPECIAL |
| 397 | else: |
| 398 | upper_count = statistics.upper_count |
| 399 | if upper_count == 1 and chars[0].isupper(): |
| 400 | return Encoding.FIRST_TO_LOWER_SPECIAL |
| 401 | if (len(chars) + upper_count) * 5 < len(chars) * 6 and Encoding.ALL_TO_LOWER_SPECIAL in encodings: |
| 402 | return Encoding.ALL_TO_LOWER_SPECIAL |
| 403 | else: |
| 404 | if Encoding.LOWER_UPPER_DIGIT_SPECIAL in encodings: |
| 405 | return Encoding.LOWER_UPPER_DIGIT_SPECIAL |
| 406 | if Encoding.UTF_8 in encodings: |
| 407 | return Encoding.UTF_8 |
| 408 | raise ValueError(f"No encoding found for string: {input_string}, encodings: {encodings}") |
| 409 | |
| 410 | def _compute_statistics(self, chars: List[str]) -> Statistics: |
| 411 | """ |
no test coverage detected