intl/icu/source/i18n/unicode/translit.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial