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