]> git.saurik.com Git - apple/icu.git/blame - icuSources/i18n/rbt_data.cpp
ICU-6.2.14.tar.gz
[apple/icu.git] / icuSources / i18n / rbt_data.cpp
CommitLineData
b75a7d8f
A
1/*
2**********************************************************************
374ca955 3* Copyright (C) 1999-2004, 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
22U_NAMESPACE_BEGIN
23
24TransliterationRuleData::TransliterationRuleData(UErrorCode& status)
25 : UMemory(), ruleSet(status),
26 variableNames(0), variables(0)
27{
28 if (U_FAILURE(status)) {
29 return;
30 }
31 variableNames = new Hashtable(status);
32 /* test for NULL */
33 if (variableNames == 0) {
34 status = U_MEMORY_ALLOCATION_ERROR;
35 return;
36 }
37 if (U_SUCCESS(status)) {
38 variableNames->setValueDeleter(uhash_deleteUnicodeString);
39 }
40 variables = 0;
41 variablesLength = 0;
42}
43
44TransliterationRuleData::TransliterationRuleData(const TransliterationRuleData& other) :
45 UMemory(other), ruleSet(other.ruleSet),
46 variablesBase(other.variablesBase),
47 variablesLength(other.variablesLength)
48{
49 UErrorCode status = U_ZERO_ERROR;
50 variableNames = new Hashtable(status);
51 if (U_SUCCESS(status)) {
52 variableNames->setValueDeleter(uhash_deleteUnicodeString);
53 int32_t pos = -1;
54 const UHashElement *e;
55 while ((e = other.variableNames->nextElement(pos)) != 0) {
56 UnicodeString* value =
57 new UnicodeString(*(const UnicodeString*)e->value.pointer);
58 variableNames->put(*(UnicodeString*)e->key.pointer, value, status);
59 }
60 }
61
62 variables = 0;
63 if (other.variables != 0) {
64 variables = (UnicodeFunctor **)uprv_malloc(variablesLength * sizeof(UnicodeFunctor *));
65 /* test for NULL */
66 if (variables == 0) {
67 status = U_MEMORY_ALLOCATION_ERROR;
68 return;
69 }
70 for (int32_t i=0; i<variablesLength; ++i) {
71 variables[i] = other.variables[i]->clone();
72 }
73 }
74
75 // Do this last, _after_ setting up variables[].
76 ruleSet.setData(this); // ruleSet must already be frozen
77}
78
79TransliterationRuleData::~TransliterationRuleData() {
80 delete variableNames;
81 if (variables != 0) {
82 for (int32_t i=0; i<variablesLength; ++i) {
83 delete variables[i];
84 }
85 uprv_free(variables);
86 }
87}
88
89UnicodeFunctor*
90TransliterationRuleData::lookup(UChar32 standIn) const {
91 int32_t i = standIn - variablesBase;
92 return (i >= 0 && i < variablesLength) ? variables[i] : 0;
93}
94
95UnicodeMatcher*
96TransliterationRuleData::lookupMatcher(UChar32 standIn) const {
97 UnicodeFunctor *f = lookup(standIn);
98 return (f != 0) ? f->toMatcher() : 0;
99}
100
101UnicodeReplacer*
102TransliterationRuleData::lookupReplacer(UChar32 standIn) const {
103 UnicodeFunctor *f = lookup(standIn);
104 return (f != 0) ? f->toReplacer() : 0;
105}
106
374ca955 107
b75a7d8f
A
108U_NAMESPACE_END
109
110#endif /* #if !UCONFIG_NO_TRANSLITERATION */