ICU 64.2  64.2
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 
39 // Forward declaration.
40 struct UHashtable;
41 typedef struct UHashtable UHashtable;
43 
45 
46 class AppendableWrapper;
47 class DateFormat;
48 class NumberFormat;
49 
344 public:
345 #ifndef U_HIDE_OBSOLETE_API
346 
357  kMaxFormat = 10
358  };
359 #endif /* U_HIDE_OBSOLETE_API */
360 
370  MessageFormat(const UnicodeString& pattern,
371  UErrorCode &status);
372 
381  MessageFormat(const UnicodeString& pattern,
382  const Locale& newLocale,
383  UErrorCode& status);
394  MessageFormat(const UnicodeString& pattern,
395  const Locale& newLocale,
396  UParseError& parseError,
397  UErrorCode& status);
403 
408  const MessageFormat& operator=(const MessageFormat&);
409 
414  virtual ~MessageFormat();
415 
421  virtual Format* clone(void) const;
422 
430  virtual UBool operator==(const Format& other) const;
431 
437  virtual void setLocale(const Locale& theLocale);
438 
445  virtual const Locale& getLocale(void) const;
446 
455  virtual void applyPattern(const UnicodeString& pattern,
456  UErrorCode& status);
467  virtual void applyPattern(const UnicodeString& pattern,
468  UParseError& parseError,
469  UErrorCode& status);
470 
489  virtual void applyPattern(const UnicodeString& pattern,
491  UParseError* parseError,
492  UErrorCode& status);
493 
499  return msgPattern.getApostropheMode();
500  }
501 
510  virtual UnicodeString& toPattern(UnicodeString& appendTo) const;
511 
528  virtual void adoptFormats(Format** formatsToAdopt, int32_t count);
529 
544  virtual void setFormats(const Format** newFormats, int32_t cnt);
545 
546 
561  virtual void adoptFormat(int32_t formatNumber, Format* formatToAdopt);
562 
572  virtual void setFormat(int32_t formatNumber, const Format& format);
573 
582  virtual StringEnumeration* getFormatNames(UErrorCode& status);
583 
597  virtual Format* getFormat(const UnicodeString& formatName, UErrorCode& status);
598 
612  virtual void setFormat(const UnicodeString& formatName, const Format& format, UErrorCode& status);
613 
628  virtual void adoptFormat(const UnicodeString& formatName, Format* formatToAdopt, UErrorCode& status);
629 
642  virtual const Format** getFormats(int32_t& count) const;
643 
644 
645  using Format::format;
646 
664  UnicodeString& format(const Formattable* source,
665  int32_t count,
666  UnicodeString& appendTo,
667  FieldPosition& ignore,
668  UErrorCode& status) const;
669 
687  static UnicodeString& format(const UnicodeString& pattern,
688  const Formattable* arguments,
689  int32_t count,
690  UnicodeString& appendTo,
691  UErrorCode& status);
692 
713  virtual UnicodeString& format(const Formattable& obj,
714  UnicodeString& appendTo,
715  FieldPosition& pos,
716  UErrorCode& status) const;
717 
736  UnicodeString& format(const UnicodeString* argumentNames,
737  const Formattable* arguments,
738  int32_t count,
739  UnicodeString& appendTo,
740  UErrorCode& status) const;
754  virtual Formattable* parse(const UnicodeString& source,
755  ParsePosition& pos,
756  int32_t& count) const;
757 
773  virtual Formattable* parse(const UnicodeString& source,
774  int32_t& count,
775  UErrorCode& status) const;
776 
789  virtual void parseObject(const UnicodeString& source,
790  Formattable& result,
791  ParsePosition& pos) const;
792 
812  static UnicodeString autoQuoteApostrophe(const UnicodeString& pattern,
813  UErrorCode& status);
814 
815 
823  UBool usesNamedArguments() const;
824 
825 
826 #ifndef U_HIDE_INTERNAL_API
827 
837  int32_t getArgTypeCount() const;
838 #endif /* U_HIDE_INTERNAL_API */
839 
851  virtual UClassID getDynamicClassID(void) const;
852 
864  static UClassID U_EXPORT2 getStaticClassID(void);
865 
866 #ifndef U_HIDE_INTERNAL_API
867 
877  static UBool equalFormats(const void* left, const void* right);
878 #endif /* U_HIDE_INTERNAL_API */
879 
880 private:
881 
882  Locale fLocale;
883  MessagePattern msgPattern;
884  Format** formatAliases; // see getFormats
885  int32_t formatAliasesCapacity;
886 
887  MessageFormat(); // default constructor not implemented
888 
895  class U_I18N_API PluralSelectorProvider : public PluralFormat::PluralSelector {
896  public:
897  PluralSelectorProvider(const MessageFormat &mf, UPluralType type);
898  virtual ~PluralSelectorProvider();
899  virtual UnicodeString select(void *ctx, double number, UErrorCode& ec) const;
900 
901  void reset();
902  private:
903  const MessageFormat &msgFormat;
904  PluralRules* rules;
905  UPluralType type;
906  };
907 
916  Formattable::Type* argTypes;
917  int32_t argTypeCount;
918  int32_t argTypeCapacity;
919 
925  UBool hasArgTypeConflicts;
926 
927  // Variable-size array management
928  UBool allocateArgTypes(int32_t capacity, UErrorCode& status);
929 
937  NumberFormat* defaultNumberFormat;
938  DateFormat* defaultDateFormat;
939 
940  UHashtable* cachedFormatters;
941  UHashtable* customFormatArgStarts;
942 
943  PluralSelectorProvider pluralProvider;
944  PluralSelectorProvider ordinalProvider;
945 
950  const NumberFormat* getDefaultNumberFormat(UErrorCode&) const;
951  const DateFormat* getDefaultDateFormat(UErrorCode&) const;
952 
959  static int32_t findKeyword( const UnicodeString& s,
960  const char16_t * const *list);
961 
967  UnicodeString& format(const Formattable* arguments,
968  const UnicodeString *argumentNames,
969  int32_t cnt,
970  UnicodeString& appendTo,
971  FieldPosition* pos,
972  UErrorCode& status) const;
973 
991  void format(int32_t msgStart,
992  const void *plNumber,
993  const Formattable* arguments,
994  const UnicodeString *argumentNames,
995  int32_t cnt,
996  AppendableWrapper& appendTo,
997  FieldPosition* pos,
998  UErrorCode& success) const;
999 
1000  UnicodeString getArgName(int32_t partIndex);
1001 
1002  void setArgStartFormat(int32_t argStart, Format* formatter, UErrorCode& status);
1003 
1004  void setCustomArgStartFormat(int32_t argStart, Format* formatter, UErrorCode& status);
1005 
1006  int32_t nextTopLevelArgStart(int32_t partIndex) const;
1007 
1008  UBool argNameMatches(int32_t partIndex, const UnicodeString& argName, int32_t argNumber);
1009 
1010  void cacheExplicitFormats(UErrorCode& status);
1011 
1012  int32_t skipLeadingSpaces(UnicodeString& style);
1013 
1014  Format* createAppropriateFormat(UnicodeString& type,
1015  UnicodeString& style,
1016  Formattable::Type& formattableType,
1017  UParseError& parseError,
1018  UErrorCode& ec);
1019 
1020  const Formattable* getArgFromListByName(const Formattable* arguments,
1021  const UnicodeString *argumentNames,
1022  int32_t cnt, UnicodeString& name) const;
1023 
1024  Formattable* parse(int32_t msgStart,
1025  const UnicodeString& source,
1026  ParsePosition& pos,
1027  int32_t& count,
1028  UErrorCode& ec) const;
1029 
1030  FieldPosition* updateMetaData(AppendableWrapper& dest, int32_t prevLength,
1031  FieldPosition* fp, const Formattable* argId) const;
1032 
1038  int32_t findOtherSubMessage(int32_t partIndex) const;
1039 
1045  int32_t findFirstPluralNumberArg(int32_t msgStart, const UnicodeString &argName) const;
1046 
1047  Format* getCachedFormatter(int32_t argumentNumber) const;
1048 
1049  UnicodeString getLiteralStringUntilNextArgument(int32_t from) const;
1050 
1051  void copyObjects(const MessageFormat& that, UErrorCode& ec);
1052 
1053  void formatComplexSubMessage(int32_t msgStart,
1054  const void *plNumber,
1055  const Formattable* arguments,
1056  const UnicodeString *argumentNames,
1057  int32_t cnt,
1058  AppendableWrapper& appendTo,
1059  UErrorCode& success) const;
1060 
1064  NumberFormat* createIntegerFormat(const Locale& locale, UErrorCode& status) const;
1065 
1073  const Formattable::Type* getArgTypeList(int32_t& listCount) const {
1074  listCount = argTypeCount;
1075  return argTypes;
1076  }
1077 
1081  void resetPattern();
1082 
1087  class U_I18N_API DummyFormat : public Format {
1088  public:
1089  virtual UBool operator==(const Format&) const;
1090  virtual Format* clone() const;
1091  virtual UnicodeString& format(const Formattable& obj,
1092  UnicodeString& appendTo,
1093  UErrorCode& status) const;
1094  virtual UnicodeString& format(const Formattable&,
1095  UnicodeString& appendTo,
1096  FieldPosition&,
1097  UErrorCode& status) const;
1098  virtual UnicodeString& format(const Formattable& obj,
1099  UnicodeString& appendTo,
1100  FieldPositionIterator* posIter,
1101  UErrorCode& status) const;
1102  virtual void parseObject(const UnicodeString&,
1103  Formattable&,
1104  ParsePosition&) const;
1105  };
1106 
1107  friend class MessageFormatAdapter; // getFormatTypeList() access
1108 };
1109 
1111 
1112 #endif /* #if !UCONFIG_NO_FORMATTING */
1113 
1114 #endif // _MSGFMT
1115 //eof
Base class for all formats.
Definition: format.h:96
struct UHashtable UHashtable
Definition: msgfmt.h:41
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:219
void * UClassID
UClassID is used to identify classes without using the compiler's RTTI.
Definition: uobject.h:93
C++ API: PluralFormat object.
Base class for 'pure' C++ implementations of uenum api.
Definition: strenum.h:57
virtual Format * clone() const =0
Clone this object polymorphically.
C++ API: PluralRules object.
EFormatNumber
Enum type for kMaxFormat.
Definition: msgfmt.h:351
Defines rules for mapping non-negative numeric values onto a small set of keywords.
Definition: plurrule.h:198
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
#define U_NAMESPACE_BEGIN
This is used to begin a declaration of a public ICU C++ API.
Definition: uversion.h:137
FieldPositionIterator returns the field ids and their start/limit positions generated by a call to Fo...
Definition: fpositer.h:55
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
#define U_NAMESPACE_END
This is used to end a declaration of a public ICU C++ API.
Definition: uversion.h:138
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:401
FieldPosition is a simple class used by Format and its subclasses to identify fields in formatted out...
Definition: fieldpos.h:108
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:49
DateFormat is an abstract class for a family of classes that convert dates and times from their inter...
Definition: datefmt.h:154
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:498
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:289
Formattable objects can be passed to the Format class or its subclasses for formatting.
Definition: fmtable.h:62
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:225
A Locale object represents a specific geographical, political, or cultural region.
Definition: locid.h:192