File: UnicodeGroupProber.cpp

package info (click to toggle)
texmaker 6.0.1%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 72,964 kB
  • sloc: cpp: 417,556; ansic: 102,526; python: 1,935; xml: 1,739; sh: 42; makefile: 36
file content (157 lines) | stat: -rw-r--r-- 4,517 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/*  -*- C++ -*-
*  Copyright (C) 2008 <wkai@gmail.com>
*
*
*  Permission is hereby granted, free of charge, to any person obtaining
*  a copy of this software and associated documentation files (the
*  "Software"), to deal in the Software without restriction, including
*  without limitation the rights to use, copy, modify, merge, publish,
*  distribute, sublicense, and/or sell copies of the Software, and to
*  permit persons to whom the Software is furnished to do so, subject to
*  the following conditions:
*
*  The above copyright notice and this permission notice shall be included 
*  in all copies or substantial portions of the Software.
*
*  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
*  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
*  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
*  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
*  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
*  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
*  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#include "UnicodeGroupProber.h"

#include "ctype_test_p.h"

#include <QtAlgorithms>
#include <math.h>

namespace qencodingprober {
UnicodeGroupProber::UnicodeGroupProber(void)
{
  mCodingSM[0] = new nsCodingStateMachine(&UTF8SMModel);
  mCodingSM[1] = new nsCodingStateMachine(&UCS2LESMModel);
  mCodingSM[2] = new nsCodingStateMachine(&UCS2BESMModel);
  mActiveSM = NUM_OF_UNICODE_CHARSETS;
  mState = eDetecting;
  mDetectedCharset = "UTF-8";
}

UnicodeGroupProber::~UnicodeGroupProber(void)
{
  for (unsigned int i = 0; i < NUM_OF_UNICODE_CHARSETS; i++)
    delete mCodingSM[i];
}

void UnicodeGroupProber::Reset(void)
{
  mState = eDetecting;
  for (unsigned int i = 0; i < NUM_OF_UNICODE_CHARSETS; i++)
    mCodingSM[i]->Reset();
  mActiveSM = NUM_OF_UNICODE_CHARSETS;
  mDetectedCharset = "UTF-8";
}

nsProbingState UnicodeGroupProber::HandleData(const char* aBuf, unsigned int aLen)
{
  nsSMState codingState;
  int j;
  uint i, weight_BOM, counts[5];
  static bool disableUTF16LE = false;
  static bool disableUTF16BE = false;
  double weight_zero;
  
  if (mActiveSM <= 0) {
      mState = eNotMe;
      return mState;
  }
  
  if (! (disableUTF16LE || disableUTF16BE)) {
    if (aLen%2 != 0) {
            disableUTF16LE = true;
            disableUTF16BE = true;
    }      
    weight_BOM = (uint)(sqrt((double)aLen) + aLen/10.0);
    for (uint i = 0; i < 5; i++) {
         counts[i] = std::count(aBuf, aBuf + aLen, char(i));
    }
    weight_zero = (2.0*(counts[0] + counts[1] + counts[2] + counts[3] + counts[4]) + weight_BOM)/aLen;
    if (weight_zero < log(1.4142)) {
        disableUTF16LE = true;
        disableUTF16BE = true;
    }
    if (4 >= aBuf[1] && aBuf[1] >= 0 && isprint(aBuf[0]))
        disableUTF16BE = true;
    else 
        disableUTF16LE = true;
    if (disableUTF16BE)
      mActiveSM--;
    if (disableUTF16LE) {
      nsCodingStateMachine* t;
      t = mCodingSM[1];
      mCodingSM[1] = mCodingSM[2];
      mCodingSM[2] = t;
      mActiveSM--;
    }
  }
  
  for (i = 0; i < aLen; ++i) {
    for (j = mActiveSM-1; j>= 0; --j)
    {
      //byte is feed to all active state machine 
      codingState = mCodingSM[j]->NextState(aBuf[i]);
      if (codingState == eError)
      {
        //got negative answer for this state machine, make it inactive
        mActiveSM--;
        if (mActiveSM == 0)
        {
          mState = eNotMe;
          return mState;
        }
        else if (j != (int)mActiveSM)
        {
          nsCodingStateMachine* t;
          t = mCodingSM[mActiveSM];
          mCodingSM[mActiveSM] = mCodingSM[j];
          mCodingSM[j] = t;
        }
      }
      else if (codingState == eItsMe)
      {
        mState = eFoundIt;
        mDetectedCharset = mCodingSM[j]->GetCodingStateMachine();
        return mState;
      } else if (mState == eDetecting)
          mDetectedCharset = mCodingSM[j]->GetCodingStateMachine();;
    }
  }
  return mState;
}

float UnicodeGroupProber::GetConfidence()
{
  if (mState == eFoundIt)
    return 0.99f;
  else
    return 0.0f;
}

#ifdef DEBUG_PROBE
void UnicodeGroupProber::DumpStatus()
{
    GetConfidence();
    for (uint i = 0; i < mActiveSM; i++)
    {
        kDebug(180) << "Unicode group" << mCodingSM[i]->DumpCurrentState() << mCodingSM[i]->GetCodingStateMachine() ;
    }
}
#endif

}