]>
Commit | Line | Data |
---|---|---|
b75a7d8f A |
1 | /* |
2 | ********************************************************************** | |
3 | * Copyright (C) 1999-2003, International Business Machines | |
4 | * Corporation and others. All Rights Reserved. | |
5 | ********************************************************************** | |
6 | * Date Name Description | |
7 | * 11/17/99 aliu Creation. | |
8 | ********************************************************************** | |
9 | */ | |
10 | ||
11 | #include "unicode/utypes.h" | |
12 | ||
13 | #if !UCONFIG_NO_TRANSLITERATION | |
14 | ||
15 | #include "unicode/unifilt.h" | |
16 | #include "unicode/unifltlg.h" | |
17 | #include "unicode/uniset.h" | |
18 | #include "cpdtrans.h" | |
19 | #include "uvector.h" | |
20 | #include "tridpars.h" | |
21 | #include "cmemory.h" | |
22 | ||
23 | // keep in sync with Transliterator | |
24 | static const UChar ID_SEP = 0x002D; /*-*/ | |
25 | static const UChar ID_DELIM = 0x003B; /*;*/ | |
26 | static const UChar NEWLINE = 10; | |
27 | ||
28 | // Empty string | |
29 | static const UChar EMPTY[] = {0}; //"" | |
30 | ||
31 | U_NAMESPACE_BEGIN | |
32 | ||
33 | const char CompoundTransliterator::fgClassID=0; | |
34 | ||
35 | /** | |
36 | * Constructs a new compound transliterator given an array of | |
37 | * transliterators. The array of transliterators may be of any | |
38 | * length, including zero or one, however, useful compound | |
39 | * transliterators have at least two components. | |
40 | * @param transliterators array of <code>Transliterator</code> | |
41 | * objects | |
42 | * @param transliteratorCount The number of | |
43 | * <code>Transliterator</code> objects in transliterators. | |
44 | * @param filter the filter. Any character for which | |
45 | * <tt>filter.contains()</tt> returns <tt>false</tt> will not be | |
46 | * altered by this transliterator. If <tt>filter</tt> is | |
47 | * <tt>null</tt> then no filtering is applied. | |
48 | */ | |
49 | CompoundTransliterator::CompoundTransliterator( | |
50 | Transliterator* const transliterators[], | |
51 | int32_t transliteratorCount, | |
52 | UnicodeFilter* adoptedFilter) : | |
53 | Transliterator(joinIDs(transliterators, transliteratorCount), adoptedFilter), | |
54 | trans(0), count(0), compoundRBTIndex(-1) { | |
55 | setTransliterators(transliterators, transliteratorCount); | |
56 | } | |
57 | ||
58 | /** | |
59 | * Splits an ID of the form "ID;ID;..." into a compound using each | |
60 | * of the IDs. | |
61 | * @param id of above form | |
62 | * @param forward if false, does the list in reverse order, and | |
63 | * takes the inverse of each ID. | |
64 | */ | |
65 | CompoundTransliterator::CompoundTransliterator(const UnicodeString& id, | |
66 | UTransDirection direction, | |
67 | UnicodeFilter* adoptedFilter, | |
68 | UParseError& /*parseError*/, | |
69 | UErrorCode& status) : | |
70 | Transliterator(id, adoptedFilter), | |
71 | trans(0), compoundRBTIndex(-1) { | |
72 | // TODO add code for parseError...currently unused, but | |
73 | // later may be used by parsing code... | |
74 | init(id, direction, -1, 0, TRUE, status); | |
75 | } | |
76 | ||
77 | CompoundTransliterator::CompoundTransliterator(const UnicodeString& id, | |
78 | UParseError& /*parseError*/, | |
79 | UErrorCode& status) : | |
80 | Transliterator(id, 0), // set filter to 0 here! | |
81 | trans(0), compoundRBTIndex(-1) { | |
82 | // TODO add code for parseError...currently unused, but | |
83 | // later may be used by parsing code... | |
84 | init(id, UTRANS_FORWARD, -1, 0, TRUE, status); | |
85 | } | |
86 | ||
87 | /** | |
88 | * Private constructor for Transliterator from a vector of | |
89 | * transliterators. The caller is responsible for fixing up the | |
90 | * ID. | |
91 | */ | |
92 | CompoundTransliterator::CompoundTransliterator(UVector& list, | |
93 | UParseError& /*parseError*/, | |
94 | UErrorCode& status) : | |
95 | Transliterator(EMPTY, NULL), | |
96 | trans(0), compoundRBTIndex(-1) | |
97 | { | |
98 | // TODO add code for parseError...currently unused, but | |
99 | // later may be used by parsing code... | |
100 | init(list, UTRANS_FORWARD, FALSE, status); | |
101 | // assume caller will fixup ID | |
102 | } | |
103 | ||
104 | /** | |
105 | * Private constructor for compound RBTs. Construct a compound | |
106 | * transliterator using the given idBlock, with the adoptedTrans | |
107 | * inserted at the idSplitPoint. | |
108 | */ | |
109 | CompoundTransliterator::CompoundTransliterator(const UnicodeString& newID, | |
110 | const UnicodeString& idBlock, | |
111 | int32_t idSplitPoint, | |
112 | Transliterator *adoptedTrans, | |
113 | UErrorCode& status) : | |
114 | Transliterator(newID, 0), | |
115 | trans(0), compoundRBTIndex(-1) | |
116 | { | |
117 | init(idBlock, UTRANS_FORWARD, idSplitPoint, adoptedTrans, FALSE, status); | |
118 | } | |
119 | ||
120 | /** | |
121 | * Finish constructing a transliterator: only to be called by | |
122 | * constructors. Before calling init(), set trans and filter to NULL. | |
123 | * @param id the id containing ';'-separated entries | |
124 | * @param direction either FORWARD or REVERSE | |
125 | * @param idSplitPoint the index into id at which the | |
126 | * adoptedSplitTransliterator should be inserted, if there is one, or | |
127 | * -1 if there is none. | |
128 | * @param adoptedSplitTransliterator a transliterator to be inserted | |
129 | * before the entry at offset idSplitPoint in the id string. May be | |
130 | * NULL to insert no entry. | |
131 | * @param fixReverseID if TRUE, then reconstruct the ID of reverse | |
132 | * entries by calling getID() of component entries. Some constructors | |
133 | * do not require this because they apply a facade ID anyway. | |
134 | * @param status the error code indicating success or failure | |
135 | */ | |
136 | void CompoundTransliterator::init(const UnicodeString& id, | |
137 | UTransDirection direction, | |
138 | int32_t idSplitPoint, | |
139 | Transliterator *adoptedSplitTrans, | |
140 | UBool fixReverseID, | |
141 | UErrorCode& status) { | |
142 | // assert(trans == 0); | |
143 | ||
144 | if (U_FAILURE(status)) { | |
145 | delete adoptedSplitTrans; | |
146 | return; | |
147 | } | |
148 | ||
149 | UVector list(status); | |
150 | UnicodeSet* compoundFilter = NULL; | |
151 | UnicodeString regenID; | |
152 | if (!TransliteratorIDParser::parseCompoundID(id, direction, | |
153 | regenID, list, compoundFilter)) { | |
154 | status = U_INVALID_ID; | |
155 | delete adoptedSplitTrans; | |
156 | delete compoundFilter; | |
157 | return; | |
158 | } | |
159 | ||
160 | compoundRBTIndex = TransliteratorIDParser::instantiateList(list, adoptedSplitTrans, idSplitPoint, status); | |
161 | ||
162 | init(list, direction, fixReverseID, status); | |
163 | ||
164 | if (compoundFilter != NULL) { | |
165 | adoptFilter(compoundFilter); | |
166 | } | |
167 | } | |
168 | ||
169 | /** | |
170 | * Finish constructing a transliterator: only to be called by | |
171 | * constructors. Before calling init(), set trans and filter to NULL. | |
172 | * @param list a vector of transliterator objects to be adopted. It | |
173 | * should NOT be empty. The list should be in declared order. That | |
174 | * is, it should be in the FORWARD order; if direction is REVERSE then | |
175 | * the list order will be reversed. | |
176 | * @param direction either FORWARD or REVERSE | |
177 | * @param fixReverseID if TRUE, then reconstruct the ID of reverse | |
178 | * entries by calling getID() of component entries. Some constructors | |
179 | * do not require this because they apply a facade ID anyway. | |
180 | * @param status the error code indicating success or failure | |
181 | */ | |
182 | void CompoundTransliterator::init(UVector& list, | |
183 | UTransDirection direction, | |
184 | UBool fixReverseID, | |
185 | UErrorCode& status) { | |
186 | // assert(trans == 0); | |
187 | ||
188 | // Allocate array | |
189 | if (U_SUCCESS(status)) { | |
190 | count = list.size(); | |
191 | trans = (Transliterator **)uprv_malloc(count * sizeof(Transliterator *)); | |
192 | /* test for NULL */ | |
193 | if (trans == 0) { | |
194 | status = U_MEMORY_ALLOCATION_ERROR; | |
195 | return; | |
196 | } | |
197 | } | |
198 | ||
199 | if (U_FAILURE(status) || trans == 0) { | |
200 | // assert(trans == 0); | |
201 | return; | |
202 | } | |
203 | ||
204 | // Move the transliterators from the vector into an array. | |
205 | // Reverse the order if necessary. | |
206 | int32_t i; | |
207 | for (i=0; i<count; ++i) { | |
208 | int32_t j = (direction == UTRANS_FORWARD) ? i : count - 1 - i; | |
209 | trans[i] = (Transliterator*) list.elementAt(j); | |
210 | } | |
211 | ||
212 | // Fix compoundRBTIndex for REVERSE transliterators | |
213 | if (compoundRBTIndex >= 0 && direction == UTRANS_REVERSE) { | |
214 | compoundRBTIndex = count - 1 - compoundRBTIndex; | |
215 | } | |
216 | ||
217 | // If the direction is UTRANS_REVERSE then we may need to fix the | |
218 | // ID. | |
219 | if (direction == UTRANS_REVERSE && fixReverseID) { | |
220 | UnicodeString newID; | |
221 | for (i=0; i<count; ++i) { | |
222 | if (i > 0) { | |
223 | newID.append(ID_DELIM); | |
224 | } | |
225 | newID.append(trans[i]->getID()); | |
226 | } | |
227 | setID(newID); | |
228 | } | |
229 | ||
230 | computeMaximumContextLength(); | |
231 | } | |
232 | ||
233 | /** | |
234 | * Return the IDs of the given list of transliterators, concatenated | |
235 | * with ID_DELIM delimiting them. Equivalent to the perlish expression | |
236 | * join(ID_DELIM, map($_.getID(), transliterators). | |
237 | */ | |
238 | UnicodeString CompoundTransliterator::joinIDs(Transliterator* const transliterators[], | |
239 | int32_t transCount) { | |
240 | UnicodeString id; | |
241 | for (int32_t i=0; i<transCount; ++i) { | |
242 | if (i > 0) { | |
243 | id.append(ID_DELIM); | |
244 | } | |
245 | id.append(transliterators[i]->getID()); | |
246 | } | |
247 | return id; // Return temporary | |
248 | } | |
249 | ||
250 | /** | |
251 | * Copy constructor. | |
252 | */ | |
253 | CompoundTransliterator::CompoundTransliterator(const CompoundTransliterator& t) : | |
254 | Transliterator(t), trans(0), count(0), compoundRBTIndex(-1) { | |
255 | *this = t; | |
256 | } | |
257 | ||
258 | /** | |
259 | * Destructor | |
260 | */ | |
261 | CompoundTransliterator::~CompoundTransliterator() { | |
262 | freeTransliterators(); | |
263 | } | |
264 | ||
265 | void CompoundTransliterator::freeTransliterators(void) { | |
266 | if (trans != 0) { | |
267 | for (int32_t i=0; i<count; ++i) { | |
268 | delete trans[i]; | |
269 | } | |
270 | uprv_free(trans); | |
271 | } | |
272 | trans = 0; | |
273 | count = 0; | |
274 | } | |
275 | ||
276 | /** | |
277 | * Assignment operator. | |
278 | */ | |
279 | CompoundTransliterator& CompoundTransliterator::operator=( | |
280 | const CompoundTransliterator& t) { | |
281 | Transliterator::operator=(t); | |
282 | int32_t i; | |
283 | for (i=0; i<count; ++i) { | |
284 | delete trans[i]; | |
285 | trans[i] = 0; | |
286 | } | |
287 | if (t.count > count) { | |
288 | uprv_free(trans); | |
289 | trans = (Transliterator **)uprv_malloc(t.count * sizeof(Transliterator *)); | |
290 | } | |
291 | count = t.count; | |
292 | for (i=0; i<count; ++i) { | |
293 | trans[i] = t.trans[i]->clone(); | |
294 | } | |
295 | compoundRBTIndex = t.compoundRBTIndex; | |
296 | return *this; | |
297 | } | |
298 | ||
299 | /** | |
300 | * Transliterator API. | |
301 | */ | |
302 | Transliterator* CompoundTransliterator::clone(void) const { | |
303 | return new CompoundTransliterator(*this); | |
304 | } | |
305 | ||
306 | /** | |
307 | * Returns the number of transliterators in this chain. | |
308 | * @return number of transliterators in this chain. | |
309 | */ | |
310 | int32_t CompoundTransliterator::getCount(void) const { | |
311 | return count; | |
312 | } | |
313 | ||
314 | /** | |
315 | * Returns the transliterator at the given index in this chain. | |
316 | * @param index index into chain, from 0 to <code>getCount() - 1</code> | |
317 | * @return transliterator at the given index | |
318 | */ | |
319 | const Transliterator& CompoundTransliterator::getTransliterator(int32_t index) const { | |
320 | return *trans[index]; | |
321 | } | |
322 | ||
323 | void CompoundTransliterator::setTransliterators(Transliterator* const transliterators[], | |
324 | int32_t transCount) { | |
325 | Transliterator** a = (Transliterator **)uprv_malloc(transCount * sizeof(Transliterator *)); | |
326 | for (int32_t i=0; i<transCount; ++i) { | |
327 | a[i] = transliterators[i]->clone(); | |
328 | } | |
329 | adoptTransliterators(a, transCount); | |
330 | } | |
331 | ||
332 | void CompoundTransliterator::adoptTransliterators(Transliterator* adoptedTransliterators[], | |
333 | int32_t transCount) { | |
334 | // First free trans[] and set count to zero. Once this is done, | |
335 | // orphan the filter. Set up the new trans[]. | |
336 | freeTransliterators(); | |
337 | trans = adoptedTransliterators; | |
338 | count = transCount; | |
339 | computeMaximumContextLength(); | |
340 | setID(joinIDs(trans, count)); | |
341 | } | |
342 | ||
343 | /** | |
344 | * Append c to buf, unless buf is empty or buf already ends in c. | |
345 | */ | |
346 | static void _smartAppend(UnicodeString& buf, UChar c) { | |
347 | if (buf.length() != 0 && | |
348 | buf.charAt(buf.length() - 1) != c) { | |
349 | buf.append(c); | |
350 | } | |
351 | } | |
352 | ||
353 | UnicodeString& CompoundTransliterator::toRules(UnicodeString& rulesSource, | |
354 | UBool escapeUnprintable) const { | |
355 | // We do NOT call toRules() on our component transliterators, in | |
356 | // general. If we have several rule-based transliterators, this | |
357 | // yields a concatenation of the rules -- not what we want. We do | |
358 | // handle compound RBT transliterators specially -- those for which | |
359 | // compoundRBTIndex >= 0. For the transliterator at compoundRBTIndex, | |
360 | // we do call toRules() recursively. | |
361 | rulesSource.truncate(0); | |
362 | if (compoundRBTIndex >= 0 && getFilter() != NULL) { | |
363 | // If we are a compound RBT and if we have a global | |
364 | // filter, then emit it at the top. | |
365 | UnicodeString pat; | |
366 | rulesSource.append("::").append(getFilter()->toPattern(pat, escapeUnprintable)).append(ID_DELIM); | |
367 | } | |
368 | for (int32_t i=0; i<count; ++i) { | |
369 | UnicodeString rule; | |
370 | if (i == compoundRBTIndex) { | |
371 | trans[i]->toRules(rule, escapeUnprintable); | |
372 | } else { | |
373 | trans[i]->Transliterator::toRules(rule, escapeUnprintable); | |
374 | } | |
375 | _smartAppend(rulesSource, NEWLINE); | |
376 | rulesSource.append(rule); | |
377 | _smartAppend(rulesSource, ID_DELIM); | |
378 | } | |
379 | return rulesSource; | |
380 | } | |
381 | ||
382 | /** | |
383 | * Implement Transliterator framework | |
384 | */ | |
385 | void CompoundTransliterator::handleGetSourceSet(UnicodeSet& result) const { | |
386 | UnicodeSet set; | |
387 | result.clear(); | |
388 | for (int32_t i=0; i<count; ++i) { | |
389 | result.addAll(trans[i]->getSourceSet(set)); | |
390 | // Take the example of Hiragana-Latin. This is really | |
391 | // Hiragana-Katakana; Katakana-Latin. The source set of | |
392 | // these two is roughly [:Hiragana:] and [:Katakana:]. | |
393 | // But the source set for the entire transliterator is | |
394 | // actually [:Hiragana:] ONLY -- that is, the first | |
395 | // non-empty source set. | |
396 | ||
397 | // This is a heuristic, and not 100% reliable. | |
398 | if (!result.isEmpty()) { | |
399 | break; | |
400 | } | |
401 | } | |
402 | } | |
403 | ||
404 | /** | |
405 | * Override Transliterator framework | |
406 | */ | |
407 | UnicodeSet& CompoundTransliterator::getTargetSet(UnicodeSet& result) const { | |
408 | UnicodeSet set; | |
409 | result.clear(); | |
410 | for (int32_t i=0; i<count; ++i) { | |
411 | // This is a heuristic, and not 100% reliable. | |
412 | result.addAll(trans[i]->getTargetSet(set)); | |
413 | } | |
414 | return result; | |
415 | } | |
416 | ||
417 | /** | |
418 | * Implements {@link Transliterator#handleTransliterate}. | |
419 | */ | |
420 | void CompoundTransliterator::handleTransliterate(Replaceable& text, UTransPosition& index, | |
421 | UBool incremental) const { | |
422 | /* Call each transliterator with the same contextStart and | |
423 | * start, but with the limit as modified | |
424 | * by preceding transliterators. The start index must be | |
425 | * reset for each transliterator to give each a chance to | |
426 | * transliterate the text. The initial contextStart index is known | |
427 | * to still point to the same place after each transliterator | |
428 | * is called because each transliterator will not change the | |
429 | * text between contextStart and the initial start index. | |
430 | * | |
431 | * IMPORTANT: After the first transliterator, each subsequent | |
432 | * transliterator only gets to transliterate text committed by | |
433 | * preceding transliterators; that is, the start (output | |
434 | * value) of transliterator i becomes the limit (input value) | |
435 | * of transliterator i+1. Finally, the overall limit is fixed | |
436 | * up before we return. | |
437 | * | |
438 | * Assumptions we make here: | |
439 | * (1) contextStart <= start <= limit <= contextLimit <= text.length() | |
440 | * (2) start <= start' <= limit' ;cursor doesn't move back | |
441 | * (3) start <= limit' ;text before cursor unchanged | |
442 | * - start' is the value of start after calling handleKT | |
443 | * - limit' is the value of limit after calling handleKT | |
444 | */ | |
445 | ||
446 | /** | |
447 | * Example: 3 transliterators. This example illustrates the | |
448 | * mechanics we need to implement. C, S, and L are the contextStart, | |
449 | * start, and limit. gl is the globalLimit. contextLimit is | |
450 | * equal to limit throughout. | |
451 | * | |
452 | * 1. h-u, changes hex to Unicode | |
453 | * | |
454 | * 4 7 a d 0 4 7 a | |
455 | * abc/u0061/u => abca/u | |
456 | * C S L C S L gl=f->a | |
457 | * | |
458 | * 2. upup, changes "x" to "XX" | |
459 | * | |
460 | * 4 7 a 4 7 a | |
461 | * abca/u => abcAA/u | |
462 | * C SL C S | |
463 | * L gl=a->b | |
464 | * 3. u-h, changes Unicode to hex | |
465 | * | |
466 | * 4 7 a 4 7 a d 0 3 | |
467 | * abcAA/u => abc/u0041/u0041/u | |
468 | * C S L C S | |
469 | * L gl=b->15 | |
470 | * 4. return | |
471 | * | |
472 | * 4 7 a d 0 3 | |
473 | * abc/u0041/u0041/u | |
474 | * C S L | |
475 | */ | |
476 | ||
477 | if (count < 1) { | |
478 | index.start = index.limit; | |
479 | return; // Short circuit for empty compound transliterators | |
480 | } | |
481 | ||
482 | // compoundLimit is the limit value for the entire compound | |
483 | // operation. We overwrite index.limit with the previous | |
484 | // index.start. After each transliteration, we update | |
485 | // compoundLimit for insertions or deletions that have happened. | |
486 | int32_t compoundLimit = index.limit; | |
487 | ||
488 | // compoundStart is the start for the entire compound | |
489 | // operation. | |
490 | int32_t compoundStart = index.start; | |
491 | ||
492 | int32_t delta = 0; // delta in length | |
493 | ||
494 | // Give each transliterator a crack at the run of characters. | |
495 | // See comments at the top of the method for more detail. | |
496 | for (int32_t i=0; i<count; ++i) { | |
497 | index.start = compoundStart; // Reset start | |
498 | int32_t limit = index.limit; | |
499 | ||
500 | if (index.start == index.limit) { | |
501 | // Short circuit for empty range | |
502 | break; | |
503 | } | |
504 | ||
505 | trans[i]->filteredTransliterate(text, index, incremental); | |
506 | ||
507 | // In a properly written transliterator, start == limit after | |
508 | // handleTransliterate() returns when incremental is false. | |
509 | // Catch cases where the subclass doesn't do this, and throw | |
510 | // an exception. (Just pinning start to limit is a bad idea, | |
511 | // because what's probably happening is that the subclass | |
512 | // isn't transliterating all the way to the end, and it should | |
513 | // in non-incremental mode.) | |
514 | if (!incremental && index.start != index.limit) { | |
515 | // We can't throw an exception, so just fudge things | |
516 | index.start = index.limit; | |
517 | } | |
518 | ||
519 | // Cumulative delta for insertions/deletions | |
520 | delta += index.limit - limit; | |
521 | ||
522 | if (incremental) { | |
523 | // In the incremental case, only allow subsequent | |
524 | // transliterators to modify what has already been | |
525 | // completely processed by prior transliterators. In the | |
526 | // non-incrmental case, allow each transliterator to | |
527 | // process the entire text. | |
528 | index.limit = index.start; | |
529 | } | |
530 | } | |
531 | ||
532 | compoundLimit += delta; | |
533 | ||
534 | // Start is good where it is -- where the last transliterator left | |
535 | // it. Limit needs to be put back where it was, modulo | |
536 | // adjustments for deletions/insertions. | |
537 | index.limit = compoundLimit; | |
538 | } | |
539 | ||
540 | /** | |
541 | * Sets the length of the longest context required by this transliterator. | |
542 | * This is <em>preceding</em> context. | |
543 | */ | |
544 | void CompoundTransliterator::computeMaximumContextLength(void) { | |
545 | int32_t max = 0; | |
546 | for (int32_t i=0; i<count; ++i) { | |
547 | int32_t len = trans[i]->getMaximumContextLength(); | |
548 | if (len > max) { | |
549 | max = len; | |
550 | } | |
551 | } | |
552 | setMaximumContextLength(max); | |
553 | } | |
554 | ||
555 | U_NAMESPACE_END | |
556 | ||
557 | #endif /* #if !UCONFIG_NO_TRANSLITERATION */ | |
558 | ||
559 | /* eof */ |