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