michael@0: /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "nsUTF8Prober.h" michael@0: michael@0: void nsUTF8Prober::Reset(void) michael@0: { michael@0: mCodingSM->Reset(); michael@0: mNumOfMBChar = 0; michael@0: mState = eDetecting; michael@0: } michael@0: michael@0: nsProbingState nsUTF8Prober::HandleData(const char* aBuf, uint32_t aLen) michael@0: { michael@0: nsSMState codingState; michael@0: michael@0: for (uint32_t i = 0; i < aLen; i++) michael@0: { michael@0: codingState = mCodingSM->NextState(aBuf[i]); michael@0: if (codingState == eItsMe) michael@0: { michael@0: mState = eFoundIt; michael@0: break; michael@0: } michael@0: if (codingState == eStart) michael@0: { michael@0: if (mCodingSM->GetCurrentCharLen() >= 2) michael@0: mNumOfMBChar++; michael@0: } michael@0: } michael@0: michael@0: if (mState == eDetecting) michael@0: if (GetConfidence() > SHORTCUT_THRESHOLD) michael@0: mState = eFoundIt; michael@0: return mState; michael@0: } michael@0: michael@0: #define ONE_CHAR_PROB (float)0.50 michael@0: michael@0: float nsUTF8Prober::GetConfidence(void) michael@0: { michael@0: float unlike = (float)0.99; michael@0: michael@0: if (mNumOfMBChar < 6) michael@0: { michael@0: for (uint32_t i = 0; i < mNumOfMBChar; i++) michael@0: unlike *= ONE_CHAR_PROB; michael@0: return (float)1.0 - unlike; michael@0: } michael@0: else michael@0: return (float)0.99; michael@0: } michael@0: