ICU 70.1  70.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 
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);
405 
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,
493  UParseError* parseError,
494  UErrorCode& status);
495 
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 
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 
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 
815  UErrorCode& status);
816 
817 
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(); // 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  int32_t skipLeadingSpaces(UnicodeString& style);
1015 
1016  Format* createAppropriateFormat(UnicodeString& type,
1017  UnicodeString& style,
1018  Formattable::Type& formattableType,
1019  UParseError& parseError,
1020  UErrorCode& ec);
1021 
1022  const Formattable* getArgFromListByName(const Formattable* arguments,
1023  const UnicodeString *argumentNames,
1024  int32_t cnt, UnicodeString& name) const;
1025 
1026  Formattable* parse(int32_t msgStart,
1027  const UnicodeString& source,
1028  ParsePosition& pos,
1029  int32_t& count,
1030  UErrorCode& ec) const;
1031 
1032  FieldPosition* updateMetaData(AppendableWrapper& dest, int32_t prevLength,
1033  FieldPosition* fp, const Formattable* argId) const;
1034 
1040  int32_t findOtherSubMessage(int32_t partIndex) const;
1041 
1047  int32_t findFirstPluralNumberArg(int32_t msgStart, const UnicodeString &argName) const;
1048 
1049  Format* getCachedFormatter(int32_t argumentNumber) const;
1050 
1051  UnicodeString getLiteralStringUntilNextArgument(int32_t from) const;
1052 
1053  void copyObjects(const MessageFormat& that, UErrorCode& ec);
1054 
1055  void formatComplexSubMessage(int32_t msgStart,
1056  const void *plNumber,
1057  const Formattable* arguments,
1058  const UnicodeString *argumentNames,
1059  int32_t cnt,
1060  AppendableWrapper& appendTo,
1061  UErrorCode& success) const;
1062 
1066  NumberFormat* createIntegerFormat(const Locale& locale, UErrorCode& status) const;
1067 
1075  const Formattable::Type* getArgTypeList(int32_t& listCount) const {
1076  listCount = argTypeCount;
1077  return argTypes;
1078  }
1079 
1083  void resetPattern();
1084 
1089  class U_I18N_API DummyFormat : public Format {
1090  public:
1091  virtual bool operator==(const Format&) const override;
1092  virtual DummyFormat* clone() const override;
1093  virtual UnicodeString& format(const Formattable& obj,
1094  UnicodeString& appendTo,
1095  UErrorCode& status) const;
1096  virtual UnicodeString& format(const Formattable&,
1097  UnicodeString& appendTo,
1098  FieldPosition&,
1099  UErrorCode& status) const override;
1100  virtual UnicodeString& format(const Formattable& obj,
1101  UnicodeString& appendTo,
1102  FieldPositionIterator* posIter,
1103  UErrorCode& status) const override;
1104  virtual void parseObject(const UnicodeString&,
1105  Formattable&,
1106  ParsePosition&) const override;
1107  };
1108 
1109  friend class MessageFormatAdapter; // getFormatTypeList() access
1110 };
1111 
1112 U_NAMESPACE_END
1113 
1114 #endif /* #if !UCONFIG_NO_FORMATTING */
1115 
1116 #endif /* U_SHOW_CPLUSPLUS_API */
1117 
1118 #endif // _MSGFMT
1119 //eof
DateFormat is an abstract class for a family of classes that convert dates and times from their inter...
Definition: datefmt.h:156
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
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 bool operator==(const Format &other) const =0
Return true if the given Format objects are semantically equal.
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:64
Type
Selector for flavor of data type contained within a Formattable object.
Definition: fmtable.h:221
A Locale object represents a specific geographical, political, or cultural region.
Definition: locid.h:195
virtual void applyPattern(const UnicodeString &pattern, UMessagePatternApostropheMode aposMode, UParseError *parseError, UErrorCode &status)
Sets the UMessagePatternApostropheMode and the pattern used by this message 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.
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 Format * getFormat(const UnicodeString &formatName, UErrorCode &status)
Gets subformat pointer for given format name.
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 Formattable * parse(const UnicodeString &source, ParsePosition &pos, int32_t &count) const
Parses the given string into an array of output arguments.
virtual UClassID getDynamicClassID(void) const override
Returns a unique class ID POLYMORPHICALLY.
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:500
int32_t getArgTypeCount() const
This API is for ICU internal use only.
virtual const Locale & getLocale(void) const
Gets the locale used for creating argument Format objects.
virtual StringEnumeration * getFormatNames(UErrorCode &status)
Gets format names.
virtual Formattable * parse(const UnicodeString &source, int32_t &count, UErrorCode &status) const
Parses the given string into an array of output arguments.
MessageFormat(const MessageFormat &)
Constructs a new MessageFormat from an existing one.
EFormatNumber
Enum type for kMaxFormat.
Definition: msgfmt.h:353
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 ...
virtual void setLocale(const Locale &theLocale)
Sets the locale to be used for creating argument Format objects.
virtual UnicodeString & toPattern(UnicodeString &appendTo) const
Returns a pattern that can be used to recreate this object.
virtual MessageFormat * clone() const override
Clones this Format object polymorphically.
virtual ~MessageFormat()
Destructor.
UBool usesNamedArguments() const
Returns true if this MessageFormat uses named arguments, and false otherwise.
const MessageFormat & operator=(const MessageFormat &)
Assignment operator.
virtual const Format ** getFormats(int32_t &count) const
Gets an array of subformats of this object.
virtual void setFormat(const UnicodeString &formatName, const Format &format, UErrorCode &status)
Sets one subformat for given format name.
virtual bool operator==(const Format &other) const override
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 void adoptFormat(int32_t formatNumber, Format *formatToAdopt)
Sets one subformat.
MessageFormat(const UnicodeString &pattern, const Locale &newLocale, UErrorCode &status)
Constructs a new MessageFormat using the given pattern and locale.
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.
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:206
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:296
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
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:58
Basic definitions for ICU, for both C and C++ APIs.
UErrorCode
Standard ICU4C error code type, a substitute for exceptions.
Definition: utypes.h:415
#define U_I18N_API
Set to export library symbols from inside the i18n library, and to import them from outside.
Definition: utypes.h:301