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