1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/intl/icu/source/i18n/funcrepl.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,128 @@ 1.4 +/* 1.5 +********************************************************************** 1.6 +* Copyright (c) 2002-2012, International Business Machines Corporation 1.7 +* and others. All Rights Reserved. 1.8 +********************************************************************** 1.9 +* Date Name Description 1.10 +* 02/04/2002 aliu Creation. 1.11 +********************************************************************** 1.12 +*/ 1.13 + 1.14 +#include "unicode/utypes.h" 1.15 + 1.16 +#if !UCONFIG_NO_TRANSLITERATION 1.17 + 1.18 +#include "unicode/translit.h" 1.19 +#include "unicode/uniset.h" 1.20 +#include "funcrepl.h" 1.21 + 1.22 +static const UChar AMPERSAND = 38; // '&' 1.23 +static const UChar OPEN[] = {40,32,0}; // "( " 1.24 +static const UChar CLOSE[] = {32,41,0}; // " )" 1.25 + 1.26 +U_NAMESPACE_BEGIN 1.27 + 1.28 +UOBJECT_DEFINE_RTTI_IMPLEMENTATION(FunctionReplacer) 1.29 + 1.30 +/** 1.31 + * Construct a replacer that takes the output of the given 1.32 + * replacer, passes it through the given transliterator, and emits 1.33 + * the result as output. 1.34 + */ 1.35 +FunctionReplacer::FunctionReplacer(Transliterator* adoptedTranslit, 1.36 + UnicodeFunctor* adoptedReplacer) { 1.37 + translit = adoptedTranslit; 1.38 + replacer = adoptedReplacer; 1.39 +} 1.40 + 1.41 +/** 1.42 + * Copy constructor. 1.43 + */ 1.44 +FunctionReplacer::FunctionReplacer(const FunctionReplacer& other) : 1.45 + UnicodeFunctor(other), 1.46 + UnicodeReplacer(other) 1.47 +{ 1.48 + translit = other.translit->clone(); 1.49 + replacer = other.replacer->clone(); 1.50 +} 1.51 + 1.52 +/** 1.53 + * Destructor 1.54 + */ 1.55 +FunctionReplacer::~FunctionReplacer() { 1.56 + delete translit; 1.57 + delete replacer; 1.58 +} 1.59 + 1.60 +/** 1.61 + * Implement UnicodeFunctor 1.62 + */ 1.63 +UnicodeFunctor* FunctionReplacer::clone() const { 1.64 + return new FunctionReplacer(*this); 1.65 +} 1.66 + 1.67 +/** 1.68 + * UnicodeFunctor API. Cast 'this' to a UnicodeReplacer* pointer 1.69 + * and return the pointer. 1.70 + */ 1.71 +UnicodeReplacer* FunctionReplacer::toReplacer() const { 1.72 + FunctionReplacer *nonconst_this = const_cast<FunctionReplacer *>(this); 1.73 + UnicodeReplacer *nonconst_base = static_cast<UnicodeReplacer *>(nonconst_this); 1.74 + 1.75 + return nonconst_base; 1.76 +} 1.77 + 1.78 +/** 1.79 + * UnicodeReplacer API 1.80 + */ 1.81 +int32_t FunctionReplacer::replace(Replaceable& text, 1.82 + int32_t start, 1.83 + int32_t limit, 1.84 + int32_t& cursor) 1.85 +{ 1.86 + 1.87 + // First delegate to subordinate replacer 1.88 + int32_t len = replacer->toReplacer()->replace(text, start, limit, cursor); 1.89 + limit = start + len; 1.90 + 1.91 + // Now transliterate 1.92 + limit = translit->transliterate(text, start, limit); 1.93 + 1.94 + return limit - start; 1.95 +} 1.96 + 1.97 +/** 1.98 + * UnicodeReplacer API 1.99 + */ 1.100 +UnicodeString& FunctionReplacer::toReplacerPattern(UnicodeString& rule, 1.101 + UBool escapeUnprintable) const { 1.102 + UnicodeString str; 1.103 + rule.truncate(0); 1.104 + rule.append(AMPERSAND); 1.105 + rule.append(translit->getID()); 1.106 + rule.append(OPEN, 2); 1.107 + rule.append(replacer->toReplacer()->toReplacerPattern(str, escapeUnprintable)); 1.108 + rule.append(CLOSE, 2); 1.109 + return rule; 1.110 +} 1.111 + 1.112 +/** 1.113 + * Implement UnicodeReplacer 1.114 + */ 1.115 +void FunctionReplacer::addReplacementSetTo(UnicodeSet& toUnionTo) const { 1.116 + UnicodeSet set; 1.117 + toUnionTo.addAll(translit->getTargetSet(set)); 1.118 +} 1.119 + 1.120 +/** 1.121 + * UnicodeFunctor API 1.122 + */ 1.123 +void FunctionReplacer::setData(const TransliterationRuleData* d) { 1.124 + replacer->setData(d); 1.125 +} 1.126 + 1.127 +U_NAMESPACE_END 1.128 + 1.129 +#endif /* #if !UCONFIG_NO_TRANSLITERATION */ 1.130 + 1.131 +//eof