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