]> git.saurik.com Git - apple/icu.git/blame - icuSources/i18n/csr2022.cpp
ICU-57149.0.1.tar.gz
[apple/icu.git] / icuSources / i18n / csr2022.cpp
CommitLineData
73c04bcf
A
1/*
2 **********************************************************************
2ca993e8 3 * Copyright (C) 2005-2016, International Business Machines
73c04bcf
A
4 * Corporation and others. All Rights Reserved.
5 **********************************************************************
6 */
7
8#include "unicode/utypes.h"
9
10#if !UCONFIG_NO_CONVERSION
11
2ca993e8 12#include "cmemory.h"
73c04bcf
A
13#include "cstring.h"
14
15#include "csr2022.h"
51004dcb 16#include "csmatch.h"
73c04bcf
A
17
18U_NAMESPACE_BEGIN
19
73c04bcf
A
20/**
21 * Matching function shared among the 2022 detectors JP, CN and KR
51004dcb 22 * Counts up the number of legal and unrecognized escape sequences in
73c04bcf
A
23 * the sample of text, and computes a score based on the total number &
24 * the proportion that fit the encoding.
25 *
26 *
27 * @param text the byte buffer containing text to analyse
28 * @param textLen the size of the text in the byte.
29 * @param escapeSequences the byte escape sequences to test for.
30 * @return match quality, in the range of 0-100.
31 */
51004dcb 32int32_t CharsetRecog_2022::match_2022(const uint8_t *text, int32_t textLen, const uint8_t escapeSequences[][5], int32_t escapeSequences_length) const
73c04bcf
A
33{
34 int32_t i, j;
35 int32_t escN;
36 int32_t hits = 0;
37 int32_t misses = 0;
38 int32_t shifts = 0;
39 int32_t quality;
40
41 i = 0;
42 while(i < textLen) {
43 if(text[i] == 0x1B) {
44 escN = 0;
45 while(escN < escapeSequences_length) {
73c04bcf 46 const uint8_t *seq = escapeSequences[escN];
729e4ab9 47 int32_t seq_length = (int32_t)uprv_strlen((const char *) seq);
73c04bcf 48
46f4442e
A
49 if (textLen-i >= seq_length) {
50 j = 1;
51 while(j < seq_length) {
52 if(seq[j] != text[i+j]) {
53 goto checkEscapes;
54 }
55
56 j += 1;
73c04bcf
A
57 }
58
46f4442e
A
59 hits += 1;
60 i += seq_length-1;
61 goto scanInput;
73c04bcf 62 }
46f4442e 63 // else we ran out of string to compare this time.
73c04bcf
A
64checkEscapes:
65 escN += 1;
66 }
67
68 misses += 1;
69 }
70
71 if( text[i]== 0x0e || text[i] == 0x0f){
72 shifts += 1;
73 }
74
75scanInput:
76 i += 1;
77 }
78
79 if (hits == 0) {
80 return 0;
81 }
82
83 //
84 // Initial quality is based on relative proportion of recongized vs.
85 // unrecognized escape sequences.
86 // All good: quality = 100;
87 // half or less good: quality = 0;
88 // linear inbetween.
89 quality = (100*hits - 100*misses) / (hits + misses);
90
91 // Back off quality if there were too few escape sequences seen.
92 // Include shifts in this computation, so that KR does not get penalized
93 // for having only a single Escape sequence, but many shifts.
94 if (hits+shifts < 5) {
95 quality -= (5-(hits+shifts))*10;
96 }
97
98 if (quality < 0) {
99 quality = 0;
100 }
101
102 return quality;
103}
104
105
106static const uint8_t escapeSequences_2022JP[][5] = {
107 {0x1b, 0x24, 0x28, 0x43, 0x00}, // KS X 1001:1992
108 {0x1b, 0x24, 0x28, 0x44, 0x00}, // JIS X 212-1990
109 {0x1b, 0x24, 0x40, 0x00, 0x00}, // JIS C 6226-1978
110 {0x1b, 0x24, 0x41, 0x00, 0x00}, // GB 2312-80
111 {0x1b, 0x24, 0x42, 0x00, 0x00}, // JIS X 208-1983
112 {0x1b, 0x26, 0x40, 0x00, 0x00}, // JIS X 208 1990, 1997
113 {0x1b, 0x28, 0x42, 0x00, 0x00}, // ASCII
114 {0x1b, 0x28, 0x48, 0x00, 0x00}, // JIS-Roman
115 {0x1b, 0x28, 0x49, 0x00, 0x00}, // Half-width katakana
116 {0x1b, 0x28, 0x4a, 0x00, 0x00}, // JIS-Roman
117 {0x1b, 0x2e, 0x41, 0x00, 0x00}, // ISO 8859-1
118 {0x1b, 0x2e, 0x46, 0x00, 0x00} // ISO 8859-7
119};
120
b331163b 121#if !UCONFIG_ONLY_HTML_CONVERSION
73c04bcf
A
122static const uint8_t escapeSequences_2022KR[][5] = {
123 {0x1b, 0x24, 0x29, 0x43, 0x00}
124};
125
126static const uint8_t escapeSequences_2022CN[][5] = {
127 {0x1b, 0x24, 0x29, 0x41, 0x00}, // GB 2312-80
128 {0x1b, 0x24, 0x29, 0x47, 0x00}, // CNS 11643-1992 Plane 1
129 {0x1b, 0x24, 0x2A, 0x48, 0x00}, // CNS 11643-1992 Plane 2
130 {0x1b, 0x24, 0x29, 0x45, 0x00}, // ISO-IR-165
131 {0x1b, 0x24, 0x2B, 0x49, 0x00}, // CNS 11643-1992 Plane 3
132 {0x1b, 0x24, 0x2B, 0x4A, 0x00}, // CNS 11643-1992 Plane 4
133 {0x1b, 0x24, 0x2B, 0x4B, 0x00}, // CNS 11643-1992 Plane 5
134 {0x1b, 0x24, 0x2B, 0x4C, 0x00}, // CNS 11643-1992 Plane 6
135 {0x1b, 0x24, 0x2B, 0x4D, 0x00}, // CNS 11643-1992 Plane 7
136 {0x1b, 0x4e, 0x00, 0x00, 0x00}, // SS2
137 {0x1b, 0x4f, 0x00, 0x00, 0x00}, // SS3
138};
b331163b 139#endif
73c04bcf 140
4388f060
A
141CharsetRecog_2022JP::~CharsetRecog_2022JP() {}
142
51004dcb 143const char *CharsetRecog_2022JP::getName() const {
73c04bcf
A
144 return "ISO-2022-JP";
145}
146
51004dcb
A
147UBool CharsetRecog_2022JP::match(InputText *textIn, CharsetMatch *results) const {
148 int32_t confidence = match_2022(textIn->fInputBytes,
149 textIn->fInputLen,
150 escapeSequences_2022JP,
2ca993e8 151 UPRV_LENGTHOF(escapeSequences_2022JP));
51004dcb
A
152 results->set(textIn, this, confidence);
153 return (confidence > 0);
73c04bcf
A
154}
155
b331163b 156#if !UCONFIG_ONLY_HTML_CONVERSION
4388f060
A
157CharsetRecog_2022KR::~CharsetRecog_2022KR() {}
158
51004dcb 159const char *CharsetRecog_2022KR::getName() const {
73c04bcf
A
160 return "ISO-2022-KR";
161}
162
51004dcb
A
163UBool CharsetRecog_2022KR::match(InputText *textIn, CharsetMatch *results) const {
164 int32_t confidence = match_2022(textIn->fInputBytes,
165 textIn->fInputLen,
166 escapeSequences_2022KR,
2ca993e8 167 UPRV_LENGTHOF(escapeSequences_2022KR));
51004dcb
A
168 results->set(textIn, this, confidence);
169 return (confidence > 0);
73c04bcf
A
170}
171
4388f060
A
172CharsetRecog_2022CN::~CharsetRecog_2022CN() {}
173
51004dcb 174const char *CharsetRecog_2022CN::getName() const {
73c04bcf
A
175 return "ISO-2022-CN";
176}
177
51004dcb
A
178UBool CharsetRecog_2022CN::match(InputText *textIn, CharsetMatch *results) const {
179 int32_t confidence = match_2022(textIn->fInputBytes,
180 textIn->fInputLen,
181 escapeSequences_2022CN,
2ca993e8 182 UPRV_LENGTHOF(escapeSequences_2022CN));
51004dcb
A
183 results->set(textIn, this, confidence);
184 return (confidence > 0);
73c04bcf 185}
b331163b 186#endif
73c04bcf 187
51004dcb 188CharsetRecog_2022::~CharsetRecog_2022() {
73c04bcf
A
189 // nothing to do
190}
191
192U_NAMESPACE_END
193#endif