]>
Commit | Line | Data |
---|---|---|
b75a7d8f A |
1 | /* |
2 | ********************************************************************** | |
73c04bcf | 3 | * Copyright (C) 1999-2006, International Business Machines |
b75a7d8f A |
4 | * Corporation and others. All Rights Reserved. |
5 | ********************************************************************** | |
6 | * Date Name Description | |
7 | * 11/17/99 aliu Creation. | |
8 | ********************************************************************** | |
9 | */ | |
10 | ||
11 | #include "unicode/utypes.h" | |
374ca955 | 12 | #include "umutex.h" |
b75a7d8f A |
13 | |
14 | #if !UCONFIG_NO_TRANSLITERATION | |
15 | ||
16 | #include "unicode/unistr.h" | |
17 | #include "unicode/uniset.h" | |
18 | #include "rbt_data.h" | |
19 | #include "hash.h" | |
20 | #include "cmemory.h" | |
21 | ||
22 | U_NAMESPACE_BEGIN | |
23 | ||
24 | TransliterationRuleData::TransliterationRuleData(UErrorCode& status) | |
73c04bcf A |
25 | : UMemory(), ruleSet(status), variableNames(status), |
26 | variables(0), variablesAreOwned(TRUE) | |
b75a7d8f A |
27 | { |
28 | if (U_FAILURE(status)) { | |
29 | return; | |
30 | } | |
73c04bcf | 31 | variableNames.setValueDeleter(uhash_deleteUnicodeString); |
b75a7d8f A |
32 | variables = 0; |
33 | variablesLength = 0; | |
34 | } | |
35 | ||
36 | TransliterationRuleData::TransliterationRuleData(const TransliterationRuleData& other) : | |
37 | UMemory(other), ruleSet(other.ruleSet), | |
73c04bcf | 38 | variablesAreOwned(TRUE), |
b75a7d8f A |
39 | variablesBase(other.variablesBase), |
40 | variablesLength(other.variablesLength) | |
41 | { | |
42 | UErrorCode status = U_ZERO_ERROR; | |
73c04bcf A |
43 | variableNames.setValueDeleter(uhash_deleteUnicodeString); |
44 | int32_t pos = -1; | |
45 | const UHashElement *e; | |
46 | while ((e = other.variableNames.nextElement(pos)) != 0) { | |
47 | UnicodeString* value = | |
48 | new UnicodeString(*(const UnicodeString*)e->value.pointer); | |
49 | variableNames.put(*(UnicodeString*)e->key.pointer, value, status); | |
b75a7d8f A |
50 | } |
51 | ||
52 | variables = 0; | |
53 | if (other.variables != 0) { | |
54 | variables = (UnicodeFunctor **)uprv_malloc(variablesLength * sizeof(UnicodeFunctor *)); | |
55 | /* test for NULL */ | |
56 | if (variables == 0) { | |
57 | status = U_MEMORY_ALLOCATION_ERROR; | |
58 | return; | |
59 | } | |
60 | for (int32_t i=0; i<variablesLength; ++i) { | |
61 | variables[i] = other.variables[i]->clone(); | |
62 | } | |
63 | } | |
64 | ||
65 | // Do this last, _after_ setting up variables[]. | |
66 | ruleSet.setData(this); // ruleSet must already be frozen | |
67 | } | |
68 | ||
69 | TransliterationRuleData::~TransliterationRuleData() { | |
73c04bcf | 70 | if (variablesAreOwned && variables != 0) { |
b75a7d8f A |
71 | for (int32_t i=0; i<variablesLength; ++i) { |
72 | delete variables[i]; | |
73 | } | |
b75a7d8f | 74 | } |
73c04bcf | 75 | uprv_free(variables); |
b75a7d8f A |
76 | } |
77 | ||
78 | UnicodeFunctor* | |
79 | TransliterationRuleData::lookup(UChar32 standIn) const { | |
80 | int32_t i = standIn - variablesBase; | |
81 | return (i >= 0 && i < variablesLength) ? variables[i] : 0; | |
82 | } | |
83 | ||
84 | UnicodeMatcher* | |
85 | TransliterationRuleData::lookupMatcher(UChar32 standIn) const { | |
86 | UnicodeFunctor *f = lookup(standIn); | |
87 | return (f != 0) ? f->toMatcher() : 0; | |
88 | } | |
89 | ||
90 | UnicodeReplacer* | |
91 | TransliterationRuleData::lookupReplacer(UChar32 standIn) const { | |
92 | UnicodeFunctor *f = lookup(standIn); | |
93 | return (f != 0) ? f->toReplacer() : 0; | |
94 | } | |
95 | ||
374ca955 | 96 | |
b75a7d8f A |
97 | U_NAMESPACE_END |
98 | ||
99 | #endif /* #if !UCONFIG_NO_TRANSLITERATION */ |