rbt_rule.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. // © 2016 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. /*
  4. * Copyright (C) {1999-2001}, International Business Machines Corporation and others. All Rights Reserved.
  5. **********************************************************************
  6. * Date Name Description
  7. * 11/17/99 aliu Creation.
  8. **********************************************************************
  9. */
  10. #ifndef RBT_RULE_H
  11. #define RBT_RULE_H
  12. #include "unicode/utypes.h"
  13. #if !UCONFIG_NO_TRANSLITERATION
  14. #include "unicode/uobject.h"
  15. #include "unicode/unistr.h"
  16. #include "unicode/utrans.h"
  17. #include "unicode/unimatch.h"
  18. U_NAMESPACE_BEGIN
  19. class Replaceable;
  20. class TransliterationRuleData;
  21. class StringMatcher;
  22. class UnicodeFunctor;
  23. /**
  24. * A transliteration rule used by
  25. * <code>RuleBasedTransliterator</code>.
  26. * <code>TransliterationRule</code> is an immutable object.
  27. *
  28. * <p>A rule consists of an input pattern and an output string. When
  29. * the input pattern is matched, the output string is emitted. The
  30. * input pattern consists of zero or more characters which are matched
  31. * exactly (the key) and optional context. Context must match if it
  32. * is specified. Context may be specified before the key, after the
  33. * key, or both. The key, preceding context, and following context
  34. * may contain variables. Variables represent a set of Unicode
  35. * characters, such as the letters <i>a</i> through <i>z</i>.
  36. * Variables are detected by looking up each character in a supplied
  37. * variable list to see if it has been so defined.
  38. *
  39. * <p>A rule may contain segments in its input string and segment
  40. * references in its output string. A segment is a substring of the
  41. * input pattern, indicated by an offset and limit. The segment may
  42. * be in the preceding or following context. It may not span a
  43. * context boundary. A segment reference is a special character in
  44. * the output string that causes a segment of the input string (not
  45. * the input pattern) to be copied to the output string. The range of
  46. * special characters that represent segment references is defined by
  47. * RuleBasedTransliterator.Data.
  48. *
  49. * @author Alan Liu
  50. */
  51. class TransliterationRule : public UMemory {
  52. private:
  53. // TODO Eliminate the pattern and keyLength data members. They
  54. // are used only by masks() and getIndexValue() which are called
  55. // only during build time, not during run-time. Perhaps these
  56. // methods and pattern/keyLength can be isolated into a separate
  57. // object.
  58. /**
  59. * The match that must occur before the key, or null if there is no
  60. * preceding context.
  61. */
  62. StringMatcher *anteContext;
  63. /**
  64. * The matcher object for the key. If null, then the key is empty.
  65. */
  66. StringMatcher *key;
  67. /**
  68. * The match that must occur after the key, or null if there is no
  69. * following context.
  70. */
  71. StringMatcher *postContext;
  72. /**
  73. * The object that performs the replacement if the key,
  74. * anteContext, and postContext are matched. Never null.
  75. */
  76. UnicodeFunctor* output;
  77. /**
  78. * The string that must be matched, consisting of the anteContext, key,
  79. * and postContext, concatenated together, in that order. Some components
  80. * may be empty (zero length).
  81. * @see anteContextLength
  82. * @see keyLength
  83. */
  84. UnicodeString pattern;
  85. /**
  86. * An array of matcher objects corresponding to the input pattern
  87. * segments. If there are no segments this is null. N.B. This is
  88. * a UnicodeMatcher for generality, but in practice it is always a
  89. * StringMatcher. In the future we may generalize this, but for
  90. * now we sometimes cast down to StringMatcher.
  91. *
  92. * The array is owned, but the pointers within it are not.
  93. */
  94. UnicodeFunctor** segments;
  95. /**
  96. * The number of elements in segments[] or zero if segments is NULL.
  97. */
  98. int32_t segmentsCount;
  99. /**
  100. * The length of the string that must match before the key. If
  101. * zero, then there is no matching requirement before the key.
  102. * Substring [0,anteContextLength) of pattern is the anteContext.
  103. */
  104. int32_t anteContextLength;
  105. /**
  106. * The length of the key. Substring [anteContextLength,
  107. * anteContextLength + keyLength) is the key.
  108. */
  109. int32_t keyLength;
  110. /**
  111. * Miscellaneous attributes.
  112. */
  113. int8_t flags;
  114. /**
  115. * Flag attributes.
  116. */
  117. enum {
  118. ANCHOR_START = 1,
  119. ANCHOR_END = 2
  120. };
  121. /**
  122. * An alias pointer to the data for this rule. The data provides
  123. * lookup services for matchers and segments.
  124. */
  125. const TransliterationRuleData* data;
  126. public:
  127. /**
  128. * Construct a new rule with the given input, output text, and other
  129. * attributes. A cursor position may be specified for the output text.
  130. * @param input input string, including key and optional ante and
  131. * post context.
  132. * @param anteContextPos offset into input to end of ante context, or -1 if
  133. * none. Must be <= input.length() if not -1.
  134. * @param postContextPos offset into input to start of post context, or -1
  135. * if none. Must be <= input.length() if not -1, and must be >=
  136. * anteContextPos.
  137. * @param outputStr output string.
  138. * @param cursorPosition offset into output at which cursor is located, or -1 if
  139. * none. If less than zero, then the cursor is placed after the
  140. * <code>output</code>; that is, -1 is equivalent to
  141. * <code>output.length()</code>. If greater than
  142. * <code>output.length()</code> then an exception is thrown.
  143. * @param cursorOffset an offset to be added to cursorPos to position the
  144. * cursor either in the ante context, if < 0, or in the post context, if >
  145. * 0. For example, the rule "abc{def} > | @@@ xyz;" changes "def" to
  146. * "xyz" and moves the cursor to before "a". It would have a cursorOffset
  147. * of -3.
  148. * @param segs array of UnicodeMatcher corresponding to input pattern
  149. * segments, or null if there are none. The array itself is adopted,
  150. * but the pointers within it are not.
  151. * @param segsCount number of elements in segs[].
  152. * @param anchorStart TRUE if the the rule is anchored on the left to
  153. * the context start.
  154. * @param anchorEnd TRUE if the rule is anchored on the right to the
  155. * context limit.
  156. * @param data the rule data.
  157. * @param status Output parameter filled in with success or failure status.
  158. */
  159. TransliterationRule(const UnicodeString& input,
  160. int32_t anteContextPos, int32_t postContextPos,
  161. const UnicodeString& outputStr,
  162. int32_t cursorPosition, int32_t cursorOffset,
  163. UnicodeFunctor** segs,
  164. int32_t segsCount,
  165. UBool anchorStart, UBool anchorEnd,
  166. const TransliterationRuleData* data,
  167. UErrorCode& status);
  168. /**
  169. * Copy constructor.
  170. * @param other the object to be copied.
  171. */
  172. TransliterationRule(TransliterationRule& other);
  173. /**
  174. * Destructor.
  175. */
  176. virtual ~TransliterationRule();
  177. /**
  178. * Change the data object that this rule belongs to. Used
  179. * internally by the TransliterationRuleData copy constructor.
  180. * @param data the new data value to be set.
  181. */
  182. void setData(const TransliterationRuleData* data);
  183. /**
  184. * Return the preceding context length. This method is needed to
  185. * support the <code>Transliterator</code> method
  186. * <code>getMaximumContextLength()</code>. Internally, this is
  187. * implemented as the anteContextLength, optionally plus one if
  188. * there is a start anchor. The one character anchor gap is
  189. * needed to make repeated incremental transliteration with
  190. * anchors work.
  191. * @return the preceding context length.
  192. */
  193. virtual int32_t getContextLength(void) const;
  194. /**
  195. * Internal method. Returns 8-bit index value for this rule.
  196. * This is the low byte of the first character of the key,
  197. * unless the first character of the key is a set. If it's a
  198. * set, or otherwise can match multiple keys, the index value is -1.
  199. * @return 8-bit index value for this rule.
  200. */
  201. int16_t getIndexValue() const;
  202. /**
  203. * Internal method. Returns true if this rule matches the given
  204. * index value. The index value is an 8-bit integer, 0..255,
  205. * representing the low byte of the first character of the key.
  206. * It matches this rule if it matches the first character of the
  207. * key, or if the first character of the key is a set, and the set
  208. * contains any character with a low byte equal to the index
  209. * value. If the rule contains only ante context, as in foo)>bar,
  210. * then it will match any key.
  211. * @param v the given index value.
  212. * @return true if this rule matches the given index value.
  213. */
  214. UBool matchesIndexValue(uint8_t v) const;
  215. /**
  216. * Return true if this rule masks another rule. If r1 masks r2 then
  217. * r1 matches any input string that r2 matches. If r1 masks r2 and r2 masks
  218. * r1 then r1 == r2. Examples: "a>x" masks "ab>y". "a>x" masks "a[b]>y".
  219. * "[c]a>x" masks "[dc]a>y".
  220. * @param r2 the given rule to be compared with.
  221. * @return true if this rule masks 'r2'
  222. */
  223. virtual UBool masks(const TransliterationRule& r2) const;
  224. /**
  225. * Attempt a match and replacement at the given position. Return
  226. * the degree of match between this rule and the given text. The
  227. * degree of match may be mismatch, a partial match, or a full
  228. * match. A mismatch means at least one character of the text
  229. * does not match the context or key. A partial match means some
  230. * context and key characters match, but the text is not long
  231. * enough to match all of them. A full match means all context
  232. * and key characters match.
  233. *
  234. * If a full match is obtained, perform a replacement, update pos,
  235. * and return U_MATCH. Otherwise both text and pos are unchanged.
  236. *
  237. * @param text the text
  238. * @param pos the position indices
  239. * @param incremental if TRUE, test for partial matches that may
  240. * be completed by additional text inserted at pos.limit.
  241. * @return one of <code>U_MISMATCH</code>,
  242. * <code>U_PARTIAL_MATCH</code>, or <code>U_MATCH</code>. If
  243. * incremental is FALSE then U_PARTIAL_MATCH will not be returned.
  244. */
  245. UMatchDegree matchAndReplace(Replaceable& text,
  246. UTransPosition& pos,
  247. UBool incremental) const;
  248. /**
  249. * Create a rule string that represents this rule object. Append
  250. * it to the given string.
  251. */
  252. virtual UnicodeString& toRule(UnicodeString& pat,
  253. UBool escapeUnprintable) const;
  254. /**
  255. * Union the set of all characters that may be modified by this rule
  256. * into the given set.
  257. */
  258. void addSourceSetTo(UnicodeSet& toUnionTo) const;
  259. /**
  260. * Union the set of all characters that may be emitted by this rule
  261. * into the given set.
  262. */
  263. void addTargetSetTo(UnicodeSet& toUnionTo) const;
  264. private:
  265. friend class StringMatcher;
  266. TransliterationRule &operator=(const TransliterationRule &other); // forbid copying of this class
  267. };
  268. U_NAMESPACE_END
  269. #endif /* #if !UCONFIG_NO_TRANSLITERATION */
  270. #endif