]>
git.saurik.com Git - apple/icu.git/blob - icuSources/i18n/csr2022.cpp
2 **********************************************************************
3 * Copyright (C) 2005-2006, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 **********************************************************************
8 #include "unicode/utypes.h"
10 #if !UCONFIG_NO_CONVERSION
18 #define ARRAY_SIZE(array) (sizeof array / sizeof array[0])
21 * Matching function shared among the 2022 detectors JP, CN and KR
22 * Counts up the number of legal an unrecognized escape sequences in
23 * the sample of text, and computes a score based on the total number &
24 * the proportion that fit the encoding.
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.
32 int32_t CharsetRecog_2022::match_2022(const uint8_t *text
, int32_t textLen
, const uint8_t escapeSequences
[][5], int32_t escapeSequences_length
)
45 while(escN
< escapeSequences_length
) {
46 int32_t seq_length
= uprv_strlen((const char *) escapeSequences
[escN
]);
47 const uint8_t *seq
= escapeSequences
[escN
];
50 while(j
< seq_length
) {
51 if(seq
[j
] != text
[i
+j
]) {
68 if( text
[i
]== 0x0e || text
[i
] == 0x0f){
81 // Initial quality is based on relative proportion of recongized vs.
82 // unrecognized escape sequences.
83 // All good: quality = 100;
84 // half or less good: quality = 0;
86 quality
= (100*hits
- 100*misses
) / (hits
+ misses
);
88 // Back off quality if there were too few escape sequences seen.
89 // Include shifts in this computation, so that KR does not get penalized
90 // for having only a single Escape sequence, but many shifts.
91 if (hits
+shifts
< 5) {
92 quality
-= (5-(hits
+shifts
))*10;
103 static const uint8_t escapeSequences_2022JP
[][5] = {
104 {0x1b, 0x24, 0x28, 0x43, 0x00}, // KS X 1001:1992
105 {0x1b, 0x24, 0x28, 0x44, 0x00}, // JIS X 212-1990
106 {0x1b, 0x24, 0x40, 0x00, 0x00}, // JIS C 6226-1978
107 {0x1b, 0x24, 0x41, 0x00, 0x00}, // GB 2312-80
108 {0x1b, 0x24, 0x42, 0x00, 0x00}, // JIS X 208-1983
109 {0x1b, 0x26, 0x40, 0x00, 0x00}, // JIS X 208 1990, 1997
110 {0x1b, 0x28, 0x42, 0x00, 0x00}, // ASCII
111 {0x1b, 0x28, 0x48, 0x00, 0x00}, // JIS-Roman
112 {0x1b, 0x28, 0x49, 0x00, 0x00}, // Half-width katakana
113 {0x1b, 0x28, 0x4a, 0x00, 0x00}, // JIS-Roman
114 {0x1b, 0x2e, 0x41, 0x00, 0x00}, // ISO 8859-1
115 {0x1b, 0x2e, 0x46, 0x00, 0x00} // ISO 8859-7
118 static const uint8_t escapeSequences_2022KR
[][5] = {
119 {0x1b, 0x24, 0x29, 0x43, 0x00}
122 static const uint8_t escapeSequences_2022CN
[][5] = {
123 {0x1b, 0x24, 0x29, 0x41, 0x00}, // GB 2312-80
124 {0x1b, 0x24, 0x29, 0x47, 0x00}, // CNS 11643-1992 Plane 1
125 {0x1b, 0x24, 0x2A, 0x48, 0x00}, // CNS 11643-1992 Plane 2
126 {0x1b, 0x24, 0x29, 0x45, 0x00}, // ISO-IR-165
127 {0x1b, 0x24, 0x2B, 0x49, 0x00}, // CNS 11643-1992 Plane 3
128 {0x1b, 0x24, 0x2B, 0x4A, 0x00}, // CNS 11643-1992 Plane 4
129 {0x1b, 0x24, 0x2B, 0x4B, 0x00}, // CNS 11643-1992 Plane 5
130 {0x1b, 0x24, 0x2B, 0x4C, 0x00}, // CNS 11643-1992 Plane 6
131 {0x1b, 0x24, 0x2B, 0x4D, 0x00}, // CNS 11643-1992 Plane 7
132 {0x1b, 0x4e, 0x00, 0x00, 0x00}, // SS2
133 {0x1b, 0x4f, 0x00, 0x00, 0x00}, // SS3
136 const char *CharsetRecog_2022JP::getName() const
138 return "ISO-2022-JP";
141 int32_t CharsetRecog_2022JP::match(InputText
*textIn
)
143 return match_2022(textIn
->fInputBytes
, textIn
->fInputLen
, escapeSequences_2022JP
, ARRAY_SIZE(escapeSequences_2022JP
));
146 const char *CharsetRecog_2022KR::getName() const
148 return "ISO-2022-KR";
151 int32_t CharsetRecog_2022KR::match(InputText
*textIn
)
153 return match_2022(textIn
->fInputBytes
, textIn
->fInputLen
, escapeSequences_2022KR
, ARRAY_SIZE(escapeSequences_2022KR
));
156 const char *CharsetRecog_2022CN::getName() const
158 return "ISO-2022-CN";
161 int32_t CharsetRecog_2022CN::match(InputText
*textIn
)
163 return match_2022(textIn
->fInputBytes
, textIn
->fInputLen
, escapeSequences_2022CN
, ARRAY_SIZE(escapeSequences_2022CN
));
166 CharsetRecog_2022::~CharsetRecog_2022()