]>
Commit | Line | Data |
---|---|---|
f3c0d7a5 A |
1 | // © 2016 and later: Unicode, Inc. and others. |
2 | // License & terms of use: http://www.unicode.org/copyright.html | |
b75a7d8f A |
3 | /* |
4 | ********************************************************************** | |
b331163b | 5 | * Copyright (C) 1999-2014, International Business Machines |
b75a7d8f A |
6 | * Corporation and others. All Rights Reserved. |
7 | ********************************************************************** | |
8 | * Date Name Description | |
9 | * 11/17/99 aliu Creation. | |
10 | ********************************************************************** | |
11 | */ | |
12 | ||
13 | #include "unicode/utypes.h" | |
374ca955 | 14 | #include "umutex.h" |
b75a7d8f A |
15 | |
16 | #if !UCONFIG_NO_TRANSLITERATION | |
17 | ||
18 | #include "unicode/unistr.h" | |
19 | #include "unicode/uniset.h" | |
20 | #include "rbt_data.h" | |
21 | #include "hash.h" | |
22 | #include "cmemory.h" | |
23 | ||
24 | U_NAMESPACE_BEGIN | |
25 | ||
26 | TransliterationRuleData::TransliterationRuleData(UErrorCode& status) | |
73c04bcf A |
27 | : UMemory(), ruleSet(status), variableNames(status), |
28 | variables(0), variablesAreOwned(TRUE) | |
b75a7d8f A |
29 | { |
30 | if (U_FAILURE(status)) { | |
31 | return; | |
32 | } | |
4388f060 | 33 | variableNames.setValueDeleter(uprv_deleteUObject); |
b75a7d8f A |
34 | variables = 0; |
35 | variablesLength = 0; | |
36 | } | |
37 | ||
38 | TransliterationRuleData::TransliterationRuleData(const TransliterationRuleData& other) : | |
39 | UMemory(other), ruleSet(other.ruleSet), | |
73c04bcf | 40 | variablesAreOwned(TRUE), |
b75a7d8f A |
41 | variablesBase(other.variablesBase), |
42 | variablesLength(other.variablesLength) | |
43 | { | |
44 | UErrorCode status = U_ZERO_ERROR; | |
46f4442e | 45 | int32_t i = 0; |
4388f060 | 46 | variableNames.setValueDeleter(uprv_deleteUObject); |
b331163b | 47 | int32_t pos = UHASH_FIRST; |
73c04bcf A |
48 | const UHashElement *e; |
49 | while ((e = other.variableNames.nextElement(pos)) != 0) { | |
50 | UnicodeString* value = | |
51 | new UnicodeString(*(const UnicodeString*)e->value.pointer); | |
46f4442e A |
52 | // Exit out if value could not be created. |
53 | if (value == NULL) { | |
54 | return; | |
55 | } | |
73c04bcf | 56 | variableNames.put(*(UnicodeString*)e->key.pointer, value, status); |
b75a7d8f A |
57 | } |
58 | ||
59 | variables = 0; | |
60 | if (other.variables != 0) { | |
61 | variables = (UnicodeFunctor **)uprv_malloc(variablesLength * sizeof(UnicodeFunctor *)); | |
62 | /* test for NULL */ | |
63 | if (variables == 0) { | |
64 | status = U_MEMORY_ALLOCATION_ERROR; | |
65 | return; | |
66 | } | |
46f4442e | 67 | for (i=0; i<variablesLength; ++i) { |
b75a7d8f | 68 | variables[i] = other.variables[i]->clone(); |
46f4442e A |
69 | if (variables[i] == NULL) { |
70 | status = U_MEMORY_ALLOCATION_ERROR; | |
71 | break; | |
72 | } | |
b75a7d8f | 73 | } |
46f4442e A |
74 | } |
75 | // Remove the array and exit if memory allocation error occured. | |
76 | if (U_FAILURE(status)) { | |
b331163b | 77 | for (int32_t n = i-1; n >= 0; n--) { |
46f4442e A |
78 | delete variables[n]; |
79 | } | |
80 | uprv_free(variables); | |
81 | variables = NULL; | |
82 | return; | |
83 | } | |
b75a7d8f A |
84 | |
85 | // Do this last, _after_ setting up variables[]. | |
86 | ruleSet.setData(this); // ruleSet must already be frozen | |
87 | } | |
88 | ||
89 | TransliterationRuleData::~TransliterationRuleData() { | |
73c04bcf | 90 | if (variablesAreOwned && variables != 0) { |
b75a7d8f A |
91 | for (int32_t i=0; i<variablesLength; ++i) { |
92 | delete variables[i]; | |
93 | } | |
b75a7d8f | 94 | } |
73c04bcf | 95 | uprv_free(variables); |
b75a7d8f A |
96 | } |
97 | ||
98 | UnicodeFunctor* | |
99 | TransliterationRuleData::lookup(UChar32 standIn) const { | |
100 | int32_t i = standIn - variablesBase; | |
101 | return (i >= 0 && i < variablesLength) ? variables[i] : 0; | |
102 | } | |
103 | ||
104 | UnicodeMatcher* | |
105 | TransliterationRuleData::lookupMatcher(UChar32 standIn) const { | |
106 | UnicodeFunctor *f = lookup(standIn); | |
107 | return (f != 0) ? f->toMatcher() : 0; | |
108 | } | |
109 | ||
110 | UnicodeReplacer* | |
111 | TransliterationRuleData::lookupReplacer(UChar32 standIn) const { | |
112 | UnicodeFunctor *f = lookup(standIn); | |
113 | return (f != 0) ? f->toReplacer() : 0; | |
114 | } | |
115 | ||
374ca955 | 116 | |
b75a7d8f A |
117 | U_NAMESPACE_END |
118 | ||
119 | #endif /* #if !UCONFIG_NO_TRANSLITERATION */ |