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-30 23:23:51 +00:00
|
|
|
* >>> Duplicated in rbt_pars.cpp and rbt_rule.h <<<
|
2000-05-20 04:40:29 +00:00
|
|
|
*
|
2001-07-30 23:23:51 +00:00
|
|
|
* The segments array encodes information about parentheses-
|
|
|
|
* enclosed regions of the input string. These are referenced in
|
|
|
|
* the output string using the notation $1, $2, etc. Numbering is
|
|
|
|
* in order of appearance of the left parenthesis. Number is
|
|
|
|
* one-based. Segments are defined as start, limit pairs.
|
|
|
|
* Segments may nest.
|
|
|
|
*
|
|
|
|
* In order two avoid allocating two subobjects, the segments
|
|
|
|
* array actually comprises two arrays. The first is gives the
|
|
|
|
* index values of the open and close parentheses in the order
|
|
|
|
* they appear. The second maps segment numbers to the indices of
|
|
|
|
* the first array. The two arrays have the same length.
|
|
|
|
*
|
|
|
|
* Example: (a b(c d)e f)
|
|
|
|
* 0 1 2 3 4 5 6
|
|
|
|
*
|
|
|
|
* First array: Indices are 0, 2, 4, and 6.
|
|
|
|
|
|
|
|
* Second array: $1 is at 0 and 6, and $2 is at 2 and 4, so the
|
|
|
|
* second array is 0, 3, 1 2 -- these give the indices in the
|
|
|
|
* first array at which $1:open, $1:close, $2:open, and $2:close
|
|
|
|
* occur.
|
|
|
|
*
|
|
|
|
* The final array is: 2, 7, 0, 2, 4, 6, -1, 2, 5, 3, 4, -1
|
|
|
|
*
|
|
|
|
* Each subarray is terminated with a -1, and two leading entries
|
|
|
|
* give the number of segments and the offset to the first entry
|
|
|
|
* of the second array. In addition, the second array value are
|
|
|
|
* all offset by 2 so they index directly into the final array.
|
|
|
|
* The total array size is 4*segments[0] + 4. The second index is
|
|
|
|
* 2*segments[0] + 3.
|
|
|
|
*
|
|
|
|
* In the output string, a segment reference is indicated by a
|
|
|
|
* character in a special range, as defined by
|
|
|
|
* RuleBasedTransliterator.Data.
|
2000-05-20 04:40:29 +00:00
|
|
|
*
|
|
|
|
* Most rules have no segments, in which case segments is null, and the
|
|
|
|
* output string need not be checked for segment reference characters.
|
|
|
|
*/
|
|
|
|
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.
|
|
|
|
* If there are no segments, this has the value -1.
|
|
|
|
*/
|
|
|
|
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,
|
|
|
|
ANCHOR_END = 2,
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A reference to the data for this rule. The data provides
|
|
|
|
* lookup services for matchers and segments.
|
|
|
|
*/
|
|
|
|
const TransliterationRuleData& data;
|
|
|
|
|
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-07-25 19:11:02 +00:00
|
|
|
const TransliterationRuleData& data,
|
2000-01-13 07:28:08 +00:00
|
|
|
UErrorCode& status);
|
|
|
|
|
1999-11-20 00:40:50 +00:00
|
|
|
/**
|
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.
|
1999-11-20 00:40:50 +00:00
|
|
|
*/
|
2000-05-20 04:40:29 +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,
|
2001-07-25 19:11:02 +00:00
|
|
|
const TransliterationRuleData& data,
|
2000-05-20 04:40:29 +00:00
|
|
|
UErrorCode& status);
|
1999-11-20 00:40:50 +00:00
|
|
|
|
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
|
|
|
|
|
|
|
/**
|
|
|
|
* 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:
|
2000-05-20 04:40:29 +00:00
|
|
|
|
|
|
|
void init(const UnicodeString& input,
|
|
|
|
int32_t anteContextPos, int32_t postContextPos,
|
|
|
|
const UnicodeString& output,
|
|
|
|
int32_t cursorPos, int32_t cursorOffset,
|
|
|
|
int32_t* adoptedSegs,
|
2000-08-30 20:43:25 +00:00
|
|
|
UBool anchorStart, UBool anchorEnd,
|
2000-05-20 04:40:29 +00:00
|
|
|
UErrorCode& status);
|
2000-06-30 23:26:07 +00:00
|
|
|
|
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
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|