1999-11-20 00:40:50 +00:00
|
|
|
/*
|
|
|
|
**********************************************************************
|
2001-03-21 20:31:13 +00:00
|
|
|
* Copyright (C) 1999-2001, 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.
|
|
|
|
**********************************************************************
|
|
|
|
*/
|
|
|
|
#include "rbt_rule.h"
|
1999-12-28 23:57:50 +00:00
|
|
|
#include "unicode/rep.h"
|
1999-11-20 00:40:50 +00:00
|
|
|
#include "rbt_data.h"
|
1999-12-28 23:57:50 +00:00
|
|
|
#include "unicode/unifilt.h"
|
|
|
|
#include "unicode/uniset.h"
|
2000-06-30 23:26:07 +00:00
|
|
|
#include "cmemory.h"
|
2001-10-30 18:08:53 +00:00
|
|
|
#include "strmatch.h"
|
2002-02-07 01:40:01 +00:00
|
|
|
#include "strrepl.h"
|
2001-11-21 22:43:21 +00:00
|
|
|
#include "util.h"
|
1999-11-20 00:40:50 +00:00
|
|
|
|
2002-02-07 01:40:01 +00:00
|
|
|
static const UChar FORWARD_OP[] = {32,62,32,0}; // " > "
|
2001-07-18 20:09:47 +00:00
|
|
|
|
2001-10-08 23:26:58 +00:00
|
|
|
U_NAMESPACE_BEGIN
|
|
|
|
|
2000-05-20 04:40:29 +00:00
|
|
|
/**
|
|
|
|
* Construct a new rule with the given input, output text, and other
|
|
|
|
* attributes. A cursor position may be specified for the output text.
|
|
|
|
* @param input input string, including key and optional ante and
|
|
|
|
* post context
|
|
|
|
* @param anteContextPos offset into input to end of ante context, or -1 if
|
|
|
|
* none. Must be <= input.length() if not -1.
|
|
|
|
* @param postContextPos offset into input to start of post context, or -1
|
|
|
|
* if none. Must be <= input.length() if not -1, and must be >=
|
|
|
|
* anteContextPos.
|
|
|
|
* @param output output string
|
2001-01-23 20:31:11 +00:00
|
|
|
* @param cursorPosition offset into output at which cursor is located, or -1 if
|
2000-05-20 04:40:29 +00:00
|
|
|
* none. If less than zero, then the cursor is placed after the
|
|
|
|
* <code>output</code>; that is, -1 is equivalent to
|
|
|
|
* <code>output.length()</code>. If greater than
|
|
|
|
* <code>output.length()</code> then an exception is thrown.
|
2002-02-07 01:40:01 +00:00
|
|
|
* @param segs array of UnicodeFunctors corresponding to input pattern
|
2001-10-30 18:08:53 +00:00
|
|
|
* segments, or null if there are none. The array itself is adopted,
|
|
|
|
* but the pointers within it are not.
|
|
|
|
* @param segsCount number of elements in segs[]
|
2000-08-30 20:43:25 +00:00
|
|
|
* @param anchorStart TRUE if the the rule is anchored on the left to
|
|
|
|
* the context start
|
|
|
|
* @param anchorEnd TRUE if the rule is anchored on the right to the
|
|
|
|
* context limit
|
2000-05-20 04:40:29 +00:00
|
|
|
*/
|
|
|
|
TransliterationRule::TransliterationRule(const UnicodeString& input,
|
|
|
|
int32_t anteContextPos, int32_t postContextPos,
|
2001-01-23 20:31:11 +00:00
|
|
|
const UnicodeString& outputStr,
|
|
|
|
int32_t cursorPosition, int32_t cursorOffset,
|
2002-02-07 01:40:01 +00:00
|
|
|
UnicodeFunctor** segs,
|
2001-10-30 18:08:53 +00:00
|
|
|
int32_t segsCount,
|
2000-08-30 20:43:25 +00:00
|
|
|
UBool anchorStart, UBool anchorEnd,
|
2001-09-18 00:24:14 +00:00
|
|
|
const TransliterationRuleData* theData,
|
2001-07-25 19:11:02 +00:00
|
|
|
UErrorCode& status) :
|
2001-11-14 19:26:56 +00:00
|
|
|
segments(0),
|
2001-07-25 19:11:02 +00:00
|
|
|
data(theData) {
|
2000-06-30 23:26:07 +00:00
|
|
|
|
2000-01-13 07:28:08 +00:00
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Do range checks only when warranted to save time
|
|
|
|
if (anteContextPos < 0) {
|
|
|
|
anteContextLength = 0;
|
1999-11-20 00:40:50 +00:00
|
|
|
} else {
|
2000-01-13 07:28:08 +00:00
|
|
|
if (anteContextPos > input.length()) {
|
|
|
|
// throw new IllegalArgumentException("Invalid ante context");
|
|
|
|
status = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
anteContextLength = anteContextPos;
|
1999-11-20 00:40:50 +00:00
|
|
|
}
|
2000-01-13 07:28:08 +00:00
|
|
|
if (postContextPos < 0) {
|
|
|
|
keyLength = input.length() - anteContextLength;
|
|
|
|
} else {
|
|
|
|
if (postContextPos < anteContextLength ||
|
|
|
|
postContextPos > input.length()) {
|
|
|
|
// throw new IllegalArgumentException("Invalid post context");
|
|
|
|
status = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
keyLength = postContextPos - anteContextLength;
|
|
|
|
}
|
2001-01-23 20:31:11 +00:00
|
|
|
if (cursorPosition < 0) {
|
|
|
|
cursorPosition = outputStr.length();
|
2001-12-11 22:11:51 +00:00
|
|
|
} else if (cursorPosition > outputStr.length()) {
|
|
|
|
// throw new IllegalArgumentException("Invalid cursor position");
|
|
|
|
status = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return;
|
2000-01-13 07:28:08 +00:00
|
|
|
}
|
2000-05-20 04:40:29 +00:00
|
|
|
// We don't validate the segments array. The caller must
|
2001-10-30 18:08:53 +00:00
|
|
|
// guarantee that the segments are well-formed (that is, that
|
|
|
|
// all $n references in the output refer to indices of this
|
|
|
|
// array, and that no array elements are null).
|
|
|
|
this->segments = segs;
|
|
|
|
this->segmentsCount = segsCount;
|
2001-07-25 19:11:02 +00:00
|
|
|
|
|
|
|
pattern = input;
|
|
|
|
flags = 0;
|
|
|
|
if (anchorStart) {
|
|
|
|
flags |= ANCHOR_START;
|
|
|
|
}
|
|
|
|
if (anchorEnd) {
|
|
|
|
flags |= ANCHOR_END;
|
2000-08-30 20:43:25 +00:00
|
|
|
}
|
2001-12-11 22:11:51 +00:00
|
|
|
|
|
|
|
anteContext = NULL;
|
|
|
|
if (anteContextLength > 0) {
|
|
|
|
anteContext = new StringMatcher(pattern, 0, anteContextLength,
|
|
|
|
FALSE, *data);
|
|
|
|
}
|
|
|
|
|
|
|
|
key = NULL;
|
|
|
|
if (keyLength > 0) {
|
|
|
|
key = new StringMatcher(pattern, anteContextLength, anteContextLength + keyLength,
|
|
|
|
FALSE, *data);
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t postContextLength = pattern.length() - keyLength - anteContextLength;
|
|
|
|
postContext = NULL;
|
|
|
|
if (postContextLength > 0) {
|
|
|
|
postContext = new StringMatcher(pattern, anteContextLength + keyLength, pattern.length(),
|
|
|
|
FALSE, *data);
|
|
|
|
}
|
2002-02-07 01:40:01 +00:00
|
|
|
|
|
|
|
this->output = new StringReplacer(outputStr, cursorPosition + cursorOffset, data);
|
1999-11-20 00:40:50 +00:00
|
|
|
}
|
|
|
|
|
2001-08-04 00:15:12 +00:00
|
|
|
/**
|
|
|
|
* Copy constructor.
|
|
|
|
*/
|
|
|
|
TransliterationRule::TransliterationRule(TransliterationRule& other) :
|
2001-12-11 22:11:51 +00:00
|
|
|
anteContext(NULL),
|
|
|
|
key(NULL),
|
|
|
|
postContext(NULL),
|
2001-08-04 00:15:12 +00:00
|
|
|
pattern(other.pattern),
|
|
|
|
anteContextLength(other.anteContextLength),
|
|
|
|
keyLength(other.keyLength),
|
|
|
|
flags(other.flags),
|
|
|
|
data(other.data) {
|
|
|
|
|
2001-10-30 18:08:53 +00:00
|
|
|
segments = NULL;
|
|
|
|
segmentsCount = 0;
|
|
|
|
if (other.segmentsCount > 0) {
|
2002-02-07 01:40:01 +00:00
|
|
|
segments = new UnicodeFunctor*[other.segmentsCount];
|
2001-10-30 18:08:53 +00:00
|
|
|
uprv_memcpy(segments, other.segments, other.segmentsCount*sizeof(segments[0]));
|
2001-08-04 00:15:12 +00:00
|
|
|
}
|
2001-12-11 22:11:51 +00:00
|
|
|
|
|
|
|
if (other.anteContext != NULL) {
|
|
|
|
anteContext = (StringMatcher*) other.anteContext->clone();
|
|
|
|
}
|
|
|
|
if (other.key != NULL) {
|
|
|
|
key = (StringMatcher*) other.key->clone();
|
|
|
|
}
|
|
|
|
if (other.postContext != NULL) {
|
|
|
|
postContext = (StringMatcher*) other.postContext->clone();
|
|
|
|
}
|
2002-02-07 01:40:01 +00:00
|
|
|
output = other.output->clone();
|
2001-08-04 00:15:12 +00:00
|
|
|
}
|
|
|
|
|
2000-05-20 04:40:29 +00:00
|
|
|
TransliterationRule::~TransliterationRule() {
|
|
|
|
delete[] segments;
|
2001-12-11 22:11:51 +00:00
|
|
|
delete anteContext;
|
|
|
|
delete key;
|
|
|
|
delete postContext;
|
2002-02-07 01:40:01 +00:00
|
|
|
delete output;
|
1999-11-20 00:40:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the preceding context length. This method is needed to
|
|
|
|
* support the <code>Transliterator</code> method
|
2001-07-25 19:11:02 +00:00
|
|
|
* <code>getMaximumContextLength()</code>. Internally, this is
|
|
|
|
* implemented as the anteContextLength, optionally plus one if
|
|
|
|
* there is a start anchor. The one character anchor gap is
|
|
|
|
* needed to make repeated incremental transliteration with
|
|
|
|
* anchors work.
|
1999-11-20 00:40:50 +00:00
|
|
|
*/
|
2001-07-25 19:11:02 +00:00
|
|
|
int32_t TransliterationRule::getContextLength(void) const {
|
|
|
|
return anteContextLength + ((flags & ANCHOR_START) ? 1 : 0);
|
2000-01-13 07:28:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal method. Returns 8-bit index value for this rule.
|
|
|
|
* This is the low byte of the first character of the key,
|
|
|
|
* unless the first character of the key is a set. If it's a
|
|
|
|
* set, or otherwise can match multiple keys, the index value is -1.
|
|
|
|
*/
|
2001-07-25 19:11:02 +00:00
|
|
|
int16_t TransliterationRule::getIndexValue() const {
|
2000-01-13 07:28:08 +00:00
|
|
|
if (anteContextLength == pattern.length()) {
|
|
|
|
// A pattern with just ante context {such as foo)>bar} can
|
|
|
|
// match any key.
|
|
|
|
return -1;
|
|
|
|
}
|
2001-07-25 19:11:02 +00:00
|
|
|
UChar32 c = pattern.char32At(anteContextLength);
|
2002-02-07 01:40:01 +00:00
|
|
|
return (int16_t)(data->lookupMatcher(c) == NULL ? (c & 0xFF) : -1);
|
2000-01-13 07:28:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal method. Returns true if this rule matches the given
|
|
|
|
* index value. The index value is an 8-bit integer, 0..255,
|
|
|
|
* representing the low byte of the first character of the key.
|
|
|
|
* It matches this rule if it matches the first character of the
|
|
|
|
* key, or if the first character of the key is a set, and the set
|
|
|
|
* contains any character with a low byte equal to the index
|
|
|
|
* value. If the rule contains only ante context, as in foo)>bar,
|
|
|
|
* then it will match any key.
|
|
|
|
*/
|
2001-07-25 19:11:02 +00:00
|
|
|
UBool TransliterationRule::matchesIndexValue(uint8_t v) const {
|
2001-12-11 22:11:51 +00:00
|
|
|
// Delegate to the key, or if there is none, to the postContext.
|
|
|
|
// If there is neither then we match any key; return true.
|
|
|
|
UnicodeMatcher *m = (key != NULL) ? key : postContext;
|
|
|
|
return (m != NULL) ? m->matchesIndexValue(v) : TRUE;
|
1999-11-20 00:40:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return true if this rule masks another rule. If r1 masks r2 then
|
|
|
|
* r1 matches any input string that r2 matches. If r1 masks r2 and r2 masks
|
|
|
|
* r1 then r1 == r2. Examples: "a>x" masks "ab>y". "a>x" masks "a[b]>y".
|
|
|
|
* "[c]a>x" masks "[dc]a>y".
|
|
|
|
*/
|
2000-05-18 22:08:39 +00:00
|
|
|
UBool TransliterationRule::masks(const TransliterationRule& r2) const {
|
2000-01-13 07:28:08 +00:00
|
|
|
/* Rule r1 masks rule r2 if the string formed of the
|
|
|
|
* antecontext, key, and postcontext overlaps in the following
|
|
|
|
* way:
|
1999-11-20 00:40:50 +00:00
|
|
|
*
|
2000-01-13 07:28:08 +00:00
|
|
|
* r1: aakkkpppp
|
|
|
|
* r2: aaakkkkkpppp
|
|
|
|
* ^
|
|
|
|
*
|
|
|
|
* The strings must be aligned at the first character of the
|
|
|
|
* key. The length of r1 to the left of the alignment point
|
|
|
|
* must be <= the length of r2 to the left; ditto for the
|
|
|
|
* right. The characters of r1 must equal (or be a superset
|
|
|
|
* of) the corresponding characters of r2. The superset
|
|
|
|
* operation should be performed to check for UnicodeSet
|
|
|
|
* masking.
|
2001-07-25 19:11:02 +00:00
|
|
|
*
|
|
|
|
* Anchors: Two patterns that differ only in anchors only
|
|
|
|
* mask one another if they are exactly equal, and r2 has
|
|
|
|
* all the anchors r1 has (optionally, plus some). Here Y
|
|
|
|
* means the row masks the column, N means it doesn't.
|
|
|
|
*
|
|
|
|
* ab ^ab ab$ ^ab$
|
|
|
|
* ab Y Y Y Y
|
|
|
|
* ^ab N Y N Y
|
|
|
|
* ab$ N N Y Y
|
|
|
|
* ^ab$ N N N Y
|
|
|
|
*
|
|
|
|
* Post context: {a}b masks ab, but not vice versa, since {a}b
|
|
|
|
* matches everything ab matches, and {a}b matches {|a|}b but ab
|
|
|
|
* does not. Pre context is different (a{b} does not align with
|
|
|
|
* ab).
|
1999-11-20 00:40:50 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* LIMITATION of the current mask algorithm: Some rule
|
|
|
|
* maskings are currently not detected. For example,
|
2000-01-13 07:28:08 +00:00
|
|
|
* "{Lu}]a>x" masks "A]a>y". This can be added later. TODO
|
1999-11-20 00:40:50 +00:00
|
|
|
*/
|
|
|
|
|
2000-01-13 07:28:08 +00:00
|
|
|
int32_t len = pattern.length();
|
|
|
|
int32_t left = anteContextLength;
|
|
|
|
int32_t left2 = r2.anteContextLength;
|
|
|
|
int32_t right = len - left;
|
|
|
|
int32_t right2 = r2.pattern.length() - left2;
|
2001-07-25 19:11:02 +00:00
|
|
|
|
|
|
|
// TODO Clean this up -- some logic might be combinable with the
|
|
|
|
// next statement.
|
|
|
|
|
|
|
|
// Test for anchor masking
|
|
|
|
if (left == left2 && right == right2 &&
|
|
|
|
keyLength <= r2.keyLength &&
|
|
|
|
0 == r2.pattern.compare(0, len, pattern)) {
|
|
|
|
// The following boolean logic implements the table above
|
|
|
|
return (flags == r2.flags) ||
|
|
|
|
(!(flags & ANCHOR_START) && !(flags & ANCHOR_END)) ||
|
|
|
|
((r2.flags & ANCHOR_START) && (r2.flags & ANCHOR_END));
|
|
|
|
}
|
|
|
|
|
|
|
|
return left <= left2 &&
|
|
|
|
(right < right2 ||
|
|
|
|
(right == right2 && keyLength <= r2.keyLength)) &&
|
2000-01-13 07:28:08 +00:00
|
|
|
0 == r2.pattern.compare(left2 - left, len, pattern);
|
1999-11-20 00:40:50 +00:00
|
|
|
}
|
|
|
|
|
2001-10-23 00:44:49 +00:00
|
|
|
static inline int32_t posBefore(const Replaceable& str, int32_t pos) {
|
2001-08-04 00:15:12 +00:00
|
|
|
return (pos > 0) ?
|
|
|
|
pos - UTF_CHAR_LENGTH(str.char32At(pos-1)) :
|
|
|
|
pos - 1;
|
|
|
|
}
|
|
|
|
|
2001-10-23 00:44:49 +00:00
|
|
|
static inline int32_t posAfter(const Replaceable& str, int32_t pos) {
|
2001-09-28 21:00:14 +00:00
|
|
|
return (pos >= 0 && pos < str.length()) ?
|
2001-08-04 00:15:12 +00:00
|
|
|
pos + UTF_CHAR_LENGTH(str.char32At(pos)) :
|
|
|
|
pos + 1;
|
|
|
|
}
|
|
|
|
|
1999-11-20 00:40:50 +00:00
|
|
|
/**
|
2001-07-25 19:11:02 +00:00
|
|
|
* Attempt a match and replacement at the given position. Return
|
|
|
|
* the degree of match between this rule and the given text. The
|
|
|
|
* degree of match may be mismatch, a partial match, or a full
|
|
|
|
* match. A mismatch means at least one character of the text
|
|
|
|
* does not match the context or key. A partial match means some
|
|
|
|
* context and key characters match, but the text is not long
|
|
|
|
* enough to match all of them. A full match means all context
|
|
|
|
* and key characters match.
|
|
|
|
*
|
|
|
|
* If a full match is obtained, perform a replacement, update pos,
|
|
|
|
* and return U_MATCH. Otherwise both text and pos are unchanged.
|
|
|
|
*
|
|
|
|
* @param text the text
|
|
|
|
* @param pos the position indices
|
|
|
|
* @param incremental if TRUE, test for partial matches that may
|
|
|
|
* be completed by additional text inserted at pos.limit.
|
|
|
|
* @return one of <code>U_MISMATCH</code>,
|
|
|
|
* <code>U_PARTIAL_MATCH</code>, or <code>U_MATCH</code>. If
|
|
|
|
* incremental is FALSE then U_PARTIAL_MATCH will not be returned.
|
1999-11-20 00:40:50 +00:00
|
|
|
*/
|
2001-07-25 19:11:02 +00:00
|
|
|
UMatchDegree TransliterationRule::matchAndReplace(Replaceable& text,
|
|
|
|
UTransPosition& pos,
|
|
|
|
UBool incremental) const {
|
|
|
|
// Matching and replacing are done in one method because the
|
|
|
|
// replacement operation needs information obtained during the
|
|
|
|
// match. Another way to do this is to have the match method
|
|
|
|
// create a match result struct with relevant offsets, and to pass
|
|
|
|
// this into the replace method.
|
|
|
|
|
|
|
|
// ============================ MATCH ===========================
|
|
|
|
|
2001-10-30 18:08:53 +00:00
|
|
|
// Reset segment match data
|
|
|
|
if (segments != NULL) {
|
|
|
|
for (int32_t i=0; i<segmentsCount; ++i) {
|
|
|
|
((StringMatcher*) segments[i])->resetMatch();
|
|
|
|
}
|
2001-07-30 23:23:51 +00:00
|
|
|
}
|
2001-07-25 19:11:02 +00:00
|
|
|
|
2002-02-07 01:40:01 +00:00
|
|
|
// int32_t lenDelta, keyLimit;
|
|
|
|
int32_t keyLimit;
|
2001-07-30 23:23:51 +00:00
|
|
|
|
2001-07-25 19:11:02 +00:00
|
|
|
// ------------------------ Ante Context ------------------------
|
|
|
|
|
|
|
|
// A mismatch in the ante context, or with the start anchor,
|
|
|
|
// is an outright U_MISMATCH regardless of whether we are
|
|
|
|
// incremental or not.
|
2001-09-26 18:04:13 +00:00
|
|
|
int32_t oText; // offset into 'text'
|
2002-02-07 01:40:01 +00:00
|
|
|
// int32_t newStart = 0;
|
2001-09-26 18:04:13 +00:00
|
|
|
int32_t minOText;
|
2001-07-27 00:18:53 +00:00
|
|
|
|
2001-12-11 22:11:51 +00:00
|
|
|
// Note (1): We process text in 16-bit code units, rather than
|
|
|
|
// 32-bit code points. This works because stand-ins are
|
|
|
|
// always in the BMP and because we are doing a literal match
|
|
|
|
// operation, which can be done 16-bits at a time.
|
|
|
|
|
|
|
|
int32_t anteLimit = posBefore(text, pos.contextStart);
|
|
|
|
|
|
|
|
UMatchDegree match;
|
|
|
|
|
|
|
|
// Start reverse match at char before pos.start
|
2001-09-26 18:04:13 +00:00
|
|
|
oText = posBefore(text, pos.start);
|
2001-07-27 00:18:53 +00:00
|
|
|
|
2001-12-11 22:11:51 +00:00
|
|
|
if (anteContext != NULL) {
|
|
|
|
match = anteContext->matches(text, oText, anteLimit, FALSE);
|
|
|
|
if (match != U_MATCH) {
|
|
|
|
return U_MISMATCH;
|
2001-07-27 00:18:53 +00:00
|
|
|
}
|
2000-01-19 19:25:28 +00:00
|
|
|
}
|
1999-11-20 00:40:50 +00:00
|
|
|
|
2001-09-26 18:04:13 +00:00
|
|
|
minOText = posAfter(text, oText);
|
2001-08-04 00:15:12 +00:00
|
|
|
|
2001-07-25 19:11:02 +00:00
|
|
|
// ------------------------ Start Anchor ------------------------
|
2001-12-11 22:11:51 +00:00
|
|
|
|
|
|
|
if (((flags & ANCHOR_START) != 0) && oText != anteLimit) {
|
2001-10-30 18:08:53 +00:00
|
|
|
return U_MISMATCH;
|
1999-11-20 00:40:50 +00:00
|
|
|
}
|
2001-07-25 19:11:02 +00:00
|
|
|
|
|
|
|
// -------------------- Key and Post Context --------------------
|
2001-12-11 22:11:51 +00:00
|
|
|
|
2001-09-26 18:04:13 +00:00
|
|
|
oText = pos.start;
|
2001-10-25 23:23:12 +00:00
|
|
|
|
2001-12-11 22:11:51 +00:00
|
|
|
if (key != NULL) {
|
|
|
|
match = key->matches(text, oText, pos.limit, incremental);
|
|
|
|
if (match != U_MATCH) {
|
|
|
|
return match;
|
2001-07-25 19:11:02 +00:00
|
|
|
}
|
2001-12-11 22:11:51 +00:00
|
|
|
}
|
2001-10-25 23:23:12 +00:00
|
|
|
|
2001-12-11 22:11:51 +00:00
|
|
|
keyLimit = oText;
|
|
|
|
|
|
|
|
if (postContext != NULL) {
|
|
|
|
if (incremental && keyLimit == pos.limit) {
|
|
|
|
// The key matches just before pos.limit, and there is
|
|
|
|
// a postContext. Since we are in incremental mode,
|
|
|
|
// we must assume more characters may be inserted at
|
|
|
|
// pos.limit -- this is a partial match.
|
|
|
|
return U_PARTIAL_MATCH;
|
1999-11-20 00:40:50 +00:00
|
|
|
}
|
2001-10-25 23:23:12 +00:00
|
|
|
|
2001-12-11 22:11:51 +00:00
|
|
|
match = postContext->matches(text, oText, pos.contextLimit, incremental);
|
|
|
|
if (match != U_MATCH) {
|
|
|
|
return match;
|
|
|
|
}
|
2001-07-25 19:11:02 +00:00
|
|
|
}
|
2001-12-11 22:11:51 +00:00
|
|
|
|
2001-07-25 19:11:02 +00:00
|
|
|
// ------------------------- Stop Anchor ------------------------
|
2001-12-11 22:11:51 +00:00
|
|
|
|
|
|
|
if (((flags & ANCHOR_END)) != 0) {
|
2001-09-26 18:04:13 +00:00
|
|
|
if (oText != pos.contextLimit) {
|
2001-07-25 19:11:02 +00:00
|
|
|
return U_MISMATCH;
|
|
|
|
}
|
|
|
|
if (incremental) {
|
|
|
|
return U_PARTIAL_MATCH;
|
|
|
|
}
|
|
|
|
}
|
2001-12-11 22:11:51 +00:00
|
|
|
|
2001-07-25 19:11:02 +00:00
|
|
|
// =========================== REPLACE ==========================
|
|
|
|
|
|
|
|
// We have a full match. The key is between pos.start and
|
2001-10-30 18:08:53 +00:00
|
|
|
// keyLimit.
|
2001-07-25 19:11:02 +00:00
|
|
|
|
2002-02-07 01:40:01 +00:00
|
|
|
int32_t newStart;
|
|
|
|
int32_t newLength = output->toReplacer()->replace(text, pos.start, keyLimit, newStart);
|
|
|
|
int32_t lenDelta = newLength - (keyLimit - pos.start);
|
|
|
|
|
2001-09-26 18:04:13 +00:00
|
|
|
oText += lenDelta;
|
2001-07-25 19:11:02 +00:00
|
|
|
pos.limit += lenDelta;
|
|
|
|
pos.contextLimit += lenDelta;
|
2001-09-26 18:04:13 +00:00
|
|
|
// Restrict new value of start to [minOText, min(oText, pos.limit)].
|
|
|
|
pos.start = uprv_max(minOText, uprv_min(uprv_min(oText, pos.limit), newStart));
|
2001-10-30 18:08:53 +00:00
|
|
|
return U_MATCH;
|
1999-11-20 00:40:50 +00:00
|
|
|
}
|
2000-08-30 20:43:25 +00:00
|
|
|
|
2001-06-12 18:02:16 +00:00
|
|
|
/**
|
|
|
|
* Create a source string that represents this rule. Append it to the
|
|
|
|
* given string.
|
|
|
|
*/
|
|
|
|
UnicodeString& TransliterationRule::toRule(UnicodeString& rule,
|
|
|
|
UBool escapeUnprintable) const {
|
|
|
|
|
|
|
|
// Accumulate special characters (and non-specials following them)
|
|
|
|
// into quoteBuf. Append quoteBuf, within single quotes, when
|
|
|
|
// a non-quoted element must be inserted.
|
|
|
|
UnicodeString str, quoteBuf;
|
|
|
|
|
|
|
|
// Do not emit the braces '{' '}' around the pattern if there
|
|
|
|
// is neither anteContext nor postContext.
|
|
|
|
UBool emitBraces =
|
2001-12-11 22:11:51 +00:00
|
|
|
(anteContext != NULL) || (postContext != NULL);
|
2001-06-12 18:02:16 +00:00
|
|
|
|
2001-10-18 23:05:25 +00:00
|
|
|
// Emit start anchor
|
|
|
|
if ((flags & ANCHOR_START) != 0) {
|
|
|
|
rule.append((UChar)94/*^*/);
|
|
|
|
}
|
|
|
|
|
2001-06-12 18:02:16 +00:00
|
|
|
// Emit the input pattern
|
2002-02-07 01:40:01 +00:00
|
|
|
ICU_Utility::appendToRule(rule, anteContext, escapeUnprintable, quoteBuf);
|
2001-06-12 18:02:16 +00:00
|
|
|
|
2001-12-11 22:11:51 +00:00
|
|
|
if (emitBraces) {
|
2002-02-07 01:40:01 +00:00
|
|
|
ICU_Utility::appendToRule(rule, (UChar) 0x007B /*{*/, TRUE, escapeUnprintable, quoteBuf);
|
2001-06-12 18:02:16 +00:00
|
|
|
}
|
|
|
|
|
2002-02-07 01:40:01 +00:00
|
|
|
ICU_Utility::appendToRule(rule, key, escapeUnprintable, quoteBuf);
|
2001-12-11 22:11:51 +00:00
|
|
|
|
|
|
|
if (emitBraces) {
|
2002-02-07 01:40:01 +00:00
|
|
|
ICU_Utility::appendToRule(rule, (UChar) 0x007D /*}*/, TRUE, escapeUnprintable, quoteBuf);
|
2001-06-12 18:02:16 +00:00
|
|
|
}
|
|
|
|
|
2002-02-07 01:40:01 +00:00
|
|
|
ICU_Utility::appendToRule(rule, postContext, escapeUnprintable, quoteBuf);
|
2001-12-11 22:11:51 +00:00
|
|
|
|
2001-10-18 23:05:25 +00:00
|
|
|
// Emit end anchor
|
|
|
|
if ((flags & ANCHOR_END) != 0) {
|
|
|
|
rule.append((UChar)36/*$*/);
|
|
|
|
}
|
|
|
|
|
2002-02-07 01:40:01 +00:00
|
|
|
ICU_Utility::appendToRule(rule, FORWARD_OP, TRUE, escapeUnprintable, quoteBuf);
|
2001-06-12 18:02:16 +00:00
|
|
|
|
|
|
|
// Emit the output pattern
|
|
|
|
|
2002-02-07 01:40:01 +00:00
|
|
|
ICU_Utility::appendToRule(rule, output->toReplacer()->toReplacerPattern(str, escapeUnprintable),
|
|
|
|
TRUE, escapeUnprintable, quoteBuf);
|
2001-06-12 18:02:16 +00:00
|
|
|
|
2002-02-07 01:40:01 +00:00
|
|
|
ICU_Utility::appendToRule(rule, (UChar) 0x003B /*;*/, TRUE, escapeUnprintable, quoteBuf);
|
2001-06-12 18:02:16 +00:00
|
|
|
|
|
|
|
return rule;
|
|
|
|
}
|
2001-07-25 19:11:02 +00:00
|
|
|
|
2002-03-20 00:42:02 +00:00
|
|
|
void TransliterationRule::setData(const TransliterationRuleData* d) {
|
|
|
|
data = d;
|
2002-03-20 00:53:49 +00:00
|
|
|
if (anteContext != NULL) anteContext->setData(d);
|
|
|
|
if (postContext != NULL) postContext->setData(d);
|
|
|
|
if (key != NULL) key->setData(d);
|
|
|
|
// assert(output != NULL);
|
2002-03-20 00:42:02 +00:00
|
|
|
output->setData(d);
|
|
|
|
// Don't have to do segments since they are in the context or key
|
|
|
|
}
|
|
|
|
|
2002-06-28 21:13:54 +00:00
|
|
|
/**
|
|
|
|
* Union the set of all characters that may be modified by this rule
|
|
|
|
* into the given set.
|
|
|
|
*/
|
|
|
|
void TransliterationRule::addSourceSetTo(UnicodeSet& toUnionTo) const {
|
|
|
|
int32_t limit = anteContextLength + keyLength;
|
|
|
|
for (int32_t i=anteContextLength; i<limit; ) {
|
|
|
|
UChar32 ch = pattern.char32At(i);
|
|
|
|
i += UTF_CHAR_LENGTH(ch);
|
|
|
|
const UnicodeMatcher* matcher = data->lookupMatcher(ch);
|
|
|
|
if (matcher == NULL) {
|
|
|
|
toUnionTo.add(ch);
|
|
|
|
} else {
|
|
|
|
matcher->addMatchSetTo(toUnionTo);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Union the set of all characters that may be emitted by this rule
|
|
|
|
* into the given set.
|
|
|
|
*/
|
|
|
|
void TransliterationRule::addTargetSetTo(UnicodeSet& toUnionTo) const {
|
|
|
|
output->toReplacer()->addReplacementSetTo(toUnionTo);
|
|
|
|
}
|
|
|
|
|
2001-10-08 23:26:58 +00:00
|
|
|
U_NAMESPACE_END
|
|
|
|
|
2001-07-25 19:11:02 +00:00
|
|
|
//eof
|