X-Git-Url: https://git.saurik.com/apple/icu.git/blobdiff_plain/729e4ab9bc6618bc3d8a898e575df7f4019e29ca..340931cb2e044a2141d11567dd0f782524e32994:/icuSources/i18n/unicode/msgfmt.h diff --git a/icuSources/i18n/unicode/msgfmt.h b/icuSources/i18n/unicode/msgfmt.h index 58c74f1c..99b0eaee 100644 --- a/icuSources/i18n/unicode/msgfmt.h +++ b/icuSources/i18n/unicode/msgfmt.h @@ -1,5 +1,7 @@ +// © 2016 and later: Unicode, Inc. and others. +// License & terms of use: http://www.unicode.org/copyright.html /* -* Copyright (C) 2007-2010, International Business Machines Corporation and +* Copyright (C) 2007-2013, International Business Machines Corporation and * others. All Rights Reserved. ******************************************************************************** * @@ -19,6 +21,8 @@ #include "unicode/utypes.h" +#if U_SHOW_CPLUSPLUS_API + /** * \file * \brief C++ API: Formats messages in a language-neutral way. @@ -28,106 +32,236 @@ #include "unicode/format.h" #include "unicode/locid.h" +#include "unicode/messagepattern.h" #include "unicode/parseerr.h" -#include "unicode/uchar.h" +#include "unicode/plurfmt.h" +#include "unicode/plurrule.h" + +U_CDECL_BEGIN +// Forward declaration. +struct UHashtable; +typedef struct UHashtable UHashtable; /**< @internal */ +U_CDECL_END U_NAMESPACE_BEGIN -class NumberFormat; +class AppendableWrapper; class DateFormat; +class NumberFormat; /** + *

MessageFormat prepares strings for display to users, + * with optional arguments (variables/placeholders). + * The arguments can occur in any order, which is necessary for translation + * into languages with different grammars. + * + *

A MessageFormat is constructed from a pattern string + * with arguments in {curly braces} which will be replaced by formatted values. + * + *

MessageFormat differs from the other Format + * classes in that you create a MessageFormat object with one + * of its constructors (not with a createInstance style factory + * method). Factory methods aren't necessary because MessageFormat + * itself doesn't implement locale-specific behavior. Any locale-specific + * behavior is defined by the pattern that you provide and the + * subformats used for inserted arguments. + * + *

Arguments can be named (using identifiers) or numbered (using small ASCII-digit integers). + * Some of the API methods work only with argument numbers and throw an exception + * if the pattern has named arguments (see {@link #usesNamedArguments()}). + * + *

An argument might not specify any format type. In this case, + * a numeric value is formatted with a default (for the locale) NumberFormat, + * and a date/time value is formatted with a default (for the locale) DateFormat. + * + *

An argument might specify a "simple" type for which the specified + * Format object is created, cached and used. + * + *

An argument might have a "complex" type with nested MessageFormat sub-patterns. + * During formatting, one of these sub-messages is selected according to the argument value + * and recursively formatted. + * + *

After construction, a custom Format object can be set for + * a top-level argument, overriding the default formatting and parsing behavior + * for that argument. + * However, custom formatting can be achieved more simply by writing + * a typeless argument in the pattern string + * and supplying it with a preformatted string value. + * + *

When formatting, MessageFormat takes a collection of argument values + * and writes an output string. + * The argument values may be passed as an array + * (when the pattern contains only numbered arguments) + * or as an array of names and and an array of arguments (which works for both named + * and numbered arguments). + * + *

Each argument is matched with one of the input values by array index or argument name + * and formatted according to its pattern specification + * (or using a custom Format object if one was set). + * A numbered pattern argument is matched with an argument name that contains that number + * as an ASCII-decimal-digit string (without leading zero). + * + *

Patterns and Their Interpretation

+ * + * MessageFormat uses patterns of the following form: + *
+ * message = messageText (argument messageText)*
+ * argument = noneArg | simpleArg | complexArg
+ * complexArg = choiceArg | pluralArg | selectArg | selectordinalArg
+ *
+ * noneArg = '{' argNameOrNumber '}'
+ * simpleArg = '{' argNameOrNumber ',' argType [',' argStyle] '}'
+ * choiceArg = '{' argNameOrNumber ',' "choice" ',' choiceStyle '}'
+ * pluralArg = '{' argNameOrNumber ',' "plural" ',' pluralStyle '}'
+ * selectArg = '{' argNameOrNumber ',' "select" ',' selectStyle '}'
+ * selectordinalArg = '{' argNameOrNumber ',' "selectordinal" ',' pluralStyle '}'
+ *
+ * choiceStyle: see {@link ChoiceFormat}
+ * pluralStyle: see {@link PluralFormat}
+ * selectStyle: see {@link SelectFormat}
  *
- * MessageFormat produces concatenated messages in a language-neutral
- * way.  Use this whenever concatenating strings that are displayed to
- * end users.
- *
- * 

A MessageFormat contains an array of subformats arranged - * within a template string. Together, the subformats and - * template string determine how the MessageFormat will operate during - * formatting and parsing. - * - *

Typically, both the subformats and the template string are - * specified at once in a pattern. By using different - * patterns for different locales, messages may be localized. - * - *

When formatting, MessageFormat takes an array of arguments - * and produces a user-readable string. Each argument is a - * Formattable object; they may be passed in in an array, or as a - * single Formattable object which itself contains an array. Each - * argument is matched up with its corresponding subformat, which then - * formats it into a string. The resulting strings are then assembled - * within the string template of the MessageFormat to produce the - * final output string. - * - *

Note: - * In ICU 4.0 MessageFormat supports named arguments. If a named argument - * is used, all arguments must be named. Names start with a character in - * UCHAR_ID_START and continue with characters in - * UCHARID_CONTINUE, in particular they do not start with a digit. - * If named arguments are used, {@link #usesNamedArguments()} will return true. - * - *

The other new methods supporting named arguments are - * {@link #getFormatNames(UErrorCode& status)}, - * {@link #getFormat(const UnicodeString& formatName, UErrorCode& status)} - * {@link #setFormat(const UnicodeString& formatName, const Format& format, UErrorCode& status)}, - * {@link #adoptFormat(const UnicodeString& formatName, Format* formatToAdopt, UErrorCode& status)}, - * {@link #format(const UnicodeString* argumentNames, const Formattable* arguments, - * int32_t count, UnicodeString& appendTo,UErrorCode& status)}. - * These methods are all compatible with patterns that do not used named arguments-- - * in these cases the keys in the input or output use UnicodeStrings - * that name the argument indices, e.g. "0", "1", "2"... etc. - * - *

If this format uses named arguments, certain methods that take or - * return arrays do not perform any action, since it is not possible to - * identify positions in an array using a name. Of these methods, - * UErrorCode is set to U_ILLEGAL_ARGUMENT_ERROR by format, and to - * U_ARGUMENT_TYPE_MISMATCH by parse. - * These methods are - * {@link #adoptFormats(Format** formatsToAdopt, int32_t count)}, - * {@link #setFormats(const Format** newFormats,int32_t count)}, - * {@link #adoptFormat(int32_t n, Format *newFormat)}, - * {@link #setFormat(int32_t n, Format& newFormat)}, - * {@link #format(const Formattable* source, int32_t count, UnicodeString& appendTo, FieldPosition& ignore, UErrorCode& success)}, - * {@link #format(const UnicodeString& pattern,const Formattable* arguments,int32_t cnt,UnicodeString& appendTo,UErrorCode& success)}, - * {@link #format(const Formattable& source, UnicodeString& appendTo, FieldPosition& ignore, UErrorCode& success)}, - * {@link #format(const Formattable* arguments, int32_t cnt, UnicodeString& appendTo, FieldPosition& status, int32_t recursionProtection,UErrorCode& success)}, - * {@link #parse(const UnicodeString& source, ParsePosition& pos, int32_t& count)}, - * {@link #parse(const UnicodeString& source, int32_t& cnt, UErrorCode& status)} - * - *

- * During parsing, an input string is matched against the string - * template of the MessageFormat to produce an array of Formattable - * objects. Plain text of the template string is matched directly - * against input text. At each position in the template string where - * a subformat is located, the subformat is called to parse the - * corresponding segment of input text to produce an output argument. - * In this way, an array of arguments is created which together - * constitute the parse result. - *

- * Parsing may fail or produce unexpected results in a number of - * circumstances. - *

- * Here are some examples of usage: - *

+ * argNameOrNumber = argName | argNumber + * argName = [^[[:Pattern_Syntax:][:Pattern_White_Space:]]]+ + * argNumber = '0' | ('1'..'9' ('0'..'9')*) + * + * argType = "number" | "date" | "time" | "spellout" | "ordinal" | "duration" + * argStyle = "short" | "medium" | "long" | "full" | "integer" | "currency" | "percent" | argStyleText | "::" argSkeletonText + *

+ * + * + * + *

Recommendation: Use the real apostrophe (single quote) character + * \htmlonly’\endhtmlonly (U+2019) for + * human-readable text, and use the ASCII apostrophe ' (U+0027) + * only in program syntax, like quoting in MessageFormat. + * See the annotations for U+0027 Apostrophe in The Unicode Standard. + * + *

The choice argument type is deprecated. + * Use plural arguments for proper plural selection, + * and select arguments for simple selection among a fixed set of choices. + * + *

The argType and argStyle values are used to create + * a Format instance for the format element. The following + * table shows how the values map to Format instances. Combinations not + * shown in the table are illegal. Any argStyleText must + * be a valid pattern string for the Format subclass used. + * + *

+ * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + *
argType + * argStyle + * resulting Format object + *
(none) + * null + *
number + * (none) + * NumberFormat.createInstance(getLocale(), status) + *
integer + * NumberFormat.createInstance(getLocale(), kNumberStyle, status) + *
currency + * NumberFormat.createCurrencyInstance(getLocale(), status) + *
percent + * NumberFormat.createPercentInstance(getLocale(), status) + *
argStyleText + * new DecimalFormat(argStyleText, new DecimalFormatSymbols(getLocale(), status), status) + *
argSkeletonText + * NumberFormatter::forSkeleton(argSkeletonText, status).locale(getLocale()).toFormat(status) + *
date + * (none) + * DateFormat.createDateInstance(kDefault, getLocale(), status) + *
short + * DateFormat.createDateInstance(kShort, getLocale(), status) + *
medium + * DateFormat.createDateInstance(kDefault, getLocale(), status) + *
long + * DateFormat.createDateInstance(kLong, getLocale(), status) + *
full + * DateFormat.createDateInstance(kFull, getLocale(), status) + *
argStyleText + * new SimpleDateFormat(argStyleText, getLocale(), status) + *
argSkeletonText + * DateFormat::createInstanceForSkeleton(argSkeletonText, getLocale(), status) + *
time + * (none) + * DateFormat.createTimeInstance(kDefault, getLocale(), status) + *
short + * DateFormat.createTimeInstance(kShort, getLocale(), status) + *
medium + * DateFormat.createTimeInstance(kDefault, getLocale(), status) + *
long + * DateFormat.createTimeInstance(kLong, getLocale(), status) + *
full + * DateFormat.createTimeInstance(kFull, getLocale(), status) + *
argStyleText + * new SimpleDateFormat(argStyleText, getLocale(), status) + *
spellout + * argStyleText (optional) + * new RuleBasedNumberFormat(URBNF_SPELLOUT, getLocale(), status) + *
    .setDefaultRuleset(argStyleText, status);
+ *
ordinal + * argStyleText (optional) + * new RuleBasedNumberFormat(URBNF_ORDINAL, getLocale(), status) + *
    .setDefaultRuleset(argStyleText, status);
+ *
duration + * argStyleText (optional) + * new RuleBasedNumberFormat(URBNF_DURATION, getLocale(), status) + *
    .setDefaultRuleset(argStyleText, status);
+ *
+ *

+ * + *

Argument formatting

+ * + *

Arguments are formatted according to their type, using the default + * ICU formatters for those types, unless otherwise specified.

+ * + *

There are also several ways to control the formatting.

+ * + *

We recommend you use default styles, predefined style values, skeletons, + * or preformatted values, but not pattern strings or custom format objects.

+ * + *

For more details, see the + * ICU User Guide.

+ * + *

Usage Information

+ * + *

Here are some examples of usage: * Example 1: + * *

  * \code
  *     UErrorCode success = U_ZERO_ERROR;
@@ -140,18 +274,20 @@ class DateFormat;
  *
  *     UnicodeString result;
  *     MessageFormat::format(
- *          "At {1,time} on {1,date}, there was {2} on planet {0,number}.",
+ *          "At {1,time,::jmm} on {1,date,::dMMMM}, there was {2} on planet {0,number}.",
  *          arguments, 3, result, success );
  *
  *     cout << "result: " << result << endl;
- *     //: At 4:34:20 PM on 23-Mar-98, there was a disturbance
+ *     //: At 4:34 PM on March 23, there was a disturbance
  *     //             in the Force on planet 7.
  * \endcode
  * 
+ * * Typically, the message format will come from resources, and the * arguments will be dynamically set at runtime. - *

- * Example 2: + * + *

Example 2: + * *

  *  \code
  *     success = U_ZERO_ERROR;
@@ -171,125 +307,44 @@ class DateFormat;
  *  \endcode
  *  
* - * The pattern is of the following form. Legend: - *
- * \code
- *       {optional item}
- *       (group that may be repeated)*
- * \endcode
- *  
- * Do not confuse optional items with items inside quoted braces, such - * as this: "{". Quoted braces are literals. - *
- *  \code
- *       messageFormatPattern := string ( "{" messageFormatElement "}" string )*
- *
- *       messageFormatElement := argumentIndex | argumentName { "," elementFormat }
- *
- *       elementFormat := "time" { "," datetimeStyle }
- *                      | "date" { "," datetimeStyle }
- *                      | "number" { "," numberStyle }
- *                      | "choice" "," choiceStyle
- *                      | "spellout" { "," spelloutStyle }
- *                      | "ordinal" { "," spelloutStyle }
- *                      | "duration" { "," spelloutStyle }
- *                      | "plural" "," pluralStyle
- *                      | "select" "," selectStyle
- *
- *       datetimeStyle := "short"
- *                      | "medium"
- *                      | "long"
- *                      | "full"
- *                      | dateFormatPattern
  *
- *       numberStyle :=   "currency"
- *                      | "percent"
- *                      | "integer"
- *                      | numberFormatPattern
- *
- *       choiceStyle :=   choiceFormatPattern
- *
- *       pluralStyle :=   pluralFormatPattern
- *
- *       selectStyle :=   selectFormatPattern
- *
- *       spelloutStyle := ruleSetName
- * \endcode
- * 
- * If there is no elementFormat, then the argument must be a string, - * which is substituted. If there is no dateTimeStyle or numberStyle, - * then the default format is used (e.g. NumberFormat::createInstance(), - * DateFormat::createTimeInstance(DateFormat::kDefault, ...) or - * DateFormat::createDateInstance(DateFormat::kDefault, ...). For - * a RuleBasedNumberFormat, if there is no ruleSetName, the default - * rule set is used. For a ChoiceFormat or PluralFormat or SelectFormat, the pattern - * must always be specified, since there is no default. - *

- * In strings, single quotes can be used to quote syntax characters. - * A literal single quote is represented by '', both within and outside - * of single-quoted segments. Inside a - * messageFormatElement, quotes are not removed. For example, - * {1,number,$'#',##} will produce a number format with the pound-sign - * quoted, with a result such as: "$#31,45". - *

- * If a pattern is used, then unquoted braces in the pattern, if any, - * must match: that is, "ab {0} de" and "ab '}' de" are ok, but "ab - * {0'}' de" and "ab } de" are not. - *

- *

Warning:
The rules for using quotes within message - * format patterns unfortunately have shown to be somewhat confusing. - * In particular, it isn't always obvious to localizers whether single - * quotes need to be doubled or not. Make sure to inform localizers about - * the rules, and tell them (for example, by using comments in resource - * bundle source files) which strings will be processed by MessageFormat. - * Note that localizers may need to use single quotes in translated - * strings where the original version doesn't have them. - *
Note also that the simplest way to avoid the problem is to - * use the real apostrophe (single quote) character U+2019 (') for - * human-readable text, and to use the ASCII apostrophe (U+0027 ' ) - * only in program syntax, like quoting in MessageFormat. - * See the annotations for U+0027 Apostrophe in The Unicode Standard.

- *
- *

- * The argumentIndex is a non-negative integer, which corresponds to the - * index of the arguments presented in an array to be formatted. The - * first argument has argumentIndex 0. - *

- * It is acceptable to have unused arguments in the array. With missing - * arguments, or arguments that are not of the right class for the - * specified format, a failing UErrorCode result is set. - *

- * Creating internationalized messages that include plural forms, you - * can use a PluralFormat: + *

For messages that include plural forms, you can use a plural argument: *

  * \code
- *  UErrorCode err = U_ZERO_ERROR;
- *  UnicodeString t1("{0, plural, one{C''est # fichier} other{Ce sont # fichiers}} dans la liste.");
- *  MessageFormat* msgFmt = new MessageFormat(t1, Locale("fr"), err);
- *  if (U_FAILURE(err)) {
- *      return err;
- *  }
- *
- *  Formattable args1[] = {(int32_t)0};
- *  Formattable args2[] = {(int32_t)3};
- *  FieldPosition ignore(FieldPosition::DONT_CARE);
+ *  success = U_ZERO_ERROR;
+ *  MessageFormat msgFmt(
+ *       "{num_files, plural, "
+ *       "=0{There are no files on disk \"{disk_name}\".}"
+ *       "=1{There is one file on disk \"{disk_name}\".}"
+ *       "other{There are # files on disk \"{disk_name}\".}}",
+ *      Locale("en"),
+ *      success);
+ *  FieldPosition fpos = 0;
+ *  Formattable testArgs[] = {0L, "MyDisk"};
+ *  UnicodeString testArgsNames[] = {"num_files", "disk_name"};
  *  UnicodeString result;
- *  msgFmt->format(args1, 1, result, ignore, status);
- *  cout << result << endl;
- *  result.remove();
- *  msgFmt->format(args2, 1, result, ignore, status);
- *  cout << result << endl;
- *
- *  // output, with different args
- *  // output: C'est 0,0 fichier dans la liste.
- *  // output: Ce sont 3 fichiers dans la liste."
+ *  cout << msgFmt.format(testArgs, testArgsNames, 2, result, fpos, 0, success);
+ *  testArgs[0] = 3L;
+ *  cout << msgFmt.format(testArgs, testArgsNames, 2, result, fpos, 0, success);
  * \endcode
+ * output:
+ * There are no files on disk "MyDisk".
+ * There are 3 files on "MyDisk".
  * 
- * Please check PluralFormat and PluralRules for details. - *

+ * See {@link PluralFormat} and {@link PluralRules} for details. + * + *

Synchronization

+ * + *

MessageFormats are not synchronized. + * It is recommended to create separate format instances for each thread. + * If multiple threads access a format concurrently, it must be synchronized + * externally. + * + * @stable ICU 2.0 */ class U_I18N_API MessageFormat : public Format { public: +#ifndef U_HIDE_OBSOLETE_API /** * Enum type for kMaxFormat. * @obsolete ICU 3.0. The 10-argument limit was removed as of ICU 2.6, @@ -303,6 +358,7 @@ public: */ kMaxFormat = 10 }; +#endif /* U_HIDE_OBSOLETE_API */ /** * Constructs a new MessageFormat using the given pattern and the @@ -331,8 +387,8 @@ public: * Constructs a new MessageFormat using the given pattern and locale. * @param pattern Pattern used to construct object. * @param newLocale The locale to use for formatting dates and numbers. - * @param parseError Struct to recieve information on position - * of error within the pattern. + * @param parseError Struct to receive information on the position + * of an error within the pattern. * @param status Input/output error code. If the * pattern cannot be parsed, set to failure code. * @stable ICU 2.0 @@ -364,7 +420,7 @@ public: * result and should delete it when done. * @stable ICU 2.0 */ - virtual Format* clone(void) const; + virtual MessageFormat* clone() const; /** * Returns true if the given Format objects are semantically equal. @@ -376,15 +432,14 @@ public: virtual UBool operator==(const Format& other) const; /** - * Sets the locale. This locale is used for fetching default number or date - * format information. + * Sets the locale to be used for creating argument Format objects. * @param theLocale the new locale value to be set. * @stable ICU 2.0 */ virtual void setLocale(const Locale& theLocale); /** - * Gets the locale. This locale is used for fetching default number or date + * Gets the locale used for creating argument Format objects. * format information. * @return the locale of the object. * @stable ICU 2.0 @@ -405,8 +460,8 @@ public: * Applies the given pattern string to this message format. * * @param pattern The pattern to be applied. - * @param parseError Struct to recieve information on position - * of error within pattern. + * @param parseError Struct to receive information on the position + * of an error within the pattern. * @param status Input/output error code. If the * pattern cannot be parsed, set to failure code. * @stable ICU 2.0 @@ -415,6 +470,37 @@ public: UParseError& parseError, UErrorCode& status); + /** + * Sets the UMessagePatternApostropheMode and the pattern used by this message format. + * Parses the pattern and caches Format objects for simple argument types. + * Patterns and their interpretation are specified in the + * class description. + *

+ * This method is best used only once on a given object to avoid confusion about the mode, + * and after constructing the object with an empty pattern string to minimize overhead. + * + * @param pattern The pattern to be applied. + * @param aposMode The new apostrophe mode. + * @param parseError Struct to receive information on the position + * of an error within the pattern. + * Can be NULL. + * @param status Input/output error code. If the + * pattern cannot be parsed, set to failure code. + * @stable ICU 4.8 + */ + virtual void applyPattern(const UnicodeString& pattern, + UMessagePatternApostropheMode aposMode, + UParseError* parseError, + UErrorCode& status); + + /** + * @return this instance's UMessagePatternApostropheMode. + * @stable ICU 4.8 + */ + UMessagePatternApostropheMode getApostropheMode() const { + return msgPattern.getApostropheMode(); + } + /** * Returns a pattern that can be used to recreate this object. * @@ -490,7 +576,7 @@ public: /** * Gets format names. This function returns formatNames in StringEnumerations * which can be used with getFormat() and setFormat() to export formattable - * array from current MessageFormat to another. It is caller's resposibility + * array from current MessageFormat to another. It is the caller's responsibility * to delete the returned formatNames. * @param status output param set to success/failure code. * @stable ICU 4.0 @@ -500,7 +586,7 @@ public: /** * Gets subformat pointer for given format name. * This function supports both named and numbered - * arguments-- if numbered, the formatName is the + * arguments. If numbered, the formatName is the * corresponding UnicodeStrings (e.g. "0", "1", "2"...). * The returned Format object should not be deleted by the caller, * nor should the ponter of other object . The pointer and its @@ -631,25 +717,6 @@ public: FieldPosition& pos, UErrorCode& status) const; - /** - * Formats the given array of arguments into a user-readable - * string. The array must be stored within a single Formattable - * object of type kArray. If the Formattable object type is not of - * type kArray, then returns a failing UErrorCode. - * - * @param obj The object to format - * @param appendTo Output parameter to receive result. - * Result is appended to existing contents. - * @param status Input/output error code. If the - * pattern cannot be parsed, set to failure code. - * @return Reference to 'appendTo' parameter. - * @stable ICU 2.0 - */ - UnicodeString& format(const Formattable& obj, - UnicodeString& appendTo, - UErrorCode& status) const; - - /** * Formats the given array of arguments into a user-defined argument name * array. This function supports both named and numbered @@ -747,6 +814,7 @@ public: static UnicodeString autoQuoteApostrophe(const UnicodeString& pattern, UErrorCode& status); + /** * Returns true if this MessageFormat uses named arguments, * and false otherwise. See class description. @@ -757,6 +825,7 @@ public: UBool usesNamedArguments() const; +#ifndef U_HIDE_INTERNAL_API /** * This API is for ICU internal use only. * Please do not use it. @@ -768,6 +837,7 @@ public: * @internal */ int32_t getArgTypeCount() const; +#endif /* U_HIDE_INTERNAL_API */ /** * Returns a unique class ID POLYMORPHICALLY. Pure virtual override. @@ -795,33 +865,47 @@ public: */ static UClassID U_EXPORT2 getStaticClassID(void); +#ifndef U_HIDE_INTERNAL_API + /** + * Compares two Format objects. This is used for constructing the hash + * tables. + * + * @param left pointer to a Format object. Must not be NULL. + * @param right pointer to a Format object. Must not be NULL. + * + * @return whether the two objects are the same + * @internal + */ + static UBool equalFormats(const void* left, const void* right); +#endif /* U_HIDE_INTERNAL_API */ + private: Locale fLocale; - UnicodeString fPattern; + MessagePattern msgPattern; Format** formatAliases; // see getFormats int32_t formatAliasesCapacity; - UProperty idStart; - UProperty idContinue; MessageFormat(); // default constructor not implemented - /* - * A structure representing one subformat of this MessageFormat. - * Each subformat has a Format object, an offset into the plain - * pattern text fPattern, and an argument number. The argument - * number corresponds to the array of arguments to be formatted. - * @internal - */ - class Subformat; - - /** - * A MessageFormat contains an array of subformats. This array - * needs to grow dynamically if the MessageFormat is modified. - */ - Subformat* subformats; - int32_t subformatCount; - int32_t subformatCapacity; + /** + * This provider helps defer instantiation of a PluralRules object + * until we actually need to select a keyword. + * For example, if the number matches an explicit-value selector like "=1" + * we do not need any PluralRules. + */ + class U_I18N_API PluralSelectorProvider : public PluralFormat::PluralSelector { + public: + PluralSelectorProvider(const MessageFormat &mf, UPluralType type); + virtual ~PluralSelectorProvider(); + virtual UnicodeString select(void *ctx, double number, UErrorCode& ec) const; + + void reset(); + private: + const MessageFormat &msgFormat; + PluralRules* rules; + UPluralType type; + }; /** * A MessageFormat formats an array of arguments. Each argument @@ -836,14 +920,14 @@ private: int32_t argTypeCapacity; /** - * Is true iff all argument names are non-negative numbers. - * - */ - UBool isArgNumeric; + * TRUE if there are different argTypes for the same argument. + * This only matters when the MessageFormat is used in the plain C (umsg_xxx) API + * where the pattern argTypes determine how the va_arg list is read. + */ + UBool hasArgTypeConflicts; // Variable-size array management - UBool allocateSubformats(int32_t capacity); - UBool allocateArgTypes(int32_t capacity); + UBool allocateArgTypes(int32_t capacity, UErrorCode& status); /** * Default Format objects used when no format is specified and a @@ -855,6 +939,12 @@ private: NumberFormat* defaultNumberFormat; DateFormat* defaultDateFormat; + UHashtable* cachedFormatters; + UHashtable* customFormatArgStarts; + + PluralSelectorProvider pluralProvider; + PluralSelectorProvider ordinalProvider; + /** * Method to retrieve default formats (or NULL on failure). * These are semantically const, but may modify *this. @@ -869,59 +959,111 @@ private: * @return the index of the list which matches the keyword s. */ static int32_t findKeyword( const UnicodeString& s, - const UChar * const *list); + const char16_t * const *list); + + /** + * Thin wrapper around the format(... AppendableWrapper ...) variant. + * Wraps the destination UnicodeString into an AppendableWrapper and + * supplies default values for some other parameters. + */ + UnicodeString& format(const Formattable* arguments, + const UnicodeString *argumentNames, + int32_t cnt, + UnicodeString& appendTo, + FieldPosition* pos, + UErrorCode& status) const; /** - * Formats the array of arguments and copies the result into the - * result buffer, updates the field position. + * Formats the arguments and writes the result into the + * AppendableWrapper, updates the field position. * - * @param arguments The formattable objects array. - * @param cnt The array count. - * @param appendTo Output parameter to receive result. - * Result is appended to existing contents. - * @param status Field position status. - * @param recursionProtection - * Initially zero. Bits 0..9 are used to indicate - * that a parameter has already been seen, to - * avoid recursion. Currently unused. - * @param success The error code status. - * @return Reference to 'appendTo' parameter. + * @param msgStart Index to msgPattern part to start formatting from. + * @param plNumber NULL except when formatting a plural argument sub-message + * where a '#' is replaced by the format string for this number. + * @param arguments The formattable objects array. (Must not be NULL.) + * @param argumentNames NULL if numbered values are used. Otherwise the same + * length as "arguments", and each entry is the name of the + * corresponding argument in "arguments". + * @param cnt The length of arguments (and of argumentNames if that is not NULL). + * @param appendTo Output parameter to receive the result. + * The result string is appended to existing contents. + * @param pos Field position status. + * @param success The error code status. */ - UnicodeString& format( const Formattable* arguments, - int32_t cnt, - UnicodeString& appendTo, - FieldPosition& status, - int32_t recursionProtection, - UErrorCode& success) const; + void format(int32_t msgStart, + const void *plNumber, + const Formattable* arguments, + const UnicodeString *argumentNames, + int32_t cnt, + AppendableWrapper& appendTo, + FieldPosition* pos, + UErrorCode& success) const; + + UnicodeString getArgName(int32_t partIndex); + + void setArgStartFormat(int32_t argStart, Format* formatter, UErrorCode& status); + + void setCustomArgStartFormat(int32_t argStart, Format* formatter, UErrorCode& status); - UnicodeString& format( const Formattable* arguments, - const UnicodeString *argumentNames, - int32_t cnt, - UnicodeString& appendTo, - FieldPosition& status, - int32_t recursionProtection, - UErrorCode& success) const; + int32_t nextTopLevelArgStart(int32_t partIndex) const; - void makeFormat(int32_t offsetNumber, - UnicodeString* segments, - UParseError& parseError, - UErrorCode& success); + UBool argNameMatches(int32_t partIndex, const UnicodeString& argName, int32_t argNumber); + + void cacheExplicitFormats(UErrorCode& status); + + int32_t skipLeadingSpaces(UnicodeString& style); + + Format* createAppropriateFormat(UnicodeString& type, + UnicodeString& style, + Formattable::Type& formattableType, + UParseError& parseError, + UErrorCode& ec); + + const Formattable* getArgFromListByName(const Formattable* arguments, + const UnicodeString *argumentNames, + int32_t cnt, UnicodeString& name) const; + + Formattable* parse(int32_t msgStart, + const UnicodeString& source, + ParsePosition& pos, + int32_t& count, + UErrorCode& ec) const; + + FieldPosition* updateMetaData(AppendableWrapper& dest, int32_t prevLength, + FieldPosition* fp, const Formattable* argId) const; /** - * Convenience method that ought to be in NumberFormat + * Finds the "other" sub-message. + * @param partIndex the index of the first PluralFormat argument style part. + * @return the "other" sub-message start part index. */ - NumberFormat* createIntegerFormat(const Locale& locale, UErrorCode& status) const; + int32_t findOtherSubMessage(int32_t partIndex) const; /** - * Checks the range of the source text to quote the special - * characters, { and ' and copy to target buffer. - * @param source - * @param start the text offset to start the process of in the source string - * @param end the text offset to end the process of in the source string - * @param appendTo Output parameter to receive result. - * Result is appended to existing contents. + * Returns the ARG_START index of the first occurrence of the plural number in a sub-message. + * Returns -1 if it is a REPLACE_NUMBER. + * Returns 0 if there is neither. + */ + int32_t findFirstPluralNumberArg(int32_t msgStart, const UnicodeString &argName) const; + + Format* getCachedFormatter(int32_t argumentNumber) const; + + UnicodeString getLiteralStringUntilNextArgument(int32_t from) const; + + void copyObjects(const MessageFormat& that, UErrorCode& ec); + + void formatComplexSubMessage(int32_t msgStart, + const void *plNumber, + const Formattable* arguments, + const UnicodeString *argumentNames, + int32_t cnt, + AppendableWrapper& appendTo, + UErrorCode& success) const; + + /** + * Convenience method that ought to be in NumberFormat */ - static void copyAndFixQuotes(const UnicodeString& appendTo, int32_t start, int32_t end, UnicodeString& target); + NumberFormat* createIntegerFormat(const Locale& locale, UErrorCode& status) const; /** * Returns array of argument types in the parsed pattern @@ -929,7 +1071,6 @@ private: * for public consumption. * @param listCount Output parameter to receive the size of array * @return The array of formattable types in the pattern - * @internal */ const Formattable::Type* getArgTypeList(int32_t& listCount) const { listCount = argTypeCount; @@ -937,25 +1078,42 @@ private: } /** - * Returns FALSE if the argument name is not legal. - * @param argName argument name. - * @return TRUE if the argument name is legal, otherwise return FALSE. + * Resets the internal MessagePattern, and other associated caches. */ - UBool isLegalArgName(const UnicodeString& argName) const; + void resetPattern(); + + /** + * A DummyFormatter that we use solely to store a NULL value. UHash does + * not support storing NULL values. + */ + class U_I18N_API DummyFormat : public Format { + public: + virtual UBool operator==(const Format&) const; + virtual DummyFormat* clone() const; + virtual UnicodeString& format(const Formattable& obj, + UnicodeString& appendTo, + UErrorCode& status) const; + virtual UnicodeString& format(const Formattable&, + UnicodeString& appendTo, + FieldPosition&, + UErrorCode& status) const; + virtual UnicodeString& format(const Formattable& obj, + UnicodeString& appendTo, + FieldPositionIterator* posIter, + UErrorCode& status) const; + virtual void parseObject(const UnicodeString&, + Formattable&, + ParsePosition&) const; + }; friend class MessageFormatAdapter; // getFormatTypeList() access }; -inline UnicodeString& -MessageFormat::format(const Formattable& obj, - UnicodeString& appendTo, - UErrorCode& status) const { - return Format::format(obj, appendTo, status); -} - U_NAMESPACE_END #endif /* #if !UCONFIG_NO_FORMATTING */ +#endif /* U_SHOW_CPLUSPLUS_API */ + #endif // _MSGFMT //eof