File: | out/../deps/icu-small/source/i18n/translit.cpp |
Warning: | line 1351, column 5 Returning null reference |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
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_TRANSLITERATION0 | |||
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)) (registry!=0 || initializeRegistry(status)) | |||
104 | ||||
105 | U_NAMESPACE_BEGINnamespace icu_71 { | |||
106 | ||||
107 | UOBJECT_DEFINE_ABSTRACT_RTTI_IMPLEMENTATION(Transliterator)UClassID Transliterator::getStaticClassID() { static char classID = 0; return (UClassID)&classID; } | |||
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 | */ | |||
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 | */ | |||
142 | Transliterator::~Transliterator() { | |||
143 | if (filter) { | |||
144 | delete filter; | |||
145 | } | |||
146 | } | |||
147 | ||||
148 | /** | |||
149 | * Copy constructor. | |||
150 | */ | |||
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 | ||||
165 | Transliterator* Transliterator::clone() const { | |||
166 | return NULL__null; | |||
167 | } | |||
168 | ||||
169 | /** | |||
170 | * Assignment operator. | |||
171 | */ | |||
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 | */ | |||
192 | 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, FALSE0, TRUE1); | |||
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 | */ | |||
213 | 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 | */ | |||
281 | 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 | */ | |||
303 | 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 | */ | |||
322 | 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 | */ | |||
338 | void Transliterator::finishTransliteration(Replaceable& text, | |||
339 | UTransPosition& index) const { | |||
340 | if (!positionIsValid(index, text.length())) { | |||
341 | return; | |||
342 | } | |||
343 | ||||
344 | filteredTransliterate(text, index, FALSE0, TRUE1); | |||
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 | */ | |||
354 | 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))(((text.charAt(index.limit - 1))&0xfffffc00)==0xd800)) { | |||
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, TRUE1, TRUE1); | |||
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))((uint32_t)(text.char32At(newCS))<=0xffff ? 1 : 2) - 1; | |||
415 | } | |||
416 | index.contextStart = uprv_maxuprv_max_71(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 | */ | |||
428 | 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__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)((uint32_t)(c)<=0xffff ? 1 : 2); | |||
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)((uint32_t)(c)<=0xffff ? 1 : 2); | |||
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 ? FALSE0 : 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))((uint32_t)(text.char32At(passLimit))<=0xffff ? 1 : 2); | |||
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, TRUE1); | |||
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__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 | ||||
682 | void Transliterator::filteredTransliterate(Replaceable& text, | |||
683 | UTransPosition& index, | |||
684 | UBool incremental) const { | |||
685 | filteredTransliterate(text, index, incremental, FALSE0); | |||
686 | } | |||
687 | ||||
688 | /** | |||
689 | * Method for subclasses to use to set the maximum context length. | |||
690 | * @see #getMaximumContextLength | |||
691 | */ | |||
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 | */ | |||
703 | 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 | */ | |||
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 | */ | |||
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"icudt" "71" "l" "-" "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_isInvariantUStringuprv_isInvariantUString_71(ID.getBuffer(), ID.length())) { | |||
762 | char key[200]; | |||
763 | uprv_strcpy(key, RB_DISPLAY_NAME_PREFIX):: strcpy(key, RB_DISPLAY_NAME_PREFIX); | |||
764 | int32_t length=(int32_t)uprv_strlen(RB_DISPLAY_NAME_PREFIX):: strlen(RB_DISPLAY_NAME_PREFIX); | |||
765 | ID.extract(0, (int32_t)(sizeof(key)-length), key+length, (int32_t)(sizeof(key)-length), US_INVicu::UnicodeString::kInvariant); | |||
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_FORMATTING0 | |||
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):: 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):: strcpy(key, RB_SCRIPT_DISPLAY_NAME_PREFIX); | |||
802 | args[j].getString(s); | |||
803 | if (uprv_isInvariantUStringuprv_isInvariantUString_71(s.getBuffer(), s.length())) { | |||
804 | s.extract(0, sizeof(key)-length-1, key+length, (int32_t)sizeof(key)-length-1, US_INVicu::UnicodeString::kInvariant); | |||
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 | */ | |||
837 | 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 | */ | |||
847 | UnicodeFilter* Transliterator::orphanFilter(void) { | |||
848 | UnicodeFilter *result = filter; | |||
849 | filter = NULL__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 | */ | |||
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 | */ | |||
886 | Transliterator* Transliterator::createInverse(UErrorCode& status) const { | |||
887 | UParseError parseError; | |||
888 | return Transliterator::createInstance(ID, UTRANS_REVERSE,parseError,status); | |||
889 | } | |||
890 | ||||
891 | Transliterator* U_EXPORT2 | |||
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 | |||
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__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__null; | |||
934 | } | |||
935 | LocalPointer<UnicodeSet> lpGlobalFilter(globalFilter); | |||
936 | ||||
937 | TransliteratorIDParser::instantiateList(list, status); | |||
938 | if (U_FAILURE(status)) { | |||
939 | return NULL__null; | |||
940 | } | |||
941 | ||||
942 | U_ASSERT(list.size() > 0)(void)0; | |||
943 | Transliterator* t = NULL__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__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 | */ | |||
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_lockumtx_lock_71(®istryMutex); | |||
985 | if (HAVE_REGISTRY(ec)(registry!=0 || initializeRegistry(ec))) { | |||
986 | t = registry->get(id, alias, ec); | |||
987 | } | |||
988 | umtx_unlockumtx_unlock_71(®istryMutex); | |||
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)(void)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_lockumtx_lock_71(®istryMutex); | |||
1017 | if (HAVE_REGISTRY(ec)(registry!=0 || initializeRegistry(ec))) { | |||
1018 | t = registry->reget(id, parser, alias, ec); | |||
1019 | } | |||
1020 | umtx_unlockumtx_unlock_71(®istryMutex); | |||
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__null; | |||
1033 | break; | |||
1034 | } | |||
1035 | } | |||
1036 | ||||
1037 | if (t != NULL__null && canon != NULL__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 | |||
1053 | Transliterator::createFromRules(const UnicodeString& ID, | |||
1054 | const UnicodeString& rules, | |||
1055 | UTransDirection dir, | |||
1056 | UParseError& parseError, | |||
1057 | UErrorCode& status) | |||
1058 | { | |||
1059 | Transliterator* t = NULL__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), TRUE1); | |||
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__null) { | |||
1081 | UnicodeString filterPattern; | |||
1082 | parser.compoundFilter->toPattern(filterPattern, FALSE0); | |||
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__null) { | |||
1091 | t->setID(ID); | |||
1092 | } | |||
1093 | } | |||
1094 | else { | |||
1095 | UVector transliterators(status); | |||
1096 | // TODO ICU-21701 missing U_FAILURE check here. | |||
1097 | // Error and nullptr checking through this whole block looks suspect. | |||
1098 | int32_t passNumber = 1; | |||
1099 | ||||
1100 | int32_t limit = parser.idBlockVector.size(); | |||
1101 | if (parser.dataVector.size() > limit) | |||
1102 | limit = parser.dataVector.size(); | |||
1103 | ||||
1104 | for (int32_t i = 0; i < limit; i++) { | |||
1105 | if (i < parser.idBlockVector.size()) { | |||
1106 | UnicodeString* idBlock = (UnicodeString*)parser.idBlockVector.elementAt(i); | |||
1107 | if (!idBlock->isEmpty()) { | |||
1108 | Transliterator* temp = createInstance(*idBlock, UTRANS_FORWARD, parseError, status); | |||
1109 | if (U_FAILURE(status)) { | |||
1110 | delete temp; | |||
1111 | return nullptr; | |||
1112 | } | |||
1113 | if (temp != NULL__null && typeid(*temp) != typeid(NullTransliterator)) { | |||
1114 | transliterators.addElement(temp, status); | |||
1115 | if (U_FAILURE(status)) { | |||
1116 | delete temp; | |||
1117 | return nullptr; | |||
1118 | } | |||
1119 | } else { | |||
1120 | delete temp; | |||
1121 | } | |||
1122 | } | |||
1123 | } | |||
1124 | if (!parser.dataVector.isEmpty()) { | |||
1125 | TransliterationRuleData* data = (TransliterationRuleData*)parser.dataVector.orphanElementAt(0); | |||
1126 | // TODO: Should passNumber be turned into a decimal-string representation (1 -> "1")? | |||
1127 | RuleBasedTransliterator* temprbt = new RuleBasedTransliterator(UnicodeString(CompoundTransliterator::PASS_STRING) + UnicodeString(passNumber++), | |||
1128 | data, TRUE1); | |||
1129 | // Check if NULL before adding it to transliterators to avoid future usage of NULL pointer. | |||
1130 | if (temprbt == NULL__null) { | |||
1131 | if (U_SUCCESS(status)) { | |||
1132 | status = U_MEMORY_ALLOCATION_ERROR; | |||
1133 | } | |||
1134 | return t; | |||
1135 | } | |||
1136 | transliterators.addElement(temprbt, status); | |||
1137 | if (U_FAILURE(status)) { | |||
1138 | delete temprbt; | |||
1139 | return t; | |||
1140 | } | |||
1141 | // TODO: ICU-21701 the transliterators vector will leak its contents if anything goes wrong. | |||
1142 | // Under normal operation, the CompoundTransliterator constructor adopts the | |||
1143 | // the contents of the vector. | |||
1144 | } | |||
1145 | } | |||
1146 | ||||
1147 | t = new CompoundTransliterator(transliterators, passNumber - 1, parseError, status); | |||
1148 | // Null pointer check | |||
1149 | if (t != NULL__null) { | |||
1150 | t->setID(ID); | |||
1151 | t->adoptFilter(parser.orphanCompoundFilter()); | |||
1152 | } | |||
1153 | } | |||
1154 | if (U_SUCCESS(status) && t == NULL__null) { | |||
1155 | status = U_MEMORY_ALLOCATION_ERROR; | |||
1156 | } | |||
1157 | return t; | |||
1158 | } | |||
1159 | ||||
1160 | UnicodeString& Transliterator::toRules(UnicodeString& rulesSource, | |||
1161 | UBool escapeUnprintable) const { | |||
1162 | // The base class implementation of toRules munges the ID into | |||
1163 | // the correct format. That is: foo => ::foo | |||
1164 | if (escapeUnprintable) { | |||
1165 | rulesSource.truncate(0); | |||
1166 | UnicodeString id = getID(); | |||
1167 | for (int32_t i=0; i<id.length();) { | |||
1168 | UChar32 c = id.char32At(i); | |||
1169 | if (!ICU_Utility::escapeUnprintable(rulesSource, c)) { | |||
1170 | rulesSource.append(c); | |||
1171 | } | |||
1172 | i += U16_LENGTH(c)((uint32_t)(c)<=0xffff ? 1 : 2); | |||
1173 | } | |||
1174 | } else { | |||
1175 | rulesSource = getID(); | |||
1176 | } | |||
1177 | // KEEP in sync with rbt_pars | |||
1178 | rulesSource.insert(0, UNICODE_STRING_SIMPLE("::")icu::UnicodeString(true, u"::", -1)); | |||
1179 | rulesSource.append(ID_DELIM); | |||
1180 | return rulesSource; | |||
1181 | } | |||
1182 | ||||
1183 | int32_t Transliterator::countElements() const { | |||
1184 | const CompoundTransliterator* ct = dynamic_cast<const CompoundTransliterator*>(this); | |||
1185 | return ct != NULL__null ? ct->getCount() : 0; | |||
1186 | } | |||
1187 | ||||
1188 | const Transliterator& Transliterator::getElement(int32_t index, UErrorCode& ec) const { | |||
1189 | if (U_FAILURE(ec)) { | |||
1190 | return *this; | |||
1191 | } | |||
1192 | const CompoundTransliterator* cpd = dynamic_cast<const CompoundTransliterator*>(this); | |||
1193 | int32_t n = (cpd == NULL__null) ? 1 : cpd->getCount(); | |||
1194 | if (index < 0 || index >= n) { | |||
1195 | ec = U_INDEX_OUTOFBOUNDS_ERROR; | |||
1196 | return *this; | |||
1197 | } else { | |||
1198 | return (n == 1) ? *this : cpd->getTransliterator(index); | |||
1199 | } | |||
1200 | } | |||
1201 | ||||
1202 | UnicodeSet& Transliterator::getSourceSet(UnicodeSet& result) const { | |||
1203 | handleGetSourceSet(result); | |||
1204 | if (filter != NULL__null) { | |||
1205 | UnicodeSet* filterSet = dynamic_cast<UnicodeSet*>(filter); | |||
1206 | UBool deleteFilterSet = FALSE0; | |||
1207 | // Most, but not all filters will be UnicodeSets. Optimize for | |||
1208 | // the high-runner case. | |||
1209 | if (filterSet == NULL__null) { | |||
1210 | filterSet = new UnicodeSet(); | |||
1211 | // Check null pointer | |||
1212 | if (filterSet == NULL__null) { | |||
1213 | return result; | |||
1214 | } | |||
1215 | deleteFilterSet = TRUE1; | |||
1216 | filter->addMatchSetTo(*filterSet); | |||
1217 | } | |||
1218 | result.retainAll(*filterSet); | |||
1219 | if (deleteFilterSet) { | |||
1220 | delete filterSet; | |||
1221 | } | |||
1222 | } | |||
1223 | return result; | |||
1224 | } | |||
1225 | ||||
1226 | void Transliterator::handleGetSourceSet(UnicodeSet& result) const { | |||
1227 | result.clear(); | |||
1228 | } | |||
1229 | ||||
1230 | UnicodeSet& Transliterator::getTargetSet(UnicodeSet& result) const { | |||
1231 | return result.clear(); | |||
1232 | } | |||
1233 | ||||
1234 | // For public consumption | |||
1235 | void U_EXPORT2 Transliterator::registerFactory(const UnicodeString& id, | |||
1236 | Transliterator::Factory factory, | |||
1237 | Transliterator::Token context) { | |||
1238 | Mutex lock(®istryMutex); | |||
1239 | UErrorCode ec = U_ZERO_ERROR; | |||
1240 | if (HAVE_REGISTRY(ec)(registry!=0 || initializeRegistry(ec))) { | |||
1241 | _registerFactory(id, factory, context); | |||
1242 | } | |||
1243 | } | |||
1244 | ||||
1245 | // To be called only by Transliterator subclasses that are called | |||
1246 | // to register themselves by initializeRegistry(). | |||
1247 | void Transliterator::_registerFactory(const UnicodeString& id, | |||
1248 | Transliterator::Factory factory, | |||
1249 | Transliterator::Token context) { | |||
1250 | UErrorCode ec = U_ZERO_ERROR; | |||
1251 | registry->put(id, factory, context, TRUE1, ec); | |||
1252 | } | |||
1253 | ||||
1254 | // To be called only by Transliterator subclasses that are called | |||
1255 | // to register themselves by initializeRegistry(). | |||
1256 | void Transliterator::_registerSpecialInverse(const UnicodeString& target, | |||
1257 | const UnicodeString& inverseTarget, | |||
1258 | UBool bidirectional) { | |||
1259 | UErrorCode status = U_ZERO_ERROR; | |||
1260 | TransliteratorIDParser::registerSpecialInverse(target, inverseTarget, bidirectional, status); | |||
1261 | } | |||
1262 | ||||
1263 | /** | |||
1264 | * Registers a instance <tt>obj</tt> of a subclass of | |||
1265 | * <code>Transliterator</code> with the system. This object must | |||
1266 | * implement the <tt>clone()</tt> method. When | |||
1267 | * <tt>getInstance()</tt> is called with an ID string that is | |||
1268 | * equal to <tt>obj.getID()</tt>, then <tt>obj.clone()</tt> is | |||
1269 | * returned. | |||
1270 | * | |||
1271 | * @param obj an instance of subclass of | |||
1272 | * <code>Transliterator</code> that defines <tt>clone()</tt> | |||
1273 | * @see #getInstance | |||
1274 | * @see #unregister | |||
1275 | */ | |||
1276 | void U_EXPORT2 Transliterator::registerInstance(Transliterator* adoptedPrototype) { | |||
1277 | Mutex lock(®istryMutex); | |||
1278 | UErrorCode ec = U_ZERO_ERROR; | |||
1279 | if (HAVE_REGISTRY(ec)(registry!=0 || initializeRegistry(ec))) { | |||
1280 | _registerInstance(adoptedPrototype); | |||
1281 | } | |||
1282 | } | |||
1283 | ||||
1284 | void Transliterator::_registerInstance(Transliterator* adoptedPrototype) { | |||
1285 | UErrorCode ec = U_ZERO_ERROR; | |||
1286 | registry->put(adoptedPrototype, TRUE1, ec); | |||
1287 | } | |||
1288 | ||||
1289 | void U_EXPORT2 Transliterator::registerAlias(const UnicodeString& aliasID, | |||
1290 | const UnicodeString& realID) { | |||
1291 | Mutex lock(®istryMutex); | |||
1292 | UErrorCode ec = U_ZERO_ERROR; | |||
1293 | if (HAVE_REGISTRY(ec)(registry!=0 || initializeRegistry(ec))) { | |||
1294 | _registerAlias(aliasID, realID); | |||
1295 | } | |||
1296 | } | |||
1297 | ||||
1298 | void Transliterator::_registerAlias(const UnicodeString& aliasID, | |||
1299 | const UnicodeString& realID) { | |||
1300 | UErrorCode ec = U_ZERO_ERROR; | |||
1301 | registry->put(aliasID, realID, FALSE0, TRUE1, ec); | |||
1302 | } | |||
1303 | ||||
1304 | /** | |||
1305 | * Unregisters a transliterator or class. This may be either | |||
1306 | * a system transliterator or a user transliterator or class. | |||
1307 | * | |||
1308 | * @param ID the ID of the transliterator or class | |||
1309 | * @see #registerInstance | |||
1310 | ||||
1311 | */ | |||
1312 | void U_EXPORT2 Transliterator::unregister(const UnicodeString& ID) { | |||
1313 | Mutex lock(®istryMutex); | |||
1314 | UErrorCode ec = U_ZERO_ERROR; | |||
1315 | if (HAVE_REGISTRY(ec)(registry!=0 || initializeRegistry(ec))) { | |||
1316 | registry->remove(ID); | |||
1317 | } | |||
1318 | } | |||
1319 | ||||
1320 | /** | |||
1321 | * == OBSOLETE - remove in ICU 3.4 == | |||
1322 | * Return the number of IDs currently registered with the system. | |||
1323 | * To retrieve the actual IDs, call getAvailableID(i) with | |||
1324 | * i from 0 to countAvailableIDs() - 1. | |||
1325 | */ | |||
1326 | int32_t U_EXPORT2 Transliterator::countAvailableIDs(void) { | |||
1327 | int32_t retVal = 0; | |||
1328 | Mutex lock(®istryMutex); | |||
1329 | UErrorCode ec = U_ZERO_ERROR; | |||
1330 | if (HAVE_REGISTRY(ec)(registry!=0 || initializeRegistry(ec))) { | |||
1331 | retVal = registry->countAvailableIDs(); | |||
1332 | } | |||
1333 | return retVal; | |||
1334 | } | |||
1335 | ||||
1336 | /** | |||
1337 | * == OBSOLETE - remove in ICU 3.4 == | |||
1338 | * Return the index-th available ID. index must be between 0 | |||
1339 | * and countAvailableIDs() - 1, inclusive. If index is out of | |||
1340 | * range, the result of getAvailableID(0) is returned. | |||
1341 | */ | |||
1342 | const UnicodeString& U_EXPORT2 Transliterator::getAvailableID(int32_t index) { | |||
1343 | const UnicodeString* result = NULL__null; | |||
| ||||
1344 | umtx_lockumtx_lock_71(®istryMutex); | |||
1345 | UErrorCode ec = U_ZERO_ERROR; | |||
1346 | if (HAVE_REGISTRY(ec)(registry!=0 || initializeRegistry(ec))) { | |||
1347 | result = ®istry->getAvailableID(index); | |||
1348 | } | |||
1349 | umtx_unlockumtx_unlock_71(®istryMutex); | |||
1350 | U_ASSERT(result != NULL)(void)0; // fail if no registry | |||
1351 | return *result; | |||
| ||||
1352 | } | |||
1353 | ||||
1354 | StringEnumeration* U_EXPORT2 Transliterator::getAvailableIDs(UErrorCode& ec) { | |||
1355 | if (U_FAILURE(ec)) return NULL__null; | |||
1356 | StringEnumeration* result = NULL__null; | |||
1357 | umtx_lockumtx_lock_71(®istryMutex); | |||
1358 | if (HAVE_REGISTRY(ec)(registry!=0 || initializeRegistry(ec))) { | |||
1359 | result = registry->getAvailableIDs(); | |||
1360 | } | |||
1361 | umtx_unlockumtx_unlock_71(®istryMutex); | |||
1362 | if (result == NULL__null) { | |||
1363 | ec = U_INTERNAL_TRANSLITERATOR_ERROR; | |||
1364 | } | |||
1365 | return result; | |||
1366 | } | |||
1367 | ||||
1368 | int32_t U_EXPORT2 Transliterator::countAvailableSources(void) { | |||
1369 | Mutex lock(®istryMutex); | |||
1370 | UErrorCode ec = U_ZERO_ERROR; | |||
1371 | return HAVE_REGISTRY(ec)(registry!=0 || initializeRegistry(ec)) ? _countAvailableSources() : 0; | |||
1372 | } | |||
1373 | ||||
1374 | UnicodeString& U_EXPORT2 Transliterator::getAvailableSource(int32_t index, | |||
1375 | UnicodeString& result) { | |||
1376 | Mutex lock(®istryMutex); | |||
1377 | UErrorCode ec = U_ZERO_ERROR; | |||
1378 | if (HAVE_REGISTRY(ec)(registry!=0 || initializeRegistry(ec))) { | |||
1379 | _getAvailableSource(index, result); | |||
1380 | } | |||
1381 | return result; | |||
1382 | } | |||
1383 | ||||
1384 | int32_t U_EXPORT2 Transliterator::countAvailableTargets(const UnicodeString& source) { | |||
1385 | Mutex lock(®istryMutex); | |||
1386 | UErrorCode ec = U_ZERO_ERROR; | |||
1387 | return HAVE_REGISTRY(ec)(registry!=0 || initializeRegistry(ec)) ? _countAvailableTargets(source) : 0; | |||
1388 | } | |||
1389 | ||||
1390 | UnicodeString& U_EXPORT2 Transliterator::getAvailableTarget(int32_t index, | |||
1391 | const UnicodeString& source, | |||
1392 | UnicodeString& result) { | |||
1393 | Mutex lock(®istryMutex); | |||
1394 | UErrorCode ec = U_ZERO_ERROR; | |||
1395 | if (HAVE_REGISTRY(ec)(registry!=0 || initializeRegistry(ec))) { | |||
1396 | _getAvailableTarget(index, source, result); | |||
1397 | } | |||
1398 | return result; | |||
1399 | } | |||
1400 | ||||
1401 | int32_t U_EXPORT2 Transliterator::countAvailableVariants(const UnicodeString& source, | |||
1402 | const UnicodeString& target) { | |||
1403 | Mutex lock(®istryMutex); | |||
1404 | UErrorCode ec = U_ZERO_ERROR; | |||
1405 | return HAVE_REGISTRY(ec)(registry!=0 || initializeRegistry(ec)) ? _countAvailableVariants(source, target) : 0; | |||
1406 | } | |||
1407 | ||||
1408 | UnicodeString& U_EXPORT2 Transliterator::getAvailableVariant(int32_t index, | |||
1409 | const UnicodeString& source, | |||
1410 | const UnicodeString& target, | |||
1411 | UnicodeString& result) { | |||
1412 | Mutex lock(®istryMutex); | |||
1413 | UErrorCode ec = U_ZERO_ERROR; | |||
1414 | if (HAVE_REGISTRY(ec)(registry!=0 || initializeRegistry(ec))) { | |||
1415 | _getAvailableVariant(index, source, target, result); | |||
1416 | } | |||
1417 | return result; | |||
1418 | } | |||
1419 | ||||
1420 | int32_t Transliterator::_countAvailableSources(void) { | |||
1421 | return registry->countAvailableSources(); | |||
1422 | } | |||
1423 | ||||
1424 | UnicodeString& Transliterator::_getAvailableSource(int32_t index, | |||
1425 | UnicodeString& result) { | |||
1426 | return registry->getAvailableSource(index, result); | |||
1427 | } | |||
1428 | ||||
1429 | int32_t Transliterator::_countAvailableTargets(const UnicodeString& source) { | |||
1430 | return registry->countAvailableTargets(source); | |||
1431 | } | |||
1432 | ||||
1433 | UnicodeString& Transliterator::_getAvailableTarget(int32_t index, | |||
1434 | const UnicodeString& source, | |||
1435 | UnicodeString& result) { | |||
1436 | return registry->getAvailableTarget(index, source, result); | |||
1437 | } | |||
1438 | ||||
1439 | int32_t Transliterator::_countAvailableVariants(const UnicodeString& source, | |||
1440 | const UnicodeString& target) { | |||
1441 | return registry->countAvailableVariants(source, target); | |||
1442 | } | |||
1443 | ||||
1444 | UnicodeString& Transliterator::_getAvailableVariant(int32_t index, | |||
1445 | const UnicodeString& source, | |||
1446 | const UnicodeString& target, | |||
1447 | UnicodeString& result) { | |||
1448 | return registry->getAvailableVariant(index, source, target, result); | |||
1449 | } | |||
1450 | ||||
1451 | #ifdef U_USE_DEPRECATED_TRANSLITERATOR_API | |||
1452 | ||||
1453 | /** | |||
1454 | * Method for subclasses to use to obtain a character in the given | |||
1455 | * string, with filtering. | |||
1456 | * @deprecated the new architecture provides filtering at the top | |||
1457 | * level. This method will be removed Dec 31 2001. | |||
1458 | */ | |||
1459 | UChar Transliterator::filteredCharAt(const Replaceable& text, int32_t i) const { | |||
1460 | UChar c; | |||
1461 | const UnicodeFilter* localFilter = getFilter(); | |||
1462 | return (localFilter == 0) ? text.charAt(i) : | |||
1463 | (localFilter->contains(c = text.charAt(i)) ? c : (UChar)0xFFFE); | |||
1464 | } | |||
1465 | ||||
1466 | #endif | |||
1467 | ||||
1468 | /** | |||
1469 | * If the registry is initialized, return TRUE. If not, initialize it | |||
1470 | * and return TRUE. If the registry cannot be initialized, return | |||
1471 | * FALSE (rare). | |||
1472 | * | |||
1473 | * IMPORTANT: Upon entry, registryMutex must be LOCKED. The entire | |||
1474 | * initialization is done with the lock held. There is NO REASON to | |||
1475 | * unlock, since no other thread that is waiting on the registryMutex | |||
1476 | * cannot itself proceed until the registry is initialized. | |||
1477 | */ | |||
1478 | UBool Transliterator::initializeRegistry(UErrorCode &status) { | |||
1479 | if (registry != 0) { | |||
1480 | return TRUE1; | |||
1481 | } | |||
1482 | ||||
1483 | registry = new TransliteratorRegistry(status); | |||
1484 | if (registry == 0 || U_FAILURE(status)) { | |||
1485 | delete registry; | |||
1486 | registry = 0; | |||
1487 | return FALSE0; // can't create registry, no recovery | |||
1488 | } | |||
1489 | ||||
1490 | /* The following code parses the index table located in | |||
1491 | * icu/data/translit/root.txt. The index is an n x 4 table | |||
1492 | * that follows this format: | |||
1493 | * <id>{ | |||
1494 | * file{ | |||
1495 | * resource{"<resource>"} | |||
1496 | * direction{"<direction>"} | |||
1497 | * } | |||
1498 | * } | |||
1499 | * <id>{ | |||
1500 | * internal{ | |||
1501 | * resource{"<resource>"} | |||
1502 | * direction{"<direction"} | |||
1503 | * } | |||
1504 | * } | |||
1505 | * <id>{ | |||
1506 | * alias{"<getInstanceArg"} | |||
1507 | * } | |||
1508 | * <id> is the ID of the system transliterator being defined. These | |||
1509 | * are public IDs enumerated by Transliterator.getAvailableIDs(), | |||
1510 | * unless the second field is "internal". | |||
1511 | * | |||
1512 | * <resource> is a ResourceReader resource name. Currently these refer | |||
1513 | * to file names under com/ibm/text/resources. This string is passed | |||
1514 | * directly to ResourceReader, together with <encoding>. | |||
1515 | * | |||
1516 | * <direction> is either "FORWARD" or "REVERSE". | |||
1517 | * | |||
1518 | * <getInstanceArg> is a string to be passed directly to | |||
1519 | * Transliterator.getInstance(). The returned Transliterator object | |||
1520 | * then has its ID changed to <id> and is returned. | |||
1521 | * | |||
1522 | * The extra blank field on "alias" lines is to make the array square. | |||
1523 | */ | |||
1524 | //static const char translit_index[] = "translit_index"; | |||
1525 | ||||
1526 | UErrorCode lstatus = U_ZERO_ERROR; | |||
1527 | UResourceBundle *bundle, *transIDs, *colBund; | |||
1528 | bundle = ures_openures_open_71(U_ICUDATA_TRANSLIT"icudt" "71" "l" "-" "translit", NULL__null/*open default locale*/, &lstatus); | |||
1529 | transIDs = ures_getByKeyures_getByKey_71(bundle, RB_RULE_BASED_IDS, 0, &lstatus); | |||
1530 | const UnicodeString T_PART = UNICODE_STRING_SIMPLE("-t-")icu::UnicodeString(true, u"-t-", -1); | |||
1531 | ||||
1532 | int32_t row, maxRows; | |||
1533 | if (lstatus == U_MEMORY_ALLOCATION_ERROR) { | |||
1534 | delete registry; | |||
1535 | registry = nullptr; | |||
1536 | status = U_MEMORY_ALLOCATION_ERROR; | |||
1537 | return FALSE0; | |||
1538 | } | |||
1539 | if (U_SUCCESS(lstatus)) { | |||
1540 | maxRows = ures_getSizeures_getSize_71(transIDs); | |||
1541 | for (row = 0; row < maxRows; row++) { | |||
1542 | colBund = ures_getByIndexures_getByIndex_71(transIDs, row, 0, &lstatus); | |||
1543 | if (U_SUCCESS(lstatus)) { | |||
1544 | UnicodeString id(ures_getKeyures_getKey_71(colBund), -1, US_INVicu::UnicodeString::kInvariant); | |||
1545 | if(id.indexOf(T_PART) != -1) { | |||
1546 | ures_closeures_close_71(colBund); | |||
1547 | continue; | |||
1548 | } | |||
1549 | UResourceBundle* res = ures_getNextResourceures_getNextResource_71(colBund, NULL__null, &lstatus); | |||
1550 | const char* typeStr = ures_getKeyures_getKey_71(res); | |||
1551 | UChar type; | |||
1552 | u_charsToUCharsu_charsToUChars_71(typeStr, &type, 1); | |||
1553 | ||||
1554 | if (U_SUCCESS(lstatus)) { | |||
1555 | int32_t len = 0; | |||
1556 | const UChar *resString; | |||
1557 | switch (type) { | |||
1558 | case 0x66: // 'f' | |||
1559 | case 0x69: // 'i' | |||
1560 | // 'file' or 'internal'; | |||
1561 | // row[2]=resource, row[3]=direction | |||
1562 | { | |||
1563 | ||||
1564 | resString = ures_getStringByKeyures_getStringByKey_71(res, "resource", &len, &lstatus); | |||
1565 | UBool visible = (type == 0x0066 /*f*/); | |||
1566 | UTransDirection dir = | |||
1567 | (ures_getUnicodeStringByKey(res, "direction", &lstatus).charAt(0) == | |||
1568 | 0x0046 /*F*/) ? | |||
1569 | UTRANS_FORWARD : UTRANS_REVERSE; | |||
1570 | registry->put(id, UnicodeString(TRUE1, resString, len), dir, TRUE1, visible, lstatus); | |||
1571 | } | |||
1572 | break; | |||
1573 | case 0x61: // 'a' | |||
1574 | // 'alias'; row[2]=createInstance argument | |||
1575 | resString = ures_getStringures_getString_71(res, &len, &lstatus); | |||
1576 | registry->put(id, UnicodeString(TRUE1, resString, len), TRUE1, TRUE1, lstatus); | |||
1577 | break; | |||
1578 | } | |||
1579 | } | |||
1580 | ures_closeures_close_71(res); | |||
1581 | } | |||
1582 | ures_closeures_close_71(colBund); | |||
1583 | } | |||
1584 | } | |||
1585 | ||||
1586 | ures_closeures_close_71(transIDs); | |||
1587 | ures_closeures_close_71(bundle); | |||
1588 | ||||
1589 | // Manually add prototypes that the system knows about to the | |||
1590 | // cache. This is how new non-rule-based transliterators are | |||
1591 | // added to the system. | |||
1592 | ||||
1593 | // This is to allow for null pointer check | |||
1594 | NullTransliterator* tempNullTranslit = new NullTransliterator(); | |||
1595 | LowercaseTransliterator* tempLowercaseTranslit = new LowercaseTransliterator(); | |||
1596 | UppercaseTransliterator* tempUppercaseTranslit = new UppercaseTransliterator(); | |||
1597 | TitlecaseTransliterator* tempTitlecaseTranslit = new TitlecaseTransliterator(); | |||
1598 | UnicodeNameTransliterator* tempUnicodeTranslit = new UnicodeNameTransliterator(); | |||
1599 | NameUnicodeTransliterator* tempNameUnicodeTranslit = new NameUnicodeTransliterator(); | |||
1600 | #if !UCONFIG_NO_BREAK_ITERATION0 | |||
1601 | // TODO: could or should these transliterators be referenced polymorphically once constructed? | |||
1602 | BreakTransliterator* tempBreakTranslit = new BreakTransliterator(); | |||
1603 | #endif | |||
1604 | // Check for null pointers | |||
1605 | if (tempNullTranslit == NULL__null || tempLowercaseTranslit == NULL__null || tempUppercaseTranslit == NULL__null || | |||
1606 | tempTitlecaseTranslit == NULL__null || tempUnicodeTranslit == NULL__null || | |||
1607 | #if !UCONFIG_NO_BREAK_ITERATION0 | |||
1608 | tempBreakTranslit == NULL__null || | |||
1609 | #endif | |||
1610 | tempNameUnicodeTranslit == NULL__null ) | |||
1611 | { | |||
1612 | delete tempNullTranslit; | |||
1613 | delete tempLowercaseTranslit; | |||
1614 | delete tempUppercaseTranslit; | |||
1615 | delete tempTitlecaseTranslit; | |||
1616 | delete tempUnicodeTranslit; | |||
1617 | delete tempNameUnicodeTranslit; | |||
1618 | #if !UCONFIG_NO_BREAK_ITERATION0 | |||
1619 | delete tempBreakTranslit; | |||
1620 | #endif | |||
1621 | // Since there was an error, remove registry | |||
1622 | delete registry; | |||
1623 | registry = NULL__null; | |||
1624 | ||||
1625 | status = U_MEMORY_ALLOCATION_ERROR; | |||
1626 | return 0; | |||
1627 | } | |||
1628 | ||||
1629 | registry->put(tempNullTranslit, TRUE1, status); | |||
1630 | registry->put(tempLowercaseTranslit, TRUE1, status); | |||
1631 | registry->put(tempUppercaseTranslit, TRUE1, status); | |||
1632 | registry->put(tempTitlecaseTranslit, TRUE1, status); | |||
1633 | registry->put(tempUnicodeTranslit, TRUE1, status); | |||
1634 | registry->put(tempNameUnicodeTranslit, TRUE1, status); | |||
1635 | #if !UCONFIG_NO_BREAK_ITERATION0 | |||
1636 | registry->put(tempBreakTranslit, FALSE0, status); // FALSE means invisible. | |||
1637 | #endif | |||
1638 | ||||
1639 | RemoveTransliterator::registerIDs(); // Must be within mutex | |||
1640 | EscapeTransliterator::registerIDs(); | |||
1641 | UnescapeTransliterator::registerIDs(); | |||
1642 | NormalizationTransliterator::registerIDs(); | |||
1643 | AnyTransliterator::registerIDs(); | |||
1644 | ||||
1645 | _registerSpecialInverse(UNICODE_STRING_SIMPLE("Null")icu::UnicodeString(true, u"Null", -1), | |||
1646 | UNICODE_STRING_SIMPLE("Null")icu::UnicodeString(true, u"Null", -1), FALSE0); | |||
1647 | _registerSpecialInverse(UNICODE_STRING_SIMPLE("Upper")icu::UnicodeString(true, u"Upper", -1), | |||
1648 | UNICODE_STRING_SIMPLE("Lower")icu::UnicodeString(true, u"Lower", -1), TRUE1); | |||
1649 | _registerSpecialInverse(UNICODE_STRING_SIMPLE("Title")icu::UnicodeString(true, u"Title", -1), | |||
1650 | UNICODE_STRING_SIMPLE("Lower")icu::UnicodeString(true, u"Lower", -1), FALSE0); | |||
1651 | ||||
1652 | ucln_i18n_registerCleanupucln_i18n_registerCleanup_71(UCLN_I18N_TRANSLITERATOR, utrans_transliterator_cleanuputrans_transliterator_cleanup_71); | |||
1653 | ||||
1654 | return TRUE1; | |||
1655 | } | |||
1656 | ||||
1657 | U_NAMESPACE_END} | |||
1658 | ||||
1659 | // Defined in transreg.h: | |||
1660 | ||||
1661 | /** | |||
1662 | * Release all static memory held by transliterator. This will | |||
1663 | * necessarily invalidate any rule-based transliterators held by the | |||
1664 | * user, because RBTs hold pointers to common data objects. | |||
1665 | */ | |||
1666 | U_CFUNCextern "C" UBool utrans_transliterator_cleanuputrans_transliterator_cleanup_71(void) { | |||
1667 | U_NAMESPACE_USEusing namespace icu_71; | |||
1668 | TransliteratorIDParser::cleanup(); | |||
1669 | if (registry) { | |||
1670 | delete registry; | |||
1671 | registry = NULL__null; | |||
1672 | } | |||
1673 | return TRUE1; | |||
1674 | } | |||
1675 | ||||
1676 | #endif /* #if !UCONFIG_NO_TRANSLITERATION */ | |||
1677 | ||||
1678 | //eof |