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