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