annotate CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/include/unicode/translit.h @ 69:33d812a61356

planemo upload commit 2e9511a184a1ca667c7be0c6321a36dc4e3d116d
author jpayne
date Tue, 18 Mar 2025 17:55:14 -0400
parents
children
rev   line source
jpayne@69 1 // © 2016 and later: Unicode, Inc. and others.
jpayne@69 2 // License & terms of use: http://www.unicode.org/copyright.html
jpayne@69 3 /*
jpayne@69 4 **********************************************************************
jpayne@69 5 * Copyright (C) 1999-2014, International Business Machines
jpayne@69 6 * Corporation and others. All Rights Reserved.
jpayne@69 7 **********************************************************************
jpayne@69 8 * Date Name Description
jpayne@69 9 * 11/17/99 aliu Creation.
jpayne@69 10 **********************************************************************
jpayne@69 11 */
jpayne@69 12 #ifndef TRANSLIT_H
jpayne@69 13 #define TRANSLIT_H
jpayne@69 14
jpayne@69 15 #include "unicode/utypes.h"
jpayne@69 16
jpayne@69 17 #if U_SHOW_CPLUSPLUS_API
jpayne@69 18
jpayne@69 19 /**
jpayne@69 20 * \file
jpayne@69 21 * \brief C++ API: Tranforms text from one format to another.
jpayne@69 22 */
jpayne@69 23
jpayne@69 24 #if !UCONFIG_NO_TRANSLITERATION
jpayne@69 25
jpayne@69 26 #include "unicode/uobject.h"
jpayne@69 27 #include "unicode/unistr.h"
jpayne@69 28 #include "unicode/parseerr.h"
jpayne@69 29 #include "unicode/utrans.h" // UTransPosition, UTransDirection
jpayne@69 30 #include "unicode/strenum.h"
jpayne@69 31
jpayne@69 32 U_NAMESPACE_BEGIN
jpayne@69 33
jpayne@69 34 class UnicodeFilter;
jpayne@69 35 class UnicodeSet;
jpayne@69 36 class TransliteratorParser;
jpayne@69 37 class NormalizationTransliterator;
jpayne@69 38 class TransliteratorIDParser;
jpayne@69 39
jpayne@69 40 /**
jpayne@69 41 *
jpayne@69 42 * <code>Transliterator</code> is an abstract class that
jpayne@69 43 * transliterates text from one format to another. The most common
jpayne@69 44 * kind of transliterator is a script, or alphabet, transliterator.
jpayne@69 45 * For example, a Russian to Latin transliterator changes Russian text
jpayne@69 46 * written in Cyrillic characters to phonetically equivalent Latin
jpayne@69 47 * characters. It does not <em>translate</em> Russian to English!
jpayne@69 48 * Transliteration, unlike translation, operates on characters, without
jpayne@69 49 * reference to the meanings of words and sentences.
jpayne@69 50 *
jpayne@69 51 * <p>Although script conversion is its most common use, a
jpayne@69 52 * transliterator can actually perform a more general class of tasks.
jpayne@69 53 * In fact, <code>Transliterator</code> defines a very general API
jpayne@69 54 * which specifies only that a segment of the input text is replaced
jpayne@69 55 * by new text. The particulars of this conversion are determined
jpayne@69 56 * entirely by subclasses of <code>Transliterator</code>.
jpayne@69 57 *
jpayne@69 58 * <p><b>Transliterators are stateless</b>
jpayne@69 59 *
jpayne@69 60 * <p><code>Transliterator</code> objects are <em>stateless</em>; they
jpayne@69 61 * retain no information between calls to
jpayne@69 62 * <code>transliterate()</code>. (However, this does <em>not</em>
jpayne@69 63 * mean that threads may share transliterators without synchronizing
jpayne@69 64 * them. Transliterators are not immutable, so they must be
jpayne@69 65 * synchronized when shared between threads.) This might seem to
jpayne@69 66 * limit the complexity of the transliteration operation. In
jpayne@69 67 * practice, subclasses perform complex transliterations by delaying
jpayne@69 68 * the replacement of text until it is known that no other
jpayne@69 69 * replacements are possible. In other words, although the
jpayne@69 70 * <code>Transliterator</code> objects are stateless, the source text
jpayne@69 71 * itself embodies all the needed information, and delayed operation
jpayne@69 72 * allows arbitrary complexity.
jpayne@69 73 *
jpayne@69 74 * <p><b>Batch transliteration</b>
jpayne@69 75 *
jpayne@69 76 * <p>The simplest way to perform transliteration is all at once, on a
jpayne@69 77 * string of existing text. This is referred to as <em>batch</em>
jpayne@69 78 * transliteration. For example, given a string <code>input</code>
jpayne@69 79 * and a transliterator <code>t</code>, the call
jpayne@69 80 *
jpayne@69 81 * String result = t.transliterate(input);
jpayne@69 82 *
jpayne@69 83 * will transliterate it and return the result. Other methods allow
jpayne@69 84 * the client to specify a substring to be transliterated and to use
jpayne@69 85 * {@link Replaceable } objects instead of strings, in order to
jpayne@69 86 * preserve out-of-band information (such as text styles).
jpayne@69 87 *
jpayne@69 88 * <p><b>Keyboard transliteration</b>
jpayne@69 89 *
jpayne@69 90 * <p>Somewhat more involved is <em>keyboard</em>, or incremental
jpayne@69 91 * transliteration. This is the transliteration of text that is
jpayne@69 92 * arriving from some source (typically the user's keyboard) one
jpayne@69 93 * character at a time, or in some other piecemeal fashion.
jpayne@69 94 *
jpayne@69 95 * <p>In keyboard transliteration, a <code>Replaceable</code> buffer
jpayne@69 96 * stores the text. As text is inserted, as much as possible is
jpayne@69 97 * transliterated on the fly. This means a GUI that displays the
jpayne@69 98 * contents of the buffer may show text being modified as each new
jpayne@69 99 * character arrives.
jpayne@69 100 *
jpayne@69 101 * <p>Consider the simple rule-based Transliterator:
jpayne@69 102 * <pre>
jpayne@69 103 * th>{theta}
jpayne@69 104 * t>{tau}
jpayne@69 105 * </pre>
jpayne@69 106 *
jpayne@69 107 * When the user types 't', nothing will happen, since the
jpayne@69 108 * transliterator is waiting to see if the next character is 'h'. To
jpayne@69 109 * remedy this, we introduce the notion of a cursor, marked by a '|'
jpayne@69 110 * in the output string:
jpayne@69 111 * <pre>
jpayne@69 112 * t>|{tau}
jpayne@69 113 * {tau}h>{theta}
jpayne@69 114 * </pre>
jpayne@69 115 *
jpayne@69 116 * Now when the user types 't', tau appears, and if the next character
jpayne@69 117 * is 'h', the tau changes to a theta. This is accomplished by
jpayne@69 118 * maintaining a cursor position (independent of the insertion point,
jpayne@69 119 * and invisible in the GUI) across calls to
jpayne@69 120 * <code>transliterate()</code>. Typically, the cursor will
jpayne@69 121 * be coincident with the insertion point, but in a case like the one
jpayne@69 122 * above, it will precede the insertion point.
jpayne@69 123 *
jpayne@69 124 * <p>Keyboard transliteration methods maintain a set of three indices
jpayne@69 125 * that are updated with each call to
jpayne@69 126 * <code>transliterate()</code>, including the cursor, start,
jpayne@69 127 * and limit. Since these indices are changed by the method, they are
jpayne@69 128 * passed in an <code>int[]</code> array. The <code>START</code> index
jpayne@69 129 * marks the beginning of the substring that the transliterator will
jpayne@69 130 * look at. It is advanced as text becomes committed (but it is not
jpayne@69 131 * the committed index; that's the <code>CURSOR</code>). The
jpayne@69 132 * <code>CURSOR</code> index, described above, marks the point at
jpayne@69 133 * which the transliterator last stopped, either because it reached
jpayne@69 134 * the end, or because it required more characters to disambiguate
jpayne@69 135 * between possible inputs. The <code>CURSOR</code> can also be
jpayne@69 136 * explicitly set by rules in a rule-based Transliterator.
jpayne@69 137 * Any characters before the <code>CURSOR</code> index are frozen;
jpayne@69 138 * future keyboard transliteration calls within this input sequence
jpayne@69 139 * will not change them. New text is inserted at the
jpayne@69 140 * <code>LIMIT</code> index, which marks the end of the substring that
jpayne@69 141 * the transliterator looks at.
jpayne@69 142 *
jpayne@69 143 * <p>Because keyboard transliteration assumes that more characters
jpayne@69 144 * are to arrive, it is conservative in its operation. It only
jpayne@69 145 * transliterates when it can do so unambiguously. Otherwise it waits
jpayne@69 146 * for more characters to arrive. When the client code knows that no
jpayne@69 147 * more characters are forthcoming, perhaps because the user has
jpayne@69 148 * performed some input termination operation, then it should call
jpayne@69 149 * <code>finishTransliteration()</code> to complete any
jpayne@69 150 * pending transliterations.
jpayne@69 151 *
jpayne@69 152 * <p><b>Inverses</b>
jpayne@69 153 *
jpayne@69 154 * <p>Pairs of transliterators may be inverses of one another. For
jpayne@69 155 * example, if transliterator <b>A</b> transliterates characters by
jpayne@69 156 * incrementing their Unicode value (so "abc" -> "def"), and
jpayne@69 157 * transliterator <b>B</b> decrements character values, then <b>A</b>
jpayne@69 158 * is an inverse of <b>B</b> and vice versa. If we compose <b>A</b>
jpayne@69 159 * with <b>B</b> in a compound transliterator, the result is the
jpayne@69 160 * indentity transliterator, that is, a transliterator that does not
jpayne@69 161 * change its input text.
jpayne@69 162 *
jpayne@69 163 * The <code>Transliterator</code> method <code>getInverse()</code>
jpayne@69 164 * returns a transliterator's inverse, if one exists, or
jpayne@69 165 * <code>null</code> otherwise. However, the result of
jpayne@69 166 * <code>getInverse()</code> usually will <em>not</em> be a true
jpayne@69 167 * mathematical inverse. This is because true inverse transliterators
jpayne@69 168 * are difficult to formulate. For example, consider two
jpayne@69 169 * transliterators: <b>AB</b>, which transliterates the character 'A'
jpayne@69 170 * to 'B', and <b>BA</b>, which transliterates 'B' to 'A'. It might
jpayne@69 171 * seem that these are exact inverses, since
jpayne@69 172 *
jpayne@69 173 * \htmlonly<blockquote>\endhtmlonly"A" x <b>AB</b> -> "B"<br>
jpayne@69 174 * "B" x <b>BA</b> -> "A"\htmlonly</blockquote>\endhtmlonly
jpayne@69 175 *
jpayne@69 176 * where 'x' represents transliteration. However,
jpayne@69 177 *
jpayne@69 178 * \htmlonly<blockquote>\endhtmlonly"ABCD" x <b>AB</b> -> "BBCD"<br>
jpayne@69 179 * "BBCD" x <b>BA</b> -> "AACD"\htmlonly</blockquote>\endhtmlonly
jpayne@69 180 *
jpayne@69 181 * so <b>AB</b> composed with <b>BA</b> is not the
jpayne@69 182 * identity. Nonetheless, <b>BA</b> may be usefully considered to be
jpayne@69 183 * <b>AB</b>'s inverse, and it is on this basis that
jpayne@69 184 * <b>AB</b><code>.getInverse()</code> could legitimately return
jpayne@69 185 * <b>BA</b>.
jpayne@69 186 *
jpayne@69 187 * <p><b>IDs and display names</b>
jpayne@69 188 *
jpayne@69 189 * <p>A transliterator is designated by a short identifier string or
jpayne@69 190 * <em>ID</em>. IDs follow the format <em>source-destination</em>,
jpayne@69 191 * where <em>source</em> describes the entity being replaced, and
jpayne@69 192 * <em>destination</em> describes the entity replacing
jpayne@69 193 * <em>source</em>. The entities may be the names of scripts,
jpayne@69 194 * particular sequences of characters, or whatever else it is that the
jpayne@69 195 * transliterator converts to or from. For example, a transliterator
jpayne@69 196 * from Russian to Latin might be named "Russian-Latin". A
jpayne@69 197 * transliterator from keyboard escape sequences to Latin-1 characters
jpayne@69 198 * might be named "KeyboardEscape-Latin1". By convention, system
jpayne@69 199 * entity names are in English, with the initial letters of words
jpayne@69 200 * capitalized; user entity names may follow any format so long as
jpayne@69 201 * they do not contain dashes.
jpayne@69 202 *
jpayne@69 203 * <p>In addition to programmatic IDs, transliterator objects have
jpayne@69 204 * display names for presentation in user interfaces, returned by
jpayne@69 205 * {@link #getDisplayName }.
jpayne@69 206 *
jpayne@69 207 * <p><b>Factory methods and registration</b>
jpayne@69 208 *
jpayne@69 209 * <p>In general, client code should use the factory method
jpayne@69 210 * {@link #createInstance } to obtain an instance of a
jpayne@69 211 * transliterator given its ID. Valid IDs may be enumerated using
jpayne@69 212 * <code>getAvailableIDs()</code>. Since transliterators are mutable,
jpayne@69 213 * multiple calls to {@link #createInstance } with the same ID will
jpayne@69 214 * return distinct objects.
jpayne@69 215 *
jpayne@69 216 * <p>In addition to the system transliterators registered at startup,
jpayne@69 217 * user transliterators may be registered by calling
jpayne@69 218 * <code>registerInstance()</code> at run time. A registered instance
jpayne@69 219 * acts a template; future calls to {@link #createInstance } with the ID
jpayne@69 220 * of the registered object return clones of that object. Thus any
jpayne@69 221 * object passed to <tt>registerInstance()</tt> must implement
jpayne@69 222 * <tt>clone()</tt> propertly. To register a transliterator subclass
jpayne@69 223 * without instantiating it (until it is needed), users may call
jpayne@69 224 * {@link #registerFactory }. In this case, the objects are
jpayne@69 225 * instantiated by invoking the zero-argument public constructor of
jpayne@69 226 * the class.
jpayne@69 227 *
jpayne@69 228 * <p><b>Subclassing</b>
jpayne@69 229 *
jpayne@69 230 * Subclasses must implement the abstract method
jpayne@69 231 * <code>handleTransliterate()</code>. <p>Subclasses should override
jpayne@69 232 * the <code>transliterate()</code> method taking a
jpayne@69 233 * <code>Replaceable</code> and the <code>transliterate()</code>
jpayne@69 234 * method taking a <code>String</code> and <code>StringBuffer</code>
jpayne@69 235 * if the performance of these methods can be improved over the
jpayne@69 236 * performance obtained by the default implementations in this class.
jpayne@69 237 *
jpayne@69 238 * <p><b>Rule syntax</b>
jpayne@69 239 *
jpayne@69 240 * <p>A set of rules determines how to perform translations.
jpayne@69 241 * Rules within a rule set are separated by semicolons (';').
jpayne@69 242 * To include a literal semicolon, prefix it with a backslash ('\').
jpayne@69 243 * Unicode Pattern_White_Space is ignored.
jpayne@69 244 * If the first non-blank character on a line is '#',
jpayne@69 245 * the entire line is ignored as a comment.
jpayne@69 246 *
jpayne@69 247 * <p>Each set of rules consists of two groups, one forward, and one
jpayne@69 248 * reverse. This is a convention that is not enforced; rules for one
jpayne@69 249 * direction may be omitted, with the result that translations in
jpayne@69 250 * that direction will not modify the source text. In addition,
jpayne@69 251 * bidirectional forward-reverse rules may be specified for
jpayne@69 252 * symmetrical transformations.
jpayne@69 253 *
jpayne@69 254 * <p>Note: Another description of the Transliterator rule syntax is available in
jpayne@69 255 * <a href="https://www.unicode.org/reports/tr35/tr35-general.html#Transform_Rules_Syntax">section
jpayne@69 256 * Transform Rules Syntax of UTS #35: Unicode LDML</a>.
jpayne@69 257 * The rules are shown there using arrow symbols ← and → and ↔.
jpayne@69 258 * ICU supports both those and the equivalent ASCII symbols &lt; and &gt; and &lt;&gt;.
jpayne@69 259 *
jpayne@69 260 * <p>Rule statements take one of the following forms:
jpayne@69 261 *
jpayne@69 262 * <dl>
jpayne@69 263 * <dt><code>$alefmadda=\\u0622;</code></dt>
jpayne@69 264 * <dd><strong>Variable definition.</strong> The name on the
jpayne@69 265 * left is assigned the text on the right. In this example,
jpayne@69 266 * after this statement, instances of the left hand name,
jpayne@69 267 * &quot;<code>$alefmadda</code>&quot;, will be replaced by
jpayne@69 268 * the Unicode character U+0622. Variable names must begin
jpayne@69 269 * with a letter and consist only of letters, digits, and
jpayne@69 270 * underscores. Case is significant. Duplicate names cause
jpayne@69 271 * an exception to be thrown, that is, variables cannot be
jpayne@69 272 * redefined. The right hand side may contain well-formed
jpayne@69 273 * text of any length, including no text at all (&quot;<code>$empty=;</code>&quot;).
jpayne@69 274 * The right hand side may contain embedded <code>UnicodeSet</code>
jpayne@69 275 * patterns, for example, &quot;<code>$softvowel=[eiyEIY]</code>&quot;.</dd>
jpayne@69 276 * <dt><code>ai&gt;$alefmadda;</code></dt>
jpayne@69 277 * <dd><strong>Forward translation rule.</strong> This rule
jpayne@69 278 * states that the string on the left will be changed to the
jpayne@69 279 * string on the right when performing forward
jpayne@69 280 * transliteration.</dd>
jpayne@69 281 * <dt><code>ai&lt;$alefmadda;</code></dt>
jpayne@69 282 * <dd><strong>Reverse translation rule.</strong> This rule
jpayne@69 283 * states that the string on the right will be changed to
jpayne@69 284 * the string on the left when performing reverse
jpayne@69 285 * transliteration.</dd>
jpayne@69 286 * </dl>
jpayne@69 287 *
jpayne@69 288 * <dl>
jpayne@69 289 * <dt><code>ai&lt;&gt;$alefmadda;</code></dt>
jpayne@69 290 * <dd><strong>Bidirectional translation rule.</strong> This
jpayne@69 291 * rule states that the string on the right will be changed
jpayne@69 292 * to the string on the left when performing forward
jpayne@69 293 * transliteration, and vice versa when performing reverse
jpayne@69 294 * transliteration.</dd>
jpayne@69 295 * </dl>
jpayne@69 296 *
jpayne@69 297 * <p>Translation rules consist of a <em>match pattern</em> and an <em>output
jpayne@69 298 * string</em>. The match pattern consists of literal characters,
jpayne@69 299 * optionally preceded by context, and optionally followed by
jpayne@69 300 * context. Context characters, like literal pattern characters,
jpayne@69 301 * must be matched in the text being transliterated. However, unlike
jpayne@69 302 * literal pattern characters, they are not replaced by the output
jpayne@69 303 * text. For example, the pattern &quot;<code>abc{def}</code>&quot;
jpayne@69 304 * indicates the characters &quot;<code>def</code>&quot; must be
jpayne@69 305 * preceded by &quot;<code>abc</code>&quot; for a successful match.
jpayne@69 306 * If there is a successful match, &quot;<code>def</code>&quot; will
jpayne@69 307 * be replaced, but not &quot;<code>abc</code>&quot;. The final '<code>}</code>'
jpayne@69 308 * is optional, so &quot;<code>abc{def</code>&quot; is equivalent to
jpayne@69 309 * &quot;<code>abc{def}</code>&quot;. Another example is &quot;<code>{123}456</code>&quot;
jpayne@69 310 * (or &quot;<code>123}456</code>&quot;) in which the literal
jpayne@69 311 * pattern &quot;<code>123</code>&quot; must be followed by &quot;<code>456</code>&quot;.
jpayne@69 312 *
jpayne@69 313 * <p>The output string of a forward or reverse rule consists of
jpayne@69 314 * characters to replace the literal pattern characters. If the
jpayne@69 315 * output string contains the character '<code>|</code>', this is
jpayne@69 316 * taken to indicate the location of the <em>cursor</em> after
jpayne@69 317 * replacement. The cursor is the point in the text at which the
jpayne@69 318 * next replacement, if any, will be applied. The cursor is usually
jpayne@69 319 * placed within the replacement text; however, it can actually be
jpayne@69 320 * placed into the precending or following context by using the
jpayne@69 321 * special character '@'. Examples:
jpayne@69 322 *
jpayne@69 323 * <pre>
jpayne@69 324 * a {foo} z &gt; | @ bar; # foo -&gt; bar, move cursor before a
jpayne@69 325 * {foo} xyz &gt; bar @@|; #&nbsp;foo -&gt; bar, cursor between y and z
jpayne@69 326 * </pre>
jpayne@69 327 *
jpayne@69 328 * <p><b>UnicodeSet</b>
jpayne@69 329 *
jpayne@69 330 * <p><code>UnicodeSet</code> patterns may appear anywhere that
jpayne@69 331 * makes sense. They may appear in variable definitions.
jpayne@69 332 * Contrariwise, <code>UnicodeSet</code> patterns may themselves
jpayne@69 333 * contain variable references, such as &quot;<code>$a=[a-z];$not_a=[^$a]</code>&quot;,
jpayne@69 334 * or &quot;<code>$range=a-z;$ll=[$range]</code>&quot;.
jpayne@69 335 *
jpayne@69 336 * <p><code>UnicodeSet</code> patterns may also be embedded directly
jpayne@69 337 * into rule strings. Thus, the following two rules are equivalent:
jpayne@69 338 *
jpayne@69 339 * <pre>
jpayne@69 340 * $vowel=[aeiou]; $vowel&gt;'*'; # One way to do this
jpayne@69 341 * [aeiou]&gt;'*'; # Another way
jpayne@69 342 * </pre>
jpayne@69 343 *
jpayne@69 344 * <p>See {@link UnicodeSet} for more documentation and examples.
jpayne@69 345 *
jpayne@69 346 * <p><b>Segments</b>
jpayne@69 347 *
jpayne@69 348 * <p>Segments of the input string can be matched and copied to the
jpayne@69 349 * output string. This makes certain sets of rules simpler and more
jpayne@69 350 * general, and makes reordering possible. For example:
jpayne@69 351 *
jpayne@69 352 * <pre>
jpayne@69 353 * ([a-z]) &gt; $1 $1; # double lowercase letters
jpayne@69 354 * ([:Lu:]) ([:Ll:]) &gt; $2 $1; # reverse order of Lu-Ll pairs
jpayne@69 355 * </pre>
jpayne@69 356 *
jpayne@69 357 * <p>The segment of the input string to be copied is delimited by
jpayne@69 358 * &quot;<code>(</code>&quot; and &quot;<code>)</code>&quot;. Up to
jpayne@69 359 * nine segments may be defined. Segments may not overlap. In the
jpayne@69 360 * output string, &quot;<code>$1</code>&quot; through &quot;<code>$9</code>&quot;
jpayne@69 361 * represent the input string segments, in left-to-right order of
jpayne@69 362 * definition.
jpayne@69 363 *
jpayne@69 364 * <p><b>Anchors</b>
jpayne@69 365 *
jpayne@69 366 * <p>Patterns can be anchored to the beginning or the end of the text. This is done with the
jpayne@69 367 * special characters '<code>^</code>' and '<code>$</code>'. For example:
jpayne@69 368 *
jpayne@69 369 * <pre>
jpayne@69 370 * ^ a&nbsp;&nbsp; &gt; 'BEG_A'; &nbsp;&nbsp;# match 'a' at start of text
jpayne@69 371 * &nbsp; a&nbsp;&nbsp; &gt; 'A'; # match other instances of 'a'
jpayne@69 372 * &nbsp; z $ &gt; 'END_Z'; &nbsp;&nbsp;# match 'z' at end of text
jpayne@69 373 * &nbsp; z&nbsp;&nbsp; &gt; 'Z';&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # match other instances of 'z'
jpayne@69 374 * </pre>
jpayne@69 375 *
jpayne@69 376 * <p>It is also possible to match the beginning or the end of the text using a <code>UnicodeSet</code>.
jpayne@69 377 * This is done by including a virtual anchor character '<code>$</code>' at the end of the
jpayne@69 378 * set pattern. Although this is usually the match chafacter for the end anchor, the set will
jpayne@69 379 * match either the beginning or the end of the text, depending on its placement. For
jpayne@69 380 * example:
jpayne@69 381 *
jpayne@69 382 * <pre>
jpayne@69 383 * $x = [a-z$]; &nbsp;&nbsp;# match 'a' through 'z' OR anchor
jpayne@69 384 * $x 1&nbsp;&nbsp;&nbsp; &gt; 2;&nbsp;&nbsp; # match '1' after a-z or at the start
jpayne@69 385 * &nbsp;&nbsp; 3 $x &gt; 4; &nbsp;&nbsp;# match '3' before a-z or at the end
jpayne@69 386 * </pre>
jpayne@69 387 *
jpayne@69 388 * <p><b>Example</b>
jpayne@69 389 *
jpayne@69 390 * <p>The following example rules illustrate many of the features of
jpayne@69 391 * the rule language.
jpayne@69 392 *
jpayne@69 393 * <table border="0" cellpadding="4">
jpayne@69 394 * <tr>
jpayne@69 395 * <td style="vertical-align: top;">Rule 1.</td>
jpayne@69 396 * <td style="vertical-align: top; write-space: nowrap;"><code>abc{def}&gt;x|y</code></td>
jpayne@69 397 * </tr>
jpayne@69 398 * <tr>
jpayne@69 399 * <td style="vertical-align: top;">Rule 2.</td>
jpayne@69 400 * <td style="vertical-align: top; write-space: nowrap;"><code>xyz&gt;r</code></td>
jpayne@69 401 * </tr>
jpayne@69 402 * <tr>
jpayne@69 403 * <td style="vertical-align: top;">Rule 3.</td>
jpayne@69 404 * <td style="vertical-align: top; write-space: nowrap;"><code>yz&gt;q</code></td>
jpayne@69 405 * </tr>
jpayne@69 406 * </table>
jpayne@69 407 *
jpayne@69 408 * <p>Applying these rules to the string &quot;<code>adefabcdefz</code>&quot;
jpayne@69 409 * yields the following results:
jpayne@69 410 *
jpayne@69 411 * <table border="0" cellpadding="4">
jpayne@69 412 * <tr>
jpayne@69 413 * <td style="vertical-align: top; write-space: nowrap;"><code>|adefabcdefz</code></td>
jpayne@69 414 * <td style="vertical-align: top;">Initial state, no rules match. Advance
jpayne@69 415 * cursor.</td>
jpayne@69 416 * </tr>
jpayne@69 417 * <tr>
jpayne@69 418 * <td style="vertical-align: top; write-space: nowrap;"><code>a|defabcdefz</code></td>
jpayne@69 419 * <td style="vertical-align: top;">Still no match. Rule 1 does not match
jpayne@69 420 * because the preceding context is not present.</td>
jpayne@69 421 * </tr>
jpayne@69 422 * <tr>
jpayne@69 423 * <td style="vertical-align: top; write-space: nowrap;"><code>ad|efabcdefz</code></td>
jpayne@69 424 * <td style="vertical-align: top;">Still no match. Keep advancing until
jpayne@69 425 * there is a match...</td>
jpayne@69 426 * </tr>
jpayne@69 427 * <tr>
jpayne@69 428 * <td style="vertical-align: top; write-space: nowrap;"><code>ade|fabcdefz</code></td>
jpayne@69 429 * <td style="vertical-align: top;">...</td>
jpayne@69 430 * </tr>
jpayne@69 431 * <tr>
jpayne@69 432 * <td style="vertical-align: top; write-space: nowrap;"><code>adef|abcdefz</code></td>
jpayne@69 433 * <td style="vertical-align: top;">...</td>
jpayne@69 434 * </tr>
jpayne@69 435 * <tr>
jpayne@69 436 * <td style="vertical-align: top; write-space: nowrap;"><code>adefa|bcdefz</code></td>
jpayne@69 437 * <td style="vertical-align: top;">...</td>
jpayne@69 438 * </tr>
jpayne@69 439 * <tr>
jpayne@69 440 * <td style="vertical-align: top; write-space: nowrap;"><code>adefab|cdefz</code></td>
jpayne@69 441 * <td style="vertical-align: top;">...</td>
jpayne@69 442 * </tr>
jpayne@69 443 * <tr>
jpayne@69 444 * <td style="vertical-align: top; write-space: nowrap;"><code>adefabc|defz</code></td>
jpayne@69 445 * <td style="vertical-align: top;">Rule 1 matches; replace &quot;<code>def</code>&quot;
jpayne@69 446 * with &quot;<code>xy</code>&quot; and back up the cursor
jpayne@69 447 * to before the '<code>y</code>'.</td>
jpayne@69 448 * </tr>
jpayne@69 449 * <tr>
jpayne@69 450 * <td style="vertical-align: top; write-space: nowrap;"><code>adefabcx|yz</code></td>
jpayne@69 451 * <td style="vertical-align: top;">Although &quot;<code>xyz</code>&quot; is
jpayne@69 452 * present, rule 2 does not match because the cursor is
jpayne@69 453 * before the '<code>y</code>', not before the '<code>x</code>'.
jpayne@69 454 * Rule 3 does match. Replace &quot;<code>yz</code>&quot;
jpayne@69 455 * with &quot;<code>q</code>&quot;.</td>
jpayne@69 456 * </tr>
jpayne@69 457 * <tr>
jpayne@69 458 * <td style="vertical-align: top; write-space: nowrap;"><code>adefabcxq|</code></td>
jpayne@69 459 * <td style="vertical-align: top;">The cursor is at the end;
jpayne@69 460 * transliteration is complete.</td>
jpayne@69 461 * </tr>
jpayne@69 462 * </table>
jpayne@69 463 *
jpayne@69 464 * <p>The order of rules is significant. If multiple rules may match
jpayne@69 465 * at some point, the first matching rule is applied.
jpayne@69 466 *
jpayne@69 467 * <p>Forward and reverse rules may have an empty output string.
jpayne@69 468 * Otherwise, an empty left or right hand side of any statement is a
jpayne@69 469 * syntax error.
jpayne@69 470 *
jpayne@69 471 * <p>Single quotes are used to quote any character other than a
jpayne@69 472 * digit or letter. To specify a single quote itself, inside or
jpayne@69 473 * outside of quotes, use two single quotes in a row. For example,
jpayne@69 474 * the rule &quot;<code>'&gt;'&gt;o''clock</code>&quot; changes the
jpayne@69 475 * string &quot;<code>&gt;</code>&quot; to the string &quot;<code>o'clock</code>&quot;.
jpayne@69 476 *
jpayne@69 477 * <p><b>Notes</b>
jpayne@69 478 *
jpayne@69 479 * <p>While a Transliterator is being built from rules, it checks that
jpayne@69 480 * the rules are added in proper order. For example, if the rule
jpayne@69 481 * &quot;a&gt;x&quot; is followed by the rule &quot;ab&gt;y&quot;,
jpayne@69 482 * then the second rule will throw an exception. The reason is that
jpayne@69 483 * the second rule can never be triggered, since the first rule
jpayne@69 484 * always matches anything it matches. In other words, the first
jpayne@69 485 * rule <em>masks</em> the second rule.
jpayne@69 486 *
jpayne@69 487 * @author Alan Liu
jpayne@69 488 * @stable ICU 2.0
jpayne@69 489 */
jpayne@69 490 class U_I18N_API Transliterator : public UObject {
jpayne@69 491
jpayne@69 492 private:
jpayne@69 493
jpayne@69 494 /**
jpayne@69 495 * Programmatic name, e.g., "Latin-Arabic".
jpayne@69 496 */
jpayne@69 497 UnicodeString ID;
jpayne@69 498
jpayne@69 499 /**
jpayne@69 500 * This transliterator's filter. Any character for which
jpayne@69 501 * <tt>filter.contains()</tt> returns <tt>false</tt> will not be
jpayne@69 502 * altered by this transliterator. If <tt>filter</tt> is
jpayne@69 503 * <tt>null</tt> then no filtering is applied.
jpayne@69 504 */
jpayne@69 505 UnicodeFilter* filter;
jpayne@69 506
jpayne@69 507 int32_t maximumContextLength;
jpayne@69 508
jpayne@69 509 public:
jpayne@69 510
jpayne@69 511 /**
jpayne@69 512 * A context integer or pointer for a factory function, passed by
jpayne@69 513 * value.
jpayne@69 514 * @stable ICU 2.4
jpayne@69 515 */
jpayne@69 516 union Token {
jpayne@69 517 /**
jpayne@69 518 * This token, interpreted as a 32-bit integer.
jpayne@69 519 * @stable ICU 2.4
jpayne@69 520 */
jpayne@69 521 int32_t integer;
jpayne@69 522 /**
jpayne@69 523 * This token, interpreted as a native pointer.
jpayne@69 524 * @stable ICU 2.4
jpayne@69 525 */
jpayne@69 526 void* pointer;
jpayne@69 527 };
jpayne@69 528
jpayne@69 529 #ifndef U_HIDE_INTERNAL_API
jpayne@69 530 /**
jpayne@69 531 * Return a token containing an integer.
jpayne@69 532 * @return a token containing an integer.
jpayne@69 533 * @internal
jpayne@69 534 */
jpayne@69 535 inline static Token integerToken(int32_t);
jpayne@69 536
jpayne@69 537 /**
jpayne@69 538 * Return a token containing a pointer.
jpayne@69 539 * @return a token containing a pointer.
jpayne@69 540 * @internal
jpayne@69 541 */
jpayne@69 542 inline static Token pointerToken(void*);
jpayne@69 543 #endif /* U_HIDE_INTERNAL_API */
jpayne@69 544
jpayne@69 545 /**
jpayne@69 546 * A function that creates and returns a Transliterator. When
jpayne@69 547 * invoked, it will be passed the ID string that is being
jpayne@69 548 * instantiated, together with the context pointer that was passed
jpayne@69 549 * in when the factory function was first registered. Many
jpayne@69 550 * factory functions will ignore both parameters, however,
jpayne@69 551 * functions that are registered to more than one ID may use the
jpayne@69 552 * ID or the context parameter to parameterize the transliterator
jpayne@69 553 * they create.
jpayne@69 554 * @param ID the string identifier for this transliterator
jpayne@69 555 * @param context a context pointer that will be stored and
jpayne@69 556 * later passed to the factory function when an ID matching
jpayne@69 557 * the registration ID is being instantiated with this factory.
jpayne@69 558 * @stable ICU 2.4
jpayne@69 559 */
jpayne@69 560 typedef Transliterator* (U_EXPORT2 *Factory)(const UnicodeString& ID, Token context);
jpayne@69 561
jpayne@69 562 protected:
jpayne@69 563
jpayne@69 564 /**
jpayne@69 565 * Default constructor.
jpayne@69 566 * @param ID the string identifier for this transliterator
jpayne@69 567 * @param adoptedFilter the filter. Any character for which
jpayne@69 568 * <tt>filter.contains()</tt> returns <tt>false</tt> will not be
jpayne@69 569 * altered by this transliterator. If <tt>filter</tt> is
jpayne@69 570 * <tt>null</tt> then no filtering is applied.
jpayne@69 571 * @stable ICU 2.4
jpayne@69 572 */
jpayne@69 573 Transliterator(const UnicodeString& ID, UnicodeFilter* adoptedFilter);
jpayne@69 574
jpayne@69 575 /**
jpayne@69 576 * Copy constructor.
jpayne@69 577 * @stable ICU 2.4
jpayne@69 578 */
jpayne@69 579 Transliterator(const Transliterator&);
jpayne@69 580
jpayne@69 581 /**
jpayne@69 582 * Assignment operator.
jpayne@69 583 * @stable ICU 2.4
jpayne@69 584 */
jpayne@69 585 Transliterator& operator=(const Transliterator&);
jpayne@69 586
jpayne@69 587 /**
jpayne@69 588 * Create a transliterator from a basic ID. This is an ID
jpayne@69 589 * containing only the forward direction source, target, and
jpayne@69 590 * variant.
jpayne@69 591 * @param id a basic ID of the form S-T or S-T/V.
jpayne@69 592 * @param canon canonical ID to assign to the object, or
jpayne@69 593 * NULL to leave the ID unchanged
jpayne@69 594 * @return a newly created Transliterator or null if the ID is
jpayne@69 595 * invalid.
jpayne@69 596 * @stable ICU 2.4
jpayne@69 597 */
jpayne@69 598 static Transliterator* createBasicInstance(const UnicodeString& id,
jpayne@69 599 const UnicodeString* canon);
jpayne@69 600
jpayne@69 601 friend class TransliteratorParser; // for parseID()
jpayne@69 602 friend class TransliteratorIDParser; // for createBasicInstance()
jpayne@69 603 friend class TransliteratorAlias; // for setID()
jpayne@69 604
jpayne@69 605 public:
jpayne@69 606
jpayne@69 607 /**
jpayne@69 608 * Destructor.
jpayne@69 609 * @stable ICU 2.0
jpayne@69 610 */
jpayne@69 611 virtual ~Transliterator();
jpayne@69 612
jpayne@69 613 /**
jpayne@69 614 * Implements Cloneable.
jpayne@69 615 * All subclasses are encouraged to implement this method if it is
jpayne@69 616 * possible and reasonable to do so. Subclasses that are to be
jpayne@69 617 * registered with the system using <tt>registerInstance()</tt>
jpayne@69 618 * are required to implement this method. If a subclass does not
jpayne@69 619 * implement clone() properly and is registered with the system
jpayne@69 620 * using registerInstance(), then the default clone() implementation
jpayne@69 621 * will return null, and calls to createInstance() will fail.
jpayne@69 622 *
jpayne@69 623 * @return a copy of the object.
jpayne@69 624 * @see #registerInstance
jpayne@69 625 * @stable ICU 2.0
jpayne@69 626 */
jpayne@69 627 virtual Transliterator* clone() const;
jpayne@69 628
jpayne@69 629 /**
jpayne@69 630 * Transliterates a segment of a string, with optional filtering.
jpayne@69 631 *
jpayne@69 632 * @param text the string to be transliterated
jpayne@69 633 * @param start the beginning index, inclusive; <code>0 <= start
jpayne@69 634 * <= limit</code>.
jpayne@69 635 * @param limit the ending index, exclusive; <code>start <= limit
jpayne@69 636 * <= text.length()</code>.
jpayne@69 637 * @return The new limit index. The text previously occupying <code>[start,
jpayne@69 638 * limit)</code> has been transliterated, possibly to a string of a different
jpayne@69 639 * length, at <code>[start, </code><em>new-limit</em><code>)</code>, where
jpayne@69 640 * <em>new-limit</em> is the return value. If the input offsets are out of bounds,
jpayne@69 641 * the returned value is -1 and the input string remains unchanged.
jpayne@69 642 * @stable ICU 2.0
jpayne@69 643 */
jpayne@69 644 virtual int32_t transliterate(Replaceable& text,
jpayne@69 645 int32_t start, int32_t limit) const;
jpayne@69 646
jpayne@69 647 /**
jpayne@69 648 * Transliterates an entire string in place. Convenience method.
jpayne@69 649 * @param text the string to be transliterated
jpayne@69 650 * @stable ICU 2.0
jpayne@69 651 */
jpayne@69 652 virtual void transliterate(Replaceable& text) const;
jpayne@69 653
jpayne@69 654 /**
jpayne@69 655 * Transliterates the portion of the text buffer that can be
jpayne@69 656 * transliterated unambiguosly after new text has been inserted,
jpayne@69 657 * typically as a result of a keyboard event. The new text in
jpayne@69 658 * <code>insertion</code> will be inserted into <code>text</code>
jpayne@69 659 * at <code>index.limit</code>, advancing
jpayne@69 660 * <code>index.limit</code> by <code>insertion.length()</code>.
jpayne@69 661 * Then the transliterator will try to transliterate characters of
jpayne@69 662 * <code>text</code> between <code>index.cursor</code> and
jpayne@69 663 * <code>index.limit</code>. Characters before
jpayne@69 664 * <code>index.cursor</code> will not be changed.
jpayne@69 665 *
jpayne@69 666 * <p>Upon return, values in <code>index</code> will be updated.
jpayne@69 667 * <code>index.start</code> will be advanced to the first
jpayne@69 668 * character that future calls to this method will read.
jpayne@69 669 * <code>index.cursor</code> and <code>index.limit</code> will
jpayne@69 670 * be adjusted to delimit the range of text that future calls to
jpayne@69 671 * this method may change.
jpayne@69 672 *
jpayne@69 673 * <p>Typical usage of this method begins with an initial call
jpayne@69 674 * with <code>index.start</code> and <code>index.limit</code>
jpayne@69 675 * set to indicate the portion of <code>text</code> to be
jpayne@69 676 * transliterated, and <code>index.cursor == index.start</code>.
jpayne@69 677 * Thereafter, <code>index</code> can be used without
jpayne@69 678 * modification in future calls, provided that all changes to
jpayne@69 679 * <code>text</code> are made via this method.
jpayne@69 680 *
jpayne@69 681 * <p>This method assumes that future calls may be made that will
jpayne@69 682 * insert new text into the buffer. As a result, it only performs
jpayne@69 683 * unambiguous transliterations. After the last call to this
jpayne@69 684 * method, there may be untransliterated text that is waiting for
jpayne@69 685 * more input to resolve an ambiguity. In order to perform these
jpayne@69 686 * pending transliterations, clients should call {@link
jpayne@69 687 * #finishTransliteration } after the last call to this
jpayne@69 688 * method has been made.
jpayne@69 689 *
jpayne@69 690 * @param text the buffer holding transliterated and untransliterated text
jpayne@69 691 * @param index an array of three integers.
jpayne@69 692 *
jpayne@69 693 * <ul><li><code>index.start</code>: the beginning index,
jpayne@69 694 * inclusive; <code>0 <= index.start <= index.limit</code>.
jpayne@69 695 *
jpayne@69 696 * <li><code>index.limit</code>: the ending index, exclusive;
jpayne@69 697 * <code>index.start <= index.limit <= text.length()</code>.
jpayne@69 698 * <code>insertion</code> is inserted at
jpayne@69 699 * <code>index.limit</code>.
jpayne@69 700 *
jpayne@69 701 * <li><code>index.cursor</code>: the next character to be
jpayne@69 702 * considered for transliteration; <code>index.start <=
jpayne@69 703 * index.cursor <= index.limit</code>. Characters before
jpayne@69 704 * <code>index.cursor</code> will not be changed by future calls
jpayne@69 705 * to this method.</ul>
jpayne@69 706 *
jpayne@69 707 * @param insertion text to be inserted and possibly
jpayne@69 708 * transliterated into the translation buffer at
jpayne@69 709 * <code>index.limit</code>. If <code>null</code> then no text
jpayne@69 710 * is inserted.
jpayne@69 711 * @param status Output param to filled in with a success or an error.
jpayne@69 712 * @see #handleTransliterate
jpayne@69 713 * @exception IllegalArgumentException if <code>index</code>
jpayne@69 714 * is invalid
jpayne@69 715 * @see UTransPosition
jpayne@69 716 * @stable ICU 2.0
jpayne@69 717 */
jpayne@69 718 virtual void transliterate(Replaceable& text, UTransPosition& index,
jpayne@69 719 const UnicodeString& insertion,
jpayne@69 720 UErrorCode& status) const;
jpayne@69 721
jpayne@69 722 /**
jpayne@69 723 * Transliterates the portion of the text buffer that can be
jpayne@69 724 * transliterated unambiguosly after a new character has been
jpayne@69 725 * inserted, typically as a result of a keyboard event. This is a
jpayne@69 726 * convenience method.
jpayne@69 727 * @param text the buffer holding transliterated and
jpayne@69 728 * untransliterated text
jpayne@69 729 * @param index an array of three integers.
jpayne@69 730 * @param insertion text to be inserted and possibly
jpayne@69 731 * transliterated into the translation buffer at
jpayne@69 732 * <code>index.limit</code>.
jpayne@69 733 * @param status Output param to filled in with a success or an error.
jpayne@69 734 * @see #transliterate(Replaceable&, UTransPosition&, const UnicodeString&, UErrorCode&) const
jpayne@69 735 * @stable ICU 2.0
jpayne@69 736 */
jpayne@69 737 virtual void transliterate(Replaceable& text, UTransPosition& index,
jpayne@69 738 UChar32 insertion,
jpayne@69 739 UErrorCode& status) const;
jpayne@69 740
jpayne@69 741 /**
jpayne@69 742 * Transliterates the portion of the text buffer that can be
jpayne@69 743 * transliterated unambiguosly. This is a convenience method; see
jpayne@69 744 * {@link
jpayne@69 745 * #transliterate(Replaceable&, UTransPosition&, const UnicodeString&, UErrorCode&) const }
jpayne@69 746 * for details.
jpayne@69 747 * @param text the buffer holding transliterated and
jpayne@69 748 * untransliterated text
jpayne@69 749 * @param index an array of three integers.
jpayne@69 750 * @param status Output param to filled in with a success or an error.
jpayne@69 751 * @see #transliterate(Replaceable&, UTransPosition&, const UnicodeString&, UErrorCode &) const
jpayne@69 752 * @stable ICU 2.0
jpayne@69 753 */
jpayne@69 754 virtual void transliterate(Replaceable& text, UTransPosition& index,
jpayne@69 755 UErrorCode& status) const;
jpayne@69 756
jpayne@69 757 /**
jpayne@69 758 * Finishes any pending transliterations that were waiting for
jpayne@69 759 * more characters. Clients should call this method as the last
jpayne@69 760 * call after a sequence of one or more calls to
jpayne@69 761 * <code>transliterate()</code>.
jpayne@69 762 * @param text the buffer holding transliterated and
jpayne@69 763 * untransliterated text.
jpayne@69 764 * @param index the array of indices previously passed to {@link
jpayne@69 765 * #transliterate }
jpayne@69 766 * @stable ICU 2.0
jpayne@69 767 */
jpayne@69 768 virtual void finishTransliteration(Replaceable& text,
jpayne@69 769 UTransPosition& index) const;
jpayne@69 770
jpayne@69 771 private:
jpayne@69 772
jpayne@69 773 /**
jpayne@69 774 * This internal method does incremental transliteration. If the
jpayne@69 775 * 'insertion' is non-null then we append it to 'text' before
jpayne@69 776 * proceeding. This method calls through to the pure virtual
jpayne@69 777 * framework method handleTransliterate() to do the actual
jpayne@69 778 * work.
jpayne@69 779 * @param text the buffer holding transliterated and
jpayne@69 780 * untransliterated text
jpayne@69 781 * @param index an array of three integers. See {@link
jpayne@69 782 * #transliterate(Replaceable, int[], String)}.
jpayne@69 783 * @param insertion text to be inserted and possibly
jpayne@69 784 * transliterated into the translation buffer at
jpayne@69 785 * <code>index.limit</code>.
jpayne@69 786 * @param status Output param to filled in with a success or an error.
jpayne@69 787 */
jpayne@69 788 void _transliterate(Replaceable& text,
jpayne@69 789 UTransPosition& index,
jpayne@69 790 const UnicodeString* insertion,
jpayne@69 791 UErrorCode &status) const;
jpayne@69 792
jpayne@69 793 protected:
jpayne@69 794
jpayne@69 795 /**
jpayne@69 796 * Abstract method that concrete subclasses define to implement
jpayne@69 797 * their transliteration algorithm. This method handles both
jpayne@69 798 * incremental and non-incremental transliteration. Let
jpayne@69 799 * <code>originalStart</code> refer to the value of
jpayne@69 800 * <code>pos.start</code> upon entry.
jpayne@69 801 *
jpayne@69 802 * <ul>
jpayne@69 803 * <li>If <code>incremental</code> is false, then this method
jpayne@69 804 * should transliterate all characters between
jpayne@69 805 * <code>pos.start</code> and <code>pos.limit</code>. Upon return
jpayne@69 806 * <code>pos.start</code> must == <code> pos.limit</code>.</li>
jpayne@69 807 *
jpayne@69 808 * <li>If <code>incremental</code> is true, then this method
jpayne@69 809 * should transliterate all characters between
jpayne@69 810 * <code>pos.start</code> and <code>pos.limit</code> that can be
jpayne@69 811 * unambiguously transliterated, regardless of future insertions
jpayne@69 812 * of text at <code>pos.limit</code>. Upon return,
jpayne@69 813 * <code>pos.start</code> should be in the range
jpayne@69 814 * [<code>originalStart</code>, <code>pos.limit</code>).
jpayne@69 815 * <code>pos.start</code> should be positioned such that
jpayne@69 816 * characters [<code>originalStart</code>, <code>
jpayne@69 817 * pos.start</code>) will not be changed in the future by this
jpayne@69 818 * transliterator and characters [<code>pos.start</code>,
jpayne@69 819 * <code>pos.limit</code>) are unchanged.</li>
jpayne@69 820 * </ul>
jpayne@69 821 *
jpayne@69 822 * <p>Implementations of this method should also obey the
jpayne@69 823 * following invariants:</p>
jpayne@69 824 *
jpayne@69 825 * <ul>
jpayne@69 826 * <li> <code>pos.limit</code> and <code>pos.contextLimit</code>
jpayne@69 827 * should be updated to reflect changes in length of the text
jpayne@69 828 * between <code>pos.start</code> and <code>pos.limit</code>. The
jpayne@69 829 * difference <code> pos.contextLimit - pos.limit</code> should
jpayne@69 830 * not change.</li>
jpayne@69 831 *
jpayne@69 832 * <li><code>pos.contextStart</code> should not change.</li>
jpayne@69 833 *
jpayne@69 834 * <li>Upon return, neither <code>pos.start</code> nor
jpayne@69 835 * <code>pos.limit</code> should be less than
jpayne@69 836 * <code>originalStart</code>.</li>
jpayne@69 837 *
jpayne@69 838 * <li>Text before <code>originalStart</code> and text after
jpayne@69 839 * <code>pos.limit</code> should not change.</li>
jpayne@69 840 *
jpayne@69 841 * <li>Text before <code>pos.contextStart</code> and text after
jpayne@69 842 * <code> pos.contextLimit</code> should be ignored.</li>
jpayne@69 843 * </ul>
jpayne@69 844 *
jpayne@69 845 * <p>Subclasses may safely assume that all characters in
jpayne@69 846 * [<code>pos.start</code>, <code>pos.limit</code>) are filtered.
jpayne@69 847 * In other words, the filter has already been applied by the time
jpayne@69 848 * this method is called. See
jpayne@69 849 * <code>filteredTransliterate()</code>.
jpayne@69 850 *
jpayne@69 851 * <p>This method is <b>not</b> for public consumption. Calling
jpayne@69 852 * this method directly will transliterate
jpayne@69 853 * [<code>pos.start</code>, <code>pos.limit</code>) without
jpayne@69 854 * applying the filter. End user code should call <code>
jpayne@69 855 * transliterate()</code> instead of this method. Subclass code
jpayne@69 856 * and wrapping transliterators should call
jpayne@69 857 * <code>filteredTransliterate()</code> instead of this method.<p>
jpayne@69 858 *
jpayne@69 859 * @param text the buffer holding transliterated and
jpayne@69 860 * untransliterated text
jpayne@69 861 *
jpayne@69 862 * @param pos the indices indicating the start, limit, context
jpayne@69 863 * start, and context limit of the text.
jpayne@69 864 *
jpayne@69 865 * @param incremental if true, assume more text may be inserted at
jpayne@69 866 * <code>pos.limit</code> and act accordingly. Otherwise,
jpayne@69 867 * transliterate all text between <code>pos.start</code> and
jpayne@69 868 * <code>pos.limit</code> and move <code>pos.start</code> up to
jpayne@69 869 * <code>pos.limit</code>.
jpayne@69 870 *
jpayne@69 871 * @see #transliterate
jpayne@69 872 * @stable ICU 2.4
jpayne@69 873 */
jpayne@69 874 virtual void handleTransliterate(Replaceable& text,
jpayne@69 875 UTransPosition& pos,
jpayne@69 876 UBool incremental) const = 0;
jpayne@69 877
jpayne@69 878 public:
jpayne@69 879 /**
jpayne@69 880 * Transliterate a substring of text, as specified by index, taking filters
jpayne@69 881 * into account. This method is for subclasses that need to delegate to
jpayne@69 882 * another transliterator.
jpayne@69 883 * @param text the text to be transliterated
jpayne@69 884 * @param index the position indices
jpayne@69 885 * @param incremental if TRUE, then assume more characters may be inserted
jpayne@69 886 * at index.limit, and postpone processing to accomodate future incoming
jpayne@69 887 * characters
jpayne@69 888 * @stable ICU 2.4
jpayne@69 889 */
jpayne@69 890 virtual void filteredTransliterate(Replaceable& text,
jpayne@69 891 UTransPosition& index,
jpayne@69 892 UBool incremental) const;
jpayne@69 893
jpayne@69 894 private:
jpayne@69 895
jpayne@69 896 /**
jpayne@69 897 * Top-level transliteration method, handling filtering, incremental and
jpayne@69 898 * non-incremental transliteration, and rollback. All transliteration
jpayne@69 899 * public API methods eventually call this method with a rollback argument
jpayne@69 900 * of TRUE. Other entities may call this method but rollback should be
jpayne@69 901 * FALSE.
jpayne@69 902 *
jpayne@69 903 * <p>If this transliterator has a filter, break up the input text into runs
jpayne@69 904 * of unfiltered characters. Pass each run to
jpayne@69 905 * subclass.handleTransliterate().
jpayne@69 906 *
jpayne@69 907 * <p>In incremental mode, if rollback is TRUE, perform a special
jpayne@69 908 * incremental procedure in which several passes are made over the input
jpayne@69 909 * text, adding one character at a time, and committing successful
jpayne@69 910 * transliterations as they occur. Unsuccessful transliterations are rolled
jpayne@69 911 * back and retried with additional characters to give correct results.
jpayne@69 912 *
jpayne@69 913 * @param text the text to be transliterated
jpayne@69 914 * @param index the position indices
jpayne@69 915 * @param incremental if TRUE, then assume more characters may be inserted
jpayne@69 916 * at index.limit, and postpone processing to accomodate future incoming
jpayne@69 917 * characters
jpayne@69 918 * @param rollback if TRUE and if incremental is TRUE, then perform special
jpayne@69 919 * incremental processing, as described above, and undo partial
jpayne@69 920 * transliterations where necessary. If incremental is FALSE then this
jpayne@69 921 * parameter is ignored.
jpayne@69 922 */
jpayne@69 923 virtual void filteredTransliterate(Replaceable& text,
jpayne@69 924 UTransPosition& index,
jpayne@69 925 UBool incremental,
jpayne@69 926 UBool rollback) const;
jpayne@69 927
jpayne@69 928 public:
jpayne@69 929
jpayne@69 930 /**
jpayne@69 931 * Returns the length of the longest context required by this transliterator.
jpayne@69 932 * This is <em>preceding</em> context. The default implementation supplied
jpayne@69 933 * by <code>Transliterator</code> returns zero; subclasses
jpayne@69 934 * that use preceding context should override this method to return the
jpayne@69 935 * correct value. For example, if a transliterator translates "ddd" (where
jpayne@69 936 * d is any digit) to "555" when preceded by "(ddd)", then the preceding
jpayne@69 937 * context length is 5, the length of "(ddd)".
jpayne@69 938 *
jpayne@69 939 * @return The maximum number of preceding context characters this
jpayne@69 940 * transliterator needs to examine
jpayne@69 941 * @stable ICU 2.0
jpayne@69 942 */
jpayne@69 943 int32_t getMaximumContextLength(void) const;
jpayne@69 944
jpayne@69 945 protected:
jpayne@69 946
jpayne@69 947 /**
jpayne@69 948 * Method for subclasses to use to set the maximum context length.
jpayne@69 949 * @param maxContextLength the new value to be set.
jpayne@69 950 * @see #getMaximumContextLength
jpayne@69 951 * @stable ICU 2.4
jpayne@69 952 */
jpayne@69 953 void setMaximumContextLength(int32_t maxContextLength);
jpayne@69 954
jpayne@69 955 public:
jpayne@69 956
jpayne@69 957 /**
jpayne@69 958 * Returns a programmatic identifier for this transliterator.
jpayne@69 959 * If this identifier is passed to <code>createInstance()</code>, it
jpayne@69 960 * will return this object, if it has been registered.
jpayne@69 961 * @return a programmatic identifier for this transliterator.
jpayne@69 962 * @see #registerInstance
jpayne@69 963 * @see #registerFactory
jpayne@69 964 * @see #getAvailableIDs
jpayne@69 965 * @stable ICU 2.0
jpayne@69 966 */
jpayne@69 967 virtual const UnicodeString& getID(void) const;
jpayne@69 968
jpayne@69 969 /**
jpayne@69 970 * Returns a name for this transliterator that is appropriate for
jpayne@69 971 * display to the user in the default locale. See {@link
jpayne@69 972 * #getDisplayName } for details.
jpayne@69 973 * @param ID the string identifier for this transliterator
jpayne@69 974 * @param result Output param to receive the display name
jpayne@69 975 * @return A reference to 'result'.
jpayne@69 976 * @stable ICU 2.0
jpayne@69 977 */
jpayne@69 978 static UnicodeString& U_EXPORT2 getDisplayName(const UnicodeString& ID,
jpayne@69 979 UnicodeString& result);
jpayne@69 980
jpayne@69 981 /**
jpayne@69 982 * Returns a name for this transliterator that is appropriate for
jpayne@69 983 * display to the user in the given locale. This name is taken
jpayne@69 984 * from the locale resource data in the standard manner of the
jpayne@69 985 * <code>java.text</code> package.
jpayne@69 986 *
jpayne@69 987 * <p>If no localized names exist in the system resource bundles,
jpayne@69 988 * a name is synthesized using a localized
jpayne@69 989 * <code>MessageFormat</code> pattern from the resource data. The
jpayne@69 990 * arguments to this pattern are an integer followed by one or two
jpayne@69 991 * strings. The integer is the number of strings, either 1 or 2.
jpayne@69 992 * The strings are formed by splitting the ID for this
jpayne@69 993 * transliterator at the first '-'. If there is no '-', then the
jpayne@69 994 * entire ID forms the only string.
jpayne@69 995 * @param ID the string identifier for this transliterator
jpayne@69 996 * @param inLocale the Locale in which the display name should be
jpayne@69 997 * localized.
jpayne@69 998 * @param result Output param to receive the display name
jpayne@69 999 * @return A reference to 'result'.
jpayne@69 1000 * @stable ICU 2.0
jpayne@69 1001 */
jpayne@69 1002 static UnicodeString& U_EXPORT2 getDisplayName(const UnicodeString& ID,
jpayne@69 1003 const Locale& inLocale,
jpayne@69 1004 UnicodeString& result);
jpayne@69 1005
jpayne@69 1006 /**
jpayne@69 1007 * Returns the filter used by this transliterator, or <tt>NULL</tt>
jpayne@69 1008 * if this transliterator uses no filter.
jpayne@69 1009 * @return the filter used by this transliterator, or <tt>NULL</tt>
jpayne@69 1010 * if this transliterator uses no filter.
jpayne@69 1011 * @stable ICU 2.0
jpayne@69 1012 */
jpayne@69 1013 const UnicodeFilter* getFilter(void) const;
jpayne@69 1014
jpayne@69 1015 /**
jpayne@69 1016 * Returns the filter used by this transliterator, or <tt>NULL</tt> if this
jpayne@69 1017 * transliterator uses no filter. The caller must eventually delete the
jpayne@69 1018 * result. After this call, this transliterator's filter is set to
jpayne@69 1019 * <tt>NULL</tt>.
jpayne@69 1020 * @return the filter used by this transliterator, or <tt>NULL</tt> if this
jpayne@69 1021 * transliterator uses no filter.
jpayne@69 1022 * @stable ICU 2.4
jpayne@69 1023 */
jpayne@69 1024 UnicodeFilter* orphanFilter(void);
jpayne@69 1025
jpayne@69 1026 /**
jpayne@69 1027 * Changes the filter used by this transliterator. If the filter
jpayne@69 1028 * is set to <tt>null</tt> then no filtering will occur.
jpayne@69 1029 *
jpayne@69 1030 * <p>Callers must take care if a transliterator is in use by
jpayne@69 1031 * multiple threads. The filter should not be changed by one
jpayne@69 1032 * thread while another thread may be transliterating.
jpayne@69 1033 * @param adoptedFilter the new filter to be adopted.
jpayne@69 1034 * @stable ICU 2.0
jpayne@69 1035 */
jpayne@69 1036 void adoptFilter(UnicodeFilter* adoptedFilter);
jpayne@69 1037
jpayne@69 1038 /**
jpayne@69 1039 * Returns this transliterator's inverse. See the class
jpayne@69 1040 * documentation for details. This implementation simply inverts
jpayne@69 1041 * the two entities in the ID and attempts to retrieve the
jpayne@69 1042 * resulting transliterator. That is, if <code>getID()</code>
jpayne@69 1043 * returns "A-B", then this method will return the result of
jpayne@69 1044 * <code>createInstance("B-A")</code>, or <code>null</code> if that
jpayne@69 1045 * call fails.
jpayne@69 1046 *
jpayne@69 1047 * <p>Subclasses with knowledge of their inverse may wish to
jpayne@69 1048 * override this method.
jpayne@69 1049 *
jpayne@69 1050 * @param status Output param to filled in with a success or an error.
jpayne@69 1051 * @return a transliterator that is an inverse, not necessarily
jpayne@69 1052 * exact, of this transliterator, or <code>null</code> if no such
jpayne@69 1053 * transliterator is registered.
jpayne@69 1054 * @see #registerInstance
jpayne@69 1055 * @stable ICU 2.0
jpayne@69 1056 */
jpayne@69 1057 Transliterator* createInverse(UErrorCode& status) const;
jpayne@69 1058
jpayne@69 1059 /**
jpayne@69 1060 * Returns a <code>Transliterator</code> object given its ID.
jpayne@69 1061 * The ID must be either a system transliterator ID or a ID registered
jpayne@69 1062 * using <code>registerInstance()</code>.
jpayne@69 1063 *
jpayne@69 1064 * @param ID a valid ID, as enumerated by <code>getAvailableIDs()</code>
jpayne@69 1065 * @param dir either FORWARD or REVERSE.
jpayne@69 1066 * @param parseError Struct to recieve information on position
jpayne@69 1067 * of error if an error is encountered
jpayne@69 1068 * @param status Output param to filled in with a success or an error.
jpayne@69 1069 * @return A <code>Transliterator</code> object with the given ID
jpayne@69 1070 * @see #registerInstance
jpayne@69 1071 * @see #getAvailableIDs
jpayne@69 1072 * @see #getID
jpayne@69 1073 * @stable ICU 2.0
jpayne@69 1074 */
jpayne@69 1075 static Transliterator* U_EXPORT2 createInstance(const UnicodeString& ID,
jpayne@69 1076 UTransDirection dir,
jpayne@69 1077 UParseError& parseError,
jpayne@69 1078 UErrorCode& status);
jpayne@69 1079
jpayne@69 1080 /**
jpayne@69 1081 * Returns a <code>Transliterator</code> object given its ID.
jpayne@69 1082 * The ID must be either a system transliterator ID or a ID registered
jpayne@69 1083 * using <code>registerInstance()</code>.
jpayne@69 1084 * @param ID a valid ID, as enumerated by <code>getAvailableIDs()</code>
jpayne@69 1085 * @param dir either FORWARD or REVERSE.
jpayne@69 1086 * @param status Output param to filled in with a success or an error.
jpayne@69 1087 * @return A <code>Transliterator</code> object with the given ID
jpayne@69 1088 * @stable ICU 2.0
jpayne@69 1089 */
jpayne@69 1090 static Transliterator* U_EXPORT2 createInstance(const UnicodeString& ID,
jpayne@69 1091 UTransDirection dir,
jpayne@69 1092 UErrorCode& status);
jpayne@69 1093
jpayne@69 1094 /**
jpayne@69 1095 * Returns a <code>Transliterator</code> object constructed from
jpayne@69 1096 * the given rule string. This will be a rule-based Transliterator,
jpayne@69 1097 * if the rule string contains only rules, or a
jpayne@69 1098 * compound Transliterator, if it contains ID blocks, or a
jpayne@69 1099 * null Transliterator, if it contains ID blocks which parse as
jpayne@69 1100 * empty for the given direction.
jpayne@69 1101 *
jpayne@69 1102 * @param ID the id for the transliterator.
jpayne@69 1103 * @param rules rules, separated by ';'
jpayne@69 1104 * @param dir either FORWARD or REVERSE.
jpayne@69 1105 * @param parseError Struct to receive information on position
jpayne@69 1106 * of error if an error is encountered
jpayne@69 1107 * @param status Output param set to success/failure code.
jpayne@69 1108 * @return a newly created Transliterator
jpayne@69 1109 * @stable ICU 2.0
jpayne@69 1110 */
jpayne@69 1111 static Transliterator* U_EXPORT2 createFromRules(const UnicodeString& ID,
jpayne@69 1112 const UnicodeString& rules,
jpayne@69 1113 UTransDirection dir,
jpayne@69 1114 UParseError& parseError,
jpayne@69 1115 UErrorCode& status);
jpayne@69 1116
jpayne@69 1117 /**
jpayne@69 1118 * Create a rule string that can be passed to createFromRules()
jpayne@69 1119 * to recreate this transliterator.
jpayne@69 1120 * @param result the string to receive the rules. Previous
jpayne@69 1121 * contents will be deleted.
jpayne@69 1122 * @param escapeUnprintable if TRUE then convert unprintable
jpayne@69 1123 * character to their hex escape representations, \\uxxxx or
jpayne@69 1124 * \\Uxxxxxxxx. Unprintable characters are those other than
jpayne@69 1125 * U+000A, U+0020..U+007E.
jpayne@69 1126 * @stable ICU 2.0
jpayne@69 1127 */
jpayne@69 1128 virtual UnicodeString& toRules(UnicodeString& result,
jpayne@69 1129 UBool escapeUnprintable) const;
jpayne@69 1130
jpayne@69 1131 /**
jpayne@69 1132 * Return the number of elements that make up this transliterator.
jpayne@69 1133 * For example, if the transliterator "NFD;Jamo-Latin;Latin-Greek"
jpayne@69 1134 * were created, the return value of this method would be 3.
jpayne@69 1135 *
jpayne@69 1136 * <p>If this transliterator is not composed of other
jpayne@69 1137 * transliterators, then this method returns 1.
jpayne@69 1138 * @return the number of transliterators that compose this
jpayne@69 1139 * transliterator, or 1 if this transliterator is not composed of
jpayne@69 1140 * multiple transliterators
jpayne@69 1141 * @stable ICU 3.0
jpayne@69 1142 */
jpayne@69 1143 int32_t countElements() const;
jpayne@69 1144
jpayne@69 1145 /**
jpayne@69 1146 * Return an element that makes up this transliterator. For
jpayne@69 1147 * example, if the transliterator "NFD;Jamo-Latin;Latin-Greek"
jpayne@69 1148 * were created, the return value of this method would be one
jpayne@69 1149 * of the three transliterator objects that make up that
jpayne@69 1150 * transliterator: [NFD, Jamo-Latin, Latin-Greek].
jpayne@69 1151 *
jpayne@69 1152 * <p>If this transliterator is not composed of other
jpayne@69 1153 * transliterators, then this method will return a reference to
jpayne@69 1154 * this transliterator when given the index 0.
jpayne@69 1155 * @param index a value from 0..countElements()-1 indicating the
jpayne@69 1156 * transliterator to return
jpayne@69 1157 * @param ec input-output error code
jpayne@69 1158 * @return one of the transliterators that makes up this
jpayne@69 1159 * transliterator, if this transliterator is made up of multiple
jpayne@69 1160 * transliterators, otherwise a reference to this object if given
jpayne@69 1161 * an index of 0
jpayne@69 1162 * @stable ICU 3.0
jpayne@69 1163 */
jpayne@69 1164 const Transliterator& getElement(int32_t index, UErrorCode& ec) const;
jpayne@69 1165
jpayne@69 1166 /**
jpayne@69 1167 * Returns the set of all characters that may be modified in the
jpayne@69 1168 * input text by this Transliterator. This incorporates this
jpayne@69 1169 * object's current filter; if the filter is changed, the return
jpayne@69 1170 * value of this function will change. The default implementation
jpayne@69 1171 * returns an empty set. Some subclasses may override {@link
jpayne@69 1172 * #handleGetSourceSet } to return a more precise result. The
jpayne@69 1173 * return result is approximate in any case and is intended for
jpayne@69 1174 * use by tests, tools, or utilities.
jpayne@69 1175 * @param result receives result set; previous contents lost
jpayne@69 1176 * @return a reference to result
jpayne@69 1177 * @see #getTargetSet
jpayne@69 1178 * @see #handleGetSourceSet
jpayne@69 1179 * @stable ICU 2.4
jpayne@69 1180 */
jpayne@69 1181 UnicodeSet& getSourceSet(UnicodeSet& result) const;
jpayne@69 1182
jpayne@69 1183 /**
jpayne@69 1184 * Framework method that returns the set of all characters that
jpayne@69 1185 * may be modified in the input text by this Transliterator,
jpayne@69 1186 * ignoring the effect of this object's filter. The base class
jpayne@69 1187 * implementation returns the empty set. Subclasses that wish to
jpayne@69 1188 * implement this should override this method.
jpayne@69 1189 * @return the set of characters that this transliterator may
jpayne@69 1190 * modify. The set may be modified, so subclasses should return a
jpayne@69 1191 * newly-created object.
jpayne@69 1192 * @param result receives result set; previous contents lost
jpayne@69 1193 * @see #getSourceSet
jpayne@69 1194 * @see #getTargetSet
jpayne@69 1195 * @stable ICU 2.4
jpayne@69 1196 */
jpayne@69 1197 virtual void handleGetSourceSet(UnicodeSet& result) const;
jpayne@69 1198
jpayne@69 1199 /**
jpayne@69 1200 * Returns the set of all characters that may be generated as
jpayne@69 1201 * replacement text by this transliterator. The default
jpayne@69 1202 * implementation returns the empty set. Some subclasses may
jpayne@69 1203 * override this method to return a more precise result. The
jpayne@69 1204 * return result is approximate in any case and is intended for
jpayne@69 1205 * use by tests, tools, or utilities requiring such
jpayne@69 1206 * meta-information.
jpayne@69 1207 * @param result receives result set; previous contents lost
jpayne@69 1208 * @return a reference to result
jpayne@69 1209 * @see #getTargetSet
jpayne@69 1210 * @stable ICU 2.4
jpayne@69 1211 */
jpayne@69 1212 virtual UnicodeSet& getTargetSet(UnicodeSet& result) const;
jpayne@69 1213
jpayne@69 1214 public:
jpayne@69 1215
jpayne@69 1216 /**
jpayne@69 1217 * Registers a factory function that creates transliterators of
jpayne@69 1218 * a given ID.
jpayne@69 1219 *
jpayne@69 1220 * Because ICU may choose to cache Transliterators internally, this must
jpayne@69 1221 * be called at application startup, prior to any calls to
jpayne@69 1222 * Transliterator::createXXX to avoid undefined behavior.
jpayne@69 1223 *
jpayne@69 1224 * @param id the ID being registered
jpayne@69 1225 * @param factory a function pointer that will be copied and
jpayne@69 1226 * called later when the given ID is passed to createInstance()
jpayne@69 1227 * @param context a context pointer that will be stored and
jpayne@69 1228 * later passed to the factory function when an ID matching
jpayne@69 1229 * the registration ID is being instantiated with this factory.
jpayne@69 1230 * @stable ICU 2.0
jpayne@69 1231 */
jpayne@69 1232 static void U_EXPORT2 registerFactory(const UnicodeString& id,
jpayne@69 1233 Factory factory,
jpayne@69 1234 Token context);
jpayne@69 1235
jpayne@69 1236 /**
jpayne@69 1237 * Registers an instance <tt>obj</tt> of a subclass of
jpayne@69 1238 * <code>Transliterator</code> with the system. When
jpayne@69 1239 * <tt>createInstance()</tt> is called with an ID string that is
jpayne@69 1240 * equal to <tt>obj->getID()</tt>, then <tt>obj->clone()</tt> is
jpayne@69 1241 * returned.
jpayne@69 1242 *
jpayne@69 1243 * After this call the Transliterator class owns the adoptedObj
jpayne@69 1244 * and will delete it.
jpayne@69 1245 *
jpayne@69 1246 * Because ICU may choose to cache Transliterators internally, this must
jpayne@69 1247 * be called at application startup, prior to any calls to
jpayne@69 1248 * Transliterator::createXXX to avoid undefined behavior.
jpayne@69 1249 *
jpayne@69 1250 * @param adoptedObj an instance of subclass of
jpayne@69 1251 * <code>Transliterator</code> that defines <tt>clone()</tt>
jpayne@69 1252 * @see #createInstance
jpayne@69 1253 * @see #registerFactory
jpayne@69 1254 * @see #unregister
jpayne@69 1255 * @stable ICU 2.0
jpayne@69 1256 */
jpayne@69 1257 static void U_EXPORT2 registerInstance(Transliterator* adoptedObj);
jpayne@69 1258
jpayne@69 1259 /**
jpayne@69 1260 * Registers an ID string as an alias of another ID string.
jpayne@69 1261 * That is, after calling this function, <tt>createInstance(aliasID)</tt>
jpayne@69 1262 * will return the same thing as <tt>createInstance(realID)</tt>.
jpayne@69 1263 * This is generally used to create shorter, more mnemonic aliases
jpayne@69 1264 * for long compound IDs.
jpayne@69 1265 *
jpayne@69 1266 * @param aliasID The new ID being registered.
jpayne@69 1267 * @param realID The ID that the new ID is to be an alias for.
jpayne@69 1268 * This can be a compound ID and can include filters and should
jpayne@69 1269 * refer to transliterators that have already been registered with
jpayne@69 1270 * the framework, although this isn't checked.
jpayne@69 1271 * @stable ICU 3.6
jpayne@69 1272 */
jpayne@69 1273 static void U_EXPORT2 registerAlias(const UnicodeString& aliasID,
jpayne@69 1274 const UnicodeString& realID);
jpayne@69 1275
jpayne@69 1276 protected:
jpayne@69 1277
jpayne@69 1278 #ifndef U_HIDE_INTERNAL_API
jpayne@69 1279 /**
jpayne@69 1280 * @param id the ID being registered
jpayne@69 1281 * @param factory a function pointer that will be copied and
jpayne@69 1282 * called later when the given ID is passed to createInstance()
jpayne@69 1283 * @param context a context pointer that will be stored and
jpayne@69 1284 * later passed to the factory function when an ID matching
jpayne@69 1285 * the registration ID is being instantiated with this factory.
jpayne@69 1286 * @internal
jpayne@69 1287 */
jpayne@69 1288 static void _registerFactory(const UnicodeString& id,
jpayne@69 1289 Factory factory,
jpayne@69 1290 Token context);
jpayne@69 1291
jpayne@69 1292 /**
jpayne@69 1293 * @internal
jpayne@69 1294 */
jpayne@69 1295 static void _registerInstance(Transliterator* adoptedObj);
jpayne@69 1296
jpayne@69 1297 /**
jpayne@69 1298 * @internal
jpayne@69 1299 */
jpayne@69 1300 static void _registerAlias(const UnicodeString& aliasID, const UnicodeString& realID);
jpayne@69 1301
jpayne@69 1302 /**
jpayne@69 1303 * Register two targets as being inverses of one another. For
jpayne@69 1304 * example, calling registerSpecialInverse("NFC", "NFD", true) causes
jpayne@69 1305 * Transliterator to form the following inverse relationships:
jpayne@69 1306 *
jpayne@69 1307 * <pre>NFC => NFD
jpayne@69 1308 * Any-NFC => Any-NFD
jpayne@69 1309 * NFD => NFC
jpayne@69 1310 * Any-NFD => Any-NFC</pre>
jpayne@69 1311 *
jpayne@69 1312 * (Without the special inverse registration, the inverse of NFC
jpayne@69 1313 * would be NFC-Any.) Note that NFD is shorthand for Any-NFD, but
jpayne@69 1314 * that the presence or absence of "Any-" is preserved.
jpayne@69 1315 *
jpayne@69 1316 * <p>The relationship is symmetrical; registering (a, b) is
jpayne@69 1317 * equivalent to registering (b, a).
jpayne@69 1318 *
jpayne@69 1319 * <p>The relevant IDs must still be registered separately as
jpayne@69 1320 * factories or classes.
jpayne@69 1321 *
jpayne@69 1322 * <p>Only the targets are specified. Special inverses always
jpayne@69 1323 * have the form Any-Target1 <=> Any-Target2. The target should
jpayne@69 1324 * have canonical casing (the casing desired to be produced when
jpayne@69 1325 * an inverse is formed) and should contain no whitespace or other
jpayne@69 1326 * extraneous characters.
jpayne@69 1327 *
jpayne@69 1328 * @param target the target against which to register the inverse
jpayne@69 1329 * @param inverseTarget the inverse of target, that is
jpayne@69 1330 * Any-target.getInverse() => Any-inverseTarget
jpayne@69 1331 * @param bidirectional if true, register the reverse relation
jpayne@69 1332 * as well, that is, Any-inverseTarget.getInverse() => Any-target
jpayne@69 1333 * @internal
jpayne@69 1334 */
jpayne@69 1335 static void _registerSpecialInverse(const UnicodeString& target,
jpayne@69 1336 const UnicodeString& inverseTarget,
jpayne@69 1337 UBool bidirectional);
jpayne@69 1338 #endif /* U_HIDE_INTERNAL_API */
jpayne@69 1339
jpayne@69 1340 public:
jpayne@69 1341
jpayne@69 1342 /**
jpayne@69 1343 * Unregisters a transliterator or class. This may be either
jpayne@69 1344 * a system transliterator or a user transliterator or class.
jpayne@69 1345 * Any attempt to construct an unregistered transliterator based
jpayne@69 1346 * on its ID will fail.
jpayne@69 1347 *
jpayne@69 1348 * Because ICU may choose to cache Transliterators internally, this should
jpayne@69 1349 * be called during application shutdown, after all calls to
jpayne@69 1350 * Transliterator::createXXX to avoid undefined behavior.
jpayne@69 1351 *
jpayne@69 1352 * @param ID the ID of the transliterator or class
jpayne@69 1353 * @return the <code>Object</code> that was registered with
jpayne@69 1354 * <code>ID</code>, or <code>null</code> if none was
jpayne@69 1355 * @see #registerInstance
jpayne@69 1356 * @see #registerFactory
jpayne@69 1357 * @stable ICU 2.0
jpayne@69 1358 */
jpayne@69 1359 static void U_EXPORT2 unregister(const UnicodeString& ID);
jpayne@69 1360
jpayne@69 1361 public:
jpayne@69 1362
jpayne@69 1363 /**
jpayne@69 1364 * Return a StringEnumeration over the IDs available at the time of the
jpayne@69 1365 * call, including user-registered IDs.
jpayne@69 1366 * @param ec input-output error code
jpayne@69 1367 * @return a newly-created StringEnumeration over the transliterators
jpayne@69 1368 * available at the time of the call. The caller should delete this object
jpayne@69 1369 * when done using it.
jpayne@69 1370 * @stable ICU 3.0
jpayne@69 1371 */
jpayne@69 1372 static StringEnumeration* U_EXPORT2 getAvailableIDs(UErrorCode& ec);
jpayne@69 1373
jpayne@69 1374 /**
jpayne@69 1375 * Return the number of registered source specifiers.
jpayne@69 1376 * @return the number of registered source specifiers.
jpayne@69 1377 * @stable ICU 2.0
jpayne@69 1378 */
jpayne@69 1379 static int32_t U_EXPORT2 countAvailableSources(void);
jpayne@69 1380
jpayne@69 1381 /**
jpayne@69 1382 * Return a registered source specifier.
jpayne@69 1383 * @param index which specifier to return, from 0 to n-1, where
jpayne@69 1384 * n = countAvailableSources()
jpayne@69 1385 * @param result fill-in paramter to receive the source specifier.
jpayne@69 1386 * If index is out of range, result will be empty.
jpayne@69 1387 * @return reference to result
jpayne@69 1388 * @stable ICU 2.0
jpayne@69 1389 */
jpayne@69 1390 static UnicodeString& U_EXPORT2 getAvailableSource(int32_t index,
jpayne@69 1391 UnicodeString& result);
jpayne@69 1392
jpayne@69 1393 /**
jpayne@69 1394 * Return the number of registered target specifiers for a given
jpayne@69 1395 * source specifier.
jpayne@69 1396 * @param source the given source specifier.
jpayne@69 1397 * @return the number of registered target specifiers for a given
jpayne@69 1398 * source specifier.
jpayne@69 1399 * @stable ICU 2.0
jpayne@69 1400 */
jpayne@69 1401 static int32_t U_EXPORT2 countAvailableTargets(const UnicodeString& source);
jpayne@69 1402
jpayne@69 1403 /**
jpayne@69 1404 * Return a registered target specifier for a given source.
jpayne@69 1405 * @param index which specifier to return, from 0 to n-1, where
jpayne@69 1406 * n = countAvailableTargets(source)
jpayne@69 1407 * @param source the source specifier
jpayne@69 1408 * @param result fill-in paramter to receive the target specifier.
jpayne@69 1409 * If source is invalid or if index is out of range, result will
jpayne@69 1410 * be empty.
jpayne@69 1411 * @return reference to result
jpayne@69 1412 * @stable ICU 2.0
jpayne@69 1413 */
jpayne@69 1414 static UnicodeString& U_EXPORT2 getAvailableTarget(int32_t index,
jpayne@69 1415 const UnicodeString& source,
jpayne@69 1416 UnicodeString& result);
jpayne@69 1417
jpayne@69 1418 /**
jpayne@69 1419 * Return the number of registered variant specifiers for a given
jpayne@69 1420 * source-target pair.
jpayne@69 1421 * @param source the source specifiers.
jpayne@69 1422 * @param target the target specifiers.
jpayne@69 1423 * @stable ICU 2.0
jpayne@69 1424 */
jpayne@69 1425 static int32_t U_EXPORT2 countAvailableVariants(const UnicodeString& source,
jpayne@69 1426 const UnicodeString& target);
jpayne@69 1427
jpayne@69 1428 /**
jpayne@69 1429 * Return a registered variant specifier for a given source-target
jpayne@69 1430 * pair.
jpayne@69 1431 * @param index which specifier to return, from 0 to n-1, where
jpayne@69 1432 * n = countAvailableVariants(source, target)
jpayne@69 1433 * @param source the source specifier
jpayne@69 1434 * @param target the target specifier
jpayne@69 1435 * @param result fill-in paramter to receive the variant
jpayne@69 1436 * specifier. If source is invalid or if target is invalid or if
jpayne@69 1437 * index is out of range, result will be empty.
jpayne@69 1438 * @return reference to result
jpayne@69 1439 * @stable ICU 2.0
jpayne@69 1440 */
jpayne@69 1441 static UnicodeString& U_EXPORT2 getAvailableVariant(int32_t index,
jpayne@69 1442 const UnicodeString& source,
jpayne@69 1443 const UnicodeString& target,
jpayne@69 1444 UnicodeString& result);
jpayne@69 1445
jpayne@69 1446 protected:
jpayne@69 1447
jpayne@69 1448 #ifndef U_HIDE_INTERNAL_API
jpayne@69 1449 /**
jpayne@69 1450 * Non-mutexed internal method
jpayne@69 1451 * @internal
jpayne@69 1452 */
jpayne@69 1453 static int32_t _countAvailableSources(void);
jpayne@69 1454
jpayne@69 1455 /**
jpayne@69 1456 * Non-mutexed internal method
jpayne@69 1457 * @internal
jpayne@69 1458 */
jpayne@69 1459 static UnicodeString& _getAvailableSource(int32_t index,
jpayne@69 1460 UnicodeString& result);
jpayne@69 1461
jpayne@69 1462 /**
jpayne@69 1463 * Non-mutexed internal method
jpayne@69 1464 * @internal
jpayne@69 1465 */
jpayne@69 1466 static int32_t _countAvailableTargets(const UnicodeString& source);
jpayne@69 1467
jpayne@69 1468 /**
jpayne@69 1469 * Non-mutexed internal method
jpayne@69 1470 * @internal
jpayne@69 1471 */
jpayne@69 1472 static UnicodeString& _getAvailableTarget(int32_t index,
jpayne@69 1473 const UnicodeString& source,
jpayne@69 1474 UnicodeString& result);
jpayne@69 1475
jpayne@69 1476 /**
jpayne@69 1477 * Non-mutexed internal method
jpayne@69 1478 * @internal
jpayne@69 1479 */
jpayne@69 1480 static int32_t _countAvailableVariants(const UnicodeString& source,
jpayne@69 1481 const UnicodeString& target);
jpayne@69 1482
jpayne@69 1483 /**
jpayne@69 1484 * Non-mutexed internal method
jpayne@69 1485 * @internal
jpayne@69 1486 */
jpayne@69 1487 static UnicodeString& _getAvailableVariant(int32_t index,
jpayne@69 1488 const UnicodeString& source,
jpayne@69 1489 const UnicodeString& target,
jpayne@69 1490 UnicodeString& result);
jpayne@69 1491 #endif /* U_HIDE_INTERNAL_API */
jpayne@69 1492
jpayne@69 1493 protected:
jpayne@69 1494
jpayne@69 1495 /**
jpayne@69 1496 * Set the ID of this transliterators. Subclasses shouldn't do
jpayne@69 1497 * this, unless the underlying script behavior has changed.
jpayne@69 1498 * @param id the new id t to be set.
jpayne@69 1499 * @stable ICU 2.4
jpayne@69 1500 */
jpayne@69 1501 void setID(const UnicodeString& id);
jpayne@69 1502
jpayne@69 1503 public:
jpayne@69 1504
jpayne@69 1505 /**
jpayne@69 1506 * Return the class ID for this class. This is useful only for
jpayne@69 1507 * comparing to a return value from getDynamicClassID().
jpayne@69 1508 * Note that Transliterator is an abstract base class, and therefor
jpayne@69 1509 * no fully constructed object will have a dynamic
jpayne@69 1510 * UCLassID that equals the UClassID returned from
jpayne@69 1511 * TRansliterator::getStaticClassID().
jpayne@69 1512 * @return The class ID for class Transliterator.
jpayne@69 1513 * @stable ICU 2.0
jpayne@69 1514 */
jpayne@69 1515 static UClassID U_EXPORT2 getStaticClassID(void);
jpayne@69 1516
jpayne@69 1517 /**
jpayne@69 1518 * Returns a unique class ID <b>polymorphically</b>. This method
jpayne@69 1519 * is to implement a simple version of RTTI, since not all C++
jpayne@69 1520 * compilers support genuine RTTI. Polymorphic operator==() and
jpayne@69 1521 * clone() methods call this method.
jpayne@69 1522 *
jpayne@69 1523 * <p>Concrete subclasses of Transliterator must use the
jpayne@69 1524 * UOBJECT_DEFINE_RTTI_IMPLEMENTATION macro from
jpayne@69 1525 * uobject.h to provide the RTTI functions.
jpayne@69 1526 *
jpayne@69 1527 * @return The class ID for this object. All objects of a given
jpayne@69 1528 * class have the same class ID. Objects of other classes have
jpayne@69 1529 * different class IDs.
jpayne@69 1530 * @stable ICU 2.0
jpayne@69 1531 */
jpayne@69 1532 virtual UClassID getDynamicClassID(void) const = 0;
jpayne@69 1533
jpayne@69 1534 private:
jpayne@69 1535 static UBool initializeRegistry(UErrorCode &status);
jpayne@69 1536
jpayne@69 1537 public:
jpayne@69 1538 #ifndef U_HIDE_OBSOLETE_API
jpayne@69 1539 /**
jpayne@69 1540 * Return the number of IDs currently registered with the system.
jpayne@69 1541 * To retrieve the actual IDs, call getAvailableID(i) with
jpayne@69 1542 * i from 0 to countAvailableIDs() - 1.
jpayne@69 1543 * @return the number of IDs currently registered with the system.
jpayne@69 1544 * @obsolete ICU 3.4 use getAvailableIDs() instead
jpayne@69 1545 */
jpayne@69 1546 static int32_t U_EXPORT2 countAvailableIDs(void);
jpayne@69 1547
jpayne@69 1548 /**
jpayne@69 1549 * Return the index-th available ID. index must be between 0
jpayne@69 1550 * and countAvailableIDs() - 1, inclusive. If index is out of
jpayne@69 1551 * range, the result of getAvailableID(0) is returned.
jpayne@69 1552 * @param index the given ID index.
jpayne@69 1553 * @return the index-th available ID. index must be between 0
jpayne@69 1554 * and countAvailableIDs() - 1, inclusive. If index is out of
jpayne@69 1555 * range, the result of getAvailableID(0) is returned.
jpayne@69 1556 * @obsolete ICU 3.4 use getAvailableIDs() instead; this function
jpayne@69 1557 * is not thread safe, since it returns a reference to storage that
jpayne@69 1558 * may become invalid if another thread calls unregister
jpayne@69 1559 */
jpayne@69 1560 static const UnicodeString& U_EXPORT2 getAvailableID(int32_t index);
jpayne@69 1561 #endif /* U_HIDE_OBSOLETE_API */
jpayne@69 1562 };
jpayne@69 1563
jpayne@69 1564 inline int32_t Transliterator::getMaximumContextLength(void) const {
jpayne@69 1565 return maximumContextLength;
jpayne@69 1566 }
jpayne@69 1567
jpayne@69 1568 inline void Transliterator::setID(const UnicodeString& id) {
jpayne@69 1569 ID = id;
jpayne@69 1570 // NUL-terminate the ID string, which is a non-aliased copy.
jpayne@69 1571 ID.append((char16_t)0);
jpayne@69 1572 ID.truncate(ID.length()-1);
jpayne@69 1573 }
jpayne@69 1574
jpayne@69 1575 #ifndef U_HIDE_INTERNAL_API
jpayne@69 1576 inline Transliterator::Token Transliterator::integerToken(int32_t i) {
jpayne@69 1577 Token t;
jpayne@69 1578 t.integer = i;
jpayne@69 1579 return t;
jpayne@69 1580 }
jpayne@69 1581
jpayne@69 1582 inline Transliterator::Token Transliterator::pointerToken(void* p) {
jpayne@69 1583 Token t;
jpayne@69 1584 t.pointer = p;
jpayne@69 1585 return t;
jpayne@69 1586 }
jpayne@69 1587 #endif /* U_HIDE_INTERNAL_API */
jpayne@69 1588
jpayne@69 1589 U_NAMESPACE_END
jpayne@69 1590
jpayne@69 1591 #endif /* #if !UCONFIG_NO_TRANSLITERATION */
jpayne@69 1592
jpayne@69 1593 #endif /* U_SHOW_CPLUSPLUS_API */
jpayne@69 1594
jpayne@69 1595 #endif