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