]> git.saurik.com Git - apple/icu.git/blame - icuSources/i18n/translit.cpp
ICU-400.42.tar.gz
[apple/icu.git] / icuSources / i18n / translit.cpp
CommitLineData
b75a7d8f 1/*
46f4442e
A
2 **********************************************************************
3 * Copyright (C) 1999-2008, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 **********************************************************************
6 * Date Name Description
7 * 11/17/99 aliu Creation.
8 **********************************************************************
9 */
b75a7d8f
A
10
11#include "unicode/utypes.h"
12
13#if !UCONFIG_NO_TRANSLITERATION
14
15#include "unicode/putil.h"
16#include "unicode/translit.h"
17#include "unicode/locid.h"
18#include "unicode/msgfmt.h"
19#include "unicode/rep.h"
20#include "unicode/resbund.h"
21#include "unicode/unifilt.h"
b75a7d8f
A
22#include "unicode/uniset.h"
23#include "unicode/uscript.h"
374ca955 24#include "unicode/strenum.h"
b75a7d8f
A
25#include "cpdtrans.h"
26#include "nultrans.h"
27#include "rbt_data.h"
28#include "rbt_pars.h"
29#include "rbt.h"
30#include "transreg.h"
31#include "name2uni.h"
32#include "nortrans.h"
33#include "remtrans.h"
34#include "titletrn.h"
35#include "tolowtrn.h"
36#include "toupptrn.h"
37#include "uni2name.h"
46f4442e 38#include "brktrans.h"
b75a7d8f
A
39#include "esctrn.h"
40#include "unesctrn.h"
41#include "tridpars.h"
42#include "anytrans.h"
43#include "util.h"
44#include "hash.h"
45#include "mutex.h"
46#include "ucln_in.h"
47#include "uassert.h"
48#include "cmemory.h"
49#include "cstring.h"
73c04bcf 50#include "uinvchar.h"
b75a7d8f
A
51
52static const UChar TARGET_SEP = 0x002D; /*-*/
53static const UChar ID_DELIM = 0x003B; /*;*/
54static const UChar VARIANT_SEP = 0x002F; // '/'
55
56/**
57 * Prefix for resource bundle key for the display name for a
58 * transliterator. The ID is appended to this to form the key.
59 * The resource bundle value should be a String.
60 */
61static const char RB_DISPLAY_NAME_PREFIX[] = "%Translit%%";
62
63/**
64 * Prefix for resource bundle key for the display name for a
65 * transliterator SCRIPT. The ID is appended to this to form the key.
66 * The resource bundle value should be a String.
67 */
68static const char RB_SCRIPT_DISPLAY_NAME_PREFIX[] = "%Translit%";
69
70/**
71 * Resource bundle key for display name pattern.
72 * The resource bundle value should be a String forming a
73 * MessageFormat pattern, e.g.:
74 * "{0,choice,0#|1#{1} Transliterator|2#{1} to {2} Transliterator}".
75 */
76static const char RB_DISPLAY_NAME_PATTERN[] = "TransliteratorNamePattern";
77
78/**
79 * Resource bundle key for the list of RuleBasedTransliterator IDs.
80 * The resource bundle value should be a String[] with each element
81 * being a valid ID. The ID will be appended to RB_RULE_BASED_PREFIX
82 * to obtain the class name in which the RB_RULE key will be sought.
83 */
84static const char RB_RULE_BASED_IDS[] = "RuleBasedTransliteratorIDs";
85
86/**
87 * The mutex controlling access to registry object.
88 */
89static UMTX registryMutex = 0;
90
91/**
92 * System transliterator registry; non-null when initialized.
93 */
46f4442e 94static U_NAMESPACE_QUALIFIER TransliteratorRegistry* registry = 0;
b75a7d8f
A
95
96// Macro to check/initialize the registry. ONLY USE WITHIN
97// MUTEX. Avoids function call when registry is initialized.
46f4442e 98#define HAVE_REGISTRY(status) (registry!=0 || initializeRegistry(status))
b75a7d8f
A
99
100// Empty string
101static const UChar EMPTY[] = {0}; //""
102
103U_NAMESPACE_BEGIN
104
374ca955 105UOBJECT_DEFINE_ABSTRACT_RTTI_IMPLEMENTATION(Transliterator)
b75a7d8f
A
106
107/**
108 * Return TRUE if the given UTransPosition is valid for text of
109 * the given length.
110 */
73c04bcf 111static inline UBool positionIsValid(UTransPosition& index, int32_t len) {
b75a7d8f
A
112 return !(index.contextStart < 0 ||
113 index.start < index.contextStart ||
114 index.limit < index.start ||
115 index.contextLimit < index.limit ||
116 len < index.contextLimit);
117}
118
119/**
120 * Default constructor.
121 * @param theID the string identifier for this transliterator
122 * @param theFilter the filter. Any character for which
123 * <tt>filter.contains()</tt> returns <tt>FALSE</tt> will not be
124 * altered by this transliterator. If <tt>filter</tt> is
125 * <tt>null</tt> then no filtering is applied.
126 */
127Transliterator::Transliterator(const UnicodeString& theID,
128 UnicodeFilter* adoptedFilter) :
129 UObject(), ID(theID), filter(adoptedFilter),
73c04bcf
A
130 maximumContextLength(0)
131{
132 // NUL-terminate the ID string, which is a non-aliased copy.
133 ID.append((UChar)0);
134 ID.truncate(ID.length()-1);
374ca955 135}
b75a7d8f
A
136
137/**
138 * Destructor.
139 */
140Transliterator::~Transliterator() {
73c04bcf
A
141 if (filter) {
142 delete filter;
143 }
b75a7d8f
A
144}
145
146/**
147 * Copy constructor.
148 */
149Transliterator::Transliterator(const Transliterator& other) :
150 UObject(other), ID(other.ID), filter(0),
73c04bcf
A
151 maximumContextLength(other.maximumContextLength)
152{
153 // NUL-terminate the ID string, which is a non-aliased copy.
154 ID.append((UChar)0);
155 ID.truncate(ID.length()-1);
374ca955 156
b75a7d8f
A
157 if (other.filter != 0) {
158 // We own the filter, so we must have our own copy
159 filter = (UnicodeFilter*) other.filter->clone();
160 }
161}
162
73c04bcf
A
163Transliterator* Transliterator::clone() const {
164 return NULL;
165}
166
b75a7d8f
A
167/**
168 * Assignment operator.
169 */
170Transliterator& Transliterator::operator=(const Transliterator& other) {
171 ID = other.ID;
374ca955
A
172 // NUL-terminate the ID string
173 ID.getTerminatedBuffer();
174
b75a7d8f
A
175 maximumContextLength = other.maximumContextLength;
176 adoptFilter((other.filter == 0) ? 0 : (UnicodeFilter*) other.filter->clone());
177 return *this;
178}
179
180/**
181 * Transliterates a segment of a string. <code>Transliterator</code> API.
182 * @param text the string to be transliterated
183 * @param start the beginning index, inclusive; <code>0 <= start
184 * <= limit</code>.
185 * @param limit the ending index, exclusive; <code>start <= limit
186 * <= text.length()</code>.
187 * @return the new limit index, or -1
188 */
189int32_t Transliterator::transliterate(Replaceable& text,
190 int32_t start, int32_t limit) const {
191 if (start < 0 ||
192 limit < start ||
193 text.length() < limit) {
194 return -1;
195 }
196
197 UTransPosition offsets;
198 offsets.contextStart= start;
199 offsets.contextLimit = limit;
200 offsets.start = start;
201 offsets.limit = limit;
202 filteredTransliterate(text, offsets, FALSE, TRUE);
203 return offsets.limit;
204}
205
206/**
207 * Transliterates an entire string in place. Convenience method.
208 * @param text the string to be transliterated
209 */
210void Transliterator::transliterate(Replaceable& text) const {
211 transliterate(text, 0, text.length());
212}
213
214/**
215 * Transliterates the portion of the text buffer that can be
216 * transliterated unambiguosly after new text has been inserted,
217 * typically as a result of a keyboard event. The new text in
218 * <code>insertion</code> will be inserted into <code>text</code>
219 * at <code>index.contextLimit</code>, advancing
220 * <code>index.contextLimit</code> by <code>insertion.length()</code>.
221 * Then the transliterator will try to transliterate characters of
222 * <code>text</code> between <code>index.start</code> and
223 * <code>index.contextLimit</code>. Characters before
224 * <code>index.start</code> will not be changed.
225 *
226 * <p>Upon return, values in <code>index</code> will be updated.
227 * <code>index.contextStart</code> will be advanced to the first
228 * character that future calls to this method will read.
229 * <code>index.start</code> and <code>index.contextLimit</code> will
230 * be adjusted to delimit the range of text that future calls to
231 * this method may change.
232 *
233 * <p>Typical usage of this method begins with an initial call
234 * with <code>index.contextStart</code> and <code>index.contextLimit</code>
235 * set to indicate the portion of <code>text</code> to be
236 * transliterated, and <code>index.start == index.contextStart</code>.
237 * Thereafter, <code>index</code> can be used without
238 * modification in future calls, provided that all changes to
239 * <code>text</code> are made via this method.
240 *
241 * <p>This method assumes that future calls may be made that will
242 * insert new text into the buffer. As a result, it only performs
243 * unambiguous transliterations. After the last call to this
244 * method, there may be untransliterated text that is waiting for
245 * more input to resolve an ambiguity. In order to perform these
246 * pending transliterations, clients should call {@link
247 * #finishKeyboardTransliteration} after the last call to this
248 * method has been made.
249 *
250 * @param text the buffer holding transliterated and untransliterated text
251 * @param index an array of three integers.
252 *
253 * <ul><li><code>index.contextStart</code>: the beginning index,
254 * inclusive; <code>0 <= index.contextStart <= index.contextLimit</code>.
255 *
256 * <li><code>index.contextLimit</code>: the ending index, exclusive;
257 * <code>index.contextStart <= index.contextLimit <= text.length()</code>.
258 * <code>insertion</code> is inserted at
259 * <code>index.contextLimit</code>.
260 *
261 * <li><code>index.start</code>: the next character to be
262 * considered for transliteration; <code>index.contextStart <=
263 * index.start <= index.contextLimit</code>. Characters before
264 * <code>index.start</code> will not be changed by future calls
265 * to this method.</ul>
266 *
267 * @param insertion text to be inserted and possibly
268 * transliterated into the translation buffer at
269 * <code>index.contextLimit</code>. If <code>null</code> then no text
270 * is inserted.
271 * @see #START
272 * @see #LIMIT
273 * @see #CURSOR
274 * @see #handleTransliterate
275 * @exception IllegalArgumentException if <code>index</code>
276 * is invalid
277 */
278void Transliterator::transliterate(Replaceable& text,
279 UTransPosition& index,
280 const UnicodeString& insertion,
281 UErrorCode &status) const {
282 _transliterate(text, index, &insertion, status);
283}
284
285/**
286 * Transliterates the portion of the text buffer that can be
287 * transliterated unambiguosly after a new character has been
288 * inserted, typically as a result of a keyboard event. This is a
289 * convenience method; see {@link
290 * #transliterate(Replaceable, int[], String)} for details.
291 * @param text the buffer holding transliterated and
292 * untransliterated text
293 * @param index an array of three integers. See {@link
294 * #transliterate(Replaceable, int[], String)}.
295 * @param insertion text to be inserted and possibly
296 * transliterated into the translation buffer at
297 * <code>index.contextLimit</code>.
298 * @see #transliterate(Replaceable, int[], String)
299 */
300void Transliterator::transliterate(Replaceable& text,
301 UTransPosition& index,
302 UChar32 insertion,
303 UErrorCode& status) const {
304 UnicodeString str(insertion);
305 _transliterate(text, index, &str, status);
306}
307
308/**
309 * Transliterates the portion of the text buffer that can be
310 * transliterated unambiguosly. This is a convenience method; see
311 * {@link #transliterate(Replaceable, int[], String)} for
312 * details.
313 * @param text the buffer holding transliterated and
314 * untransliterated text
315 * @param index an array of three integers. See {@link
316 * #transliterate(Replaceable, int[], String)}.
317 * @see #transliterate(Replaceable, int[], String)
318 */
319void Transliterator::transliterate(Replaceable& text,
320 UTransPosition& index,
321 UErrorCode& status) const {
322 _transliterate(text, index, 0, status);
323}
324
325/**
326 * Finishes any pending transliterations that were waiting for
327 * more characters. Clients should call this method as the last
328 * call after a sequence of one or more calls to
329 * <code>transliterate()</code>.
330 * @param text the buffer holding transliterated and
331 * untransliterated text.
332 * @param index the array of indices previously passed to {@link
333 * #transliterate}
334 */
335void Transliterator::finishTransliteration(Replaceable& text,
336 UTransPosition& index) const {
337 if (!positionIsValid(index, text.length())) {
338 return;
339 }
340
341 filteredTransliterate(text, index, FALSE, TRUE);
342}
343
344/**
345 * This internal method does keyboard transliteration. If the
346 * 'insertion' is non-null then we append it to 'text' before
347 * proceeding. This method calls through to the pure virtual
348 * framework method handleTransliterate() to do the actual
349 * work.
350 */
351void Transliterator::_transliterate(Replaceable& text,
352 UTransPosition& index,
353 const UnicodeString* insertion,
354 UErrorCode &status) const {
355 if (U_FAILURE(status)) {
356 return;
357 }
358
359 if (!positionIsValid(index, text.length())) {
360 status = U_ILLEGAL_ARGUMENT_ERROR;
361 return;
362 }
363
364// int32_t originalStart = index.contextStart;
365 if (insertion != 0) {
366 text.handleReplaceBetween(index.limit, index.limit, *insertion);
367 index.limit += insertion->length();
368 index.contextLimit += insertion->length();
369 }
370
371 if (index.limit > 0 &&
372 UTF_IS_LEAD(text.charAt(index.limit - 1))) {
373 // Oops, there is a dangling lead surrogate in the buffer.
374 // This will break most transliterators, since they will
375 // assume it is part of a pair. Don't transliterate until
376 // more text comes in.
377 return;
378 }
379
380 filteredTransliterate(text, index, TRUE, TRUE);
381
382#if 0
383 // TODO
384 // I CAN'T DO what I'm attempting below now that the Kleene star
385 // operator is supported. For example, in the rule
386
387 // ([:Lu:]+) { x } > $1;
388
389 // what is the maximum context length? getMaximumContextLength()
390 // will return 1, but this is just the length of the ante context
391 // part of the pattern string -- 1 character, which is a standin
392 // for a Quantifier, which contains a StringMatcher, which
393 // contains a UnicodeSet.
394
395 // There is a complicated way to make this work again, and that's
396 // to add a "maximum left context" protocol into the
397 // UnicodeMatcher hierarchy. At present I'm not convinced this is
398 // worth it.
399
400 // ---
401
402 // The purpose of the code below is to keep the context small
403 // while doing incremental transliteration. When part of the left
404 // context (between contextStart and start) is no longer needed,
405 // we try to advance contextStart past that portion. We use the
406 // maximum context length to do so.
407 int32_t newCS = index.start;
408 int32_t n = getMaximumContextLength();
409 while (newCS > originalStart && n-- > 0) {
410 --newCS;
411 newCS -= UTF_CHAR_LENGTH(text.char32At(newCS)) - 1;
412 }
413 index.contextStart = uprv_max(newCS, originalStart);
414#endif
415}
416
417/**
418 * This method breaks up the input text into runs of unfiltered
419 * characters. It passes each such run to
420 * <subclass>.handleTransliterate(). Subclasses that can handle the
421 * filter logic more efficiently themselves may override this method.
422 *
423 * All transliteration calls in this class go through this method.
424 */
425void Transliterator::filteredTransliterate(Replaceable& text,
426 UTransPosition& index,
427 UBool incremental,
428 UBool rollback) const {
429 // Short circuit path for transliterators with no filter in
430 // non-incremental mode.
431 if (filter == 0 && !rollback) {
432 handleTransliterate(text, index, incremental);
433 return;
434 }
435
436 //----------------------------------------------------------------------
437 // This method processes text in two groupings:
438 //
439 // RUNS -- A run is a contiguous group of characters which are contained
440 // in the filter for this transliterator (filter.contains(ch) == TRUE).
441 // Text outside of runs may appear as context but it is not modified.
442 // The start and limit Position values are narrowed to each run.
443 //
444 // PASSES (incremental only) -- To make incremental mode work correctly,
445 // each run is broken up into n passes, where n is the length (in code
446 // points) of the run. Each pass contains the first n characters. If a
447 // pass is completely transliterated, it is committed, and further passes
448 // include characters after the committed text. If a pass is blocked,
449 // and does not transliterate completely, then this method rolls back
450 // the changes made during the pass, extends the pass by one code point,
451 // and tries again.
452 //----------------------------------------------------------------------
453
454 // globalLimit is the limit value for the entire operation. We
455 // set index.limit to the end of each unfiltered run before
456 // calling handleTransliterate(), so we need to maintain the real
457 // value of index.limit here. After each transliteration, we
458 // update globalLimit for insertions or deletions that have
459 // happened.
460 int32_t globalLimit = index.limit;
461
462 // If there is a non-null filter, then break the input text up. Say the
463 // input text has the form:
464 // xxxabcxxdefxx
465 // where 'x' represents a filtered character (filter.contains('x') ==
466 // false). Then we break this up into:
467 // xxxabc xxdef xx
468 // Each pass through the loop consumes a run of filtered
469 // characters (which are ignored) and a subsequent run of
470 // unfiltered characters (which are transliterated).
471
472 for (;;) {
473
474 if (filter != NULL) {
475 // Narrow the range to be transliterated to the first segment
476 // of unfiltered characters at or after index.start.
477
478 // Advance past filtered chars
479 UChar32 c;
480 while (index.start < globalLimit &&
481 !filter->contains(c=text.char32At(index.start))) {
482 index.start += UTF_CHAR_LENGTH(c);
483 }
484
485 // Find the end of this run of unfiltered chars
486 index.limit = index.start;
487 while (index.limit < globalLimit &&
488 filter->contains(c=text.char32At(index.limit))) {
489 index.limit += UTF_CHAR_LENGTH(c);
490 }
491 }
492
493 // Check to see if the unfiltered run is empty. This only
494 // happens at the end of the string when all the remaining
495 // characters are filtered.
496 if (index.limit == index.start) {
497 // assert(index.start == globalLimit);
498 break;
499 }
500
501 // Is this run incremental? If there is additional
502 // filtered text (if limit < globalLimit) then we pass in
503 // an incremental value of FALSE to force the subclass to
504 // complete the transliteration for this run.
505 UBool isIncrementalRun =
506 (index.limit < globalLimit ? FALSE : incremental);
507
508 int32_t delta;
509
510 // Implement rollback. To understand the need for rollback,
511 // consider the following transliterator:
512 //
513 // "t" is "a > A;"
514 // "u" is "A > b;"
515 // "v" is a compound of "t; NFD; u" with a filter [:Ll:]
516 //
517 // Now apply "c" to the input text "a". The result is "b". But if
518 // the transliteration is done incrementally, then the NFD holds
519 // things up after "t" has already transformed "a" to "A". When
520 // finishTransliterate() is called, "A" is _not_ processed because
521 // it gets excluded by the [:Ll:] filter, and the end result is "A"
522 // -- incorrect. The problem is that the filter is applied to a
523 // partially-transliterated result, when we only want it to apply to
524 // input text. Although this example hinges on a compound
525 // transliterator containing NFD and a specific filter, it can
526 // actually happen with any transliterator which may do a partial
527 // transformation in incremental mode into characters outside its
528 // filter.
529 //
530 // To handle this, when in incremental mode we supply characters to
531 // handleTransliterate() in several passes. Each pass adds one more
532 // input character to the input text. That is, for input "ABCD", we
533 // first try "A", then "AB", then "ABC", and finally "ABCD". If at
534 // any point we block (upon return, start < limit) then we roll
535 // back. If at any point we complete the run (upon return start ==
536 // limit) then we commit that run.
537
538 if (rollback && isIncrementalRun) {
539
540 int32_t runStart = index.start;
541 int32_t runLimit = index.limit;
542 int32_t runLength = runLimit - runStart;
543
544 // Make a rollback copy at the end of the string
545 int32_t rollbackOrigin = text.length();
546 text.copy(runStart, runLimit, rollbackOrigin);
547
548 // Variables reflecting the commitment of completely
549 // transliterated text. passStart is the runStart, advanced
550 // past committed text. rollbackStart is the rollbackOrigin,
551 // advanced past rollback text that corresponds to committed
552 // text.
553 int32_t passStart = runStart;
554 int32_t rollbackStart = rollbackOrigin;
555
556 // The limit for each pass; we advance by one code point with
557 // each iteration.
558 int32_t passLimit = index.start;
559
560 // Total length, in 16-bit code units, of uncommitted text.
561 // This is the length to be rolled back.
562 int32_t uncommittedLength = 0;
563
564 // Total delta (change in length) for all passes
565 int32_t totalDelta = 0;
566
567 // PASS MAIN LOOP -- Start with a single character, and extend
568 // the text by one character at a time. Roll back partial
569 // transliterations and commit complete transliterations.
570 for (;;) {
571 // Length of additional code point, either one or two
572 int32_t charLength =
573 UTF_CHAR_LENGTH(text.char32At(passLimit));
574 passLimit += charLength;
575 if (passLimit > runLimit) {
576 break;
577 }
578 uncommittedLength += charLength;
579
580 index.limit = passLimit;
581
582 // Delegate to subclass for actual transliteration. Upon
583 // return, start will be updated to point after the
584 // transliterated text, and limit and contextLimit will be
585 // adjusted for length changes.
586 handleTransliterate(text, index, TRUE);
587
588 delta = index.limit - passLimit; // change in length
589
590 // We failed to completely transliterate this pass.
591 // Roll back the text. Indices remain unchanged; reset
592 // them where necessary.
593 if (index.start != index.limit) {
594 // Find the rollbackStart, adjusted for length changes
595 // and the deletion of partially transliterated text.
596 int32_t rs = rollbackStart + delta - (index.limit - passStart);
597
598 // Delete the partially transliterated text
599 text.handleReplaceBetween(passStart, index.limit, EMPTY);
600
601 // Copy the rollback text back
602 text.copy(rs, rs + uncommittedLength, passStart);
603
604 // Restore indices to their original values
605 index.start = passStart;
606 index.limit = passLimit;
607 index.contextLimit -= delta;
608 }
609
610 // We did completely transliterate this pass. Update the
611 // commit indices to record how far we got. Adjust indices
612 // for length change.
613 else {
614 // Move the pass indices past the committed text.
615 passStart = passLimit = index.start;
616
617 // Adjust the rollbackStart for length changes and move
618 // it past the committed text. All characters we've
619 // processed to this point are committed now, so zero
620 // out the uncommittedLength.
621 rollbackStart += delta + uncommittedLength;
622 uncommittedLength = 0;
623
624 // Adjust indices for length changes.
625 runLimit += delta;
626 totalDelta += delta;
627 }
628 }
629
630 // Adjust overall limit and rollbackOrigin for insertions and
631 // deletions. Don't need to worry about contextLimit because
632 // handleTransliterate() maintains that.
633 rollbackOrigin += totalDelta;
634 globalLimit += totalDelta;
635
636 // Delete the rollback copy
637 text.handleReplaceBetween(rollbackOrigin, rollbackOrigin + runLength, EMPTY);
638
639 // Move start past committed text
640 index.start = passStart;
641 }
642
643 else {
644 // Delegate to subclass for actual transliteration.
645 int32_t limit = index.limit;
646 handleTransliterate(text, index, isIncrementalRun);
647 delta = index.limit - limit; // change in length
648
649 // In a properly written transliterator, start == limit after
650 // handleTransliterate() returns when incremental is false.
651 // Catch cases where the subclass doesn't do this, and throw
652 // an exception. (Just pinning start to limit is a bad idea,
653 // because what's probably happening is that the subclass
654 // isn't transliterating all the way to the end, and it should
655 // in non-incremental mode.)
656 if (!incremental && index.start != index.limit) {
657 // We can't throw an exception, so just fudge things
658 index.start = index.limit;
659 }
660
661 // Adjust overall limit for insertions/deletions. Don't need
662 // to worry about contextLimit because handleTransliterate()
663 // maintains that.
664 globalLimit += delta;
665 }
666
667 if (filter == NULL || isIncrementalRun) {
668 break;
669 }
670
671 // If we did completely transliterate this
672 // run, then repeat with the next unfiltered run.
673 }
674
675 // Start is valid where it is. Limit needs to be put back where
676 // it was, modulo adjustments for deletions/insertions.
677 index.limit = globalLimit;
678}
679
680void Transliterator::filteredTransliterate(Replaceable& text,
681 UTransPosition& index,
682 UBool incremental) const {
683 filteredTransliterate(text, index, incremental, FALSE);
684}
685
686/**
687 * Method for subclasses to use to set the maximum context length.
688 * @see #getMaximumContextLength
689 */
690void Transliterator::setMaximumContextLength(int32_t maxContextLength) {
691 maximumContextLength = maxContextLength;
692}
693
694/**
695 * Returns a programmatic identifier for this transliterator.
696 * If this identifier is passed to <code>getInstance()</code>, it
697 * will return this object, if it has been registered.
698 * @see #registerInstance
699 * @see #getAvailableIDs
700 */
701const UnicodeString& Transliterator::getID(void) const {
702 return ID;
703}
704
705/**
706 * Returns a name for this transliterator that is appropriate for
707 * display to the user in the default locale. See {@link
708 * #getDisplayName(Locale)} for details.
709 */
374ca955 710UnicodeString& U_EXPORT2 Transliterator::getDisplayName(const UnicodeString& ID,
b75a7d8f
A
711 UnicodeString& result) {
712 return getDisplayName(ID, Locale::getDefault(), result);
713}
714
715/**
716 * Returns a name for this transliterator that is appropriate for
717 * display to the user in the given locale. This name is taken
718 * from the locale resource data in the standard manner of the
719 * <code>java.text</code> package.
720 *
721 * <p>If no localized names exist in the system resource bundles,
722 * a name is synthesized using a localized
723 * <code>MessageFormat</code> pattern from the resource data. The
724 * arguments to this pattern are an integer followed by one or two
725 * strings. The integer is the number of strings, either 1 or 2.
726 * The strings are formed by splitting the ID for this
727 * transliterator at the first TARGET_SEP. If there is no TARGET_SEP, then the
728 * entire ID forms the only string.
729 * @param inLocale the Locale in which the display name should be
730 * localized.
731 * @see java.text.MessageFormat
732 */
374ca955 733UnicodeString& U_EXPORT2 Transliterator::getDisplayName(const UnicodeString& id,
b75a7d8f
A
734 const Locale& inLocale,
735 UnicodeString& result) {
736 UErrorCode status = U_ZERO_ERROR;
737
374ca955 738 ResourceBundle bundle(U_ICUDATA_TRANSLIT, inLocale, status);
b75a7d8f
A
739
740 // Suspend checking status until later...
741
742 result.truncate(0);
743
744 // Normalize the ID
745 UnicodeString source, target, variant;
746 UBool sawSource;
747 TransliteratorIDParser::IDtoSTV(id, source, target, variant, sawSource);
748 if (target.length() < 1) {
749 // No target; malformed id
750 return result;
751 }
752 if (variant.length() > 0) { // Change "Foo" to "/Foo"
753 variant.insert(0, VARIANT_SEP);
754 }
755 UnicodeString ID(source);
756 ID.append(TARGET_SEP).append(target).append(variant);
757
758 // build the char* key
73c04bcf
A
759 if (uprv_isInvariantUString(ID.getBuffer(), ID.length())) {
760 char key[200];
761 uprv_strcpy(key, RB_DISPLAY_NAME_PREFIX);
762 int32_t length=(int32_t)uprv_strlen(RB_DISPLAY_NAME_PREFIX);
763 ID.extract(0, (int32_t)(sizeof(key)-length), key+length, (int32_t)(sizeof(key)-length), US_INV);
b75a7d8f 764
73c04bcf
A
765 // Try to retrieve a UnicodeString from the bundle.
766 UnicodeString resString = bundle.getStringEx(key, status);
b75a7d8f 767
73c04bcf
A
768 if (U_SUCCESS(status) && resString.length() != 0) {
769 return result = resString; // [sic] assign & return
770 }
b75a7d8f
A
771
772#if !UCONFIG_NO_FORMATTING
73c04bcf
A
773 // We have failed to get a name from the locale data. This is
774 // typical, since most transliterators will not have localized
775 // name data. The next step is to retrieve the MessageFormat
776 // pattern from the locale data and to use it to synthesize the
777 // name from the ID.
b75a7d8f 778
73c04bcf
A
779 status = U_ZERO_ERROR;
780 resString = bundle.getStringEx(RB_DISPLAY_NAME_PATTERN, status);
781
782 if (U_SUCCESS(status) && resString.length() != 0) {
783 MessageFormat msg(resString, inLocale, status);
784 // Suspend checking status until later...
785
786 // We pass either 2 or 3 Formattable objects to msg.
787 Formattable args[3];
788 int32_t nargs;
789 args[0].setLong(2); // # of args to follow
790 args[1].setString(source);
791 args[2].setString(target);
792 nargs = 3;
793
794 // Use display names for the scripts, if they exist
795 UnicodeString s;
796 length=(int32_t)uprv_strlen(RB_SCRIPT_DISPLAY_NAME_PREFIX);
797 for (int j=1; j<=2; ++j) {
798 status = U_ZERO_ERROR;
799 uprv_strcpy(key, RB_SCRIPT_DISPLAY_NAME_PREFIX);
800 args[j].getString(s);
801 if (uprv_isInvariantUString(s.getBuffer(), s.length())) {
802 s.extract(0, sizeof(key)-length-1, key+length, (int32_t)sizeof(key)-length-1, US_INV);
803
804 resString = bundle.getStringEx(key, status);
805
806 if (U_SUCCESS(status)) {
807 args[j] = resString;
808 }
809 }
810 }
b75a7d8f 811
73c04bcf
A
812 status = U_ZERO_ERROR;
813 FieldPosition pos; // ignored by msg
814 msg.format(args, nargs, result, pos, status);
b75a7d8f 815 if (U_SUCCESS(status)) {
73c04bcf
A
816 result.append(variant);
817 return result;
b75a7d8f
A
818 }
819 }
b75a7d8f 820#endif
73c04bcf 821 }
b75a7d8f
A
822
823 // We should not reach this point unless there is something
824 // wrong with the build or the RB_DISPLAY_NAME_PATTERN has
825 // been deleted from the root RB_LOCALE_ELEMENTS resource.
826 result = ID;
827 return result;
828}
829
830/**
831 * Returns the filter used by this transliterator, or <tt>null</tt>
832 * if this transliterator uses no filter. Caller musn't delete
833 * the result!
834 */
835const UnicodeFilter* Transliterator::getFilter(void) const {
836 return filter;
837}
838
839/**
840 * Returns the filter used by this transliterator, or
841 * <tt>NULL</tt> if this transliterator uses no filter. The
842 * caller must eventually delete the result. After this call,
843 * this transliterator's filter is set to <tt>NULL</tt>.
844 */
845UnicodeFilter* Transliterator::orphanFilter(void) {
846 UnicodeFilter *result = filter;
847 filter = NULL;
848 return result;
849}
850
851/**
852 * Changes the filter used by this transliterator. If the filter
853 * is set to <tt>null</tt> then no filtering will occur.
854 *
855 * <p>Callers must take care if a transliterator is in use by
856 * multiple threads. The filter should not be changed by one
857 * thread while another thread may be transliterating.
858 */
859void Transliterator::adoptFilter(UnicodeFilter* filterToAdopt) {
860 delete filter;
861 filter = filterToAdopt;
862}
863
864/**
865 * Returns this transliterator's inverse. See the class
866 * documentation for details. This implementation simply inverts
867 * the two entities in the ID and attempts to retrieve the
868 * resulting transliterator. That is, if <code>getID()</code>
869 * returns "A-B", then this method will return the result of
870 * <code>getInstance("B-A")</code>, or <code>null</code> if that
871 * call fails.
872 *
873 * <p>This method does not take filtering into account. The
874 * returned transliterator will have no filter.
875 *
876 * <p>Subclasses with knowledge of their inverse may wish to
877 * override this method.
878 *
879 * @return a transliterator that is an inverse, not necessarily
880 * exact, of this transliterator, or <code>null</code> if no such
881 * transliterator is registered.
882 * @see #registerInstance
883 */
884Transliterator* Transliterator::createInverse(UErrorCode& status) const {
885 UParseError parseError;
886 return Transliterator::createInstance(ID, UTRANS_REVERSE,parseError,status);
887}
888
374ca955
A
889Transliterator* U_EXPORT2
890Transliterator::createInstance(const UnicodeString& ID,
891 UTransDirection dir,
892 UErrorCode& status)
893{
b75a7d8f
A
894 UParseError parseError;
895 return createInstance(ID, dir, parseError, status);
896}
897
898/**
899 * Returns a <code>Transliterator</code> object given its ID.
900 * The ID must be either a system transliterator ID or a ID registered
901 * using <code>registerInstance()</code>.
902 *
903 * @param ID a valid ID, as enumerated by <code>getAvailableIDs()</code>
904 * @return A <code>Transliterator</code> object with the given ID
905 * @see #registerInstance
906 * @see #getAvailableIDs
907 * @see #getID
908 */
374ca955
A
909Transliterator* U_EXPORT2
910Transliterator::createInstance(const UnicodeString& ID,
911 UTransDirection dir,
912 UParseError& parseError,
913 UErrorCode& status)
914{
b75a7d8f
A
915 if (U_FAILURE(status)) {
916 return 0;
917 }
918
919 UnicodeString canonID;
920 UVector list(status);
921 if (U_FAILURE(status)) {
922 return NULL;
923 }
924
925 UnicodeSet* globalFilter;
926 // TODO add code for parseError...currently unused, but
927 // later may be used by parsing code...
928 if (!TransliteratorIDParser::parseCompoundID(ID, dir, canonID, list, globalFilter)) {
929 status = U_INVALID_ID;
930 return NULL;
931 }
932
73c04bcf 933 TransliteratorIDParser::instantiateList(list, status);
b75a7d8f
A
934 if (U_FAILURE(status)) {
935 return NULL;
936 }
937
938 U_ASSERT(list.size() > 0);
939 Transliterator* t = NULL;
73c04bcf
A
940
941 if (list.size() > 1 || canonID.indexOf(ID_DELIM) >= 0) {
942 // [NOTE: If it's a compoundID, we instantiate a CompoundTransliterator even if it only
943 // has one child transliterator. This is so that toRules() will return the right thing
944 // (without any inactive ID), but our main ID still comes out correct. That is, if we
945 // instantiate "(Lower);Latin-Greek;", we want the rules to come out as "::Latin-Greek;"
946 // even though the ID is "(Lower);Latin-Greek;".
b75a7d8f 947 t = new CompoundTransliterator(list, parseError, status);
b75a7d8f 948 }
73c04bcf
A
949 else {
950 t = (Transliterator*)list.elementAt(0);
951 }
46f4442e
A
952 // Check null pointer
953 if (t != NULL) {
954 t->setID(canonID);
955 if (globalFilter != NULL) {
956 t->adoptFilter(globalFilter);
957 }
958 }
959 else if (U_SUCCESS(status)) {
960 status = U_MEMORY_ALLOCATION_ERROR;
b75a7d8f
A
961 }
962 return t;
963}
964
965/**
966 * Create a transliterator from a basic ID. This is an ID
967 * containing only the forward direction source, target, and
968 * variant.
969 * @param id a basic ID of the form S-T or S-T/V.
970 * @return a newly created Transliterator or null if the ID is
971 * invalid.
972 */
973Transliterator* Transliterator::createBasicInstance(const UnicodeString& id,
974 const UnicodeString* canon) {
975 UParseError pe;
976 UErrorCode ec = U_ZERO_ERROR;
977 TransliteratorAlias* alias = 0;
978 Transliterator* t = 0;
374ca955 979
b75a7d8f
A
980 umtx_init(&registryMutex);
981 umtx_lock(&registryMutex);
46f4442e 982 if (HAVE_REGISTRY(ec)) {
374ca955 983 t = registry->get(id, alias, ec);
b75a7d8f
A
984 }
985 umtx_unlock(&registryMutex);
986
987 if (U_FAILURE(ec)) {
988 delete t;
989 delete alias;
374ca955 990 return 0;
b75a7d8f
A
991 }
992
374ca955
A
993 // We may have not gotten a transliterator: Because we can't
994 // instantiate a transliterator from inside TransliteratorRegistry::
995 // get() (that would deadlock), we sometimes pass back an alias. This
996 // contains the data we need to finish the instantiation outside the
997 // registry mutex. The alias may, in turn, generate another alias, so
998 // we handle aliases in a loop. The max times through the loop is two.
999 // [alan]
1000 while (alias != 0) {
b75a7d8f 1001 U_ASSERT(t==0);
374ca955
A
1002 // Rule-based aliases are handled with TransliteratorAlias::
1003 // parse(), followed by TransliteratorRegistry::reget().
1004 // Other aliases are handled with TransliteratorAlias::create().
1005 if (alias->isRuleBased()) {
1006 // Step 1. parse
73c04bcf 1007 TransliteratorParser parser(ec);
374ca955
A
1008 alias->parse(parser, pe, ec);
1009 delete alias;
1010 alias = 0;
1011
1012 // Step 2. reget
1013 umtx_lock(&registryMutex);
46f4442e 1014 if (HAVE_REGISTRY(ec)) {
374ca955
A
1015 t = registry->reget(id, parser, alias, ec);
1016 }
1017 umtx_unlock(&registryMutex);
1018
1019 // Step 3. Loop back around!
1020 } else {
1021 t = alias->create(pe, ec);
1022 delete alias;
1023 alias = 0;
1024 break;
1025 }
b75a7d8f
A
1026 if (U_FAILURE(ec)) {
1027 delete t;
374ca955 1028 delete alias;
b75a7d8f 1029 t = NULL;
374ca955 1030 break;
b75a7d8f
A
1031 }
1032 }
1033
1034 if (t != NULL && canon != NULL) {
1035 t->setID(*canon);
1036 }
1037
1038 return t;
1039}
1040
1041/**
1042 * Returns a <code>Transliterator</code> object constructed from
1043 * the given rule string. This will be a RuleBasedTransliterator,
1044 * if the rule string contains only rules, or a
1045 * CompoundTransliterator, if it contains ID blocks, or a
1046 * NullTransliterator, if it contains ID blocks which parse as
1047 * empty for the given direction.
1048 */
374ca955
A
1049Transliterator* U_EXPORT2
1050Transliterator::createFromRules(const UnicodeString& ID,
1051 const UnicodeString& rules,
1052 UTransDirection dir,
1053 UParseError& parseError,
1054 UErrorCode& status)
1055{
b75a7d8f
A
1056 Transliterator* t = NULL;
1057
73c04bcf 1058 TransliteratorParser parser(status);
b75a7d8f
A
1059 parser.parse(rules, dir, parseError, status);
1060
1061 if (U_FAILURE(status)) {
1062 return 0;
1063 }
1064
1065 // NOTE: The logic here matches that in TransliteratorRegistry.
73c04bcf
A
1066 if (parser.idBlockVector.size() == 0 && parser.dataVector.size() == 0) {
1067 t = new NullTransliterator();
1068 }
1069 else if (parser.idBlockVector.size() == 0 && parser.dataVector.size() == 1) {
1070 t = new RuleBasedTransliterator(ID, (TransliterationRuleData*)parser.dataVector.orphanElementAt(0), TRUE);
1071 }
1072 else if (parser.idBlockVector.size() == 1 && parser.dataVector.size() == 0) {
1073 // idBlock, no data -- this is an alias. The ID has
1074 // been munged from reverse into forward mode, if
1075 // necessary, so instantiate the ID in the forward
1076 // direction.
1077 if (parser.compoundFilter != NULL) {
1078 UnicodeString filterPattern;
1079 parser.compoundFilter->toPattern(filterPattern, FALSE);
1080 t = createInstance(filterPattern + UnicodeString(ID_DELIM)
1081 + *((UnicodeString*)parser.idBlockVector.elementAt(0)), UTRANS_FORWARD, parseError, status);
b75a7d8f 1082 }
73c04bcf
A
1083 else
1084 t = createInstance(*((UnicodeString*)parser.idBlockVector.elementAt(0)), UTRANS_FORWARD, parseError, status);
1085
1086
1087 if (t != NULL) {
1088 t->setID(ID);
b75a7d8f 1089 }
73c04bcf
A
1090 }
1091 else {
1092 UVector transliterators(status);
1093 int32_t passNumber = 1;
1094
1095 int32_t limit = parser.idBlockVector.size();
1096 if (parser.dataVector.size() > limit)
1097 limit = parser.dataVector.size();
1098
1099 for (int32_t i = 0; i < limit; i++) {
1100 if (i < parser.idBlockVector.size()) {
1101 UnicodeString* idBlock = (UnicodeString*)parser.idBlockVector.elementAt(i);
1102 if (!idBlock->isEmpty()) {
1103 Transliterator* temp = createInstance(*idBlock, UTRANS_FORWARD, parseError, status);
1104 if (temp != NULL && temp->getDynamicClassID() != NullTransliterator::getStaticClassID())
1105 transliterators.addElement(temp, status);
1106 else
1107 delete temp;
1108 }
b75a7d8f 1109 }
73c04bcf
A
1110 if (!parser.dataVector.isEmpty()) {
1111 TransliterationRuleData* data = (TransliterationRuleData*)parser.dataVector.orphanElementAt(0);
46f4442e
A
1112 RuleBasedTransliterator* temprbt = new RuleBasedTransliterator(UnicodeString(CompoundTransliterator::PASS_STRING) + (passNumber++),
1113 data, TRUE);
1114 // Check if NULL before adding it to transliterators to avoid future usage of NULL pointer.
1115 if (temprbt == NULL) {
1116 status = U_MEMORY_ALLOCATION_ERROR;
1117 return t;
1118 }
1119 transliterators.addElement(temprbt, status);
b75a7d8f 1120 }
b75a7d8f 1121 }
b75a7d8f 1122
73c04bcf 1123 t = new CompoundTransliterator(transliterators, passNumber - 1, parseError, status);
46f4442e
A
1124 // Null pointer check
1125 if (t != NULL) {
1126 t->setID(ID);
1127 t->adoptFilter(parser.orphanCompoundFilter());
1128 }
1129 }
1130 if (U_SUCCESS(status) && t == NULL) {
1131 status = U_MEMORY_ALLOCATION_ERROR;
73c04bcf 1132 }
b75a7d8f
A
1133 return t;
1134}
1135
1136UnicodeString& Transliterator::toRules(UnicodeString& rulesSource,
1137 UBool escapeUnprintable) const {
1138 // The base class implementation of toRules munges the ID into
1139 // the correct format. That is: foo => ::foo
1140 if (escapeUnprintable) {
1141 rulesSource.truncate(0);
1142 UnicodeString id = getID();
1143 for (int32_t i=0; i<id.length();) {
1144 UChar32 c = id.char32At(i);
1145 if (!ICU_Utility::escapeUnprintable(rulesSource, c)) {
1146 rulesSource.append(c);
1147 }
1148 i += UTF_CHAR_LENGTH(c);
1149 }
1150 } else {
1151 rulesSource = getID();
1152 }
1153 // KEEP in sync with rbt_pars
1154 rulesSource.insert(0, UNICODE_STRING_SIMPLE("::"));
1155 rulesSource.append(ID_DELIM);
1156 return rulesSource;
1157}
1158
374ca955
A
1159int32_t Transliterator::countElements() const {
1160 return (this->getDynamicClassID() ==
1161 CompoundTransliterator::getStaticClassID()) ?
1162 ((const CompoundTransliterator*) this)->getCount() : 0;
1163}
1164
1165const Transliterator& Transliterator::getElement(int32_t index, UErrorCode& ec) const {
1166 if (U_FAILURE(ec)) {
1167 return *this;
1168 }
1169 const CompoundTransliterator* cpd =
1170 (this->getDynamicClassID() == CompoundTransliterator::getStaticClassID()) ?
1171 (const CompoundTransliterator*) this : 0;
1172 int32_t n = (cpd == NULL) ? 1 : cpd->getCount();
1173 if (index < 0 || index >= n) {
1174 ec = U_INDEX_OUTOFBOUNDS_ERROR;
1175 return *this;
1176 } else {
1177 return (n == 1) ? *this : cpd->getTransliterator(index);
1178 }
1179}
1180
b75a7d8f
A
1181UnicodeSet& Transliterator::getSourceSet(UnicodeSet& result) const {
1182 handleGetSourceSet(result);
1183 if (filter != NULL) {
46f4442e
A
1184 UnicodeSet* filterSet;
1185 UBool deleteFilterSet = FALSE;
1186 // Most, but not all filters will be UnicodeSets. Optimize for
1187 // the high-runner case.
1188 if (filter->getDynamicClassID() == UnicodeSet::getStaticClassID()) {
1189 filterSet = (UnicodeSet*) filter;
1190 } else {
1191 filterSet = new UnicodeSet();
1192 // Check null pointer
1193 if (filterSet == NULL) {
1194 return result;
1195 }
1196 deleteFilterSet = TRUE;
1197 filter->addMatchSetTo(*filterSet);
1198 }
1199 result.retainAll(*filterSet);
1200 if (deleteFilterSet) {
1201 delete filterSet;
1202 }
b75a7d8f
A
1203 }
1204 return result;
1205}
1206
1207void Transliterator::handleGetSourceSet(UnicodeSet& result) const {
1208 result.clear();
1209}
1210
1211UnicodeSet& Transliterator::getTargetSet(UnicodeSet& result) const {
1212 return result.clear();
1213}
1214
1215// For public consumption
374ca955 1216void U_EXPORT2 Transliterator::registerFactory(const UnicodeString& id,
b75a7d8f
A
1217 Transliterator::Factory factory,
1218 Transliterator::Token context) {
1219 umtx_init(&registryMutex);
1220 Mutex lock(&registryMutex);
46f4442e
A
1221 UErrorCode ec = U_ZERO_ERROR;
1222 if (HAVE_REGISTRY(ec)) {
b75a7d8f
A
1223 _registerFactory(id, factory, context);
1224 }
1225}
1226
1227// To be called only by Transliterator subclasses that are called
1228// to register themselves by initializeRegistry().
1229void Transliterator::_registerFactory(const UnicodeString& id,
1230 Transliterator::Factory factory,
1231 Transliterator::Token context) {
46f4442e
A
1232 UErrorCode ec = U_ZERO_ERROR;
1233 registry->put(id, factory, context, TRUE, ec);
b75a7d8f
A
1234}
1235
1236// To be called only by Transliterator subclasses that are called
1237// to register themselves by initializeRegistry().
1238void Transliterator::_registerSpecialInverse(const UnicodeString& target,
1239 const UnicodeString& inverseTarget,
1240 UBool bidirectional) {
374ca955
A
1241 UErrorCode status = U_ZERO_ERROR;
1242 TransliteratorIDParser::registerSpecialInverse(target, inverseTarget, bidirectional, status);
b75a7d8f
A
1243}
1244
1245/**
1246 * Registers a instance <tt>obj</tt> of a subclass of
1247 * <code>Transliterator</code> with the system. This object must
1248 * implement the <tt>clone()</tt> method. When
1249 * <tt>getInstance()</tt> is called with an ID string that is
1250 * equal to <tt>obj.getID()</tt>, then <tt>obj.clone()</tt> is
1251 * returned.
1252 *
1253 * @param obj an instance of subclass of
1254 * <code>Transliterator</code> that defines <tt>clone()</tt>
1255 * @see #getInstance
1256 * @see #unregister
1257 */
374ca955 1258void U_EXPORT2 Transliterator::registerInstance(Transliterator* adoptedPrototype) {
b75a7d8f
A
1259 umtx_init(&registryMutex);
1260 Mutex lock(&registryMutex);
46f4442e
A
1261 UErrorCode ec = U_ZERO_ERROR;
1262 if (HAVE_REGISTRY(ec)) {
b75a7d8f
A
1263 _registerInstance(adoptedPrototype);
1264 }
1265}
1266
1267void Transliterator::_registerInstance(Transliterator* adoptedPrototype) {
46f4442e
A
1268 UErrorCode ec = U_ZERO_ERROR;
1269 registry->put(adoptedPrototype, TRUE, ec);
b75a7d8f
A
1270}
1271
73c04bcf
A
1272void U_EXPORT2 Transliterator::registerAlias(const UnicodeString& aliasID,
1273 const UnicodeString& realID) {
1274 umtx_init(&registryMutex);
1275 Mutex lock(&registryMutex);
46f4442e
A
1276 UErrorCode ec = U_ZERO_ERROR;
1277 if (HAVE_REGISTRY(ec)) {
73c04bcf
A
1278 _registerAlias(aliasID, realID);
1279 }
1280}
1281
1282void Transliterator::_registerAlias(const UnicodeString& aliasID,
1283 const UnicodeString& realID) {
46f4442e
A
1284 UErrorCode ec = U_ZERO_ERROR;
1285 registry->put(aliasID, realID, FALSE, TRUE, ec);
73c04bcf
A
1286}
1287
b75a7d8f
A
1288/**
1289 * Unregisters a transliterator or class. This may be either
1290 * a system transliterator or a user transliterator or class.
1291 *
1292 * @param ID the ID of the transliterator or class
1293 * @see #registerInstance
1294
1295 */
374ca955 1296void U_EXPORT2 Transliterator::unregister(const UnicodeString& ID) {
b75a7d8f
A
1297 umtx_init(&registryMutex);
1298 Mutex lock(&registryMutex);
46f4442e
A
1299 UErrorCode ec = U_ZERO_ERROR;
1300 if (HAVE_REGISTRY(ec)) {
b75a7d8f
A
1301 registry->remove(ID);
1302 }
1303}
1304
1305/**
374ca955 1306 * == OBSOLETE - remove in ICU 3.4 ==
b75a7d8f
A
1307 * Return the number of IDs currently registered with the system.
1308 * To retrieve the actual IDs, call getAvailableID(i) with
1309 * i from 0 to countAvailableIDs() - 1.
1310 */
374ca955 1311int32_t U_EXPORT2 Transliterator::countAvailableIDs(void) {
46f4442e 1312 int32_t retVal = 0;
b75a7d8f
A
1313 umtx_init(&registryMutex);
1314 Mutex lock(&registryMutex);
46f4442e
A
1315 UErrorCode ec = U_ZERO_ERROR;
1316 if (HAVE_REGISTRY(ec)) {
1317 retVal = registry->countAvailableIDs();
1318 }
1319 return retVal;
b75a7d8f
A
1320}
1321
1322/**
374ca955 1323 * == OBSOLETE - remove in ICU 3.4 ==
b75a7d8f
A
1324 * Return the index-th available ID. index must be between 0
1325 * and countAvailableIDs() - 1, inclusive. If index is out of
1326 * range, the result of getAvailableID(0) is returned.
1327 */
374ca955 1328const UnicodeString& U_EXPORT2 Transliterator::getAvailableID(int32_t index) {
b75a7d8f
A
1329 const UnicodeString* result = NULL;
1330 umtx_init(&registryMutex);
1331 umtx_lock(&registryMutex);
46f4442e
A
1332 UErrorCode ec = U_ZERO_ERROR;
1333 if (HAVE_REGISTRY(ec)) {
b75a7d8f
A
1334 result = &registry->getAvailableID(index);
1335 }
1336 umtx_unlock(&registryMutex);
1337 U_ASSERT(result != NULL); // fail if no registry
1338 return *result;
1339}
1340
374ca955
A
1341StringEnumeration* U_EXPORT2 Transliterator::getAvailableIDs(UErrorCode& ec) {
1342 if (U_FAILURE(ec)) return NULL;
1343 StringEnumeration* result = NULL;
1344 umtx_init(&registryMutex);
1345 umtx_lock(&registryMutex);
46f4442e 1346 if (HAVE_REGISTRY(ec)) {
374ca955
A
1347 result = registry->getAvailableIDs();
1348 }
1349 umtx_unlock(&registryMutex);
1350 if (result == NULL) {
1351 ec = U_INTERNAL_TRANSLITERATOR_ERROR;
1352 }
1353 return result;
1354}
1355
1356int32_t U_EXPORT2 Transliterator::countAvailableSources(void) {
b75a7d8f
A
1357 umtx_init(&registryMutex);
1358 Mutex lock(&registryMutex);
46f4442e
A
1359 UErrorCode ec = U_ZERO_ERROR;
1360 return HAVE_REGISTRY(ec) ? _countAvailableSources() : 0;
b75a7d8f
A
1361}
1362
374ca955 1363UnicodeString& U_EXPORT2 Transliterator::getAvailableSource(int32_t index,
b75a7d8f
A
1364 UnicodeString& result) {
1365 umtx_init(&registryMutex);
1366 Mutex lock(&registryMutex);
46f4442e
A
1367 UErrorCode ec = U_ZERO_ERROR;
1368 if (HAVE_REGISTRY(ec)) {
b75a7d8f
A
1369 _getAvailableSource(index, result);
1370 }
1371 return result;
1372}
1373
374ca955 1374int32_t U_EXPORT2 Transliterator::countAvailableTargets(const UnicodeString& source) {
b75a7d8f
A
1375 umtx_init(&registryMutex);
1376 Mutex lock(&registryMutex);
46f4442e
A
1377 UErrorCode ec = U_ZERO_ERROR;
1378 return HAVE_REGISTRY(ec) ? _countAvailableTargets(source) : 0;
b75a7d8f
A
1379}
1380
374ca955 1381UnicodeString& U_EXPORT2 Transliterator::getAvailableTarget(int32_t index,
b75a7d8f
A
1382 const UnicodeString& source,
1383 UnicodeString& result) {
1384 umtx_init(&registryMutex);
1385 Mutex lock(&registryMutex);
46f4442e
A
1386 UErrorCode ec = U_ZERO_ERROR;
1387 if (HAVE_REGISTRY(ec)) {
b75a7d8f
A
1388 _getAvailableTarget(index, source, result);
1389 }
1390 return result;
1391}
1392
374ca955 1393int32_t U_EXPORT2 Transliterator::countAvailableVariants(const UnicodeString& source,
b75a7d8f
A
1394 const UnicodeString& target) {
1395 umtx_init(&registryMutex);
1396 Mutex lock(&registryMutex);
46f4442e
A
1397 UErrorCode ec = U_ZERO_ERROR;
1398 return HAVE_REGISTRY(ec) ? _countAvailableVariants(source, target) : 0;
b75a7d8f
A
1399}
1400
374ca955 1401UnicodeString& U_EXPORT2 Transliterator::getAvailableVariant(int32_t index,
b75a7d8f
A
1402 const UnicodeString& source,
1403 const UnicodeString& target,
1404 UnicodeString& result) {
1405 umtx_init(&registryMutex);
1406 Mutex lock(&registryMutex);
46f4442e
A
1407 UErrorCode ec = U_ZERO_ERROR;
1408 if (HAVE_REGISTRY(ec)) {
b75a7d8f
A
1409 _getAvailableVariant(index, source, target, result);
1410 }
1411 return result;
1412}
1413
1414int32_t Transliterator::_countAvailableSources(void) {
1415 return registry->countAvailableSources();
1416}
1417
1418UnicodeString& Transliterator::_getAvailableSource(int32_t index,
1419 UnicodeString& result) {
1420 return registry->getAvailableSource(index, result);
1421}
1422
1423int32_t Transliterator::_countAvailableTargets(const UnicodeString& source) {
1424 return registry->countAvailableTargets(source);
1425}
1426
1427UnicodeString& Transliterator::_getAvailableTarget(int32_t index,
1428 const UnicodeString& source,
1429 UnicodeString& result) {
1430 return registry->getAvailableTarget(index, source, result);
1431}
1432
1433int32_t Transliterator::_countAvailableVariants(const UnicodeString& source,
1434 const UnicodeString& target) {
1435 return registry->countAvailableVariants(source, target);
1436}
1437
1438UnicodeString& Transliterator::_getAvailableVariant(int32_t index,
1439 const UnicodeString& source,
1440 const UnicodeString& target,
1441 UnicodeString& result) {
1442 return registry->getAvailableVariant(index, source, target, result);
1443}
1444
1445#ifdef U_USE_DEPRECATED_TRANSLITERATOR_API
1446
1447/**
1448 * Method for subclasses to use to obtain a character in the given
1449 * string, with filtering.
1450 * @deprecated the new architecture provides filtering at the top
1451 * level. This method will be removed Dec 31 2001.
1452 */
1453UChar Transliterator::filteredCharAt(const Replaceable& text, int32_t i) const {
1454 UChar c;
1455 const UnicodeFilter* localFilter = getFilter();
1456 return (localFilter == 0) ? text.charAt(i) :
1457 (localFilter->contains(c = text.charAt(i)) ? c : (UChar)0xFFFE);
1458}
1459
1460#endif
1461
1462/**
1463 * If the registry is initialized, return TRUE. If not, initialize it
1464 * and return TRUE. If the registry cannot be initialized, return
1465 * FALSE (rare).
1466 *
1467 * IMPORTANT: Upon entry, registryMutex must be LOCKED. The entirely
1468 * initialization is done with the lock held. There is NO REASON to
1469 * unlock, since no other thread that is waiting on the registryMutex
1470 * cannot itself proceed until the registry is initialized.
1471 */
46f4442e 1472UBool Transliterator::initializeRegistry(UErrorCode &status) {
b75a7d8f
A
1473 if (registry != 0) {
1474 return TRUE;
1475 }
1476
b75a7d8f
A
1477 registry = new TransliteratorRegistry(status);
1478 if (registry == 0 || U_FAILURE(status)) {
1479 delete registry;
1480 registry = 0;
1481 return FALSE; // can't create registry, no recovery
1482 }
1483
1484 /* The following code parses the index table located in
374ca955 1485 * icu/data/translit/root.txt. The index is an n x 4 table
b75a7d8f 1486 * that follows this format:
374ca955
A
1487 * <id>{
1488 * file{
1489 * resource{"<resource>"}
1490 * direction{"<direction>"}
1491 * }
1492 * }
1493 * <id>{
1494 * internal{
1495 * resource{"<resource>"}
1496 * direction{"<direction"}
1497 * }
1498 * }
1499 * <id>{
1500 * alias{"<getInstanceArg"}
1501 * }
b75a7d8f
A
1502 * <id> is the ID of the system transliterator being defined. These
1503 * are public IDs enumerated by Transliterator.getAvailableIDs(),
1504 * unless the second field is "internal".
1505 *
1506 * <resource> is a ResourceReader resource name. Currently these refer
1507 * to file names under com/ibm/text/resources. This string is passed
1508 * directly to ResourceReader, together with <encoding>.
1509 *
1510 * <direction> is either "FORWARD" or "REVERSE".
1511 *
1512 * <getInstanceArg> is a string to be passed directly to
1513 * Transliterator.getInstance(). The returned Transliterator object
1514 * then has its ID changed to <id> and is returned.
1515 *
1516 * The extra blank field on "alias" lines is to make the array square.
1517 */
374ca955 1518 //static const char translit_index[] = "translit_index";
b75a7d8f
A
1519
1520 UResourceBundle *bundle, *transIDs, *colBund;
46f4442e 1521 bundle = ures_open(U_ICUDATA_TRANSLIT, NULL/*open default locale*/, &status);
b75a7d8f
A
1522 transIDs = ures_getByKey(bundle, RB_RULE_BASED_IDS, 0, &status);
1523
1524 int32_t row, maxRows;
1525 if (U_SUCCESS(status)) {
1526 maxRows = ures_getSize(transIDs);
1527 for (row = 0; row < maxRows; row++) {
1528 colBund = ures_getByIndex(transIDs, row, 0, &status);
374ca955 1529 if (U_SUCCESS(status)) {
73c04bcf 1530 UnicodeString id(ures_getKey(colBund), -1, US_INV);
374ca955
A
1531 UResourceBundle* res = ures_getNextResource(colBund, NULL, &status);
1532 const char* typeStr = ures_getKey(res);
1533 UChar type;
1534 u_charsToUChars(typeStr, &type, 1);
b75a7d8f
A
1535
1536 if (U_SUCCESS(status)) {
73c04bcf
A
1537 int32_t len = 0;
1538 const UChar *resString;
b75a7d8f
A
1539 switch (type) {
1540 case 0x66: // 'f'
1541 case 0x69: // 'i'
1542 // 'file' or 'internal';
1543 // row[2]=resource, row[3]=direction
1544 {
374ca955 1545
73c04bcf 1546 resString = ures_getStringByKey(res, "resource", &len, &status);
b75a7d8f
A
1547 UBool visible = (type == 0x0066 /*f*/);
1548 UTransDirection dir =
374ca955 1549 (ures_getUnicodeStringByKey(res, "direction", &status).charAt(0) ==
b75a7d8f
A
1550 0x0046 /*F*/) ?
1551 UTRANS_FORWARD : UTRANS_REVERSE;
46f4442e 1552 registry->put(id, UnicodeString(TRUE, resString, len), dir, TRUE, visible, status);
b75a7d8f
A
1553 }
1554 break;
1555 case 0x61: // 'a'
1556 // 'alias'; row[2]=createInstance argument
73c04bcf 1557 resString = ures_getString(res, &len, &status);
46f4442e 1558 registry->put(id, UnicodeString(TRUE, resString, len), TRUE, TRUE, status);
b75a7d8f
A
1559 break;
1560 }
1561 }
374ca955 1562 ures_close(res);
b75a7d8f 1563 }
b75a7d8f
A
1564 ures_close(colBund);
1565 }
1566 }
1567
1568 ures_close(transIDs);
1569 ures_close(bundle);
1570
1571 // Manually add prototypes that the system knows about to the
1572 // cache. This is how new non-rule-based transliterators are
1573 // added to the system.
46f4442e
A
1574
1575 // This is to allow for null pointer check
1576 NullTransliterator* tempNullTranslit = new NullTransliterator();
1577 LowercaseTransliterator* tempLowercaseTranslit = new LowercaseTransliterator();
1578 UppercaseTransliterator* tempUppercaseTranslit = new UppercaseTransliterator();
1579 TitlecaseTransliterator* tempTitlecaseTranslit = new TitlecaseTransliterator();
1580 UnicodeNameTransliterator* tempUnicodeTranslit = new UnicodeNameTransliterator();
1581 NameUnicodeTransliterator* tempNameUnicodeTranslit = new NameUnicodeTransliterator();
1582#if !UCONFIG_NO_BREAK_ITERATION
1583 // TODO: could or should these transliterators be referenced polymorphically once constructed?
1584 BreakTransliterator* tempBreakTranslit = new BreakTransliterator();
1585#endif
1586 // Check for null pointers
1587 if (tempNullTranslit == NULL || tempLowercaseTranslit == NULL || tempUppercaseTranslit == NULL ||
1588 tempTitlecaseTranslit == NULL || tempUnicodeTranslit == NULL ||
1589#if !UCONFIG_NO_BREAK_ITERATION
1590 tempBreakTranslit == NULL ||
1591#endif
1592 tempNameUnicodeTranslit == NULL )
1593 {
1594 delete tempNullTranslit;
1595 delete tempLowercaseTranslit;
1596 delete tempUppercaseTranslit;
1597 delete tempTitlecaseTranslit;
1598 delete tempUnicodeTranslit;
1599 delete tempNameUnicodeTranslit;
1600#if !UCONFIG_NO_BREAK_ITERATION
1601 delete tempBreakTranslit;
1602#endif
1603 // Since there was an error, remove registry
1604 delete registry;
1605 registry = NULL;
b75a7d8f 1606
46f4442e
A
1607 status = U_MEMORY_ALLOCATION_ERROR;
1608 return 0;
1609 }
1610
1611 registry->put(tempNullTranslit, TRUE, status);
1612 registry->put(tempLowercaseTranslit, TRUE, status);
1613 registry->put(tempUppercaseTranslit, TRUE, status);
1614 registry->put(tempTitlecaseTranslit, TRUE, status);
1615 registry->put(tempUnicodeTranslit, TRUE, status);
1616 registry->put(tempNameUnicodeTranslit, TRUE, status);
1617#if !UCONFIG_NO_BREAK_ITERATION
1618 registry->put(tempBreakTranslit, FALSE, status); // FALSE means invisible.
1619#endif
b75a7d8f
A
1620
1621 RemoveTransliterator::registerIDs(); // Must be within mutex
1622 EscapeTransliterator::registerIDs();
1623 UnescapeTransliterator::registerIDs();
1624 NormalizationTransliterator::registerIDs();
1625 AnyTransliterator::registerIDs();
1626
73c04bcf
A
1627 _registerSpecialInverse(UNICODE_STRING_SIMPLE("Null"),
1628 UNICODE_STRING_SIMPLE("Null"), FALSE);
374ca955
A
1629 _registerSpecialInverse(UNICODE_STRING_SIMPLE("Upper"),
1630 UNICODE_STRING_SIMPLE("Lower"), TRUE);
1631 _registerSpecialInverse(UNICODE_STRING_SIMPLE("Title"),
1632 UNICODE_STRING_SIMPLE("Lower"), FALSE);
b75a7d8f 1633
374ca955 1634 ucln_i18n_registerCleanup(UCLN_I18N_TRANSLITERATOR, transliterator_cleanup);
b75a7d8f
A
1635
1636 return TRUE;
1637}
1638
1639U_NAMESPACE_END
1640
1641// Defined in ucln_in.h:
1642
1643/**
1644 * Release all static memory held by transliterator. This will
1645 * necessarily invalidate any rule-based transliterators held by the
1646 * user, because RBTs hold pointers to common data objects.
1647 */
1648U_CFUNC UBool transliterator_cleanup(void) {
46f4442e 1649 U_NAMESPACE_USE
b75a7d8f
A
1650 TransliteratorIDParser::cleanup();
1651 if (registry) {
1652 delete registry;
1653 registry = NULL;
1654 }
1655 umtx_destroy(&registryMutex);
1656 return TRUE;
1657}
1658
1659#endif /* #if !UCONFIG_NO_TRANSLITERATION */
1660
1661//eof