1ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/* 2ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru********************************************************************** 354dcd9b6a06071f647dac967e9e267abb9410720Craig Cornelius* Copyright (c) 2002-2012, International Business Machines Corporation 4ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru* and others. All Rights Reserved. 5ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru********************************************************************** 6ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru* Date Name Description 7ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru* 02/04/2002 aliu Creation. 8ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru********************************************************************** 9ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru*/ 10ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 11ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "unicode/utypes.h" 12ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 13ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#if !UCONFIG_NO_TRANSLITERATION 14ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 15ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "unicode/translit.h" 16ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "unicode/uniset.h" 17ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "funcrepl.h" 18ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 19ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Querustatic const UChar AMPERSAND = 38; // '&' 20ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Querustatic const UChar OPEN[] = {40,32,0}; // "( " 21ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Querustatic const UChar CLOSE[] = {32,41,0}; // " )" 22ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 23ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruU_NAMESPACE_BEGIN 24ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 25ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruUOBJECT_DEFINE_RTTI_IMPLEMENTATION(FunctionReplacer) 26ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 27ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/** 28ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * Construct a replacer that takes the output of the given 29ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * replacer, passes it through the given transliterator, and emits 30ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * the result as output. 31ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru */ 32ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruFunctionReplacer::FunctionReplacer(Transliterator* adoptedTranslit, 33ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru UnicodeFunctor* adoptedReplacer) { 34ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru translit = adoptedTranslit; 35ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru replacer = adoptedReplacer; 36ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru} 37ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 38ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/** 39ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * Copy constructor. 40ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru */ 41ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruFunctionReplacer::FunctionReplacer(const FunctionReplacer& other) : 42ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru UnicodeFunctor(other), 43ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru UnicodeReplacer(other) 44ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru{ 45ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru translit = other.translit->clone(); 46ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru replacer = other.replacer->clone(); 47ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru} 48ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 49ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/** 50ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * Destructor 51ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru */ 52ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruFunctionReplacer::~FunctionReplacer() { 53ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru delete translit; 54ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru delete replacer; 55ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru} 56ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 57ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/** 58ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * Implement UnicodeFunctor 59ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru */ 60ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruUnicodeFunctor* FunctionReplacer::clone() const { 61ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return new FunctionReplacer(*this); 62ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru} 63ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 64ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/** 65ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * UnicodeFunctor API. Cast 'this' to a UnicodeReplacer* pointer 66ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * and return the pointer. 67ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru */ 68ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruUnicodeReplacer* FunctionReplacer::toReplacer() const { 6954dcd9b6a06071f647dac967e9e267abb9410720Craig Cornelius FunctionReplacer *nonconst_this = const_cast<FunctionReplacer *>(this); 7054dcd9b6a06071f647dac967e9e267abb9410720Craig Cornelius UnicodeReplacer *nonconst_base = static_cast<UnicodeReplacer *>(nonconst_this); 7154dcd9b6a06071f647dac967e9e267abb9410720Craig Cornelius 7254dcd9b6a06071f647dac967e9e267abb9410720Craig Cornelius return nonconst_base; 73ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru} 74ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 75ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/** 76ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * UnicodeReplacer API 77ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru */ 78ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruint32_t FunctionReplacer::replace(Replaceable& text, 79ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru int32_t start, 80ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru int32_t limit, 8185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho int32_t& cursor) 8285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho{ 83ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 84ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // First delegate to subordinate replacer 85ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru int32_t len = replacer->toReplacer()->replace(text, start, limit, cursor); 86ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru limit = start + len; 87ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 88ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // Now transliterate 89ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru limit = translit->transliterate(text, start, limit); 90ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 91ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return limit - start; 92ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru} 93ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 94ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/** 95ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * UnicodeReplacer API 96ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru */ 97ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruUnicodeString& FunctionReplacer::toReplacerPattern(UnicodeString& rule, 98ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru UBool escapeUnprintable) const { 99ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru UnicodeString str; 100ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru rule.truncate(0); 101ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru rule.append(AMPERSAND); 102ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru rule.append(translit->getID()); 103103e9ffba2cba345d0078eb8b8db33249f81840aCraig Cornelius rule.append(OPEN, 2); 104ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru rule.append(replacer->toReplacer()->toReplacerPattern(str, escapeUnprintable)); 105103e9ffba2cba345d0078eb8b8db33249f81840aCraig Cornelius rule.append(CLOSE, 2); 106ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return rule; 107ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru} 108ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 109ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/** 110ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * Implement UnicodeReplacer 111ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru */ 112ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruvoid FunctionReplacer::addReplacementSetTo(UnicodeSet& toUnionTo) const { 113ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru UnicodeSet set; 114ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru toUnionTo.addAll(translit->getTargetSet(set)); 115ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru} 116ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 117ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/** 118ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * UnicodeFunctor API 119ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru */ 120ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruvoid FunctionReplacer::setData(const TransliterationRuleData* d) { 121ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru replacer->setData(d); 122ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru} 123ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 124ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruU_NAMESPACE_END 125ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 126ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#endif /* #if !UCONFIG_NO_TRANSLITERATION */ 127ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 128ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru//eof 129