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 constants import eStart, eError, eItsMe
32
from charsetprober import CharSetProber
34
class MultiByteCharSetProber(CharSetProber):
36
CharSetProber.__init__(self)
37
self._mDistributionAnalyzer = None
38
self._mCodingSM = None
39
self._mLastChar = ['\x00', '\x00']
42
CharSetProber.reset(self)
44
self._mCodingSM.reset()
45
if self._mDistributionAnalyzer:
46
self._mDistributionAnalyzer.reset()
47
self._mLastChar = ['\x00', '\x00']
49
def get_charset_name(self):
54
for i in range(0, aLen):
55
codingState = self._mCodingSM.next_state(aBuf[i])
56
if codingState == eError:
58
sys.stderr.write(self.get_charset_name() + ' prober hit error at byte ' + str(i) + '\n')
59
self._mState = constants.eNotMe
61
elif codingState == eItsMe:
62
self._mState = constants.eFoundIt
64
elif codingState == eStart:
65
charLen = self._mCodingSM.get_current_charlen()
67
self._mLastChar[1] = aBuf[0]
68
self._mDistributionAnalyzer.feed(self._mLastChar, charLen)
70
self._mDistributionAnalyzer.feed(aBuf[i-1:i+1], charLen)
72
self._mLastChar[0] = aBuf[aLen - 1]
74
if self.get_state() == constants.eDetecting:
75
if self._mDistributionAnalyzer.got_enough_data() and \
76
(self.get_confidence() > constants.SHORTCUT_THRESHOLD):
77
self._mState = constants.eFoundIt
79
return self.get_state()
81
def get_confidence(self):
82
return self._mDistributionAnalyzer.get_confidence()