Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/chardet/mbcharsetprober.py: 98%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

45 statements  

1######################## BEGIN LICENSE BLOCK ######################## 

2# The Original Code is Mozilla Universal charset detector code. 

3# 

4# The Initial Developer of the Original Code is 

5# Netscape Communications Corporation. 

6# Portions created by the Initial Developer are Copyright (C) 2001 

7# the Initial Developer. All Rights Reserved. 

8# 

9# Contributor(s): 

10# Mark Pilgrim - port to Python 

11# Shy Shalom - original C code 

12# Proofpoint, Inc. 

13# 

14# This library is free software; you can redistribute it and/or 

15# modify it under the terms of the GNU Lesser General Public 

16# License as published by the Free Software Foundation; either 

17# version 2.1 of the License, or (at your option) any later version. 

18# 

19# This library is distributed in the hope that it will be useful, 

20# but WITHOUT ANY WARRANTY; without even the implied warranty of 

21# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 

22# Lesser General Public License for more details. 

23# 

24# You should have received a copy of the GNU Lesser General Public 

25# License along with this library; if not, see 

26# <https://www.gnu.org/licenses/>. 

27######################### END LICENSE BLOCK ######################### 

28 

29from typing import Optional, Union 

30 

31from .chardistribution import CharDistributionAnalysis 

32from .charsetprober import CharSetProber 

33from .codingstatemachine import CodingStateMachine 

34from .enums import LanguageFilter, MachineState, ProbingState 

35 

36 

37class MultiByteCharSetProber(CharSetProber): 

38 """ 

39 MultiByteCharSetProber 

40 """ 

41 

42 def __init__(self, lang_filter: LanguageFilter = LanguageFilter.NONE) -> None: 

43 super().__init__(lang_filter=lang_filter) 

44 self.distribution_analyzer: Optional[CharDistributionAnalysis] = None 

45 self.coding_sm: Optional[CodingStateMachine] = None 

46 self._last_char = bytearray(b"\0\0") 

47 

48 def reset(self) -> None: 

49 super().reset() 

50 if self.coding_sm: 

51 self.coding_sm.reset() 

52 if self.distribution_analyzer: 

53 self.distribution_analyzer.reset() 

54 self._last_char = bytearray(b"\0\0") 

55 

56 def feed(self, byte_str: Union[bytes, bytearray]) -> ProbingState: 

57 assert self.coding_sm is not None 

58 assert self.distribution_analyzer is not None 

59 

60 for i, byte in enumerate(byte_str): 

61 coding_state = self.coding_sm.next_state(byte) 

62 if coding_state == MachineState.ERROR: 

63 self.logger.debug( 

64 "%s %s prober hit error at byte %s", 

65 self.charset_name, 

66 self.language, 

67 i, 

68 ) 

69 self._state = ProbingState.NOT_ME 

70 break 

71 if coding_state == MachineState.ITS_ME: 

72 self._state = ProbingState.FOUND_IT 

73 break 

74 if coding_state == MachineState.START: 

75 char_len = self.coding_sm.get_current_charlen() 

76 if i == 0: 

77 self._last_char[1] = byte 

78 self.distribution_analyzer.feed(self._last_char, char_len) 

79 else: 

80 self.distribution_analyzer.feed(byte_str[i - 1 : i + 1], char_len) 

81 

82 self._last_char[0] = byte_str[-1] 

83 

84 if self.state == ProbingState.DETECTING: 

85 if self.distribution_analyzer.got_enough_data() and ( 

86 self.get_confidence() > self.SHORTCUT_THRESHOLD 

87 ): 

88 self._state = ProbingState.FOUND_IT 

89 

90 return self.state 

91 

92 def get_confidence(self) -> float: 

93 assert self.distribution_analyzer is not None 

94 return self.distribution_analyzer.get_confidence()