ThirdParty/CharDet/chardet/codingstatemachine.py

Fri, 25 Apr 2014 22:07:19 +0200

author
T.Rzepka <Tobias.Rzepka@gmail.com>
date
Fri, 25 Apr 2014 22:07:19 +0200
changeset 3537
7662053c3906
parent 12
1d8dd9706f46
child 5714
90c57b50600f
permissions
-rw-r--r--

updated CharDet to 2.2.1, updated changelog

3537
7662053c3906 updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 12
diff changeset
1 ######################## BEGIN LICENSE BLOCK ########################
7662053c3906 updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 12
diff changeset
2 # The Original Code is mozilla.org code.
7662053c3906 updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 12
diff changeset
3 #
7662053c3906 updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 12
diff changeset
4 # The Initial Developer of the Original Code is
7662053c3906 updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 12
diff changeset
5 # Netscape Communications Corporation.
7662053c3906 updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 12
diff changeset
6 # Portions created by the Initial Developer are Copyright (C) 1998
7662053c3906 updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 12
diff changeset
7 # the Initial Developer. All Rights Reserved.
7662053c3906 updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 12
diff changeset
8 #
7662053c3906 updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 12
diff changeset
9 # Contributor(s):
7662053c3906 updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 12
diff changeset
10 # Mark Pilgrim - port to Python
7662053c3906 updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 12
diff changeset
11 #
7662053c3906 updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 12
diff changeset
12 # This library is free software; you can redistribute it and/or
7662053c3906 updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 12
diff changeset
13 # modify it under the terms of the GNU Lesser General Public
7662053c3906 updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 12
diff changeset
14 # License as published by the Free Software Foundation; either
7662053c3906 updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 12
diff changeset
15 # version 2.1 of the License, or (at your option) any later version.
7662053c3906 updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 12
diff changeset
16 #
7662053c3906 updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 12
diff changeset
17 # This library is distributed in the hope that it will be useful,
7662053c3906 updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 12
diff changeset
18 # but WITHOUT ANY WARRANTY; without even the implied warranty of
7662053c3906 updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 12
diff changeset
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
7662053c3906 updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 12
diff changeset
20 # Lesser General Public License for more details.
7662053c3906 updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 12
diff changeset
21 #
7662053c3906 updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 12
diff changeset
22 # You should have received a copy of the GNU Lesser General Public
7662053c3906 updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 12
diff changeset
23 # License along with this library; if not, write to the Free Software
7662053c3906 updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 12
diff changeset
24 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
7662053c3906 updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 12
diff changeset
25 # 02110-1301 USA
7662053c3906 updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 12
diff changeset
26 ######################### END LICENSE BLOCK #########################
7662053c3906 updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 12
diff changeset
27
7662053c3906 updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 12
diff changeset
28 from .constants import eStart
7662053c3906 updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 12
diff changeset
29 from .compat import wrap_ord
7662053c3906 updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 12
diff changeset
30
7662053c3906 updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 12
diff changeset
31
7662053c3906 updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 12
diff changeset
32 class CodingStateMachine:
7662053c3906 updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 12
diff changeset
33 def __init__(self, sm):
7662053c3906 updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 12
diff changeset
34 self._mModel = sm
7662053c3906 updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 12
diff changeset
35 self._mCurrentBytePos = 0
7662053c3906 updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 12
diff changeset
36 self._mCurrentCharLen = 0
7662053c3906 updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 12
diff changeset
37 self.reset()
7662053c3906 updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 12
diff changeset
38
7662053c3906 updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 12
diff changeset
39 def reset(self):
7662053c3906 updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 12
diff changeset
40 self._mCurrentState = eStart
7662053c3906 updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 12
diff changeset
41
7662053c3906 updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 12
diff changeset
42 def next_state(self, c):
7662053c3906 updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 12
diff changeset
43 # for each byte we get its class
7662053c3906 updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 12
diff changeset
44 # if it is first byte, we also get byte length
7662053c3906 updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 12
diff changeset
45 # PY3K: aBuf is a byte stream, so c is an int, not a byte
7662053c3906 updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 12
diff changeset
46 byteCls = self._mModel['classTable'][wrap_ord(c)]
7662053c3906 updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 12
diff changeset
47 if self._mCurrentState == eStart:
7662053c3906 updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 12
diff changeset
48 self._mCurrentBytePos = 0
7662053c3906 updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 12
diff changeset
49 self._mCurrentCharLen = self._mModel['charLenTable'][byteCls]
7662053c3906 updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 12
diff changeset
50 # from byte's class and stateTable, we get its next state
7662053c3906 updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 12
diff changeset
51 curr_state = (self._mCurrentState * self._mModel['classFactor']
7662053c3906 updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 12
diff changeset
52 + byteCls)
7662053c3906 updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 12
diff changeset
53 self._mCurrentState = self._mModel['stateTable'][curr_state]
7662053c3906 updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 12
diff changeset
54 self._mCurrentBytePos += 1
7662053c3906 updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 12
diff changeset
55 return self._mCurrentState
7662053c3906 updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 12
diff changeset
56
7662053c3906 updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 12
diff changeset
57 def get_current_charlen(self):
7662053c3906 updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 12
diff changeset
58 return self._mCurrentCharLen
7662053c3906 updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 12
diff changeset
59
7662053c3906 updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 12
diff changeset
60 def get_coding_state_machine(self):
7662053c3906 updated CharDet to 2.2.1, updated changelog
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 12
diff changeset
61 return self._mModel['name']

eric ide

mercurial