ICU 56.1: msgfmt.h Source File

ICU 56.1 56.1

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

msgfmt.h
Go to the documentation of this file.
1 /*
2 * Copyright (C) 2007-2013, International Business Machines Corporation and
3 * others. All Rights Reserved.
4 ********************************************************************************
5 *
6 * File MSGFMT.H
7 *
8 * Modification History:
9 *
10 * Date Name Description
11 * 02/19/97 aliu Converted from java.
12 * 03/20/97 helena Finished first cut of implementation.
13 * 07/22/98 stephen Removed operator!= (defined in Format)
14 * 08/19/2002 srl Removing Javaisms
15 *******************************************************************************/
16
17 #ifndef MSGFMT_H
18 #define MSGFMT_H
19
20 #include "unicode/utypes.h"
21
27 #if !UCONFIG_NO_FORMATTING
28
29 #include "unicode/format.h"
30 #include "unicode/locid.h"
31 #include "unicode/messagepattern.h"
32 #include "unicode/parseerr.h"
33 #include "unicode/plurfmt.h"
34 #include "unicode/plurrule.h"
35
36 U_CDECL_BEGIN
37 // Forward declaration.
38 struct UHashtable;
39 typedef struct UHashtable UHashtable;
40 U_CDECL_END
41
42 U_NAMESPACE_BEGIN
43
44 class AppendableWrapper;
45 class DateFormat;
46 class NumberFormat;
47
323 class U_I18N_API MessageFormat : public Format {
324 public:
325 #ifndef U_HIDE_OBSOLETE_API
326
331 enum EFormatNumber {
337 kMaxFormat = 10
338 };
339 #endif /* U_HIDE_OBSOLETE_API */
340
350 MessageFormat(const UnicodeString& pattern,
351 UErrorCode &status);
352
361 MessageFormat(const UnicodeString& pattern,
362 const Locale& newLocale,
363 UErrorCode& status);
374 MessageFormat(const UnicodeString& pattern,
375 const Locale& newLocale,
376 UParseError& parseError,
377 UErrorCode& status);
382 MessageFormat(const MessageFormat&);
383
388 const MessageFormat& operator=(const MessageFormat&);
389
394 virtual ~MessageFormat();
395
401 virtual Format* clone(void) const;
402
410 virtual UBool operator==(const Format& other) const;
411
417 virtual void setLocale(const Locale& theLocale);
418
425 virtual const Locale& getLocale(void) const;
426
435 virtual void applyPattern(const UnicodeString& pattern,
436 UErrorCode& status);
447 virtual void applyPattern(const UnicodeString& pattern,
448 UParseError& parseError,
449 UErrorCode& status);
450
469 virtual void applyPattern(const UnicodeString& pattern,
470 UMessagePatternApostropheMode aposMode,
471 UParseError* parseError,
472 UErrorCode& status);
473
478 UMessagePatternApostropheMode getApostropheMode() const {
479 return msgPattern.getApostropheMode();
480 }
481
490 virtual UnicodeString& toPattern(UnicodeString& appendTo) const;
491
508 virtual void adoptFormats(Format** formatsToAdopt, int32_t count);
509
524 virtual void setFormats(const Format** newFormats, int32_t cnt);
525
526
541 virtual void adoptFormat(int32_t formatNumber, Format* formatToAdopt);
542
552 virtual void setFormat(int32_t formatNumber, const Format& format);
553
562 virtual StringEnumeration* getFormatNames(UErrorCode& status);
563
577 virtual Format* getFormat(const UnicodeString& formatName, UErrorCode& status);
578
592 virtual void setFormat(const UnicodeString& formatName, const Format& format, UErrorCode& status);
593
608 virtual void adoptFormat(const UnicodeString& formatName, Format* formatToAdopt, UErrorCode& status);
609
622 virtual const Format** getFormats(int32_t& count) const;
623
624
625 using Format::format;
626
644 UnicodeString& format(const Formattable* source,
645 int32_t count,
646 UnicodeString& appendTo,
647 FieldPosition& ignore,
648 UErrorCode& status) const;
649
667 static UnicodeString& format(const UnicodeString& pattern,
668 const Formattable* arguments,
669 int32_t count,
670 UnicodeString& appendTo,
671 UErrorCode& status);
672
693 virtual UnicodeString& format(const Formattable& obj,
694 UnicodeString& appendTo,
695 FieldPosition& pos,
696 UErrorCode& status) const;
697
716 UnicodeString& format(const UnicodeString* argumentNames,
717 const Formattable* arguments,
718 int32_t count,
719 UnicodeString& appendTo,
720 UErrorCode& status) const;
734 virtual Formattable* parse(const UnicodeString& source,
735 ParsePosition& pos,
736 int32_t& count) const;
737
753 virtual Formattable* parse(const UnicodeString& source,
754 int32_t& count,
755 UErrorCode& status) const;
756
769 virtual void parseObject(const UnicodeString& source,
770 Formattable& result,
771 ParsePosition& pos) const;
772
792 static UnicodeString autoQuoteApostrophe(const UnicodeString& pattern,
793 UErrorCode& status);
794
795
803 UBool usesNamedArguments() const;
804
805
806 #ifndef U_HIDE_INTERNAL_API
807
817 int32_t getArgTypeCount() const;
818 #endif /* U_HIDE_INTERNAL_API */
819
831 virtual UClassID getDynamicClassID(void) const;
832
844 static UClassID U_EXPORT2 getStaticClassID(void);
845
846 #ifndef U_HIDE_INTERNAL_API
847
857 static UBool equalFormats(const void* left, const void* right);
858 #endif /* U_HIDE_INTERNAL_API */
859
860 private:
861
862 Locale fLocale;
863 MessagePattern msgPattern;
864 Format** formatAliases; // see getFormats
865 int32_t formatAliasesCapacity;
866
867 MessageFormat(); // default constructor not implemented
868
875 class U_I18N_API PluralSelectorProvider : public PluralFormat::PluralSelector {
876 public:
877 PluralSelectorProvider(const MessageFormat &mf, UPluralType type);
878 virtual ~PluralSelectorProvider();
879 virtual UnicodeString select(void *ctx, double number, UErrorCode& ec) const;
880
881 void reset();
882 private:
883 const MessageFormat &msgFormat;
884 PluralRules* rules;
885 UPluralType type;
886 };
887
896 Formattable::Type* argTypes;
897 int32_t argTypeCount;
898 int32_t argTypeCapacity;
899
905 UBool hasArgTypeConflicts;
906
907 // Variable-size array management
908 UBool allocateArgTypes(int32_t capacity, UErrorCode& status);
909
917 NumberFormat* defaultNumberFormat;
918 DateFormat* defaultDateFormat;
919
920 UHashtable* cachedFormatters;
921 UHashtable* customFormatArgStarts;
922
923 PluralSelectorProvider pluralProvider;
924 PluralSelectorProvider ordinalProvider;
925
930 const NumberFormat* getDefaultNumberFormat(UErrorCode&) const;
931 const DateFormat* getDefaultDateFormat(UErrorCode&) const;
932
939 static int32_t findKeyword( const UnicodeString& s,
940 const UChar * const *list);
941
947 UnicodeString& format(const Formattable* arguments,
948 const UnicodeString *argumentNames,
949 int32_t cnt,
950 UnicodeString& appendTo,
951 FieldPosition* pos,
952 UErrorCode& status) const;
953
971 void format(int32_t msgStart,
972 const void *plNumber,
973 const Formattable* arguments,
974 const UnicodeString *argumentNames,
975 int32_t cnt,
976 AppendableWrapper& appendTo,
977 FieldPosition* pos,
978 UErrorCode& success) const;
979
980 UnicodeString getArgName(int32_t partIndex);
981
982 void setArgStartFormat(int32_t argStart, Format* formatter, UErrorCode& status);
983
984 void setCustomArgStartFormat(int32_t argStart, Format* formatter, UErrorCode& status);
985
986 int32_t nextTopLevelArgStart(int32_t partIndex) const;
987
988 UBool argNameMatches(int32_t partIndex, const UnicodeString& argName, int32_t argNumber);
989
990 void cacheExplicitFormats(UErrorCode& status);
991
992 Format* createAppropriateFormat(UnicodeString& type,
993 UnicodeString& style,
994 Formattable::Type& formattableType,
995 UParseError& parseError,
996 UErrorCode& ec);
997
998 const Formattable* getArgFromListByName(const Formattable* arguments,
999 const UnicodeString *argumentNames,
1000 int32_t cnt, UnicodeString& name) const;
1001
1002 Formattable* parse(int32_t msgStart,
1003 const UnicodeString& source,
1004 ParsePosition& pos,
1005 int32_t& count,
1006 UErrorCode& ec) const;
1007
1008 FieldPosition* updateMetaData(AppendableWrapper& dest, int32_t prevLength,
1009 FieldPosition* fp, const Formattable* argId) const;
1010
1016 int32_t findOtherSubMessage(int32_t partIndex) const;
1017
1023 int32_t findFirstPluralNumberArg(int32_t msgStart, const UnicodeString &argName) const;
1024
1025 Format* getCachedFormatter(int32_t argumentNumber) const;
1026
1027 UnicodeString getLiteralStringUntilNextArgument(int32_t from) const;
1028
1029 void copyObjects(const MessageFormat& that, UErrorCode& ec);
1030
1031 void formatComplexSubMessage(int32_t msgStart,
1032 const void *plNumber,
1033 const Formattable* arguments,
1034 const UnicodeString *argumentNames,
1035 int32_t cnt,
1036 AppendableWrapper& appendTo,
1037 UErrorCode& success) const;
1038
1042 NumberFormat* createIntegerFormat(const Locale& locale, UErrorCode& status) const;
1043
1051 const Formattable::Type* getArgTypeList(int32_t& listCount) const {
1052 listCount = argTypeCount;
1053 return argTypes;
1054 }
1055
1059 void resetPattern();
1060
1065 class U_I18N_API DummyFormat : public Format {
1066 public:
1067 virtual UBool operator==(const Format&) const;
1068 virtual Format* clone() const;
1069 virtual UnicodeString& format(const Formattable& obj,
1070 UnicodeString& appendTo,
1071 UErrorCode& status) const;
1072 virtual UnicodeString& format(const Formattable&,
1073 UnicodeString& appendTo,
1074 FieldPosition&,
1075 UErrorCode& status) const;
1076 virtual UnicodeString& format(const Formattable& obj,
1077 UnicodeString& appendTo,
1078 FieldPositionIterator* posIter,
1079 UErrorCode& status) const;
1080 virtual void parseObject(const UnicodeString&,
1081 Formattable&,
1082 ParsePosition&) const;
1083 };
1084
1085 friend class MessageFormatAdapter; // getFormatTypeList() access
1086 };
1087
1088 U_NAMESPACE_END
1089
1090 #endif /* #if !UCONFIG_NO_FORMATTING */
1091
1092 #endif // _MSGFMT
1093 //eof
icu::Format
Base class for all formats.
Definition: format.h:94
icu::PluralFormat::copyObjects
void copyObjects(const PluralFormat &other)
Copies dynamically allocated values (pointer fields).
icu::UObject::getDynamicClassID
virtual UClassID getDynamicClassID() const
ICU4C "poor man's RTTI", returns a UClassID for the actual ICU class.
icu::Format::getLocale
Locale getLocale(ULocDataLocaleType type, UErrorCode &status) const
Get the locale for this format object.
icu::operator==
U_EXPORT UBool operator==(const StringPiece &x, const StringPiece &y)
Global operator == for StringPiece.
UClassID
void * UClassID
UClassID is used to identify classes without using the compiler's RTTI.
Definition: uobject.h:91
plurfmt.h
C++ API: PluralFormat object.
icu::StringEnumeration
Base class for 'pure' C++ implementations of uenum api.
Definition: strenum.h:55
icu::PluralFormat::format
UnicodeString format(int32_t number, UErrorCode &status) const
Formats a plural message for a given number.
plurrule.h
C++ API: PluralRules object.
icu::MessageFormat::EFormatNumber
EFormatNumber
Enum type for kMaxFormat.
Definition: msgfmt.h:331
icu::PluralRules
Defines rules for mapping non-negative numeric values onto a small set of keywords.
Definition: plurrule.h:192
format.h
C++ API: Base class for all formats.
icu::MessagePattern
Parses and represents ICU MessageFormat patterns.
Definition: messagepattern.h:358
U_CDECL_BEGIN
#define U_CDECL_BEGIN
This is used to begin a declaration of a library private ICU C API.
Definition: umachine.h:82
U_I18N_API
#define U_I18N_API
Set to export library symbols from inside the i18n library, and to import them from outside...
Definition: utypes.h:358
U_NAMESPACE_BEGIN
#define U_NAMESPACE_BEGIN
This is used to begin a declaration of a public ICU C++ API.
Definition: uversion.h:129
icu::Format::format
UnicodeString & format(const Formattable &obj, UnicodeString &appendTo, UErrorCode &status) const
Formats an object to produce a string.
icu::PluralFormat::PluralSelector
Definition: plurfmt.h:533
icu::MessageFormat::getApostropheMode
UMessagePatternApostropheMode getApostropheMode() const
Definition: msgfmt.h:478
icu::PluralFormat::PluralSelector::select
virtual UnicodeString select(void *context, double number, UErrorCode &ec) const =0
Given a number, returns the appropriate PluralFormat keyword.
icu::MessageFormat
Definition: msgfmt.h:323
UChar
uint16_t UChar
Define UChar to be UCHAR_TYPE, if that is #defined (for example, to char16_t), or wchar_t if that is ...
Definition: umachine.h:312
U_CDECL_END
#define U_CDECL_END
This is used to end a declaration of a library private ICU C API.
Definition: umachine.h:83
U_NAMESPACE_END
#define U_NAMESPACE_END
This is used to end a declaration of a public ICU C++ API.
Definition: uversion.h:130
icu::Format::parseObject
virtual void parseObject(const UnicodeString &source, Formattable &result, ParsePosition &parse_pos) const =0
Parse a string to produce an object.
parseerr.h
C API: Parse Error Information.
icu::Format::operator==
virtual UBool operator==(const Format &other) const =0
Return true if the given Format objects are semantically equal.
UErrorCode
UErrorCode
Error code to replace exception handling, so that the code is compatible with all C++ compilers...
Definition: utypes.h:476
icu::FieldPosition
FieldPosition is a simple class used by Format and its subclasses to identify fields in formatted out...
Definition: fieldpos.h:106
UPluralType
UPluralType
Type of plurals and PluralRules.
Definition: upluralrules.h:44
icu::ParsePosition
ParsePosition is a simple class used by Format and its subclasses to keep track of the current positi...
Definition: parsepos.h:47
locid.h
C++ API: Locale ID object.
UParseError
A UParseError struct is used to returned detailed information about parsing errors.
Definition: parseerr.h:56
utypes.h
Basic definitions for ICU, for both C and C++ APIs.
icu::Format::operator=
Format & operator=(const Format &)
icu::UnicodeString
UnicodeString is a string class that stores Unicode characters directly and provides similar function...
Definition: unistr.h:293
icu::Formattable
Formattable objects can be passed to the Format class or its subclasses for formatting.
Definition: fmtable.h:66
messagepattern.h
C++ API: MessagePattern class: Parses and represents ICU MessageFormat patterns.
UMessagePatternApostropheMode
UMessagePatternApostropheMode
Mode for when an apostrophe starts quoted literal text for MessageFormat output.
Definition: messagepattern.h:66
icu::Format::clone
virtual Format * clone() const =0
Clone this object polymorphically.
UBool
int8_t UBool
The ICU boolean type.
Definition: umachine.h:234
icu::Locale
A Locale object represents a specific geographical, political, or cultural region.
Definition: locid.h:185