ICU 74.1 74.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
24 #if U_SHOW_CPLUSPLUS_API
25
31 #if !UCONFIG_NO_FORMATTING
32
33 #include "unicode/format.h"
34 #include "unicode/locid.h"
35 #include "unicode/messagepattern.h"
36 #include "unicode/parseerr.h"
37 #include "unicode/plurfmt.h"
38 #include "unicode/plurrule.h"
39
40 U_CDECL_BEGIN
41 // Forward declaration.
42 struct UHashtable;
43 typedef struct UHashtable UHashtable;
44 U_CDECL_END
45
46 U_NAMESPACE_BEGIN
47
48 class AppendableWrapper;
49 class DateFormat;
50 class NumberFormat;
51
345 class U_I18N_API MessageFormat : public Format {
346 public:
347 #ifndef U_HIDE_OBSOLETE_API
353 enum EFormatNumber {
359 kMaxFormat = 10
360 };
361 #endif /* U_HIDE_OBSOLETE_API */
362
372 MessageFormat(const UnicodeString& pattern,
373 UErrorCode &status);
374
383 MessageFormat(const UnicodeString& pattern,
384 const Locale& newLocale,
385 UErrorCode& status);
396 MessageFormat(const UnicodeString& pattern,
397 const Locale& newLocale,
398 UParseError& parseError,
399 UErrorCode& status);
404 MessageFormat(const MessageFormat&);
405
410 const MessageFormat& operator=(const MessageFormat&);
411
416 virtual ~MessageFormat();
417
423 virtual MessageFormat* clone() const override;
424
432 virtual bool operator==(const Format& other) const override;
433
439 virtual void setLocale(const Locale& theLocale);
440
447 virtual const Locale& getLocale(void) const;
448
457 virtual void applyPattern(const UnicodeString& pattern,
458 UErrorCode& status);
469 virtual void applyPattern(const UnicodeString& pattern,
470 UParseError& parseError,
471 UErrorCode& status);
472
491 virtual void applyPattern(const UnicodeString& pattern,
492 UMessagePatternApostropheMode aposMode,
493 UParseError* parseError,
494 UErrorCode& status);
495
500 UMessagePatternApostropheMode getApostropheMode() const {
501 return msgPattern.getApostropheMode();
502 }
503
512 virtual UnicodeString& toPattern(UnicodeString& appendTo) const;
513
530 virtual void adoptFormats(Format** formatsToAdopt, int32_t count);
531
546 virtual void setFormats(const Format** newFormats, int32_t cnt);
547
548
563 virtual void adoptFormat(int32_t formatNumber, Format* formatToAdopt);
564
574 virtual void setFormat(int32_t formatNumber, const Format& format);
575
584 virtual StringEnumeration* getFormatNames(UErrorCode& status);
585
599 virtual Format* getFormat(const UnicodeString& formatName, UErrorCode& status);
600
614 virtual void setFormat(const UnicodeString& formatName, const Format& format, UErrorCode& status);
615
630 virtual void adoptFormat(const UnicodeString& formatName, Format* formatToAdopt, UErrorCode& status);
631
644 virtual const Format** getFormats(int32_t& count) const;
645
646
647 using Format::format;
648
666 UnicodeString& format(const Formattable* source,
667 int32_t count,
668 UnicodeString& appendTo,
669 FieldPosition& ignore,
670 UErrorCode& status) const;
671
689 static UnicodeString& format(const UnicodeString& pattern,
690 const Formattable* arguments,
691 int32_t count,
692 UnicodeString& appendTo,
693 UErrorCode& status);
694
715 virtual UnicodeString& format(const Formattable& obj,
716 UnicodeString& appendTo,
717 FieldPosition& pos,
718 UErrorCode& status) const override;
719
738 UnicodeString& format(const UnicodeString* argumentNames,
739 const Formattable* arguments,
740 int32_t count,
741 UnicodeString& appendTo,
742 UErrorCode& status) const;
756 virtual Formattable* parse(const UnicodeString& source,
757 ParsePosition& pos,
758 int32_t& count) const;
759
775 virtual Formattable* parse(const UnicodeString& source,
776 int32_t& count,
777 UErrorCode& status) const;
778
791 virtual void parseObject(const UnicodeString& source,
792 Formattable& result,
793 ParsePosition& pos) const override;
794
814 static UnicodeString autoQuoteApostrophe(const UnicodeString& pattern,
815 UErrorCode& status);
816
817
825 UBool usesNamedArguments() const;
826
827
828 #ifndef U_HIDE_INTERNAL_API
839 int32_t getArgTypeCount() const;
840 #endif /* U_HIDE_INTERNAL_API */
841
853 virtual UClassID getDynamicClassID(void) const override;
854
866 static UClassID U_EXPORT2 getStaticClassID(void);
867
868 #ifndef U_HIDE_INTERNAL_API
879 static UBool equalFormats(const void* left, const void* right);
880 #endif /* U_HIDE_INTERNAL_API */
881
882 private:
883
884 Locale fLocale;
885 MessagePattern msgPattern;
886 Format** formatAliases; // see getFormats
887 int32_t formatAliasesCapacity;
888
889 MessageFormat() = delete; // default constructor not implemented
890
897 class U_I18N_API PluralSelectorProvider : public PluralFormat::PluralSelector {
898 public:
899 PluralSelectorProvider(const MessageFormat &mf, UPluralType type);
900 virtual ~PluralSelectorProvider();
901 virtual UnicodeString select(void *ctx, double number, UErrorCode& ec) const override;
902
903 void reset();
904 private:
905 const MessageFormat &msgFormat;
906 PluralRules* rules;
907 UPluralType type;
908 };
909
918 Formattable::Type* argTypes;
919 int32_t argTypeCount;
920 int32_t argTypeCapacity;
921
927 UBool hasArgTypeConflicts;
928
929 // Variable-size array management
930 UBool allocateArgTypes(int32_t capacity, UErrorCode& status);
931
939 NumberFormat* defaultNumberFormat;
940 DateFormat* defaultDateFormat;
941
942 UHashtable* cachedFormatters;
943 UHashtable* customFormatArgStarts;
944
945 PluralSelectorProvider pluralProvider;
946 PluralSelectorProvider ordinalProvider;
947
952 const NumberFormat* getDefaultNumberFormat(UErrorCode&) const;
953 const DateFormat* getDefaultDateFormat(UErrorCode&) const;
954
961 static int32_t findKeyword( const UnicodeString& s,
962 const char16_t * const *list);
963
969 UnicodeString& format(const Formattable* arguments,
970 const UnicodeString *argumentNames,
971 int32_t cnt,
972 UnicodeString& appendTo,
973 FieldPosition* pos,
974 UErrorCode& status) const;
975
993 void format(int32_t msgStart,
994 const void *plNumber,
995 const Formattable* arguments,
996 const UnicodeString *argumentNames,
997 int32_t cnt,
998 AppendableWrapper& appendTo,
999 FieldPosition* pos,
1000 UErrorCode& success) const;
1001
1002 UnicodeString getArgName(int32_t partIndex);
1003
1004 void setArgStartFormat(int32_t argStart, Format* formatter, UErrorCode& status);
1005
1006 void setCustomArgStartFormat(int32_t argStart, Format* formatter, UErrorCode& status);
1007
1008 int32_t nextTopLevelArgStart(int32_t partIndex) const;
1009
1010 UBool argNameMatches(int32_t partIndex, const UnicodeString& argName, int32_t argNumber);
1011
1012 void cacheExplicitFormats(UErrorCode& status);
1013
1014 Format* createAppropriateFormat(UnicodeString& type,
1015 UnicodeString& style,
1016 Formattable::Type& formattableType,
1017 UParseError& parseError,
1018 UErrorCode& ec);
1019
1020 const Formattable* getArgFromListByName(const Formattable* arguments,
1021 const UnicodeString *argumentNames,
1022 int32_t cnt, UnicodeString& name) const;
1023
1024 Formattable* parse(int32_t msgStart,
1025 const UnicodeString& source,
1026 ParsePosition& pos,
1027 int32_t& count,
1028 UErrorCode& ec) const;
1029
1030 FieldPosition* updateMetaData(AppendableWrapper& dest, int32_t prevLength,
1031 FieldPosition* fp, const Formattable* argId) const;
1032
1038 int32_t findOtherSubMessage(int32_t partIndex) const;
1039
1045 int32_t findFirstPluralNumberArg(int32_t msgStart, const UnicodeString &argName) const;
1046
1047 Format* getCachedFormatter(int32_t argumentNumber) const;
1048
1049 UnicodeString getLiteralStringUntilNextArgument(int32_t from) const;
1050
1051 void copyObjects(const MessageFormat& that, UErrorCode& ec);
1052
1053 void formatComplexSubMessage(int32_t msgStart,
1054 const void *plNumber,
1055 const Formattable* arguments,
1056 const UnicodeString *argumentNames,
1057 int32_t cnt,
1058 AppendableWrapper& appendTo,
1059 UErrorCode& success) const;
1060
1064 NumberFormat* createIntegerFormat(const Locale& locale, UErrorCode& status) const;
1065
1073 const Formattable::Type* getArgTypeList(int32_t& listCount) const {
1074 listCount = argTypeCount;
1075 return argTypes;
1076 }
1077
1081 void resetPattern();
1082
1087 class U_I18N_API DummyFormat : public Format {
1088 public:
1089 virtual bool operator==(const Format&) const override;
1090 virtual DummyFormat* clone() const override;
1091 virtual UnicodeString& format(const Formattable& obj,
1092 UnicodeString& appendTo,
1093 UErrorCode& status) const;
1094 virtual UnicodeString& format(const Formattable&,
1095 UnicodeString& appendTo,
1096 FieldPosition&,
1097 UErrorCode& status) const override;
1098 virtual UnicodeString& format(const Formattable& obj,
1099 UnicodeString& appendTo,
1100 FieldPositionIterator* posIter,
1101 UErrorCode& status) const override;
1102 virtual void parseObject(const UnicodeString&,
1103 Formattable&,
1104 ParsePosition&) const override;
1105 };
1106
1107 friend class MessageFormatAdapter; // getFormatTypeList() access
1108 };
1109
1110 U_NAMESPACE_END
1111
1112 #endif /* #if !UCONFIG_NO_FORMATTING */
1113
1114 #endif /* U_SHOW_CPLUSPLUS_API */
1115
1116 #endif // _MSGFMT
1117 //eof
icu::DateFormat
DateFormat is an abstract class for a family of classes that convert dates and times from their inter...
Definition: datefmt.h:156
icu::FieldPosition
FieldPosition is a simple class used by Format and its subclasses to identify fields in formatted out...
Definition: fieldpos.h:110
icu::Format
Base class for all formats.
Definition: format.h:98
icu::Format::parseObject
virtual void parseObject(const UnicodeString &source, Formattable &result, ParsePosition &parse_pos) const =0
Parse a string to produce an object.
icu::Format::clone
virtual Format * clone() const =0
Clone this object polymorphically.
icu::Format::operator==
virtual bool operator==(const Format &other) const =0
Return true if the given Format objects are semantically equal.
icu::Format::format
UnicodeString & format(const Formattable &obj, UnicodeString &appendTo, UErrorCode &status) const
Formats an object to produce a string.
icu::Formattable
Formattable objects can be passed to the Format class or its subclasses for formatting.
Definition: fmtable.h:64
icu::Formattable::Type
Type
Selector for flavor of data type contained within a Formattable object.
Definition: fmtable.h:221
icu::Locale
A Locale object represents a specific geographical, political, or cultural region.
Definition: locid.h:195
icu::MessageFormat
Definition: msgfmt.h:345
icu::MessageFormat::applyPattern
virtual void applyPattern(const UnicodeString &pattern, UMessagePatternApostropheMode aposMode, UParseError *parseError, UErrorCode &status)
Sets the UMessagePatternApostropheMode and the pattern used by this message format.
icu::MessageFormat::format
virtual UnicodeString & format(const Formattable &obj, UnicodeString &appendTo, FieldPosition &pos, UErrorCode &status) const override
Formats the given array of arguments into a user-readable string.
icu::MessageFormat::format
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.
icu::MessageFormat::getFormat
virtual Format * getFormat(const UnicodeString &formatName, UErrorCode &status)
Gets subformat pointer for given format name.
icu::MessageFormat::format
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.
icu::MessageFormat::parse
virtual Formattable * parse(const UnicodeString &source, ParsePosition &pos, int32_t &count) const
Parses the given string into an array of output arguments.
icu::MessageFormat::getDynamicClassID
virtual UClassID getDynamicClassID(void) const override
Returns a unique class ID POLYMORPHICALLY.
icu::MessageFormat::getStaticClassID
static UClassID getStaticClassID(void)
Return the class ID for this class.
icu::MessageFormat::adoptFormat
virtual void adoptFormat(const UnicodeString &formatName, Format *formatToAdopt, UErrorCode &status)
Sets one subformat for given format name.
icu::MessageFormat::MessageFormat
MessageFormat(const UnicodeString &pattern, UErrorCode &status)
Constructs a new MessageFormat using the given pattern and the default locale.
icu::MessageFormat::MessageFormat
MessageFormat(const UnicodeString &pattern, const Locale &newLocale, UParseError &parseError, UErrorCode &status)
Constructs a new MessageFormat using the given pattern and locale.
icu::MessageFormat::equalFormats
static UBool equalFormats(const void *left, const void *right)
Compares two Format objects.
icu::MessageFormat::setFormats
virtual void setFormats(const Format **newFormats, int32_t cnt)
Sets subformats.
icu::MessageFormat::autoQuoteApostrophe
static UnicodeString autoQuoteApostrophe(const UnicodeString &pattern, UErrorCode &status)
Convert an 'apostrophe-friendly' pattern into a standard pattern.
icu::MessageFormat::setFormat
virtual void setFormat(int32_t formatNumber, const Format &format)
Sets one subformat.
icu::MessageFormat::getApostropheMode
UMessagePatternApostropheMode getApostropheMode() const
Definition: msgfmt.h:500
icu::MessageFormat::getArgTypeCount
int32_t getArgTypeCount() const
This API is for ICU internal use only.
icu::MessageFormat::getLocale
virtual const Locale & getLocale(void) const
Gets the locale used for creating argument Format objects.
icu::MessageFormat::getFormatNames
virtual StringEnumeration * getFormatNames(UErrorCode &status)
Gets format names.
icu::MessageFormat::parse
virtual Formattable * parse(const UnicodeString &source, int32_t &count, UErrorCode &status) const
Parses the given string into an array of output arguments.
icu::MessageFormat::MessageFormat
MessageFormat(const MessageFormat &)
Constructs a new MessageFormat from an existing one.
icu::MessageFormat::EFormatNumber
EFormatNumber
Enum type for kMaxFormat.
Definition: msgfmt.h:353
icu::MessageFormat::parseObject
virtual void parseObject(const UnicodeString &source, Formattable &result, ParsePosition &pos) const override
Parses the given string into an array of output arguments stored within a single Formattable of type ...
icu::MessageFormat::setLocale
virtual void setLocale(const Locale &theLocale)
Sets the locale to be used for creating argument Format objects.
icu::MessageFormat::toPattern
virtual UnicodeString & toPattern(UnicodeString &appendTo) const
Returns a pattern that can be used to recreate this object.
icu::MessageFormat::clone
virtual MessageFormat * clone() const override
Clones this Format object polymorphically.
icu::MessageFormat::~MessageFormat
virtual ~MessageFormat()
Destructor.
icu::MessageFormat::usesNamedArguments
UBool usesNamedArguments() const
Returns true if this MessageFormat uses named arguments, and false otherwise.
icu::MessageFormat::operator=
const MessageFormat & operator=(const MessageFormat &)
Assignment operator.
icu::MessageFormat::getFormats
virtual const Format ** getFormats(int32_t &count) const
Gets an array of subformats of this object.
icu::MessageFormat::setFormat
virtual void setFormat(const UnicodeString &formatName, const Format &format, UErrorCode &status)
Sets one subformat for given format name.
icu::MessageFormat::operator==
virtual bool operator==(const Format &other) const override
Returns true if the given Format objects are semantically equal.
icu::MessageFormat::format
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.
icu::MessageFormat::adoptFormat
virtual void adoptFormat(int32_t formatNumber, Format *formatToAdopt)
Sets one subformat.
icu::MessageFormat::MessageFormat
MessageFormat(const UnicodeString &pattern, const Locale &newLocale, UErrorCode &status)
Constructs a new MessageFormat using the given pattern and locale.
icu::MessageFormat::applyPattern
virtual void applyPattern(const UnicodeString &pattern, UParseError &parseError, UErrorCode &status)
Applies the given pattern string to this message format.
icu::MessageFormat::adoptFormats
virtual void adoptFormats(Format **formatsToAdopt, int32_t count)
Sets subformats.
icu::MessageFormat::applyPattern
virtual void applyPattern(const UnicodeString &pattern, UErrorCode &status)
Applies the given pattern string to this message format.
icu::MessagePattern
Parses and represents ICU MessageFormat patterns.
Definition: messagepattern.h:362
icu::NumberFormat
Definition: numfmt.h:175
icu::ParsePosition
ParsePosition is a simple class used by Format and its subclasses to keep track of the current positi...
Definition: parsepos.h:52
icu::PluralRules
Defines rules for mapping non-negative numeric values onto a small set of keywords.
Definition: plurrule.h:212
icu::StringEnumeration
Base class for 'pure' C++ implementations of uenum api.
Definition: strenum.h:61
icu::UnicodeString
UnicodeString is a string class that stores Unicode characters directly and provides similar function...
Definition: unistr.h:296
format.h
C++ API: Base class for all formats.
locid.h
C++ API: Locale ID object.
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:70
UHashtable
struct UHashtable UHashtable
Definition: msgfmt.h:43
parseerr.h
C API: Parse Error Information.
plurfmt.h
C++ API: PluralFormat object.
plurrule.h
C++ API: PluralRules object.
UParseError
A UParseError struct is used to returned detailed information about parsing errors.
Definition: parseerr.h:58
U_CDECL_END
#define U_CDECL_END
This is used to end a declaration of a library private ICU C API.
Definition: umachine.h:86
UBool
int8_t UBool
The ICU boolean type, a signed-byte integer.
Definition: umachine.h:247
U_CDECL_BEGIN
#define U_CDECL_BEGIN
This is used to begin a declaration of a library private ICU C API.
Definition: umachine.h:85
UClassID
void * UClassID
UClassID is used to identify classes without using the compiler's RTTI.
Definition: uobject.h:96
UPluralType
UPluralType
Type of plurals and PluralRules.
Definition: upluralrules.h:59
utypes.h
Basic definitions for ICU, for both C and C++ APIs.
UErrorCode
UErrorCode
Standard ICU4C error code type, a substitute for exceptions.
Definition: utypes.h:415
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:301