1999-11-20 00:40:50 +00:00
|
|
|
/*
|
2001-03-22 00:09:10 +00:00
|
|
|
* Copyright (C) {1999-2001}, International Business Machines Corporation and others. All Rights Reserved.
|
1999-11-20 00:40:50 +00:00
|
|
|
**********************************************************************
|
|
|
|
* Date Name Description
|
|
|
|
* 11/17/99 aliu Creation.
|
|
|
|
**********************************************************************
|
|
|
|
*/
|
|
|
|
#ifndef RBT_RULE_H
|
|
|
|
#define RBT_RULE_H
|
|
|
|
|
1999-12-28 23:57:50 +00:00
|
|
|
#include "unicode/unistr.h"
|
2000-06-29 00:18:43 +00:00
|
|
|
#include "unicode/utrans.h"
|
2001-07-25 19:11:02 +00:00
|
|
|
#include "unicode/unimatch.h"
|
1999-11-20 00:40:50 +00:00
|
|
|
|
|
|
|
class Replaceable;
|
|
|
|
class TransliterationRuleData;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A transliteration rule used by
|
|
|
|
* <code>RuleBasedTransliterator</code>.
|
|
|
|
* <code>TransliterationRule</code> is an immutable object.
|
|
|
|
*
|
|
|
|
* <p>A rule consists of an input pattern and an output string. When
|
|
|
|
* the input pattern is matched, the output string is emitted. The
|
|
|
|
* input pattern consists of zero or more characters which are matched
|
|
|
|
* exactly (the key) and optional context. Context must match if it
|
|
|
|
* is specified. Context may be specified before the key, after the
|
|
|
|
* key, or both. The key, preceding context, and following context
|
|
|
|
* may contain variables. Variables represent a set of Unicode
|
|
|
|
* characters, such as the letters <i>a</i> through <i>z</i>.
|
|
|
|
* Variables are detected by looking up each character in a supplied
|
2001-03-22 00:09:10 +00:00
|
|
|
* variable list to see if it has been so defined.
|
1999-11-20 00:40:50 +00:00
|
|
|
*
|
|
|
|
* @author Alan Liu
|
|
|
|
*/
|
|
|
|
class TransliterationRule {
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
2000-08-30 20:43:25 +00:00
|
|
|
/**
|
|
|
|
* The character at index i, where i < contextStart || i >= contextLimit,
|
|
|
|
* is ETHER. This allows explicit matching by rules and UnicodeSets
|
|
|
|
* of text outside the context. In traditional terms, this allows anchoring
|
|
|
|
* at the start and/or end.
|
|
|
|
*/
|
|
|
|
static const UChar ETHER;
|
|
|
|
|
1999-11-20 00:40:50 +00:00
|
|
|
private:
|
|
|
|
|
|
|
|
/**
|
2000-01-13 07:28:08 +00:00
|
|
|
* The string that must be matched, consisting of the anteContext, key,
|
|
|
|
* and postContext, concatenated together, in that order. Some components
|
|
|
|
* may be empty (zero length).
|
|
|
|
* @see anteContextLength
|
|
|
|
* @see keyLength
|
1999-11-20 00:40:50 +00:00
|
|
|
*/
|
2000-01-13 07:28:08 +00:00
|
|
|
UnicodeString pattern;
|
1999-11-20 00:40:50 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The string that is emitted if the key, anteContext, and postContext
|
|
|
|
* are matched.
|
|
|
|
*/
|
|
|
|
UnicodeString output;
|
|
|
|
|
2000-05-20 04:40:29 +00:00
|
|
|
/**
|
2001-07-31 18:22:15 +00:00
|
|
|
* An array of integers encoding the position of the segments.
|
|
|
|
* See rbt_pars.cpp::Segments for more details.
|
2000-05-20 04:40:29 +00:00
|
|
|
*/
|
|
|
|
int32_t* segments;
|
|
|
|
|
2001-07-25 19:11:02 +00:00
|
|
|
/**
|
|
|
|
* A value we compute from segments. The first index into segments[]
|
|
|
|
* that is >= anteContextLength. That is, the first one that is within
|
|
|
|
* the forward scanned part of the pattern -- the key or the postContext.
|
2001-09-26 18:04:13 +00:00
|
|
|
* If there are no segments, this has the value -1. This index is relative
|
|
|
|
* to FIRST_SEG_POS_INDEX; that is, it should be used as follows:
|
|
|
|
* segments[FIRST_SEG_POS_INDEX + firstKeySeg].
|
2001-07-25 19:11:02 +00:00
|
|
|
*/
|
|
|
|
int32_t firstKeySeg;
|
|
|
|
|
1999-11-20 00:40:50 +00:00
|
|
|
/**
|
2000-01-13 07:28:08 +00:00
|
|
|
* The length of the string that must match before the key. If
|
|
|
|
* zero, then there is no matching requirement before the key.
|
|
|
|
* Substring [0,anteContextLength) of pattern is the anteContext.
|
1999-11-20 00:40:50 +00:00
|
|
|
*/
|
2000-01-13 07:28:08 +00:00
|
|
|
int32_t anteContextLength;
|
1999-11-20 00:40:50 +00:00
|
|
|
|
|
|
|
/**
|
2000-01-13 07:28:08 +00:00
|
|
|
* The length of the key. Substring [anteContextLength,
|
|
|
|
* anteContextLength + keyLength) is the key.
|
|
|
|
|
1999-11-20 00:40:50 +00:00
|
|
|
*/
|
2000-01-13 07:28:08 +00:00
|
|
|
int32_t keyLength;
|
1999-11-20 00:40:50 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The position of the cursor after emitting the output string, from 0 to
|
|
|
|
* output.length(). For most rules with no special cursor specification,
|
|
|
|
* the cursorPos is output.length().
|
|
|
|
*/
|
|
|
|
int32_t cursorPos;
|
|
|
|
|
2001-07-25 19:11:02 +00:00
|
|
|
/**
|
|
|
|
* Miscellaneous attributes.
|
|
|
|
*/
|
|
|
|
int8_t flags;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Flag attributes.
|
|
|
|
*/
|
|
|
|
enum {
|
|
|
|
ANCHOR_START = 1,
|
2001-08-03 16:03:31 +00:00
|
|
|
ANCHOR_END = 2
|
2001-07-25 19:11:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2001-09-18 00:24:14 +00:00
|
|
|
* An alias pointer to the data for this rule. The data provides
|
2001-07-25 19:11:02 +00:00
|
|
|
* lookup services for matchers and segments.
|
|
|
|
*/
|
2001-09-18 00:24:14 +00:00
|
|
|
const TransliterationRuleData* data;
|
2001-07-25 19:11:02 +00:00
|
|
|
|
1999-11-20 00:40:50 +00:00
|
|
|
public:
|
|
|
|
|
2000-01-13 07:28:08 +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-01-13 07:28:08 +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.
|
2000-11-29 19:11:04 +00:00
|
|
|
* @param cursorOffset an offset to be added to cursorPos to position the
|
|
|
|
* cursor either in the ante context, if < 0, or in the post context, if >
|
|
|
|
* 0. For example, the rule "abc{def} > | @@@ xyz;" changes "def" to
|
2000-11-29 19:12:57 +00:00
|
|
|
* "xyz" and moves the cursor to before "a". It would have a cursorOffset
|
|
|
|
* of -3.
|
2000-05-20 04:40:29 +00:00
|
|
|
* @param adoptedSegs array of 2n integers. Each of n pairs consists of offset,
|
|
|
|
* limit for a segment of the input string. Characters in the output string
|
|
|
|
* refer to these segments if they are in a special range determined by the
|
|
|
|
* associated RuleBasedTransliterator.Data object. May be null if there are
|
|
|
|
* no segments.
|
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-01-13 07:28:08 +00:00
|
|
|
*/
|
|
|
|
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,
|
2000-05-20 04:40:29 +00:00
|
|
|
int32_t* adoptedSegs,
|
2000-08-30 20:43:25 +00:00
|
|
|
UBool anchorStart, UBool anchorEnd,
|
2001-09-18 00:24:14 +00:00
|
|
|
const TransliterationRuleData* data,
|
2000-01-13 07:28:08 +00:00
|
|
|
UErrorCode& status);
|
|
|
|
|
2000-06-30 23:26:07 +00:00
|
|
|
/**
|
|
|
|
* Copy constructor.
|
|
|
|
*/
|
2000-07-11 18:45:49 +00:00
|
|
|
TransliterationRule(TransliterationRule& other);
|
2000-06-30 23:26:07 +00:00
|
|
|
|
1999-11-20 00:40:50 +00:00
|
|
|
/**
|
2000-05-20 04:40:29 +00:00
|
|
|
* Destructor.
|
1999-11-20 00:40:50 +00:00
|
|
|
*/
|
2000-05-20 04:40:29 +00:00
|
|
|
virtual ~TransliterationRule();
|
1999-11-20 00:40:50 +00:00
|
|
|
|
2001-09-18 00:24:14 +00:00
|
|
|
/**
|
|
|
|
* Change the data object that this rule belongs to. Used
|
|
|
|
* internally by the TransliterationRuleData copy constructor.
|
|
|
|
*/
|
|
|
|
inline void setData(const TransliterationRuleData* data);
|
|
|
|
|
1999-11-20 00:40:50 +00:00
|
|
|
/**
|
|
|
|
* Return the position of the cursor within the output string.
|
|
|
|
* @return a value from 0 to <code>getOutput().length()</code>, inclusive.
|
|
|
|
*/
|
1999-12-22 22:57:04 +00:00
|
|
|
virtual int32_t getCursorPos(void) const;
|
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
|
|
|
virtual int32_t getContextLength(void) const;
|
1999-11-20 00:40:50 +00:00
|
|
|
|
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 getIndexValue() const;
|
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 matchesIndexValue(uint8_t v) const;
|
2000-01-13 07:28:08 +00:00
|
|
|
|
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
|
|
|
virtual UBool masks(const TransliterationRule& r2) const;
|
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 matchAndReplace(Replaceable& text,
|
|
|
|
UTransPosition& pos,
|
|
|
|
UBool incremental) const;
|
2000-08-30 20:43:25 +00:00
|
|
|
|
|
|
|
/**
|
2001-06-12 18:02:16 +00:00
|
|
|
* Create a rule string that represents this rule object. Append
|
|
|
|
* it to the given string.
|
2000-08-30 20:43:25 +00:00
|
|
|
*/
|
2001-06-12 18:02:16 +00:00
|
|
|
virtual UnicodeString& toRule(UnicodeString& pat,
|
|
|
|
UBool escapeUnprintable) const;
|
2001-07-30 23:23:51 +00:00
|
|
|
private:
|
|
|
|
|
|
|
|
friend class StringMatcher;
|
|
|
|
|
|
|
|
static void appendToRule(UnicodeString& rule,
|
|
|
|
UChar32 c,
|
|
|
|
UBool isLiteral,
|
|
|
|
UBool escapeUnprintable,
|
|
|
|
UnicodeString& quoteBuf);
|
|
|
|
|
|
|
|
static void appendToRule(UnicodeString& rule,
|
|
|
|
const UnicodeString& text,
|
|
|
|
UBool isLiteral,
|
|
|
|
UBool escapeUnprintable,
|
|
|
|
UnicodeString& quoteBuf);
|
1999-11-20 00:40:50 +00:00
|
|
|
};
|
|
|
|
|
2001-09-18 00:24:14 +00:00
|
|
|
inline void TransliterationRule::setData(const TransliterationRuleData* d) {
|
|
|
|
data = d;
|
|
|
|
}
|
|
|
|
|
1999-11-20 00:40:50 +00:00
|
|
|
#endif
|