2 **********************************************************************
3 * Copyright (C) 1999-2004, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 **********************************************************************
6 * Date Name Description
7 * 11/17/99 aliu Creation.
8 **********************************************************************
11 #include "unicode/utypes.h"
13 #if !UCONFIG_NO_TRANSLITERATION
15 #include "unicode/unifilt.h"
16 #include "unicode/uniset.h"
22 // keep in sync with Transliterator
23 static const UChar ID_SEP
= 0x002D; /*-*/
24 static const UChar ID_DELIM
= 0x003B; /*;*/
25 static const UChar NEWLINE
= 10;
28 static const UChar EMPTY
[] = {0}; //""
29 static const UChar COLON_COLON
[] = {0x3A, 0x3A, 0}; //"::"
33 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(CompoundTransliterator
)
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>
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.
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
);
59 * Splits an ID of the form "ID;ID;..." into a compound using each
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.
65 CompoundTransliterator::CompoundTransliterator(const UnicodeString
& id
,
66 UTransDirection direction
,
67 UnicodeFilter
* adoptedFilter
,
68 UParseError
& /*parseError*/,
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
);
77 CompoundTransliterator::CompoundTransliterator(const UnicodeString
& id
,
78 UParseError
& /*parseError*/,
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
);
88 * Private constructor for Transliterator from a vector of
89 * transliterators. The caller is responsible for fixing up the
92 CompoundTransliterator::CompoundTransliterator(UVector
& list
,
93 UParseError
& /*parseError*/,
95 Transliterator(EMPTY
, NULL
),
96 trans(0), compoundRBTIndex(-1)
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
105 * Private constructor for compound RBTs. Construct a compound
106 * transliterator using the given idBlock, with the adoptedTrans
107 * inserted at the idSplitPoint.
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)
117 init(idBlock
, UTRANS_FORWARD
, idSplitPoint
, adoptedTrans
, FALSE
, status
);
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
136 void CompoundTransliterator::init(const UnicodeString
& id
,
137 UTransDirection direction
,
138 int32_t idSplitPoint
,
139 Transliterator
*adoptedSplitTrans
,
141 UErrorCode
& status
) {
142 // assert(trans == 0);
144 if (U_FAILURE(status
)) {
145 delete adoptedSplitTrans
;
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
;
160 compoundRBTIndex
= TransliteratorIDParser::instantiateList(list
, adoptedSplitTrans
, idSplitPoint
, status
);
162 init(list
, direction
, fixReverseID
, status
);
164 if (compoundFilter
!= NULL
) {
165 adoptFilter(compoundFilter
);
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
182 void CompoundTransliterator::init(UVector
& list
,
183 UTransDirection direction
,
185 UErrorCode
& status
) {
186 // assert(trans == 0);
189 if (U_SUCCESS(status
)) {
191 trans
= (Transliterator
**)uprv_malloc(count
* sizeof(Transliterator
*));
194 status
= U_MEMORY_ALLOCATION_ERROR
;
199 if (U_FAILURE(status
) || trans
== 0) {
200 // assert(trans == 0);
204 // Move the transliterators from the vector into an array.
205 // Reverse the order if necessary.
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
);
212 // Fix compoundRBTIndex for REVERSE transliterators
213 if (compoundRBTIndex
>= 0 && direction
== UTRANS_REVERSE
) {
214 compoundRBTIndex
= count
- 1 - compoundRBTIndex
;
217 // If the direction is UTRANS_REVERSE then we may need to fix the
219 if (direction
== UTRANS_REVERSE
&& fixReverseID
) {
221 for (i
=0; i
<count
; ++i
) {
223 newID
.append(ID_DELIM
);
225 newID
.append(trans
[i
]->getID());
230 computeMaximumContextLength();
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).
238 UnicodeString
CompoundTransliterator::joinIDs(Transliterator
* const transliterators
[],
239 int32_t transCount
) {
241 for (int32_t i
=0; i
<transCount
; ++i
) {
245 id
.append(transliterators
[i
]->getID());
247 return id
; // Return temporary
253 CompoundTransliterator::CompoundTransliterator(const CompoundTransliterator
& t
) :
254 Transliterator(t
), trans(0), count(0), compoundRBTIndex(-1) {
261 CompoundTransliterator::~CompoundTransliterator() {
262 freeTransliterators();
265 void CompoundTransliterator::freeTransliterators(void) {
267 for (int32_t i
=0; i
<count
; ++i
) {
277 * Assignment operator.
279 CompoundTransliterator
& CompoundTransliterator::operator=(
280 const CompoundTransliterator
& t
) {
281 Transliterator::operator=(t
);
283 for (i
=0; i
<count
; ++i
) {
287 if (t
.count
> count
) {
289 trans
= (Transliterator
**)uprv_malloc(t
.count
* sizeof(Transliterator
*));
292 for (i
=0; i
<count
; ++i
) {
293 trans
[i
] = t
.trans
[i
]->clone();
295 compoundRBTIndex
= t
.compoundRBTIndex
;
300 * Transliterator API.
302 Transliterator
* CompoundTransliterator::clone(void) const {
303 return new CompoundTransliterator(*this);
307 * Returns the number of transliterators in this chain.
308 * @return number of transliterators in this chain.
310 int32_t CompoundTransliterator::getCount(void) const {
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
319 const Transliterator
& CompoundTransliterator::getTransliterator(int32_t index
) const {
320 return *trans
[index
];
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();
329 adoptTransliterators(a
, transCount
);
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
;
339 computeMaximumContextLength();
340 setID(joinIDs(trans
, count
));
344 * Append c to buf, unless buf is empty or buf already ends in c.
346 static void _smartAppend(UnicodeString
& buf
, UChar c
) {
347 if (buf
.length() != 0 &&
348 buf
.charAt(buf
.length() - 1) != c
) {
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.
366 rulesSource
.append(COLON_COLON
).append(getFilter()->toPattern(pat
, escapeUnprintable
)).append(ID_DELIM
);
368 for (int32_t i
=0; i
<count
; ++i
) {
370 if (i
== compoundRBTIndex
) {
371 trans
[i
]->toRules(rule
, escapeUnprintable
);
373 trans
[i
]->Transliterator::toRules(rule
, escapeUnprintable
);
375 _smartAppend(rulesSource
, NEWLINE
);
376 rulesSource
.append(rule
);
377 _smartAppend(rulesSource
, ID_DELIM
);
383 * Implement Transliterator framework
385 void CompoundTransliterator::handleGetSourceSet(UnicodeSet
& result
) const {
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.
397 // This is a heuristic, and not 100% reliable.
398 if (!result
.isEmpty()) {
405 * Override Transliterator framework
407 UnicodeSet
& CompoundTransliterator::getTargetSet(UnicodeSet
& result
) const {
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
));
418 * Implements {@link Transliterator#handleTransliterate}.
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.
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.
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
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.
452 * 1. h-u, changes hex to Unicode
455 * abc/u0061/u => abca/u
456 * C S L C S L gl=f->a
458 * 2. upup, changes "x" to "XX"
464 * 3. u-h, changes Unicode to hex
467 * abcAA/u => abc/u0041/u0041/u
478 index
.start
= index
.limit
;
479 return; // Short circuit for empty compound transliterators
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
;
488 // compoundStart is the start for the entire compound
490 int32_t compoundStart
= index
.start
;
492 int32_t delta
= 0; // delta in length
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
;
500 if (index
.start
== index
.limit
) {
501 // Short circuit for empty range
505 trans
[i
]->filteredTransliterate(text
, index
, incremental
);
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
;
519 // Cumulative delta for insertions/deletions
520 delta
+= index
.limit
- limit
;
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
;
532 compoundLimit
+= delta
;
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
;
541 * Sets the length of the longest context required by this transliterator.
542 * This is <em>preceding</em> context.
544 void CompoundTransliterator::computeMaximumContextLength(void) {
546 for (int32_t i
=0; i
<count
; ++i
) {
547 int32_t len
= trans
[i
]->getMaximumContextLength();
552 setMaximumContextLength(max
);
557 #endif /* #if !UCONFIG_NO_TRANSLITERATION */