|
1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 #include "nsBig5Prober.h" |
|
7 #include "nsDebug.h" |
|
8 |
|
9 void nsBig5Prober::Reset(void) |
|
10 { |
|
11 mCodingSM->Reset(); |
|
12 mState = eDetecting; |
|
13 mDistributionAnalyser.Reset(mIsPreferredLanguage); |
|
14 } |
|
15 |
|
16 nsProbingState nsBig5Prober::HandleData(const char* aBuf, uint32_t aLen) |
|
17 { |
|
18 NS_ASSERTION(aLen, "HandleData called with empty buffer"); |
|
19 nsSMState codingState; |
|
20 |
|
21 for (uint32_t i = 0; i < aLen; i++) |
|
22 { |
|
23 codingState = mCodingSM->NextState(aBuf[i]); |
|
24 if (codingState == eItsMe) |
|
25 { |
|
26 mState = eFoundIt; |
|
27 break; |
|
28 } |
|
29 if (codingState == eStart) |
|
30 { |
|
31 uint32_t charLen = mCodingSM->GetCurrentCharLen(); |
|
32 |
|
33 if (i == 0) |
|
34 { |
|
35 mLastChar[1] = aBuf[0]; |
|
36 mDistributionAnalyser.HandleOneChar(mLastChar, charLen); |
|
37 } |
|
38 else |
|
39 mDistributionAnalyser.HandleOneChar(aBuf+i-1, charLen); |
|
40 } |
|
41 } |
|
42 |
|
43 mLastChar[0] = aBuf[aLen-1]; |
|
44 |
|
45 if (mState == eDetecting) |
|
46 if (mDistributionAnalyser.GotEnoughData() && GetConfidence() > SHORTCUT_THRESHOLD) |
|
47 mState = eFoundIt; |
|
48 |
|
49 return mState; |
|
50 } |
|
51 |
|
52 float nsBig5Prober::GetConfidence(void) |
|
53 { |
|
54 float distribCf = mDistributionAnalyser.GetConfidence(); |
|
55 |
|
56 return (float)distribCf; |
|
57 } |
|
58 |