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