1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /*
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 **********************************************************************
11 */
12
13 #include "unicode/utypes.h"
14
15 #if !UCONFIG_NO_TRANSLITERATION
16
17 #include "unicode/translit.h"
18 #include "unicode/uniset.h"
19 #include "funcrepl.h"
20
21 static const UChar AMPERSAND = 38; // '&'
22 static const UChar OPEN[] = {40,32,0}; // "( "
23 static const UChar CLOSE[] = {32,41,0}; // " )"
24
25 U_NAMESPACE_BEGIN
26
UOBJECT_DEFINE_RTTI_IMPLEMENTATION(FunctionReplacer)27 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(FunctionReplacer)
28
29 /**
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.
33 */
34 FunctionReplacer::FunctionReplacer(Transliterator* adoptedTranslit,
35 UnicodeFunctor* adoptedReplacer) {
36 translit = adoptedTranslit;
37 replacer = adoptedReplacer;
38 }
39
40 /**
41 * Copy constructor.
42 */
FunctionReplacer(const FunctionReplacer & other)43 FunctionReplacer::FunctionReplacer(const FunctionReplacer& other) :
44 UnicodeFunctor(other),
45 UnicodeReplacer(other)
46 {
47 translit = other.translit->clone();
48 replacer = other.replacer->clone();
49 }
50
51 /**
52 * Destructor
53 */
~FunctionReplacer()54 FunctionReplacer::~FunctionReplacer() {
55 delete translit;
56 delete replacer;
57 }
58
59 /**
60 * Implement UnicodeFunctor
61 */
clone() const62 UnicodeFunctor* FunctionReplacer::clone() const {
63 return new FunctionReplacer(*this);
64 }
65
66 /**
67 * UnicodeFunctor API. Cast 'this' to a UnicodeReplacer* pointer
68 * and return the pointer.
69 */
toReplacer() const70 UnicodeReplacer* FunctionReplacer::toReplacer() const {
71 FunctionReplacer *nonconst_this = const_cast<FunctionReplacer *>(this);
72 UnicodeReplacer *nonconst_base = static_cast<UnicodeReplacer *>(nonconst_this);
73
74 return nonconst_base;
75 }
76
77 /**
78 * UnicodeReplacer API
79 */
replace(Replaceable & text,int32_t start,int32_t limit,int32_t & cursor)80 int32_t FunctionReplacer::replace(Replaceable& text,
81 int32_t start,
82 int32_t limit,
83 int32_t& cursor)
84 {
85
86 // First delegate to subordinate replacer
87 int32_t len = replacer->toReplacer()->replace(text, start, limit, cursor);
88 limit = start + len;
89
90 // Now transliterate
91 limit = translit->transliterate(text, start, limit);
92
93 return limit - start;
94 }
95
96 /**
97 * UnicodeReplacer API
98 */
toReplacerPattern(UnicodeString & rule,UBool escapeUnprintable) const99 UnicodeString& FunctionReplacer::toReplacerPattern(UnicodeString& rule,
100 UBool escapeUnprintable) const {
101 UnicodeString str;
102 rule.truncate(0);
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);
108 return rule;
109 }
110
111 /**
112 * Implement UnicodeReplacer
113 */
addReplacementSetTo(UnicodeSet & toUnionTo) const114 void FunctionReplacer::addReplacementSetTo(UnicodeSet& toUnionTo) const {
115 UnicodeSet set;
116 toUnionTo.addAll(translit->getTargetSet(set));
117 }
118
119 /**
120 * UnicodeFunctor API
121 */
setData(const TransliterationRuleData * d)122 void FunctionReplacer::setData(const TransliterationRuleData* d) {
123 replacer->setData(d);
124 }
125
126 U_NAMESPACE_END
127
128 #endif /* #if !UCONFIG_NO_TRANSLITERATION */
129
130 //eof
131