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