|
||||
File indexing completed on 2025-01-18 10:13:09
0001 // © 2016 and later: Unicode, Inc. and others. 0002 // License & terms of use: http://www.unicode.org/copyright.html 0003 /* 0004 ********************************************************************** 0005 * Copyright (C) 1999-2014, International Business Machines 0006 * Corporation and others. All Rights Reserved. 0007 ********************************************************************** 0008 * Date Name Description 0009 * 11/17/99 aliu Creation. 0010 ********************************************************************** 0011 */ 0012 #ifndef TRANSLIT_H 0013 #define TRANSLIT_H 0014 0015 #include "unicode/utypes.h" 0016 0017 #if U_SHOW_CPLUSPLUS_API 0018 0019 /** 0020 * \file 0021 * \brief C++ API: Transforms text from one format to another. 0022 */ 0023 0024 #if !UCONFIG_NO_TRANSLITERATION 0025 0026 #include "unicode/uobject.h" 0027 #include "unicode/unistr.h" 0028 #include "unicode/parseerr.h" 0029 #include "unicode/utrans.h" // UTransPosition, UTransDirection 0030 #include "unicode/strenum.h" 0031 0032 U_NAMESPACE_BEGIN 0033 0034 class UnicodeFilter; 0035 class UnicodeSet; 0036 class TransliteratorParser; 0037 class NormalizationTransliterator; 0038 class TransliteratorIDParser; 0039 0040 /** 0041 * 0042 * <code>Transliterator</code> is an abstract class that 0043 * transliterates text from one format to another. The most common 0044 * kind of transliterator is a script, or alphabet, transliterator. 0045 * For example, a Russian to Latin transliterator changes Russian text 0046 * written in Cyrillic characters to phonetically equivalent Latin 0047 * characters. It does not <em>translate</em> Russian to English! 0048 * Transliteration, unlike translation, operates on characters, without 0049 * reference to the meanings of words and sentences. 0050 * 0051 * <p>Although script conversion is its most common use, a 0052 * transliterator can actually perform a more general class of tasks. 0053 * In fact, <code>Transliterator</code> defines a very general API 0054 * which specifies only that a segment of the input text is replaced 0055 * by new text. The particulars of this conversion are determined 0056 * entirely by subclasses of <code>Transliterator</code>. 0057 * 0058 * <p><b>Transliterators are stateless</b> 0059 * 0060 * <p><code>Transliterator</code> objects are <em>stateless</em>; they 0061 * retain no information between calls to 0062 * <code>transliterate()</code>. (However, this does <em>not</em> 0063 * mean that threads may share transliterators without synchronizing 0064 * them. Transliterators are not immutable, so they must be 0065 * synchronized when shared between threads.) This might seem to 0066 * limit the complexity of the transliteration operation. In 0067 * practice, subclasses perform complex transliterations by delaying 0068 * the replacement of text until it is known that no other 0069 * replacements are possible. In other words, although the 0070 * <code>Transliterator</code> objects are stateless, the source text 0071 * itself embodies all the needed information, and delayed operation 0072 * allows arbitrary complexity. 0073 * 0074 * <p><b>Batch transliteration</b> 0075 * 0076 * <p>The simplest way to perform transliteration is all at once, on a 0077 * string of existing text. This is referred to as <em>batch</em> 0078 * transliteration. For example, given a string <code>input</code> 0079 * and a transliterator <code>t</code>, the call 0080 * 0081 * String result = t.transliterate(input); 0082 * 0083 * will transliterate it and return the result. Other methods allow 0084 * the client to specify a substring to be transliterated and to use 0085 * {@link Replaceable } objects instead of strings, in order to 0086 * preserve out-of-band information (such as text styles). 0087 * 0088 * <p><b>Keyboard transliteration</b> 0089 * 0090 * <p>Somewhat more involved is <em>keyboard</em>, or incremental 0091 * transliteration. This is the transliteration of text that is 0092 * arriving from some source (typically the user's keyboard) one 0093 * character at a time, or in some other piecemeal fashion. 0094 * 0095 * <p>In keyboard transliteration, a <code>Replaceable</code> buffer 0096 * stores the text. As text is inserted, as much as possible is 0097 * transliterated on the fly. This means a GUI that displays the 0098 * contents of the buffer may show text being modified as each new 0099 * character arrives. 0100 * 0101 * <p>Consider the simple rule-based Transliterator: 0102 * <pre> 0103 * th>{theta} 0104 * t>{tau} 0105 * </pre> 0106 * 0107 * When the user types 't', nothing will happen, since the 0108 * transliterator is waiting to see if the next character is 'h'. To 0109 * remedy this, we introduce the notion of a cursor, marked by a '|' 0110 * in the output string: 0111 * <pre> 0112 * t>|{tau} 0113 * {tau}h>{theta} 0114 * </pre> 0115 * 0116 * Now when the user types 't', tau appears, and if the next character 0117 * is 'h', the tau changes to a theta. This is accomplished by 0118 * maintaining a cursor position (independent of the insertion point, 0119 * and invisible in the GUI) across calls to 0120 * <code>transliterate()</code>. Typically, the cursor will 0121 * be coincident with the insertion point, but in a case like the one 0122 * above, it will precede the insertion point. 0123 * 0124 * <p>Keyboard transliteration methods maintain a set of three indices 0125 * that are updated with each call to 0126 * <code>transliterate()</code>, including the cursor, start, 0127 * and limit. Since these indices are changed by the method, they are 0128 * passed in an <code>int[]</code> array. The <code>START</code> index 0129 * marks the beginning of the substring that the transliterator will 0130 * look at. It is advanced as text becomes committed (but it is not 0131 * the committed index; that's the <code>CURSOR</code>). The 0132 * <code>CURSOR</code> index, described above, marks the point at 0133 * which the transliterator last stopped, either because it reached 0134 * the end, or because it required more characters to disambiguate 0135 * between possible inputs. The <code>CURSOR</code> can also be 0136 * explicitly set by rules in a rule-based Transliterator. 0137 * Any characters before the <code>CURSOR</code> index are frozen; 0138 * future keyboard transliteration calls within this input sequence 0139 * will not change them. New text is inserted at the 0140 * <code>LIMIT</code> index, which marks the end of the substring that 0141 * the transliterator looks at. 0142 * 0143 * <p>Because keyboard transliteration assumes that more characters 0144 * are to arrive, it is conservative in its operation. It only 0145 * transliterates when it can do so unambiguously. Otherwise it waits 0146 * for more characters to arrive. When the client code knows that no 0147 * more characters are forthcoming, perhaps because the user has 0148 * performed some input termination operation, then it should call 0149 * <code>finishTransliteration()</code> to complete any 0150 * pending transliterations. 0151 * 0152 * <p><b>Inverses</b> 0153 * 0154 * <p>Pairs of transliterators may be inverses of one another. For 0155 * example, if transliterator <b>A</b> transliterates characters by 0156 * incrementing their Unicode value (so "abc" -> "def"), and 0157 * transliterator <b>B</b> decrements character values, then <b>A</b> 0158 * is an inverse of <b>B</b> and vice versa. If we compose <b>A</b> 0159 * with <b>B</b> in a compound transliterator, the result is the 0160 * identity transliterator, that is, a transliterator that does not 0161 * change its input text. 0162 * 0163 * The <code>Transliterator</code> method <code>getInverse()</code> 0164 * returns a transliterator's inverse, if one exists, or 0165 * <code>null</code> otherwise. However, the result of 0166 * <code>getInverse()</code> usually will <em>not</em> be a true 0167 * mathematical inverse. This is because true inverse transliterators 0168 * are difficult to formulate. For example, consider two 0169 * transliterators: <b>AB</b>, which transliterates the character 'A' 0170 * to 'B', and <b>BA</b>, which transliterates 'B' to 'A'. It might 0171 * seem that these are exact inverses, since 0172 * 0173 * \htmlonly<blockquote>\endhtmlonly"A" x <b>AB</b> -> "B"<br> 0174 * "B" x <b>BA</b> -> "A"\htmlonly</blockquote>\endhtmlonly 0175 * 0176 * where 'x' represents transliteration. However, 0177 * 0178 * \htmlonly<blockquote>\endhtmlonly"ABCD" x <b>AB</b> -> "BBCD"<br> 0179 * "BBCD" x <b>BA</b> -> "AACD"\htmlonly</blockquote>\endhtmlonly 0180 * 0181 * so <b>AB</b> composed with <b>BA</b> is not the 0182 * identity. Nonetheless, <b>BA</b> may be usefully considered to be 0183 * <b>AB</b>'s inverse, and it is on this basis that 0184 * <b>AB</b><code>.getInverse()</code> could legitimately return 0185 * <b>BA</b>. 0186 * 0187 * <p><b>IDs and display names</b> 0188 * 0189 * <p>A transliterator is designated by a short identifier string or 0190 * <em>ID</em>. IDs follow the format <em>source-destination</em>, 0191 * where <em>source</em> describes the entity being replaced, and 0192 * <em>destination</em> describes the entity replacing 0193 * <em>source</em>. The entities may be the names of scripts, 0194 * particular sequences of characters, or whatever else it is that the 0195 * transliterator converts to or from. For example, a transliterator 0196 * from Russian to Latin might be named "Russian-Latin". A 0197 * transliterator from keyboard escape sequences to Latin-1 characters 0198 * might be named "KeyboardEscape-Latin1". By convention, system 0199 * entity names are in English, with the initial letters of words 0200 * capitalized; user entity names may follow any format so long as 0201 * they do not contain dashes. 0202 * 0203 * <p>In addition to programmatic IDs, transliterator objects have 0204 * display names for presentation in user interfaces, returned by 0205 * {@link #getDisplayName }. 0206 * 0207 * <p><b>Factory methods and registration</b> 0208 * 0209 * <p>In general, client code should use the factory method 0210 * {@link #createInstance } to obtain an instance of a 0211 * transliterator given its ID. Valid IDs may be enumerated using 0212 * <code>getAvailableIDs()</code>. Since transliterators are mutable, 0213 * multiple calls to {@link #createInstance } with the same ID will 0214 * return distinct objects. 0215 * 0216 * <p>In addition to the system transliterators registered at startup, 0217 * user transliterators may be registered by calling 0218 * <code>registerInstance()</code> at run time. A registered instance 0219 * acts a template; future calls to {@link #createInstance } with the ID 0220 * of the registered object return clones of that object. Thus any 0221 * object passed to <tt>registerInstance()</tt> must implement 0222 * <tt>clone()</tt> properly. To register a transliterator subclass 0223 * without instantiating it (until it is needed), users may call 0224 * {@link #registerFactory }. In this case, the objects are 0225 * instantiated by invoking the zero-argument public constructor of 0226 * the class. 0227 * 0228 * <p><b>Subclassing</b> 0229 * 0230 * Subclasses must implement the abstract method 0231 * <code>handleTransliterate()</code>. <p>Subclasses should override 0232 * the <code>transliterate()</code> method taking a 0233 * <code>Replaceable</code> and the <code>transliterate()</code> 0234 * method taking a <code>String</code> and <code>StringBuffer</code> 0235 * if the performance of these methods can be improved over the 0236 * performance obtained by the default implementations in this class. 0237 * 0238 * <p><b>Rule syntax</b> 0239 * 0240 * <p>A set of rules determines how to perform translations. 0241 * Rules within a rule set are separated by semicolons (';'). 0242 * To include a literal semicolon, prefix it with a backslash ('\'). 0243 * Unicode Pattern_White_Space is ignored. 0244 * If the first non-blank character on a line is '#', 0245 * the entire line is ignored as a comment. 0246 * 0247 * <p>Each set of rules consists of two groups, one forward, and one 0248 * reverse. This is a convention that is not enforced; rules for one 0249 * direction may be omitted, with the result that translations in 0250 * that direction will not modify the source text. In addition, 0251 * bidirectional forward-reverse rules may be specified for 0252 * symmetrical transformations. 0253 * 0254 * <p>Note: Another description of the Transliterator rule syntax is available in 0255 * <a href="https://www.unicode.org/reports/tr35/tr35-general.html#Transform_Rules_Syntax">section 0256 * Transform Rules Syntax of UTS #35: Unicode LDML</a>. 0257 * The rules are shown there using arrow symbols ← and → and ↔. 0258 * ICU supports both those and the equivalent ASCII symbols < and > and <>. 0259 * 0260 * <p>Rule statements take one of the following forms: 0261 * 0262 * <dl> 0263 * <dt><code>$alefmadda=\\u0622;</code></dt> 0264 * <dd><strong>Variable definition.</strong> The name on the 0265 * left is assigned the text on the right. In this example, 0266 * after this statement, instances of the left hand name, 0267 * "<code>$alefmadda</code>", will be replaced by 0268 * the Unicode character U+0622. Variable names must begin 0269 * with a letter and consist only of letters, digits, and 0270 * underscores. Case is significant. Duplicate names cause 0271 * an exception to be thrown, that is, variables cannot be 0272 * redefined. The right hand side may contain well-formed 0273 * text of any length, including no text at all ("<code>$empty=;</code>"). 0274 * The right hand side may contain embedded <code>UnicodeSet</code> 0275 * patterns, for example, "<code>$softvowel=[eiyEIY]</code>".</dd> 0276 * <dt><code>ai>$alefmadda;</code></dt> 0277 * <dd><strong>Forward translation rule.</strong> This rule 0278 * states that the string on the left will be changed to the 0279 * string on the right when performing forward 0280 * transliteration.</dd> 0281 * <dt><code>ai<$alefmadda;</code></dt> 0282 * <dd><strong>Reverse translation rule.</strong> This rule 0283 * states that the string on the right will be changed to 0284 * the string on the left when performing reverse 0285 * transliteration.</dd> 0286 * </dl> 0287 * 0288 * <dl> 0289 * <dt><code>ai<>$alefmadda;</code></dt> 0290 * <dd><strong>Bidirectional translation rule.</strong> This 0291 * rule states that the string on the right will be changed 0292 * to the string on the left when performing forward 0293 * transliteration, and vice versa when performing reverse 0294 * transliteration.</dd> 0295 * </dl> 0296 * 0297 * <p>Translation rules consist of a <em>match pattern</em> and an <em>output 0298 * string</em>. The match pattern consists of literal characters, 0299 * optionally preceded by context, and optionally followed by 0300 * context. Context characters, like literal pattern characters, 0301 * must be matched in the text being transliterated. However, unlike 0302 * literal pattern characters, they are not replaced by the output 0303 * text. For example, the pattern "<code>abc{def}</code>" 0304 * indicates the characters "<code>def</code>" must be 0305 * preceded by "<code>abc</code>" for a successful match. 0306 * If there is a successful match, "<code>def</code>" will 0307 * be replaced, but not "<code>abc</code>". The final '<code>}</code>' 0308 * is optional, so "<code>abc{def</code>" is equivalent to 0309 * "<code>abc{def}</code>". Another example is "<code>{123}456</code>" 0310 * (or "<code>123}456</code>") in which the literal 0311 * pattern "<code>123</code>" must be followed by "<code>456</code>". 0312 * 0313 * <p>The output string of a forward or reverse rule consists of 0314 * characters to replace the literal pattern characters. If the 0315 * output string contains the character '<code>|</code>', this is 0316 * taken to indicate the location of the <em>cursor</em> after 0317 * replacement. The cursor is the point in the text at which the 0318 * next replacement, if any, will be applied. The cursor is usually 0319 * placed within the replacement text; however, it can actually be 0320 * placed into the preceding or following context by using the 0321 * special character '@'. Examples: 0322 * 0323 * <pre> 0324 * a {foo} z > | @ bar; # foo -> bar, move cursor before a 0325 * {foo} xyz > bar @@|; # foo -> bar, cursor between y and z 0326 * </pre> 0327 * 0328 * <p><b>UnicodeSet</b> 0329 * 0330 * <p><code>UnicodeSet</code> patterns may appear anywhere that 0331 * makes sense. They may appear in variable definitions. 0332 * Contrariwise, <code>UnicodeSet</code> patterns may themselves 0333 * contain variable references, such as "<code>$a=[a-z];$not_a=[^$a]</code>", 0334 * or "<code>$range=a-z;$ll=[$range]</code>". 0335 * 0336 * <p><code>UnicodeSet</code> patterns may also be embedded directly 0337 * into rule strings. Thus, the following two rules are equivalent: 0338 * 0339 * <pre> 0340 * $vowel=[aeiou]; $vowel>'*'; # One way to do this 0341 * [aeiou]>'*'; # Another way 0342 * </pre> 0343 * 0344 * <p>See {@link UnicodeSet} for more documentation and examples. 0345 * 0346 * <p><b>Segments</b> 0347 * 0348 * <p>Segments of the input string can be matched and copied to the 0349 * output string. This makes certain sets of rules simpler and more 0350 * general, and makes reordering possible. For example: 0351 * 0352 * <pre> 0353 * ([a-z]) > $1 $1; # double lowercase letters 0354 * ([:Lu:]) ([:Ll:]) > $2 $1; # reverse order of Lu-Ll pairs 0355 * </pre> 0356 * 0357 * <p>The segment of the input string to be copied is delimited by 0358 * "<code>(</code>" and "<code>)</code>". Up to 0359 * nine segments may be defined. Segments may not overlap. In the 0360 * output string, "<code>$1</code>" through "<code>$9</code>" 0361 * represent the input string segments, in left-to-right order of 0362 * definition. 0363 * 0364 * <p><b>Anchors</b> 0365 * 0366 * <p>Patterns can be anchored to the beginning or the end of the text. This is done with the 0367 * special characters '<code>^</code>' and '<code>$</code>'. For example: 0368 * 0369 * <pre> 0370 * ^ a > 'BEG_A'; # match 'a' at start of text 0371 * a > 'A'; # match other instances of 'a' 0372 * z $ > 'END_Z'; # match 'z' at end of text 0373 * z > 'Z'; # match other instances of 'z' 0374 * </pre> 0375 * 0376 * <p>It is also possible to match the beginning or the end of the text using a <code>UnicodeSet</code>. 0377 * This is done by including a virtual anchor character '<code>$</code>' at the end of the 0378 * set pattern. Although this is usually the match character for the end anchor, the set will 0379 * match either the beginning or the end of the text, depending on its placement. For 0380 * example: 0381 * 0382 * <pre> 0383 * $x = [a-z$]; # match 'a' through 'z' OR anchor 0384 * $x 1 > 2; # match '1' after a-z or at the start 0385 * 3 $x > 4; # match '3' before a-z or at the end 0386 * </pre> 0387 * 0388 * <p><b>Example</b> 0389 * 0390 * <p>The following example rules illustrate many of the features of 0391 * the rule language. 0392 * 0393 * <table border="0" cellpadding="4"> 0394 * <tr> 0395 * <td style="vertical-align: top;">Rule 1.</td> 0396 * <td style="vertical-align: top; write-space: nowrap;"><code>abc{def}>x|y</code></td> 0397 * </tr> 0398 * <tr> 0399 * <td style="vertical-align: top;">Rule 2.</td> 0400 * <td style="vertical-align: top; write-space: nowrap;"><code>xyz>r</code></td> 0401 * </tr> 0402 * <tr> 0403 * <td style="vertical-align: top;">Rule 3.</td> 0404 * <td style="vertical-align: top; write-space: nowrap;"><code>yz>q</code></td> 0405 * </tr> 0406 * </table> 0407 * 0408 * <p>Applying these rules to the string "<code>adefabcdefz</code>" 0409 * yields the following results: 0410 * 0411 * <table border="0" cellpadding="4"> 0412 * <tr> 0413 * <td style="vertical-align: top; write-space: nowrap;"><code>|adefabcdefz</code></td> 0414 * <td style="vertical-align: top;">Initial state, no rules match. Advance 0415 * cursor.</td> 0416 * </tr> 0417 * <tr> 0418 * <td style="vertical-align: top; write-space: nowrap;"><code>a|defabcdefz</code></td> 0419 * <td style="vertical-align: top;">Still no match. Rule 1 does not match 0420 * because the preceding context is not present.</td> 0421 * </tr> 0422 * <tr> 0423 * <td style="vertical-align: top; write-space: nowrap;"><code>ad|efabcdefz</code></td> 0424 * <td style="vertical-align: top;">Still no match. Keep advancing until 0425 * there is a match...</td> 0426 * </tr> 0427 * <tr> 0428 * <td style="vertical-align: top; write-space: nowrap;"><code>ade|fabcdefz</code></td> 0429 * <td style="vertical-align: top;">...</td> 0430 * </tr> 0431 * <tr> 0432 * <td style="vertical-align: top; write-space: nowrap;"><code>adef|abcdefz</code></td> 0433 * <td style="vertical-align: top;">...</td> 0434 * </tr> 0435 * <tr> 0436 * <td style="vertical-align: top; write-space: nowrap;"><code>adefa|bcdefz</code></td> 0437 * <td style="vertical-align: top;">...</td> 0438 * </tr> 0439 * <tr> 0440 * <td style="vertical-align: top; write-space: nowrap;"><code>adefab|cdefz</code></td> 0441 * <td style="vertical-align: top;">...</td> 0442 * </tr> 0443 * <tr> 0444 * <td style="vertical-align: top; write-space: nowrap;"><code>adefabc|defz</code></td> 0445 * <td style="vertical-align: top;">Rule 1 matches; replace "<code>def</code>" 0446 * with "<code>xy</code>" and back up the cursor 0447 * to before the '<code>y</code>'.</td> 0448 * </tr> 0449 * <tr> 0450 * <td style="vertical-align: top; write-space: nowrap;"><code>adefabcx|yz</code></td> 0451 * <td style="vertical-align: top;">Although "<code>xyz</code>" is 0452 * present, rule 2 does not match because the cursor is 0453 * before the '<code>y</code>', not before the '<code>x</code>'. 0454 * Rule 3 does match. Replace "<code>yz</code>" 0455 * with "<code>q</code>".</td> 0456 * </tr> 0457 * <tr> 0458 * <td style="vertical-align: top; write-space: nowrap;"><code>adefabcxq|</code></td> 0459 * <td style="vertical-align: top;">The cursor is at the end; 0460 * transliteration is complete.</td> 0461 * </tr> 0462 * </table> 0463 * 0464 * <p>The order of rules is significant. If multiple rules may match 0465 * at some point, the first matching rule is applied. 0466 * 0467 * <p>Forward and reverse rules may have an empty output string. 0468 * Otherwise, an empty left or right hand side of any statement is a 0469 * syntax error. 0470 * 0471 * <p>Single quotes are used to quote any character other than a 0472 * digit or letter. To specify a single quote itself, inside or 0473 * outside of quotes, use two single quotes in a row. For example, 0474 * the rule "<code>'>'>o''clock</code>" changes the 0475 * string "<code>></code>" to the string "<code>o'clock</code>". 0476 * 0477 * <p><b>Notes</b> 0478 * 0479 * <p>While a Transliterator is being built from rules, it checks that 0480 * the rules are added in proper order. For example, if the rule 0481 * "a>x" is followed by the rule "ab>y", 0482 * then the second rule will throw an exception. The reason is that 0483 * the second rule can never be triggered, since the first rule 0484 * always matches anything it matches. In other words, the first 0485 * rule <em>masks</em> the second rule. 0486 * 0487 * @author Alan Liu 0488 * @stable ICU 2.0 0489 */ 0490 class U_I18N_API Transliterator : public UObject { 0491 0492 private: 0493 0494 /** 0495 * Programmatic name, e.g., "Latin-Arabic". 0496 */ 0497 UnicodeString ID; 0498 0499 /** 0500 * This transliterator's filter. Any character for which 0501 * <tt>filter.contains()</tt> returns <tt>false</tt> will not be 0502 * altered by this transliterator. If <tt>filter</tt> is 0503 * <tt>null</tt> then no filtering is applied. 0504 */ 0505 UnicodeFilter* filter; 0506 0507 int32_t maximumContextLength; 0508 0509 public: 0510 0511 /** 0512 * A context integer or pointer for a factory function, passed by 0513 * value. 0514 * @stable ICU 2.4 0515 */ 0516 union Token { 0517 /** 0518 * This token, interpreted as a 32-bit integer. 0519 * @stable ICU 2.4 0520 */ 0521 int32_t integer; 0522 /** 0523 * This token, interpreted as a native pointer. 0524 * @stable ICU 2.4 0525 */ 0526 void* pointer; 0527 }; 0528 0529 #ifndef U_HIDE_INTERNAL_API 0530 /** 0531 * Return a token containing an integer. 0532 * @return a token containing an integer. 0533 * @internal 0534 */ 0535 inline static Token integerToken(int32_t); 0536 0537 /** 0538 * Return a token containing a pointer. 0539 * @return a token containing a pointer. 0540 * @internal 0541 */ 0542 inline static Token pointerToken(void*); 0543 #endif /* U_HIDE_INTERNAL_API */ 0544 0545 /** 0546 * A function that creates and returns a Transliterator. When 0547 * invoked, it will be passed the ID string that is being 0548 * instantiated, together with the context pointer that was passed 0549 * in when the factory function was first registered. Many 0550 * factory functions will ignore both parameters, however, 0551 * functions that are registered to more than one ID may use the 0552 * ID or the context parameter to parameterize the transliterator 0553 * they create. 0554 * @param ID the string identifier for this transliterator 0555 * @param context a context pointer that will be stored and 0556 * later passed to the factory function when an ID matching 0557 * the registration ID is being instantiated with this factory. 0558 * @stable ICU 2.4 0559 */ 0560 typedef Transliterator* (U_EXPORT2 *Factory)(const UnicodeString& ID, Token context); 0561 0562 protected: 0563 0564 /** 0565 * Default constructor. 0566 * @param ID the string identifier for this transliterator 0567 * @param adoptedFilter the filter. Any character for which 0568 * <tt>filter.contains()</tt> returns <tt>false</tt> will not be 0569 * altered by this transliterator. If <tt>filter</tt> is 0570 * <tt>null</tt> then no filtering is applied. 0571 * @stable ICU 2.4 0572 */ 0573 Transliterator(const UnicodeString& ID, UnicodeFilter* adoptedFilter); 0574 0575 /** 0576 * Copy constructor. 0577 * @stable ICU 2.4 0578 */ 0579 Transliterator(const Transliterator&); 0580 0581 /** 0582 * Assignment operator. 0583 * @stable ICU 2.4 0584 */ 0585 Transliterator& operator=(const Transliterator&); 0586 0587 /** 0588 * Create a transliterator from a basic ID. This is an ID 0589 * containing only the forward direction source, target, and 0590 * variant. 0591 * @param id a basic ID of the form S-T or S-T/V. 0592 * @param canon canonical ID to assign to the object, or 0593 * nullptr to leave the ID unchanged 0594 * @return a newly created Transliterator or null if the ID is 0595 * invalid. 0596 * @stable ICU 2.4 0597 */ 0598 static Transliterator* createBasicInstance(const UnicodeString& id, 0599 const UnicodeString* canon); 0600 0601 friend class TransliteratorParser; // for parseID() 0602 friend class TransliteratorIDParser; // for createBasicInstance() 0603 friend class TransliteratorAlias; // for setID() 0604 0605 public: 0606 0607 /** 0608 * Destructor. 0609 * @stable ICU 2.0 0610 */ 0611 virtual ~Transliterator(); 0612 0613 /** 0614 * Implements Cloneable. 0615 * All subclasses are encouraged to implement this method if it is 0616 * possible and reasonable to do so. Subclasses that are to be 0617 * registered with the system using <tt>registerInstance()</tt> 0618 * are required to implement this method. If a subclass does not 0619 * implement clone() properly and is registered with the system 0620 * using registerInstance(), then the default clone() implementation 0621 * will return null, and calls to createInstance() will fail. 0622 * 0623 * @return a copy of the object. 0624 * @see #registerInstance 0625 * @stable ICU 2.0 0626 */ 0627 virtual Transliterator* clone() const; 0628 0629 /** 0630 * Transliterates a segment of a string, with optional filtering. 0631 * 0632 * @param text the string to be transliterated 0633 * @param start the beginning index, inclusive; <code>0 <= start 0634 * <= limit</code>. 0635 * @param limit the ending index, exclusive; <code>start <= limit 0636 * <= text.length()</code>. 0637 * @return The new limit index. The text previously occupying <code>[start, 0638 * limit)</code> has been transliterated, possibly to a string of a different 0639 * length, at <code>[start, </code><em>new-limit</em><code>)</code>, where 0640 * <em>new-limit</em> is the return value. If the input offsets are out of bounds, 0641 * the returned value is -1 and the input string remains unchanged. 0642 * @stable ICU 2.0 0643 */ 0644 virtual int32_t transliterate(Replaceable& text, 0645 int32_t start, int32_t limit) const; 0646 0647 /** 0648 * Transliterates an entire string in place. Convenience method. 0649 * @param text the string to be transliterated 0650 * @stable ICU 2.0 0651 */ 0652 virtual void transliterate(Replaceable& text) const; 0653 0654 /** 0655 * Transliterates the portion of the text buffer that can be 0656 * transliterated unambiguosly after new text has been inserted, 0657 * typically as a result of a keyboard event. The new text in 0658 * <code>insertion</code> will be inserted into <code>text</code> 0659 * at <code>index.limit</code>, advancing 0660 * <code>index.limit</code> by <code>insertion.length()</code>. 0661 * Then the transliterator will try to transliterate characters of 0662 * <code>text</code> between <code>index.cursor</code> and 0663 * <code>index.limit</code>. Characters before 0664 * <code>index.cursor</code> will not be changed. 0665 * 0666 * <p>Upon return, values in <code>index</code> will be updated. 0667 * <code>index.start</code> will be advanced to the first 0668 * character that future calls to this method will read. 0669 * <code>index.cursor</code> and <code>index.limit</code> will 0670 * be adjusted to delimit the range of text that future calls to 0671 * this method may change. 0672 * 0673 * <p>Typical usage of this method begins with an initial call 0674 * with <code>index.start</code> and <code>index.limit</code> 0675 * set to indicate the portion of <code>text</code> to be 0676 * transliterated, and <code>index.cursor == index.start</code>. 0677 * Thereafter, <code>index</code> can be used without 0678 * modification in future calls, provided that all changes to 0679 * <code>text</code> are made via this method. 0680 * 0681 * <p>This method assumes that future calls may be made that will 0682 * insert new text into the buffer. As a result, it only performs 0683 * unambiguous transliterations. After the last call to this 0684 * method, there may be untransliterated text that is waiting for 0685 * more input to resolve an ambiguity. In order to perform these 0686 * pending transliterations, clients should call 0687 * {@link #finishTransliteration } after the last call to this 0688 * method has been made. 0689 * 0690 * @param text the buffer holding transliterated and untransliterated text 0691 * @param index an array of three integers. 0692 * 0693 * <ul><li><code>index.start</code>: the beginning index, 0694 * inclusive; <code>0 <= index.start <= index.limit</code>. 0695 * 0696 * <li><code>index.limit</code>: the ending index, exclusive; 0697 * <code>index.start <= index.limit <= text.length()</code>. 0698 * <code>insertion</code> is inserted at 0699 * <code>index.limit</code>. 0700 * 0701 * <li><code>index.cursor</code>: the next character to be 0702 * considered for transliteration; <code>index.start <= 0703 * index.cursor <= index.limit</code>. Characters before 0704 * <code>index.cursor</code> will not be changed by future calls 0705 * to this method.</ul> 0706 * 0707 * @param insertion text to be inserted and possibly 0708 * transliterated into the translation buffer at 0709 * <code>index.limit</code>. If <code>null</code> then no text 0710 * is inserted. 0711 * @param status Output param to filled in with a success or an error. 0712 * @see #handleTransliterate 0713 * @exception IllegalArgumentException if <code>index</code> 0714 * is invalid 0715 * @see UTransPosition 0716 * @stable ICU 2.0 0717 */ 0718 virtual void transliterate(Replaceable& text, UTransPosition& index, 0719 const UnicodeString& insertion, 0720 UErrorCode& status) const; 0721 0722 /** 0723 * Transliterates the portion of the text buffer that can be 0724 * transliterated unambiguosly after a new character has been 0725 * inserted, typically as a result of a keyboard event. This is a 0726 * convenience method. 0727 * @param text the buffer holding transliterated and 0728 * untransliterated text 0729 * @param index an array of three integers. 0730 * @param insertion text to be inserted and possibly 0731 * transliterated into the translation buffer at 0732 * <code>index.limit</code>. 0733 * @param status Output param to filled in with a success or an error. 0734 * @see #transliterate(Replaceable&, UTransPosition&, const UnicodeString&, UErrorCode&) const 0735 * @stable ICU 2.0 0736 */ 0737 virtual void transliterate(Replaceable& text, UTransPosition& index, 0738 UChar32 insertion, 0739 UErrorCode& status) const; 0740 0741 /** 0742 * Transliterates the portion of the text buffer that can be 0743 * transliterated unambiguosly. This is a convenience method; see 0744 * {@link #transliterate(Replaceable&, UTransPosition&, const UnicodeString&, UErrorCode&) const } 0745 * for details. 0746 * @param text the buffer holding transliterated and 0747 * untransliterated text 0748 * @param index an array of three integers. 0749 * @param status Output param to filled in with a success or an error. 0750 * @see #transliterate(Replaceable&, UTransPosition&, const UnicodeString&, UErrorCode &) const 0751 * @stable ICU 2.0 0752 */ 0753 virtual void transliterate(Replaceable& text, UTransPosition& index, 0754 UErrorCode& status) const; 0755 0756 /** 0757 * Finishes any pending transliterations that were waiting for 0758 * more characters. Clients should call this method as the last 0759 * call after a sequence of one or more calls to 0760 * <code>transliterate()</code>. 0761 * @param text the buffer holding transliterated and 0762 * untransliterated text. 0763 * @param index the array of indices previously passed to {@link #transliterate } 0764 * @stable ICU 2.0 0765 */ 0766 virtual void finishTransliteration(Replaceable& text, 0767 UTransPosition& index) const; 0768 0769 private: 0770 0771 /** 0772 * This internal method does incremental transliteration. If the 0773 * 'insertion' is non-null then we append it to 'text' before 0774 * proceeding. This method calls through to the pure virtual 0775 * framework method handleTransliterate() to do the actual 0776 * work. 0777 * @param text the buffer holding transliterated and 0778 * untransliterated text 0779 * @param index an array of three integers. See {@link 0780 * #transliterate(Replaceable, int[], String)}. 0781 * @param insertion text to be inserted and possibly 0782 * transliterated into the translation buffer at 0783 * <code>index.limit</code>. 0784 * @param status Output param to filled in with a success or an error. 0785 */ 0786 void _transliterate(Replaceable& text, 0787 UTransPosition& index, 0788 const UnicodeString* insertion, 0789 UErrorCode &status) const; 0790 0791 protected: 0792 0793 /** 0794 * Abstract method that concrete subclasses define to implement 0795 * their transliteration algorithm. This method handles both 0796 * incremental and non-incremental transliteration. Let 0797 * <code>originalStart</code> refer to the value of 0798 * <code>pos.start</code> upon entry. 0799 * 0800 * <ul> 0801 * <li>If <code>incremental</code> is false, then this method 0802 * should transliterate all characters between 0803 * <code>pos.start</code> and <code>pos.limit</code>. Upon return 0804 * <code>pos.start</code> must == <code> pos.limit</code>.</li> 0805 * 0806 * <li>If <code>incremental</code> is true, then this method 0807 * should transliterate all characters between 0808 * <code>pos.start</code> and <code>pos.limit</code> that can be 0809 * unambiguously transliterated, regardless of future insertions 0810 * of text at <code>pos.limit</code>. Upon return, 0811 * <code>pos.start</code> should be in the range 0812 * [<code>originalStart</code>, <code>pos.limit</code>). 0813 * <code>pos.start</code> should be positioned such that 0814 * characters [<code>originalStart</code>, <code> 0815 * pos.start</code>) will not be changed in the future by this 0816 * transliterator and characters [<code>pos.start</code>, 0817 * <code>pos.limit</code>) are unchanged.</li> 0818 * </ul> 0819 * 0820 * <p>Implementations of this method should also obey the 0821 * following invariants:</p> 0822 * 0823 * <ul> 0824 * <li> <code>pos.limit</code> and <code>pos.contextLimit</code> 0825 * should be updated to reflect changes in length of the text 0826 * between <code>pos.start</code> and <code>pos.limit</code>. The 0827 * difference <code> pos.contextLimit - pos.limit</code> should 0828 * not change.</li> 0829 * 0830 * <li><code>pos.contextStart</code> should not change.</li> 0831 * 0832 * <li>Upon return, neither <code>pos.start</code> nor 0833 * <code>pos.limit</code> should be less than 0834 * <code>originalStart</code>.</li> 0835 * 0836 * <li>Text before <code>originalStart</code> and text after 0837 * <code>pos.limit</code> should not change.</li> 0838 * 0839 * <li>Text before <code>pos.contextStart</code> and text after 0840 * <code> pos.contextLimit</code> should be ignored.</li> 0841 * </ul> 0842 * 0843 * <p>Subclasses may safely assume that all characters in 0844 * [<code>pos.start</code>, <code>pos.limit</code>) are filtered. 0845 * In other words, the filter has already been applied by the time 0846 * this method is called. See 0847 * <code>filteredTransliterate()</code>. 0848 * 0849 * <p>This method is <b>not</b> for public consumption. Calling 0850 * this method directly will transliterate 0851 * [<code>pos.start</code>, <code>pos.limit</code>) without 0852 * applying the filter. End user code should call <code> 0853 * transliterate()</code> instead of this method. Subclass code 0854 * and wrapping transliterators should call 0855 * <code>filteredTransliterate()</code> instead of this method.<p> 0856 * 0857 * @param text the buffer holding transliterated and 0858 * untransliterated text 0859 * 0860 * @param pos the indices indicating the start, limit, context 0861 * start, and context limit of the text. 0862 * 0863 * @param incremental if true, assume more text may be inserted at 0864 * <code>pos.limit</code> and act accordingly. Otherwise, 0865 * transliterate all text between <code>pos.start</code> and 0866 * <code>pos.limit</code> and move <code>pos.start</code> up to 0867 * <code>pos.limit</code>. 0868 * 0869 * @see #transliterate 0870 * @stable ICU 2.4 0871 */ 0872 virtual void handleTransliterate(Replaceable& text, 0873 UTransPosition& pos, 0874 UBool incremental) const = 0; 0875 0876 public: 0877 /** 0878 * Transliterate a substring of text, as specified by index, taking filters 0879 * into account. This method is for subclasses that need to delegate to 0880 * another transliterator. 0881 * @param text the text to be transliterated 0882 * @param index the position indices 0883 * @param incremental if true, then assume more characters may be inserted 0884 * at index.limit, and postpone processing to accommodate future incoming 0885 * characters 0886 * @stable ICU 2.4 0887 */ 0888 virtual void filteredTransliterate(Replaceable& text, 0889 UTransPosition& index, 0890 UBool incremental) const; 0891 0892 private: 0893 0894 /** 0895 * Top-level transliteration method, handling filtering, incremental and 0896 * non-incremental transliteration, and rollback. All transliteration 0897 * public API methods eventually call this method with a rollback argument 0898 * of true. Other entities may call this method but rollback should be 0899 * false. 0900 * 0901 * <p>If this transliterator has a filter, break up the input text into runs 0902 * of unfiltered characters. Pass each run to 0903 * subclass.handleTransliterate(). 0904 * 0905 * <p>In incremental mode, if rollback is true, perform a special 0906 * incremental procedure in which several passes are made over the input 0907 * text, adding one character at a time, and committing successful 0908 * transliterations as they occur. Unsuccessful transliterations are rolled 0909 * back and retried with additional characters to give correct results. 0910 * 0911 * @param text the text to be transliterated 0912 * @param index the position indices 0913 * @param incremental if true, then assume more characters may be inserted 0914 * at index.limit, and postpone processing to accommodate future incoming 0915 * characters 0916 * @param rollback if true and if incremental is true, then perform special 0917 * incremental processing, as described above, and undo partial 0918 * transliterations where necessary. If incremental is false then this 0919 * parameter is ignored. 0920 */ 0921 virtual void filteredTransliterate(Replaceable& text, 0922 UTransPosition& index, 0923 UBool incremental, 0924 UBool rollback) const; 0925 0926 public: 0927 0928 /** 0929 * Returns the length of the longest context required by this transliterator. 0930 * This is <em>preceding</em> context. The default implementation supplied 0931 * by <code>Transliterator</code> returns zero; subclasses 0932 * that use preceding context should override this method to return the 0933 * correct value. For example, if a transliterator translates "ddd" (where 0934 * d is any digit) to "555" when preceded by "(ddd)", then the preceding 0935 * context length is 5, the length of "(ddd)". 0936 * 0937 * @return The maximum number of preceding context characters this 0938 * transliterator needs to examine 0939 * @stable ICU 2.0 0940 */ 0941 int32_t getMaximumContextLength(void) const; 0942 0943 protected: 0944 0945 /** 0946 * Method for subclasses to use to set the maximum context length. 0947 * @param maxContextLength the new value to be set. 0948 * @see #getMaximumContextLength 0949 * @stable ICU 2.4 0950 */ 0951 void setMaximumContextLength(int32_t maxContextLength); 0952 0953 public: 0954 0955 /** 0956 * Returns a programmatic identifier for this transliterator. 0957 * If this identifier is passed to <code>createInstance()</code>, it 0958 * will return this object, if it has been registered. 0959 * @return a programmatic identifier for this transliterator. 0960 * @see #registerInstance 0961 * @see #registerFactory 0962 * @see #getAvailableIDs 0963 * @stable ICU 2.0 0964 */ 0965 virtual const UnicodeString& getID(void) const; 0966 0967 /** 0968 * Returns a name for this transliterator that is appropriate for 0969 * display to the user in the default locale. See {@link #getDisplayName } 0970 * for details. 0971 * @param ID the string identifier for this transliterator 0972 * @param result Output param to receive the display name 0973 * @return A reference to 'result'. 0974 * @stable ICU 2.0 0975 */ 0976 static UnicodeString& U_EXPORT2 getDisplayName(const UnicodeString& ID, 0977 UnicodeString& result); 0978 0979 /** 0980 * Returns a name for this transliterator that is appropriate for 0981 * display to the user in the given locale. This name is taken 0982 * from the locale resource data in the standard manner of the 0983 * <code>java.text</code> package. 0984 * 0985 * <p>If no localized names exist in the system resource bundles, 0986 * a name is synthesized using a localized 0987 * <code>MessageFormat</code> pattern from the resource data. The 0988 * arguments to this pattern are an integer followed by one or two 0989 * strings. The integer is the number of strings, either 1 or 2. 0990 * The strings are formed by splitting the ID for this 0991 * transliterator at the first '-'. If there is no '-', then the 0992 * entire ID forms the only string. 0993 * @param ID the string identifier for this transliterator 0994 * @param inLocale the Locale in which the display name should be 0995 * localized. 0996 * @param result Output param to receive the display name 0997 * @return A reference to 'result'. 0998 * @stable ICU 2.0 0999 */ 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>nullptr</tt> 1006 * if this transliterator uses no filter. 1007 * @return the filter used by this transliterator, or <tt>nullptr</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>nullptr</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>nullptr</tt>. 1018 * @return the filter used by this transliterator, or <tt>nullptr</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 receive 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 1170 * {@link #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 parameter 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 parameter 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 parameter 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 override = 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 /* U_SHOW_CPLUSPLUS_API */ 1592 1593 #endif
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |