ICU 63.1 63.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"
33 #include "unicode/messagepattern.h"
34 #include "unicode/parseerr.h"
35 #include "unicode/plurfmt.h"
36 #include "unicode/plurrule.h"
37
38 U_CDECL_BEGIN
39 // Forward declaration.
40 struct UHashtable;
41 typedef struct UHashtable UHashtable;
42 U_CDECL_END
43
44 U_NAMESPACE_BEGIN
45
46 class AppendableWrapper;
47 class DateFormat;
48 class NumberFormat;
49
328 class U_I18N_API MessageFormat : public Format {
329 public:
330 #ifndef U_HIDE_OBSOLETE_API
331
336 enum EFormatNumber {
342 kMaxFormat = 10
343 };
344 #endif /* U_HIDE_OBSOLETE_API */
345
355 MessageFormat(const UnicodeString& pattern,
356 UErrorCode &status);
357
366 MessageFormat(const UnicodeString& pattern,
367 const Locale& newLocale,
368 UErrorCode& status);
379 MessageFormat(const UnicodeString& pattern,
380 const Locale& newLocale,
381 UParseError& parseError,
382 UErrorCode& status);
387 MessageFormat(const MessageFormat&);
388
393 const MessageFormat& operator=(const MessageFormat&);
394
399 virtual ~MessageFormat();
400
406 virtual Format* clone(void) const;
407
415 virtual UBool operator==(const Format& other) const;
416
422 virtual void setLocale(const Locale& theLocale);
423
430 virtual const Locale& getLocale(void) const;
431
440 virtual void applyPattern(const UnicodeString& pattern,
441 UErrorCode& status);
452 virtual void applyPattern(const UnicodeString& pattern,
453 UParseError& parseError,
454 UErrorCode& status);
455
474 virtual void applyPattern(const UnicodeString& pattern,
475 UMessagePatternApostropheMode aposMode,
476 UParseError* parseError,
477 UErrorCode& status);
478
483 UMessagePatternApostropheMode getApostropheMode() const {
484 return msgPattern.getApostropheMode();
485 }
486
495 virtual UnicodeString& toPattern(UnicodeString& appendTo) const;
496
513 virtual void adoptFormats(Format** formatsToAdopt, int32_t count);
514
529 virtual void setFormats(const Format** newFormats, int32_t cnt);
530
531
546 virtual void adoptFormat(int32_t formatNumber, Format* formatToAdopt);
547
557 virtual void setFormat(int32_t formatNumber, const Format& format);
558
567 virtual StringEnumeration* getFormatNames(UErrorCode& status);
568
582 virtual Format* getFormat(const UnicodeString& formatName, UErrorCode& status);
583
597 virtual void setFormat(const UnicodeString& formatName, const Format& format, UErrorCode& status);
598
613 virtual void adoptFormat(const UnicodeString& formatName, Format* formatToAdopt, UErrorCode& status);
614
627 virtual const Format** getFormats(int32_t& count) const;
628
629
630 using Format::format;
631
649 UnicodeString& format(const Formattable* source,
650 int32_t count,
651 UnicodeString& appendTo,
652 FieldPosition& ignore,
653 UErrorCode& status) const;
654
672 static UnicodeString& format(const UnicodeString& pattern,
673 const Formattable* arguments,
674 int32_t count,
675 UnicodeString& appendTo,
676 UErrorCode& status);
677
698 virtual UnicodeString& format(const Formattable& obj,
699 UnicodeString& appendTo,
700 FieldPosition& pos,
701 UErrorCode& status) const;
702
721 UnicodeString& format(const UnicodeString* argumentNames,
722 const Formattable* arguments,
723 int32_t count,
724 UnicodeString& appendTo,
725 UErrorCode& status) const;
739 virtual Formattable* parse(const UnicodeString& source,
740 ParsePosition& pos,
741 int32_t& count) const;
742
758 virtual Formattable* parse(const UnicodeString& source,
759 int32_t& count,
760 UErrorCode& status) const;
761
774 virtual void parseObject(const UnicodeString& source,
775 Formattable& result,
776 ParsePosition& pos) const;
777
797 static UnicodeString autoQuoteApostrophe(const UnicodeString& pattern,
798 UErrorCode& status);
799
800
808 UBool usesNamedArguments() const;
809
810
811 #ifndef U_HIDE_INTERNAL_API
812
822 int32_t getArgTypeCount() const;
823 #endif /* U_HIDE_INTERNAL_API */
824
836 virtual UClassID getDynamicClassID(void) const;
837
849 static UClassID U_EXPORT2 getStaticClassID(void);
850
851 #ifndef U_HIDE_INTERNAL_API
852
862 static UBool equalFormats(const void* left, const void* right);
863 #endif /* U_HIDE_INTERNAL_API */
864
865 private:
866
867 Locale fLocale;
868 MessagePattern msgPattern;
869 Format** formatAliases; // see getFormats
870 int32_t formatAliasesCapacity;
871
872 MessageFormat(); // default constructor not implemented
873
880 class U_I18N_API PluralSelectorProvider : public PluralFormat::PluralSelector {
881 public:
882 PluralSelectorProvider(const MessageFormat &mf, UPluralType type);
883 virtual ~PluralSelectorProvider();
884 virtual UnicodeString select(void *ctx, double number, UErrorCode& ec) const;
885
886 void reset();
887 private:
888 const MessageFormat &msgFormat;
889 PluralRules* rules;
890 UPluralType type;
891 };
892
901 Formattable::Type* argTypes;
902 int32_t argTypeCount;
903 int32_t argTypeCapacity;
904
910 UBool hasArgTypeConflicts;
911
912 // Variable-size array management
913 UBool allocateArgTypes(int32_t capacity, UErrorCode& status);
914
922 NumberFormat* defaultNumberFormat;
923 DateFormat* defaultDateFormat;
924
925 UHashtable* cachedFormatters;
926 UHashtable* customFormatArgStarts;
927
928 PluralSelectorProvider pluralProvider;
929 PluralSelectorProvider ordinalProvider;
930
935 const NumberFormat* getDefaultNumberFormat(UErrorCode&) const;
936 const DateFormat* getDefaultDateFormat(UErrorCode&) const;
937
944 static int32_t findKeyword( const UnicodeString& s,
945 const char16_t * const *list);
946
952 UnicodeString& format(const Formattable* arguments,
953 const UnicodeString *argumentNames,
954 int32_t cnt,
955 UnicodeString& appendTo,
956 FieldPosition* pos,
957 UErrorCode& status) const;
958
976 void format(int32_t msgStart,
977 const void *plNumber,
978 const Formattable* arguments,
979 const UnicodeString *argumentNames,
980 int32_t cnt,
981 AppendableWrapper& appendTo,
982 FieldPosition* pos,
983 UErrorCode& success) const;
984
985 UnicodeString getArgName(int32_t partIndex);
986
987 void setArgStartFormat(int32_t argStart, Format* formatter, UErrorCode& status);
988
989 void setCustomArgStartFormat(int32_t argStart, Format* formatter, UErrorCode& status);
990
991 int32_t nextTopLevelArgStart(int32_t partIndex) const;
992
993 UBool argNameMatches(int32_t partIndex, const UnicodeString& argName, int32_t argNumber);
994
995 void cacheExplicitFormats(UErrorCode& status);
996
997 Format* createAppropriateFormat(UnicodeString& type,
998 UnicodeString& style,
999 Formattable::Type& formattableType,
1000 UParseError& parseError,
1001 UErrorCode& ec);
1002
1003 const Formattable* getArgFromListByName(const Formattable* arguments,
1004 const UnicodeString *argumentNames,
1005 int32_t cnt, UnicodeString& name) const;
1006
1007 Formattable* parse(int32_t msgStart,
1008 const UnicodeString& source,
1009 ParsePosition& pos,
1010 int32_t& count,
1011 UErrorCode& ec) const;
1012
1013 FieldPosition* updateMetaData(AppendableWrapper& dest, int32_t prevLength,
1014 FieldPosition* fp, const Formattable* argId) const;
1015
1021 int32_t findOtherSubMessage(int32_t partIndex) const;
1022
1028 int32_t findFirstPluralNumberArg(int32_t msgStart, const UnicodeString &argName) 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 const void *plNumber,
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
1056 const Formattable::Type* getArgTypeList(int32_t& listCount) const {
1057 listCount = argTypeCount;
1058 return argTypes;
1059 }
1060
1064 void resetPattern();
1065
1070 class U_I18N_API DummyFormat : public Format {
1071 public:
1072 virtual UBool operator==(const Format&) const;
1073 virtual Format* clone() const;
1074 virtual UnicodeString& format(const Formattable& obj,
1075 UnicodeString& appendTo,
1076 UErrorCode& status) const;
1077 virtual UnicodeString& format(const Formattable&,
1078 UnicodeString& appendTo,
1079 FieldPosition&,
1080 UErrorCode& status) const;
1081 virtual UnicodeString& format(const Formattable& obj,
1082 UnicodeString& appendTo,
1083 FieldPositionIterator* posIter,
1084 UErrorCode& status) const;
1085 virtual void parseObject(const UnicodeString&,
1086 Formattable&,
1087 ParsePosition&) const;
1088 };
1089
1090 friend class MessageFormatAdapter; // getFormatTypeList() access
1091 };
1092
1093 U_NAMESPACE_END
1094
1095 #endif /* #if !UCONFIG_NO_FORMATTING */
1096
1097 #endif // _MSGFMT
1098 //eof
icu::Format
Base class for all formats.
Definition: format.h:96
UHashtable
struct UHashtable UHashtable
Definition: msgfmt.h:41
icu::Format::format
UnicodeString & format(const Formattable &obj, UnicodeString &appendTo, UErrorCode &status) const
Formats an object to produce a string.
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&#39;s RTTI.
Definition: uobject.h:90
plurfmt.h
C++ API: PluralFormat object.
icu::StringEnumeration
Base class for &#39;pure&#39; C++ implementations of uenum api.
Definition: strenum.h:57
icu::Format::clone
virtual Format * clone() const =0
Clone this object polymorphically.
plurrule.h
C++ API: PluralRules object.
icu::MessageFormat::EFormatNumber
EFormatNumber
Enum type for kMaxFormat.
Definition: msgfmt.h:336
icu::PluralRules
Defines rules for mapping non-negative numeric values onto a small set of keywords.
Definition: plurrule.h:194
format.h
C++ API: Base class for all formats.
icu::MessagePattern
Parses and represents ICU MessageFormat patterns.
Definition: messagepattern.h:360
U_CDECL_BEGIN
#define U_CDECL_BEGIN
This is used to begin a declaration of a library private ICU C API.
Definition: umachine.h:84
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
U_NAMESPACE_BEGIN
#define U_NAMESPACE_BEGIN
This is used to begin a declaration of a public ICU C++ API.
Definition: uversion.h:137
icu::UObject::getDynamicClassID
virtual UClassID getDynamicClassID() const
ICU4C "poor man&#39;s RTTI", returns a UClassID for the actual ICU class.
icu::MessageFormat
Definition: msgfmt.h:328
U_CDECL_END
#define U_CDECL_END
This is used to end a declaration of a library private ICU C API.
Definition: umachine.h:85
U_NAMESPACE_END
#define U_NAMESPACE_END
This is used to end a declaration of a public ICU C++ API.
Definition: uversion.h:138
parseerr.h
C API: Parse Error Information.
UErrorCode
UErrorCode
Error code to replace exception handling, so that the code is compatible with all C++ compilers...
Definition: utypes.h:401
icu::FieldPosition
FieldPosition is a simple class used by Format and its subclasses to identify fields in formatted out...
Definition: fieldpos.h:108
UPluralType
UPluralType
Type of plurals and PluralRules.
Definition: upluralrules.h:50
icu::ParsePosition
ParsePosition is a simple class used by Format and its subclasses to keep track of the current positi...
Definition: parsepos.h:49
locid.h
C++ API: Locale ID object.
icu::Format::operator==
virtual UBool operator==(const Format &other) const =0
Return true if the given Format objects are semantically equal.
icu::Format::parseObject
virtual void parseObject(const UnicodeString &source, Formattable &result, ParsePosition &parse_pos) const =0
Parse a string to produce an object.
UParseError
A UParseError struct is used to returned detailed information about parsing errors.
Definition: parseerr.h:58
icu::MessageFormat::getApostropheMode
UMessagePatternApostropheMode getApostropheMode() const
Definition: msgfmt.h:483
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:289
icu::Formattable
Formattable objects can be passed to the Format class or its subclasses for formatting.
Definition: fmtable.h:62
icu::Format::getLocale
Locale getLocale(ULocDataLocaleType type, UErrorCode &status) const
Get the locale for this format 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:68
UBool
int8_t UBool
The ICU boolean type.
Definition: umachine.h:225
icu::Locale
A Locale object represents a specific geographical, political, or cultural region.
Definition: locid.h:192