1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 **********************************************************************
5 * Copyright (c) 2001-2016 International Business Machines
6 * Corporation and others. All Rights Reserved.
7 **********************************************************************
8 * Date Name Description
9 * 08/10/2001 aliu Creation.
10 **********************************************************************
13 #include "unicode/utypes.h"
15 #if !UCONFIG_NO_TRANSLITERATION
17 #include "unicode/translit.h"
18 #include "unicode/resbund.h"
19 #include "unicode/uniset.h"
20 #include "unicode/uscript.h"
32 // Enable the following symbol to add debugging code that tracks the
33 // allocation, deletion, and use of Entry objects. BoundsChecker has
34 // reported dangling pointer errors with these objects, but I have
35 // been unable to confirm them. I suspect BoundsChecker is getting
36 // confused with pointers going into and coming out of a UHashtable,
37 // despite the hinting code that is designed to help it.
44 static const UChar LOCALE_SEP
= 95; // '_'
45 //static const UChar ID_SEP = 0x002D; /*-*/
46 //static const UChar VARIANT_SEP = 0x002F; // '/'
49 static const UChar ANY
[] = { 0x41, 0x6E, 0x79, 0 }; // Any
50 static const UChar LAT
[] = { 0x4C, 0x61, 0x74, 0 }; // Lat
53 #define NO_VARIANT UnicodeString()
55 // initial estimate for specDAG size
56 #define SPECDAG_INIT_SIZE 134
58 // initial estimate for number of variant names
59 #define VARIANT_LIST_INIT_SIZE 11
60 #define VARIANT_LIST_MAX_SIZE 31
62 // initial estimate for availableIDs count (default estimate is 8 => multiple reallocs)
63 #define AVAILABLE_IDS_INIT_SIZE 493
65 // initial estimate for number of targets for source "Any", "Lat"
66 #define ANY_TARGETS_INIT_SIZE 102
67 #define LAT_TARGETS_INIT_SIZE 23
70 * Resource bundle key for the RuleBasedTransliterator rule.
72 //static const char RB_RULE[] = "Rule";
76 //------------------------------------------------------------------
78 //------------------------------------------------------------------
80 TransliteratorAlias::TransliteratorAlias(const UnicodeString
& theAliasID
,
81 const UnicodeSet
* cpdFilter
) :
83 aliasesOrRules(theAliasID
),
85 compoundFilter(cpdFilter
),
86 direction(UTRANS_FORWARD
),
87 type(TransliteratorAlias::SIMPLE
) {
90 TransliteratorAlias::TransliteratorAlias(const UnicodeString
& theID
,
91 const UnicodeString
& idBlocks
,
92 UVector
* adoptedTransliterators
,
93 const UnicodeSet
* cpdFilter
) :
95 aliasesOrRules(idBlocks
),
96 transes(adoptedTransliterators
),
97 compoundFilter(cpdFilter
),
98 direction(UTRANS_FORWARD
),
99 type(TransliteratorAlias::COMPOUND
) {
102 TransliteratorAlias::TransliteratorAlias(const UnicodeString
& theID
,
103 const UnicodeString
& rules
,
104 UTransDirection dir
) :
106 aliasesOrRules(rules
),
110 type(TransliteratorAlias::RULES
) {
113 TransliteratorAlias::~TransliteratorAlias() {
118 Transliterator
* TransliteratorAlias::create(UParseError
& pe
,
123 Transliterator
*t
= NULL
;
126 t
= Transliterator::createInstance(aliasesOrRules
, UTRANS_FORWARD
, pe
, ec
);
130 if (compoundFilter
!= 0)
131 t
->adoptFilter((UnicodeSet
*)compoundFilter
->clone());
135 // the total number of transliterators in the compound is the total number of anonymous transliterators
136 // plus the total number of ID blocks-- we start by assuming the list begins and ends with an ID
137 // block and that each pair anonymous transliterators has an ID block between them. Then we go back
138 // to see whether there really are ID blocks at the beginning and end (by looking for U+FFFF, which
139 // marks the position where an anonymous transliterator goes) and adjust accordingly
140 int32_t anonymousRBTs
= transes
->size();
141 int32_t transCount
= anonymousRBTs
* 2 + 1;
142 if (!aliasesOrRules
.isEmpty() && aliasesOrRules
[0] == (UChar
)(0xffff))
144 if (aliasesOrRules
.length() >= 2 && aliasesOrRules
[aliasesOrRules
.length() - 1] == (UChar
)(0xffff))
146 UnicodeString
noIDBlock((UChar
)(0xffff));
147 noIDBlock
+= ((UChar
)(0xffff));
148 int32_t pos
= aliasesOrRules
.indexOf(noIDBlock
);
151 pos
= aliasesOrRules
.indexOf(noIDBlock
, pos
+ 1);
154 UVector
transliterators(ec
);
155 UnicodeString idBlock
;
156 int32_t blockSeparatorPos
= aliasesOrRules
.indexOf((UChar
)(0xffff));
157 while (blockSeparatorPos
>= 0) {
158 aliasesOrRules
.extract(0, blockSeparatorPos
, idBlock
);
159 aliasesOrRules
.remove(0, blockSeparatorPos
+ 1);
160 if (!idBlock
.isEmpty())
161 transliterators
.addElement(Transliterator::createInstance(idBlock
, UTRANS_FORWARD
, pe
, ec
), ec
);
162 if (!transes
->isEmpty())
163 transliterators
.addElement(transes
->orphanElementAt(0), ec
);
164 blockSeparatorPos
= aliasesOrRules
.indexOf((UChar
)(0xffff));
166 if (!aliasesOrRules
.isEmpty())
167 transliterators
.addElement(Transliterator::createInstance(aliasesOrRules
, UTRANS_FORWARD
, pe
, ec
), ec
);
168 while (!transes
->isEmpty())
169 transliterators
.addElement(transes
->orphanElementAt(0), ec
);
172 t
= new CompoundTransliterator(ID
, transliterators
,
173 (compoundFilter
? (UnicodeSet
*)(compoundFilter
->clone()) : 0),
174 anonymousRBTs
, pe
, ec
);
176 ec
= U_MEMORY_ALLOCATION_ERROR
;
180 for (int32_t i
= 0; i
< transliterators
.size(); i
++)
181 delete (Transliterator
*)(transliterators
.elementAt(i
));
186 U_ASSERT(FALSE
); // don't call create() if isRuleBased() returns TRUE!
192 UBool
TransliteratorAlias::isRuleBased() const {
193 return type
== RULES
;
196 void TransliteratorAlias::parse(TransliteratorParser
& parser
,
197 UParseError
& pe
, UErrorCode
& ec
) const {
198 U_ASSERT(type
== RULES
);
203 parser
.parse(aliasesOrRules
, direction
, pe
, ec
);
206 //----------------------------------------------------------------------
207 // class TransliteratorSpec
208 //----------------------------------------------------------------------
211 * A TransliteratorSpec is a string specifying either a source or a target. In more
212 * general terms, it may also specify a variant, but we only use the
213 * Spec class for sources and targets.
215 * A Spec may be a locale or a script. If it is a locale, it has a
216 * fallback chain that goes xx_YY_ZZZ -> xx_YY -> xx -> ssss, where
217 * ssss is the script mapping of xx_YY_ZZZ. The Spec API methods
218 * hasFallback(), next(), and reset() iterate over this fallback
221 * The Spec class canonicalizes itself, so the locale is put into
222 * canonical form, or the script is transformed from an abbreviation
225 class TransliteratorSpec
: public UMemory
{
227 TransliteratorSpec(const UnicodeString
& spec
);
228 ~TransliteratorSpec();
230 const UnicodeString
& get() const;
231 UBool
hasFallback() const;
232 const UnicodeString
& next();
235 UBool
isLocale() const;
236 ResourceBundle
& getBundle() const;
238 operator const UnicodeString
&() const { return get(); }
239 const UnicodeString
& getTop() const { return top
; }
246 UnicodeString nextSpec
;
247 UnicodeString scriptName
;
248 UBool isSpecLocale
; // TRUE if spec is a locale
249 UBool isNextLocale
; // TRUE if nextSpec is a locale
252 TransliteratorSpec(const TransliteratorSpec
&other
); // forbid copying of this class
253 TransliteratorSpec
&operator=(const TransliteratorSpec
&other
); // forbid copying of this class
256 TransliteratorSpec::TransliteratorSpec(const UnicodeString
& theSpec
)
260 UErrorCode status
= U_ZERO_ERROR
;
262 LocaleUtility::initLocaleFromName(theSpec
, topLoc
);
263 if (!topLoc
.isBogus()) {
264 res
= new ResourceBundle(U_ICUDATA_TRANSLIT
, topLoc
, status
);
269 if (U_FAILURE(status
) || status
== U_USING_DEFAULT_WARNING
) {
275 // Canonicalize script name -or- do locale->script mapping
276 status
= U_ZERO_ERROR
;
277 static const int32_t capacity
= 10;
278 UScriptCode script
[capacity
]={USCRIPT_INVALID_CODE
};
279 int32_t num
= uscript_getCode(CharString().appendInvariantChars(theSpec
, status
).data(),
280 script
, capacity
, &status
);
281 if (num
> 0 && script
[0] != USCRIPT_INVALID_CODE
) {
282 scriptName
= UnicodeString(uscript_getName(script
[0]), -1, US_INV
);
287 // Canonicalize locale name
288 UnicodeString locStr
;
289 LocaleUtility::initNameFromLocale(topLoc
, locStr
);
290 if (!locStr
.isBogus()) {
293 } else if (scriptName
.length() != 0) {
294 // We are a script; use canonical name
298 // assert(spec != top);
302 TransliteratorSpec::~TransliteratorSpec() {
306 UBool
TransliteratorSpec::hasFallback() const {
307 return nextSpec
.length() != 0;
310 void TransliteratorSpec::reset() {
313 isSpecLocale
= (res
!= 0);
318 void TransliteratorSpec::setupNext() {
319 isNextLocale
= FALSE
;
322 int32_t i
= nextSpec
.lastIndexOf(LOCALE_SEP
);
323 // If i == 0 then we have _FOO, so we fall through
324 // to the scriptName.
326 nextSpec
.truncate(i
);
329 nextSpec
= scriptName
; // scriptName may be empty
332 // spec is a script, so we are at the end
333 nextSpec
.truncate(0);
338 // for(const UnicodeString& s(spec.get());
339 // spec.hasFallback(); s(spec.next())) { ...
341 const UnicodeString
& TransliteratorSpec::next() {
343 isSpecLocale
= isNextLocale
;
348 const UnicodeString
& TransliteratorSpec::get() const {
352 UBool
TransliteratorSpec::isLocale() const {
356 ResourceBundle
& TransliteratorSpec::getBundle() const {
360 //----------------------------------------------------------------------
364 // Vector of Entry pointers currently in use
365 static UVector
* DEBUG_entries
= NULL
;
367 static void DEBUG_setup() {
368 if (DEBUG_entries
== NULL
) {
369 UErrorCode ec
= U_ZERO_ERROR
;
370 DEBUG_entries
= new UVector(ec
);
374 // Caller must call DEBUG_setup first. Return index of given Entry,
375 // if it is in use (not deleted yet), or -1 if not found.
376 static int DEBUG_findEntry(TransliteratorEntry
* e
) {
377 for (int i
=0; i
<DEBUG_entries
->size(); ++i
) {
378 if (e
== (TransliteratorEntry
*) DEBUG_entries
->elementAt(i
)) {
385 // Track object creation
386 static void DEBUG_newEntry(TransliteratorEntry
* e
) {
388 if (DEBUG_findEntry(e
) >= 0) {
389 // This should really never happen unless the heap is broken
390 printf("ERROR DEBUG_newEntry duplicate new pointer %08X\n", e
);
393 UErrorCode ec
= U_ZERO_ERROR
;
394 DEBUG_entries
->addElement(e
, ec
);
397 // Track object deletion
398 static void DEBUG_delEntry(TransliteratorEntry
* e
) {
400 int i
= DEBUG_findEntry(e
);
402 printf("ERROR DEBUG_delEntry possible double deletion %08X\n", e
);
405 DEBUG_entries
->removeElementAt(i
);
408 // Track object usage
409 static void DEBUG_useEntry(TransliteratorEntry
* e
) {
410 if (e
== NULL
) return;
412 int i
= DEBUG_findEntry(e
);
414 printf("ERROR DEBUG_useEntry possible dangling pointer %08X\n", e
);
419 // If we're not debugging then make these macros into NOPs
420 #define DEBUG_newEntry(x)
421 #define DEBUG_delEntry(x)
422 #define DEBUG_useEntry(x)
425 //----------------------------------------------------------------------
427 //----------------------------------------------------------------------
430 * The Entry object stores objects of different types and
431 * singleton objects as placeholders for rule-based transliterators to
432 * be built as needed. Instances of this struct can be placeholders,
433 * can represent prototype transliterators to be cloned, or can
434 * represent TransliteratorData objects. We don't support storing
435 * classes in the registry because we don't have the rtti infrastructure
436 * for it. We could easily add this if there is a need for it in the
439 class TransliteratorEntry
: public UMemory
{
450 NONE
// Only used for uninitialized entries
452 // NOTE: stringArg cannot go inside the union because
453 // it has a copy constructor
454 UnicodeString stringArg
; // For RULES_*, ALIAS, COMPOUND_RBT
455 int32_t intArg
; // For COMPOUND_RBT, LOCALE_RULES
456 UnicodeSet
* compoundFilter
; // For COMPOUND_RBT
458 Transliterator
* prototype
; // For PROTOTYPE
459 TransliterationRuleData
* data
; // For RBT_DATA
460 UVector
* dataVector
; // For COMPOUND_RBT
462 Transliterator::Factory function
;
463 Transliterator::Token context
;
464 } factory
; // For FACTORY
466 TransliteratorEntry();
467 ~TransliteratorEntry();
468 void adoptPrototype(Transliterator
* adopted
);
469 void setFactory(Transliterator::Factory factory
,
470 Transliterator::Token context
);
474 TransliteratorEntry(const TransliteratorEntry
&other
); // forbid copying of this class
475 TransliteratorEntry
&operator=(const TransliteratorEntry
&other
); // forbid copying of this class
478 TransliteratorEntry::TransliteratorEntry() {
480 compoundFilter
= NULL
;
482 DEBUG_newEntry(this);
485 TransliteratorEntry::~TransliteratorEntry() {
486 DEBUG_delEntry(this);
487 if (entryType
== PROTOTYPE
) {
489 } else if (entryType
== RBT_DATA
) {
490 // The data object is shared between instances of RBT. The
491 // entry object owns it. It should only be deleted when the
492 // transliterator component is being cleaned up. Doing so
493 // invalidates any RBTs that the user has instantiated.
495 } else if (entryType
== COMPOUND_RBT
) {
496 while (u
.dataVector
!= NULL
&& !u
.dataVector
->isEmpty())
497 delete (TransliterationRuleData
*)u
.dataVector
->orphanElementAt(0);
500 delete compoundFilter
;
503 void TransliteratorEntry::adoptPrototype(Transliterator
* adopted
) {
504 if (entryType
== PROTOTYPE
) {
507 entryType
= PROTOTYPE
;
508 u
.prototype
= adopted
;
511 void TransliteratorEntry::setFactory(Transliterator::Factory factory
,
512 Transliterator::Token context
) {
513 if (entryType
== PROTOTYPE
) {
517 u
.factory
.function
= factory
;
518 u
.factory
.context
= context
;
521 // UObjectDeleter for Hashtable::setValueDeleter
523 static void U_CALLCONV
524 deleteEntry(void* obj
) {
525 delete (TransliteratorEntry
*) obj
;
529 //----------------------------------------------------------------------
530 // class TransliteratorRegistry: Basic public API
531 //----------------------------------------------------------------------
533 TransliteratorRegistry::TransliteratorRegistry(UErrorCode
& status
) :
534 registry(TRUE
, status
),
535 specDAG(TRUE
, SPECDAG_INIT_SIZE
, status
),
536 variantList(VARIANT_LIST_INIT_SIZE
, status
),
537 availableIDs(AVAILABLE_IDS_INIT_SIZE
, status
)
539 registry
.setValueDeleter(deleteEntry
);
540 variantList
.setDeleter(uprv_deleteUObject
);
541 variantList
.setComparer(uhash_compareCaselessUnicodeString
);
542 UnicodeString
*emptyString
= new UnicodeString();
543 if (emptyString
!= NULL
) {
544 variantList
.addElement(emptyString
, status
);
546 availableIDs
.setDeleter(uprv_deleteUObject
);
547 availableIDs
.setComparer(uhash_compareCaselessUnicodeString
);
548 specDAG
.setValueDeleter(uhash_deleteHashtable
);
551 TransliteratorRegistry::~TransliteratorRegistry() {
552 // Through the magic of C++, everything cleans itself up
555 Transliterator
* TransliteratorRegistry::get(const UnicodeString
& ID
,
556 TransliteratorAlias
*& aliasReturn
,
557 UErrorCode
& status
) {
558 U_ASSERT(aliasReturn
== NULL
);
559 TransliteratorEntry
*entry
= find(ID
);
560 return (entry
== 0) ? 0
561 : instantiateEntry(ID
, entry
, aliasReturn
, status
);
564 Transliterator
* TransliteratorRegistry::reget(const UnicodeString
& ID
,
565 TransliteratorParser
& parser
,
566 TransliteratorAlias
*& aliasReturn
,
567 UErrorCode
& status
) {
568 U_ASSERT(aliasReturn
== NULL
);
569 TransliteratorEntry
*entry
= find(ID
);
572 // We get to this point if there are two threads, one of which
573 // is instantiating an ID, and another of which is removing
574 // the same ID from the registry, and the timing is just right.
578 // The usage model for the caller is that they will first call
579 // reg->get() inside the mutex, they'll get back an alias, they call
580 // alias->isRuleBased(), and if they get TRUE, they call alias->parse()
581 // outside the mutex, then reg->reget() inside the mutex again. A real
582 // mess, but it gets things working for ICU 3.0. [alan].
584 // Note: It's possible that in between the caller calling
585 // alias->parse() and reg->reget(), that another thread will have
586 // called reg->reget(), and the entry will already have been fixed up.
587 // We have to detect this so we don't stomp over existing entry
588 // data members and potentially leak memory (u.data and compoundFilter).
590 if (entry
->entryType
== TransliteratorEntry::RULES_FORWARD
||
591 entry
->entryType
== TransliteratorEntry::RULES_REVERSE
||
592 entry
->entryType
== TransliteratorEntry::LOCALE_RULES
) {
594 if (parser
.idBlockVector
.isEmpty() && parser
.dataVector
.isEmpty()) {
596 entry
->entryType
= TransliteratorEntry::ALIAS
;
597 entry
->stringArg
= UNICODE_STRING_SIMPLE("Any-NULL");
599 else if (parser
.idBlockVector
.isEmpty() && parser
.dataVector
.size() == 1) {
600 entry
->u
.data
= (TransliterationRuleData
*)parser
.dataVector
.orphanElementAt(0);
601 entry
->entryType
= TransliteratorEntry::RBT_DATA
;
603 else if (parser
.idBlockVector
.size() == 1 && parser
.dataVector
.isEmpty()) {
604 entry
->stringArg
= *(UnicodeString
*)(parser
.idBlockVector
.elementAt(0));
605 entry
->compoundFilter
= parser
.orphanCompoundFilter();
606 entry
->entryType
= TransliteratorEntry::ALIAS
;
609 entry
->entryType
= TransliteratorEntry::COMPOUND_RBT
;
610 entry
->compoundFilter
= parser
.orphanCompoundFilter();
611 entry
->u
.dataVector
= new UVector(status
);
612 entry
->stringArg
.remove();
614 int32_t limit
= parser
.idBlockVector
.size();
615 if (parser
.dataVector
.size() > limit
)
616 limit
= parser
.dataVector
.size();
618 for (int32_t i
= 0; i
< limit
; i
++) {
619 if (i
< parser
.idBlockVector
.size()) {
620 UnicodeString
* idBlock
= (UnicodeString
*)parser
.idBlockVector
.elementAt(i
);
621 if (!idBlock
->isEmpty())
622 entry
->stringArg
+= *idBlock
;
624 if (!parser
.dataVector
.isEmpty()) {
625 TransliterationRuleData
* data
= (TransliterationRuleData
*)parser
.dataVector
.orphanElementAt(0);
626 entry
->u
.dataVector
->addElement(data
, status
);
627 entry
->stringArg
+= (UChar
)0xffff; // use U+FFFF to mark position of RBTs in ID block
634 instantiateEntry(ID
, entry
, aliasReturn
, status
);
638 void TransliteratorRegistry::put(Transliterator
* adoptedProto
,
642 TransliteratorEntry
*entry
= new TransliteratorEntry();
644 ec
= U_MEMORY_ALLOCATION_ERROR
;
647 entry
->adoptPrototype(adoptedProto
);
648 registerEntry(adoptedProto
->getID(), entry
, visible
);
651 void TransliteratorRegistry::put(const UnicodeString
& ID
,
652 Transliterator::Factory factory
,
653 Transliterator::Token context
,
656 TransliteratorEntry
*entry
= new TransliteratorEntry();
658 ec
= U_MEMORY_ALLOCATION_ERROR
;
661 entry
->setFactory(factory
, context
);
662 registerEntry(ID
, entry
, visible
);
665 void TransliteratorRegistry::put(const UnicodeString
& ID
,
666 const UnicodeString
& resourceName
,
668 UBool readonlyResourceAlias
,
671 TransliteratorEntry
*entry
= new TransliteratorEntry();
673 ec
= U_MEMORY_ALLOCATION_ERROR
;
676 entry
->entryType
= (dir
== UTRANS_FORWARD
) ? TransliteratorEntry::RULES_FORWARD
677 : TransliteratorEntry::RULES_REVERSE
;
678 if (readonlyResourceAlias
) {
679 entry
->stringArg
.setTo(TRUE
, resourceName
.getBuffer(), -1);
682 entry
->stringArg
= resourceName
;
684 registerEntry(ID
, entry
, visible
);
687 void TransliteratorRegistry::put(const UnicodeString
& ID
,
688 const UnicodeString
& alias
,
689 UBool readonlyAliasAlias
,
691 UErrorCode
& /*ec*/) {
692 TransliteratorEntry
*entry
= new TransliteratorEntry();
693 // Null pointer check
695 entry
->entryType
= TransliteratorEntry::ALIAS
;
696 if (readonlyAliasAlias
) {
697 entry
->stringArg
.setTo(TRUE
, alias
.getBuffer(), -1);
700 entry
->stringArg
= alias
;
702 registerEntry(ID
, entry
, visible
);
706 void TransliteratorRegistry::remove(const UnicodeString
& ID
) {
707 UnicodeString source
, target
, variant
;
709 TransliteratorIDParser::IDtoSTV(ID
, source
, target
, variant
, sawSource
);
710 // Only need to do this if ID.indexOf('-') < 0
712 TransliteratorIDParser::STVtoID(source
, target
, variant
, id
);
714 removeSTV(source
, target
, variant
);
715 availableIDs
.removeElement((void*) &id
);
718 //----------------------------------------------------------------------
719 // class TransliteratorRegistry: Public ID and spec management
720 //----------------------------------------------------------------------
723 * == OBSOLETE - remove in ICU 3.4 ==
724 * Return the number of IDs currently registered with the system.
725 * To retrieve the actual IDs, call getAvailableID(i) with
726 * i from 0 to countAvailableIDs() - 1.
728 int32_t TransliteratorRegistry::countAvailableIDs(void) const {
729 return availableIDs
.size();
733 * == OBSOLETE - remove in ICU 3.4 ==
734 * Return the index-th available ID. index must be between 0
735 * and countAvailableIDs() - 1, inclusive. If index is out of
736 * range, the result of getAvailableID(0) is returned.
738 const UnicodeString
& TransliteratorRegistry::getAvailableID(int32_t index
) const {
739 if (index
< 0 || index
>= availableIDs
.size()) {
742 return *(const UnicodeString
*) availableIDs
[index
];
745 StringEnumeration
* TransliteratorRegistry::getAvailableIDs() const {
746 return new Enumeration(*this);
749 int32_t TransliteratorRegistry::countAvailableSources(void) const {
750 return specDAG
.count();
753 UnicodeString
& TransliteratorRegistry::getAvailableSource(int32_t index
,
754 UnicodeString
& result
) const {
755 int32_t pos
= UHASH_FIRST
;
756 const UHashElement
*e
= 0;
757 while (index
-- >= 0) {
758 e
= specDAG
.nextElement(pos
);
766 result
= *(UnicodeString
*) e
->key
.pointer
;
771 int32_t TransliteratorRegistry::countAvailableTargets(const UnicodeString
& source
) const {
772 Hashtable
*targets
= (Hashtable
*) specDAG
.get(source
);
773 return (targets
== 0) ? 0 : targets
->count();
776 UnicodeString
& TransliteratorRegistry::getAvailableTarget(int32_t index
,
777 const UnicodeString
& source
,
778 UnicodeString
& result
) const {
779 Hashtable
*targets
= (Hashtable
*) specDAG
.get(source
);
781 result
.truncate(0); // invalid source
784 int32_t pos
= UHASH_FIRST
;
785 const UHashElement
*e
= 0;
786 while (index
-- >= 0) {
787 e
= targets
->nextElement(pos
);
793 result
.truncate(0); // invalid index
795 result
= *(UnicodeString
*) e
->key
.pointer
;
800 int32_t TransliteratorRegistry::countAvailableVariants(const UnicodeString
& source
,
801 const UnicodeString
& target
) const {
802 Hashtable
*targets
= (Hashtable
*) specDAG
.get(source
);
806 int32_t varMask
= targets
->geti(target
);
807 int32_t varCount
= 0;
808 while (varMask
> 0) {
817 UnicodeString
& TransliteratorRegistry::getAvailableVariant(int32_t index
,
818 const UnicodeString
& source
,
819 const UnicodeString
& target
,
820 UnicodeString
& result
) const {
821 Hashtable
*targets
= (Hashtable
*) specDAG
.get(source
);
823 result
.truncate(0); // invalid source
826 int32_t varMask
= targets
->geti(target
);
827 int32_t varCount
= 0;
828 int32_t varListIndex
= 0;
829 while (varMask
> 0) {
831 if (varCount
== index
) {
832 UnicodeString
*v
= (UnicodeString
*) variantList
.elementAt(varListIndex
);
844 result
.truncate(0); // invalid target or index
848 //----------------------------------------------------------------------
849 // class TransliteratorRegistry::Enumeration
850 //----------------------------------------------------------------------
852 TransliteratorRegistry::Enumeration::Enumeration(const TransliteratorRegistry
& _reg
) :
853 index(0), reg(_reg
) {
856 TransliteratorRegistry::Enumeration::~Enumeration() {
859 int32_t TransliteratorRegistry::Enumeration::count(UErrorCode
& /*status*/) const {
860 return reg
.availableIDs
.size();
863 const UnicodeString
* TransliteratorRegistry::Enumeration::snext(UErrorCode
& status
) {
864 // This is sloppy but safe -- if we get out of sync with the underlying
865 // registry, we will still return legal strings, but they might not
866 // correspond to the snapshot at construction time. So there could be
867 // duplicate IDs or omitted IDs if insertions or deletions occur in one
868 // thread while another is iterating. To be more rigorous, add a timestamp,
869 // which is incremented with any modification, and validate this iterator
870 // against the timestamp at construction time. This probably isn't worth
871 // doing as long as there is some possibility of removing this code in favor
872 // of some new code based on Doug's service framework.
873 if (U_FAILURE(status
)) {
876 int32_t n
= reg
.availableIDs
.size();
878 status
= U_ENUM_OUT_OF_SYNC_ERROR
;
880 // index == n is okay -- this means we've reached the end
882 // Copy the string! This avoids lifetime problems.
883 unistr
= *(const UnicodeString
*)reg
.availableIDs
[index
++];
890 void TransliteratorRegistry::Enumeration::reset(UErrorCode
& /*status*/) {
894 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(TransliteratorRegistry::Enumeration
)
896 //----------------------------------------------------------------------
897 // class TransliteratorRegistry: internal
898 //----------------------------------------------------------------------
901 * Convenience method. Calls 6-arg registerEntry().
903 void TransliteratorRegistry::registerEntry(const UnicodeString
& source
,
904 const UnicodeString
& target
,
905 const UnicodeString
& variant
,
906 TransliteratorEntry
* adopted
,
909 UnicodeString
s(source
);
910 if (s
.length() == 0) {
911 s
.setTo(TRUE
, ANY
, 3);
913 TransliteratorIDParser::STVtoID(source
, target
, variant
, ID
);
914 registerEntry(ID
, s
, target
, variant
, adopted
, visible
);
918 * Convenience method. Calls 6-arg registerEntry().
920 void TransliteratorRegistry::registerEntry(const UnicodeString
& ID
,
921 TransliteratorEntry
* adopted
,
923 UnicodeString source
, target
, variant
;
925 TransliteratorIDParser::IDtoSTV(ID
, source
, target
, variant
, sawSource
);
926 // Only need to do this if ID.indexOf('-') < 0
928 TransliteratorIDParser::STVtoID(source
, target
, variant
, id
);
929 registerEntry(id
, source
, target
, variant
, adopted
, visible
);
933 * Register an entry object (adopted) with the given ID, source,
934 * target, and variant strings.
936 void TransliteratorRegistry::registerEntry(const UnicodeString
& ID
,
937 const UnicodeString
& source
,
938 const UnicodeString
& target
,
939 const UnicodeString
& variant
,
940 TransliteratorEntry
* adopted
,
942 UErrorCode status
= U_ZERO_ERROR
;
943 registry
.put(ID
, adopted
, status
);
945 registerSTV(source
, target
, variant
);
946 if (!availableIDs
.contains((void*) &ID
)) {
947 UnicodeString
*newID
= (UnicodeString
*)ID
.clone();
948 // Check to make sure newID was created.
950 // NUL-terminate the ID string
951 newID
->getTerminatedBuffer();
952 availableIDs
.addElement(newID
, status
);
956 removeSTV(source
, target
, variant
);
957 availableIDs
.removeElement((void*) &ID
);
962 * Register a source-target/variant in the specDAG. Variant may be
963 * empty, but source and target must not be.
965 void TransliteratorRegistry::registerSTV(const UnicodeString
& source
,
966 const UnicodeString
& target
,
967 const UnicodeString
& variant
) {
968 // assert(source.length() > 0);
969 // assert(target.length() > 0);
970 UErrorCode status
= U_ZERO_ERROR
;
971 Hashtable
*targets
= (Hashtable
*) specDAG
.get(source
);
974 if (source
.compare(ANY
,3) == 0) {
975 size
= ANY_TARGETS_INIT_SIZE
;
976 } else if (source
.compare(LAT
,3) == 0) {
977 size
= LAT_TARGETS_INIT_SIZE
;
979 targets
= new Hashtable(TRUE
, size
, status
);
980 if (U_FAILURE(status
) || targets
== NULL
) {
983 specDAG
.put(source
, targets
, status
);
985 int32_t variantListIndex
= variantList
.indexOf((void*) &variant
, 0);
986 if (variantListIndex
< 0) {
987 if (variantList
.size() >= VARIANT_LIST_MAX_SIZE
) {
988 // can't handle any more variants
991 UnicodeString
*variantEntry
= new UnicodeString(variant
);
992 if (variantEntry
!= NULL
) {
993 variantList
.addElement(variantEntry
, status
);
994 if (U_SUCCESS(status
)) {
995 variantListIndex
= variantList
.size() - 1;
998 if (variantListIndex
< 0) {
1002 int32_t addMask
= 1 << variantListIndex
;
1003 int32_t varMask
= targets
->geti(target
);
1004 targets
->puti(target
, varMask
| addMask
, status
);
1008 * Remove a source-target/variant from the specDAG.
1010 void TransliteratorRegistry::removeSTV(const UnicodeString
& source
,
1011 const UnicodeString
& target
,
1012 const UnicodeString
& variant
) {
1013 // assert(source.length() > 0);
1014 // assert(target.length() > 0);
1015 UErrorCode status
= U_ZERO_ERROR
;
1016 Hashtable
*targets
= (Hashtable
*) specDAG
.get(source
);
1017 if (targets
== NULL
) {
1018 return; // should never happen for valid s-t/v
1020 int32_t varMask
= targets
->geti(target
);
1022 return; // should never happen for valid s-t/v
1024 int32_t variantListIndex
= variantList
.indexOf((void*) &variant
, 0);
1025 if (variantListIndex
< 0) {
1026 return; // should never happen for valid s-t/v
1028 int32_t remMask
= 1 << variantListIndex
;
1029 varMask
&= (~remMask
);
1031 targets
->puti(target
, varMask
, status
);
1033 targets
->remove(target
); // should delete variants
1034 if (targets
->count() == 0) {
1035 specDAG
.remove(source
); // should delete targetss
1041 * Attempt to find a source-target/variant in the dynamic registry
1042 * store. Return 0 on failure.
1044 * Caller does NOT own returned object.
1046 TransliteratorEntry
* TransliteratorRegistry::findInDynamicStore(const TransliteratorSpec
& src
,
1047 const TransliteratorSpec
& trg
,
1048 const UnicodeString
& variant
) const {
1050 TransliteratorIDParser::STVtoID(src
, trg
, variant
, ID
);
1051 TransliteratorEntry
*e
= (TransliteratorEntry
*) registry
.get(ID
);
1057 * Attempt to find a source-target/variant in the static locale
1058 * resource store. Do not perform fallback. Return 0 on failure.
1060 * On success, create a new entry object, register it in the dynamic
1061 * store, and return a pointer to it, but do not make it public --
1062 * just because someone requested something, we do not expand the
1063 * available ID list (or spec DAG).
1065 * Caller does NOT own returned object.
1067 TransliteratorEntry
* TransliteratorRegistry::findInStaticStore(const TransliteratorSpec
& src
,
1068 const TransliteratorSpec
& trg
,
1069 const UnicodeString
& variant
) {
1070 TransliteratorEntry
* entry
= 0;
1071 if (src
.isLocale()) {
1072 entry
= findInBundle(src
, trg
, variant
, UTRANS_FORWARD
);
1073 } else if (trg
.isLocale()) {
1074 entry
= findInBundle(trg
, src
, variant
, UTRANS_REVERSE
);
1077 // If we found an entry, store it in the Hashtable for next
1080 registerEntry(src
.getTop(), trg
.getTop(), variant
, entry
, FALSE
);
1086 // As of 2.0, resource bundle keys cannot contain '_'
1087 static const UChar TRANSLITERATE_TO
[] = {84,114,97,110,115,108,105,116,101,114,97,116,101,84,111,0}; // "TransliterateTo"
1089 static const UChar TRANSLITERATE_FROM
[] = {84,114,97,110,115,108,105,116,101,114,97,116,101,70,114,111,109,0}; // "TransliterateFrom"
1091 static const UChar TRANSLITERATE
[] = {84,114,97,110,115,108,105,116,101,114,97,116,101,0}; // "Transliterate"
1094 * Attempt to find an entry in a single resource bundle. This is
1095 * a one-sided lookup. findInStaticStore() performs up to two such
1096 * lookups, one for the source, and one for the target.
1098 * Do not perform fallback. Return 0 on failure.
1100 * On success, create a new Entry object, populate it, and return it.
1101 * The caller owns the returned object.
1103 TransliteratorEntry
* TransliteratorRegistry::findInBundle(const TransliteratorSpec
& specToOpen
,
1104 const TransliteratorSpec
& specToFind
,
1105 const UnicodeString
& variant
,
1106 UTransDirection direction
)
1109 UnicodeString resStr
;
1112 for (pass
=0; pass
<2; ++pass
) {
1114 // First try either TransliteratorTo_xxx or
1115 // TransliterateFrom_xxx, then try the bidirectional
1116 // Transliterate_xxx. This precedence order is arbitrary
1117 // but must be consistent and documented.
1119 utag
.append(direction
== UTRANS_FORWARD
?
1120 TRANSLITERATE_TO
: TRANSLITERATE_FROM
, -1);
1122 utag
.append(TRANSLITERATE
, -1);
1124 UnicodeString
s(specToFind
.get());
1125 utag
.append(s
.toUpper(""));
1126 UErrorCode status
= U_ZERO_ERROR
;
1127 ResourceBundle
subres(specToOpen
.getBundle().get(
1128 CharString().appendInvariantChars(utag
, status
).data(), status
));
1129 if (U_FAILURE(status
) || status
== U_USING_DEFAULT_WARNING
) {
1134 if (specToOpen
.get() != LocaleUtility::initNameFromLocale(subres
.getLocale(), s
)) {
1138 if (variant
.length() != 0) {
1139 status
= U_ZERO_ERROR
;
1140 resStr
= subres
.getStringEx(
1141 CharString().appendInvariantChars(variant
, status
).data(), status
);
1142 if (U_SUCCESS(status
)) {
1143 // Exit loop successfully
1147 // Variant is empty, which means match the first variant listed.
1148 status
= U_ZERO_ERROR
;
1149 resStr
= subres
.getStringEx(1, status
);
1150 if (U_SUCCESS(status
)) {
1151 // Exit loop successfully
1162 // We have succeeded in loading a string from the locale
1163 // resources. Create a new registry entry to hold it and return it.
1164 TransliteratorEntry
*entry
= new TransliteratorEntry();
1166 // The direction is always forward for the
1167 // TransliterateTo_xxx and TransliterateFrom_xxx
1168 // items; those are unidirectional forward rules.
1169 // For the bidirectional Transliterate_xxx items,
1170 // the direction is the value passed in to this
1172 int32_t dir
= (pass
== 0) ? UTRANS_FORWARD
: direction
;
1173 entry
->entryType
= TransliteratorEntry::LOCALE_RULES
;
1174 entry
->stringArg
= resStr
;
1175 entry
->intArg
= dir
;
1182 * Convenience method. Calls 3-arg find().
1184 TransliteratorEntry
* TransliteratorRegistry::find(const UnicodeString
& ID
) {
1185 UnicodeString source
, target
, variant
;
1187 TransliteratorIDParser::IDtoSTV(ID
, source
, target
, variant
, sawSource
);
1188 return find(source
, target
, variant
);
1192 * Top-level find method. Attempt to find a source-target/variant in
1193 * either the dynamic or the static (locale resource) store. Perform
1196 * Lookup sequence for ss_SS_SSS-tt_TT_TTT/v:
1198 * ss_SS_SSS-tt_TT_TTT/v -- in hashtable
1199 * ss_SS_SSS-tt_TT_TTT/v -- in ss_SS_SSS (no fallback)
1201 * repeat with t = tt_TT_TTT, tt_TT, tt, and tscript
1208 * Here * matches the first variant listed.
1210 * Caller does NOT own returned object. Return 0 on failure.
1212 TransliteratorEntry
* TransliteratorRegistry::find(UnicodeString
& source
,
1213 UnicodeString
& target
,
1214 UnicodeString
& variant
) {
1216 TransliteratorSpec
src(source
);
1217 TransliteratorSpec
trg(target
);
1218 TransliteratorEntry
* entry
;
1220 // Seek exact match in hashtable. Temporary fix for ICU 4.6.
1221 // TODO: The general logic for finding a matching transliterator needs to be reviewed.
1224 TransliteratorIDParser::STVtoID(source
, target
, variant
, ID
);
1225 entry
= (TransliteratorEntry
*) registry
.get(ID
);
1228 // std::cout << ID.toUTF8String(ss) << std::endl;
1232 if (variant
.length() != 0) {
1234 // Seek exact match in hashtable
1235 entry
= findInDynamicStore(src
, trg
, variant
);
1240 // Seek exact match in locale resources
1241 entry
= findInStaticStore(src
, trg
, variant
);
1250 // Seek match in hashtable
1251 entry
= findInDynamicStore(src
, trg
, NO_VARIANT
);
1256 // Seek match in locale resources
1257 entry
= findInStaticStore(src
, trg
, NO_VARIANT
);
1261 if (!src
.hasFallback()) {
1266 if (!trg
.hasFallback()) {
1276 * Given an Entry object, instantiate it. Caller owns result. Return
1279 * Return a non-empty aliasReturn value if the ID points to an alias.
1280 * We cannot instantiate it ourselves because the alias may contain
1281 * filters or compounds, which we do not understand. Caller should
1282 * make aliasReturn empty before calling.
1284 * The entry object is assumed to reside in the dynamic store. It may be
1287 Transliterator
* TransliteratorRegistry::instantiateEntry(const UnicodeString
& ID
,
1288 TransliteratorEntry
*entry
,
1289 TransliteratorAlias
* &aliasReturn
,
1290 UErrorCode
& status
) {
1291 Transliterator
*t
= 0;
1292 U_ASSERT(aliasReturn
== 0);
1294 switch (entry
->entryType
) {
1295 case TransliteratorEntry::RBT_DATA
:
1296 t
= new RuleBasedTransliterator(ID
, entry
->u
.data
);
1298 status
= U_MEMORY_ALLOCATION_ERROR
;
1301 case TransliteratorEntry::PROTOTYPE
:
1302 t
= entry
->u
.prototype
->clone();
1304 status
= U_MEMORY_ALLOCATION_ERROR
;
1307 case TransliteratorEntry::ALIAS
:
1308 aliasReturn
= new TransliteratorAlias(entry
->stringArg
, entry
->compoundFilter
);
1309 if (aliasReturn
== 0) {
1310 status
= U_MEMORY_ALLOCATION_ERROR
;
1313 case TransliteratorEntry::FACTORY
:
1314 t
= entry
->u
.factory
.function(ID
, entry
->u
.factory
.context
);
1316 status
= U_MEMORY_ALLOCATION_ERROR
;
1319 case TransliteratorEntry::COMPOUND_RBT
:
1321 UVector
* rbts
= new UVector(entry
->u
.dataVector
->size(), status
);
1322 // Check for null pointer
1324 status
= U_MEMORY_ALLOCATION_ERROR
;
1327 int32_t passNumber
= 1;
1328 for (int32_t i
= 0; U_SUCCESS(status
) && i
< entry
->u
.dataVector
->size(); i
++) {
1329 // TODO: Should passNumber be turned into a decimal-string representation (1 -> "1")?
1330 Transliterator
* t
= new RuleBasedTransliterator(UnicodeString(CompoundTransliterator::PASS_STRING
) + UnicodeString(passNumber
++),
1331 (TransliterationRuleData
*)(entry
->u
.dataVector
->elementAt(i
)), FALSE
);
1333 status
= U_MEMORY_ALLOCATION_ERROR
;
1335 rbts
->addElement(t
, status
);
1337 if (U_FAILURE(status
)) {
1341 aliasReturn
= new TransliteratorAlias(ID
, entry
->stringArg
, rbts
, entry
->compoundFilter
);
1343 if (aliasReturn
== 0) {
1344 status
= U_MEMORY_ALLOCATION_ERROR
;
1347 case TransliteratorEntry::LOCALE_RULES
:
1348 aliasReturn
= new TransliteratorAlias(ID
, entry
->stringArg
,
1349 (UTransDirection
) entry
->intArg
);
1350 if (aliasReturn
== 0) {
1351 status
= U_MEMORY_ALLOCATION_ERROR
;
1354 case TransliteratorEntry::RULES_FORWARD
:
1355 case TransliteratorEntry::RULES_REVERSE
:
1356 // Process the rule data into a TransliteratorRuleData object,
1357 // and possibly also into an ::id header and/or footer. Then
1358 // we modify the registry with the parsed data and retry.
1360 TransliteratorParser
parser(status
);
1362 // We use the file name, taken from another resource bundle
1363 // 2-d array at static init time, as a locale language. We're
1364 // just using the locale mechanism to map through to a file
1365 // name; this in no way represents an actual locale.
1366 //CharString ch(entry->stringArg);
1367 //UResourceBundle *bundle = ures_openDirect(0, ch, &status);
1368 UnicodeString rules
= entry
->stringArg
;
1369 //ures_close(bundle);
1371 //if (U_FAILURE(status)) {
1372 // We have a failure of some kind. Remove the ID from the
1373 // registry so we don't keep trying. NOTE: This will throw off
1374 // anyone who is, at the moment, trying to iterate over the
1375 // available IDs. That's acceptable since we should never
1376 // really get here except under installation, configuration,
1377 // or unrecoverable run time memory failures.
1381 // If the status indicates a failure, then we don't have any
1382 // rules -- there is probably an installation error. The list
1383 // in the root locale should correspond to all the installed
1384 // transliterators; if it lists something that's not
1385 // installed, we'll get an error from ResourceBundle.
1386 aliasReturn
= new TransliteratorAlias(ID
, rules
,
1387 ((entry
->entryType
== TransliteratorEntry::RULES_REVERSE
) ?
1388 UTRANS_REVERSE
: UTRANS_FORWARD
));
1389 if (aliasReturn
== 0) {
1390 status
= U_MEMORY_ALLOCATION_ERROR
;
1396 U_ASSERT(FALSE
); // can't get here
1402 #endif /* #if !UCONFIG_NO_TRANSLITERATION */