1 ######################## BEGIN LICENSE BLOCK ########################
2 # The Original Code is Mozilla Universal charset detector code.
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.
10 # Mark Pilgrim - port to Python
11 # Shy Shalom - original C code
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.
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.
24 # You should have received a copy of the GNU Lesser General Public
25 # License along with this library; if not, write to the Free Software
26 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
28 ######################### END LICENSE BLOCK #########################
31 from . import constants
32 from .charsetprober import CharSetProber
35 class MultiByteCharSetProber(CharSetProber):
37 CharSetProber.__init__(self)
38 self._mDistributionAnalyzer = None
39 self._mCodingSM = None
40 self._mLastChar = [0, 0]
43 CharSetProber.reset(self)
45 self._mCodingSM.reset()
46 if self._mDistributionAnalyzer:
47 self._mDistributionAnalyzer.reset()
48 self._mLastChar = [0, 0]
50 def get_charset_name(self):
55 for i in range(0, aLen):
56 codingState = self._mCodingSM.next_state(aBuf[i])
57 if codingState == constants.eError:
59 sys.stderr.write(self.get_charset_name()
60 + ' prober hit error at byte ' + str(i)
62 self._mState = constants.eNotMe
64 elif codingState == constants.eItsMe:
65 self._mState = constants.eFoundIt
67 elif codingState == constants.eStart:
68 charLen = self._mCodingSM.get_current_charlen()
70 self._mLastChar[1] = aBuf[0]
71 self._mDistributionAnalyzer.feed(self._mLastChar, charLen)
73 self._mDistributionAnalyzer.feed(aBuf[i - 1:i + 1],
76 self._mLastChar[0] = aBuf[aLen - 1]
78 if self.get_state() == constants.eDetecting:
79 if (self._mDistributionAnalyzer.got_enough_data() and
80 (self.get_confidence() > constants.SHORTCUT_THRESHOLD)):
81 self._mState = constants.eFoundIt
83 return self.get_state()
85 def get_confidence(self):
86 return self._mDistributionAnalyzer.get_confidence()