-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathescprober.go
118 lines (110 loc) · 3.15 KB
/
escprober.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package chardet
/*
######################## BEGIN LICENSE BLOCK ########################
# The Original Code is mozilla.org code.
#
# The Initial Developer of the Original Code is
# Netscape Communications Corporation.
# Portions created by the Initial Developer are Copyright (C) 1998
# the Initial Developer. All Rights Reserved.
#
# Contributor(s):
# Mark Pilgrim - port to Python
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
# 02110-1301 USA
######################### END LICENSE BLOCK #########################
*/
// EscCharSetProber Uses a "code scheme" approach for detecting encodings
// Whereby easily recognizable escape or shift sequences are relied on
// to identify these encodings
type EscCharSetProber struct {
CharSetProber
codingSms []*codingStateMachine
activeSmCount int
detectedCharset string
detectedLanguage string
}
func newEscCharSetProber(langFilter LanguageFilter) Prober {
p := EscCharSetProber{
*newCharSetProber(langFilter),
[]*codingStateMachine{},
0,
"",
"",
}
if langFilter != LFNone {
if langFilter&LFChineseSimplified > 0 {
p.codingSms = append(p.codingSms, newCodingStateMachine(HZSmModel))
p.codingSms = append(p.codingSms, newCodingStateMachine(ISO2022CNSmModel))
}
if langFilter&LFJapanese > 0 {
p.codingSms = append(p.codingSms, newCodingStateMachine(ISO2022JPSmModel))
}
if langFilter&LFKorean > 0 {
p.codingSms = append(p.codingSms, newCodingStateMachine(ISO2022KRSmModel))
}
}
p.reset()
return &p
}
func (e *EscCharSetProber) reset() {
e.CharSetProber.reset()
for _, c := range e.codingSms {
if c != nil {
c.active = true
c.reset()
}
}
e.activeSmCount = len(e.codingSms)
e.detectedCharset = ""
e.detectedLanguage = ""
}
func (e *EscCharSetProber) charsetName() string {
return e.detectedCharset
}
func (e *EscCharSetProber) language() string {
return e.detectedLanguage
}
func (e *EscCharSetProber) getConfidence() float64 {
if len(e.detectedCharset) > 0 {
return 0.99
}
return 0.0
}
func (e *EscCharSetProber) feed(data []byte) ProbingState {
for _, chr := range data {
for _, sm := range e.codingSms {
if sm == nil || !sm.active {
continue
}
cState := sm.nextState(chr)
if cState == MSError {
sm.active = false
e.activeSmCount--
if e.activeSmCount <= 0 {
e.state = PSNotMe
return e.state
}
} else if cState == MSItsMe {
e.state = PSFound
e.detectedCharset = sm.getCodingStateMachine()
e.detectedLanguage = sm.language()
return e.state
}
}
}
return e.state
}