]>
git.saurik.com Git - apple/icu.git/blob - icuSources/i18n/csr2022.cpp
1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 **********************************************************************
5 * Copyright (C) 2005-2016, International Business Machines
6 * Corporation and others. All Rights Reserved.
7 **********************************************************************
10 #include "unicode/utypes.h"
12 #if !UCONFIG_NO_CONVERSION
23 * Matching function shared among the 2022 detectors JP, CN and KR
24 * Counts up the number of legal and unrecognized escape sequences in
25 * the sample of text, and computes a score based on the total number &
26 * the proportion that fit the encoding.
29 * @param text the byte buffer containing text to analyse
30 * @param textLen the size of the text in the byte.
31 * @param escapeSequences the byte escape sequences to test for.
32 * @return match quality, in the range of 0-100.
34 int32_t CharsetRecog_2022::match_2022(const uint8_t *text
, int32_t textLen
, const uint8_t escapeSequences
[][5], int32_t escapeSequences_length
) const
47 while(escN
< escapeSequences_length
) {
48 const uint8_t *seq
= escapeSequences
[escN
];
49 int32_t seq_length
= (int32_t)uprv_strlen((const char *) seq
);
51 if (textLen
-i
>= seq_length
) {
53 while(j
< seq_length
) {
54 if(seq
[j
] != text
[i
+j
]) {
65 // else we ran out of string to compare this time.
73 if( text
[i
]== 0x0e || text
[i
] == 0x0f){
86 // Initial quality is based on relative proportion of recongized vs.
87 // unrecognized escape sequences.
88 // All good: quality = 100;
89 // half or less good: quality = 0;
91 quality
= (100*hits
- 100*misses
) / (hits
+ misses
);
93 // Back off quality if there were too few escape sequences seen.
94 // Include shifts in this computation, so that KR does not get penalized
95 // for having only a single Escape sequence, but many shifts.
96 if (hits
+shifts
< 5) {
97 quality
-= (5-(hits
+shifts
))*10;
108 static const uint8_t escapeSequences_2022JP
[][5] = {
109 {0x1b, 0x24, 0x28, 0x43, 0x00}, // KS X 1001:1992
110 {0x1b, 0x24, 0x28, 0x44, 0x00}, // JIS X 212-1990
111 {0x1b, 0x24, 0x40, 0x00, 0x00}, // JIS C 6226-1978
112 {0x1b, 0x24, 0x41, 0x00, 0x00}, // GB 2312-80
113 {0x1b, 0x24, 0x42, 0x00, 0x00}, // JIS X 208-1983
114 {0x1b, 0x26, 0x40, 0x00, 0x00}, // JIS X 208 1990, 1997
115 {0x1b, 0x28, 0x42, 0x00, 0x00}, // ASCII
116 {0x1b, 0x28, 0x48, 0x00, 0x00}, // JIS-Roman
117 {0x1b, 0x28, 0x49, 0x00, 0x00}, // Half-width katakana
118 {0x1b, 0x28, 0x4a, 0x00, 0x00}, // JIS-Roman
119 {0x1b, 0x2e, 0x41, 0x00, 0x00}, // ISO 8859-1
120 {0x1b, 0x2e, 0x46, 0x00, 0x00} // ISO 8859-7
123 #if !UCONFIG_ONLY_HTML_CONVERSION
124 static const uint8_t escapeSequences_2022KR
[][5] = {
125 {0x1b, 0x24, 0x29, 0x43, 0x00}
128 static const uint8_t escapeSequences_2022CN
[][5] = {
129 {0x1b, 0x24, 0x29, 0x41, 0x00}, // GB 2312-80
130 {0x1b, 0x24, 0x29, 0x47, 0x00}, // CNS 11643-1992 Plane 1
131 {0x1b, 0x24, 0x2A, 0x48, 0x00}, // CNS 11643-1992 Plane 2
132 {0x1b, 0x24, 0x29, 0x45, 0x00}, // ISO-IR-165
133 {0x1b, 0x24, 0x2B, 0x49, 0x00}, // CNS 11643-1992 Plane 3
134 {0x1b, 0x24, 0x2B, 0x4A, 0x00}, // CNS 11643-1992 Plane 4
135 {0x1b, 0x24, 0x2B, 0x4B, 0x00}, // CNS 11643-1992 Plane 5
136 {0x1b, 0x24, 0x2B, 0x4C, 0x00}, // CNS 11643-1992 Plane 6
137 {0x1b, 0x24, 0x2B, 0x4D, 0x00}, // CNS 11643-1992 Plane 7
138 {0x1b, 0x4e, 0x00, 0x00, 0x00}, // SS2
139 {0x1b, 0x4f, 0x00, 0x00, 0x00}, // SS3
143 CharsetRecog_2022JP::~CharsetRecog_2022JP() {}
145 const char *CharsetRecog_2022JP::getName() const {
146 return "ISO-2022-JP";
149 UBool
CharsetRecog_2022JP::match(InputText
*textIn
, CharsetMatch
*results
) const {
150 int32_t confidence
= match_2022(textIn
->fInputBytes
,
152 escapeSequences_2022JP
,
153 UPRV_LENGTHOF(escapeSequences_2022JP
));
154 results
->set(textIn
, this, confidence
);
155 return (confidence
> 0);
158 #if !UCONFIG_ONLY_HTML_CONVERSION
159 CharsetRecog_2022KR::~CharsetRecog_2022KR() {}
161 const char *CharsetRecog_2022KR::getName() const {
162 return "ISO-2022-KR";
165 UBool
CharsetRecog_2022KR::match(InputText
*textIn
, CharsetMatch
*results
) const {
166 int32_t confidence
= match_2022(textIn
->fInputBytes
,
168 escapeSequences_2022KR
,
169 UPRV_LENGTHOF(escapeSequences_2022KR
));
170 results
->set(textIn
, this, confidence
);
171 return (confidence
> 0);
174 CharsetRecog_2022CN::~CharsetRecog_2022CN() {}
176 const char *CharsetRecog_2022CN::getName() const {
177 return "ISO-2022-CN";
180 UBool
CharsetRecog_2022CN::match(InputText
*textIn
, CharsetMatch
*results
) const {
181 int32_t confidence
= match_2022(textIn
->fInputBytes
,
183 escapeSequences_2022CN
,
184 UPRV_LENGTHOF(escapeSequences_2022CN
));
185 results
->set(textIn
, this, confidence
);
186 return (confidence
> 0);
190 CharsetRecog_2022::~CharsetRecog_2022() {