164339d36f8bd4db5025fe2988eda22b491a9219cFredrik Roubert// Copyright (C) 2016 and later: Unicode, Inc. and others. 264339d36f8bd4db5025fe2988eda22b491a9219cFredrik Roubert// License & terms of use: http://www.unicode.org/copyright.html 3ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/* 4ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru********************************************************************** 5f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius* Copyright (C) 1999-2014, International Business Machines 6ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru* Corporation and others. All Rights Reserved. 7ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru********************************************************************** 8ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru* Date Name Description 9ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru* 11/17/99 aliu Creation. 10ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru********************************************************************** 11ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru*/ 12ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 13ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "unicode/utypes.h" 14ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "umutex.h" 15ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 16ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#if !UCONFIG_NO_TRANSLITERATION 17ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 18ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "unicode/unistr.h" 19ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "unicode/uniset.h" 20ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "rbt_data.h" 21ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "hash.h" 22ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "cmemory.h" 23ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 24ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruU_NAMESPACE_BEGIN 25ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 26ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruTransliterationRuleData::TransliterationRuleData(UErrorCode& status) 27ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru : UMemory(), ruleSet(status), variableNames(status), 28ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru variables(0), variablesAreOwned(TRUE) 29ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru{ 30ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (U_FAILURE(status)) { 31ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return; 32ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 3383a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius variableNames.setValueDeleter(uprv_deleteUObject); 34ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru variables = 0; 35ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru variablesLength = 0; 36ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru} 37ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 38ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruTransliterationRuleData::TransliterationRuleData(const TransliterationRuleData& other) : 39ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru UMemory(other), ruleSet(other.ruleSet), 40ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru variablesAreOwned(TRUE), 41ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru variablesBase(other.variablesBase), 42ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru variablesLength(other.variablesLength) 43ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru{ 44ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru UErrorCode status = U_ZERO_ERROR; 4585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho int32_t i = 0; 4683a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius variableNames.setValueDeleter(uprv_deleteUObject); 471b7d32f919554dda9c193b32188251337bc756f1Fredrik Roubert int32_t pos = UHASH_FIRST; 48ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru const UHashElement *e; 49ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru while ((e = other.variableNames.nextElement(pos)) != 0) { 50ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru UnicodeString* value = 51ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru new UnicodeString(*(const UnicodeString*)e->value.pointer); 5285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho // Exit out if value could not be created. 5385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho if (value == NULL) { 5485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho return; 5585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho } 56ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru variableNames.put(*(UnicodeString*)e->key.pointer, value, status); 57ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 58ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 59ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru variables = 0; 60ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (other.variables != 0) { 61ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru variables = (UnicodeFunctor **)uprv_malloc(variablesLength * sizeof(UnicodeFunctor *)); 62ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* test for NULL */ 63ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (variables == 0) { 64ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru status = U_MEMORY_ALLOCATION_ERROR; 65ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return; 66ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 6785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho for (i=0; i<variablesLength; ++i) { 68ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru variables[i] = other.variables[i]->clone(); 6985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho if (variables[i] == NULL) { 7085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho status = U_MEMORY_ALLOCATION_ERROR; 7185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho break; 7285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho } 73ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 7485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho } 7585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho // Remove the array and exit if memory allocation error occured. 7685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho if (U_FAILURE(status)) { 77f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius for (int32_t n = i-1; n >= 0; n--) { 7885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho delete variables[n]; 7985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho } 8085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho uprv_free(variables); 8185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho variables = NULL; 8285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho return; 8385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho } 84ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 85ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // Do this last, _after_ setting up variables[]. 86ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru ruleSet.setData(this); // ruleSet must already be frozen 87ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru} 88ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 89ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruTransliterationRuleData::~TransliterationRuleData() { 90ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (variablesAreOwned && variables != 0) { 91ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru for (int32_t i=0; i<variablesLength; ++i) { 92ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru delete variables[i]; 93ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 94ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 95ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru uprv_free(variables); 96ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru} 97ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 98ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruUnicodeFunctor* 99ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruTransliterationRuleData::lookup(UChar32 standIn) const { 100ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru int32_t i = standIn - variablesBase; 101ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return (i >= 0 && i < variablesLength) ? variables[i] : 0; 102ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru} 103ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 104ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruUnicodeMatcher* 105ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruTransliterationRuleData::lookupMatcher(UChar32 standIn) const { 106ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru UnicodeFunctor *f = lookup(standIn); 107ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return (f != 0) ? f->toMatcher() : 0; 108ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru} 109ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 110ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruUnicodeReplacer* 111ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruTransliterationRuleData::lookupReplacer(UChar32 standIn) const { 112ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru UnicodeFunctor *f = lookup(standIn); 113ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return (f != 0) ? f->toReplacer() : 0; 114ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru} 115ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 116ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 117ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruU_NAMESPACE_END 118ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 119ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#endif /* #if !UCONFIG_NO_TRANSLITERATION */ 120