]> git.saurik.com Git - apple/icu.git/blame - icuSources/i18n/funcrepl.cpp
ICU-64243.0.1.tar.gz
[apple/icu.git] / icuSources / i18n / funcrepl.cpp
CommitLineData
f3c0d7a5
A
1// © 2016 and later: Unicode, Inc. and others.
2// License & terms of use: http://www.unicode.org/copyright.html
b75a7d8f
A
3/*
4**********************************************************************
51004dcb 5* Copyright (c) 2002-2012, International Business Machines Corporation
b75a7d8f
A
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
21static const UChar AMPERSAND = 38; // '&'
22static const UChar OPEN[] = {40,32,0}; // "( "
23static const UChar CLOSE[] = {32,41,0}; // " )"
24
25U_NAMESPACE_BEGIN
26
374ca955 27UOBJECT_DEFINE_RTTI_IMPLEMENTATION(FunctionReplacer)
b75a7d8f
A
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 */
34FunctionReplacer::FunctionReplacer(Transliterator* adoptedTranslit,
35 UnicodeFunctor* adoptedReplacer) {
36 translit = adoptedTranslit;
37 replacer = adoptedReplacer;
38}
39
40/**
41 * Copy constructor.
42 */
374ca955
A
43FunctionReplacer::FunctionReplacer(const FunctionReplacer& other) :
44 UnicodeFunctor(other),
45 UnicodeReplacer(other)
46{
b75a7d8f
A
47 translit = other.translit->clone();
48 replacer = other.replacer->clone();
49}
50
51/**
52 * Destructor
53 */
54FunctionReplacer::~FunctionReplacer() {
55 delete translit;
56 delete replacer;
57}
58
59/**
60 * Implement UnicodeFunctor
61 */
62UnicodeFunctor* 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 */
70UnicodeReplacer* FunctionReplacer::toReplacer() const {
51004dcb
A
71 FunctionReplacer *nonconst_this = const_cast<FunctionReplacer *>(this);
72 UnicodeReplacer *nonconst_base = static_cast<UnicodeReplacer *>(nonconst_this);
73
74 return nonconst_base;
b75a7d8f
A
75}
76
77/**
78 * UnicodeReplacer API
79 */
80int32_t FunctionReplacer::replace(Replaceable& text,
81 int32_t start,
82 int32_t limit,
46f4442e
A
83 int32_t& cursor)
84{
b75a7d8f
A
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 */
99UnicodeString& FunctionReplacer::toReplacerPattern(UnicodeString& rule,
100 UBool escapeUnprintable) const {
101 UnicodeString str;
102 rule.truncate(0);
103 rule.append(AMPERSAND);
104 rule.append(translit->getID());
4388f060 105 rule.append(OPEN, 2);
b75a7d8f 106 rule.append(replacer->toReplacer()->toReplacerPattern(str, escapeUnprintable));
4388f060 107 rule.append(CLOSE, 2);
b75a7d8f
A
108 return rule;
109}
110
111/**
112 * Implement UnicodeReplacer
113 */
114void FunctionReplacer::addReplacementSetTo(UnicodeSet& toUnionTo) const {
115 UnicodeSet set;
116 toUnionTo.addAll(translit->getTargetSet(set));
117}
118
119/**
120 * UnicodeFunctor API
121 */
122void FunctionReplacer::setData(const TransliterationRuleData* d) {
123 replacer->setData(d);
124}
125
126U_NAMESPACE_END
127
128#endif /* #if !UCONFIG_NO_TRANSLITERATION */
129
130//eof