2002-06-07 20:24:40 +00:00
|
|
|
/*
|
|
|
|
*****************************************************************
|
2003-06-03 20:58:22 +00:00
|
|
|
* Copyright (c) 2002-2003, International Business Machines Corporation
|
2002-06-07 20:24:40 +00:00
|
|
|
* and others. All Rights Reserved.
|
|
|
|
*****************************************************************
|
|
|
|
* Date Name Description
|
|
|
|
* 06/06/2002 aliu Creation.
|
|
|
|
*****************************************************************
|
|
|
|
*/
|
2002-06-27 01:19:20 +00:00
|
|
|
|
|
|
|
#include "unicode/utypes.h"
|
2002-09-20 01:54:48 +00:00
|
|
|
|
|
|
|
#if !UCONFIG_NO_TRANSLITERATION
|
|
|
|
|
2002-06-27 01:19:20 +00:00
|
|
|
#include "unicode/uobject.h"
|
|
|
|
#include "unicode/uscript.h"
|
2003-02-14 01:44:49 +00:00
|
|
|
#include "nultrans.h"
|
2002-06-07 20:24:40 +00:00
|
|
|
#include "anytrans.h"
|
|
|
|
#include "uvector.h"
|
2002-06-11 22:36:11 +00:00
|
|
|
#include "tridpars.h"
|
|
|
|
#include "hash.h"
|
2002-06-07 20:24:40 +00:00
|
|
|
|
|
|
|
//------------------------------------------------------------
|
|
|
|
// Constants
|
|
|
|
|
2002-06-11 22:36:11 +00:00
|
|
|
static const UChar TARGET_SEP = 45; // '-'
|
|
|
|
static const UChar VARIANT_SEP = 47; // '/'
|
|
|
|
static const UChar ANY[] = {65,110,121,0}; // "Any"
|
|
|
|
static const UChar NULL_ID[] = {78,117,108,108,0}; // "Null"
|
|
|
|
static const UChar LATIN_PIVOT[] = {45,76,97,116,105,110,59,76,97,116,105,110,45,0}; // "-Latin;Latin-"
|
2002-06-07 20:24:40 +00:00
|
|
|
|
|
|
|
//------------------------------------------------------------
|
|
|
|
|
2002-06-12 21:27:08 +00:00
|
|
|
U_CDECL_BEGIN
|
2002-06-07 20:24:40 +00:00
|
|
|
/**
|
2002-06-11 22:36:11 +00:00
|
|
|
* Deleter function for Transliterator*.
|
2002-06-07 20:24:40 +00:00
|
|
|
*/
|
2002-06-18 20:17:48 +00:00
|
|
|
static void U_CALLCONV
|
2002-06-12 21:27:08 +00:00
|
|
|
_deleteTransliterator(void *obj) {
|
2002-06-11 22:36:11 +00:00
|
|
|
delete (Transliterator*) obj;
|
2002-06-07 20:24:40 +00:00
|
|
|
}
|
2002-06-12 21:27:08 +00:00
|
|
|
U_CDECL_END
|
2002-06-07 20:24:40 +00:00
|
|
|
|
2002-06-11 22:36:11 +00:00
|
|
|
//------------------------------------------------------------
|
2002-06-07 20:24:40 +00:00
|
|
|
|
2002-06-11 22:36:11 +00:00
|
|
|
U_NAMESPACE_BEGIN
|
|
|
|
|
|
|
|
//------------------------------------------------------------
|
|
|
|
// ScriptRunIterator
|
2002-06-07 20:24:40 +00:00
|
|
|
|
|
|
|
/**
|
2002-06-11 22:36:11 +00:00
|
|
|
* Returns a series of ranges corresponding to scripts. They will be
|
|
|
|
* of the form:
|
|
|
|
*
|
|
|
|
* ccccSScSSccccTTcTcccc - c = common, S = first script, T = second
|
|
|
|
* | | - first run (start, limit)
|
|
|
|
* | | - second run (start, limit)
|
|
|
|
*
|
|
|
|
* That is, the runs will overlap. The reason for this is so that a
|
|
|
|
* transliterator can consider common characters both before and after
|
|
|
|
* the scripts.
|
2002-06-07 20:24:40 +00:00
|
|
|
*/
|
2002-10-04 17:46:52 +00:00
|
|
|
class ScriptRunIterator : public UMemory {
|
2002-06-11 22:36:11 +00:00
|
|
|
private:
|
|
|
|
const Replaceable& text;
|
|
|
|
int32_t textStart;
|
|
|
|
int32_t textLimit;
|
|
|
|
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* The code of the current run, valid after next() returns. May
|
|
|
|
* be USCRIPT_INVALID_CODE if and only if the entire text is
|
|
|
|
* COMMON/INHERITED.
|
|
|
|
*/
|
|
|
|
UScriptCode scriptCode;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The start of the run, inclusive, valid after next() returns.
|
|
|
|
*/
|
|
|
|
int32_t start;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The end of the run, exclusive, valid after next() returns.
|
|
|
|
*/
|
|
|
|
int32_t limit;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructs a run iterator over the given text from start
|
|
|
|
* (inclusive) to limit (exclusive).
|
|
|
|
*/
|
|
|
|
ScriptRunIterator(const Replaceable& text, int32_t start, int32_t limit);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns TRUE if there are any more runs. TRUE is always
|
|
|
|
* returned at least once. Upon return, the caller should
|
|
|
|
* examine scriptCode, start, and limit.
|
|
|
|
*/
|
|
|
|
UBool next();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adjusts internal indices for a change in the limit index of the
|
|
|
|
* given delta. A positive delta means the limit has increased.
|
|
|
|
*/
|
|
|
|
void adjustLimit(int32_t delta);
|
2002-06-29 00:04:16 +00:00
|
|
|
|
|
|
|
private:
|
2002-10-04 17:46:52 +00:00
|
|
|
ScriptRunIterator(const ScriptRunIterator &other); // forbid copying of this class
|
|
|
|
ScriptRunIterator &operator=(const ScriptRunIterator &other); // forbid copying of this class
|
2002-06-11 22:36:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
ScriptRunIterator::ScriptRunIterator(const Replaceable& theText,
|
2002-08-02 00:27:48 +00:00
|
|
|
int32_t myStart, int32_t myLimit) :
|
|
|
|
text(theText)
|
|
|
|
{
|
|
|
|
textStart = myStart;
|
|
|
|
textLimit = myLimit;
|
|
|
|
limit = myStart;
|
2002-06-07 20:24:40 +00:00
|
|
|
}
|
|
|
|
|
2002-06-11 22:36:11 +00:00
|
|
|
UBool ScriptRunIterator::next() {
|
|
|
|
UChar32 ch;
|
|
|
|
UScriptCode s;
|
2002-06-07 20:24:40 +00:00
|
|
|
UErrorCode ec = U_ZERO_ERROR;
|
|
|
|
|
2002-06-11 22:36:11 +00:00
|
|
|
scriptCode = USCRIPT_INVALID_CODE; // don't know script yet
|
|
|
|
start = limit;
|
2002-06-07 20:24:40 +00:00
|
|
|
|
2002-06-11 22:36:11 +00:00
|
|
|
// Are we done?
|
|
|
|
if (start == textLimit) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
2002-06-07 20:24:40 +00:00
|
|
|
|
2002-06-11 22:36:11 +00:00
|
|
|
// Move start back to include adjacent COMMON or INHERITED
|
|
|
|
// characters
|
|
|
|
while (start > textStart) {
|
|
|
|
ch = text.char32At(start - 1); // look back
|
|
|
|
s = uscript_getScript(ch, &ec);
|
|
|
|
if (s == USCRIPT_COMMON || s == USCRIPT_INHERITED) {
|
|
|
|
--start;
|
|
|
|
} else {
|
|
|
|
break;
|
2002-06-07 20:24:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-06-11 22:36:11 +00:00
|
|
|
// Move limit ahead to include COMMON, INHERITED, and characters
|
|
|
|
// of the current script.
|
|
|
|
while (limit < textLimit) {
|
|
|
|
ch = text.char32At(limit); // look ahead
|
|
|
|
s = uscript_getScript(ch, &ec);
|
|
|
|
if (s != USCRIPT_COMMON && s != USCRIPT_INHERITED) {
|
|
|
|
if (scriptCode == USCRIPT_INVALID_CODE) {
|
|
|
|
scriptCode = s;
|
|
|
|
} else if (s != scriptCode) {
|
|
|
|
break;
|
2002-06-07 20:24:40 +00:00
|
|
|
}
|
|
|
|
}
|
2002-06-11 22:36:11 +00:00
|
|
|
++limit;
|
2002-06-07 20:24:40 +00:00
|
|
|
}
|
|
|
|
|
2002-06-11 22:36:11 +00:00
|
|
|
// Return TRUE even if the entire text is COMMON / INHERITED, in
|
|
|
|
// which case scriptCode will be USCRIPT_INVALID_CODE.
|
|
|
|
return TRUE;
|
2002-06-07 20:24:40 +00:00
|
|
|
}
|
|
|
|
|
2002-06-11 22:36:11 +00:00
|
|
|
void ScriptRunIterator::adjustLimit(int32_t delta) {
|
|
|
|
limit += delta;
|
|
|
|
textLimit += delta;
|
|
|
|
}
|
2002-06-07 20:24:40 +00:00
|
|
|
|
2002-06-11 22:36:11 +00:00
|
|
|
//------------------------------------------------------------
|
|
|
|
// AnyTransliterator
|
|
|
|
|
2003-08-31 20:53:46 +00:00
|
|
|
UOBJECT_DEFINE_RTTI_IMPLEMENTATION(AnyTransliterator)
|
2002-06-29 00:04:16 +00:00
|
|
|
|
2002-06-11 22:36:11 +00:00
|
|
|
AnyTransliterator::AnyTransliterator(const UnicodeString& id,
|
|
|
|
const UnicodeString& theTarget,
|
|
|
|
const UnicodeString& theVariant,
|
|
|
|
UScriptCode theTargetScript,
|
|
|
|
UErrorCode& ec) :
|
|
|
|
Transliterator(id, NULL),
|
|
|
|
targetScript(theTargetScript)
|
2002-06-07 20:24:40 +00:00
|
|
|
{
|
2002-06-11 22:36:11 +00:00
|
|
|
cache = uhash_open(uhash_hashLong, uhash_compareLong, &ec);
|
|
|
|
uhash_setValueDeleter(cache, _deleteTransliterator);
|
|
|
|
|
|
|
|
target = theTarget;
|
|
|
|
if (theVariant.length() > 0) {
|
|
|
|
target.append(VARIANT_SEP).append(theVariant);
|
2002-06-07 20:24:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
AnyTransliterator::~AnyTransliterator() {
|
2002-06-11 22:36:11 +00:00
|
|
|
uhash_close(cache);
|
2002-06-07 20:24:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Copy constructor.
|
|
|
|
*/
|
|
|
|
AnyTransliterator::AnyTransliterator(const AnyTransliterator& o) :
|
2002-06-11 22:36:11 +00:00
|
|
|
Transliterator(o),
|
|
|
|
target(o.target),
|
|
|
|
targetScript(o.targetScript)
|
2002-06-07 20:24:40 +00:00
|
|
|
{
|
2002-06-11 22:36:11 +00:00
|
|
|
// Don't copy the cache contents
|
|
|
|
UErrorCode ec = U_ZERO_ERROR;
|
|
|
|
cache = uhash_open(uhash_hashLong, uhash_compareLong, &ec);
|
|
|
|
uhash_setValueDeleter(cache, _deleteTransliterator);
|
2002-06-07 20:24:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Transliterator API.
|
|
|
|
*/
|
|
|
|
Transliterator* AnyTransliterator::clone() const {
|
|
|
|
return new AnyTransliterator(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Implements {@link Transliterator#handleTransliterate}.
|
|
|
|
*/
|
|
|
|
void AnyTransliterator::handleTransliterate(Replaceable& text, UTransPosition& pos,
|
|
|
|
UBool isIncremental) const {
|
2002-06-11 22:36:11 +00:00
|
|
|
int32_t allStart = pos.start;
|
|
|
|
int32_t allLimit = pos.limit;
|
2002-06-07 20:24:40 +00:00
|
|
|
|
2002-06-11 22:36:11 +00:00
|
|
|
ScriptRunIterator it(text, pos.contextStart, pos.contextLimit);
|
2002-06-07 20:24:40 +00:00
|
|
|
|
2002-06-11 22:36:11 +00:00
|
|
|
while (it.next()) {
|
|
|
|
// Ignore runs in the ante context
|
|
|
|
if (it.limit <= allStart) continue;
|
2002-06-07 20:24:40 +00:00
|
|
|
|
2002-06-11 22:36:11 +00:00
|
|
|
// Try to instantiate transliterator from it.scriptCode to
|
|
|
|
// our target or target/variant
|
|
|
|
Transliterator* t = getTransliterator(it.scriptCode);
|
|
|
|
|
|
|
|
if (t == NULL) {
|
|
|
|
// We have no transliterator. Do nothing, but keep
|
|
|
|
// pos.start up to date.
|
|
|
|
pos.start = it.limit;
|
|
|
|
continue;
|
|
|
|
}
|
2002-06-07 20:24:40 +00:00
|
|
|
|
2002-06-11 22:36:11 +00:00
|
|
|
// If the run end is before the transliteration limit, do
|
|
|
|
// a non-incremental transliteration. Otherwise do an
|
|
|
|
// incremental one.
|
|
|
|
UBool incremental = isIncremental && (it.limit >= allLimit);
|
|
|
|
|
|
|
|
pos.start = uprv_max(allStart, it.start);
|
|
|
|
pos.limit = uprv_min(allLimit, it.limit);
|
|
|
|
int32_t limit = pos.limit;
|
|
|
|
t->filteredTransliterate(text, pos, incremental);
|
|
|
|
int32_t delta = pos.limit - limit;
|
|
|
|
allLimit += delta;
|
|
|
|
it.adjustLimit(delta);
|
|
|
|
|
|
|
|
// We're done if we enter the post context
|
|
|
|
if (it.limit >= allLimit) break;
|
2002-06-07 20:24:40 +00:00
|
|
|
}
|
|
|
|
|
2002-06-11 22:36:11 +00:00
|
|
|
// Restore limit. pos.start is fine where the last transliterator
|
|
|
|
// left it, or at the end of the last run.
|
|
|
|
pos.limit = allLimit;
|
|
|
|
}
|
2002-06-07 20:24:40 +00:00
|
|
|
|
2002-06-11 22:36:11 +00:00
|
|
|
Transliterator* AnyTransliterator::getTransliterator(UScriptCode source) const {
|
2002-06-07 20:24:40 +00:00
|
|
|
|
2002-06-11 22:36:11 +00:00
|
|
|
if (source == targetScript || source == USCRIPT_INVALID_CODE) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2002-06-07 20:24:40 +00:00
|
|
|
|
2002-06-11 22:36:11 +00:00
|
|
|
Transliterator* t = (Transliterator*) uhash_iget(cache, (int32_t) source);
|
|
|
|
if (t == NULL) {
|
|
|
|
UErrorCode ec = U_ZERO_ERROR;
|
|
|
|
UnicodeString sourceName(uscript_getName(source), "");
|
|
|
|
UnicodeString id(sourceName);
|
|
|
|
id.append(TARGET_SEP).append(target);
|
|
|
|
|
|
|
|
t = Transliterator::createInstance(id, UTRANS_FORWARD, ec);
|
|
|
|
if (U_FAILURE(ec) || t == NULL) {
|
|
|
|
delete t;
|
|
|
|
|
|
|
|
// Try to pivot around Latin, our most common script
|
|
|
|
id = sourceName;
|
|
|
|
id.append(LATIN_PIVOT).append(target);
|
|
|
|
t = Transliterator::createInstance(id, UTRANS_FORWARD, ec);
|
|
|
|
if (U_FAILURE(ec) || t == NULL) {
|
|
|
|
delete t;
|
|
|
|
t = NULL;
|
2002-06-07 20:24:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-06-11 22:36:11 +00:00
|
|
|
if (t != NULL) {
|
|
|
|
uhash_iput(cache, (int32_t) source, t, &ec);
|
2002-06-07 20:24:40 +00:00
|
|
|
}
|
2002-06-11 22:36:11 +00:00
|
|
|
}
|
2002-06-07 20:24:40 +00:00
|
|
|
|
2002-06-11 22:36:11 +00:00
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the script code for a given name, or -1 if not found.
|
|
|
|
*/
|
|
|
|
UScriptCode AnyTransliterator::scriptNameToCode(const UnicodeString& name) {
|
|
|
|
char buf[128];
|
|
|
|
UScriptCode code;
|
|
|
|
UErrorCode ec = U_ZERO_ERROR;
|
|
|
|
|
|
|
|
name.extract(0, 128, buf, 128, "");
|
|
|
|
if (uscript_getCode(buf, &code, 1, &ec) != 1 ||
|
|
|
|
U_FAILURE(ec)) {
|
|
|
|
code = USCRIPT_INVALID_CODE;
|
2002-06-07 20:24:40 +00:00
|
|
|
}
|
2002-06-11 22:36:11 +00:00
|
|
|
return code;
|
|
|
|
}
|
2002-06-07 20:24:40 +00:00
|
|
|
|
2002-06-11 22:36:11 +00:00
|
|
|
/**
|
|
|
|
* Registers standard transliterators with the system. Called by
|
|
|
|
* Transliterator during initialization. Scan all current targets and
|
|
|
|
* register those that are scripts T as Any-T/V.
|
|
|
|
*/
|
|
|
|
void AnyTransliterator::registerIDs() {
|
|
|
|
|
|
|
|
UErrorCode ec;
|
|
|
|
Hashtable seen(TRUE);
|
|
|
|
|
2002-06-11 22:52:32 +00:00
|
|
|
int32_t sourceCount = Transliterator::_countAvailableSources();
|
2002-06-11 22:36:11 +00:00
|
|
|
for (int32_t s=0; s<sourceCount; ++s) {
|
|
|
|
UnicodeString source;
|
2002-06-11 22:52:32 +00:00
|
|
|
Transliterator::_getAvailableSource(s, source);
|
2002-06-07 20:24:40 +00:00
|
|
|
|
2002-06-11 22:36:11 +00:00
|
|
|
// Ignore the "Any" source
|
|
|
|
if (source.caseCompare(ANY, 0 /*U_FOLD_CASE_DEFAULT*/) == 0) continue;
|
|
|
|
|
2002-06-11 22:52:32 +00:00
|
|
|
int32_t targetCount = Transliterator::_countAvailableTargets(source);
|
2002-06-11 22:36:11 +00:00
|
|
|
for (int32_t t=0; t<targetCount; ++t) {
|
|
|
|
UnicodeString target;
|
2002-06-11 22:52:32 +00:00
|
|
|
Transliterator::_getAvailableTarget(t, source, target);
|
2002-06-11 22:36:11 +00:00
|
|
|
|
|
|
|
// Only process each target once
|
|
|
|
if (seen.geti(target) != 0) continue;
|
|
|
|
ec = U_ZERO_ERROR;
|
|
|
|
seen.puti(target, 1, ec);
|
|
|
|
|
|
|
|
// Get the script code for the target. If not a script, ignore.
|
|
|
|
UScriptCode targetScript = scriptNameToCode(target);
|
|
|
|
if (targetScript == USCRIPT_INVALID_CODE) continue;
|
|
|
|
|
2002-06-11 22:52:32 +00:00
|
|
|
int32_t variantCount = Transliterator::_countAvailableVariants(source, target);
|
2002-06-11 22:36:11 +00:00
|
|
|
// assert(variantCount >= 1);
|
|
|
|
for (int32_t v=0; v<variantCount; ++v) {
|
|
|
|
UnicodeString variant;
|
2002-06-11 22:52:32 +00:00
|
|
|
Transliterator::_getAvailableVariant(v, source, target, variant);
|
2002-06-11 22:36:11 +00:00
|
|
|
|
|
|
|
UnicodeString id;
|
|
|
|
TransliteratorIDParser::STVtoID(ANY, target, variant, id);
|
|
|
|
ec = U_ZERO_ERROR;
|
|
|
|
AnyTransliterator* t = new AnyTransliterator(id, target, variant,
|
|
|
|
targetScript, ec);
|
|
|
|
if (U_FAILURE(ec)) {
|
|
|
|
delete t;
|
|
|
|
} else {
|
|
|
|
Transliterator::_registerInstance(t);
|
|
|
|
Transliterator::_registerSpecialInverse(target, NULL_ID, FALSE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2002-06-07 20:24:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
U_NAMESPACE_END
|
|
|
|
|
2002-09-20 01:54:48 +00:00
|
|
|
#endif /* #if !UCONFIG_NO_TRANSLITERATION */
|
|
|
|
|
2002-06-07 20:24:40 +00:00
|
|
|
//eof
|