1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 **********************************************************************
5 * Copyright (C) 1999-2014, International Business Machines
6 * Corporation and others. All Rights Reserved.
7 **********************************************************************
8 * Date Name Description
9 * 11/17/99 aliu Creation.
10 **********************************************************************
13 #include "unicode/utypes.h"
16 #if !UCONFIG_NO_TRANSLITERATION
18 #include "unicode/unistr.h"
19 #include "unicode/uniset.h"
26 TransliterationRuleData::TransliterationRuleData(UErrorCode
& status
)
27 : UMemory(), ruleSet(status
), variableNames(status
),
28 variables(0), variablesAreOwned(TRUE
)
30 if (U_FAILURE(status
)) {
33 variableNames
.setValueDeleter(uprv_deleteUObject
);
38 TransliterationRuleData::TransliterationRuleData(const TransliterationRuleData
& other
) :
39 UMemory(other
), ruleSet(other
.ruleSet
),
40 variablesAreOwned(TRUE
),
41 variablesBase(other
.variablesBase
),
42 variablesLength(other
.variablesLength
)
44 UErrorCode status
= U_ZERO_ERROR
;
46 variableNames
.setValueDeleter(uprv_deleteUObject
);
47 int32_t pos
= UHASH_FIRST
;
48 const UHashElement
*e
;
49 while ((e
= other
.variableNames
.nextElement(pos
)) != 0) {
50 UnicodeString
* value
=
51 new UnicodeString(*(const UnicodeString
*)e
->value
.pointer
);
52 // Exit out if value could not be created.
56 variableNames
.put(*(UnicodeString
*)e
->key
.pointer
, value
, status
);
60 if (other
.variables
!= 0) {
61 variables
= (UnicodeFunctor
**)uprv_malloc(variablesLength
* sizeof(UnicodeFunctor
*));
64 status
= U_MEMORY_ALLOCATION_ERROR
;
67 for (i
=0; i
<variablesLength
; ++i
) {
68 variables
[i
] = other
.variables
[i
]->clone();
69 if (variables
[i
] == NULL
) {
70 status
= U_MEMORY_ALLOCATION_ERROR
;
75 // Remove the array and exit if memory allocation error occured.
76 if (U_FAILURE(status
)) {
77 for (int32_t n
= i
-1; n
>= 0; n
--) {
85 // Do this last, _after_ setting up variables[].
86 ruleSet
.setData(this); // ruleSet must already be frozen
89 TransliterationRuleData::~TransliterationRuleData() {
90 if (variablesAreOwned
&& variables
!= 0) {
91 for (int32_t i
=0; i
<variablesLength
; ++i
) {
99 TransliterationRuleData::lookup(UChar32 standIn
) const {
100 int32_t i
= standIn
- variablesBase
;
101 return (i
>= 0 && i
< variablesLength
) ? variables
[i
] : 0;
105 TransliterationRuleData::lookupMatcher(UChar32 standIn
) const {
106 UnicodeFunctor
*f
= lookup(standIn
);
107 return (f
!= 0) ? f
->toMatcher() : 0;
111 TransliterationRuleData::lookupReplacer(UChar32 standIn
) const {
112 UnicodeFunctor
*f
= lookup(standIn
);
113 return (f
!= 0) ? f
->toReplacer() : 0;
119 #endif /* #if !UCONFIG_NO_TRANSLITERATION */