]> git.saurik.com Git - apple/icu.git/blob - icuSources/i18n/rbt_data.cpp
ICU-8.11.tar.gz
[apple/icu.git] / icuSources / i18n / rbt_data.cpp
1 /*
2 **********************************************************************
3 * Copyright (C) 1999-2006, International Business Machines
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"
12 #include "umutex.h"
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)
25 : UMemory(), ruleSet(status), variableNames(status),
26 variables(0), variablesAreOwned(TRUE)
27 {
28 if (U_FAILURE(status)) {
29 return;
30 }
31 variableNames.setValueDeleter(uhash_deleteUnicodeString);
32 variables = 0;
33 variablesLength = 0;
34 }
35
36 TransliterationRuleData::TransliterationRuleData(const TransliterationRuleData& other) :
37 UMemory(other), ruleSet(other.ruleSet),
38 variablesAreOwned(TRUE),
39 variablesBase(other.variablesBase),
40 variablesLength(other.variablesLength)
41 {
42 UErrorCode status = U_ZERO_ERROR;
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);
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() {
70 if (variablesAreOwned && variables != 0) {
71 for (int32_t i=0; i<variablesLength; ++i) {
72 delete variables[i];
73 }
74 }
75 uprv_free(variables);
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
96
97 U_NAMESPACE_END
98
99 #endif /* #if !UCONFIG_NO_TRANSLITERATION */