ICU 50.2 50.2
msgfmt.h
Go to the documentation of this file.
1/*
2* Copyright (C) 2007-2012, 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
26
27#if !UCONFIG_NO_FORMATTING
28
29#include "unicode/format.h"
30#include "unicode/locid.h"
32#include "unicode/parseerr.h"
33#include "unicode/plurfmt.h"
34#include "unicode/plurrule.h"
35
37// Forward declaration.
38struct UHashtable;
39typedef struct UHashtable UHashtable;
41
43
44class AppendableWrapper;
45class DateFormat;
46class NumberFormat;
47
324public:
325#ifndef U_HIDE_OBSOLETE_API
338 };
339#endif /* U_HIDE_OBSOLETE_API */
340
351 UErrorCode &status);
352
362 const Locale& newLocale,
363 UErrorCode& status);
375 const Locale& newLocale,
376 UParseError& parseError,
377 UErrorCode& status);
383
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,
471 UParseError* parseError,
472 UErrorCode& status);
473
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
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
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
712 UnicodeString& format(const Formattable& obj,
713 UnicodeString& appendTo,
714 UErrorCode& status) const;
715
716
735 UnicodeString& format(const UnicodeString* argumentNames,
736 const Formattable* arguments,
737 int32_t count,
738 UnicodeString& appendTo,
739 UErrorCode& status) const;
753 virtual Formattable* parse(const UnicodeString& source,
754 ParsePosition& pos,
755 int32_t& count) const;
756
772 virtual Formattable* parse(const UnicodeString& source,
773 int32_t& count,
774 UErrorCode& status) const;
775
788 virtual void parseObject(const UnicodeString& source,
789 Formattable& result,
790 ParsePosition& pos) const;
791
812 UErrorCode& status);
813
814
823
824
825#ifndef U_HIDE_INTERNAL_API
836 int32_t getArgTypeCount() const;
837#endif /* U_HIDE_INTERNAL_API */
838
850 virtual UClassID getDynamicClassID(void) const;
851
863 static UClassID U_EXPORT2 getStaticClassID(void);
864
865#ifndef U_HIDE_INTERNAL_API
876 static UBool equalFormats(const void* left, const void* right);
877#endif /* U_HIDE_INTERNAL_API */
878
879private:
880
881 Locale fLocale;
882 MessagePattern msgPattern;
883 Format** formatAliases; // see getFormats
884 int32_t formatAliasesCapacity;
885
886 MessageFormat(); // default constructor not implemented
887
894 class U_I18N_API PluralSelectorProvider : public PluralFormat::PluralSelector {
895 public:
896 PluralSelectorProvider(const Locale* loc, UPluralType type);
897 virtual ~PluralSelectorProvider();
898 virtual UnicodeString select(double number, UErrorCode& ec) const;
899
900 void reset(const Locale* loc);
901 private:
902 const Locale* locale;
903 PluralRules* rules;
904 UPluralType type;
905 };
906
915 Formattable::Type* argTypes;
916 int32_t argTypeCount;
917 int32_t argTypeCapacity;
918
924 UBool hasArgTypeConflicts;
925
926 // Variable-size array management
927 UBool allocateArgTypes(int32_t capacity, UErrorCode& status);
928
936 NumberFormat* defaultNumberFormat;
937 DateFormat* defaultDateFormat;
938
939 UHashtable* cachedFormatters;
940 UHashtable* customFormatArgStarts;
941
942 PluralSelectorProvider pluralProvider;
943 PluralSelectorProvider ordinalProvider;
944
949 const NumberFormat* getDefaultNumberFormat(UErrorCode&) const;
950 const DateFormat* getDefaultDateFormat(UErrorCode&) const;
951
958 static int32_t findKeyword( const UnicodeString& s,
959 const UChar * const *list);
960
966 UnicodeString& format(const Formattable* arguments,
967 const UnicodeString *argumentNames,
968 int32_t cnt,
969 UnicodeString& appendTo,
970 FieldPosition* pos,
971 UErrorCode& status) const;
972
990 void format(int32_t msgStart,
991 double pluralNumber,
992 const Formattable* arguments,
993 const UnicodeString *argumentNames,
994 int32_t cnt,
995 AppendableWrapper& appendTo,
996 FieldPosition* pos,
997 UErrorCode& success) const;
998
999 UnicodeString getArgName(int32_t partIndex);
1000
1001 void setArgStartFormat(int32_t argStart, Format* formatter, UErrorCode& status);
1002
1003 void setCustomArgStartFormat(int32_t argStart, Format* formatter, UErrorCode& status);
1004
1005 int32_t nextTopLevelArgStart(int32_t partIndex) const;
1006
1007 UBool argNameMatches(int32_t partIndex, const UnicodeString& argName, int32_t argNumber);
1008
1009 void cacheExplicitFormats(UErrorCode& status);
1010
1011 Format* createAppropriateFormat(UnicodeString& type,
1012 UnicodeString& style,
1013 Formattable::Type& formattableType,
1014 UParseError& parseError,
1015 UErrorCode& ec);
1016
1017 const Formattable* getArgFromListByName(const Formattable* arguments,
1018 const UnicodeString *argumentNames,
1019 int32_t cnt, UnicodeString& name) const;
1020
1021 Formattable* parse(int32_t msgStart,
1022 const UnicodeString& source,
1023 ParsePosition& pos,
1024 int32_t& count,
1025 UErrorCode& ec) const;
1026
1027 FieldPosition* updateMetaData(AppendableWrapper& dest, int32_t prevLength,
1028 FieldPosition* fp, const Formattable* argId) const;
1029
1030 Format* getCachedFormatter(int32_t argumentNumber) const;
1031
1032 UnicodeString getLiteralStringUntilNextArgument(int32_t from) const;
1033
1034 void copyObjects(const MessageFormat& that, UErrorCode& ec);
1035
1036 void formatComplexSubMessage(int32_t msgStart,
1037 double pluralNumber,
1038 const Formattable* arguments,
1039 const UnicodeString *argumentNames,
1040 int32_t cnt,
1041 AppendableWrapper& appendTo,
1042 UErrorCode& success) const;
1043
1047 NumberFormat* createIntegerFormat(const Locale& locale, UErrorCode& status) const;
1048
1057 const Formattable::Type* getArgTypeList(int32_t& listCount) const {
1058 listCount = argTypeCount;
1059 return argTypes;
1060 }
1061
1065 void resetPattern();
1066
1072 class U_I18N_API DummyFormat : public Format {
1073 public:
1074 virtual UBool operator==(const Format&) const;
1075 virtual Format* clone() const;
1076 virtual UnicodeString& format(const Formattable& obj,
1077 UnicodeString& appendTo,
1078 UErrorCode& status) const;
1079 virtual UnicodeString& format(const Formattable&,
1080 UnicodeString& appendTo,
1081 FieldPosition&,
1082 UErrorCode& status) const;
1083 virtual UnicodeString& format(const Formattable& obj,
1084 UnicodeString& appendTo,
1085 FieldPositionIterator* posIter,
1086 UErrorCode& status) const;
1087 virtual void parseObject(const UnicodeString&,
1088 Formattable&,
1089 ParsePosition&) const;
1090 virtual UClassID getDynamicClassID() const;
1091 };
1092
1093 friend class MessageFormatAdapter; // getFormatTypeList() access
1094};
1095
1096inline UnicodeString&
1098 UnicodeString& appendTo,
1099 UErrorCode& status) const {
1100 return Format::format(obj, appendTo, status);
1101}
1102
1103
1105
1106#endif /* #if !UCONFIG_NO_FORMATTING */
1107
1108#endif // _MSGFMT
1109//eof
DateFormat is an abstract class for a family of classes that convert dates and times from their inter...
Definition datefmt.h:138
FieldPosition is a simple class used by Format and its subclasses to identify fields in formatted out...
Definition fieldpos.h:106
Base class for all formats.
Definition format.h:94
UnicodeString & format(const Formattable &obj, UnicodeString &appendTo, UErrorCode &status) const
Formats an object to produce a string.
Format()
Default constructor for subclass use only.
virtual void parseObject(const UnicodeString &source, Formattable &result, ParsePosition &parse_pos) const =0
Parse a string to produce an object.
virtual Format * clone() const =0
Clone this object polymorphically.
virtual UBool operator==(const Format &other) const =0
Return true if the given Format objects are semantically equal.
Formattable objects can be passed to the Format class or its subclasses for formatting.
Definition fmtable.h:62
Type
Selector for flavor of data type contained within a Formattable object.
Definition fmtable.h:219
A Locale object represents a specific geographical, political, or cultural region.
Definition locid.h:182
MessageFormat(const UnicodeString &pattern, const Locale &newLocale, UErrorCode &status)
Constructs a new MessageFormat using the given pattern and locale.
static UnicodeString autoQuoteApostrophe(const UnicodeString &pattern, UErrorCode &status)
Convert an 'apostrophe-friendly' pattern into a standard pattern.
virtual void setFormat(int32_t formatNumber, const Format &format)
Sets one subformat.
virtual UBool operator==(const Format &other) const
Returns true if the given Format objects are semantically equal.
UnicodeString & format(const Formattable *source, int32_t count, UnicodeString &appendTo, FieldPosition &ignore, UErrorCode &status) const
Formats the given array of arguments into a user-readable string.
virtual const Format ** getFormats(int32_t &count) const
Gets an array of subformats of this object.
virtual Format * getFormat(const UnicodeString &formatName, UErrorCode &status)
Gets subformat pointer for given format name.
virtual ~MessageFormat()
Destructor.
UBool usesNamedArguments() const
Returns true if this MessageFormat uses named arguments, and false otherwise.
static UBool equalFormats(const void *left, const void *right)
Compares two Format objects.
EFormatNumber
Enum type for kMaxFormat.
Definition msgfmt.h:331
@ kMaxFormat
The maximum number of arguments.
Definition msgfmt.h:337
virtual void adoptFormats(Format **formatsToAdopt, int32_t count)
Sets subformats.
virtual Formattable * parse(const UnicodeString &source, int32_t &count, UErrorCode &status) const
Parses the given string into an array of output arguments.
virtual void setLocale(const Locale &theLocale)
Sets the locale to be used for creating argument Format objects.
static UnicodeString & format(const UnicodeString &pattern, const Formattable *arguments, int32_t count, UnicodeString &appendTo, UErrorCode &status)
Formats the given array of arguments into a user-readable string using the given pattern.
virtual void applyPattern(const UnicodeString &pattern, UMessagePatternApostropheMode aposMode, UParseError *parseError, UErrorCode &status)
Sets the UMessagePatternApostropheMode and the pattern used by this message format.
virtual const Locale & getLocale(void) const
Gets the locale used for creating argument Format objects.
virtual UnicodeString & format(const Formattable &obj, UnicodeString &appendTo, FieldPosition &pos, UErrorCode &status) const
Formats the given array of arguments into a user-readable string.
virtual Formattable * parse(const UnicodeString &source, ParsePosition &pos, int32_t &count) const
Parses the given string into an array of output arguments.
virtual void setFormat(const UnicodeString &formatName, const Format &format, UErrorCode &status)
Sets one subformat for given format name.
virtual void setFormats(const Format **newFormats, int32_t cnt)
Sets subformats.
virtual void parseObject(const UnicodeString &source, Formattable &result, ParsePosition &pos) const
Parses the given string into an array of output arguments stored within a single Formattable of type ...
MessageFormat(const MessageFormat &)
Constructs a new MessageFormat from an existing one.
virtual void adoptFormat(int32_t formatNumber, Format *formatToAdopt)
Sets one subformat.
virtual UnicodeString & toPattern(UnicodeString &appendTo) const
Returns a pattern that can be used to recreate this object.
MessageFormat(const UnicodeString &pattern, UErrorCode &status)
Constructs a new MessageFormat using the given pattern and the default locale.
virtual StringEnumeration * getFormatNames(UErrorCode &status)
Gets format names.
static UClassID getStaticClassID(void)
Return the class ID for this class.
const MessageFormat & operator=(const MessageFormat &)
Assignment operator.
virtual void adoptFormat(const UnicodeString &formatName, Format *formatToAdopt, UErrorCode &status)
Sets one subformat for given format name.
virtual UClassID getDynamicClassID(void) const
Returns a unique class ID POLYMORPHICALLY.
virtual void applyPattern(const UnicodeString &pattern, UErrorCode &status)
Applies the given pattern string to this message format.
UMessagePatternApostropheMode getApostropheMode() const
Definition msgfmt.h:478
virtual void applyPattern(const UnicodeString &pattern, UParseError &parseError, UErrorCode &status)
Applies the given pattern string to this message format.
MessageFormat(const UnicodeString &pattern, const Locale &newLocale, UParseError &parseError, UErrorCode &status)
Constructs a new MessageFormat using the given pattern and locale.
int32_t getArgTypeCount() const
This API is for ICU internal use only.
virtual Format * clone(void) const
Clones this Format object polymorphically.
UnicodeString & format(const UnicodeString *argumentNames, const Formattable *arguments, int32_t count, UnicodeString &appendTo, UErrorCode &status) const
Formats the given array of arguments into a user-defined argument name array.
Parses and represents ICU MessageFormat patterns.
Abstract base class for all number formats.
Definition numfmt.h:166
ParsePosition is a simple class used by Format and its subclasses to keep track of the current positi...
Definition parsepos.h:47
Defines rules for mapping non-negative numeric values onto a small set of keywords.
Definition plurrule.h:127
Base class for 'pure' C++ implementations of uenum api.
Definition strenum.h:55
virtual UClassID getDynamicClassID() const =0
ICU4C "poor man's RTTI", returns a UClassID for the actual ICU class.
UnicodeString is a string class that stores Unicode characters directly and provides similar function...
Definition unistr.h:247
C++ API: Base class for all formats.
C++ API: Locale ID object.
C++ API: MessagePattern class: Parses and represents ICU MessageFormat patterns.
UMessagePatternApostropheMode
Mode for when an apostrophe starts quoted literal text for MessageFormat output.
C API: Parse Error Information.
C++ API: PluralFormat object.
C++ API: PluralRules object.
A UParseError struct is used to returned detailed information about parsing errors.
Definition parseerr.h:56
#define U_CDECL_END
This is used to end a declaration of a library private ICU C API.
Definition umachine.h:83
int8_t UBool
The ICU boolean type.
Definition umachine.h:200
#define U_CDECL_BEGIN
This is used to begin a declaration of a library private ICU C API.
Definition umachine.h:82
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:278
void * UClassID
UClassID is used to identify classes without using the compiler's RTTI.
Definition uobject.h:96
UPluralType
Type of plurals and PluralRules.
Basic definitions for ICU, for both C and C++ APIs.
UErrorCode
Error code to replace exception handling, so that the code is compatible with all C++ compilers,...
Definition utypes.h:476
#define U_I18N_API
Set to export library symbols from inside the i18n library, and to import them from outside.
Definition utypes.h:358
#define U_NAMESPACE_END
This is used to end a declaration of a public ICU C++ API.
Definition uversion.h:130
#define U_NAMESPACE_BEGIN
This is used to begin a declaration of a public ICU C++ API.
Definition uversion.h:129