ICU 72.1 72.1
translit.h
Go to the documentation of this file.
1// © 2016 and later: Unicode, Inc. and others.
2// License & terms of use: http://www.unicode.org/copyright.html
3/*
4**********************************************************************
5* Copyright (C) 1999-2014, International Business Machines
6* Corporation and others. All Rights Reserved.
7**********************************************************************
8* Date Name Description
9* 11/17/99 aliu Creation.
10**********************************************************************
11*/
12#ifndef TRANSLIT_H
13#define TRANSLIT_H
14
15#include "unicode/utypes.h"
16
17#if U_SHOW_CPLUSPLUS_API
18
24#if !UCONFIG_NO_TRANSLITERATION
25
26#include "unicode/uobject.h"
27#include "unicode/unistr.h"
28#include "unicode/parseerr.h"
29#include "unicode/utrans.h" // UTransPosition, UTransDirection
30#include "unicode/strenum.h"
31
32U_NAMESPACE_BEGIN
33
34class UnicodeFilter;
35class UnicodeSet;
36class TransliteratorParser;
37class NormalizationTransliterator;
38class TransliteratorIDParser;
39
491
492private:
493
497 UnicodeString ID;
498
505 UnicodeFilter* filter;
506
507 int32_t maximumContextLength;
508
509 public:
510
516 union Token {
521 int32_t integer;
526 void* pointer;
527 };
528
529#ifndef U_HIDE_INTERNAL_API
535 inline static Token integerToken(int32_t);
536
542 inline static Token pointerToken(void*);
543#endif /* U_HIDE_INTERNAL_API */
544
560 typedef Transliterator* (U_EXPORT2 *Factory)(const UnicodeString& ID, Token context);
561
562protected:
563
573 Transliterator(const UnicodeString& ID, UnicodeFilter* adoptedFilter);
574
580
586
599 const UnicodeString* canon);
600
601 friend class TransliteratorParser; // for parseID()
602 friend class TransliteratorIDParser; // for createBasicInstance()
603 friend class TransliteratorAlias; // for setID()
604
605public:
606
612
627 virtual Transliterator* clone() const;
628
644 virtual int32_t transliterate(Replaceable& text,
645 int32_t start, int32_t limit) const;
646
652 virtual void transliterate(Replaceable& text) const;
653
718 virtual void transliterate(Replaceable& text, UTransPosition& index,
719 const UnicodeString& insertion,
720 UErrorCode& status) const;
721
737 virtual void transliterate(Replaceable& text, UTransPosition& index,
738 UChar32 insertion,
739 UErrorCode& status) const;
740
753 virtual void transliterate(Replaceable& text, UTransPosition& index,
754 UErrorCode& status) const;
755
767 UTransPosition& index) const;
768
769private:
770
786 void _transliterate(Replaceable& text,
787 UTransPosition& index,
788 const UnicodeString* insertion,
789 UErrorCode &status) const;
790
791protected:
792
873 UTransPosition& pos,
874 UBool incremental) const = 0;
875
876public:
889 UTransPosition& index,
890 UBool incremental) const;
891
892private:
893
921 virtual void filteredTransliterate(Replaceable& text,
922 UTransPosition& index,
923 UBool incremental,
924 UBool rollback) const;
925
926public:
927
941 int32_t getMaximumContextLength(void) const;
942
943protected:
944
951 void setMaximumContextLength(int32_t maxContextLength);
952
953public:
954
965 virtual const UnicodeString& getID(void) const;
966
976 static UnicodeString& U_EXPORT2 getDisplayName(const UnicodeString& ID,
977 UnicodeString& result);
978
1000 static UnicodeString& U_EXPORT2 getDisplayName(const UnicodeString& ID,
1001 const Locale& inLocale,
1002 UnicodeString& result);
1003
1011 const UnicodeFilter* getFilter(void) const;
1012
1023
1034 void adoptFilter(UnicodeFilter* adoptedFilter);
1035
1056
1073 static Transliterator* U_EXPORT2 createInstance(const UnicodeString& ID,
1074 UTransDirection dir,
1075 UParseError& parseError,
1076 UErrorCode& status);
1077
1088 static Transliterator* U_EXPORT2 createInstance(const UnicodeString& ID,
1089 UTransDirection dir,
1090 UErrorCode& status);
1091
1109 static Transliterator* U_EXPORT2 createFromRules(const UnicodeString& ID,
1110 const UnicodeString& rules,
1111 UTransDirection dir,
1112 UParseError& parseError,
1113 UErrorCode& status);
1114
1127 UBool escapeUnprintable) const;
1128
1141 int32_t countElements() const;
1142
1162 const Transliterator& getElement(int32_t index, UErrorCode& ec) const;
1163
1180
1195 virtual void handleGetSourceSet(UnicodeSet& result) const;
1196
1210 virtual UnicodeSet& getTargetSet(UnicodeSet& result) const;
1211
1212public:
1213
1230 static void U_EXPORT2 registerFactory(const UnicodeString& id,
1231 Factory factory,
1232 Token context);
1233
1255 static void U_EXPORT2 registerInstance(Transliterator* adoptedObj);
1256
1271 static void U_EXPORT2 registerAlias(const UnicodeString& aliasID,
1272 const UnicodeString& realID);
1273
1274protected:
1275
1276#ifndef U_HIDE_INTERNAL_API
1286 static void _registerFactory(const UnicodeString& id,
1287 Factory factory,
1288 Token context);
1289
1293 static void _registerInstance(Transliterator* adoptedObj);
1294
1298 static void _registerAlias(const UnicodeString& aliasID, const UnicodeString& realID);
1299
1333 static void _registerSpecialInverse(const UnicodeString& target,
1334 const UnicodeString& inverseTarget,
1335 UBool bidirectional);
1336#endif /* U_HIDE_INTERNAL_API */
1337
1338public:
1339
1357 static void U_EXPORT2 unregister(const UnicodeString& ID);
1358
1359public:
1360
1371
1377 static int32_t U_EXPORT2 countAvailableSources(void);
1378
1388 static UnicodeString& U_EXPORT2 getAvailableSource(int32_t index,
1389 UnicodeString& result);
1390
1399 static int32_t U_EXPORT2 countAvailableTargets(const UnicodeString& source);
1400
1412 static UnicodeString& U_EXPORT2 getAvailableTarget(int32_t index,
1413 const UnicodeString& source,
1414 UnicodeString& result);
1415
1423 static int32_t U_EXPORT2 countAvailableVariants(const UnicodeString& source,
1424 const UnicodeString& target);
1425
1439 static UnicodeString& U_EXPORT2 getAvailableVariant(int32_t index,
1440 const UnicodeString& source,
1441 const UnicodeString& target,
1442 UnicodeString& result);
1443
1444protected:
1445
1446#ifndef U_HIDE_INTERNAL_API
1451 static int32_t _countAvailableSources(void);
1452
1457 static UnicodeString& _getAvailableSource(int32_t index,
1458 UnicodeString& result);
1459
1464 static int32_t _countAvailableTargets(const UnicodeString& source);
1465
1470 static UnicodeString& _getAvailableTarget(int32_t index,
1471 const UnicodeString& source,
1472 UnicodeString& result);
1473
1478 static int32_t _countAvailableVariants(const UnicodeString& source,
1479 const UnicodeString& target);
1480
1486 const UnicodeString& source,
1487 const UnicodeString& target,
1488 UnicodeString& result);
1489#endif /* U_HIDE_INTERNAL_API */
1490
1491protected:
1492
1499 void setID(const UnicodeString& id);
1500
1501public:
1502
1513 static UClassID U_EXPORT2 getStaticClassID(void);
1514
1530 virtual UClassID getDynamicClassID(void) const override = 0;
1531
1532private:
1533 static UBool initializeRegistry(UErrorCode &status);
1534
1535public:
1536#ifndef U_HIDE_OBSOLETE_API
1544 static int32_t U_EXPORT2 countAvailableIDs(void);
1545
1558 static const UnicodeString& U_EXPORT2 getAvailableID(int32_t index);
1559#endif /* U_HIDE_OBSOLETE_API */
1560};
1561
1562inline int32_t Transliterator::getMaximumContextLength(void) const {
1563 return maximumContextLength;
1564}
1565
1566inline void Transliterator::setID(const UnicodeString& id) {
1567 ID = id;
1568 // NUL-terminate the ID string, which is a non-aliased copy.
1569 ID.append((char16_t)0);
1570 ID.truncate(ID.length()-1);
1571}
1572
1573#ifndef U_HIDE_INTERNAL_API
1574inline Transliterator::Token Transliterator::integerToken(int32_t i) {
1575 Token t;
1576 t.integer = i;
1577 return t;
1578}
1579
1580inline Transliterator::Token Transliterator::pointerToken(void* p) {
1581 Token t;
1582 t.pointer = p;
1583 return t;
1584}
1585#endif /* U_HIDE_INTERNAL_API */
1586
1587U_NAMESPACE_END
1588
1589#endif /* #if !UCONFIG_NO_TRANSLITERATION */
1590
1591#endif /* U_SHOW_CPLUSPLUS_API */
1592
1593#endif
A Locale object represents a specific geographical, political, or cultural region.
Definition: locid.h:195
Replaceable is an abstract base class representing a string of characters that supports the replaceme...
Definition: rep.h:77
Base class for 'pure' C++ implementations of uenum api.
Definition: strenum.h:61
Transliterator is an abstract class that transliterates text from one format to another.
Definition: translit.h:490
static Transliterator * createFromRules(const UnicodeString &ID, const UnicodeString &rules, UTransDirection dir, UParseError &parseError, UErrorCode &status)
Returns a Transliterator object constructed from the given rule string.
static UnicodeString & getAvailableVariant(int32_t index, const UnicodeString &source, const UnicodeString &target, UnicodeString &result)
Return a registered variant specifier for a given source-target pair.
virtual void transliterate(Replaceable &text) const
Transliterates an entire string in place.
static UClassID getStaticClassID(void)
Return the class ID for this class.
const UnicodeFilter * getFilter(void) const
Returns the filter used by this transliterator, or NULL if this transliterator uses no filter.
const Transliterator & getElement(int32_t index, UErrorCode &ec) const
Return an element that makes up this transliterator.
virtual const UnicodeString & getID(void) const
Returns a programmatic identifier for this transliterator.
void adoptFilter(UnicodeFilter *adoptedFilter)
Changes the filter used by this transliterator.
static int32_t countAvailableIDs(void)
Return the number of IDs currently registered with the system.
static UnicodeString & _getAvailableVariant(int32_t index, const UnicodeString &source, const UnicodeString &target, UnicodeString &result)
Non-mutexed internal method.
static void _registerAlias(const UnicodeString &aliasID, const UnicodeString &realID)
static void unregister(const UnicodeString &ID)
Unregisters a transliterator or class.
static UnicodeString & getDisplayName(const UnicodeString &ID, UnicodeString &result)
Returns a name for this transliterator that is appropriate for display to the user in the default loc...
virtual int32_t transliterate(Replaceable &text, int32_t start, int32_t limit) const
Transliterates a segment of a string, with optional filtering.
Transliterator & operator=(const Transliterator &)
Assignment operator.
virtual UnicodeSet & getTargetSet(UnicodeSet &result) const
Returns the set of all characters that may be generated as replacement text by this transliterator.
virtual void handleGetSourceSet(UnicodeSet &result) const
Framework method that returns the set of all characters that may be modified in the input text by thi...
static UnicodeString & getDisplayName(const UnicodeString &ID, const Locale &inLocale, UnicodeString &result)
Returns a name for this transliterator that is appropriate for display to the user in the given local...
static int32_t countAvailableSources(void)
Return the number of registered source specifiers.
Transliterator * createInverse(UErrorCode &status) const
Returns this transliterator's inverse.
static UnicodeString & getAvailableSource(int32_t index, UnicodeString &result)
Return a registered source specifier.
static Transliterator * createInstance(const UnicodeString &ID, UTransDirection dir, UErrorCode &status)
Returns a Transliterator object given its ID.
virtual ~Transliterator()
Destructor.
static void registerAlias(const UnicodeString &aliasID, const UnicodeString &realID)
Registers an ID string as an alias of another ID string.
static int32_t countAvailableVariants(const UnicodeString &source, const UnicodeString &target)
Return the number of registered variant specifiers for a given source-target pair.
static Transliterator * createBasicInstance(const UnicodeString &id, const UnicodeString *canon)
Create a transliterator from a basic ID.
virtual void transliterate(Replaceable &text, UTransPosition &index, UErrorCode &status) const
Transliterates the portion of the text buffer that can be transliterated unambiguosly.
virtual Transliterator * clone() const
Implements Cloneable.
virtual void handleTransliterate(Replaceable &text, UTransPosition &pos, UBool incremental) const =0
Abstract method that concrete subclasses define to implement their transliteration algorithm.
virtual void filteredTransliterate(Replaceable &text, UTransPosition &index, UBool incremental) const
Transliterate a substring of text, as specified by index, taking filters into account.
Transliterator *(* Factory)(const UnicodeString &ID, Token context)
A function that creates and returns a Transliterator.
Definition: translit.h:560
static UnicodeString & _getAvailableTarget(int32_t index, const UnicodeString &source, UnicodeString &result)
Non-mutexed internal method.
static UnicodeString & getAvailableTarget(int32_t index, const UnicodeString &source, UnicodeString &result)
Return a registered target specifier for a given source.
static void _registerSpecialInverse(const UnicodeString &target, const UnicodeString &inverseTarget, UBool bidirectional)
Register two targets as being inverses of one another.
UnicodeSet & getSourceSet(UnicodeSet &result) const
Returns the set of all characters that may be modified in the input text by this Transliterator.
static int32_t _countAvailableSources(void)
Non-mutexed internal method.
static int32_t _countAvailableTargets(const UnicodeString &source)
Non-mutexed internal method.
virtual UnicodeString & toRules(UnicodeString &result, UBool escapeUnprintable) const
Create a rule string that can be passed to createFromRules() to recreate this transliterator.
virtual void transliterate(Replaceable &text, UTransPosition &index, const UnicodeString &insertion, UErrorCode &status) const
Transliterates the portion of the text buffer that can be transliterated unambiguosly after new text ...
static int32_t countAvailableTargets(const UnicodeString &source)
Return the number of registered target specifiers for a given source specifier.
Transliterator(const UnicodeString &ID, UnicodeFilter *adoptedFilter)
Default constructor.
Transliterator(const Transliterator &)
Copy constructor.
static void registerFactory(const UnicodeString &id, Factory factory, Token context)
Registers a factory function that creates transliterators of a given ID.
virtual UClassID getDynamicClassID(void) const override=0
Returns a unique class ID polymorphically.
static UnicodeString & _getAvailableSource(int32_t index, UnicodeString &result)
Non-mutexed internal method.
void setMaximumContextLength(int32_t maxContextLength)
Method for subclasses to use to set the maximum context length.
static const UnicodeString & getAvailableID(int32_t index)
Return the index-th available ID.
static Transliterator * createInstance(const UnicodeString &ID, UTransDirection dir, UParseError &parseError, UErrorCode &status)
Returns a Transliterator object given its ID.
static void _registerFactory(const UnicodeString &id, Factory factory, Token context)
static void registerInstance(Transliterator *adoptedObj)
Registers an instance obj of a subclass of Transliterator with the system.
UnicodeFilter * orphanFilter(void)
Returns the filter used by this transliterator, or NULL if this transliterator uses no filter.
int32_t countElements() const
Return the number of elements that make up this transliterator.
virtual void finishTransliteration(Replaceable &text, UTransPosition &index) const
Finishes any pending transliterations that were waiting for more characters.
virtual void transliterate(Replaceable &text, UTransPosition &index, UChar32 insertion, UErrorCode &status) const
Transliterates the portion of the text buffer that can be transliterated unambiguosly after a new cha...
static void _registerInstance(Transliterator *adoptedObj)
static StringEnumeration * getAvailableIDs(UErrorCode &ec)
Return a StringEnumeration over the IDs available at the time of the call, including user-registered ...
static int32_t _countAvailableVariants(const UnicodeString &source, const UnicodeString &target)
Non-mutexed internal method.
UObject is the common ICU "boilerplate" class.
Definition: uobject.h:223
UnicodeFilter defines a protocol for selecting a subset of the full range (U+0000 to U+10FFFF) of Uni...
Definition: unifilt.h:65
A mutable set of Unicode characters and multicharacter strings.
Definition: uniset.h:285
UnicodeString is a string class that stores Unicode characters directly and provides similar function...
Definition: unistr.h:296
UBool truncate(int32_t targetLength)
Truncate this UnicodeString to the targetLength.
Definition: unistr.h:4730
UnicodeString & append(const UnicodeString &srcText, int32_t srcStart, int32_t srcLength)
Append the characters in srcText in the range [srcStart, srcStart + srcLength) to the UnicodeString o...
Definition: unistr.h:4622
C API: Parse Error Information.
C++ API: String Enumeration.
A UParseError struct is used to returned detailed information about parsing errors.
Definition: parseerr.h:58
Position structure for utrans_transIncremental() incremental transliteration.
Definition: utrans.h:125
int32_t UChar32
Define UChar32 as a type for single Unicode code points.
Definition: umachine.h:461
int8_t UBool
The ICU boolean type, a signed-byte integer.
Definition: umachine.h:269
A context integer or pointer for a factory function, passed by value.
Definition: translit.h:516
void * pointer
This token, interpreted as a native pointer.
Definition: translit.h:526
int32_t integer
This token, interpreted as a 32-bit integer.
Definition: translit.h:521
C++ API: Unicode String.
C++ API: Common ICU base class UObject.
void * UClassID
UClassID is used to identify classes without using the compiler's RTTI.
Definition: uobject.h:96
C API: Transliterator.
UTransDirection
Direction constant indicating the direction in a transliterator, e.g., the forward or reverse rules o...
Definition: utrans.h:83
Basic definitions for ICU, for both C and C++ APIs.
UErrorCode
Standard ICU4C error code type, a substitute for exceptions.
Definition: utypes.h:415
#define U_I18N_API
Set to export library symbols from inside the i18n library, and to import them from outside.
Definition: utypes.h:301