]> git.saurik.com Git - apple/icu.git/blame - icuSources/i18n/funcrepl.cpp
ICU-6.2.21.tar.gz
[apple/icu.git] / icuSources / i18n / funcrepl.cpp
CommitLineData
b75a7d8f
A
1/*
2**********************************************************************
374ca955 3* Copyright (c) 2002-2003, International Business Machines Corporation
b75a7d8f
A
4* and others. All Rights Reserved.
5**********************************************************************
6* Date Name Description
7* 02/04/2002 aliu Creation.
8**********************************************************************
9*/
10
11#include "unicode/utypes.h"
12
13#if !UCONFIG_NO_TRANSLITERATION
14
15#include "unicode/translit.h"
16#include "unicode/uniset.h"
17#include "funcrepl.h"
18
19static const UChar AMPERSAND = 38; // '&'
20static const UChar OPEN[] = {40,32,0}; // "( "
21static const UChar CLOSE[] = {32,41,0}; // " )"
22
23U_NAMESPACE_BEGIN
24
374ca955 25UOBJECT_DEFINE_RTTI_IMPLEMENTATION(FunctionReplacer)
b75a7d8f
A
26
27/**
28 * Construct a replacer that takes the output of the given
29 * replacer, passes it through the given transliterator, and emits
30 * the result as output.
31 */
32FunctionReplacer::FunctionReplacer(Transliterator* adoptedTranslit,
33 UnicodeFunctor* adoptedReplacer) {
34 translit = adoptedTranslit;
35 replacer = adoptedReplacer;
36}
37
38/**
39 * Copy constructor.
40 */
374ca955
A
41FunctionReplacer::FunctionReplacer(const FunctionReplacer& other) :
42 UnicodeFunctor(other),
43 UnicodeReplacer(other)
44{
b75a7d8f
A
45 translit = other.translit->clone();
46 replacer = other.replacer->clone();
47}
48
49/**
50 * Destructor
51 */
52FunctionReplacer::~FunctionReplacer() {
53 delete translit;
54 delete replacer;
55}
56
57/**
58 * Implement UnicodeFunctor
59 */
60UnicodeFunctor* FunctionReplacer::clone() const {
61 return new FunctionReplacer(*this);
62}
63
64/**
65 * UnicodeFunctor API. Cast 'this' to a UnicodeReplacer* pointer
66 * and return the pointer.
67 */
68UnicodeReplacer* FunctionReplacer::toReplacer() const {
69 return (UnicodeReplacer*) this;
70}
71
72/**
73 * UnicodeReplacer API
74 */
75int32_t FunctionReplacer::replace(Replaceable& text,
76 int32_t start,
77 int32_t limit,
78 int32_t& cursor) {
79
80 // First delegate to subordinate replacer
81 int32_t len = replacer->toReplacer()->replace(text, start, limit, cursor);
82 limit = start + len;
83
84 // Now transliterate
85 limit = translit->transliterate(text, start, limit);
86
87 return limit - start;
88}
89
90/**
91 * UnicodeReplacer API
92 */
93UnicodeString& FunctionReplacer::toReplacerPattern(UnicodeString& rule,
94 UBool escapeUnprintable) const {
95 UnicodeString str;
96 rule.truncate(0);
97 rule.append(AMPERSAND);
98 rule.append(translit->getID());
99 rule.append(OPEN);
100 rule.append(replacer->toReplacer()->toReplacerPattern(str, escapeUnprintable));
101 rule.append(CLOSE);
102 return rule;
103}
104
105/**
106 * Implement UnicodeReplacer
107 */
108void FunctionReplacer::addReplacementSetTo(UnicodeSet& toUnionTo) const {
109 UnicodeSet set;
110 toUnionTo.addAll(translit->getTargetSet(set));
111}
112
113/**
114 * UnicodeFunctor API
115 */
116void FunctionReplacer::setData(const TransliterationRuleData* d) {
117 replacer->setData(d);
118}
119
120U_NAMESPACE_END
121
122#endif /* #if !UCONFIG_NO_TRANSLITERATION */
123
124//eof