ICU 50.1.2  50.1.2
msgfmt.h
Go to the documentation of this file.
1 /*
2 * Copyright (C) 2007-2012, International Business Machines Corporation and
3 * others. All Rights Reserved.
4 ********************************************************************************
5 *
6 * File MSGFMT.H
7 *
8 * Modification History:
9 *
10 * Date Name Description
11 * 02/19/97 aliu Converted from java.
12 * 03/20/97 helena Finished first cut of implementation.
13 * 07/22/98 stephen Removed operator!= (defined in Format)
14 * 08/19/2002 srl Removing Javaisms
15 *******************************************************************************/
16 
17 #ifndef MSGFMT_H
18 #define MSGFMT_H
19 
20 #include "unicode/utypes.h"
21 
27 #if !UCONFIG_NO_FORMATTING
28 
29 #include "unicode/format.h"
30 #include "unicode/locid.h"
31 #include "unicode/messagepattern.h"
32 #include "unicode/parseerr.h"
33 #include "unicode/plurfmt.h"
34 #include "unicode/plurrule.h"
35 
37 // Forward declaration.
38 struct UHashtable;
39 typedef struct UHashtable UHashtable;
41 
43 
44 class AppendableWrapper;
45 class DateFormat;
46 class NumberFormat;
47 
324 public:
325 #ifndef U_HIDE_OBSOLETE_API
326 
337  kMaxFormat = 10
338  };
339 #endif /* U_HIDE_OBSOLETE_API */
340 
350  MessageFormat(const UnicodeString& pattern,
351  UErrorCode &status);
352 
361  MessageFormat(const UnicodeString& pattern,
362  const Locale& newLocale,
363  UErrorCode& status);
374  MessageFormat(const UnicodeString& pattern,
375  const Locale& newLocale,
376  UParseError& parseError,
377  UErrorCode& status);
383 
388  const MessageFormat& operator=(const MessageFormat&);
389 
394  virtual ~MessageFormat();
395 
401  virtual Format* clone(void) const;
402 
410  virtual UBool operator==(const Format& other) const;
411 
417  virtual void setLocale(const Locale& theLocale);
418 
425  virtual const Locale& getLocale(void) const;
426 
435  virtual void applyPattern(const UnicodeString& pattern,
436  UErrorCode& status);
447  virtual void applyPattern(const UnicodeString& pattern,
448  UParseError& parseError,
449  UErrorCode& status);
450 
469  virtual void applyPattern(const UnicodeString& pattern,
471  UParseError* parseError,
472  UErrorCode& status);
473 
479  return msgPattern.getApostropheMode();
480  }
481 
490  virtual UnicodeString& toPattern(UnicodeString& appendTo) const;
491 
508  virtual void adoptFormats(Format** formatsToAdopt, int32_t count);
509 
524  virtual void setFormats(const Format** newFormats, int32_t cnt);
525 
526 
541  virtual void adoptFormat(int32_t formatNumber, Format* formatToAdopt);
542 
552  virtual void setFormat(int32_t formatNumber, const Format& format);
553 
562  virtual StringEnumeration* getFormatNames(UErrorCode& status);
563 
577  virtual Format* getFormat(const UnicodeString& formatName, UErrorCode& status);
578 
592  virtual void setFormat(const UnicodeString& formatName, const Format& format, UErrorCode& status);
593 
608  virtual void adoptFormat(const UnicodeString& formatName, Format* formatToAdopt, UErrorCode& status);
609 
622  virtual const Format** getFormats(int32_t& count) const;
623 
624 
625  using Format::format;
626 
644  UnicodeString& format(const Formattable* source,
645  int32_t count,
646  UnicodeString& appendTo,
647  FieldPosition& ignore,
648  UErrorCode& status) const;
649 
667  static UnicodeString& format(const UnicodeString& pattern,
668  const Formattable* arguments,
669  int32_t count,
670  UnicodeString& appendTo,
671  UErrorCode& status);
672 
693  virtual UnicodeString& format(const Formattable& obj,
694  UnicodeString& appendTo,
695  FieldPosition& pos,
696  UErrorCode& status) const;
697 
712  UnicodeString& format(const Formattable& obj,
713  UnicodeString& appendTo,
714  UErrorCode& status) const;
715 
716 
735  UnicodeString& format(const UnicodeString* argumentNames,
736  const Formattable* arguments,
737  int32_t count,
738  UnicodeString& appendTo,
739  UErrorCode& status) const;
753  virtual Formattable* parse(const UnicodeString& source,
754  ParsePosition& pos,
755  int32_t& count) const;
756 
772  virtual Formattable* parse(const UnicodeString& source,
773  int32_t& count,
774  UErrorCode& status) const;
775 
788  virtual void parseObject(const UnicodeString& source,
789  Formattable& result,
790  ParsePosition& pos) const;
791 
811  static UnicodeString autoQuoteApostrophe(const UnicodeString& pattern,
812  UErrorCode& status);
813 
814 
822  UBool usesNamedArguments() const;
823 
824 
825 #ifndef U_HIDE_INTERNAL_API
826 
836  int32_t getArgTypeCount() const;
837 #endif /* U_HIDE_INTERNAL_API */
838 
850  virtual UClassID getDynamicClassID(void) const;
851 
863  static UClassID U_EXPORT2 getStaticClassID(void);
864 
865 #ifndef U_HIDE_INTERNAL_API
866 
876  static UBool equalFormats(const void* left, const void* right);
877 #endif /* U_HIDE_INTERNAL_API */
878 
879 private:
880 
881  Locale fLocale;
882  MessagePattern msgPattern;
883  Format** formatAliases; // see getFormats
884  int32_t formatAliasesCapacity;
885 
886  MessageFormat(); // default constructor not implemented
887 
894  class U_I18N_API PluralSelectorProvider : public PluralFormat::PluralSelector {
895  public:
896  PluralSelectorProvider(const Locale* loc, UPluralType type);
897  virtual ~PluralSelectorProvider();
898  virtual UnicodeString select(double number, UErrorCode& ec) const;
899 
900  void reset(const Locale* loc);
901  private:
902  const Locale* locale;
903  PluralRules* rules;
904  UPluralType type;
905  };
906 
915  Formattable::Type* argTypes;
916  int32_t argTypeCount;
917  int32_t argTypeCapacity;
918 
924  UBool hasArgTypeConflicts;
925 
926  // Variable-size array management
927  UBool allocateArgTypes(int32_t capacity, UErrorCode& status);
928 
936  NumberFormat* defaultNumberFormat;
937  DateFormat* defaultDateFormat;
938 
939  UHashtable* cachedFormatters;
940  UHashtable* customFormatArgStarts;
941 
942  PluralSelectorProvider pluralProvider;
943  PluralSelectorProvider ordinalProvider;
944 
949  const NumberFormat* getDefaultNumberFormat(UErrorCode&) const;
950  const DateFormat* getDefaultDateFormat(UErrorCode&) const;
951 
958  static int32_t findKeyword( const UnicodeString& s,
959  const UChar * const *list);
960 
966  UnicodeString& format(const Formattable* arguments,
967  const UnicodeString *argumentNames,
968  int32_t cnt,
969  UnicodeString& appendTo,
970  FieldPosition* pos,
971  UErrorCode& status) const;
972 
990  void format(int32_t msgStart,
991  double pluralNumber,
992  const Formattable* arguments,
993  const UnicodeString *argumentNames,
994  int32_t cnt,
995  AppendableWrapper& appendTo,
996  FieldPosition* pos,
997  UErrorCode& success) const;
998 
999  UnicodeString getArgName(int32_t partIndex);
1000 
1001  void setArgStartFormat(int32_t argStart, Format* formatter, UErrorCode& status);
1002 
1003  void setCustomArgStartFormat(int32_t argStart, Format* formatter, UErrorCode& status);
1004 
1005  int32_t nextTopLevelArgStart(int32_t partIndex) const;
1006 
1007  UBool argNameMatches(int32_t partIndex, const UnicodeString& argName, int32_t argNumber);
1008 
1009  void cacheExplicitFormats(UErrorCode& status);
1010 
1011  Format* createAppropriateFormat(UnicodeString& type,
1012  UnicodeString& style,
1013  Formattable::Type& formattableType,
1014  UParseError& parseError,
1015  UErrorCode& ec);
1016 
1017  const Formattable* getArgFromListByName(const Formattable* arguments,
1018  const UnicodeString *argumentNames,
1019  int32_t cnt, UnicodeString& name) const;
1020 
1021  Formattable* parse(int32_t msgStart,
1022  const UnicodeString& source,
1023  ParsePosition& pos,
1024  int32_t& count,
1025  UErrorCode& ec) const;
1026 
1027  FieldPosition* updateMetaData(AppendableWrapper& dest, int32_t prevLength,
1028  FieldPosition* fp, const Formattable* argId) 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  double pluralNumber,
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 
1057  const Formattable::Type* getArgTypeList(int32_t& listCount) const {
1058  listCount = argTypeCount;
1059  return argTypes;
1060  }
1061 
1065  void resetPattern();
1066 
1072  class U_I18N_API DummyFormat : public Format {
1073  public:
1074  virtual UBool operator==(const Format&) const;
1075  virtual Format* clone() const;
1076  virtual UnicodeString& format(const Formattable& obj,
1077  UnicodeString& appendTo,
1078  UErrorCode& status) const;
1079  virtual UnicodeString& format(const Formattable&,
1080  UnicodeString& appendTo,
1081  FieldPosition&,
1082  UErrorCode& status) const;
1083  virtual UnicodeString& format(const Formattable& obj,
1084  UnicodeString& appendTo,
1085  FieldPositionIterator* posIter,
1086  UErrorCode& status) const;
1087  virtual void parseObject(const UnicodeString&,
1088  Formattable&,
1089  ParsePosition&) const;
1090  virtual UClassID getDynamicClassID() const;
1091  };
1092 
1093  friend class MessageFormatAdapter; // getFormatTypeList() access
1094 };
1095 
1096 inline UnicodeString&
1097 MessageFormat::format(const Formattable& obj,
1098  UnicodeString& appendTo,
1099  UErrorCode& status) const {
1100  return Format::format(obj, appendTo, status);
1101 }
1102 
1103 
1105 
1106 #endif /* #if !UCONFIG_NO_FORMATTING */
1107 
1108 #endif // _MSGFMT
1109 //eof
Base class for all formats.
Definition: format.h:94
void copyObjects(const PluralFormat &other)
Copies dynamically allocated values (pointer fields).
virtual UClassID getDynamicClassID() const =0
ICU4C "poor man's RTTI", returns a UClassID for the actual ICU class.
Locale getLocale(ULocDataLocaleType type, UErrorCode &status) const
Get the locale for this format object.
U_EXPORT UBool operator==(const StringPiece &x, const StringPiece &y)
Global operator == for StringPiece.
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:55
virtual Format * clone() const =0
Clone this object polymorphically.
UnicodeString format(int32_t number, UErrorCode &status) const
Formats a plural message for a given number.
C++ API: PluralRules object.
EFormatNumber
Enum type for kMaxFormat.
Definition: msgfmt.h:331
Defines rules for mapping non-negative numeric values onto a small set of keywords.
Definition: plurrule.h:127
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:82
#define U_I18N_API
Set to export library symbols from inside the i18n library, and to import them from outside...
Definition: utypes.h:358
#define U_NAMESPACE_BEGIN
This is used to begin a declaration of a public ICU C++ API.
Definition: uversion.h:129
UnicodeString & format(const Formattable &obj, UnicodeString &appendTo, UErrorCode &status) const
Formats an object to produce a string.
UMessagePatternApostropheMode getApostropheMode() const
Definition: msgfmt.h:478
uint16_t UChar
Define UChar to be UCHAR_TYPE, if that is #defined (for example, to char16_t), or wchar_t if that is ...
Definition: umachine.h:278
#define U_CDECL_END
This is used to end a declaration of a library private ICU C API.
Definition: umachine.h:83
#define U_NAMESPACE_END
This is used to end a declaration of a public ICU C++ API.
Definition: uversion.h:130
C API: Parse Error Information.
UErrorCode
Error code to replace exception handling, so that the code is compatible with all C++ compilers...
Definition: utypes.h:476
FieldPosition is a simple class used by Format and its subclasses to identify fields in formatted out...
Definition: fieldpos.h:106
UPluralType
Type of plurals and PluralRules.
Definition: upluralrules.h:44
ParsePosition is a simple class used by Format and its subclasses to keep track of the current positi...
Definition: parsepos.h:47
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:56
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:246
Formattable objects can be passed to the Format class or its subclasses for formatting.
Definition: fmtable.h:62
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:200
A Locale object represents a specific geographical, political, or cultural region.
Definition: locid.h:182