1999-11-20 00:40:50 +00:00
|
|
|
/*
|
|
|
|
**********************************************************************
|
|
|
|
* Copyright (C) 1999, International Business Machines
|
|
|
|
* Corporation and others. All Rights Reserved.
|
|
|
|
**********************************************************************
|
|
|
|
* Date Name Description
|
|
|
|
* 11/17/99 aliu Creation.
|
|
|
|
**********************************************************************
|
|
|
|
*/
|
2002-08-21 19:12:24 +00:00
|
|
|
|
|
|
|
/* These APIs are becoming private */
|
|
|
|
#define ICU_COMPOUNDTRANSLITERATOR_USE_DEPRECATES 1
|
|
|
|
|
1999-12-28 23:57:50 +00:00
|
|
|
#include "unicode/cpdtrans.h"
|
|
|
|
#include "unicode/unifilt.h"
|
|
|
|
#include "unicode/unifltlg.h"
|
2001-10-10 19:29:45 +00:00
|
|
|
#include "unicode/uniset.h"
|
2001-07-13 21:09:41 +00:00
|
|
|
#include "uvector.h"
|
2002-02-07 01:07:55 +00:00
|
|
|
#include "tridpars.h"
|
2002-07-16 16:30:15 +00:00
|
|
|
#include "cmemory.h"
|
1999-11-20 00:40:50 +00:00
|
|
|
|
2001-07-17 23:55:42 +00:00
|
|
|
// keep in sync with Transliterator
|
|
|
|
static const UChar ID_SEP = 0x002D; /*-*/
|
|
|
|
static const UChar ID_DELIM = 0x003B; /*;*/
|
|
|
|
static const UChar NEWLINE = 10;
|
2001-07-17 18:13:51 +00:00
|
|
|
|
2001-10-26 22:47:26 +00:00
|
|
|
// Empty string
|
|
|
|
static const UChar EMPTY[] = {0}; //""
|
|
|
|
|
2001-10-08 23:26:58 +00:00
|
|
|
U_NAMESPACE_BEGIN
|
|
|
|
|
2002-06-29 00:04:16 +00:00
|
|
|
const char CompoundTransliterator::fgClassID=0;
|
|
|
|
|
1999-11-20 00:40:50 +00:00
|
|
|
/**
|
|
|
|
* Constructs a new compound transliterator given an array of
|
|
|
|
* transliterators. The array of transliterators may be of any
|
|
|
|
* length, including zero or one, however, useful compound
|
|
|
|
* transliterators have at least two components.
|
|
|
|
* @param transliterators array of <code>Transliterator</code>
|
|
|
|
* objects
|
2000-12-09 02:38:12 +00:00
|
|
|
* @param transliteratorCount The number of
|
|
|
|
* <code>Transliterator</code> objects in transliterators.
|
1999-11-20 00:40:50 +00:00
|
|
|
* @param filter the filter. Any character for which
|
2000-01-18 20:00:56 +00:00
|
|
|
* <tt>filter.contains()</tt> returns <tt>false</tt> will not be
|
1999-11-20 00:40:50 +00:00
|
|
|
* altered by this transliterator. If <tt>filter</tt> is
|
|
|
|
* <tt>null</tt> then no filtering is applied.
|
|
|
|
*/
|
2000-01-14 21:15:25 +00:00
|
|
|
CompoundTransliterator::CompoundTransliterator(
|
1999-11-20 00:40:50 +00:00
|
|
|
Transliterator* const transliterators[],
|
2000-12-09 02:38:12 +00:00
|
|
|
int32_t transliteratorCount,
|
1999-11-20 00:40:50 +00:00
|
|
|
UnicodeFilter* adoptedFilter) :
|
2000-12-09 02:38:12 +00:00
|
|
|
Transliterator(joinIDs(transliterators, transliteratorCount), adoptedFilter),
|
2001-07-17 00:16:02 +00:00
|
|
|
trans(0), count(0), compoundRBTIndex(-1) {
|
2000-12-09 02:38:12 +00:00
|
|
|
setTransliterators(transliterators, transliteratorCount);
|
2000-01-14 21:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Splits an ID of the form "ID;ID;..." into a compound using each
|
|
|
|
* of the IDs.
|
2000-09-25 22:03:13 +00:00
|
|
|
* @param id of above form
|
2000-01-14 21:15:25 +00:00
|
|
|
* @param forward if false, does the list in reverse order, and
|
|
|
|
* takes the inverse of each ID.
|
|
|
|
*/
|
2000-09-25 22:03:13 +00:00
|
|
|
CompoundTransliterator::CompoundTransliterator(const UnicodeString& id,
|
2000-06-27 19:00:38 +00:00
|
|
|
UTransDirection direction,
|
2000-05-20 02:08:55 +00:00
|
|
|
UnicodeFilter* adoptedFilter,
|
2002-02-20 00:01:31 +00:00
|
|
|
UParseError& /*parseError*/,
|
2000-05-20 02:08:55 +00:00
|
|
|
UErrorCode& status) :
|
2001-07-17 00:16:02 +00:00
|
|
|
Transliterator(id, adoptedFilter),
|
|
|
|
trans(0), compoundRBTIndex(-1) {
|
2002-02-20 00:01:31 +00:00
|
|
|
// TODO add code for parseError...currently unused, but
|
|
|
|
// later may be used by parsing code...
|
|
|
|
init(id, direction, -1, 0, TRUE, status);
|
2000-05-20 02:08:55 +00:00
|
|
|
}
|
|
|
|
|
2000-09-25 22:03:13 +00:00
|
|
|
CompoundTransliterator::CompoundTransliterator(const UnicodeString& id,
|
2002-02-20 00:01:31 +00:00
|
|
|
UParseError& /*parseError*/,
|
2000-05-20 02:08:55 +00:00
|
|
|
UErrorCode& status) :
|
2000-09-25 22:03:13 +00:00
|
|
|
Transliterator(id, 0), // set filter to 0 here!
|
2001-07-17 00:16:02 +00:00
|
|
|
trans(0), compoundRBTIndex(-1) {
|
2002-02-20 00:01:31 +00:00
|
|
|
// TODO add code for parseError...currently unused, but
|
|
|
|
// later may be used by parsing code...
|
|
|
|
init(id, UTRANS_FORWARD, -1, 0, TRUE, status);
|
2000-05-20 02:08:55 +00:00
|
|
|
}
|
|
|
|
|
2002-02-07 01:07:55 +00:00
|
|
|
/**
|
|
|
|
* Private constructor for Transliterator from a vector of
|
|
|
|
* transliterators. The caller is responsible for fixing up the
|
|
|
|
* ID.
|
|
|
|
*/
|
|
|
|
CompoundTransliterator::CompoundTransliterator(UVector& list,
|
2002-03-01 21:56:40 +00:00
|
|
|
UParseError& /*parseError*/,
|
2002-02-07 01:07:55 +00:00
|
|
|
UErrorCode& status) :
|
|
|
|
Transliterator(EMPTY, NULL),
|
|
|
|
trans(0), compoundRBTIndex(-1)
|
|
|
|
{
|
2002-03-01 21:56:40 +00:00
|
|
|
// TODO add code for parseError...currently unused, but
|
|
|
|
// later may be used by parsing code...
|
2002-02-07 01:07:55 +00:00
|
|
|
init(list, UTRANS_FORWARD, FALSE, status);
|
|
|
|
// assume caller will fixup ID
|
|
|
|
}
|
|
|
|
|
2001-07-13 21:09:41 +00:00
|
|
|
/**
|
|
|
|
* Private constructor for compound RBTs. Construct a compound
|
|
|
|
* transliterator using the given idBlock, with the adoptedTrans
|
|
|
|
* inserted at the idSplitPoint.
|
|
|
|
*/
|
2001-12-03 18:32:20 +00:00
|
|
|
CompoundTransliterator::CompoundTransliterator(const UnicodeString& newID,
|
2001-07-13 21:09:41 +00:00
|
|
|
const UnicodeString& idBlock,
|
|
|
|
int32_t idSplitPoint,
|
|
|
|
Transliterator *adoptedTrans,
|
|
|
|
UErrorCode& status) :
|
2001-12-03 18:32:20 +00:00
|
|
|
Transliterator(newID, 0),
|
|
|
|
trans(0), compoundRBTIndex(-1)
|
|
|
|
{
|
2002-02-20 00:01:31 +00:00
|
|
|
init(idBlock, UTRANS_FORWARD, idSplitPoint, adoptedTrans, FALSE, status);
|
2001-07-13 21:09:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Finish constructing a transliterator: only to be called by
|
|
|
|
* constructors. Before calling init(), set trans and filter to NULL.
|
|
|
|
* @param id the id containing ';'-separated entries
|
|
|
|
* @param direction either FORWARD or REVERSE
|
|
|
|
* @param idSplitPoint the index into id at which the
|
|
|
|
* adoptedSplitTransliterator should be inserted, if there is one, or
|
|
|
|
* -1 if there is none.
|
|
|
|
* @param adoptedSplitTransliterator a transliterator to be inserted
|
|
|
|
* before the entry at offset idSplitPoint in the id string. May be
|
|
|
|
* NULL to insert no entry.
|
|
|
|
* @param fixReverseID if TRUE, then reconstruct the ID of reverse
|
|
|
|
* entries by calling getID() of component entries. Some constructors
|
|
|
|
* do not require this because they apply a facade ID anyway.
|
|
|
|
* @param status the error code indicating success or failure
|
|
|
|
*/
|
2000-09-25 22:03:13 +00:00
|
|
|
void CompoundTransliterator::init(const UnicodeString& id,
|
2000-06-27 19:00:38 +00:00
|
|
|
UTransDirection direction,
|
2001-07-13 21:09:41 +00:00
|
|
|
int32_t idSplitPoint,
|
|
|
|
Transliterator *adoptedSplitTrans,
|
|
|
|
UBool fixReverseID,
|
2000-05-20 02:08:55 +00:00
|
|
|
UErrorCode& status) {
|
2001-07-13 21:09:41 +00:00
|
|
|
// assert(trans == 0);
|
|
|
|
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
delete adoptedSplitTrans;
|
2000-09-25 22:03:13 +00:00
|
|
|
return;
|
2000-01-14 21:15:25 +00:00
|
|
|
}
|
2001-03-28 01:39:30 +00:00
|
|
|
|
2001-08-23 01:06:08 +00:00
|
|
|
UVector list(status);
|
2001-10-10 19:29:45 +00:00
|
|
|
UnicodeSet* compoundFilter = NULL;
|
2001-07-16 20:48:46 +00:00
|
|
|
UnicodeString regenID;
|
2002-02-07 01:07:55 +00:00
|
|
|
if (!TransliteratorIDParser::parseCompoundID(id, direction,
|
|
|
|
regenID, list, compoundFilter)) {
|
|
|
|
status = U_INVALID_ID;
|
|
|
|
delete adoptedSplitTrans;
|
|
|
|
delete compoundFilter;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
compoundRBTIndex = TransliteratorIDParser::instantiateList(list, adoptedSplitTrans, idSplitPoint, status);
|
2001-07-13 21:09:41 +00:00
|
|
|
|
2001-07-17 00:16:02 +00:00
|
|
|
init(list, direction, fixReverseID, status);
|
2001-10-10 19:29:45 +00:00
|
|
|
|
|
|
|
if (compoundFilter != NULL) {
|
|
|
|
adoptFilter(compoundFilter);
|
|
|
|
}
|
2001-07-13 21:09:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Finish constructing a transliterator: only to be called by
|
|
|
|
* constructors. Before calling init(), set trans and filter to NULL.
|
|
|
|
* @param list a vector of transliterator objects to be adopted. It
|
|
|
|
* should NOT be empty. The list should be in declared order. That
|
|
|
|
* is, it should be in the FORWARD order; if direction is REVERSE then
|
|
|
|
* the list order will be reversed.
|
|
|
|
* @param direction either FORWARD or REVERSE
|
|
|
|
* @param fixReverseID if TRUE, then reconstruct the ID of reverse
|
|
|
|
* entries by calling getID() of component entries. Some constructors
|
|
|
|
* do not require this because they apply a facade ID anyway.
|
|
|
|
* @param status the error code indicating success or failure
|
|
|
|
*/
|
|
|
|
void CompoundTransliterator::init(UVector& list,
|
|
|
|
UTransDirection direction,
|
|
|
|
UBool fixReverseID,
|
|
|
|
UErrorCode& status) {
|
|
|
|
// assert(trans == 0);
|
|
|
|
|
|
|
|
// Allocate array
|
|
|
|
if (U_SUCCESS(status)) {
|
|
|
|
count = list.size();
|
2002-07-16 16:30:15 +00:00
|
|
|
trans = (Transliterator **)uprv_malloc(count * sizeof(Transliterator *));
|
2002-07-02 15:10:30 +00:00
|
|
|
/* test for NULL */
|
2002-06-29 09:31:05 +00:00
|
|
|
if (trans == 0) {
|
|
|
|
status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
2001-07-13 21:09:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (U_FAILURE(status) || trans == 0) {
|
2001-07-17 00:16:02 +00:00
|
|
|
// assert(trans == 0);
|
2001-07-13 21:09:41 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Move the transliterators from the vector into an array.
|
|
|
|
// Reverse the order if necessary.
|
|
|
|
int32_t i;
|
|
|
|
for (i=0; i<count; ++i) {
|
|
|
|
int32_t j = (direction == UTRANS_FORWARD) ? i : count - 1 - i;
|
|
|
|
trans[i] = (Transliterator*) list.elementAt(j);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fix compoundRBTIndex for REVERSE transliterators
|
|
|
|
if (compoundRBTIndex >= 0 && direction == UTRANS_REVERSE) {
|
|
|
|
compoundRBTIndex = count - 1 - compoundRBTIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the direction is UTRANS_REVERSE then we may need to fix the
|
|
|
|
// ID.
|
|
|
|
if (direction == UTRANS_REVERSE && fixReverseID) {
|
2001-03-28 01:39:30 +00:00
|
|
|
UnicodeString newID;
|
2001-07-13 21:09:41 +00:00
|
|
|
for (i=0; i<count; ++i) {
|
2001-03-28 01:39:30 +00:00
|
|
|
if (i > 0) {
|
|
|
|
newID.append(ID_DELIM);
|
|
|
|
}
|
|
|
|
newID.append(trans[i]->getID());
|
|
|
|
}
|
|
|
|
setID(newID);
|
|
|
|
}
|
|
|
|
|
2000-01-18 18:27:27 +00:00
|
|
|
computeMaximumContextLength();
|
2000-01-14 21:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the IDs of the given list of transliterators, concatenated
|
2000-03-04 01:51:51 +00:00
|
|
|
* with ID_DELIM delimiting them. Equivalent to the perlish expression
|
|
|
|
* join(ID_DELIM, map($_.getID(), transliterators).
|
2000-01-14 21:15:25 +00:00
|
|
|
*/
|
|
|
|
UnicodeString CompoundTransliterator::joinIDs(Transliterator* const transliterators[],
|
2000-09-25 22:03:13 +00:00
|
|
|
int32_t transCount) {
|
2000-01-14 21:15:25 +00:00
|
|
|
UnicodeString id;
|
2000-09-25 22:03:13 +00:00
|
|
|
for (int32_t i=0; i<transCount; ++i) {
|
2000-01-14 21:15:25 +00:00
|
|
|
if (i > 0) {
|
2000-03-04 01:51:51 +00:00
|
|
|
id.append(ID_DELIM);
|
2000-01-14 21:15:25 +00:00
|
|
|
}
|
|
|
|
id.append(transliterators[i]->getID());
|
|
|
|
}
|
|
|
|
return id; // Return temporary
|
|
|
|
}
|
|
|
|
|
1999-11-20 00:40:50 +00:00
|
|
|
/**
|
|
|
|
* Copy constructor.
|
|
|
|
*/
|
|
|
|
CompoundTransliterator::CompoundTransliterator(const CompoundTransliterator& t) :
|
2001-07-17 00:16:02 +00:00
|
|
|
Transliterator(t), trans(0), count(0), compoundRBTIndex(-1) {
|
1999-11-20 00:40:50 +00:00
|
|
|
*this = t;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Destructor
|
|
|
|
*/
|
|
|
|
CompoundTransliterator::~CompoundTransliterator() {
|
|
|
|
freeTransliterators();
|
|
|
|
}
|
|
|
|
|
1999-12-22 22:57:04 +00:00
|
|
|
void CompoundTransliterator::freeTransliterators(void) {
|
2001-11-16 22:37:05 +00:00
|
|
|
if (trans != 0) {
|
|
|
|
for (int32_t i=0; i<count; ++i) {
|
2000-05-20 02:08:55 +00:00
|
|
|
delete trans[i];
|
|
|
|
}
|
2002-07-16 16:30:15 +00:00
|
|
|
uprv_free(trans);
|
1999-11-20 00:40:50 +00:00
|
|
|
}
|
|
|
|
trans = 0;
|
|
|
|
count = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Assignment operator.
|
|
|
|
*/
|
|
|
|
CompoundTransliterator& CompoundTransliterator::operator=(
|
|
|
|
const CompoundTransliterator& t) {
|
|
|
|
Transliterator::operator=(t);
|
|
|
|
int32_t i;
|
|
|
|
for (i=0; i<count; ++i) {
|
|
|
|
delete trans[i];
|
|
|
|
trans[i] = 0;
|
|
|
|
}
|
|
|
|
if (t.count > count) {
|
2002-07-16 16:30:15 +00:00
|
|
|
uprv_free(trans);
|
|
|
|
trans = (Transliterator **)uprv_malloc(t.count * sizeof(Transliterator *));
|
1999-11-20 00:40:50 +00:00
|
|
|
}
|
|
|
|
count = t.count;
|
|
|
|
for (i=0; i<count; ++i) {
|
|
|
|
trans[i] = t.trans[i]->clone();
|
|
|
|
}
|
2001-07-16 20:48:46 +00:00
|
|
|
compoundRBTIndex = t.compoundRBTIndex;
|
1999-11-20 00:40:50 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Transliterator API.
|
|
|
|
*/
|
1999-12-22 22:57:04 +00:00
|
|
|
Transliterator* CompoundTransliterator::clone(void) const {
|
1999-11-20 00:40:50 +00:00
|
|
|
return new CompoundTransliterator(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the number of transliterators in this chain.
|
|
|
|
* @return number of transliterators in this chain.
|
|
|
|
*/
|
1999-12-22 22:57:04 +00:00
|
|
|
int32_t CompoundTransliterator::getCount(void) const {
|
1999-11-20 00:40:50 +00:00
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the transliterator at the given index in this chain.
|
|
|
|
* @param index index into chain, from 0 to <code>getCount() - 1</code>
|
|
|
|
* @return transliterator at the given index
|
|
|
|
*/
|
|
|
|
const Transliterator& CompoundTransliterator::getTransliterator(int32_t index) const {
|
|
|
|
return *trans[index];
|
|
|
|
}
|
|
|
|
|
|
|
|
void CompoundTransliterator::setTransliterators(Transliterator* const transliterators[],
|
|
|
|
int32_t transCount) {
|
2002-07-16 16:30:15 +00:00
|
|
|
Transliterator** a = (Transliterator **)uprv_malloc(transCount * sizeof(Transliterator *));
|
1999-11-20 00:40:50 +00:00
|
|
|
for (int32_t i=0; i<transCount; ++i) {
|
|
|
|
a[i] = transliterators[i]->clone();
|
|
|
|
}
|
|
|
|
adoptTransliterators(a, transCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CompoundTransliterator::adoptTransliterators(Transliterator* adoptedTransliterators[],
|
|
|
|
int32_t transCount) {
|
2000-02-05 00:24:24 +00:00
|
|
|
// First free trans[] and set count to zero. Once this is done,
|
2001-07-17 00:16:02 +00:00
|
|
|
// orphan the filter. Set up the new trans[].
|
1999-11-20 00:40:50 +00:00
|
|
|
freeTransliterators();
|
|
|
|
trans = adoptedTransliterators;
|
|
|
|
count = transCount;
|
2000-01-18 18:27:27 +00:00
|
|
|
computeMaximumContextLength();
|
2000-03-31 00:31:42 +00:00
|
|
|
setID(joinIDs(trans, count));
|
2000-02-05 00:24:24 +00:00
|
|
|
}
|
|
|
|
|
2001-11-17 06:44:50 +00:00
|
|
|
/**
|
|
|
|
* Append c to buf, unless buf is empty or buf already ends in c.
|
|
|
|
*/
|
|
|
|
static void _smartAppend(UnicodeString& buf, UChar c) {
|
|
|
|
if (buf.length() != 0 &&
|
|
|
|
buf.charAt(buf.length() - 1) != c) {
|
|
|
|
buf.append(c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-07-13 21:09:41 +00:00
|
|
|
UnicodeString& CompoundTransliterator::toRules(UnicodeString& rulesSource,
|
|
|
|
UBool escapeUnprintable) const {
|
|
|
|
// We do NOT call toRules() on our component transliterators, in
|
|
|
|
// general. If we have several rule-based transliterators, this
|
|
|
|
// yields a concatenation of the rules -- not what we want. We do
|
|
|
|
// handle compound RBT transliterators specially -- those for which
|
|
|
|
// compoundRBTIndex >= 0. For the transliterator at compoundRBTIndex,
|
|
|
|
// we do call toRules() recursively.
|
|
|
|
rulesSource.truncate(0);
|
2001-11-17 06:44:50 +00:00
|
|
|
if (compoundRBTIndex >= 0 && getFilter() != NULL) {
|
|
|
|
// If we are a compound RBT and if we have a global
|
|
|
|
// filter, then emit it at the top.
|
|
|
|
UnicodeString pat;
|
|
|
|
rulesSource.append("::").append(getFilter()->toPattern(pat, escapeUnprintable)).append(ID_DELIM);
|
|
|
|
}
|
2001-07-13 21:09:41 +00:00
|
|
|
for (int32_t i=0; i<count; ++i) {
|
|
|
|
UnicodeString rule;
|
|
|
|
if (i == compoundRBTIndex) {
|
|
|
|
trans[i]->toRules(rule, escapeUnprintable);
|
|
|
|
} else {
|
|
|
|
trans[i]->Transliterator::toRules(rule, escapeUnprintable);
|
|
|
|
}
|
2001-11-17 06:44:50 +00:00
|
|
|
_smartAppend(rulesSource, NEWLINE);
|
2001-07-13 21:09:41 +00:00
|
|
|
rulesSource.append(rule);
|
2001-11-17 06:44:50 +00:00
|
|
|
_smartAppend(rulesSource, ID_DELIM);
|
2001-07-13 21:09:41 +00:00
|
|
|
}
|
|
|
|
return rulesSource;
|
|
|
|
}
|
|
|
|
|
2002-06-28 21:13:54 +00:00
|
|
|
/**
|
|
|
|
* Implement Transliterator framework
|
|
|
|
*/
|
|
|
|
void CompoundTransliterator::handleGetSourceSet(UnicodeSet& result) const {
|
|
|
|
UnicodeSet set;
|
|
|
|
result.clear();
|
|
|
|
for (int32_t i=0; i<count; ++i) {
|
2002-06-29 09:31:05 +00:00
|
|
|
result.addAll(trans[i]->getSourceSet(set));
|
|
|
|
// Take the example of Hiragana-Latin. This is really
|
|
|
|
// Hiragana-Katakana; Katakana-Latin. The source set of
|
|
|
|
// these two is roughly [:Hiragana:] and [:Katakana:].
|
|
|
|
// But the source set for the entire transliterator is
|
|
|
|
// actually [:Hiragana:] ONLY -- that is, the first
|
|
|
|
// non-empty source set.
|
|
|
|
|
|
|
|
// This is a heuristic, and not 100% reliable.
|
|
|
|
if (!result.isEmpty()) {
|
|
|
|
break;
|
|
|
|
}
|
2002-06-28 21:13:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Override Transliterator framework
|
|
|
|
*/
|
|
|
|
UnicodeSet& CompoundTransliterator::getTargetSet(UnicodeSet& result) const {
|
|
|
|
UnicodeSet set;
|
|
|
|
result.clear();
|
|
|
|
for (int32_t i=0; i<count; ++i) {
|
2002-06-29 09:31:05 +00:00
|
|
|
// This is a heuristic, and not 100% reliable.
|
|
|
|
result.addAll(trans[i]->getTargetSet(set));
|
2002-06-28 21:13:54 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
1999-11-20 00:40:50 +00:00
|
|
|
/**
|
2000-01-18 18:27:27 +00:00
|
|
|
* Implements {@link Transliterator#handleTransliterate}.
|
1999-11-20 00:40:50 +00:00
|
|
|
*/
|
2000-06-27 19:00:38 +00:00
|
|
|
void CompoundTransliterator::handleTransliterate(Replaceable& text, UTransPosition& index,
|
2000-05-18 22:08:39 +00:00
|
|
|
UBool incremental) const {
|
2001-07-13 21:09:41 +00:00
|
|
|
/* Call each transliterator with the same contextStart and
|
|
|
|
* start, but with the limit as modified
|
|
|
|
* by preceding transliterators. The start index must be
|
1999-11-20 00:40:50 +00:00
|
|
|
* reset for each transliterator to give each a chance to
|
2001-07-13 21:09:41 +00:00
|
|
|
* transliterate the text. The initial contextStart index is known
|
1999-11-20 00:40:50 +00:00
|
|
|
* to still point to the same place after each transliterator
|
|
|
|
* is called because each transliterator will not change the
|
2001-07-13 21:09:41 +00:00
|
|
|
* text between contextStart and the initial start index.
|
1999-11-20 00:40:50 +00:00
|
|
|
*
|
|
|
|
* IMPORTANT: After the first transliterator, each subsequent
|
|
|
|
* transliterator only gets to transliterate text committed by
|
2001-07-13 21:09:41 +00:00
|
|
|
* preceding transliterators; that is, the start (output
|
1999-11-20 00:40:50 +00:00
|
|
|
* value) of transliterator i becomes the limit (input value)
|
|
|
|
* of transliterator i+1. Finally, the overall limit is fixed
|
|
|
|
* up before we return.
|
|
|
|
*
|
2001-11-19 19:53:10 +00:00
|
|
|
* Assumptions we make here:
|
|
|
|
* (1) contextStart <= start <= limit <= contextLimit <= text.length()
|
|
|
|
* (2) start <= start' <= limit' ;cursor doesn't move back
|
|
|
|
* (3) start <= limit' ;text before cursor unchanged
|
|
|
|
* - start' is the value of start after calling handleKT
|
|
|
|
* - limit' is the value of limit after calling handleKT
|
1999-11-20 00:40:50 +00:00
|
|
|
*/
|
2001-11-19 19:53:10 +00:00
|
|
|
|
1999-11-20 00:40:50 +00:00
|
|
|
/**
|
|
|
|
* Example: 3 transliterators. This example illustrates the
|
2001-07-13 21:09:41 +00:00
|
|
|
* mechanics we need to implement. C, S, and L are the contextStart,
|
2001-11-19 19:53:10 +00:00
|
|
|
* start, and limit. gl is the globalLimit. contextLimit is
|
|
|
|
* equal to limit throughout.
|
1999-11-20 00:40:50 +00:00
|
|
|
*
|
|
|
|
* 1. h-u, changes hex to Unicode
|
|
|
|
*
|
|
|
|
* 4 7 a d 0 4 7 a
|
|
|
|
* abc/u0061/u => abca/u
|
2001-07-13 21:09:41 +00:00
|
|
|
* C S L C S L gl=f->a
|
1999-11-20 00:40:50 +00:00
|
|
|
*
|
|
|
|
* 2. upup, changes "x" to "XX"
|
|
|
|
*
|
|
|
|
* 4 7 a 4 7 a
|
|
|
|
* abca/u => abcAA/u
|
2001-07-13 21:09:41 +00:00
|
|
|
* C SL C S
|
1999-11-20 00:40:50 +00:00
|
|
|
* L gl=a->b
|
|
|
|
* 3. u-h, changes Unicode to hex
|
|
|
|
*
|
|
|
|
* 4 7 a 4 7 a d 0 3
|
|
|
|
* abcAA/u => abc/u0041/u0041/u
|
2001-07-13 21:09:41 +00:00
|
|
|
* C S L C S
|
1999-11-20 00:40:50 +00:00
|
|
|
* L gl=b->15
|
|
|
|
* 4. return
|
|
|
|
*
|
|
|
|
* 4 7 a d 0 3
|
|
|
|
* abc/u0041/u0041/u
|
2001-11-19 19:53:10 +00:00
|
|
|
* C S L
|
1999-11-20 00:40:50 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
if (count < 1) {
|
2001-07-13 21:09:41 +00:00
|
|
|
index.start = index.limit;
|
1999-11-20 00:40:50 +00:00
|
|
|
return; // Short circuit for empty compound transliterators
|
|
|
|
}
|
|
|
|
|
2001-07-17 00:16:02 +00:00
|
|
|
// compoundLimit is the limit value for the entire compound
|
|
|
|
// operation. We overwrite index.limit with the previous
|
|
|
|
// index.start. After each transliteration, we update
|
|
|
|
// compoundLimit for insertions or deletions that have happened.
|
|
|
|
int32_t compoundLimit = index.limit;
|
|
|
|
|
2001-07-17 23:36:41 +00:00
|
|
|
// compoundStart is the start for the entire compound
|
|
|
|
// operation.
|
|
|
|
int32_t compoundStart = index.start;
|
|
|
|
|
2001-10-26 22:47:26 +00:00
|
|
|
int32_t delta = 0; // delta in length
|
|
|
|
|
2001-07-17 23:36:41 +00:00
|
|
|
// Give each transliterator a crack at the run of characters.
|
|
|
|
// See comments at the top of the method for more detail.
|
|
|
|
for (int32_t i=0; i<count; ++i) {
|
|
|
|
index.start = compoundStart; // Reset start
|
|
|
|
int32_t limit = index.limit;
|
|
|
|
|
2001-11-30 01:24:25 +00:00
|
|
|
if (index.start == index.limit) {
|
|
|
|
// Short circuit for empty range
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2001-07-17 23:36:41 +00:00
|
|
|
trans[i]->filteredTransliterate(text, index, incremental);
|
|
|
|
|
2001-11-30 01:24:25 +00:00
|
|
|
// In a properly written transliterator, start == limit after
|
|
|
|
// handleTransliterate() returns when incremental is false.
|
|
|
|
// Catch cases where the subclass doesn't do this, and throw
|
|
|
|
// an exception. (Just pinning start to limit is a bad idea,
|
|
|
|
// because what's probably happening is that the subclass
|
|
|
|
// isn't transliterating all the way to the end, and it should
|
|
|
|
// in non-incremental mode.)
|
|
|
|
if (!incremental && index.start != index.limit) {
|
|
|
|
// We can't throw an exception, so just fudge things
|
|
|
|
index.start = index.limit;
|
|
|
|
}
|
|
|
|
|
2001-10-26 22:47:26 +00:00
|
|
|
// Cumulative delta for insertions/deletions
|
|
|
|
delta += index.limit - limit;
|
2001-09-14 21:51:29 +00:00
|
|
|
|
|
|
|
if (incremental) {
|
|
|
|
// In the incremental case, only allow subsequent
|
|
|
|
// transliterators to modify what has already been
|
|
|
|
// completely processed by prior transliterators. In the
|
|
|
|
// non-incrmental case, allow each transliterator to
|
|
|
|
// process the entire text.
|
|
|
|
index.limit = index.start;
|
|
|
|
}
|
2001-07-17 23:36:41 +00:00
|
|
|
}
|
2001-07-17 00:16:02 +00:00
|
|
|
|
2001-10-26 22:47:26 +00:00
|
|
|
compoundLimit += delta;
|
|
|
|
|
2001-07-17 00:16:02 +00:00
|
|
|
// Start is good where it is -- where the last transliterator left
|
|
|
|
// it. Limit needs to be put back where it was, modulo
|
|
|
|
// adjustments for deletions/insertions.
|
|
|
|
index.limit = compoundLimit;
|
1999-11-20 00:40:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2000-01-18 18:27:27 +00:00
|
|
|
* Sets the length of the longest context required by this transliterator.
|
1999-11-20 00:40:50 +00:00
|
|
|
* This is <em>preceding</em> context.
|
|
|
|
*/
|
2000-01-18 18:27:27 +00:00
|
|
|
void CompoundTransliterator::computeMaximumContextLength(void) {
|
1999-11-20 00:40:50 +00:00
|
|
|
int32_t max = 0;
|
|
|
|
for (int32_t i=0; i<count; ++i) {
|
|
|
|
int32_t len = trans[i]->getMaximumContextLength();
|
|
|
|
if (len > max) {
|
|
|
|
max = len;
|
|
|
|
}
|
|
|
|
}
|
2000-01-18 18:27:27 +00:00
|
|
|
setMaximumContextLength(max);
|
1999-11-20 00:40:50 +00:00
|
|
|
}
|
2001-10-08 23:26:58 +00:00
|
|
|
|
|
|
|
U_NAMESPACE_END
|
|
|
|
|
|
|
|
/* eof */
|