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
|
|
|
|
2001-10-08 23:26:58 +00:00
|
|
|
U_NAMESPACE_BEGIN
|
|
|
|
|
1999-11-20 00:40:50 +00:00
|
|
|
class Replaceable;
|
|
|
|
class TransliterationRuleData;
|
2001-12-11 22:11:51 +00:00
|
|
|
class StringMatcher;
|
1999-11-20 00:40:50 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
*
|
2001-10-30 18:08:53 +00:00
|
|
|
* <p>A rule may contain segments in its input string and segment
|
|
|
|
* references in its output string. A segment is a substring of the
|
|
|
|
* input pattern, indicated by an offset and limit. The segment may
|
|
|
|
* be in the preceding or following context. It may not span a
|
|
|
|
* context boundary. A segment reference is a special character in
|
|
|
|
* the output string that causes a segment of the input string (not
|
|
|
|
* the input pattern) to be copied to the output string. The range of
|
|
|
|
* special characters that represent segment references is defined by
|
|
|
|
* RuleBasedTransliterator.Data.
|
|
|
|
*
|
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:
|
|
|
|
|
2001-12-11 22:11:51 +00:00
|
|
|
// TODO Eliminate the pattern and keyLength data members. They
|
|
|
|
// are used only by masks() and getIndexValue() which are called
|
|
|
|
// only during build time, not during run-time. Perhaps these
|
|
|
|
// methods and pattern/keyLength can be isolated into a separate
|
|
|
|
// object.
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The match that must occur before the key, or null if there is no
|
|
|
|
* preceding context.
|
|
|
|
*/
|
|
|
|
StringMatcher *anteContext;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The matcher object for the key. If null, then the key is empty.
|
|
|
|
*/
|
|
|
|
StringMatcher *key;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The match that must occur after the key, or null if there is no
|
|
|
|
* following context.
|
|
|
|
*/
|
|
|
|
StringMatcher *postContext;
|
|
|
|
|
1999-11-20 00:40:50 +00:00
|
|
|
/**
|
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-10-30 18:08:53 +00:00
|
|
|
* An array of matcher objects corresponding to the input pattern
|
|
|
|
* segments. If there are no segments this is null. N.B. This is
|
|
|
|
* a UnicodeMatcher for generality, but in practice it is always a
|
|
|
|
* StringMatcher. In the future we may generalize this, but for
|
|
|
|
* now we sometimes cast down to StringMatcher.
|
|
|
|
*
|
|
|
|
* The array is owned, but the pointers within it are not.
|
2000-05-20 04:40:29 +00:00
|
|
|
*/
|
2001-10-30 18:08:53 +00:00
|
|
|
UnicodeMatcher** segments;
|
2000-05-20 04:40:29 +00:00
|
|
|
|
2001-07-25 19:11:02 +00:00
|
|
|
/**
|
2001-10-30 18:08:53 +00:00
|
|
|
* The number of elements in segments[] or zero if segments is NULL.
|
2001-07-25 19:11:02 +00:00
|
|
|
*/
|
2001-10-30 18:08:53 +00:00
|
|
|
int32_t segmentsCount;
|
2001-07-25 19:11:02 +00:00
|
|
|
|
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.
|
2001-10-30 18:08:53 +00:00
|
|
|
* @param segs array of UnicodeMatcher corresponding to input pattern
|
|
|
|
* 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-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,
|
2001-10-30 18:08:53 +00:00
|
|
|
UnicodeMatcher** segs,
|
|
|
|
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* 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);
|
2001-12-11 22:11:51 +00:00
|
|
|
|
|
|
|
static void appendToRule(UnicodeString& rule,
|
|
|
|
const UnicodeMatcher* matcher,
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2001-10-08 23:26:58 +00:00
|
|
|
U_NAMESPACE_END
|
|
|
|
|
1999-11-20 00:40:50 +00:00
|
|
|
#endif
|