/src/kcodecs/src/probers/nsEUCJPProber.cpp
Line | Count | Source |
1 | | /* -*- C++ -*- |
2 | | SPDX-FileCopyrightText: 1998 Netscape Communications Corporation <developer@mozilla.org> |
3 | | |
4 | | SPDX-License-Identifier: MIT |
5 | | */ |
6 | | |
7 | | // for japanese encoding, observe characteristic: |
8 | | // 1, kana character (or hankaku?) often have high frequency of appearance |
9 | | // 2, kana character often exist in group |
10 | | // 3, certain combination of kana is never used in japanese language |
11 | | |
12 | | #include "nsEUCJPProber.h" |
13 | | #include "nsMBCSSM.h" |
14 | | |
15 | | #include <format> |
16 | | |
17 | | namespace kencodingprober |
18 | | { |
19 | | nsEUCJPProber::nsEUCJPProber() |
20 | 6.51k | : mCodingSM{std::make_unique<nsCodingStateMachine>(EUCJPSMModel)} |
21 | 6.51k | { |
22 | 6.51k | } |
23 | | |
24 | | nsProbingState nsEUCJPProber::HandleData(const char *aBuf, unsigned int aLen) |
25 | 4.65k | { |
26 | 4.65k | if (aLen == 0) { |
27 | 0 | return mState; |
28 | 0 | } |
29 | | |
30 | 5.51M | for (unsigned int i = 0; i < aLen; i++) { |
31 | 5.51M | const nsSMState codingState = mCodingSM->NextState(aBuf[i]); |
32 | 5.51M | if (codingState == eError) { |
33 | 2.80k | mState = eNotMe; |
34 | 2.80k | break; |
35 | 2.80k | } |
36 | 5.51M | if (codingState == eItsMe) { |
37 | 0 | mState = eFoundIt; |
38 | 0 | break; |
39 | 0 | } |
40 | 5.51M | if (codingState == eStart) { |
41 | 2.75M | const int64_t charLen = i - mLastStart; |
42 | | |
43 | 2.75M | if (i == 0) { |
44 | 3.34k | mLastChar[1] = aBuf[0]; |
45 | 3.34k | mContextAnalyser.HandleOneChar(mLastChar, charLen); |
46 | 3.34k | mDistributionAnalyser.HandleOneChar(mLastChar, charLen); |
47 | 2.75M | } else { |
48 | 2.75M | mContextAnalyser.HandleOneChar(aBuf + i - 1, charLen); |
49 | 2.75M | mDistributionAnalyser.HandleOneChar(aBuf + i - 1, charLen); |
50 | 2.75M | } |
51 | 2.75M | mLastStart = i; |
52 | 2.75M | } |
53 | 5.51M | } |
54 | | |
55 | 4.65k | mLastChar[0] = aBuf[aLen - 1]; |
56 | 4.65k | mLastStart -= aLen; |
57 | | |
58 | 4.65k | if (mState == eDetecting) { |
59 | 1.85k | if (mContextAnalyser.GotEnoughData() && GetConfidence() > SHORTCUT_THRESHOLD) { |
60 | 67 | mState = eFoundIt; |
61 | 67 | } |
62 | 1.85k | } |
63 | | |
64 | 4.65k | return mState; |
65 | 4.65k | } |
66 | | |
67 | | float nsEUCJPProber::GetConfidence(void) |
68 | 7.17k | { |
69 | 7.17k | float contxtCf = mContextAnalyser.GetConfidence(); |
70 | 7.17k | float distribCf = mDistributionAnalyser.GetConfidence(); |
71 | | |
72 | 7.17k | return (contxtCf > distribCf ? contxtCf : distribCf); |
73 | 7.17k | } |
74 | | |
75 | | std::string nsEUCJPProber::StatusOutput(uint8_t /* indent */) |
76 | 0 | { |
77 | 0 | return std::format( // |
78 | 0 | "{:1.3f} [{}] {} {}", |
79 | 0 | GetConfidence(), |
80 | 0 | GetCharSetName(), |
81 | 0 | mDistributionAnalyser.StatusOutput(), |
82 | 0 | mContextAnalyser.StatusOutput()); |
83 | 0 | } |
84 | | } |