]>
Commit | Line | Data |
---|---|---|
b75a7d8f A |
1 | /* |
2 | ********************************************************************** | |
2ca993e8 | 3 | * Copyright (c) 2001-2016 International Business Machines |
b75a7d8f A |
4 | * Corporation and others. All Rights Reserved. |
5 | ********************************************************************** | |
6 | * Date Name Description | |
7 | * 08/10/2001 aliu Creation. | |
8 | ********************************************************************** | |
9 | */ | |
10 | ||
11 | #include "unicode/utypes.h" | |
12 | ||
13 | #if !UCONFIG_NO_TRANSLITERATION | |
14 | ||
b75a7d8f | 15 | #include "unicode/translit.h" |
73c04bcf | 16 | #include "unicode/resbund.h" |
b75a7d8f A |
17 | #include "unicode/uniset.h" |
18 | #include "unicode/uscript.h" | |
19 | #include "rbt.h" | |
20 | #include "cpdtrans.h" | |
21 | #include "nultrans.h" | |
22 | #include "transreg.h" | |
23 | #include "rbt_data.h" | |
24 | #include "rbt_pars.h" | |
25 | #include "tridpars.h" | |
26 | #include "charstr.h" | |
374ca955 | 27 | #include "uassert.h" |
73c04bcf | 28 | #include "locutil.h" |
b75a7d8f A |
29 | |
30 | // Enable the following symbol to add debugging code that tracks the | |
31 | // allocation, deletion, and use of Entry objects. BoundsChecker has | |
32 | // reported dangling pointer errors with these objects, but I have | |
33 | // been unable to confirm them. I suspect BoundsChecker is getting | |
34 | // confused with pointers going into and coming out of a UHashtable, | |
35 | // despite the hinting code that is designed to help it. | |
36 | // #define DEBUG_MEM | |
37 | #ifdef DEBUG_MEM | |
38 | #include <stdio.h> | |
39 | #endif | |
40 | ||
41 | // UChar constants | |
42 | static const UChar LOCALE_SEP = 95; // '_' | |
73c04bcf A |
43 | //static const UChar ID_SEP = 0x002D; /*-*/ |
44 | //static const UChar VARIANT_SEP = 0x002F; // '/' | |
b75a7d8f A |
45 | |
46 | // String constants | |
2ca993e8 A |
47 | static const UChar ANY[] = { 0x41, 0x6E, 0x79, 0 }; // Any |
48 | static const UChar LAT[] = { 0x4C, 0x61, 0x74, 0 }; // Lat | |
b75a7d8f | 49 | |
4388f060 A |
50 | // empty string |
51 | #define NO_VARIANT UnicodeString() | |
52 | ||
2ca993e8 A |
53 | // initial estimate for specDAG size |
54 | #define SPECDAG_INIT_SIZE 134 | |
55 | ||
56 | // initial estimate for number of variant names | |
57 | #define VARIANT_LIST_INIT_SIZE 11 | |
58 | #define VARIANT_LIST_MAX_SIZE 31 | |
59 | ||
60 | // initial estimate for availableIDs count (default estimate is 8 => multiple reallocs) | |
61 | #define AVAILABLE_IDS_INIT_SIZE 493 | |
62 | ||
63 | // initial estimate for number of targets for source "Any", "Lat" | |
64 | #define ANY_TARGETS_INIT_SIZE 102 | |
65 | #define LAT_TARGETS_INIT_SIZE 23 | |
66 | ||
b75a7d8f A |
67 | /** |
68 | * Resource bundle key for the RuleBasedTransliterator rule. | |
69 | */ | |
73c04bcf | 70 | //static const char RB_RULE[] = "Rule"; |
b75a7d8f A |
71 | |
72 | U_NAMESPACE_BEGIN | |
73 | ||
74 | //------------------------------------------------------------------ | |
75 | // Alias | |
76 | //------------------------------------------------------------------ | |
77 | ||
73c04bcf A |
78 | TransliteratorAlias::TransliteratorAlias(const UnicodeString& theAliasID, |
79 | const UnicodeSet* cpdFilter) : | |
b75a7d8f | 80 | ID(), |
73c04bcf A |
81 | aliasesOrRules(theAliasID), |
82 | transes(0), | |
83 | compoundFilter(cpdFilter), | |
84 | direction(UTRANS_FORWARD), | |
374ca955 | 85 | type(TransliteratorAlias::SIMPLE) { |
b75a7d8f A |
86 | } |
87 | ||
88 | TransliteratorAlias::TransliteratorAlias(const UnicodeString& theID, | |
73c04bcf A |
89 | const UnicodeString& idBlocks, |
90 | UVector* adoptedTransliterators, | |
b75a7d8f A |
91 | const UnicodeSet* cpdFilter) : |
92 | ID(theID), | |
73c04bcf A |
93 | aliasesOrRules(idBlocks), |
94 | transes(adoptedTransliterators), | |
b75a7d8f | 95 | compoundFilter(cpdFilter), |
73c04bcf | 96 | direction(UTRANS_FORWARD), |
374ca955 A |
97 | type(TransliteratorAlias::COMPOUND) { |
98 | } | |
99 | ||
100 | TransliteratorAlias::TransliteratorAlias(const UnicodeString& theID, | |
101 | const UnicodeString& rules, | |
102 | UTransDirection dir) : | |
103 | ID(theID), | |
73c04bcf A |
104 | aliasesOrRules(rules), |
105 | transes(0), | |
374ca955 | 106 | compoundFilter(0), |
73c04bcf | 107 | direction(dir), |
374ca955 | 108 | type(TransliteratorAlias::RULES) { |
b75a7d8f A |
109 | } |
110 | ||
111 | TransliteratorAlias::~TransliteratorAlias() { | |
73c04bcf | 112 | delete transes; |
b75a7d8f A |
113 | } |
114 | ||
115 | ||
116 | Transliterator* TransliteratorAlias::create(UParseError& pe, | |
117 | UErrorCode& ec) { | |
374ca955 A |
118 | if (U_FAILURE(ec)) { |
119 | return 0; | |
120 | } | |
121 | Transliterator *t = NULL; | |
122 | switch (type) { | |
123 | case SIMPLE: | |
73c04bcf A |
124 | t = Transliterator::createInstance(aliasesOrRules, UTRANS_FORWARD, pe, ec); |
125 | if(U_FAILURE(ec)){ | |
b75a7d8f A |
126 | return 0; |
127 | } | |
73c04bcf A |
128 | if (compoundFilter != 0) |
129 | t->adoptFilter((UnicodeSet*)compoundFilter->clone()); | |
130 | break; | |
131 | case COMPOUND: | |
132 | { | |
133 | // the total number of transliterators in the compound is the total number of anonymous transliterators | |
134 | // plus the total number of ID blocks-- we start by assuming the list begins and ends with an ID | |
135 | // block and that each pair anonymous transliterators has an ID block between them. Then we go back | |
136 | // to see whether there really are ID blocks at the beginning and end (by looking for U+FFFF, which | |
137 | // marks the position where an anonymous transliterator goes) and adjust accordingly | |
138 | int32_t anonymousRBTs = transes->size(); | |
139 | int32_t transCount = anonymousRBTs * 2 + 1; | |
140 | if (!aliasesOrRules.isEmpty() && aliasesOrRules[0] == (UChar)(0xffff)) | |
141 | --transCount; | |
142 | if (aliasesOrRules.length() >= 2 && aliasesOrRules[aliasesOrRules.length() - 1] == (UChar)(0xffff)) | |
143 | --transCount; | |
144 | UnicodeString noIDBlock((UChar)(0xffff)); | |
145 | noIDBlock += ((UChar)(0xffff)); | |
146 | int32_t pos = aliasesOrRules.indexOf(noIDBlock); | |
147 | while (pos >= 0) { | |
148 | --transCount; | |
149 | pos = aliasesOrRules.indexOf(noIDBlock, pos + 1); | |
150 | } | |
151 | ||
152 | UVector transliterators(ec); | |
153 | UnicodeString idBlock; | |
154 | int32_t blockSeparatorPos = aliasesOrRules.indexOf((UChar)(0xffff)); | |
155 | while (blockSeparatorPos >= 0) { | |
156 | aliasesOrRules.extract(0, blockSeparatorPos, idBlock); | |
157 | aliasesOrRules.remove(0, blockSeparatorPos + 1); | |
158 | if (!idBlock.isEmpty()) | |
159 | transliterators.addElement(Transliterator::createInstance(idBlock, UTRANS_FORWARD, pe, ec), ec); | |
160 | if (!transes->isEmpty()) | |
161 | transliterators.addElement(transes->orphanElementAt(0), ec); | |
162 | blockSeparatorPos = aliasesOrRules.indexOf((UChar)(0xffff)); | |
163 | } | |
164 | if (!aliasesOrRules.isEmpty()) | |
165 | transliterators.addElement(Transliterator::createInstance(aliasesOrRules, UTRANS_FORWARD, pe, ec), ec); | |
166 | while (!transes->isEmpty()) | |
167 | transliterators.addElement(transes->orphanElementAt(0), ec); | |
168 | ||
169 | if (U_SUCCESS(ec)) { | |
170 | t = new CompoundTransliterator(ID, transliterators, | |
171 | (compoundFilter ? (UnicodeSet*)(compoundFilter->clone()) : 0), | |
172 | anonymousRBTs, pe, ec); | |
173 | if (t == 0) { | |
174 | ec = U_MEMORY_ALLOCATION_ERROR; | |
175 | return 0; | |
176 | } | |
177 | } else { | |
178 | for (int32_t i = 0; i < transliterators.size(); i++) | |
179 | delete (Transliterator*)(transliterators.elementAt(i)); | |
180 | } | |
b75a7d8f | 181 | } |
374ca955 A |
182 | break; |
183 | case RULES: | |
184 | U_ASSERT(FALSE); // don't call create() if isRuleBased() returns TRUE! | |
185 | break; | |
b75a7d8f A |
186 | } |
187 | return t; | |
188 | } | |
189 | ||
374ca955 A |
190 | UBool TransliteratorAlias::isRuleBased() const { |
191 | return type == RULES; | |
192 | } | |
193 | ||
194 | void TransliteratorAlias::parse(TransliteratorParser& parser, | |
195 | UParseError& pe, UErrorCode& ec) const { | |
196 | U_ASSERT(type == RULES); | |
197 | if (U_FAILURE(ec)) { | |
198 | return; | |
199 | } | |
200 | ||
73c04bcf | 201 | parser.parse(aliasesOrRules, direction, pe, ec); |
374ca955 A |
202 | } |
203 | ||
b75a7d8f | 204 | //---------------------------------------------------------------------- |
729e4ab9 | 205 | // class TransliteratorSpec |
b75a7d8f A |
206 | //---------------------------------------------------------------------- |
207 | ||
208 | /** | |
729e4ab9 | 209 | * A TransliteratorSpec is a string specifying either a source or a target. In more |
b75a7d8f A |
210 | * general terms, it may also specify a variant, but we only use the |
211 | * Spec class for sources and targets. | |
212 | * | |
213 | * A Spec may be a locale or a script. If it is a locale, it has a | |
214 | * fallback chain that goes xx_YY_ZZZ -> xx_YY -> xx -> ssss, where | |
215 | * ssss is the script mapping of xx_YY_ZZZ. The Spec API methods | |
216 | * hasFallback(), next(), and reset() iterate over this fallback | |
217 | * sequence. | |
218 | * | |
219 | * The Spec class canonicalizes itself, so the locale is put into | |
220 | * canonical form, or the script is transformed from an abbreviation | |
221 | * to a full name. | |
222 | */ | |
729e4ab9 | 223 | class TransliteratorSpec : public UMemory { |
b75a7d8f | 224 | public: |
729e4ab9 A |
225 | TransliteratorSpec(const UnicodeString& spec); |
226 | ~TransliteratorSpec(); | |
b75a7d8f A |
227 | |
228 | const UnicodeString& get() const; | |
229 | UBool hasFallback() const; | |
230 | const UnicodeString& next(); | |
231 | void reset(); | |
232 | ||
233 | UBool isLocale() const; | |
234 | ResourceBundle& getBundle() const; | |
235 | ||
236 | operator const UnicodeString&() const { return get(); } | |
237 | const UnicodeString& getTop() const { return top; } | |
238 | ||
239 | private: | |
240 | void setupNext(); | |
241 | ||
242 | UnicodeString top; | |
243 | UnicodeString spec; | |
244 | UnicodeString nextSpec; | |
245 | UnicodeString scriptName; | |
246 | UBool isSpecLocale; // TRUE if spec is a locale | |
247 | UBool isNextLocale; // TRUE if nextSpec is a locale | |
248 | ResourceBundle* res; | |
249 | ||
729e4ab9 A |
250 | TransliteratorSpec(const TransliteratorSpec &other); // forbid copying of this class |
251 | TransliteratorSpec &operator=(const TransliteratorSpec &other); // forbid copying of this class | |
b75a7d8f A |
252 | }; |
253 | ||
729e4ab9 | 254 | TransliteratorSpec::TransliteratorSpec(const UnicodeString& theSpec) |
73c04bcf A |
255 | : top(theSpec), |
256 | res(0) | |
257 | { | |
b75a7d8f | 258 | UErrorCode status = U_ZERO_ERROR; |
73c04bcf A |
259 | Locale topLoc(""); |
260 | LocaleUtility::initLocaleFromName(theSpec, topLoc); | |
261 | if (!topLoc.isBogus()) { | |
262 | res = new ResourceBundle(U_ICUDATA_TRANSLIT, topLoc, status); | |
263 | /* test for NULL */ | |
264 | if (res == 0) { | |
265 | return; | |
266 | } | |
267 | if (U_FAILURE(status) || status == U_USING_DEFAULT_WARNING) { | |
268 | delete res; | |
269 | res = 0; | |
270 | } | |
b75a7d8f A |
271 | } |
272 | ||
273 | // Canonicalize script name -or- do locale->script mapping | |
274 | status = U_ZERO_ERROR; | |
73c04bcf | 275 | static const int32_t capacity = 10; |
b75a7d8f | 276 | UScriptCode script[capacity]={USCRIPT_INVALID_CODE}; |
729e4ab9 A |
277 | int32_t num = uscript_getCode(CharString().appendInvariantChars(theSpec, status).data(), |
278 | script, capacity, &status); | |
b75a7d8f | 279 | if (num > 0 && script[0] != USCRIPT_INVALID_CODE) { |
73c04bcf | 280 | scriptName = UnicodeString(uscript_getName(script[0]), -1, US_INV); |
b75a7d8f A |
281 | } |
282 | ||
283 | // Canonicalize top | |
b75a7d8f A |
284 | if (res != 0) { |
285 | // Canonicalize locale name | |
73c04bcf A |
286 | UnicodeString locStr; |
287 | LocaleUtility::initNameFromLocale(topLoc, locStr); | |
288 | if (!locStr.isBogus()) { | |
289 | top = locStr; | |
b75a7d8f A |
290 | } |
291 | } else if (scriptName.length() != 0) { | |
292 | // We are a script; use canonical name | |
293 | top = scriptName; | |
294 | } | |
295 | ||
296 | // assert(spec != top); | |
297 | reset(); | |
298 | } | |
299 | ||
729e4ab9 | 300 | TransliteratorSpec::~TransliteratorSpec() { |
b75a7d8f A |
301 | delete res; |
302 | } | |
303 | ||
729e4ab9 | 304 | UBool TransliteratorSpec::hasFallback() const { |
b75a7d8f A |
305 | return nextSpec.length() != 0; |
306 | } | |
307 | ||
729e4ab9 | 308 | void TransliteratorSpec::reset() { |
b75a7d8f A |
309 | if (spec != top) { |
310 | spec = top; | |
311 | isSpecLocale = (res != 0); | |
312 | setupNext(); | |
313 | } | |
314 | } | |
315 | ||
729e4ab9 | 316 | void TransliteratorSpec::setupNext() { |
b75a7d8f A |
317 | isNextLocale = FALSE; |
318 | if (isSpecLocale) { | |
319 | nextSpec = spec; | |
320 | int32_t i = nextSpec.lastIndexOf(LOCALE_SEP); | |
321 | // If i == 0 then we have _FOO, so we fall through | |
322 | // to the scriptName. | |
323 | if (i > 0) { | |
324 | nextSpec.truncate(i); | |
325 | isNextLocale = TRUE; | |
326 | } else { | |
327 | nextSpec = scriptName; // scriptName may be empty | |
328 | } | |
329 | } else { | |
330 | // spec is a script, so we are at the end | |
331 | nextSpec.truncate(0); | |
332 | } | |
333 | } | |
334 | ||
335 | // Protocol: | |
336 | // for(const UnicodeString& s(spec.get()); | |
337 | // spec.hasFallback(); s(spec.next())) { ... | |
338 | ||
729e4ab9 | 339 | const UnicodeString& TransliteratorSpec::next() { |
b75a7d8f A |
340 | spec = nextSpec; |
341 | isSpecLocale = isNextLocale; | |
342 | setupNext(); | |
343 | return spec; | |
344 | } | |
345 | ||
729e4ab9 | 346 | const UnicodeString& TransliteratorSpec::get() const { |
b75a7d8f A |
347 | return spec; |
348 | } | |
349 | ||
729e4ab9 | 350 | UBool TransliteratorSpec::isLocale() const { |
b75a7d8f A |
351 | return isSpecLocale; |
352 | } | |
353 | ||
729e4ab9 | 354 | ResourceBundle& TransliteratorSpec::getBundle() const { |
b75a7d8f A |
355 | return *res; |
356 | } | |
357 | ||
358 | //---------------------------------------------------------------------- | |
359 | ||
360 | #ifdef DEBUG_MEM | |
361 | ||
362 | // Vector of Entry pointers currently in use | |
363 | static UVector* DEBUG_entries = NULL; | |
364 | ||
365 | static void DEBUG_setup() { | |
366 | if (DEBUG_entries == NULL) { | |
367 | UErrorCode ec = U_ZERO_ERROR; | |
368 | DEBUG_entries = new UVector(ec); | |
369 | } | |
370 | } | |
371 | ||
372 | // Caller must call DEBUG_setup first. Return index of given Entry, | |
373 | // if it is in use (not deleted yet), or -1 if not found. | |
729e4ab9 | 374 | static int DEBUG_findEntry(TransliteratorEntry* e) { |
b75a7d8f | 375 | for (int i=0; i<DEBUG_entries->size(); ++i) { |
729e4ab9 | 376 | if (e == (TransliteratorEntry*) DEBUG_entries->elementAt(i)) { |
b75a7d8f A |
377 | return i; |
378 | } | |
379 | } | |
380 | return -1; | |
381 | } | |
382 | ||
383 | // Track object creation | |
729e4ab9 | 384 | static void DEBUG_newEntry(TransliteratorEntry* e) { |
b75a7d8f A |
385 | DEBUG_setup(); |
386 | if (DEBUG_findEntry(e) >= 0) { | |
387 | // This should really never happen unless the heap is broken | |
388 | printf("ERROR DEBUG_newEntry duplicate new pointer %08X\n", e); | |
389 | return; | |
390 | } | |
391 | UErrorCode ec = U_ZERO_ERROR; | |
392 | DEBUG_entries->addElement(e, ec); | |
393 | } | |
394 | ||
395 | // Track object deletion | |
729e4ab9 | 396 | static void DEBUG_delEntry(TransliteratorEntry* e) { |
b75a7d8f A |
397 | DEBUG_setup(); |
398 | int i = DEBUG_findEntry(e); | |
399 | if (i < 0) { | |
400 | printf("ERROR DEBUG_delEntry possible double deletion %08X\n", e); | |
401 | return; | |
402 | } | |
403 | DEBUG_entries->removeElementAt(i); | |
404 | } | |
405 | ||
406 | // Track object usage | |
729e4ab9 | 407 | static void DEBUG_useEntry(TransliteratorEntry* e) { |
b75a7d8f A |
408 | if (e == NULL) return; |
409 | DEBUG_setup(); | |
410 | int i = DEBUG_findEntry(e); | |
411 | if (i < 0) { | |
412 | printf("ERROR DEBUG_useEntry possible dangling pointer %08X\n", e); | |
413 | } | |
414 | } | |
415 | ||
416 | #else | |
417 | // If we're not debugging then make these macros into NOPs | |
418 | #define DEBUG_newEntry(x) | |
419 | #define DEBUG_delEntry(x) | |
420 | #define DEBUG_useEntry(x) | |
421 | #endif | |
422 | ||
423 | //---------------------------------------------------------------------- | |
424 | // class Entry | |
425 | //---------------------------------------------------------------------- | |
426 | ||
427 | /** | |
428 | * The Entry object stores objects of different types and | |
429 | * singleton objects as placeholders for rule-based transliterators to | |
430 | * be built as needed. Instances of this struct can be placeholders, | |
431 | * can represent prototype transliterators to be cloned, or can | |
432 | * represent TransliteratorData objects. We don't support storing | |
433 | * classes in the registry because we don't have the rtti infrastructure | |
434 | * for it. We could easily add this if there is a need for it in the | |
435 | * future. | |
436 | */ | |
729e4ab9 | 437 | class TransliteratorEntry : public UMemory { |
b75a7d8f A |
438 | public: |
439 | enum Type { | |
440 | RULES_FORWARD, | |
441 | RULES_REVERSE, | |
442 | LOCALE_RULES, | |
443 | PROTOTYPE, | |
444 | RBT_DATA, | |
445 | COMPOUND_RBT, | |
446 | ALIAS, | |
447 | FACTORY, | |
448 | NONE // Only used for uninitialized entries | |
449 | } entryType; | |
450 | // NOTE: stringArg cannot go inside the union because | |
451 | // it has a copy constructor | |
452 | UnicodeString stringArg; // For RULES_*, ALIAS, COMPOUND_RBT | |
453 | int32_t intArg; // For COMPOUND_RBT, LOCALE_RULES | |
454 | UnicodeSet* compoundFilter; // For COMPOUND_RBT | |
455 | union { | |
456 | Transliterator* prototype; // For PROTOTYPE | |
73c04bcf A |
457 | TransliterationRuleData* data; // For RBT_DATA |
458 | UVector* dataVector; // For COMPOUND_RBT | |
b75a7d8f A |
459 | struct { |
460 | Transliterator::Factory function; | |
461 | Transliterator::Token context; | |
462 | } factory; // For FACTORY | |
463 | } u; | |
729e4ab9 A |
464 | TransliteratorEntry(); |
465 | ~TransliteratorEntry(); | |
b75a7d8f A |
466 | void adoptPrototype(Transliterator* adopted); |
467 | void setFactory(Transliterator::Factory factory, | |
468 | Transliterator::Token context); | |
469 | ||
470 | private: | |
471 | ||
729e4ab9 A |
472 | TransliteratorEntry(const TransliteratorEntry &other); // forbid copying of this class |
473 | TransliteratorEntry &operator=(const TransliteratorEntry &other); // forbid copying of this class | |
b75a7d8f A |
474 | }; |
475 | ||
729e4ab9 | 476 | TransliteratorEntry::TransliteratorEntry() { |
b75a7d8f A |
477 | u.prototype = 0; |
478 | compoundFilter = NULL; | |
479 | entryType = NONE; | |
480 | DEBUG_newEntry(this); | |
481 | } | |
482 | ||
729e4ab9 | 483 | TransliteratorEntry::~TransliteratorEntry() { |
b75a7d8f A |
484 | DEBUG_delEntry(this); |
485 | if (entryType == PROTOTYPE) { | |
486 | delete u.prototype; | |
73c04bcf | 487 | } else if (entryType == RBT_DATA) { |
b75a7d8f A |
488 | // The data object is shared between instances of RBT. The |
489 | // entry object owns it. It should only be deleted when the | |
490 | // transliterator component is being cleaned up. Doing so | |
491 | // invalidates any RBTs that the user has instantiated. | |
492 | delete u.data; | |
73c04bcf A |
493 | } else if (entryType == COMPOUND_RBT) { |
494 | while (u.dataVector != NULL && !u.dataVector->isEmpty()) | |
495 | delete (TransliterationRuleData*)u.dataVector->orphanElementAt(0); | |
496 | delete u.dataVector; | |
b75a7d8f A |
497 | } |
498 | delete compoundFilter; | |
499 | } | |
500 | ||
729e4ab9 | 501 | void TransliteratorEntry::adoptPrototype(Transliterator* adopted) { |
b75a7d8f A |
502 | if (entryType == PROTOTYPE) { |
503 | delete u.prototype; | |
504 | } | |
505 | entryType = PROTOTYPE; | |
506 | u.prototype = adopted; | |
507 | } | |
508 | ||
729e4ab9 | 509 | void TransliteratorEntry::setFactory(Transliterator::Factory factory, |
b75a7d8f A |
510 | Transliterator::Token context) { |
511 | if (entryType == PROTOTYPE) { | |
512 | delete u.prototype; | |
513 | } | |
514 | entryType = FACTORY; | |
515 | u.factory.function = factory; | |
516 | u.factory.context = context; | |
517 | } | |
518 | ||
519 | // UObjectDeleter for Hashtable::setValueDeleter | |
520 | U_CDECL_BEGIN | |
73c04bcf | 521 | static void U_CALLCONV |
b75a7d8f | 522 | deleteEntry(void* obj) { |
729e4ab9 | 523 | delete (TransliteratorEntry*) obj; |
b75a7d8f A |
524 | } |
525 | U_CDECL_END | |
526 | ||
527 | //---------------------------------------------------------------------- | |
528 | // class TransliteratorRegistry: Basic public API | |
529 | //---------------------------------------------------------------------- | |
530 | ||
531 | TransliteratorRegistry::TransliteratorRegistry(UErrorCode& status) : | |
374ca955 | 532 | registry(TRUE, status), |
2ca993e8 A |
533 | specDAG(TRUE, SPECDAG_INIT_SIZE, status), |
534 | variantList(VARIANT_LIST_INIT_SIZE, status), | |
535 | availableIDs(AVAILABLE_IDS_INIT_SIZE, status) | |
b75a7d8f A |
536 | { |
537 | registry.setValueDeleter(deleteEntry); | |
2ca993e8 A |
538 | variantList.setDeleter(uprv_deleteUObject); |
539 | variantList.setComparer(uhash_compareCaselessUnicodeString); | |
540 | UnicodeString *emptyString = new UnicodeString(); | |
541 | if (emptyString != NULL) { | |
542 | variantList.addElement(emptyString, status); | |
543 | } | |
4388f060 | 544 | availableIDs.setDeleter(uprv_deleteUObject); |
b75a7d8f A |
545 | availableIDs.setComparer(uhash_compareCaselessUnicodeString); |
546 | specDAG.setValueDeleter(uhash_deleteHashtable); | |
547 | } | |
548 | ||
549 | TransliteratorRegistry::~TransliteratorRegistry() { | |
550 | // Through the magic of C++, everything cleans itself up | |
551 | } | |
552 | ||
553 | Transliterator* TransliteratorRegistry::get(const UnicodeString& ID, | |
554 | TransliteratorAlias*& aliasReturn, | |
b75a7d8f | 555 | UErrorCode& status) { |
374ca955 | 556 | U_ASSERT(aliasReturn == NULL); |
729e4ab9 | 557 | TransliteratorEntry *entry = find(ID); |
b75a7d8f | 558 | return (entry == 0) ? 0 |
374ca955 A |
559 | : instantiateEntry(ID, entry, aliasReturn, status); |
560 | } | |
561 | ||
562 | Transliterator* TransliteratorRegistry::reget(const UnicodeString& ID, | |
563 | TransliteratorParser& parser, | |
564 | TransliteratorAlias*& aliasReturn, | |
565 | UErrorCode& status) { | |
566 | U_ASSERT(aliasReturn == NULL); | |
729e4ab9 | 567 | TransliteratorEntry *entry = find(ID); |
374ca955 A |
568 | |
569 | if (entry == 0) { | |
570 | // We get to this point if there are two threads, one of which | |
571 | // is instantiating an ID, and another of which is removing | |
572 | // the same ID from the registry, and the timing is just right. | |
573 | return 0; | |
574 | } | |
575 | ||
576 | // The usage model for the caller is that they will first call | |
577 | // reg->get() inside the mutex, they'll get back an alias, they call | |
578 | // alias->isRuleBased(), and if they get TRUE, they call alias->parse() | |
579 | // outside the mutex, then reg->reget() inside the mutex again. A real | |
580 | // mess, but it gets things working for ICU 3.0. [alan]. | |
581 | ||
582 | // Note: It's possible that in between the caller calling | |
583 | // alias->parse() and reg->reget(), that another thread will have | |
584 | // called reg->reget(), and the entry will already have been fixed up. | |
585 | // We have to detect this so we don't stomp over existing entry | |
586 | // data members and potentially leak memory (u.data and compoundFilter). | |
587 | ||
729e4ab9 A |
588 | if (entry->entryType == TransliteratorEntry::RULES_FORWARD || |
589 | entry->entryType == TransliteratorEntry::RULES_REVERSE || | |
590 | entry->entryType == TransliteratorEntry::LOCALE_RULES) { | |
374ca955 | 591 | |
73c04bcf A |
592 | if (parser.idBlockVector.isEmpty() && parser.dataVector.isEmpty()) { |
593 | entry->u.data = 0; | |
729e4ab9 | 594 | entry->entryType = TransliteratorEntry::ALIAS; |
73c04bcf A |
595 | entry->stringArg = UNICODE_STRING_SIMPLE("Any-NULL"); |
596 | } | |
597 | else if (parser.idBlockVector.isEmpty() && parser.dataVector.size() == 1) { | |
598 | entry->u.data = (TransliterationRuleData*)parser.dataVector.orphanElementAt(0); | |
729e4ab9 | 599 | entry->entryType = TransliteratorEntry::RBT_DATA; |
73c04bcf A |
600 | } |
601 | else if (parser.idBlockVector.size() == 1 && parser.dataVector.isEmpty()) { | |
602 | entry->stringArg = *(UnicodeString*)(parser.idBlockVector.elementAt(0)); | |
603 | entry->compoundFilter = parser.orphanCompoundFilter(); | |
729e4ab9 | 604 | entry->entryType = TransliteratorEntry::ALIAS; |
73c04bcf A |
605 | } |
606 | else { | |
729e4ab9 | 607 | entry->entryType = TransliteratorEntry::COMPOUND_RBT; |
73c04bcf A |
608 | entry->compoundFilter = parser.orphanCompoundFilter(); |
609 | entry->u.dataVector = new UVector(status); | |
610 | entry->stringArg.remove(); | |
611 | ||
612 | int32_t limit = parser.idBlockVector.size(); | |
613 | if (parser.dataVector.size() > limit) | |
614 | limit = parser.dataVector.size(); | |
615 | ||
616 | for (int32_t i = 0; i < limit; i++) { | |
617 | if (i < parser.idBlockVector.size()) { | |
618 | UnicodeString* idBlock = (UnicodeString*)parser.idBlockVector.elementAt(i); | |
619 | if (!idBlock->isEmpty()) | |
620 | entry->stringArg += *idBlock; | |
621 | } | |
622 | if (!parser.dataVector.isEmpty()) { | |
623 | TransliterationRuleData* data = (TransliterationRuleData*)parser.dataVector.orphanElementAt(0); | |
624 | entry->u.dataVector->addElement(data, status); | |
625 | entry->stringArg += (UChar)0xffff; // use U+FFFF to mark position of RBTs in ID block | |
626 | } | |
374ca955 A |
627 | } |
628 | } | |
629 | } | |
630 | ||
631 | Transliterator *t = | |
632 | instantiateEntry(ID, entry, aliasReturn, status); | |
633 | return t; | |
b75a7d8f A |
634 | } |
635 | ||
636 | void TransliteratorRegistry::put(Transliterator* adoptedProto, | |
46f4442e A |
637 | UBool visible, |
638 | UErrorCode& ec) | |
639 | { | |
729e4ab9 | 640 | TransliteratorEntry *entry = new TransliteratorEntry(); |
46f4442e A |
641 | if (entry == NULL) { |
642 | ec = U_MEMORY_ALLOCATION_ERROR; | |
643 | return; | |
644 | } | |
b75a7d8f A |
645 | entry->adoptPrototype(adoptedProto); |
646 | registerEntry(adoptedProto->getID(), entry, visible); | |
647 | } | |
648 | ||
649 | void TransliteratorRegistry::put(const UnicodeString& ID, | |
650 | Transliterator::Factory factory, | |
651 | Transliterator::Token context, | |
46f4442e A |
652 | UBool visible, |
653 | UErrorCode& ec) { | |
729e4ab9 | 654 | TransliteratorEntry *entry = new TransliteratorEntry(); |
46f4442e A |
655 | if (entry == NULL) { |
656 | ec = U_MEMORY_ALLOCATION_ERROR; | |
657 | return; | |
658 | } | |
b75a7d8f A |
659 | entry->setFactory(factory, context); |
660 | registerEntry(ID, entry, visible); | |
661 | } | |
662 | ||
663 | void TransliteratorRegistry::put(const UnicodeString& ID, | |
664 | const UnicodeString& resourceName, | |
665 | UTransDirection dir, | |
73c04bcf | 666 | UBool readonlyResourceAlias, |
46f4442e A |
667 | UBool visible, |
668 | UErrorCode& ec) { | |
729e4ab9 | 669 | TransliteratorEntry *entry = new TransliteratorEntry(); |
46f4442e A |
670 | if (entry == NULL) { |
671 | ec = U_MEMORY_ALLOCATION_ERROR; | |
672 | return; | |
673 | } | |
729e4ab9 A |
674 | entry->entryType = (dir == UTRANS_FORWARD) ? TransliteratorEntry::RULES_FORWARD |
675 | : TransliteratorEntry::RULES_REVERSE; | |
73c04bcf A |
676 | if (readonlyResourceAlias) { |
677 | entry->stringArg.setTo(TRUE, resourceName.getBuffer(), -1); | |
678 | } | |
679 | else { | |
680 | entry->stringArg = resourceName; | |
681 | } | |
b75a7d8f A |
682 | registerEntry(ID, entry, visible); |
683 | } | |
684 | ||
685 | void TransliteratorRegistry::put(const UnicodeString& ID, | |
686 | const UnicodeString& alias, | |
73c04bcf | 687 | UBool readonlyAliasAlias, |
46f4442e A |
688 | UBool visible, |
689 | UErrorCode& /*ec*/) { | |
729e4ab9 | 690 | TransliteratorEntry *entry = new TransliteratorEntry(); |
46f4442e A |
691 | // Null pointer check |
692 | if (entry != NULL) { | |
729e4ab9 | 693 | entry->entryType = TransliteratorEntry::ALIAS; |
46f4442e A |
694 | if (readonlyAliasAlias) { |
695 | entry->stringArg.setTo(TRUE, alias.getBuffer(), -1); | |
696 | } | |
697 | else { | |
698 | entry->stringArg = alias; | |
699 | } | |
700 | registerEntry(ID, entry, visible); | |
73c04bcf | 701 | } |
b75a7d8f A |
702 | } |
703 | ||
704 | void TransliteratorRegistry::remove(const UnicodeString& ID) { | |
705 | UnicodeString source, target, variant; | |
706 | UBool sawSource; | |
707 | TransliteratorIDParser::IDtoSTV(ID, source, target, variant, sawSource); | |
708 | // Only need to do this if ID.indexOf('-') < 0 | |
709 | UnicodeString id; | |
710 | TransliteratorIDParser::STVtoID(source, target, variant, id); | |
711 | registry.remove(id); | |
712 | removeSTV(source, target, variant); | |
713 | availableIDs.removeElement((void*) &id); | |
714 | } | |
715 | ||
716 | //---------------------------------------------------------------------- | |
717 | // class TransliteratorRegistry: Public ID and spec management | |
718 | //---------------------------------------------------------------------- | |
719 | ||
720 | /** | |
374ca955 | 721 | * == OBSOLETE - remove in ICU 3.4 == |
b75a7d8f A |
722 | * Return the number of IDs currently registered with the system. |
723 | * To retrieve the actual IDs, call getAvailableID(i) with | |
724 | * i from 0 to countAvailableIDs() - 1. | |
725 | */ | |
374ca955 | 726 | int32_t TransliteratorRegistry::countAvailableIDs(void) const { |
b75a7d8f A |
727 | return availableIDs.size(); |
728 | } | |
729 | ||
730 | /** | |
374ca955 | 731 | * == OBSOLETE - remove in ICU 3.4 == |
b75a7d8f A |
732 | * Return the index-th available ID. index must be between 0 |
733 | * and countAvailableIDs() - 1, inclusive. If index is out of | |
734 | * range, the result of getAvailableID(0) is returned. | |
735 | */ | |
374ca955 | 736 | const UnicodeString& TransliteratorRegistry::getAvailableID(int32_t index) const { |
b75a7d8f A |
737 | if (index < 0 || index >= availableIDs.size()) { |
738 | index = 0; | |
739 | } | |
740 | return *(const UnicodeString*) availableIDs[index]; | |
741 | } | |
742 | ||
374ca955 A |
743 | StringEnumeration* TransliteratorRegistry::getAvailableIDs() const { |
744 | return new Enumeration(*this); | |
745 | } | |
746 | ||
747 | int32_t TransliteratorRegistry::countAvailableSources(void) const { | |
b75a7d8f A |
748 | return specDAG.count(); |
749 | } | |
750 | ||
751 | UnicodeString& TransliteratorRegistry::getAvailableSource(int32_t index, | |
374ca955 | 752 | UnicodeString& result) const { |
b331163b | 753 | int32_t pos = UHASH_FIRST; |
b75a7d8f A |
754 | const UHashElement *e = 0; |
755 | while (index-- >= 0) { | |
756 | e = specDAG.nextElement(pos); | |
757 | if (e == 0) { | |
758 | break; | |
759 | } | |
760 | } | |
761 | if (e == 0) { | |
762 | result.truncate(0); | |
763 | } else { | |
764 | result = *(UnicodeString*) e->key.pointer; | |
765 | } | |
766 | return result; | |
767 | } | |
768 | ||
374ca955 | 769 | int32_t TransliteratorRegistry::countAvailableTargets(const UnicodeString& source) const { |
b75a7d8f A |
770 | Hashtable *targets = (Hashtable*) specDAG.get(source); |
771 | return (targets == 0) ? 0 : targets->count(); | |
772 | } | |
773 | ||
774 | UnicodeString& TransliteratorRegistry::getAvailableTarget(int32_t index, | |
775 | const UnicodeString& source, | |
374ca955 | 776 | UnicodeString& result) const { |
b75a7d8f A |
777 | Hashtable *targets = (Hashtable*) specDAG.get(source); |
778 | if (targets == 0) { | |
779 | result.truncate(0); // invalid source | |
780 | return result; | |
781 | } | |
b331163b | 782 | int32_t pos = UHASH_FIRST; |
b75a7d8f A |
783 | const UHashElement *e = 0; |
784 | while (index-- >= 0) { | |
785 | e = targets->nextElement(pos); | |
786 | if (e == 0) { | |
787 | break; | |
788 | } | |
789 | } | |
790 | if (e == 0) { | |
791 | result.truncate(0); // invalid index | |
792 | } else { | |
793 | result = *(UnicodeString*) e->key.pointer; | |
794 | } | |
795 | return result; | |
796 | } | |
797 | ||
798 | int32_t TransliteratorRegistry::countAvailableVariants(const UnicodeString& source, | |
374ca955 | 799 | const UnicodeString& target) const { |
b75a7d8f A |
800 | Hashtable *targets = (Hashtable*) specDAG.get(source); |
801 | if (targets == 0) { | |
802 | return 0; | |
803 | } | |
2ca993e8 A |
804 | int32_t varMask = targets->geti(target); |
805 | int32_t varCount = 0; | |
806 | while (varMask > 0) { | |
807 | if (varMask & 1) { | |
808 | varCount++; | |
809 | } | |
810 | varMask >>= 1; | |
811 | } | |
812 | return varCount; | |
b75a7d8f A |
813 | } |
814 | ||
815 | UnicodeString& TransliteratorRegistry::getAvailableVariant(int32_t index, | |
816 | const UnicodeString& source, | |
817 | const UnicodeString& target, | |
374ca955 | 818 | UnicodeString& result) const { |
b75a7d8f A |
819 | Hashtable *targets = (Hashtable*) specDAG.get(source); |
820 | if (targets == 0) { | |
821 | result.truncate(0); // invalid source | |
822 | return result; | |
823 | } | |
2ca993e8 A |
824 | int32_t varMask = targets->geti(target); |
825 | int32_t varCount = 0; | |
826 | int32_t varListIndex = 0; | |
827 | while (varMask > 0) { | |
828 | if (varMask & 1) { | |
829 | if (varCount == index) { | |
830 | UnicodeString *v = (UnicodeString*) variantList.elementAt(varListIndex); | |
831 | if (v != NULL) { | |
832 | result = *v; | |
833 | return result; | |
834 | } | |
835 | break; | |
836 | } | |
837 | varCount++; | |
838 | } | |
839 | varMask >>= 1; | |
840 | varListIndex++; | |
b75a7d8f | 841 | } |
2ca993e8 | 842 | result.truncate(0); // invalid target or index |
b75a7d8f A |
843 | return result; |
844 | } | |
845 | ||
374ca955 A |
846 | //---------------------------------------------------------------------- |
847 | // class TransliteratorRegistry::Enumeration | |
848 | //---------------------------------------------------------------------- | |
849 | ||
850 | TransliteratorRegistry::Enumeration::Enumeration(const TransliteratorRegistry& _reg) : | |
851 | index(0), reg(_reg) { | |
852 | } | |
853 | ||
854 | TransliteratorRegistry::Enumeration::~Enumeration() { | |
855 | } | |
856 | ||
857 | int32_t TransliteratorRegistry::Enumeration::count(UErrorCode& /*status*/) const { | |
858 | return reg.availableIDs.size(); | |
859 | } | |
860 | ||
861 | const UnicodeString* TransliteratorRegistry::Enumeration::snext(UErrorCode& status) { | |
862 | // This is sloppy but safe -- if we get out of sync with the underlying | |
863 | // registry, we will still return legal strings, but they might not | |
864 | // correspond to the snapshot at construction time. So there could be | |
865 | // duplicate IDs or omitted IDs if insertions or deletions occur in one | |
866 | // thread while another is iterating. To be more rigorous, add a timestamp, | |
867 | // which is incremented with any modification, and validate this iterator | |
868 | // against the timestamp at construction time. This probably isn't worth | |
869 | // doing as long as there is some possibility of removing this code in favor | |
870 | // of some new code based on Doug's service framework. | |
871 | if (U_FAILURE(status)) { | |
872 | return NULL; | |
873 | } | |
874 | int32_t n = reg.availableIDs.size(); | |
875 | if (index > n) { | |
876 | status = U_ENUM_OUT_OF_SYNC_ERROR; | |
877 | } | |
878 | // index == n is okay -- this means we've reached the end | |
879 | if (index < n) { | |
880 | // Copy the string! This avoids lifetime problems. | |
881 | unistr = *(const UnicodeString*)reg.availableIDs[index++]; | |
882 | return &unistr; | |
883 | } else { | |
884 | return NULL; | |
885 | } | |
886 | } | |
887 | ||
888 | void TransliteratorRegistry::Enumeration::reset(UErrorCode& /*status*/) { | |
889 | index = 0; | |
890 | } | |
891 | ||
892 | UOBJECT_DEFINE_RTTI_IMPLEMENTATION(TransliteratorRegistry::Enumeration) | |
893 | ||
b75a7d8f A |
894 | //---------------------------------------------------------------------- |
895 | // class TransliteratorRegistry: internal | |
896 | //---------------------------------------------------------------------- | |
897 | ||
898 | /** | |
899 | * Convenience method. Calls 6-arg registerEntry(). | |
900 | */ | |
901 | void TransliteratorRegistry::registerEntry(const UnicodeString& source, | |
902 | const UnicodeString& target, | |
903 | const UnicodeString& variant, | |
729e4ab9 | 904 | TransliteratorEntry* adopted, |
b75a7d8f A |
905 | UBool visible) { |
906 | UnicodeString ID; | |
907 | UnicodeString s(source); | |
908 | if (s.length() == 0) { | |
4388f060 | 909 | s.setTo(TRUE, ANY, 3); |
b75a7d8f A |
910 | } |
911 | TransliteratorIDParser::STVtoID(source, target, variant, ID); | |
912 | registerEntry(ID, s, target, variant, adopted, visible); | |
913 | } | |
914 | ||
915 | /** | |
916 | * Convenience method. Calls 6-arg registerEntry(). | |
917 | */ | |
918 | void TransliteratorRegistry::registerEntry(const UnicodeString& ID, | |
729e4ab9 | 919 | TransliteratorEntry* adopted, |
b75a7d8f A |
920 | UBool visible) { |
921 | UnicodeString source, target, variant; | |
922 | UBool sawSource; | |
923 | TransliteratorIDParser::IDtoSTV(ID, source, target, variant, sawSource); | |
924 | // Only need to do this if ID.indexOf('-') < 0 | |
925 | UnicodeString id; | |
926 | TransliteratorIDParser::STVtoID(source, target, variant, id); | |
927 | registerEntry(id, source, target, variant, adopted, visible); | |
928 | } | |
929 | ||
930 | /** | |
931 | * Register an entry object (adopted) with the given ID, source, | |
932 | * target, and variant strings. | |
933 | */ | |
934 | void TransliteratorRegistry::registerEntry(const UnicodeString& ID, | |
935 | const UnicodeString& source, | |
936 | const UnicodeString& target, | |
937 | const UnicodeString& variant, | |
729e4ab9 | 938 | TransliteratorEntry* adopted, |
b75a7d8f A |
939 | UBool visible) { |
940 | UErrorCode status = U_ZERO_ERROR; | |
941 | registry.put(ID, adopted, status); | |
942 | if (visible) { | |
943 | registerSTV(source, target, variant); | |
944 | if (!availableIDs.contains((void*) &ID)) { | |
374ca955 | 945 | UnicodeString *newID = (UnicodeString *)ID.clone(); |
46f4442e A |
946 | // Check to make sure newID was created. |
947 | if (newID != NULL) { | |
2ca993e8 A |
948 | // NUL-terminate the ID string |
949 | newID->getTerminatedBuffer(); | |
950 | availableIDs.addElement(newID, status); | |
46f4442e | 951 | } |
b75a7d8f A |
952 | } |
953 | } else { | |
954 | removeSTV(source, target, variant); | |
955 | availableIDs.removeElement((void*) &ID); | |
956 | } | |
957 | } | |
958 | ||
959 | /** | |
960 | * Register a source-target/variant in the specDAG. Variant may be | |
2ca993e8 | 961 | * empty, but source and target must not be. |
b75a7d8f A |
962 | */ |
963 | void TransliteratorRegistry::registerSTV(const UnicodeString& source, | |
964 | const UnicodeString& target, | |
965 | const UnicodeString& variant) { | |
966 | // assert(source.length() > 0); | |
967 | // assert(target.length() > 0); | |
968 | UErrorCode status = U_ZERO_ERROR; | |
969 | Hashtable *targets = (Hashtable*) specDAG.get(source); | |
970 | if (targets == 0) { | |
2ca993e8 A |
971 | int32_t size = 3; |
972 | if (source.compare(ANY,3) == 0) { | |
973 | size = ANY_TARGETS_INIT_SIZE; | |
974 | } else if (source.compare(LAT,3) == 0) { | |
975 | size = LAT_TARGETS_INIT_SIZE; | |
976 | } | |
977 | targets = new Hashtable(TRUE, size, status); | |
978 | if (U_FAILURE(status) || targets == NULL) { | |
b75a7d8f A |
979 | return; |
980 | } | |
b75a7d8f A |
981 | specDAG.put(source, targets, status); |
982 | } | |
2ca993e8 A |
983 | int32_t variantListIndex = variantList.indexOf((void*) &variant, 0); |
984 | if (variantListIndex < 0) { | |
985 | if (variantList.size() >= VARIANT_LIST_MAX_SIZE) { | |
986 | // can't handle any more variants | |
b75a7d8f A |
987 | return; |
988 | } | |
2ca993e8 A |
989 | UnicodeString *variantEntry = new UnicodeString(variant); |
990 | if (variantEntry != NULL) { | |
991 | variantList.addElement(variantEntry, status); | |
992 | if (U_SUCCESS(status)) { | |
993 | variantListIndex = variantList.size() - 1; | |
994 | } | |
995 | } | |
996 | if (variantListIndex < 0) { | |
997 | return; | |
b75a7d8f A |
998 | } |
999 | } | |
2ca993e8 A |
1000 | int32_t addMask = 1 << variantListIndex; |
1001 | int32_t varMask = targets->geti(target); | |
1002 | targets->puti(target, varMask | addMask, status); | |
b75a7d8f A |
1003 | } |
1004 | ||
1005 | /** | |
1006 | * Remove a source-target/variant from the specDAG. | |
1007 | */ | |
1008 | void TransliteratorRegistry::removeSTV(const UnicodeString& source, | |
1009 | const UnicodeString& target, | |
1010 | const UnicodeString& variant) { | |
1011 | // assert(source.length() > 0); | |
1012 | // assert(target.length() > 0); | |
2ca993e8 | 1013 | UErrorCode status = U_ZERO_ERROR; |
b75a7d8f | 1014 | Hashtable *targets = (Hashtable*) specDAG.get(source); |
2ca993e8 | 1015 | if (targets == NULL) { |
b75a7d8f A |
1016 | return; // should never happen for valid s-t/v |
1017 | } | |
2ca993e8 A |
1018 | int32_t varMask = targets->geti(target); |
1019 | if (varMask == 0) { | |
b75a7d8f A |
1020 | return; // should never happen for valid s-t/v |
1021 | } | |
2ca993e8 A |
1022 | int32_t variantListIndex = variantList.indexOf((void*) &variant, 0); |
1023 | if (variantListIndex < 0) { | |
1024 | return; // should never happen for valid s-t/v | |
1025 | } | |
1026 | int32_t remMask = 1 << variantListIndex; | |
1027 | varMask &= (~remMask); | |
1028 | if (varMask != 0) { | |
1029 | targets->puti(target, varMask, status); | |
1030 | } else { | |
b75a7d8f A |
1031 | targets->remove(target); // should delete variants |
1032 | if (targets->count() == 0) { | |
2ca993e8 | 1033 | specDAG.remove(source); // should delete targetss |
b75a7d8f A |
1034 | } |
1035 | } | |
1036 | } | |
1037 | ||
1038 | /** | |
1039 | * Attempt to find a source-target/variant in the dynamic registry | |
1040 | * store. Return 0 on failure. | |
1041 | * | |
1042 | * Caller does NOT own returned object. | |
1043 | */ | |
729e4ab9 A |
1044 | TransliteratorEntry* TransliteratorRegistry::findInDynamicStore(const TransliteratorSpec& src, |
1045 | const TransliteratorSpec& trg, | |
374ca955 | 1046 | const UnicodeString& variant) const { |
b75a7d8f A |
1047 | UnicodeString ID; |
1048 | TransliteratorIDParser::STVtoID(src, trg, variant, ID); | |
729e4ab9 | 1049 | TransliteratorEntry *e = (TransliteratorEntry*) registry.get(ID); |
b75a7d8f A |
1050 | DEBUG_useEntry(e); |
1051 | return e; | |
1052 | } | |
1053 | ||
1054 | /** | |
1055 | * Attempt to find a source-target/variant in the static locale | |
1056 | * resource store. Do not perform fallback. Return 0 on failure. | |
1057 | * | |
1058 | * On success, create a new entry object, register it in the dynamic | |
1059 | * store, and return a pointer to it, but do not make it public -- | |
1060 | * just because someone requested something, we do not expand the | |
1061 | * available ID list (or spec DAG). | |
1062 | * | |
1063 | * Caller does NOT own returned object. | |
1064 | */ | |
729e4ab9 A |
1065 | TransliteratorEntry* TransliteratorRegistry::findInStaticStore(const TransliteratorSpec& src, |
1066 | const TransliteratorSpec& trg, | |
b75a7d8f | 1067 | const UnicodeString& variant) { |
729e4ab9 | 1068 | TransliteratorEntry* entry = 0; |
b75a7d8f A |
1069 | if (src.isLocale()) { |
1070 | entry = findInBundle(src, trg, variant, UTRANS_FORWARD); | |
1071 | } else if (trg.isLocale()) { | |
1072 | entry = findInBundle(trg, src, variant, UTRANS_REVERSE); | |
1073 | } | |
1074 | ||
1075 | // If we found an entry, store it in the Hashtable for next | |
1076 | // time. | |
1077 | if (entry != 0) { | |
1078 | registerEntry(src.getTop(), trg.getTop(), variant, entry, FALSE); | |
1079 | } | |
1080 | ||
1081 | return entry; | |
1082 | } | |
1083 | ||
1084 | // As of 2.0, resource bundle keys cannot contain '_' | |
1085 | static const UChar TRANSLITERATE_TO[] = {84,114,97,110,115,108,105,116,101,114,97,116,101,84,111,0}; // "TransliterateTo" | |
1086 | ||
1087 | static const UChar TRANSLITERATE_FROM[] = {84,114,97,110,115,108,105,116,101,114,97,116,101,70,114,111,109,0}; // "TransliterateFrom" | |
1088 | ||
1089 | static const UChar TRANSLITERATE[] = {84,114,97,110,115,108,105,116,101,114,97,116,101,0}; // "Transliterate" | |
1090 | ||
1091 | /** | |
1092 | * Attempt to find an entry in a single resource bundle. This is | |
1093 | * a one-sided lookup. findInStaticStore() performs up to two such | |
1094 | * lookups, one for the source, and one for the target. | |
1095 | * | |
1096 | * Do not perform fallback. Return 0 on failure. | |
1097 | * | |
1098 | * On success, create a new Entry object, populate it, and return it. | |
1099 | * The caller owns the returned object. | |
1100 | */ | |
729e4ab9 A |
1101 | TransliteratorEntry* TransliteratorRegistry::findInBundle(const TransliteratorSpec& specToOpen, |
1102 | const TransliteratorSpec& specToFind, | |
b75a7d8f A |
1103 | const UnicodeString& variant, |
1104 | UTransDirection direction) | |
1105 | { | |
1106 | UnicodeString utag; | |
1107 | UnicodeString resStr; | |
1108 | int32_t pass; | |
1109 | ||
1110 | for (pass=0; pass<2; ++pass) { | |
1111 | utag.truncate(0); | |
1112 | // First try either TransliteratorTo_xxx or | |
1113 | // TransliterateFrom_xxx, then try the bidirectional | |
1114 | // Transliterate_xxx. This precedence order is arbitrary | |
1115 | // but must be consistent and documented. | |
1116 | if (pass == 0) { | |
1117 | utag.append(direction == UTRANS_FORWARD ? | |
4388f060 | 1118 | TRANSLITERATE_TO : TRANSLITERATE_FROM, -1); |
b75a7d8f | 1119 | } else { |
4388f060 | 1120 | utag.append(TRANSLITERATE, -1); |
b75a7d8f A |
1121 | } |
1122 | UnicodeString s(specToFind.get()); | |
73c04bcf | 1123 | utag.append(s.toUpper("")); |
b75a7d8f | 1124 | UErrorCode status = U_ZERO_ERROR; |
729e4ab9 A |
1125 | ResourceBundle subres(specToOpen.getBundle().get( |
1126 | CharString().appendInvariantChars(utag, status).data(), status)); | |
b75a7d8f A |
1127 | if (U_FAILURE(status) || status == U_USING_DEFAULT_WARNING) { |
1128 | continue; | |
1129 | } | |
729e4ab9 | 1130 | |
73c04bcf A |
1131 | s.truncate(0); |
1132 | if (specToOpen.get() != LocaleUtility::initNameFromLocale(subres.getLocale(), s)) { | |
b75a7d8f A |
1133 | continue; |
1134 | } | |
729e4ab9 | 1135 | |
b75a7d8f | 1136 | if (variant.length() != 0) { |
b75a7d8f | 1137 | status = U_ZERO_ERROR; |
729e4ab9 A |
1138 | resStr = subres.getStringEx( |
1139 | CharString().appendInvariantChars(variant, status).data(), status); | |
b75a7d8f A |
1140 | if (U_SUCCESS(status)) { |
1141 | // Exit loop successfully | |
1142 | break; | |
1143 | } | |
729e4ab9 | 1144 | } else { |
b75a7d8f A |
1145 | // Variant is empty, which means match the first variant listed. |
1146 | status = U_ZERO_ERROR; | |
1147 | resStr = subres.getStringEx(1, status); | |
1148 | if (U_SUCCESS(status)) { | |
1149 | // Exit loop successfully | |
1150 | break; | |
1151 | } | |
1152 | } | |
1153 | } | |
1154 | ||
1155 | if (pass==2) { | |
1156 | // Failed | |
1157 | return NULL; | |
1158 | } | |
1159 | ||
1160 | // We have succeeded in loading a string from the locale | |
1161 | // resources. Create a new registry entry to hold it and return it. | |
729e4ab9 | 1162 | TransliteratorEntry *entry = new TransliteratorEntry(); |
b75a7d8f A |
1163 | if (entry != 0) { |
1164 | // The direction is always forward for the | |
1165 | // TransliterateTo_xxx and TransliterateFrom_xxx | |
1166 | // items; those are unidirectional forward rules. | |
1167 | // For the bidirectional Transliterate_xxx items, | |
1168 | // the direction is the value passed in to this | |
1169 | // function. | |
1170 | int32_t dir = (pass == 0) ? UTRANS_FORWARD : direction; | |
729e4ab9 | 1171 | entry->entryType = TransliteratorEntry::LOCALE_RULES; |
b75a7d8f A |
1172 | entry->stringArg = resStr; |
1173 | entry->intArg = dir; | |
1174 | } | |
1175 | ||
1176 | return entry; | |
1177 | } | |
1178 | ||
1179 | /** | |
1180 | * Convenience method. Calls 3-arg find(). | |
1181 | */ | |
729e4ab9 | 1182 | TransliteratorEntry* TransliteratorRegistry::find(const UnicodeString& ID) { |
b75a7d8f A |
1183 | UnicodeString source, target, variant; |
1184 | UBool sawSource; | |
1185 | TransliteratorIDParser::IDtoSTV(ID, source, target, variant, sawSource); | |
1186 | return find(source, target, variant); | |
1187 | } | |
1188 | ||
1189 | /** | |
1190 | * Top-level find method. Attempt to find a source-target/variant in | |
1191 | * either the dynamic or the static (locale resource) store. Perform | |
1192 | * fallback. | |
1193 | * | |
1194 | * Lookup sequence for ss_SS_SSS-tt_TT_TTT/v: | |
1195 | * | |
1196 | * ss_SS_SSS-tt_TT_TTT/v -- in hashtable | |
1197 | * ss_SS_SSS-tt_TT_TTT/v -- in ss_SS_SSS (no fallback) | |
1198 | * | |
1199 | * repeat with t = tt_TT_TTT, tt_TT, tt, and tscript | |
1200 | * | |
1201 | * ss_SS_SSS-t/ * | |
1202 | * ss_SS-t/ * | |
1203 | * ss-t/ * | |
1204 | * sscript-t/ * | |
1205 | * | |
1206 | * Here * matches the first variant listed. | |
1207 | * | |
1208 | * Caller does NOT own returned object. Return 0 on failure. | |
1209 | */ | |
729e4ab9 | 1210 | TransliteratorEntry* TransliteratorRegistry::find(UnicodeString& source, |
b75a7d8f A |
1211 | UnicodeString& target, |
1212 | UnicodeString& variant) { | |
1213 | ||
729e4ab9 A |
1214 | TransliteratorSpec src(source); |
1215 | TransliteratorSpec trg(target); | |
1216 | TransliteratorEntry* entry; | |
1217 | ||
1218 | // Seek exact match in hashtable. Temporary fix for ICU 4.6. | |
1219 | // TODO: The general logic for finding a matching transliterator needs to be reviewed. | |
1220 | // ICU ticket #8089 | |
1221 | UnicodeString ID; | |
1222 | TransliteratorIDParser::STVtoID(source, target, variant, ID); | |
1223 | entry = (TransliteratorEntry*) registry.get(ID); | |
1224 | if (entry != 0) { | |
1225 | // std::string ss; | |
1226 | // std::cout << ID.toUTF8String(ss) << std::endl; | |
1227 | return entry; | |
1228 | } | |
b75a7d8f A |
1229 | |
1230 | if (variant.length() != 0) { | |
1231 | ||
1232 | // Seek exact match in hashtable | |
1233 | entry = findInDynamicStore(src, trg, variant); | |
1234 | if (entry != 0) { | |
1235 | return entry; | |
1236 | } | |
1237 | ||
1238 | // Seek exact match in locale resources | |
1239 | entry = findInStaticStore(src, trg, variant); | |
1240 | if (entry != 0) { | |
1241 | return entry; | |
1242 | } | |
1243 | } | |
1244 | ||
1245 | for (;;) { | |
1246 | src.reset(); | |
1247 | for (;;) { | |
1248 | // Seek match in hashtable | |
1249 | entry = findInDynamicStore(src, trg, NO_VARIANT); | |
1250 | if (entry != 0) { | |
1251 | return entry; | |
1252 | } | |
1253 | ||
1254 | // Seek match in locale resources | |
1255 | entry = findInStaticStore(src, trg, NO_VARIANT); | |
1256 | if (entry != 0) { | |
1257 | return entry; | |
1258 | } | |
1259 | if (!src.hasFallback()) { | |
1260 | break; | |
1261 | } | |
1262 | src.next(); | |
1263 | } | |
1264 | if (!trg.hasFallback()) { | |
1265 | break; | |
1266 | } | |
1267 | trg.next(); | |
1268 | } | |
1269 | ||
1270 | return 0; | |
1271 | } | |
1272 | ||
1273 | /** | |
1274 | * Given an Entry object, instantiate it. Caller owns result. Return | |
1275 | * 0 on failure. | |
1276 | * | |
1277 | * Return a non-empty aliasReturn value if the ID points to an alias. | |
1278 | * We cannot instantiate it ourselves because the alias may contain | |
1279 | * filters or compounds, which we do not understand. Caller should | |
1280 | * make aliasReturn empty before calling. | |
1281 | * | |
1282 | * The entry object is assumed to reside in the dynamic store. It may be | |
1283 | * modified. | |
1284 | */ | |
1285 | Transliterator* TransliteratorRegistry::instantiateEntry(const UnicodeString& ID, | |
729e4ab9 | 1286 | TransliteratorEntry *entry, |
b75a7d8f | 1287 | TransliteratorAlias* &aliasReturn, |
b75a7d8f | 1288 | UErrorCode& status) { |
374ca955 A |
1289 | Transliterator *t = 0; |
1290 | U_ASSERT(aliasReturn == 0); | |
b75a7d8f | 1291 | |
374ca955 | 1292 | switch (entry->entryType) { |
729e4ab9 | 1293 | case TransliteratorEntry::RBT_DATA: |
374ca955 A |
1294 | t = new RuleBasedTransliterator(ID, entry->u.data); |
1295 | if (t == 0) { | |
1296 | status = U_MEMORY_ALLOCATION_ERROR; | |
1297 | } | |
1298 | return t; | |
729e4ab9 | 1299 | case TransliteratorEntry::PROTOTYPE: |
374ca955 A |
1300 | t = entry->u.prototype->clone(); |
1301 | if (t == 0) { | |
1302 | status = U_MEMORY_ALLOCATION_ERROR; | |
1303 | } | |
1304 | return t; | |
729e4ab9 | 1305 | case TransliteratorEntry::ALIAS: |
73c04bcf | 1306 | aliasReturn = new TransliteratorAlias(entry->stringArg, entry->compoundFilter); |
374ca955 A |
1307 | if (aliasReturn == 0) { |
1308 | status = U_MEMORY_ALLOCATION_ERROR; | |
1309 | } | |
1310 | return 0; | |
729e4ab9 | 1311 | case TransliteratorEntry::FACTORY: |
374ca955 A |
1312 | t = entry->u.factory.function(ID, entry->u.factory.context); |
1313 | if (t == 0) { | |
1314 | status = U_MEMORY_ALLOCATION_ERROR; | |
1315 | } | |
1316 | return t; | |
729e4ab9 | 1317 | case TransliteratorEntry::COMPOUND_RBT: |
374ca955 | 1318 | { |
46f4442e A |
1319 | UVector* rbts = new UVector(entry->u.dataVector->size(), status); |
1320 | // Check for null pointer | |
1321 | if (rbts == NULL) { | |
2ca993e8 A |
1322 | status = U_MEMORY_ALLOCATION_ERROR; |
1323 | return NULL; | |
46f4442e | 1324 | } |
73c04bcf A |
1325 | int32_t passNumber = 1; |
1326 | for (int32_t i = 0; U_SUCCESS(status) && i < entry->u.dataVector->size(); i++) { | |
4388f060 A |
1327 | // TODO: Should passNumber be turned into a decimal-string representation (1 -> "1")? |
1328 | Transliterator* t = new RuleBasedTransliterator(UnicodeString(CompoundTransliterator::PASS_STRING) + UnicodeString(passNumber++), | |
73c04bcf A |
1329 | (TransliterationRuleData*)(entry->u.dataVector->elementAt(i)), FALSE); |
1330 | if (t == 0) | |
1331 | status = U_MEMORY_ALLOCATION_ERROR; | |
1332 | else | |
1333 | rbts->addElement(t, status); | |
b75a7d8f | 1334 | } |
46f4442e A |
1335 | if (U_FAILURE(status)) { |
1336 | delete rbts; | |
73c04bcf | 1337 | return 0; |
46f4442e | 1338 | } |
73c04bcf | 1339 | aliasReturn = new TransliteratorAlias(ID, entry->stringArg, rbts, entry->compoundFilter); |
b75a7d8f | 1340 | } |
374ca955 A |
1341 | if (aliasReturn == 0) { |
1342 | status = U_MEMORY_ALLOCATION_ERROR; | |
1343 | } | |
1344 | return 0; | |
729e4ab9 | 1345 | case TransliteratorEntry::LOCALE_RULES: |
374ca955 A |
1346 | aliasReturn = new TransliteratorAlias(ID, entry->stringArg, |
1347 | (UTransDirection) entry->intArg); | |
1348 | if (aliasReturn == 0) { | |
1349 | status = U_MEMORY_ALLOCATION_ERROR; | |
1350 | } | |
1351 | return 0; | |
729e4ab9 A |
1352 | case TransliteratorEntry::RULES_FORWARD: |
1353 | case TransliteratorEntry::RULES_REVERSE: | |
374ca955 A |
1354 | // Process the rule data into a TransliteratorRuleData object, |
1355 | // and possibly also into an ::id header and/or footer. Then | |
1356 | // we modify the registry with the parsed data and retry. | |
1357 | { | |
73c04bcf | 1358 | TransliteratorParser parser(status); |
b75a7d8f A |
1359 | |
1360 | // We use the file name, taken from another resource bundle | |
1361 | // 2-d array at static init time, as a locale language. We're | |
1362 | // just using the locale mechanism to map through to a file | |
1363 | // name; this in no way represents an actual locale. | |
374ca955 A |
1364 | //CharString ch(entry->stringArg); |
1365 | //UResourceBundle *bundle = ures_openDirect(0, ch, &status); | |
1366 | UnicodeString rules = entry->stringArg; | |
1367 | //ures_close(bundle); | |
b75a7d8f | 1368 | |
374ca955 A |
1369 | //if (U_FAILURE(status)) { |
1370 | // We have a failure of some kind. Remove the ID from the | |
1371 | // registry so we don't keep trying. NOTE: This will throw off | |
1372 | // anyone who is, at the moment, trying to iterate over the | |
1373 | // available IDs. That's acceptable since we should never | |
1374 | // really get here except under installation, configuration, | |
1375 | // or unrecoverable run time memory failures. | |
1376 | // remove(ID); | |
1377 | //} else { | |
1378 | ||
1379 | // If the status indicates a failure, then we don't have any | |
1380 | // rules -- there is probably an installation error. The list | |
1381 | // in the root locale should correspond to all the installed | |
1382 | // transliterators; if it lists something that's not | |
1383 | // installed, we'll get an error from ResourceBundle. | |
1384 | aliasReturn = new TransliteratorAlias(ID, rules, | |
729e4ab9 | 1385 | ((entry->entryType == TransliteratorEntry::RULES_REVERSE) ? |
374ca955 A |
1386 | UTRANS_REVERSE : UTRANS_FORWARD)); |
1387 | if (aliasReturn == 0) { | |
1388 | status = U_MEMORY_ALLOCATION_ERROR; | |
1389 | } | |
1390 | //} | |
b75a7d8f | 1391 | } |
374ca955 A |
1392 | return 0; |
1393 | default: | |
1394 | U_ASSERT(FALSE); // can't get here | |
1395 | return 0; | |
b75a7d8f | 1396 | } |
b75a7d8f A |
1397 | } |
1398 | U_NAMESPACE_END | |
1399 | ||
1400 | #endif /* #if !UCONFIG_NO_TRANSLITERATION */ | |
1401 | ||
1402 | //eof |