ICU 62.1 62.1
msgfmt.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* Copyright (C) 2007-2013, International Business Machines Corporation and
5* others. All Rights Reserved.
6********************************************************************************
7*
8* File MSGFMT.H
9*
10* Modification History:
11*
12* Date Name Description
13* 02/19/97 aliu Converted from java.
14* 03/20/97 helena Finished first cut of implementation.
15* 07/22/98 stephen Removed operator!= (defined in Format)
16* 08/19/2002 srl Removing Javaisms
17*******************************************************************************/
18
19#ifndef MSGFMT_H
20#define MSGFMT_H
21
22#include "unicode/utypes.h"
23
29#if !UCONFIG_NO_FORMATTING
30
31#include "unicode/format.h"
32#include "unicode/locid.h"
34#include "unicode/parseerr.h"
35#include "unicode/plurfmt.h"
36#include "unicode/plurrule.h"
37
39// Forward declaration.
40struct UHashtable;
41typedef struct UHashtable UHashtable;
43
45
46class AppendableWrapper;
47class DateFormat;
48class NumberFormat;
49
326public:
327#ifndef U_HIDE_OBSOLETE_API
339 kMaxFormat = 10
340 };
341#endif /* U_HIDE_OBSOLETE_API */
342
354
364 const Locale& newLocale,
377 const Locale& newLocale,
385
391
396 virtual ~MessageFormat();
397
403 virtual Format* clone(void) const;
404
412 virtual UBool operator==(const Format& other) const;
413
419 virtual void setLocale(const Locale& theLocale);
420
427 virtual const Locale& getLocale(void) const;
428
437 virtual void applyPattern(const UnicodeString& pattern,
449 virtual void applyPattern(const UnicodeString& pattern,
452
471 virtual void applyPattern(const UnicodeString& pattern,
475
481 return msgPattern.getApostropheMode();
482 }
483
492 virtual UnicodeString& toPattern(UnicodeString& appendTo) const;
493
510 virtual void adoptFormats(Format** formatsToAdopt, int32_t count);
511
526 virtual void setFormats(const Format** newFormats, int32_t cnt);
527
528
544
554 virtual void setFormat(int32_t formatNumber, const Format& format);
555
565
580
594 virtual void setFormat(const UnicodeString& formatName, const Format& format, UErrorCode& status);
595
611
624 virtual const Format** getFormats(int32_t& count) const;
625
626
627 using Format::format;
628
647 int32_t count,
648 UnicodeString& appendTo,
650 UErrorCode& status) const;
651
669 static UnicodeString& format(const UnicodeString& pattern,
670 const Formattable* arguments,
671 int32_t count,
672 UnicodeString& appendTo,
674
696 UnicodeString& appendTo,
697 FieldPosition& pos,
698 UErrorCode& status) const;
699
719 const Formattable* arguments,
720 int32_t count,
721 UnicodeString& appendTo,
722 UErrorCode& status) const;
736 virtual Formattable* parse(const UnicodeString& source,
737 ParsePosition& pos,
738 int32_t& count) const;
739
755 virtual Formattable* parse(const UnicodeString& source,
756 int32_t& count,
757 UErrorCode& status) const;
758
771 virtual void parseObject(const UnicodeString& source,
773 ParsePosition& pos) const;
774
796
797
806
807
808#ifndef U_HIDE_INTERNAL_API
819 int32_t getArgTypeCount() const;
820#endif /* U_HIDE_INTERNAL_API */
821
833 virtual UClassID getDynamicClassID(void) const;
834
847
848#ifndef U_HIDE_INTERNAL_API
859 static UBool equalFormats(const void* left, const void* right);
860#endif /* U_HIDE_INTERNAL_API */
861
862private:
863
864 Locale fLocale;
865 MessagePattern msgPattern;
866 Format** formatAliases; // see getFormats
867 int32_t formatAliasesCapacity;
868
869 MessageFormat(); // default constructor not implemented
870
877 class U_I18N_API PluralSelectorProvider : public PluralFormat::PluralSelector {
878 public:
879 PluralSelectorProvider(const MessageFormat &mf, UPluralType type);
880 virtual ~PluralSelectorProvider();
881 virtual UnicodeString select(void *ctx, double number, UErrorCode& ec) const;
882
883 void reset();
884 private:
885 const MessageFormat &msgFormat;
886 PluralRules* rules;
887 UPluralType type;
888 };
889
898 Formattable::Type* argTypes;
899 int32_t argTypeCount;
900 int32_t argTypeCapacity;
901
907 UBool hasArgTypeConflicts;
908
909 // Variable-size array management
910 UBool allocateArgTypes(int32_t capacity, UErrorCode& status);
911
919 NumberFormat* defaultNumberFormat;
920 DateFormat* defaultDateFormat;
921
922 UHashtable* cachedFormatters;
923 UHashtable* customFormatArgStarts;
924
925 PluralSelectorProvider pluralProvider;
926 PluralSelectorProvider ordinalProvider;
927
932 const NumberFormat* getDefaultNumberFormat(UErrorCode&) const;
933 const DateFormat* getDefaultDateFormat(UErrorCode&) const;
934
941 static int32_t findKeyword( const UnicodeString& s,
942 const char16_t * const *list);
943
951 int32_t cnt,
952 UnicodeString& appendTo,
953 FieldPosition* pos,
954 UErrorCode& status) const;
955
973 void format(int32_t msgStart,
974 const void *plNumber,
975 const Formattable* arguments,
977 int32_t cnt,
978 AppendableWrapper& appendTo,
979 FieldPosition* pos,
980 UErrorCode& success) const;
981
982 UnicodeString getArgName(int32_t partIndex);
983
984 void setArgStartFormat(int32_t argStart, Format* formatter, UErrorCode& status);
985
986 void setCustomArgStartFormat(int32_t argStart, Format* formatter, UErrorCode& status);
987
988 int32_t nextTopLevelArgStart(int32_t partIndex) const;
989
990 UBool argNameMatches(int32_t partIndex, const UnicodeString& argName, int32_t argNumber);
991
992 void cacheExplicitFormats(UErrorCode& status);
993
994 Format* createAppropriateFormat(UnicodeString& type,
998 UErrorCode& ec);
999
1000 const Formattable* getArgFromListByName(const Formattable* arguments,
1002 int32_t cnt, UnicodeString& name) const;
1003
1004 Formattable* parse(int32_t msgStart,
1005 const UnicodeString& source,
1006 ParsePosition& pos,
1007 int32_t& count,
1008 UErrorCode& ec) const;
1009
1010 FieldPosition* updateMetaData(AppendableWrapper& dest, int32_t prevLength,
1011 FieldPosition* fp, const Formattable* argId) const;
1012
1018 int32_t findOtherSubMessage(int32_t partIndex) const;
1019
1025 int32_t findFirstPluralNumberArg(int32_t msgStart, const UnicodeString &argName) const;
1026
1027 Format* getCachedFormatter(int32_t argumentNumber) const;
1028
1029 UnicodeString getLiteralStringUntilNextArgument(int32_t from) const;
1030
1031 void copyObjects(const MessageFormat& that, UErrorCode& ec);
1032
1033 void formatComplexSubMessage(int32_t msgStart,
1034 const void *plNumber,
1035 const Formattable* arguments,
1037 int32_t cnt,
1038 AppendableWrapper& appendTo,
1039 UErrorCode& success) const;
1040
1044 NumberFormat* createIntegerFormat(const Locale& locale, UErrorCode& status) const;
1045
1053 const Formattable::Type* getArgTypeList(int32_t& listCount) const {
1054 listCount = argTypeCount;
1055 return argTypes;
1056 }
1057
1061 void resetPattern();
1062
1067 class U_I18N_API DummyFormat : public Format {
1068 public:
1069 virtual UBool operator==(const Format&) const;
1070 virtual Format* clone() const;
1071 virtual UnicodeString& format(const Formattable& obj,
1072 UnicodeString& appendTo,
1073 UErrorCode& status) const;
1074 virtual UnicodeString& format(const Formattable&,
1075 UnicodeString& appendTo,
1076 FieldPosition&,
1077 UErrorCode& status) const;
1078 virtual UnicodeString& format(const Formattable& obj,
1079 UnicodeString& appendTo,
1080 FieldPositionIterator* posIter,
1081 UErrorCode& status) const;
1082 virtual void parseObject(const UnicodeString&,
1083 Formattable&,
1084 ParsePosition&) const;
1085 };
1086
1087 friend class MessageFormatAdapter; // getFormatTypeList() access
1088};
1089
1091
1092#endif /* #if !UCONFIG_NO_FORMATTING */
1093
1094#endif // _MSGFMT
1095//eof
DateFormat is an abstract class for a family of classes that convert dates and times from their inter...
Definition datefmt.h:150
FieldPosition is a simple class used by Format and its subclasses to identify fields in formatted out...
Definition fieldpos.h:108
Base class for all formats.
Definition format.h:96
virtual void parseObject(const UnicodeString &source, Formattable &result, ParsePosition &parse_pos) const =0
Parse a string to produce an object.
UnicodeString & format(const Formattable &obj, UnicodeString &appendTo, UErrorCode &status) const
Formats an object to produce a string.
virtual UBool operator==(const Format &other) const =0
Return true if the given Format objects are semantically equal.
virtual Format * clone() const =0
Clone this object polymorphically.
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
"Smart pointer" base class; do not use directly: use LocalPointer etc.
A Locale object represents a specific geographical, political, or cultural region.
Definition locid.h:188
virtual Formattable * parse(const UnicodeString &source, ParsePosition &pos, int32_t &count) const
Parses the given string into an array of output arguments.
virtual UnicodeString & toPattern(UnicodeString &appendTo) const
Returns a pattern that can be used to recreate this object.
virtual Format * clone(void) const
Clones this Format object polymorphically.
virtual void applyPattern(const UnicodeString &pattern, UMessagePatternApostropheMode aposMode, UParseError *parseError, UErrorCode &status)
Sets the UMessagePatternApostropheMode and the pattern used by this message format.
virtual Formattable * parse(const UnicodeString &source, int32_t &count, UErrorCode &status) const
Parses the given string into an array of output arguments.
virtual UClassID getDynamicClassID(void) const
Returns a unique class ID POLYMORPHICALLY.
virtual UBool operator==(const Format &other) const
Returns true if the given Format objects are semantically equal.
static UClassID getStaticClassID(void)
Return the class ID for this class.
virtual void adoptFormat(const UnicodeString &formatName, Format *formatToAdopt, UErrorCode &status)
Sets one subformat for given format name.
MessageFormat(const UnicodeString &pattern, UErrorCode &status)
Constructs a new MessageFormat using the given pattern and the default locale.
MessageFormat(const UnicodeString &pattern, const Locale &newLocale, UParseError &parseError, UErrorCode &status)
Constructs a new MessageFormat using the given pattern and locale.
static UBool equalFormats(const void *left, const void *right)
Compares two Format objects.
virtual void setFormats(const Format **newFormats, int32_t cnt)
Sets subformats.
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.
UMessagePatternApostropheMode getApostropheMode() const
Definition msgfmt.h:480
int32_t getArgTypeCount() const
This API is for ICU internal use only.
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 ...
const MessageFormat & operator=(const MessageFormat &)
Assignment operator.
MessageFormat(const MessageFormat &)
Constructs a new MessageFormat from an existing one.
EFormatNumber
Enum type for kMaxFormat.
Definition msgfmt.h:333
virtual void setLocale(const Locale &theLocale)
Sets the locale to be used for creating argument Format objects.
virtual const Locale & getLocale(void) const
Gets the locale used for creating argument Format objects.
virtual ~MessageFormat()
Destructor.
UBool usesNamedArguments() const
Returns true if this MessageFormat uses named arguments, and false otherwise.
virtual void setFormat(const UnicodeString &formatName, const Format &format, UErrorCode &status)
Sets one subformat for given format name.
virtual const Format ** getFormats(int32_t &count) const
Gets an array of subformats of this object.
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 UnicodeString & format(const Formattable &obj, UnicodeString &appendTo, FieldPosition &pos, UErrorCode &status) const
Formats the given array of arguments into a user-readable string.
virtual void adoptFormat(int32_t formatNumber, Format *formatToAdopt)
Sets one subformat.
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 Format * getFormat(const UnicodeString &formatName, UErrorCode &status)
Gets subformat pointer for given format name.
MessageFormat(const UnicodeString &pattern, const Locale &newLocale, UErrorCode &status)
Constructs a new MessageFormat using the given pattern and locale.
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.
virtual void applyPattern(const UnicodeString &pattern, UParseError &parseError, UErrorCode &status)
Applies the given pattern string to this message format.
virtual void adoptFormats(Format **formatsToAdopt, int32_t count)
Sets subformats.
virtual void applyPattern(const UnicodeString &pattern, UErrorCode &status)
Applies the given pattern string to this message format.
virtual StringEnumeration * getFormatNames(UErrorCode &status)
Gets format names.
Parses and represents ICU MessageFormat patterns.
ParsePosition is a simple class used by Format and its subclasses to keep track of the current positi...
Definition parsepos.h:49
Defines rules for mapping non-negative numeric values onto a small set of keywords.
Definition plurrule.h:196
Base class for 'pure' C++ implementations of uenum api.
Definition strenum.h:57
UnicodeString is a string class that stores Unicode characters directly and provides similar function...
Definition unistr.h:287
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:58
#define U_CDECL_END
This is used to end a declaration of a library private ICU C API.
Definition umachine.h:85
int8_t UBool
The ICU boolean type.
Definition umachine.h:236
#define U_CDECL_BEGIN
This is used to begin a declaration of a library private ICU C API.
Definition umachine.h:84
void * UClassID
UClassID is used to identify classes without using the compiler's RTTI.
Definition uobject.h:93
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:396
#define U_I18N_API
Set to export library symbols from inside the i18n library, and to import them from outside.
Definition utypes.h:360
#define U_NAMESPACE_END
This is used to end a declaration of a public ICU C++ API.
Definition uversion.h:138
#define U_NAMESPACE_BEGIN
This is used to begin a declaration of a public ICU C++ API.
Definition uversion.h:137