ICU 78.1  78.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 
41 // Forward declaration.
42 struct UHashtable;
43 typedef struct UHashtable UHashtable;
45 
46 U_NAMESPACE_BEGIN
47 
48 class AppendableWrapper;
49 class DateFormat;
50 class NumberFormat;
51 
346 public:
347 #ifndef U_HIDE_OBSOLETE_API
359  kMaxFormat = 10
360  };
361 #endif /* U_HIDE_OBSOLETE_API */
362 
373 
382  U_I18N_API MessageFormat(const UnicodeString& pattern, const Locale& newLocale, UErrorCode& status);
394  const Locale& newLocale,
395  UParseError& parseError,
396  UErrorCode& status);
402 
408 
414 
420  U_I18N_API virtual MessageFormat* clone() const override;
421 
429  U_I18N_API virtual bool operator==(const Format& other) const override;
430 
436  U_I18N_API virtual void setLocale(const Locale& theLocale);
437 
444  U_I18N_API virtual const Locale& getLocale() const;
445 
454  U_I18N_API virtual void applyPattern(const UnicodeString& pattern, UErrorCode& status);
465  U_I18N_API virtual void applyPattern(const UnicodeString& pattern,
466  UParseError& parseError,
467  UErrorCode& status);
468 
487  U_I18N_API virtual void applyPattern(const UnicodeString& pattern,
489  UParseError* parseError,
490  UErrorCode& status);
491 
497  return msgPattern.getApostropheMode();
498  }
499 
508  U_I18N_API virtual UnicodeString& toPattern(UnicodeString& appendTo) const;
509 
526  U_I18N_API virtual void adoptFormats(Format** formatsToAdopt, int32_t count);
527 
542  U_I18N_API virtual void setFormats(const Format** newFormats, int32_t cnt);
543 
558  U_I18N_API virtual void adoptFormat(int32_t formatNumber, Format* formatToAdopt);
559 
569  U_I18N_API virtual void setFormat(int32_t formatNumber, const Format& format);
570 
580 
594  U_I18N_API virtual Format* getFormat(const UnicodeString& formatName, UErrorCode& status);
595 
609  U_I18N_API virtual void setFormat(const UnicodeString& formatName,
610  const Format& format,
611  UErrorCode& status);
612 
627  U_I18N_API virtual void adoptFormat(const UnicodeString& formatName,
628  Format* formatToAdopt,
629  UErrorCode& status);
630 
643  U_I18N_API virtual const Format** getFormats(int32_t& count) const;
644 
645  using Format::format;
646 
665  int32_t count,
666  UnicodeString& appendTo,
667  FieldPosition& ignore,
668  UErrorCode& status) const;
669 
688  const Formattable* arguments,
689  int32_t count,
690  UnicodeString& appendTo,
691  UErrorCode& status);
692 
714  UnicodeString& appendTo,
715  FieldPosition& pos,
716  UErrorCode& status) const override;
717 
737  const Formattable* arguments,
738  int32_t count,
739  UnicodeString& appendTo,
740  UErrorCode& status) const;
754  U_I18N_API virtual Formattable* parse(const UnicodeString& source,
755  ParsePosition& pos,
756  int32_t& count) const;
757 
773  U_I18N_API virtual Formattable* parse(const UnicodeString& source,
774  int32_t& count,
775  UErrorCode& status) const;
776 
789  U_I18N_API virtual void parseObject(const UnicodeString& source,
790  Formattable& result,
791  ParsePosition& pos) const override;
792 
813 
822 
823 #ifndef U_HIDE_INTERNAL_API
834  U_I18N_API int32_t getArgTypeCount() const;
835 #endif /* U_HIDE_INTERNAL_API */
836 
848  U_I18N_API virtual UClassID getDynamicClassID() const override;
849 
862 
863 #ifndef U_HIDE_INTERNAL_API
874  U_I18N_API static UBool equalFormats(const void* left, const void* right);
875 #endif /* U_HIDE_INTERNAL_API */
876 
877 private:
878 
879  Locale fLocale;
880  MessagePattern msgPattern;
881  Format** formatAliases; // see getFormats
882  int32_t formatAliasesCapacity;
883 
884  MessageFormat() = delete; // default constructor not implemented
885 
892  class PluralSelectorProvider : public PluralFormat::PluralSelector {
893  public:
894  PluralSelectorProvider(const MessageFormat &mf, UPluralType type);
895  virtual ~PluralSelectorProvider();
896  virtual UnicodeString select(void *ctx, double number, UErrorCode& ec) const override;
897 
898  void reset();
899  private:
900  const MessageFormat &msgFormat;
901  PluralRules* rules;
902  UPluralType type;
903  };
904 
913  Formattable::Type* argTypes;
914  int32_t argTypeCount;
915  int32_t argTypeCapacity;
916 
922  UBool hasArgTypeConflicts;
923 
924  // Variable-size array management
925  UBool allocateArgTypes(int32_t capacity, UErrorCode& status);
926 
934  NumberFormat* defaultNumberFormat;
935  DateFormat* defaultDateFormat;
936 
937  UHashtable* cachedFormatters;
938  UHashtable* customFormatArgStarts;
939 
940  PluralSelectorProvider pluralProvider;
941  PluralSelectorProvider ordinalProvider;
942 
947  const NumberFormat* getDefaultNumberFormat(UErrorCode&) const;
948  const DateFormat* getDefaultDateFormat(UErrorCode&) const;
949 
956  static int32_t findKeyword( const UnicodeString& s,
957  const char16_t * const *list);
958 
964  UnicodeString& format(const Formattable* arguments,
965  const UnicodeString *argumentNames,
966  int32_t cnt,
967  UnicodeString& appendTo,
968  FieldPosition* pos,
969  UErrorCode& status) const;
970 
988  void format(int32_t msgStart,
989  const void *plNumber,
990  const Formattable* arguments,
991  const UnicodeString *argumentNames,
992  int32_t cnt,
993  AppendableWrapper& appendTo,
994  FieldPosition* pos,
995  UErrorCode& success) const;
996 
997  UnicodeString getArgName(int32_t partIndex);
998 
999  void setArgStartFormat(int32_t argStart, Format* formatter, UErrorCode& status);
1000 
1001  void setCustomArgStartFormat(int32_t argStart, Format* formatter, UErrorCode& status);
1002 
1003  int32_t nextTopLevelArgStart(int32_t partIndex) const;
1004 
1005  UBool argNameMatches(int32_t partIndex, const UnicodeString& argName, int32_t argNumber);
1006 
1007  void cacheExplicitFormats(UErrorCode& status);
1008 
1009  Format* createAppropriateFormat(UnicodeString& type,
1010  UnicodeString& style,
1011  Formattable::Type& formattableType,
1012  UParseError& parseError,
1013  UErrorCode& ec);
1014 
1015  const Formattable* getArgFromListByName(const Formattable* arguments,
1016  const UnicodeString *argumentNames,
1017  int32_t cnt, UnicodeString& name) const;
1018 
1019  Formattable* parse(int32_t msgStart,
1020  const UnicodeString& source,
1021  ParsePosition& pos,
1022  int32_t& count,
1023  UErrorCode& ec) const;
1024 
1025  FieldPosition* updateMetaData(AppendableWrapper& dest, int32_t prevLength,
1026  FieldPosition* fp, const Formattable* argId) const;
1027 
1033  int32_t findOtherSubMessage(int32_t partIndex) const;
1034 
1040  int32_t findFirstPluralNumberArg(int32_t msgStart, const UnicodeString &argName) const;
1041 
1042  Format* getCachedFormatter(int32_t argumentNumber) const;
1043 
1044  UnicodeString getLiteralStringUntilNextArgument(int32_t from) const;
1045 
1046  void copyObjects(const MessageFormat& that, UErrorCode& ec);
1047 
1048  void formatComplexSubMessage(int32_t msgStart,
1049  const void *plNumber,
1050  const Formattable* arguments,
1051  const UnicodeString *argumentNames,
1052  int32_t cnt,
1053  AppendableWrapper& appendTo,
1054  UErrorCode& success) const;
1055 
1059  NumberFormat* createIntegerFormat(const Locale& locale, UErrorCode& status) const;
1060 
1068  const Formattable::Type* getArgTypeList(int32_t& listCount) const {
1069  listCount = argTypeCount;
1070  return argTypes;
1071  }
1072 
1076  void resetPattern();
1077 
1082  class DummyFormat : public Format {
1083  public:
1084  virtual bool operator==(const Format&) const override;
1085  virtual DummyFormat* clone() const override;
1086  virtual UnicodeString& format(const Formattable& obj,
1087  UnicodeString& appendTo,
1088  UErrorCode& status) const;
1089  virtual UnicodeString& format(const Formattable&,
1090  UnicodeString& appendTo,
1091  FieldPosition&,
1092  UErrorCode& status) const override;
1093  virtual UnicodeString& format(const Formattable& obj,
1094  UnicodeString& appendTo,
1095  FieldPositionIterator* posIter,
1096  UErrorCode& status) const override;
1097  virtual void parseObject(const UnicodeString&,
1098  Formattable&,
1099  ParsePosition&) const override;
1100  };
1101 
1102  friend class MessageFormatAdapter; // getFormatTypeList() access
1103 };
1104 
1105 U_NAMESPACE_END
1106 
1107 #endif /* #if !UCONFIG_NO_FORMATTING */
1108 
1109 #endif /* U_SHOW_CPLUSPLUS_API */
1110 
1111 #endif // _MSGFMT
1112 //eof
DateFormat is an abstract class for a family of classes that convert dates and times from their inter...
Definition: datefmt.h:144
FieldPosition is a simple class used by Format and its subclasses to identify fields in formatted out...
Definition: fieldpos.h:110
Base class for all formats.
Definition: format.h:98
U_I18N_API UnicodeString & format(const Formattable &obj, UnicodeString &appendTo, UErrorCode &status) const
Formats an object to produce a string.
Formattable objects can be passed to the Format class or its subclasses for formatting.
Definition: fmtable.h:63
Type
Selector for flavor of data type contained within a Formattable object.
Definition: fmtable.h:220
A Locale object represents a specific geographical, political, or cultural region.
Definition: locid.h:198
virtual U_I18N_API void adoptFormats(Format **formatsToAdopt, int32_t count)
Sets subformats.
U_I18N_API 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.
U_I18N_API int32_t getArgTypeCount() const
This API is for ICU internal use only.
U_I18N_API MessageFormat(const UnicodeString &pattern, const Locale &newLocale, UErrorCode &status)
Constructs a new MessageFormat using the given pattern and locale.
virtual U_I18N_API UnicodeString & toPattern(UnicodeString &appendTo) const
Returns a pattern that can be used to recreate this object.
virtual U_I18N_API void adoptFormat(const UnicodeString &formatName, Format *formatToAdopt, UErrorCode &status)
Sets one subformat for given format name.
virtual U_I18N_API StringEnumeration * getFormatNames(UErrorCode &status)
Gets format names.
virtual U_I18N_API const Format ** getFormats(int32_t &count) const
Gets an array of subformats of this object.
virtual U_I18N_API 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 ...
static U_I18N_API UClassID getStaticClassID()
Return the class ID for this class.
U_I18N_API UBool usesNamedArguments() const
Returns true if this MessageFormat uses named arguments, and false otherwise.
virtual U_I18N_API void setFormats(const Format **newFormats, int32_t cnt)
Sets subformats.
U_I18N_API MessageFormat(const UnicodeString &pattern, UErrorCode &status)
Constructs a new MessageFormat using the given pattern and the default locale.
virtual U_I18N_API bool operator==(const Format &other) const override
Returns true if the given Format objects are semantically equal.
virtual U_I18N_API UClassID getDynamicClassID() const override
Returns a unique class ID POLYMORPHICALLY.
virtual U_I18N_API void adoptFormat(int32_t formatNumber, Format *formatToAdopt)
Sets one subformat.
EFormatNumber
Enum type for kMaxFormat.
Definition: msgfmt.h:353
static U_I18N_API 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 U_I18N_API MessageFormat * clone() const override
Clones this Format object polymorphically.
virtual U_I18N_API void applyPattern(const UnicodeString &pattern, UParseError &parseError, UErrorCode &status)
Applies the given pattern string to this message format.
U_I18N_API MessageFormat(const MessageFormat &)
Constructs a new MessageFormat from an existing one.
virtual U_I18N_API void setFormat(const UnicodeString &formatName, const Format &format, UErrorCode &status)
Sets one subformat for given format name.
virtual U_I18N_API UnicodeString & format(const Formattable &obj, UnicodeString &appendTo, FieldPosition &pos, UErrorCode &status) const override
Formats the given array of arguments into a user-readable string.
virtual U_I18N_API const Locale & getLocale() const
Gets the locale used for creating argument Format objects.
U_I18N_API const MessageFormat & operator=(const MessageFormat &)
Assignment operator.
virtual U_I18N_API Formattable * parse(const UnicodeString &source, int32_t &count, UErrorCode &status) const
Parses the given string into an array of output arguments.
virtual U_I18N_API ~MessageFormat()
Destructor.
virtual U_I18N_API void applyPattern(const UnicodeString &pattern, UErrorCode &status)
Applies the given pattern string to this message format.
static U_I18N_API UnicodeString autoQuoteApostrophe(const UnicodeString &pattern, UErrorCode &status)
Convert an 'apostrophe-friendly' pattern into a standard pattern.
U_I18N_API MessageFormat(const UnicodeString &pattern, const Locale &newLocale, UParseError &parseError, UErrorCode &status)
Constructs a new MessageFormat using the given pattern and locale.
virtual U_I18N_API Format * getFormat(const UnicodeString &formatName, UErrorCode &status)
Gets subformat pointer for given format name.
virtual U_I18N_API void setLocale(const Locale &theLocale)
Sets the locale to be used for creating argument Format objects.
U_I18N_API 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.
U_I18N_API UMessagePatternApostropheMode getApostropheMode() const
Definition: msgfmt.h:496
virtual U_I18N_API void applyPattern(const UnicodeString &pattern, UMessagePatternApostropheMode aposMode, UParseError *parseError, UErrorCode &status)
Sets the UMessagePatternApostropheMode and the pattern used by this message format.
virtual U_I18N_API void setFormat(int32_t formatNumber, const Format &format)
Sets one subformat.
static U_I18N_API UBool equalFormats(const void *left, const void *right)
Compares two Format objects.
virtual U_I18N_API Formattable * parse(const UnicodeString &source, ParsePosition &pos, int32_t &count) const
Parses the given string into an array of output arguments.
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:52
Defines rules for mapping non-negative numeric values onto a small set of keywords.
Definition: plurrule.h:212
Base class for 'pure' C++ implementations of uenum api.
Definition: strenum.h:61
UnicodeString is a string class that stores Unicode characters directly and provides similar function...
Definition: unistr.h:303
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.
struct UHashtable UHashtable
Definition: msgfmt.h:43
U_COMMON_API UBool operator==(const StringPiece &x, const StringPiece &y)
Global operator == for StringPiece.
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:86
int8_t UBool
The ICU boolean type, a signed-byte integer.
Definition: umachine.h:269
#define U_CDECL_BEGIN
This is used to begin a declaration of a library private ICU C API.
Definition: umachine.h:85
void * UClassID
UClassID is used to identify classes without using the compiler's RTTI.
Definition: uobject.h:96
UPluralType
Type of plurals and PluralRules.
Definition: upluralrules.h:59
Basic definitions for ICU, for both C and C++ APIs.
UErrorCode
Standard ICU4C error code type, a substitute for exceptions.
Definition: utypes.h:509
#define U_I18N_API_CLASS
Set to export library symbols from inside the i18n library, and to import them from outside,...
Definition: utypes.h:457
#define U_I18N_API
Set to export library symbols from inside the i18n library, and to import them from outside.
Definition: utypes.h:316