1 // Copyright (C) 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 **********************************************************************
5 * Copyright (c) 2002-2012, International Business Machines Corporation
6 * and others. All Rights Reserved.
7 **********************************************************************
8 * Date Name Description
9 * 02/04/2002 aliu Creation.
10 **********************************************************************
13 #include "unicode/utypes.h"
15 #if !UCONFIG_NO_TRANSLITERATION
17 #include "unicode/translit.h"
18 #include "unicode/uniset.h"
21 static const UChar AMPERSAND = 38; // '&'
22 static const UChar OPEN[] = {40,32,0}; // "( "
23 static const UChar CLOSE[] = {32,41,0}; // " )"
27 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(FunctionReplacer)
30 * Construct a replacer that takes the output of the given
31 * replacer, passes it through the given transliterator, and emits
32 * the result as output.
34 FunctionReplacer::FunctionReplacer(Transliterator* adoptedTranslit,
35 UnicodeFunctor* adoptedReplacer) {
36 translit = adoptedTranslit;
37 replacer = adoptedReplacer;
43 FunctionReplacer::FunctionReplacer(const FunctionReplacer& other) :
44 UnicodeFunctor(other),
45 UnicodeReplacer(other)
47 translit = other.translit->clone();
48 replacer = other.replacer->clone();
54 FunctionReplacer::~FunctionReplacer() {
60 * Implement UnicodeFunctor
62 UnicodeFunctor* FunctionReplacer::clone() const {
63 return new FunctionReplacer(*this);
67 * UnicodeFunctor API. Cast 'this' to a UnicodeReplacer* pointer
68 * and return the pointer.
70 UnicodeReplacer* FunctionReplacer::toReplacer() const {
71 FunctionReplacer *nonconst_this = const_cast<FunctionReplacer *>(this);
72 UnicodeReplacer *nonconst_base = static_cast<UnicodeReplacer *>(nonconst_this);
80 int32_t FunctionReplacer::replace(Replaceable& text,
86 // First delegate to subordinate replacer
87 int32_t len = replacer->toReplacer()->replace(text, start, limit, cursor);
91 limit = translit->transliterate(text, start, limit);
99 UnicodeString& FunctionReplacer::toReplacerPattern(UnicodeString& rule,
100 UBool escapeUnprintable) const {
103 rule.append(AMPERSAND);
104 rule.append(translit->getID());
105 rule.append(OPEN, 2);
106 rule.append(replacer->toReplacer()->toReplacerPattern(str, escapeUnprintable));
107 rule.append(CLOSE, 2);
112 * Implement UnicodeReplacer
114 void FunctionReplacer::addReplacementSetTo(UnicodeSet& toUnionTo) const {
116 toUnionTo.addAll(translit->getTargetSet(set));
122 void FunctionReplacer::setData(const TransliterationRuleData* d) {
123 replacer->setData(d);
128 #endif /* #if !UCONFIG_NO_TRANSLITERATION */