/src/mozilla-central/extensions/universalchardet/src/base/nsUTF8Prober.cpp
Line | Count | Source (jump to first uncovered line) |
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 "nsUTF8Prober.h" |
7 | | |
8 | | void nsUTF8Prober::Reset(void) |
9 | 0 | { |
10 | 0 | mCodingSM->Reset(); |
11 | 0 | mNumOfMBChar = 0; |
12 | 0 | mState = eDetecting; |
13 | 0 | } |
14 | | |
15 | | nsProbingState nsUTF8Prober::HandleData(const char* aBuf, uint32_t aLen) |
16 | 0 | { |
17 | 0 | uint32_t codingState; |
18 | 0 |
|
19 | 0 | for (uint32_t i = 0; i < aLen; i++) |
20 | 0 | { |
21 | 0 | codingState = mCodingSM->NextState(aBuf[i]); |
22 | 0 | if (codingState == eItsMe) |
23 | 0 | { |
24 | 0 | mState = eFoundIt; |
25 | 0 | break; |
26 | 0 | } |
27 | 0 | if (codingState == eStart) |
28 | 0 | { |
29 | 0 | if (mCodingSM->GetCurrentCharLen() >= 2) |
30 | 0 | mNumOfMBChar++; |
31 | 0 | } |
32 | 0 | } |
33 | 0 |
|
34 | 0 | if (mState == eDetecting) |
35 | 0 | if (GetConfidence() > SHORTCUT_THRESHOLD) |
36 | 0 | mState = eFoundIt; |
37 | 0 | return mState; |
38 | 0 | } |
39 | | |
40 | 0 | #define ONE_CHAR_PROB (float)0.50 |
41 | | |
42 | | float nsUTF8Prober::GetConfidence(void) |
43 | 0 | { |
44 | 0 | float unlike = (float)0.99; |
45 | 0 |
|
46 | 0 | if (mNumOfMBChar < 6) |
47 | 0 | { |
48 | 0 | for (uint32_t i = 0; i < mNumOfMBChar; i++) |
49 | 0 | unlike *= ONE_CHAR_PROB; |
50 | 0 | return (float)1.0 - unlike; |
51 | 0 | } |
52 | 0 | else |
53 | 0 | return (float)0.99; |
54 | 0 | } |
55 | | |