ICU 66.0.1  66.0.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
348 
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 
410  const MessageFormat& operator=(const MessageFormat&);
411 
416  virtual ~MessageFormat();
417 
423  virtual MessageFormat* clone() const;
424 
432  virtual UBool operator==(const Format& other) const;
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 
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;
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;
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
829 
839  int32_t getArgTypeCount() const;
840 #endif /* U_HIDE_INTERNAL_API */
841 
853  virtual UClassID getDynamicClassID(void) const;
854 
866  static UClassID U_EXPORT2 getStaticClassID(void);
867 
868 #ifndef U_HIDE_INTERNAL_API
869 
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;
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 UBool operator==(const Format&) const;
1092  virtual DummyFormat* clone() const;
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;
1100  virtual UnicodeString& format(const Formattable& obj,
1101  UnicodeString& appendTo,
1102  FieldPositionIterator* posIter,
1103  UErrorCode& status) const;
1104  virtual void parseObject(const UnicodeString&,
1105  Formattable&,
1106  ParsePosition&) const;
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
Base class for all formats.
Definition: format.h:98
struct UHashtable UHashtable
Definition: msgfmt.h:43
UnicodeString & format(const Formattable &obj, UnicodeString &appendTo, UErrorCode &status) const
Formats an object to produce a string.
Type
Selector for flavor of data type contained within a Formattable object.
Definition: fmtable.h:221
void * UClassID
UClassID is used to identify classes without using the compiler's RTTI.
Definition: uobject.h:96
C++ API: PluralFormat object.
Base class for 'pure' C++ implementations of uenum api.
Definition: strenum.h:61
virtual Format * clone() const =0
Clone this object polymorphically.
C++ API: PluralRules object.
EFormatNumber
Enum type for kMaxFormat.
Definition: msgfmt.h:353
Defines rules for mapping non-negative numeric values onto a small set of keywords.
Definition: plurrule.h:200
C++ API: Base class for all formats.
Parses and represents ICU MessageFormat patterns.
#define U_CDECL_BEGIN
This is used to begin a declaration of a library private ICU C API.
Definition: umachine.h:84
#define U_I18N_API
Set to export library symbols from inside the i18n library, and to import them from outside...
Definition: utypes.h:301
FieldPositionIterator returns the field ids and their start/limit positions generated by a call to Fo...
Definition: fpositer.h:58
virtual UClassID getDynamicClassID() const
ICU4C "poor man's RTTI", returns a UClassID for the actual ICU class.
#define U_CDECL_END
This is used to end a declaration of a library private ICU C API.
Definition: umachine.h:85
C API: Parse Error Information.
UErrorCode
Standard ICU4C error code type, a substitute for exceptions.
Definition: utypes.h:415
FieldPosition is a simple class used by Format and its subclasses to identify fields in formatted out...
Definition: fieldpos.h:110
UPluralType
Type of plurals and PluralRules.
Definition: upluralrules.h:53
ParsePosition is a simple class used by Format and its subclasses to keep track of the current positi...
Definition: parsepos.h:52
DateFormat is an abstract class for a family of classes that convert dates and times from their inter...
Definition: datefmt.h:156
C++ API: Locale ID object.
virtual UBool operator==(const Format &other) const =0
Return true if the given Format objects are semantically equal.
virtual void parseObject(const UnicodeString &source, Formattable &result, ParsePosition &parse_pos) const =0
Parse a string to produce an object.
A UParseError struct is used to returned detailed information about parsing errors.
Definition: parseerr.h:58
UMessagePatternApostropheMode getApostropheMode() const
Definition: msgfmt.h:500
Basic definitions for ICU, for both C and C++ APIs.
Format & operator=(const Format &)
UnicodeString is a string class that stores Unicode characters directly and provides similar function...
Definition: unistr.h:294
Formattable objects can be passed to the Format class or its subclasses for formatting.
Definition: fmtable.h:64
Locale getLocale(ULocDataLocaleType type, UErrorCode &status) const
Get the locale for this format object.
C++ API: MessagePattern class: Parses and represents ICU MessageFormat patterns.
UMessagePatternApostropheMode
Mode for when an apostrophe starts quoted literal text for MessageFormat output.
int8_t UBool
The ICU boolean type.
Definition: umachine.h:261
A Locale object represents a specific geographical, political, or cultural region.
Definition: locid.h:195