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/rbt.h"
|
1999-11-20 00:40:50 +00:00
|
|
|
#include "rbt_pars.h"
|
|
|
|
#include "rbt_data.h"
|
|
|
|
#include "rbt_rule.h"
|
1999-12-28 23:57:50 +00:00
|
|
|
#include "unicode/rep.h"
|
1999-11-20 00:40:50 +00:00
|
|
|
|
|
|
|
void RuleBasedTransliterator::_construct(const UnicodeString& rules,
|
2000-06-27 19:00:38 +00:00
|
|
|
UTransDirection direction,
|
2000-03-18 01:42:45 +00:00
|
|
|
UErrorCode& status,
|
2000-06-27 19:00:38 +00:00
|
|
|
UParseError* parseError) {
|
1999-11-20 00:40:50 +00:00
|
|
|
data = 0;
|
|
|
|
isDataOwned = TRUE;
|
|
|
|
if (U_SUCCESS(status)) {
|
2000-03-18 01:42:45 +00:00
|
|
|
data = TransliterationRuleParser::parse(rules, direction, parseError);
|
1999-11-20 00:40:50 +00:00
|
|
|
if (data == 0) {
|
|
|
|
status = U_ILLEGAL_ARGUMENT_ERROR;
|
2000-01-18 18:27:27 +00:00
|
|
|
} else {
|
|
|
|
setMaximumContextLength(data->ruleSet.getMaximumContextLength());
|
1999-11-20 00:40:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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),
|
1999-11-20 01:04:34 +00:00
|
|
|
data((TransliterationRuleData*)theData), // cast away const
|
2000-01-18 18:27:27 +00:00
|
|
|
isDataOwned(FALSE) {
|
|
|
|
setMaximumContextLength(data->ruleSet.getMaximumContextLength());
|
|
|
|
}
|
1999-11-20 00:40:50 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Copy constructor. Since the data object is immutable, we can share
|
|
|
|
* it with other objects -- no need to clone it.
|
|
|
|
*/
|
|
|
|
RuleBasedTransliterator::RuleBasedTransliterator(
|
|
|
|
const RuleBasedTransliterator& other) :
|
2000-06-30 23:26:07 +00:00
|
|
|
Transliterator(other), data(other.data),
|
|
|
|
isDataOwned(other.isDataOwned) {
|
|
|
|
|
|
|
|
// Only do a deep copy if this is non-owned data, that is,
|
|
|
|
// data that will be later deleted. System transliterators
|
|
|
|
// contain owned data.
|
|
|
|
if (isDataOwned) {
|
|
|
|
data = new TransliterationRuleData(*other.data);
|
2000-01-18 18:27:27 +00:00
|
|
|
}
|
1999-11-20 01:04:34 +00:00
|
|
|
}
|
1999-11-20 00:40:50 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Destructor. We do NOT own the data object, so we do not delete it.
|
|
|
|
*/
|
1999-11-20 01:04:34 +00:00
|
|
|
RuleBasedTransliterator::~RuleBasedTransliterator() {
|
|
|
|
if (isDataOwned) {
|
|
|
|
delete data;
|
|
|
|
}
|
|
|
|
}
|
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 {
|
2000-01-19 19:12:42 +00:00
|
|
|
/* We keep start and limit fixed the entire time,
|
1999-11-20 00:40:50 +00:00
|
|
|
* relative to the text -- limit may move numerically if text is
|
|
|
|
* inserted or removed. The cursor moves from start to limit, with
|
|
|
|
* replacements happening under it.
|
|
|
|
*
|
|
|
|
* Example: rules 1. ab>x|y
|
|
|
|
* 2. yc>z
|
|
|
|
*
|
|
|
|
* |eabcd start - no match, advance cursor
|
|
|
|
* e|abcd match rule 1 - change text & adjust cursor
|
|
|
|
* ex|ycd match rule 2 - change text & adjust cursor
|
|
|
|
* exz|d no match, advance cursor
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
|
2000-05-18 22:08:39 +00:00
|
|
|
UBool isPartial = FALSE;
|
1999-11-20 00:40:50 +00:00
|
|
|
|
2000-06-29 00:18:43 +00:00
|
|
|
while (index.start < index.limit && loopCount <= loopLimit) {
|
2000-01-19 19:12:42 +00:00
|
|
|
TransliterationRule* r = isIncremental ?
|
2000-06-29 00:18:43 +00:00
|
|
|
data->ruleSet.findIncrementalMatch(text, index, *data, isPartial,
|
2000-01-19 19:12:42 +00:00
|
|
|
getFilter()) :
|
2000-06-29 00:18:43 +00:00
|
|
|
data->ruleSet.findMatch(text, index, *data,
|
2000-01-19 19:12:42 +00:00
|
|
|
getFilter());
|
|
|
|
|
1999-11-20 00:40:50 +00:00
|
|
|
/* If we match a rule then apply it by replacing the key
|
|
|
|
* with the rule output and repositioning the cursor
|
|
|
|
* appropriately. If we get a partial match, then we
|
|
|
|
* can't do anything without more text; return with the
|
|
|
|
* cursor at the current position. If we get null, then
|
|
|
|
* there is no match at this position, and we can advance
|
|
|
|
* the cursor.
|
|
|
|
*/
|
|
|
|
if (r == 0) {
|
2000-01-19 19:12:42 +00:00
|
|
|
if (isPartial) { // always FALSE unless isIncremental
|
1999-11-20 00:40:50 +00:00
|
|
|
break;
|
|
|
|
} else {
|
2000-06-29 00:18:43 +00:00
|
|
|
++index.start;
|
1999-11-20 00:40:50 +00:00
|
|
|
}
|
|
|
|
} else {
|
2000-05-20 04:40:29 +00:00
|
|
|
// Delegate replacement to TransliterationRule object
|
2000-06-29 00:18:43 +00:00
|
|
|
int32_t lenDelta = r->replace(text, index.start, *data);
|
|
|
|
index.limit += lenDelta;
|
|
|
|
index.contextLimit += lenDelta;
|
|
|
|
index.start += r->getCursorPos();
|
2000-02-12 02:05:36 +00:00
|
|
|
++loopCount;
|
1999-11-20 00:40:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|