1999-11-20 00:40:50 +00:00
|
|
|
/*
|
|
|
|
**********************************************************************
|
2003-06-03 20:58:22 +00:00
|
|
|
* Copyright (C) 1999-2003, International Business Machines
|
1999-11-20 00:40:50 +00:00
|
|
|
* Corporation and others. All Rights Reserved.
|
|
|
|
**********************************************************************
|
|
|
|
* Date Name Description
|
|
|
|
* 11/17/99 aliu Creation.
|
|
|
|
**********************************************************************
|
|
|
|
*/
|
2002-08-21 19:12:24 +00:00
|
|
|
|
2002-09-20 01:54:48 +00:00
|
|
|
#include "unicode/utypes.h"
|
|
|
|
|
|
|
|
#if !UCONFIG_NO_TRANSLITERATION
|
|
|
|
|
2002-07-12 21:42:24 +00:00
|
|
|
#include "unicode/rep.h"
|
|
|
|
#include "unicode/uniset.h"
|
1999-11-20 00:40:50 +00:00
|
|
|
#include "rbt_pars.h"
|
|
|
|
#include "rbt_data.h"
|
|
|
|
#include "rbt_rule.h"
|
2003-02-14 01:44:49 +00:00
|
|
|
#include "rbt.h"
|
2004-02-23 04:36:02 +00:00
|
|
|
#include "umutex.h"
|
1999-11-20 00:40:50 +00:00
|
|
|
|
2001-10-08 23:26:58 +00:00
|
|
|
U_NAMESPACE_BEGIN
|
|
|
|
|
2003-08-29 20:06:23 +00:00
|
|
|
UOBJECT_DEFINE_RTTI_IMPLEMENTATION(RuleBasedTransliterator)
|
2001-06-12 17:35:03 +00:00
|
|
|
|
2004-02-23 04:36:02 +00:00
|
|
|
static UMTX transliteratorDataMutex = NULL;
|
|
|
|
|
1999-11-20 00:40:50 +00:00
|
|
|
void RuleBasedTransliterator::_construct(const UnicodeString& rules,
|
2000-06-27 19:00:38 +00:00
|
|
|
UTransDirection direction,
|
2001-08-31 03:23:39 +00:00
|
|
|
UParseError& parseError,
|
|
|
|
UErrorCode& status) {
|
2003-12-10 01:52:39 +00:00
|
|
|
fData = 0;
|
1999-11-20 00:40:50 +00:00
|
|
|
isDataOwned = TRUE;
|
2001-10-10 19:29:45 +00:00
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return;
|
1999-11-20 00:40:50 +00:00
|
|
|
}
|
2001-10-10 19:29:45 +00:00
|
|
|
|
|
|
|
TransliteratorParser parser;
|
|
|
|
parser.parse(rules, direction, parseError, status);
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (parser.idBlock.length() != 0 ||
|
|
|
|
parser.compoundFilter != NULL) {
|
|
|
|
status = U_INVALID_RBT_SYNTAX; // ::ID blocks disallowed in RBT
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2003-12-10 01:52:39 +00:00
|
|
|
fData = parser.orphanData();
|
|
|
|
setMaximumContextLength(fData->ruleSet.getMaximumContextLength());
|
1999-11-20 00:40:50 +00:00
|
|
|
}
|
|
|
|
|
2003-08-29 20:06:23 +00:00
|
|
|
/**
|
|
|
|
* Constructs a new transliterator from the given rules.
|
|
|
|
* @param id the id for the transliterator.
|
|
|
|
* @param rules rules, separated by ';'
|
|
|
|
* @param direction either FORWARD or REVERSE.
|
|
|
|
* @param adoptedFilter the filter for this transliterator.
|
|
|
|
* @param parseError Struct to recieve information on position
|
|
|
|
* of error if an error is encountered
|
|
|
|
* @param status Output param set to success/failure code.
|
|
|
|
* @exception IllegalArgumentException if rules are malformed
|
|
|
|
* or direction is invalid.
|
|
|
|
*/
|
|
|
|
RuleBasedTransliterator::RuleBasedTransliterator(
|
|
|
|
const UnicodeString& id,
|
|
|
|
const UnicodeString& rules,
|
|
|
|
UTransDirection direction,
|
|
|
|
UnicodeFilter* adoptedFilter,
|
|
|
|
UParseError& parseError,
|
|
|
|
UErrorCode& status) :
|
|
|
|
Transliterator(id, adoptedFilter) {
|
|
|
|
_construct(rules, direction,parseError,status);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructs a new transliterator from the given rules.
|
|
|
|
* @param id the id for the transliterator.
|
|
|
|
* @param rules rules, separated by ';'
|
|
|
|
* @param direction either FORWARD or REVERSE.
|
|
|
|
* @param adoptedFilter the filter for this transliterator.
|
|
|
|
* @param status Output param set to success/failure code.
|
|
|
|
* @exception IllegalArgumentException if rules are malformed
|
|
|
|
* or direction is invalid.
|
|
|
|
*/
|
|
|
|
RuleBasedTransliterator::RuleBasedTransliterator(
|
|
|
|
const UnicodeString& id,
|
|
|
|
const UnicodeString& rules,
|
|
|
|
UTransDirection direction,
|
|
|
|
UnicodeFilter* adoptedFilter,
|
|
|
|
UErrorCode& status) :
|
|
|
|
Transliterator(id, adoptedFilter) {
|
|
|
|
UParseError parseError;
|
|
|
|
_construct(rules, direction,parseError, status);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Covenience constructor with no filter.
|
|
|
|
*/
|
|
|
|
RuleBasedTransliterator::RuleBasedTransliterator(
|
|
|
|
const UnicodeString& id,
|
|
|
|
const UnicodeString& rules,
|
|
|
|
UTransDirection direction,
|
|
|
|
UErrorCode& status) :
|
|
|
|
Transliterator(id, 0) {
|
|
|
|
UParseError parseError;
|
|
|
|
_construct(rules, direction,parseError, status);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Covenience constructor with no filter and FORWARD direction.
|
|
|
|
*/
|
|
|
|
RuleBasedTransliterator::RuleBasedTransliterator(
|
|
|
|
const UnicodeString& id,
|
|
|
|
const UnicodeString& rules,
|
|
|
|
UErrorCode& status) :
|
|
|
|
Transliterator(id, 0) {
|
|
|
|
UParseError parseError;
|
|
|
|
_construct(rules, UTRANS_FORWARD, parseError, status);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Covenience constructor with FORWARD direction.
|
|
|
|
*/
|
|
|
|
RuleBasedTransliterator::RuleBasedTransliterator(
|
|
|
|
const UnicodeString& id,
|
|
|
|
const UnicodeString& rules,
|
|
|
|
UnicodeFilter* adoptedFilter,
|
|
|
|
UErrorCode& status) :
|
|
|
|
Transliterator(id, adoptedFilter) {
|
|
|
|
UParseError parseError;
|
|
|
|
_construct(rules, UTRANS_FORWARD,parseError, status);
|
|
|
|
}
|
|
|
|
|
2000-09-30 00:06:54 +00:00
|
|
|
RuleBasedTransliterator::RuleBasedTransliterator(const UnicodeString& id,
|
1999-11-20 00:40:50 +00:00
|
|
|
const TransliterationRuleData* theData,
|
|
|
|
UnicodeFilter* adoptedFilter) :
|
2000-09-30 00:06:54 +00:00
|
|
|
Transliterator(id, adoptedFilter),
|
2003-12-10 01:52:39 +00:00
|
|
|
fData((TransliterationRuleData*)theData), // cast away const
|
2000-01-18 18:27:27 +00:00
|
|
|
isDataOwned(FALSE) {
|
2003-12-10 01:52:39 +00:00
|
|
|
setMaximumContextLength(fData->ruleSet.getMaximumContextLength());
|
2000-01-18 18:27:27 +00:00
|
|
|
}
|
1999-11-20 00:40:50 +00:00
|
|
|
|
2001-07-13 21:09:41 +00:00
|
|
|
/**
|
|
|
|
* Internal constructor.
|
|
|
|
*/
|
|
|
|
RuleBasedTransliterator::RuleBasedTransliterator(const UnicodeString& id,
|
|
|
|
TransliterationRuleData* theData,
|
|
|
|
UBool isDataAdopted) :
|
|
|
|
Transliterator(id, 0),
|
2003-12-10 01:52:39 +00:00
|
|
|
fData(theData),
|
2001-07-13 21:09:41 +00:00
|
|
|
isDataOwned(isDataAdopted) {
|
2003-12-10 01:52:39 +00:00
|
|
|
setMaximumContextLength(fData->ruleSet.getMaximumContextLength());
|
2001-07-13 21:09:41 +00:00
|
|
|
}
|
|
|
|
|
1999-11-20 00:40:50 +00:00
|
|
|
/**
|
2001-11-16 19:08:46 +00:00
|
|
|
* Copy constructor.
|
1999-11-20 00:40:50 +00:00
|
|
|
*/
|
|
|
|
RuleBasedTransliterator::RuleBasedTransliterator(
|
|
|
|
const RuleBasedTransliterator& other) :
|
2003-12-10 01:52:39 +00:00
|
|
|
Transliterator(other), fData(other.fData),
|
2000-06-30 23:26:07 +00:00
|
|
|
isDataOwned(other.isDataOwned) {
|
|
|
|
|
2001-11-16 19:08:46 +00:00
|
|
|
// The data object may or may not be owned. If it is not owned we
|
|
|
|
// share it; it is invariant. If it is owned, it's still
|
|
|
|
// invariant, but we need to copy it to prevent double-deletion.
|
|
|
|
// If this becomes a performance issue (if people do a lot of RBT
|
|
|
|
// copying -- unlikely) we can reference count the data object.
|
|
|
|
|
|
|
|
// Only do a deep copy if this is owned data, that is, data that
|
|
|
|
// will be later deleted. System transliterators contain
|
|
|
|
// non-owned data.
|
2000-06-30 23:26:07 +00:00
|
|
|
if (isDataOwned) {
|
2003-12-10 01:52:39 +00:00
|
|
|
fData = new TransliterationRuleData(*other.fData);
|
2000-01-18 18:27:27 +00:00
|
|
|
}
|
1999-11-20 01:04:34 +00:00
|
|
|
}
|
1999-11-20 00:40:50 +00:00
|
|
|
|
|
|
|
/**
|
2001-11-16 19:08:46 +00:00
|
|
|
* Destructor.
|
1999-11-20 00:40:50 +00:00
|
|
|
*/
|
1999-11-20 01:04:34 +00:00
|
|
|
RuleBasedTransliterator::~RuleBasedTransliterator() {
|
2001-11-16 19:08:46 +00:00
|
|
|
// Delete the data object only if we own it.
|
1999-11-20 01:04:34 +00:00
|
|
|
if (isDataOwned) {
|
2003-12-10 01:52:39 +00:00
|
|
|
delete fData;
|
1999-11-20 01:04:34 +00:00
|
|
|
}
|
|
|
|
}
|
1999-11-20 00:40:50 +00:00
|
|
|
|
|
|
|
Transliterator* // Covariant return NOT ALLOWED (for portability)
|
1999-12-22 22:57:04 +00:00
|
|
|
RuleBasedTransliterator::clone(void) const {
|
1999-11-20 00:40:50 +00:00
|
|
|
return new RuleBasedTransliterator(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2000-01-19 19:12:42 +00:00
|
|
|
* Implements {@link Transliterator#handleTransliterate}.
|
1999-11-20 00:40:50 +00:00
|
|
|
*/
|
2000-01-19 19:12:42 +00:00
|
|
|
void
|
2000-06-27 19:00:38 +00:00
|
|
|
RuleBasedTransliterator::handleTransliterate(Replaceable& text, UTransPosition& index,
|
2000-05-18 22:08:39 +00:00
|
|
|
UBool isIncremental) const {
|
2001-07-25 19:11:02 +00:00
|
|
|
/* We keep contextStart and contextLimit fixed the entire time,
|
|
|
|
* relative to the text -- contextLimit may move numerically if
|
|
|
|
* text is inserted or removed. The start offset moves toward
|
|
|
|
* limit, with replacements happening under it.
|
1999-11-20 00:40:50 +00:00
|
|
|
*
|
|
|
|
* Example: rules 1. ab>x|y
|
|
|
|
* 2. yc>z
|
|
|
|
*
|
2001-07-25 19:11:02 +00:00
|
|
|
* |eabcd begin - no match, advance start
|
|
|
|
* e|abcd match rule 1 - change text & adjust start
|
|
|
|
* ex|ycd match rule 2 - change text & adjust start
|
|
|
|
* exz|d no match, advance start
|
1999-11-20 00:40:50 +00:00
|
|
|
* exzd| done
|
|
|
|
*/
|
|
|
|
|
2000-02-12 02:05:36 +00:00
|
|
|
/* A rule like
|
|
|
|
* a>b|a
|
|
|
|
* creates an infinite loop. To prevent that, we put an arbitrary
|
|
|
|
* limit on the number of iterations that we take, one that is
|
|
|
|
* high enough that any reasonable rules are ok, but low enough to
|
|
|
|
* prevent a server from hanging. The limit is 16 times the
|
|
|
|
* number of characters n, unless n is so large that 16n exceeds a
|
|
|
|
* uint32_t.
|
|
|
|
*/
|
|
|
|
uint32_t loopCount = 0;
|
2000-06-29 00:18:43 +00:00
|
|
|
uint32_t loopLimit = index.limit - index.start;
|
2000-02-12 02:05:36 +00:00
|
|
|
if (loopLimit >= 0x10000000) {
|
|
|
|
loopLimit = 0xFFFFFFFF;
|
|
|
|
} else {
|
|
|
|
loopLimit <<= 4;
|
|
|
|
}
|
|
|
|
|
2003-12-10 01:52:39 +00:00
|
|
|
if (isDataOwned==FALSE) {
|
2004-02-23 04:36:02 +00:00
|
|
|
umtx_lock(&transliteratorDataMutex);
|
2003-12-10 01:52:39 +00:00
|
|
|
}
|
2001-07-25 19:11:02 +00:00
|
|
|
while (index.start < index.limit &&
|
|
|
|
loopCount <= loopLimit &&
|
2003-12-10 01:52:39 +00:00
|
|
|
fData->ruleSet.transliterate(text, index, isIncremental)) {
|
2001-07-25 19:11:02 +00:00
|
|
|
++loopCount;
|
1999-11-20 00:40:50 +00:00
|
|
|
}
|
2003-12-10 01:52:39 +00:00
|
|
|
if (isDataOwned==FALSE) {
|
2004-02-23 04:36:02 +00:00
|
|
|
umtx_unlock(&transliteratorDataMutex);
|
2003-12-10 01:52:39 +00:00
|
|
|
}
|
1999-11-20 00:40:50 +00:00
|
|
|
}
|
2001-06-12 17:35:03 +00:00
|
|
|
|
|
|
|
UnicodeString& RuleBasedTransliterator::toRules(UnicodeString& rulesSource,
|
|
|
|
UBool escapeUnprintable) const {
|
2003-12-10 01:52:39 +00:00
|
|
|
return fData->ruleSet.toRules(rulesSource, escapeUnprintable);
|
2001-06-12 17:35:03 +00:00
|
|
|
}
|
2001-10-08 23:26:58 +00:00
|
|
|
|
2002-06-28 21:13:54 +00:00
|
|
|
/**
|
|
|
|
* Implement Transliterator framework
|
|
|
|
*/
|
|
|
|
void RuleBasedTransliterator::handleGetSourceSet(UnicodeSet& result) const {
|
2003-12-10 01:52:39 +00:00
|
|
|
fData->ruleSet.getSourceTargetSet(result, FALSE);
|
2002-06-28 21:13:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Override Transliterator framework
|
|
|
|
*/
|
|
|
|
UnicodeSet& RuleBasedTransliterator::getTargetSet(UnicodeSet& result) const {
|
2003-12-10 01:52:39 +00:00
|
|
|
return fData->ruleSet.getSourceTargetSet(result, TRUE);
|
2002-06-28 21:13:54 +00:00
|
|
|
}
|
|
|
|
|
2001-10-08 23:26:58 +00:00
|
|
|
U_NAMESPACE_END
|
|
|
|
|
2002-09-20 01:54:48 +00:00
|
|
|
#endif /* #if !UCONFIG_NO_TRANSLITERATION */
|