1 | /* -*- C++ -*- |
---|---|
2 | SPDX-FileCopyrightText: 1998 Netscape Communications Corporation <developer@mozilla.org> |
3 | |
4 | SPDX-License-Identifier: MIT |
5 | */ |
6 | |
7 | #include "nsEUCKRProber.h" |
8 | |
9 | namespace kencodingprober |
10 | { |
11 | void nsEUCKRProber::Reset(void) |
12 | { |
13 | mCodingSM->Reset(); |
14 | mState = eDetecting; |
15 | mDistributionAnalyser.Reset(); |
16 | // mContextAnalyser.Reset(); |
17 | } |
18 | |
19 | nsProbingState nsEUCKRProber::HandleData(const char *aBuf, unsigned int aLen) |
20 | { |
21 | if (aLen == 0) { |
22 | return mState; |
23 | } |
24 | |
25 | for (unsigned int i = 0; i < aLen; i++) { |
26 | const nsSMState codingState = mCodingSM->NextState(c: aBuf[i]); |
27 | if (codingState == eError) { |
28 | mState = eNotMe; |
29 | break; |
30 | } |
31 | if (codingState == eItsMe) { |
32 | mState = eFoundIt; |
33 | break; |
34 | } |
35 | if (codingState == eStart) { |
36 | unsigned int charLen = mCodingSM->GetCurrentCharLen(); |
37 | |
38 | if (i == 0) { |
39 | mLastChar[1] = aBuf[0]; |
40 | mDistributionAnalyser.HandleOneChar(aStr: mLastChar, aCharLen: charLen); |
41 | } else { |
42 | mDistributionAnalyser.HandleOneChar(aStr: aBuf + i - 1, aCharLen: charLen); |
43 | } |
44 | } |
45 | } |
46 | |
47 | mLastChar[0] = aBuf[aLen - 1]; |
48 | |
49 | if (mState == eDetecting) { |
50 | if (mDistributionAnalyser.GotEnoughData() && GetConfidence() > SHORTCUT_THRESHOLD) { |
51 | mState = eFoundIt; |
52 | } |
53 | } |
54 | // else |
55 | // mDistributionAnalyser.HandleData(aBuf, aLen); |
56 | |
57 | return mState; |
58 | } |
59 | |
60 | float nsEUCKRProber::GetConfidence(void) |
61 | { |
62 | float distribCf = mDistributionAnalyser.GetConfidence(); |
63 | |
64 | return (float)distribCf; |
65 | } |
66 | } |
67 |