2 **********************************************************************
3 * Copyright (C) 1999-2011, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 **********************************************************************
6 * Date Name Description
7 * 11/17/99 aliu Creation.
8 **********************************************************************
11 #include "unicode/utypes.h"
14 #if !UCONFIG_NO_TRANSLITERATION
16 #include "unicode/unistr.h"
17 #include "unicode/uniset.h"
24 TransliterationRuleData::TransliterationRuleData(UErrorCode
& status
)
25 : UMemory(), ruleSet(status
), variableNames(status
),
26 variables(0), variablesAreOwned(TRUE
)
28 if (U_FAILURE(status
)) {
31 variableNames
.setValueDeleter(uprv_deleteUObject
);
36 TransliterationRuleData::TransliterationRuleData(const TransliterationRuleData
& other
) :
37 UMemory(other
), ruleSet(other
.ruleSet
),
38 variablesAreOwned(TRUE
),
39 variablesBase(other
.variablesBase
),
40 variablesLength(other
.variablesLength
)
42 UErrorCode status
= U_ZERO_ERROR
;
44 variableNames
.setValueDeleter(uprv_deleteUObject
);
46 const UHashElement
*e
;
47 while ((e
= other
.variableNames
.nextElement(pos
)) != 0) {
48 UnicodeString
* value
=
49 new UnicodeString(*(const UnicodeString
*)e
->value
.pointer
);
50 // Exit out if value could not be created.
54 variableNames
.put(*(UnicodeString
*)e
->key
.pointer
, value
, status
);
58 if (other
.variables
!= 0) {
59 variables
= (UnicodeFunctor
**)uprv_malloc(variablesLength
* sizeof(UnicodeFunctor
*));
62 status
= U_MEMORY_ALLOCATION_ERROR
;
65 for (i
=0; i
<variablesLength
; ++i
) {
66 variables
[i
] = other
.variables
[i
]->clone();
67 if (variables
[i
] == NULL
) {
68 status
= U_MEMORY_ALLOCATION_ERROR
;
73 // Remove the array and exit if memory allocation error occured.
74 if (U_FAILURE(status
)) {
75 for (int32_t n
= i
-1; n
>= 0; n
++) {
83 // Do this last, _after_ setting up variables[].
84 ruleSet
.setData(this); // ruleSet must already be frozen
87 TransliterationRuleData::~TransliterationRuleData() {
88 if (variablesAreOwned
&& variables
!= 0) {
89 for (int32_t i
=0; i
<variablesLength
; ++i
) {
97 TransliterationRuleData::lookup(UChar32 standIn
) const {
98 int32_t i
= standIn
- variablesBase
;
99 return (i
>= 0 && i
< variablesLength
) ? variables
[i
] : 0;
103 TransliterationRuleData::lookupMatcher(UChar32 standIn
) const {
104 UnicodeFunctor
*f
= lookup(standIn
);
105 return (f
!= 0) ? f
->toMatcher() : 0;
109 TransliterationRuleData::lookupReplacer(UChar32 standIn
) const {
110 UnicodeFunctor
*f
= lookup(standIn
);
111 return (f
!= 0) ? f
->toReplacer() : 0;
117 #endif /* #if !UCONFIG_NO_TRANSLITERATION */