1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/intl/icu/source/i18n/rbt_data.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,117 @@ 1.4 +/* 1.5 +********************************************************************** 1.6 +* Copyright (C) 1999-2011, International Business Machines 1.7 +* Corporation and others. All Rights Reserved. 1.8 +********************************************************************** 1.9 +* Date Name Description 1.10 +* 11/17/99 aliu Creation. 1.11 +********************************************************************** 1.12 +*/ 1.13 + 1.14 +#include "unicode/utypes.h" 1.15 +#include "umutex.h" 1.16 + 1.17 +#if !UCONFIG_NO_TRANSLITERATION 1.18 + 1.19 +#include "unicode/unistr.h" 1.20 +#include "unicode/uniset.h" 1.21 +#include "rbt_data.h" 1.22 +#include "hash.h" 1.23 +#include "cmemory.h" 1.24 + 1.25 +U_NAMESPACE_BEGIN 1.26 + 1.27 +TransliterationRuleData::TransliterationRuleData(UErrorCode& status) 1.28 + : UMemory(), ruleSet(status), variableNames(status), 1.29 + variables(0), variablesAreOwned(TRUE) 1.30 +{ 1.31 + if (U_FAILURE(status)) { 1.32 + return; 1.33 + } 1.34 + variableNames.setValueDeleter(uprv_deleteUObject); 1.35 + variables = 0; 1.36 + variablesLength = 0; 1.37 +} 1.38 + 1.39 +TransliterationRuleData::TransliterationRuleData(const TransliterationRuleData& other) : 1.40 + UMemory(other), ruleSet(other.ruleSet), 1.41 + variablesAreOwned(TRUE), 1.42 + variablesBase(other.variablesBase), 1.43 + variablesLength(other.variablesLength) 1.44 +{ 1.45 + UErrorCode status = U_ZERO_ERROR; 1.46 + int32_t i = 0; 1.47 + variableNames.setValueDeleter(uprv_deleteUObject); 1.48 + int32_t pos = -1; 1.49 + const UHashElement *e; 1.50 + while ((e = other.variableNames.nextElement(pos)) != 0) { 1.51 + UnicodeString* value = 1.52 + new UnicodeString(*(const UnicodeString*)e->value.pointer); 1.53 + // Exit out if value could not be created. 1.54 + if (value == NULL) { 1.55 + return; 1.56 + } 1.57 + variableNames.put(*(UnicodeString*)e->key.pointer, value, status); 1.58 + } 1.59 + 1.60 + variables = 0; 1.61 + if (other.variables != 0) { 1.62 + variables = (UnicodeFunctor **)uprv_malloc(variablesLength * sizeof(UnicodeFunctor *)); 1.63 + /* test for NULL */ 1.64 + if (variables == 0) { 1.65 + status = U_MEMORY_ALLOCATION_ERROR; 1.66 + return; 1.67 + } 1.68 + for (i=0; i<variablesLength; ++i) { 1.69 + variables[i] = other.variables[i]->clone(); 1.70 + if (variables[i] == NULL) { 1.71 + status = U_MEMORY_ALLOCATION_ERROR; 1.72 + break; 1.73 + } 1.74 + } 1.75 + } 1.76 + // Remove the array and exit if memory allocation error occured. 1.77 + if (U_FAILURE(status)) { 1.78 + for (int32_t n = i-1; n >= 0; n++) { 1.79 + delete variables[n]; 1.80 + } 1.81 + uprv_free(variables); 1.82 + variables = NULL; 1.83 + return; 1.84 + } 1.85 + 1.86 + // Do this last, _after_ setting up variables[]. 1.87 + ruleSet.setData(this); // ruleSet must already be frozen 1.88 +} 1.89 + 1.90 +TransliterationRuleData::~TransliterationRuleData() { 1.91 + if (variablesAreOwned && variables != 0) { 1.92 + for (int32_t i=0; i<variablesLength; ++i) { 1.93 + delete variables[i]; 1.94 + } 1.95 + } 1.96 + uprv_free(variables); 1.97 +} 1.98 + 1.99 +UnicodeFunctor* 1.100 +TransliterationRuleData::lookup(UChar32 standIn) const { 1.101 + int32_t i = standIn - variablesBase; 1.102 + return (i >= 0 && i < variablesLength) ? variables[i] : 0; 1.103 +} 1.104 + 1.105 +UnicodeMatcher* 1.106 +TransliterationRuleData::lookupMatcher(UChar32 standIn) const { 1.107 + UnicodeFunctor *f = lookup(standIn); 1.108 + return (f != 0) ? f->toMatcher() : 0; 1.109 +} 1.110 + 1.111 +UnicodeReplacer* 1.112 +TransliterationRuleData::lookupReplacer(UChar32 standIn) const { 1.113 + UnicodeFunctor *f = lookup(standIn); 1.114 + return (f != 0) ? f->toReplacer() : 0; 1.115 +} 1.116 + 1.117 + 1.118 +U_NAMESPACE_END 1.119 + 1.120 +#endif /* #if !UCONFIG_NO_TRANSLITERATION */