michael@0: /* michael@0: ********************************************************************** michael@0: * Copyright (c) 2002-2012, International Business Machines Corporation michael@0: * and others. All Rights Reserved. michael@0: ********************************************************************** michael@0: * Date Name Description michael@0: * 02/04/2002 aliu Creation. michael@0: ********************************************************************** michael@0: */ michael@0: michael@0: #include "unicode/utypes.h" michael@0: michael@0: #if !UCONFIG_NO_TRANSLITERATION michael@0: michael@0: #include "unicode/translit.h" michael@0: #include "unicode/uniset.h" michael@0: #include "funcrepl.h" michael@0: michael@0: static const UChar AMPERSAND = 38; // '&' michael@0: static const UChar OPEN[] = {40,32,0}; // "( " michael@0: static const UChar CLOSE[] = {32,41,0}; // " )" michael@0: michael@0: U_NAMESPACE_BEGIN michael@0: michael@0: UOBJECT_DEFINE_RTTI_IMPLEMENTATION(FunctionReplacer) michael@0: michael@0: /** michael@0: * Construct a replacer that takes the output of the given michael@0: * replacer, passes it through the given transliterator, and emits michael@0: * the result as output. michael@0: */ michael@0: FunctionReplacer::FunctionReplacer(Transliterator* adoptedTranslit, michael@0: UnicodeFunctor* adoptedReplacer) { michael@0: translit = adoptedTranslit; michael@0: replacer = adoptedReplacer; michael@0: } michael@0: michael@0: /** michael@0: * Copy constructor. michael@0: */ michael@0: FunctionReplacer::FunctionReplacer(const FunctionReplacer& other) : michael@0: UnicodeFunctor(other), michael@0: UnicodeReplacer(other) michael@0: { michael@0: translit = other.translit->clone(); michael@0: replacer = other.replacer->clone(); michael@0: } michael@0: michael@0: /** michael@0: * Destructor michael@0: */ michael@0: FunctionReplacer::~FunctionReplacer() { michael@0: delete translit; michael@0: delete replacer; michael@0: } michael@0: michael@0: /** michael@0: * Implement UnicodeFunctor michael@0: */ michael@0: UnicodeFunctor* FunctionReplacer::clone() const { michael@0: return new FunctionReplacer(*this); michael@0: } michael@0: michael@0: /** michael@0: * UnicodeFunctor API. Cast 'this' to a UnicodeReplacer* pointer michael@0: * and return the pointer. michael@0: */ michael@0: UnicodeReplacer* FunctionReplacer::toReplacer() const { michael@0: FunctionReplacer *nonconst_this = const_cast(this); michael@0: UnicodeReplacer *nonconst_base = static_cast(nonconst_this); michael@0: michael@0: return nonconst_base; michael@0: } michael@0: michael@0: /** michael@0: * UnicodeReplacer API michael@0: */ michael@0: int32_t FunctionReplacer::replace(Replaceable& text, michael@0: int32_t start, michael@0: int32_t limit, michael@0: int32_t& cursor) michael@0: { michael@0: michael@0: // First delegate to subordinate replacer michael@0: int32_t len = replacer->toReplacer()->replace(text, start, limit, cursor); michael@0: limit = start + len; michael@0: michael@0: // Now transliterate michael@0: limit = translit->transliterate(text, start, limit); michael@0: michael@0: return limit - start; michael@0: } michael@0: michael@0: /** michael@0: * UnicodeReplacer API michael@0: */ michael@0: UnicodeString& FunctionReplacer::toReplacerPattern(UnicodeString& rule, michael@0: UBool escapeUnprintable) const { michael@0: UnicodeString str; michael@0: rule.truncate(0); michael@0: rule.append(AMPERSAND); michael@0: rule.append(translit->getID()); michael@0: rule.append(OPEN, 2); michael@0: rule.append(replacer->toReplacer()->toReplacerPattern(str, escapeUnprintable)); michael@0: rule.append(CLOSE, 2); michael@0: return rule; michael@0: } michael@0: michael@0: /** michael@0: * Implement UnicodeReplacer michael@0: */ michael@0: void FunctionReplacer::addReplacementSetTo(UnicodeSet& toUnionTo) const { michael@0: UnicodeSet set; michael@0: toUnionTo.addAll(translit->getTargetSet(set)); michael@0: } michael@0: michael@0: /** michael@0: * UnicodeFunctor API michael@0: */ michael@0: void FunctionReplacer::setData(const TransliterationRuleData* d) { michael@0: replacer->setData(d); michael@0: } michael@0: michael@0: U_NAMESPACE_END michael@0: michael@0: #endif /* #if !UCONFIG_NO_TRANSLITERATION */ michael@0: michael@0: //eof