intl/icu/source/i18n/funcrepl.cpp

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     1 /*
     2 **********************************************************************
     3 *   Copyright (c) 2002-2012, International Business Machines Corporation
     4 *   and others.  All Rights Reserved.
     5 **********************************************************************
     6 *   Date        Name        Description
     7 *   02/04/2002  aliu        Creation.
     8 **********************************************************************
     9 */
    11 #include "unicode/utypes.h"
    13 #if !UCONFIG_NO_TRANSLITERATION
    15 #include "unicode/translit.h"
    16 #include "unicode/uniset.h"
    17 #include "funcrepl.h"
    19 static const UChar AMPERSAND = 38; // '&'
    20 static const UChar OPEN[]    = {40,32,0}; // "( "
    21 static const UChar CLOSE[]   = {32,41,0}; // " )"
    23 U_NAMESPACE_BEGIN
    25 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(FunctionReplacer)
    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  */
    32 FunctionReplacer::FunctionReplacer(Transliterator* adoptedTranslit,
    33                                    UnicodeFunctor* adoptedReplacer) {
    34     translit = adoptedTranslit;
    35     replacer = adoptedReplacer;
    36 }
    38 /**
    39  * Copy constructor.
    40  */
    41 FunctionReplacer::FunctionReplacer(const FunctionReplacer& other) :
    42     UnicodeFunctor(other),
    43     UnicodeReplacer(other)
    44 {
    45     translit = other.translit->clone();
    46     replacer = other.replacer->clone();
    47 }
    49 /**
    50  * Destructor
    51  */
    52 FunctionReplacer::~FunctionReplacer() {
    53     delete translit;
    54     delete replacer;
    55 }
    57 /**
    58  * Implement UnicodeFunctor
    59  */
    60 UnicodeFunctor* FunctionReplacer::clone() const {
    61     return new FunctionReplacer(*this);
    62 }
    64 /**
    65  * UnicodeFunctor API.  Cast 'this' to a UnicodeReplacer* pointer
    66  * and return the pointer.
    67  */
    68 UnicodeReplacer* FunctionReplacer::toReplacer() const {
    69   FunctionReplacer  *nonconst_this = const_cast<FunctionReplacer *>(this);
    70   UnicodeReplacer *nonconst_base = static_cast<UnicodeReplacer *>(nonconst_this);
    72   return nonconst_base;
    73 }
    75 /**
    76  * UnicodeReplacer API
    77  */
    78 int32_t FunctionReplacer::replace(Replaceable& text,
    79                                   int32_t start,
    80                                   int32_t limit,
    81                                   int32_t& cursor)
    82 {
    84     // First delegate to subordinate replacer
    85     int32_t len = replacer->toReplacer()->replace(text, start, limit, cursor);
    86     limit = start + len;
    88     // Now transliterate
    89     limit = translit->transliterate(text, start, limit);
    91     return limit - start;
    92 }
    94 /**
    95  * UnicodeReplacer API
    96  */
    97 UnicodeString& FunctionReplacer::toReplacerPattern(UnicodeString& rule,
    98                                                    UBool escapeUnprintable) const {
    99     UnicodeString str;
   100     rule.truncate(0);
   101     rule.append(AMPERSAND);
   102     rule.append(translit->getID());
   103     rule.append(OPEN, 2);
   104     rule.append(replacer->toReplacer()->toReplacerPattern(str, escapeUnprintable));
   105     rule.append(CLOSE, 2);
   106     return rule;
   107 }
   109 /**
   110  * Implement UnicodeReplacer
   111  */
   112 void FunctionReplacer::addReplacementSetTo(UnicodeSet& toUnionTo) const {
   113     UnicodeSet set;
   114     toUnionTo.addAll(translit->getTargetSet(set));
   115 }
   117 /**
   118  * UnicodeFunctor API
   119  */
   120 void FunctionReplacer::setData(const TransliterationRuleData* d) {
   121     replacer->setData(d);
   122 }
   124 U_NAMESPACE_END
   126 #endif /* #if !UCONFIG_NO_TRANSLITERATION */
   128 //eof

mercurial