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.
|
|
|
|
**********************************************************************
|
|
|
|
*/
|
1999-12-28 23:57:50 +00:00
|
|
|
#include "unicode/cpdtrans.h"
|
|
|
|
#include "unicode/unifilt.h"
|
|
|
|
#include "unicode/unifltlg.h"
|
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
|
|
|
|
* @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-01-14 21:15:25 +00:00
|
|
|
int32_t count,
|
1999-11-20 00:40:50 +00:00
|
|
|
UnicodeFilter* adoptedFilter) :
|
2000-01-14 21:15:25 +00:00
|
|
|
Transliterator(joinIDs(transliterators, count), adoptedFilter),
|
2000-02-05 00:24:24 +00:00
|
|
|
trans(0), count(0), filters(0) {
|
2000-01-14 21:15:25 +00:00
|
|
|
setTransliterators(transliterators, count);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Splits an ID of the form "ID;ID;..." into a compound using each
|
|
|
|
* of the IDs.
|
|
|
|
* @param ID of above form
|
|
|
|
* @param forward if false, does the list in reverse order, and
|
|
|
|
* takes the inverse of each ID.
|
|
|
|
*/
|
|
|
|
CompoundTransliterator::CompoundTransliterator(const UnicodeString& ID,
|
|
|
|
Transliterator::Direction direction,
|
2000-05-20 02:08:55 +00:00
|
|
|
UnicodeFilter* adoptedFilter,
|
|
|
|
UErrorCode& status) :
|
2000-02-05 00:24:24 +00:00
|
|
|
Transliterator(ID, 0), // set filter to 0 here!
|
2000-05-20 02:08:55 +00:00
|
|
|
filters(0), trans(0) {
|
|
|
|
init(ID, direction, adoptedFilter, status);
|
|
|
|
}
|
|
|
|
|
|
|
|
CompoundTransliterator::CompoundTransliterator(const UnicodeString& ID,
|
|
|
|
UErrorCode& status) :
|
|
|
|
Transliterator(ID, 0), // set filter to 0 here!
|
|
|
|
filters(0), trans(0) {
|
|
|
|
init(ID, FORWARD, 0, status);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CompoundTransliterator::init(const UnicodeString& ID,
|
|
|
|
Transliterator::Direction direction,
|
|
|
|
UnicodeFilter* adoptedFilter,
|
|
|
|
UErrorCode& status) {
|
|
|
|
if (U_FAILURE(status)) return;
|
2000-03-04 01:51:51 +00:00
|
|
|
UnicodeString* list = split(ID, ID_DELIM, count);
|
2000-01-14 21:15:25 +00:00
|
|
|
trans = new Transliterator*[count];
|
|
|
|
for (int32_t i = 0; i < count; ++i) {
|
|
|
|
trans[i] = createInstance(list[direction==FORWARD ? i : (count-1-i)],
|
|
|
|
direction);
|
2000-05-20 02:08:55 +00:00
|
|
|
if (trans[i] == NULL) {
|
|
|
|
while (++i < count) trans[i] = 0;
|
|
|
|
status = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
delete[] list;
|
|
|
|
delete adoptedFilter;
|
|
|
|
return;
|
|
|
|
}
|
2000-01-14 21:15:25 +00:00
|
|
|
}
|
|
|
|
delete[] list;
|
2000-01-18 18:27:27 +00:00
|
|
|
computeMaximumContextLength();
|
2000-02-05 00:24:24 +00:00
|
|
|
adoptFilter(adoptedFilter);
|
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[],
|
|
|
|
int32_t count) {
|
|
|
|
UnicodeString id;
|
|
|
|
for (int32_t i=0; i<count; ++i) {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Splits a string, as in JavaScript
|
|
|
|
*/
|
|
|
|
UnicodeString* CompoundTransliterator::split(const UnicodeString& s,
|
|
|
|
UChar divider,
|
|
|
|
int32_t& count) {
|
|
|
|
// changed MED
|
|
|
|
// see how many there are
|
|
|
|
count = 1;
|
2000-01-15 01:46:40 +00:00
|
|
|
int32_t i;
|
|
|
|
for (i = 0; i < s.length(); ++i) {
|
2000-01-14 21:15:25 +00:00
|
|
|
if (s.charAt(i) == divider) ++count;
|
|
|
|
}
|
|
|
|
|
|
|
|
// make an array with them
|
|
|
|
UnicodeString* result = new UnicodeString[count];
|
|
|
|
int32_t last = 0;
|
|
|
|
int32_t current = 0;
|
2000-01-15 01:46:40 +00:00
|
|
|
|
2000-01-14 21:15:25 +00:00
|
|
|
for (i = 0; i < s.length(); ++i) {
|
|
|
|
if (s.charAt(i) == divider) {
|
|
|
|
s.extractBetween(last, i, result[current++]);
|
|
|
|
last = i+1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s.extractBetween(last, i, result[current]);
|
|
|
|
return result;
|
1999-11-20 00:40:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Copy constructor.
|
|
|
|
*/
|
|
|
|
CompoundTransliterator::CompoundTransliterator(const CompoundTransliterator& t) :
|
2000-02-05 00:24:24 +00:00
|
|
|
Transliterator(t), trans(0), count(0), filters(0) {
|
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) {
|
1999-11-20 00:40:50 +00:00
|
|
|
for (int32_t i=0; i<count; ++i) {
|
2000-05-20 02:08:55 +00:00
|
|
|
if (trans != 0) {
|
|
|
|
delete trans[i];
|
|
|
|
}
|
2000-02-05 00:24:24 +00:00
|
|
|
if (filters != 0) {
|
|
|
|
delete filters[i];
|
|
|
|
}
|
1999-11-20 00:40:50 +00:00
|
|
|
}
|
2000-02-05 00:24:24 +00:00
|
|
|
delete[] trans;
|
|
|
|
delete[] filters;
|
1999-11-20 00:40:50 +00:00
|
|
|
trans = 0;
|
2000-02-05 00:24:24 +00:00
|
|
|
filters = 0;
|
1999-11-20 00:40:50 +00:00
|
|
|
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;
|
2000-02-05 00:24:24 +00:00
|
|
|
if (filters != 0) {
|
|
|
|
delete filters[i];
|
|
|
|
filters[i] = 0;
|
|
|
|
}
|
1999-11-20 00:40:50 +00:00
|
|
|
}
|
|
|
|
if (t.count > count) {
|
|
|
|
delete[] trans;
|
|
|
|
trans = new Transliterator*[t.count];
|
2000-02-05 00:24:24 +00:00
|
|
|
delete[] filters;
|
|
|
|
filters = (t.filter == 0) ? 0 : new UnicodeFilter*[t.count];
|
1999-11-20 00:40:50 +00:00
|
|
|
}
|
|
|
|
count = t.count;
|
|
|
|
for (i=0; i<count; ++i) {
|
|
|
|
trans[i] = t.trans[i]->clone();
|
2000-02-05 00:24:24 +00:00
|
|
|
if (t.filters != 0) {
|
|
|
|
filters[i] = t.filters[i]->clone();
|
|
|
|
}
|
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) {
|
|
|
|
Transliterator** a = new Transliterator*[transCount];
|
|
|
|
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,
|
|
|
|
// orphan the filter. Set up the new trans[], and call
|
|
|
|
// adoptFilter() to fix up the filters in trans[].
|
1999-11-20 00:40:50 +00:00
|
|
|
freeTransliterators();
|
2000-02-05 00:24:24 +00:00
|
|
|
UnicodeFilter *f = orphanFilter();
|
1999-11-20 00:40:50 +00:00
|
|
|
trans = adoptedTransliterators;
|
|
|
|
count = transCount;
|
2000-01-18 18:27:27 +00:00
|
|
|
computeMaximumContextLength();
|
2000-02-05 00:24:24 +00:00
|
|
|
adoptFilter(f);
|
2000-03-31 00:31:42 +00:00
|
|
|
setID(joinIDs(trans, count));
|
2000-02-05 00:24:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Override Transliterator. Modify the transliterators that make up
|
|
|
|
* this compound transliterator so their filters are the logical AND
|
|
|
|
* of this transliterator's filter and their own. Original filters
|
|
|
|
* are kept in the filters array.
|
|
|
|
*/
|
|
|
|
void CompoundTransliterator::adoptFilter(UnicodeFilter* f) {
|
|
|
|
/**
|
|
|
|
* If there is a filter F for the compound transliterator as a
|
|
|
|
* whole, then we need to modify every non-null filter f in
|
|
|
|
* the chain to be f' = F & f.
|
|
|
|
*
|
|
|
|
* There are two possible states:
|
|
|
|
* 1. getFilter() != 0
|
|
|
|
* original filters in filters[]
|
|
|
|
* createAnd() filters in trans[]
|
|
|
|
* 2. getFilter() == 0
|
|
|
|
* filters[] either unallocated or empty
|
|
|
|
* original filters in trans[]
|
|
|
|
* This method must insure that we stay in one of these states.
|
|
|
|
*/
|
|
|
|
if (count > 0) {
|
|
|
|
if (f == 0) {
|
|
|
|
// Restore original filters
|
|
|
|
if (getFilter() != 0 && filters != 0) {
|
|
|
|
for (int32_t i=0; i<count; ++i) {
|
|
|
|
trans[i]->adoptFilter(filters[i]);
|
|
|
|
filters[i] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// If the previous filter is 0, then the component filters
|
|
|
|
// are in trans[i], and need to be pulled out into filters[].
|
|
|
|
if (getFilter() == 0) {
|
|
|
|
if (filters == 0) {
|
|
|
|
filters = new UnicodeFilter*[count];
|
|
|
|
}
|
|
|
|
for (int32_t i=0; i<count; ++i) {
|
|
|
|
filters[i] = trans[i]->orphanFilter();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (int32_t i=0; i<count; ++i) {
|
|
|
|
trans[i]->adoptFilter(UnicodeFilterLogic::createAnd(f, filters[i]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Transliterator::adoptFilter(f);
|
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-01-19 19:02:10 +00:00
|
|
|
void CompoundTransliterator::handleTransliterate(Replaceable& text, Position& index,
|
2000-05-18 22:08:39 +00:00
|
|
|
UBool incremental) const {
|
1999-11-20 00:40:50 +00:00
|
|
|
/* Call each transliterator with the same start value and
|
|
|
|
* initial cursor index, but with the limit index as modified
|
|
|
|
* by preceding transliterators. The cursor index must be
|
|
|
|
* reset for each transliterator to give each a chance to
|
|
|
|
* transliterate the text. The initial cursor index is known
|
|
|
|
* to still point to the same place after each transliterator
|
|
|
|
* is called because each transliterator will not change the
|
|
|
|
* text between start and the initial value of cursor.
|
|
|
|
*
|
|
|
|
* IMPORTANT: After the first transliterator, each subsequent
|
|
|
|
* transliterator only gets to transliterate text committed by
|
|
|
|
* preceding transliterators; that is, the cursor (output
|
|
|
|
* value) of transliterator i becomes the limit (input value)
|
|
|
|
* of transliterator i+1. Finally, the overall limit is fixed
|
|
|
|
* up before we return.
|
|
|
|
*
|
|
|
|
* Assumptions we make here:
|
|
|
|
* (1) start <= cursor <= limit ;cursor valid on entry
|
|
|
|
* (2) cursor <= cursor' <= limit' ;cursor doesn't move back
|
|
|
|
* (3) cursor <= limit' ;text before cursor unchanged
|
|
|
|
* - cursor' is the value of cursor after calling handleKT
|
|
|
|
* - limit' is the value of limit after calling handleKT
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Example: 3 transliterators. This example illustrates the
|
|
|
|
* mechanics we need to implement. S, C, and L are the start,
|
|
|
|
* cursor, and limit. gl is the globalLimit.
|
|
|
|
*
|
|
|
|
* 1. h-u, changes hex to Unicode
|
|
|
|
*
|
|
|
|
* 4 7 a d 0 4 7 a
|
|
|
|
* abc/u0061/u => abca/u
|
|
|
|
* S C L S C L gl=f->a
|
|
|
|
*
|
|
|
|
* 2. upup, changes "x" to "XX"
|
|
|
|
*
|
|
|
|
* 4 7 a 4 7 a
|
|
|
|
* abca/u => abcAA/u
|
|
|
|
* S CL S C
|
|
|
|
* 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
|
|
|
|
* S C L S C
|
|
|
|
* L gl=b->15
|
|
|
|
* 4. return
|
|
|
|
*
|
|
|
|
* 4 7 a d 0 3
|
|
|
|
* abc/u0041/u0041/u
|
|
|
|
* S C L
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (count < 1) {
|
|
|
|
return; // Short circuit for empty compound transliterators
|
|
|
|
}
|
|
|
|
|
2000-01-15 01:46:40 +00:00
|
|
|
int32_t i;
|
2000-01-19 19:02:10 +00:00
|
|
|
int32_t cursor = index.cursor;
|
|
|
|
int32_t limit = index.limit;
|
1999-11-20 00:40:50 +00:00
|
|
|
int32_t globalLimit = limit;
|
|
|
|
/* globalLimit is the overall limit. We keep track of this
|
2000-01-19 19:02:10 +00:00
|
|
|
* since we overwrite index.limit with the previous
|
|
|
|
* index.cursor. After each transliteration, we update
|
1999-11-20 00:40:50 +00:00
|
|
|
* globalLimit for insertions or deletions that have happened.
|
|
|
|
*/
|
|
|
|
|
2000-01-15 01:46:40 +00:00
|
|
|
for (i=0; i<count; ++i) {
|
2000-01-19 19:02:10 +00:00
|
|
|
index.cursor = cursor; // Reset cursor
|
|
|
|
index.limit = limit;
|
1999-11-20 00:40:50 +00:00
|
|
|
|
2000-01-19 19:02:10 +00:00
|
|
|
trans[i]->handleTransliterate(text, index, incremental);
|
1999-11-20 00:40:50 +00:00
|
|
|
|
|
|
|
// Adjust overall limit for insertions/deletions
|
2000-01-19 19:02:10 +00:00
|
|
|
globalLimit += index.limit - limit;
|
|
|
|
limit = index.cursor; // Move limit to end of committed text
|
1999-11-20 00:40:50 +00:00
|
|
|
}
|
|
|
|
// Cursor 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.
|
2000-01-19 19:02:10 +00:00
|
|
|
index.limit = globalLimit;
|
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
|
|
|
}
|