Catch the exception if decoding failed.
[pymailheaders.git] / chardet / eucjpprober.py
blob46a8b38b7702c68ccbef121982eca3595d7f54c2
1 ######################## BEGIN LICENSE BLOCK ########################
2 # The Original Code is mozilla.org code.
4 # The Initial Developer of the Original Code is
5 # Netscape Communications Corporation.
6 # Portions created by the Initial Developer are Copyright (C) 1998
7 # the Initial Developer. All Rights Reserved.
9 # Contributor(s):
10 # Mark Pilgrim - port to Python
12 # This library is free software; you can redistribute it and/or
13 # modify it under the terms of the GNU Lesser General Public
14 # License as published by the Free Software Foundation; either
15 # version 2.1 of the License, or (at your option) any later version.
17 # This library is distributed in the hope that it will be useful,
18 # but WITHOUT ANY WARRANTY; without even the implied warranty of
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 # Lesser General Public License for more details.
22 # You should have received a copy of the GNU Lesser General Public
23 # License along with this library; if not, write to the Free Software
24 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
25 # 02110-1301 USA
26 ######################### END LICENSE BLOCK #########################
28 import constants, sys
29 from constants import eStart, eError, eItsMe
30 from mbcharsetprober import MultiByteCharSetProber
31 from codingstatemachine import CodingStateMachine
32 from chardistribution import EUCJPDistributionAnalysis
33 from jpcntx import EUCJPContextAnalysis
34 from mbcssm import EUCJPSMModel
36 class EUCJPProber(MultiByteCharSetProber):
37 def __init__(self):
38 MultiByteCharSetProber.__init__(self)
39 self._mCodingSM = CodingStateMachine(EUCJPSMModel)
40 self._mDistributionAnalyzer = EUCJPDistributionAnalysis()
41 self._mContextAnalyzer = EUCJPContextAnalysis()
42 self.reset()
44 def reset(self):
45 MultiByteCharSetProber.reset(self)
46 self._mContextAnalyzer.reset()
48 def get_charset_name(self):
49 return "EUC-JP"
51 def feed(self, aBuf):
52 aLen = len(aBuf)
53 for i in range(0, aLen):
54 codingState = self._mCodingSM.next_state(aBuf[i])
55 if codingState == eError:
56 if constants._debug:
57 sys.stderr.write(self.get_charset_name() + ' prober hit error at byte ' + str(i) + '\n')
58 self._mState = constants.eNotMe
59 break
60 elif codingState == eItsMe:
61 self._mState = constants.eFoundIt
62 break
63 elif codingState == eStart:
64 charLen = self._mCodingSM.get_current_charlen()
65 if i == 0:
66 self._mLastChar[1] = aBuf[0]
67 self._mContextAnalyzer.feed(self._mLastChar, charLen)
68 self._mDistributionAnalyzer.feed(self._mLastChar, charLen)
69 else:
70 self._mContextAnalyzer.feed(aBuf[i-1:i+1], charLen)
71 self._mDistributionAnalyzer.feed(aBuf[i-1:i+1], charLen)
73 self._mLastChar[0] = aBuf[aLen - 1]
75 if self.get_state() == constants.eDetecting:
76 if self._mContextAnalyzer.got_enough_data() and \
77 (self.get_confidence() > constants.SHORTCUT_THRESHOLD):
78 self._mState = constants.eFoundIt
80 return self.get_state()
82 def get_confidence(self):
83 contxtCf = self._mContextAnalyzer.get_confidence()
84 distribCf = self._mDistributionAnalyzer.get_confidence()
85 return max(contxtCf, distribCf)