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