File indexing completed on 2024-04-28 03:53:03

0001 /*  -*- C++ -*-
0002     SPDX-FileCopyrightText: 1998 Netscape Communications Corporation <developer@mozilla.org>
0003 
0004     SPDX-License-Identifier: MIT
0005 */
0006 
0007 #include "nsEUCKRProber.h"
0008 
0009 namespace kencodingprober
0010 {
0011 void nsEUCKRProber::Reset(void)
0012 {
0013     mCodingSM->Reset();
0014     mState = eDetecting;
0015     mDistributionAnalyser.Reset();
0016     // mContextAnalyser.Reset();
0017 }
0018 
0019 nsProbingState nsEUCKRProber::HandleData(const char *aBuf, unsigned int aLen)
0020 {
0021     if (aLen == 0) {
0022         return mState;
0023     }
0024 
0025     for (unsigned int i = 0; i < aLen; i++) {
0026         const nsSMState codingState = mCodingSM->NextState(aBuf[i]);
0027         if (codingState == eError) {
0028             mState = eNotMe;
0029             break;
0030         }
0031         if (codingState == eItsMe) {
0032             mState = eFoundIt;
0033             break;
0034         }
0035         if (codingState == eStart) {
0036             unsigned int charLen = mCodingSM->GetCurrentCharLen();
0037 
0038             if (i == 0) {
0039                 mLastChar[1] = aBuf[0];
0040                 mDistributionAnalyser.HandleOneChar(mLastChar, charLen);
0041             } else {
0042                 mDistributionAnalyser.HandleOneChar(aBuf + i - 1, charLen);
0043             }
0044         }
0045     }
0046 
0047     mLastChar[0] = aBuf[aLen - 1];
0048 
0049     if (mState == eDetecting) {
0050         if (mDistributionAnalyser.GotEnoughData() && GetConfidence() > SHORTCUT_THRESHOLD) {
0051             mState = eFoundIt;
0052         }
0053     }
0054     //    else
0055     //      mDistributionAnalyser.HandleData(aBuf, aLen);
0056 
0057     return mState;
0058 }
0059 
0060 float nsEUCKRProber::GetConfidence(void)
0061 {
0062     float distribCf = mDistributionAnalyser.GetConfidence();
0063 
0064     return (float)distribCf;
0065 }
0066 }