2002-02-07 01:07:55 +00:00
|
|
|
/*
|
|
|
|
**********************************************************************
|
2008-01-11 19:55:29 +00:00
|
|
|
* Copyright (c) 2002-2008, International Business Machines Corporation
|
2002-02-07 01:07:55 +00:00
|
|
|
* and others. All Rights Reserved.
|
|
|
|
**********************************************************************
|
|
|
|
* Date Name Description
|
|
|
|
* 02/04/2002 aliu Creation.
|
|
|
|
**********************************************************************
|
|
|
|
*/
|
2002-09-20 01:54:48 +00:00
|
|
|
|
|
|
|
#include "unicode/utypes.h"
|
|
|
|
|
|
|
|
#if !UCONFIG_NO_TRANSLITERATION
|
|
|
|
|
2002-02-07 01:07:55 +00:00
|
|
|
#include "unicode/translit.h"
|
2002-06-28 21:13:54 +00:00
|
|
|
#include "unicode/uniset.h"
|
2002-07-12 21:42:24 +00:00
|
|
|
#include "funcrepl.h"
|
2002-02-07 01:07:55 +00:00
|
|
|
|
|
|
|
static const UChar AMPERSAND = 38; // '&'
|
|
|
|
static const UChar OPEN[] = {40,32,0}; // "( "
|
|
|
|
static const UChar CLOSE[] = {32,41,0}; // " )"
|
|
|
|
|
|
|
|
U_NAMESPACE_BEGIN
|
|
|
|
|
2003-08-31 20:53:46 +00:00
|
|
|
UOBJECT_DEFINE_RTTI_IMPLEMENTATION(FunctionReplacer)
|
2002-06-29 00:04:16 +00:00
|
|
|
|
2002-02-07 01:07:55 +00:00
|
|
|
/**
|
|
|
|
* Construct a replacer that takes the output of the given
|
|
|
|
* replacer, passes it through the given transliterator, and emits
|
|
|
|
* the result as output.
|
|
|
|
*/
|
|
|
|
FunctionReplacer::FunctionReplacer(Transliterator* adoptedTranslit,
|
|
|
|
UnicodeFunctor* adoptedReplacer) {
|
|
|
|
translit = adoptedTranslit;
|
|
|
|
replacer = adoptedReplacer;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Copy constructor.
|
|
|
|
*/
|
2003-12-01 05:33:41 +00:00
|
|
|
FunctionReplacer::FunctionReplacer(const FunctionReplacer& other) :
|
|
|
|
UnicodeFunctor(other),
|
|
|
|
UnicodeReplacer(other)
|
|
|
|
{
|
2002-02-07 01:07:55 +00:00
|
|
|
translit = other.translit->clone();
|
|
|
|
replacer = other.replacer->clone();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Destructor
|
|
|
|
*/
|
|
|
|
FunctionReplacer::~FunctionReplacer() {
|
|
|
|
delete translit;
|
|
|
|
delete replacer;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Implement UnicodeFunctor
|
|
|
|
*/
|
|
|
|
UnicodeFunctor* FunctionReplacer::clone() const {
|
|
|
|
return new FunctionReplacer(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* UnicodeFunctor API. Cast 'this' to a UnicodeReplacer* pointer
|
|
|
|
* and return the pointer.
|
|
|
|
*/
|
|
|
|
UnicodeReplacer* FunctionReplacer::toReplacer() const {
|
|
|
|
return (UnicodeReplacer*) this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* UnicodeReplacer API
|
|
|
|
*/
|
|
|
|
int32_t FunctionReplacer::replace(Replaceable& text,
|
|
|
|
int32_t start,
|
|
|
|
int32_t limit,
|
2008-02-23 19:15:18 +00:00
|
|
|
int32_t& cursor)
|
|
|
|
{
|
2002-02-07 01:07:55 +00:00
|
|
|
|
|
|
|
// First delegate to subordinate replacer
|
|
|
|
int32_t len = replacer->toReplacer()->replace(text, start, limit, cursor);
|
|
|
|
limit = start + len;
|
|
|
|
|
|
|
|
// Now transliterate
|
|
|
|
limit = translit->transliterate(text, start, limit);
|
|
|
|
|
|
|
|
return limit - start;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* UnicodeReplacer API
|
|
|
|
*/
|
|
|
|
UnicodeString& FunctionReplacer::toReplacerPattern(UnicodeString& rule,
|
|
|
|
UBool escapeUnprintable) const {
|
2008-02-23 19:15:18 +00:00
|
|
|
UnicodeString str;
|
|
|
|
rule.truncate(0);
|
|
|
|
rule.append(AMPERSAND);
|
|
|
|
rule.append(translit->getID());
|
|
|
|
rule.append(OPEN);
|
|
|
|
rule.append(replacer->toReplacer()->toReplacerPattern(str, escapeUnprintable));
|
|
|
|
rule.append(CLOSE);
|
2002-02-07 01:07:55 +00:00
|
|
|
return rule;
|
|
|
|
}
|
|
|
|
|
2002-06-28 21:13:54 +00:00
|
|
|
/**
|
|
|
|
* Implement UnicodeReplacer
|
|
|
|
*/
|
|
|
|
void FunctionReplacer::addReplacementSetTo(UnicodeSet& toUnionTo) const {
|
|
|
|
UnicodeSet set;
|
2008-02-23 19:15:18 +00:00
|
|
|
toUnionTo.addAll(translit->getTargetSet(set));
|
2002-06-28 21:13:54 +00:00
|
|
|
}
|
|
|
|
|
2002-03-20 00:42:02 +00:00
|
|
|
/**
|
|
|
|
* UnicodeFunctor API
|
|
|
|
*/
|
|
|
|
void FunctionReplacer::setData(const TransliterationRuleData* d) {
|
2008-02-23 19:15:18 +00:00
|
|
|
replacer->setData(d);
|
2002-03-20 00:42:02 +00:00
|
|
|
}
|
|
|
|
|
2002-02-07 01:07:55 +00:00
|
|
|
U_NAMESPACE_END
|
|
|
|
|
2002-09-20 01:54:48 +00:00
|
|
|
#endif /* #if !UCONFIG_NO_TRANSLITERATION */
|
|
|
|
|
2002-02-07 01:07:55 +00:00
|
|
|
//eof
|