ICU 57.1 57.1
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
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
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
793 UErrorCode& status);
794
795
804
805
806#ifndef U_HIDE_INTERNAL_API
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
857 static UBool equalFormats(const void* left, const void* right);
858#endif /* U_HIDE_INTERNAL_API */
859
860private:
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
1089
1090#endif /* #if !UCONFIG_NO_FORMATTING */
1091
1092#endif // _MSGFMT
1093//eof
DateFormat is an abstract class for a family of classes that convert dates and times from their inter...
Definition datefmt.h:147
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:66
Type
Selector for flavor of data type contained within a Formattable object.
Definition fmtable.h:223
A Locale object represents a specific geographical, political, or cultural region.
Definition locid.h:185
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:167
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:192
Base class for 'pure' C++ implementations of uenum api.
Definition strenum.h:55
UnicodeString is a string class that stores Unicode characters directly and provides similar function...
Definition unistr.h:294
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.
struct UHashtable UHashtable
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:234
#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:312
void * UClassID
UClassID is used to identify classes without using the compiler's RTTI.
Definition uobject.h:91
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