1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 * Copyright (C) 2007-2013, International Business Machines Corporation and
5 * others. All Rights Reserved.
6 ********************************************************************************
10 * Modification History:
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 *******************************************************************************/
22 #include "unicode/utypes.h"
24 #if U_SHOW_CPLUSPLUS_API
28 * \brief C++ API: Formats messages in a language-neutral way.
31 #if !UCONFIG_NO_FORMATTING
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"
41 // Forward declaration.
43 typedef struct UHashtable UHashtable
; /**< @internal */
48 class AppendableWrapper
;
53 * <p>MessageFormat prepares strings for display to users,
54 * with optional arguments (variables/placeholders).
55 * The arguments can occur in any order, which is necessary for translation
56 * into languages with different grammars.
58 * <p>A MessageFormat is constructed from a <em>pattern</em> string
59 * with arguments in {curly braces} which will be replaced by formatted values.
61 * <p><code>MessageFormat</code> differs from the other <code>Format</code>
62 * classes in that you create a <code>MessageFormat</code> object with one
63 * of its constructors (not with a <code>createInstance</code> style factory
64 * method). Factory methods aren't necessary because <code>MessageFormat</code>
65 * itself doesn't implement locale-specific behavior. Any locale-specific
66 * behavior is defined by the pattern that you provide and the
67 * subformats used for inserted arguments.
69 * <p>Arguments can be named (using identifiers) or numbered (using small ASCII-digit integers).
70 * Some of the API methods work only with argument numbers and throw an exception
71 * if the pattern has named arguments (see {@link #usesNamedArguments()}).
73 * <p>An argument might not specify any format type. In this case,
74 * a numeric value is formatted with a default (for the locale) NumberFormat,
75 * and a date/time value is formatted with a default (for the locale) DateFormat.
77 * <p>An argument might specify a "simple" type for which the specified
78 * Format object is created, cached and used.
80 * <p>An argument might have a "complex" type with nested MessageFormat sub-patterns.
81 * During formatting, one of these sub-messages is selected according to the argument value
82 * and recursively formatted.
84 * <p>After construction, a custom Format object can be set for
85 * a top-level argument, overriding the default formatting and parsing behavior
87 * However, custom formatting can be achieved more simply by writing
88 * a typeless argument in the pattern string
89 * and supplying it with a preformatted string value.
91 * <p>When formatting, MessageFormat takes a collection of argument values
92 * and writes an output string.
93 * The argument values may be passed as an array
94 * (when the pattern contains only numbered arguments)
95 * or as an array of names and and an array of arguments (which works for both named
96 * and numbered arguments).
98 * <p>Each argument is matched with one of the input values by array index or argument name
99 * and formatted according to its pattern specification
100 * (or using a custom Format object if one was set).
101 * A numbered pattern argument is matched with an argument name that contains that number
102 * as an ASCII-decimal-digit string (without leading zero).
104 * <h4><a name="patterns">Patterns and Their Interpretation</a></h4>
106 * <code>MessageFormat</code> uses patterns of the following form:
108 * message = messageText (argument messageText)*
109 * argument = noneArg | simpleArg | complexArg
110 * complexArg = choiceArg | pluralArg | selectArg | selectordinalArg
112 * noneArg = '{' argNameOrNumber '}'
113 * simpleArg = '{' argNameOrNumber ',' argType [',' argStyle] '}'
114 * choiceArg = '{' argNameOrNumber ',' "choice" ',' choiceStyle '}'
115 * pluralArg = '{' argNameOrNumber ',' "plural" ',' pluralStyle '}'
116 * selectArg = '{' argNameOrNumber ',' "select" ',' selectStyle '}'
117 * selectordinalArg = '{' argNameOrNumber ',' "selectordinal" ',' pluralStyle '}'
119 * choiceStyle: see {@link ChoiceFormat}
120 * pluralStyle: see {@link PluralFormat}
121 * selectStyle: see {@link SelectFormat}
123 * argNameOrNumber = argName | argNumber
124 * argName = [^[[:Pattern_Syntax:][:Pattern_White_Space:]]]+
125 * argNumber = '0' | ('1'..'9' ('0'..'9')*)
127 * argType = "number" | "date" | "time" | "spellout" | "ordinal" | "duration"
128 * argStyle = "short" | "medium" | "long" | "full" | "integer" | "currency" | "percent" | argStyleText | "::" argSkeletonText
132 * <li>messageText can contain quoted literal strings including syntax characters.
133 * A quoted literal string begins with an ASCII apostrophe and a syntax character
134 * (usually a {curly brace}) and continues until the next single apostrophe.
135 * A double ASCII apostrohpe inside or outside of a quoted string represents
136 * one literal apostrophe.
137 * <li>Quotable syntax characters are the {curly braces} in all messageText parts,
138 * plus the '#' sign in a messageText immediately inside a pluralStyle,
139 * and the '|' symbol in a messageText immediately inside a choiceStyle.
140 * <li>See also {@link #UMessagePatternApostropheMode}
141 * <li>In argStyleText, every single ASCII apostrophe begins and ends quoted literal text,
142 * and unquoted {curly braces} must occur in matched pairs.
145 * <p>Recommendation: Use the real apostrophe (single quote) character
146 * \htmlonly’\endhtmlonly (U+2019) for
147 * human-readable text, and use the ASCII apostrophe ' (U+0027)
148 * only in program syntax, like quoting in MessageFormat.
149 * See the annotations for U+0027 Apostrophe in The Unicode Standard.
151 * <p>The <code>choice</code> argument type is deprecated.
152 * Use <code>plural</code> arguments for proper plural selection,
153 * and <code>select</code> arguments for simple selection among a fixed set of choices.
155 * <p>The <code>argType</code> and <code>argStyle</code> values are used to create
156 * a <code>Format</code> instance for the format element. The following
157 * table shows how the values map to Format instances. Combinations not
158 * shown in the table are illegal. Any <code>argStyleText</code> must
159 * be a valid pattern string for the Format subclass used.
161 * <p><table border=1>
165 * <th>resulting Format object
167 * <td colspan=2><i>(none)</i>
168 * <td><code>null</code>
170 * <td rowspan=6><code>number</code>
172 * <td><code>NumberFormat.createInstance(getLocale(), status)</code>
174 * <td><code>integer</code>
175 * <td><code>NumberFormat.createInstance(getLocale(), kNumberStyle, status)</code>
177 * <td><code>currency</code>
178 * <td><code>NumberFormat.createCurrencyInstance(getLocale(), status)</code>
180 * <td><code>percent</code>
181 * <td><code>NumberFormat.createPercentInstance(getLocale(), status)</code>
183 * <td><i>argStyleText</i>
184 * <td><code>new DecimalFormat(argStyleText, new DecimalFormatSymbols(getLocale(), status), status)</code>
186 * <td><i>argSkeletonText</i>
187 * <td><code>NumberFormatter::forSkeleton(argSkeletonText, status).locale(getLocale()).toFormat(status)</code>
189 * <td rowspan=7><code>date</code>
191 * <td><code>DateFormat.createDateInstance(kDefault, getLocale(), status)</code>
193 * <td><code>short</code>
194 * <td><code>DateFormat.createDateInstance(kShort, getLocale(), status)</code>
196 * <td><code>medium</code>
197 * <td><code>DateFormat.createDateInstance(kDefault, getLocale(), status)</code>
199 * <td><code>long</code>
200 * <td><code>DateFormat.createDateInstance(kLong, getLocale(), status)</code>
202 * <td><code>full</code>
203 * <td><code>DateFormat.createDateInstance(kFull, getLocale(), status)</code>
205 * <td><i>argStyleText</i>
206 * <td><code>new SimpleDateFormat(argStyleText, getLocale(), status)</code>
208 * <td><i>argSkeletonText</i>
209 * <td><code>DateFormat::createInstanceForSkeleton(argSkeletonText, getLocale(), status)</code>
211 * <td rowspan=6><code>time</code>
213 * <td><code>DateFormat.createTimeInstance(kDefault, getLocale(), status)</code>
215 * <td><code>short</code>
216 * <td><code>DateFormat.createTimeInstance(kShort, getLocale(), status)</code>
218 * <td><code>medium</code>
219 * <td><code>DateFormat.createTimeInstance(kDefault, getLocale(), status)</code>
221 * <td><code>long</code>
222 * <td><code>DateFormat.createTimeInstance(kLong, getLocale(), status)</code>
224 * <td><code>full</code>
225 * <td><code>DateFormat.createTimeInstance(kFull, getLocale(), status)</code>
227 * <td><i>argStyleText</i>
228 * <td><code>new SimpleDateFormat(argStyleText, getLocale(), status)</code>
230 * <td><code>spellout</code>
231 * <td><i>argStyleText (optional)</i>
232 * <td><code>new RuleBasedNumberFormat(URBNF_SPELLOUT, getLocale(), status)
233 * <br/> .setDefaultRuleset(argStyleText, status);</code>
235 * <td><code>ordinal</code>
236 * <td><i>argStyleText (optional)</i>
237 * <td><code>new RuleBasedNumberFormat(URBNF_ORDINAL, getLocale(), status)
238 * <br/> .setDefaultRuleset(argStyleText, status);</code>
240 * <td><code>duration</code>
241 * <td><i>argStyleText (optional)</i>
242 * <td><code>new RuleBasedNumberFormat(URBNF_DURATION, getLocale(), status)
243 * <br/> .setDefaultRuleset(argStyleText, status);</code>
247 * <h4>Argument formatting</h4>
249 * <p>Arguments are formatted according to their type, using the default
250 * ICU formatters for those types, unless otherwise specified.</p>
252 * <p>There are also several ways to control the formatting.</p>
254 * <p>We recommend you use default styles, predefined style values, skeletons,
255 * or preformatted values, but not pattern strings or custom format objects.</p>
257 * <p>For more details, see the
258 * <a href="http://userguide.icu-project.org/formatparse/messages">ICU User Guide</a>.</p>
260 * <h4>Usage Information</h4>
262 * <p>Here are some examples of usage:
267 * UErrorCode success = U_ZERO_ERROR;
268 * GregorianCalendar cal(success);
269 * Formattable arguments[] = {
271 * Formattable( (Date) cal.getTime(success), Formattable::kIsDate),
272 * "a disturbance in the Force"
275 * UnicodeString result;
276 * MessageFormat::format(
277 * "At {1,time,::jmm} on {1,date,::dMMMM}, there was {2} on planet {0,number}.",
278 * arguments, 3, result, success );
280 * cout << "result: " << result << endl;
281 * //<output>: At 4:34 PM on March 23, there was a disturbance
282 * // in the Force on planet 7.
286 * Typically, the message format will come from resources, and the
287 * arguments will be dynamically set at runtime.
293 * success = U_ZERO_ERROR;
294 * Formattable testArgs[] = {3L, "MyDisk"};
296 * MessageFormat form(
297 * "The disk \"{1}\" contains {0} file(s).", success );
299 * UnicodeString string;
300 * FieldPosition fpos = 0;
301 * cout << "format: " << form.format(testArgs, 2, string, fpos, success ) << endl;
303 * // output, with different testArgs:
304 * // output: The disk "MyDisk" contains 0 file(s).
305 * // output: The disk "MyDisk" contains 1 file(s).
306 * // output: The disk "MyDisk" contains 1,273 file(s).
311 * <p>For messages that include plural forms, you can use a plural argument:
314 * success = U_ZERO_ERROR;
315 * MessageFormat msgFmt(
316 * "{num_files, plural, "
317 * "=0{There are no files on disk \"{disk_name}\".}"
318 * "=1{There is one file on disk \"{disk_name}\".}"
319 * "other{There are # files on disk \"{disk_name}\".}}",
322 * FieldPosition fpos = 0;
323 * Formattable testArgs[] = {0L, "MyDisk"};
324 * UnicodeString testArgsNames[] = {"num_files", "disk_name"};
325 * UnicodeString result;
326 * cout << msgFmt.format(testArgs, testArgsNames, 2, result, fpos, 0, success);
328 * cout << msgFmt.format(testArgs, testArgsNames, 2, result, fpos, 0, success);
331 * There are no files on disk "MyDisk".
332 * There are 3 files on "MyDisk".
334 * See {@link PluralFormat} and {@link PluralRules} for details.
336 * <h4><a name="synchronization">Synchronization</a></h4>
338 * <p>MessageFormats are not synchronized.
339 * It is recommended to create separate format instances for each thread.
340 * If multiple threads access a format concurrently, it must be synchronized
345 class U_I18N_API MessageFormat
: public Format
{
347 #ifndef U_HIDE_OBSOLETE_API
349 * Enum type for kMaxFormat.
350 * @obsolete ICU 3.0. The 10-argument limit was removed as of ICU 2.6,
351 * rendering this enum type obsolete.
355 * The maximum number of arguments.
356 * @obsolete ICU 3.0. The 10-argument limit was removed as of ICU 2.6,
357 * rendering this constant obsolete.
361 #endif /* U_HIDE_OBSOLETE_API */
364 * Constructs a new MessageFormat using the given pattern and the
367 * @param pattern Pattern used to construct object.
368 * @param status Input/output error code. If the
369 * pattern cannot be parsed, set to failure code.
372 MessageFormat(const UnicodeString
& pattern
,
376 * Constructs a new MessageFormat using the given pattern and locale.
377 * @param pattern Pattern used to construct object.
378 * @param newLocale The locale to use for formatting dates and numbers.
379 * @param status Input/output error code. If the
380 * pattern cannot be parsed, set to failure code.
383 MessageFormat(const UnicodeString
& pattern
,
384 const Locale
& newLocale
,
387 * Constructs a new MessageFormat using the given pattern and locale.
388 * @param pattern Pattern used to construct object.
389 * @param newLocale The locale to use for formatting dates and numbers.
390 * @param parseError Struct to receive information on the position
391 * of an error within the pattern.
392 * @param status Input/output error code. If the
393 * pattern cannot be parsed, set to failure code.
396 MessageFormat(const UnicodeString
& pattern
,
397 const Locale
& newLocale
,
398 UParseError
& parseError
,
401 * Constructs a new MessageFormat from an existing one.
404 MessageFormat(const MessageFormat
&);
407 * Assignment operator.
410 const MessageFormat
& operator=(const MessageFormat
&);
416 virtual ~MessageFormat();
419 * Clones this Format object polymorphically. The caller owns the
420 * result and should delete it when done.
423 virtual MessageFormat
* clone() const;
426 * Returns true if the given Format objects are semantically equal.
427 * Objects of different subclasses are considered unequal.
428 * @param other the object to be compared with.
429 * @return true if the given Format objects are semantically equal.
432 virtual UBool
operator==(const Format
& other
) const;
435 * Sets the locale to be used for creating argument Format objects.
436 * @param theLocale the new locale value to be set.
439 virtual void setLocale(const Locale
& theLocale
);
442 * Gets the locale used for creating argument Format objects.
443 * format information.
444 * @return the locale of the object.
447 virtual const Locale
& getLocale(void) const;
450 * Applies the given pattern string to this message format.
452 * @param pattern The pattern to be applied.
453 * @param status Input/output error code. If the
454 * pattern cannot be parsed, set to failure code.
457 virtual void applyPattern(const UnicodeString
& pattern
,
460 * Applies the given pattern string to this message format.
462 * @param pattern The pattern to be applied.
463 * @param parseError Struct to receive information on the position
464 * of an error within the pattern.
465 * @param status Input/output error code. If the
466 * pattern cannot be parsed, set to failure code.
469 virtual void applyPattern(const UnicodeString
& pattern
,
470 UParseError
& parseError
,
474 * Sets the UMessagePatternApostropheMode and the pattern used by this message format.
475 * Parses the pattern and caches Format objects for simple argument types.
476 * Patterns and their interpretation are specified in the
477 * <a href="#patterns">class description</a>.
479 * This method is best used only once on a given object to avoid confusion about the mode,
480 * and after constructing the object with an empty pattern string to minimize overhead.
482 * @param pattern The pattern to be applied.
483 * @param aposMode The new apostrophe mode.
484 * @param parseError Struct to receive information on the position
485 * of an error within the pattern.
487 * @param status Input/output error code. If the
488 * pattern cannot be parsed, set to failure code.
491 virtual void applyPattern(const UnicodeString
& pattern
,
492 UMessagePatternApostropheMode aposMode
,
493 UParseError
* parseError
,
497 * @return this instance's UMessagePatternApostropheMode.
500 UMessagePatternApostropheMode
getApostropheMode() const {
501 return msgPattern
.getApostropheMode();
505 * Returns a pattern that can be used to recreate this object.
507 * @param appendTo Output parameter to receive the pattern.
508 * Result is appended to existing contents.
509 * @return Reference to 'appendTo' parameter.
512 virtual UnicodeString
& toPattern(UnicodeString
& appendTo
) const;
516 * See the class description about format numbering.
517 * The caller should not delete the Format objects after this call.
518 * <EM>The array formatsToAdopt is not itself adopted.</EM> Its
519 * ownership is retained by the caller. If the call fails because
520 * memory cannot be allocated, then the formats will be deleted
521 * by this method, and this object will remain unchanged.
523 * <p>If this format uses named arguments, the new formats are discarded
524 * and this format remains unchanged.
527 * @param formatsToAdopt the format to be adopted.
528 * @param count the size of the array.
530 virtual void adoptFormats(Format
** formatsToAdopt
, int32_t count
);
534 * See the class description about format numbering.
535 * Each item in the array is cloned into the internal array.
536 * If the call fails because memory cannot be allocated, then this
537 * object will remain unchanged.
539 * <p>If this format uses named arguments, the new formats are discarded
540 * and this format remains unchanged.
543 * @param newFormats the new format to be set.
544 * @param cnt the size of the array.
546 virtual void setFormats(const Format
** newFormats
, int32_t cnt
);
550 * Sets one subformat.
551 * See the class description about format numbering.
552 * The caller should not delete the Format object after this call.
553 * If the number is over the number of formats already set,
554 * the item will be deleted and ignored.
556 * <p>If this format uses named arguments, the new format is discarded
557 * and this format remains unchanged.
560 * @param formatNumber index of the subformat.
561 * @param formatToAdopt the format to be adopted.
563 virtual void adoptFormat(int32_t formatNumber
, Format
* formatToAdopt
);
566 * Sets one subformat.
567 * See the class description about format numbering.
568 * If the number is over the number of formats already set,
569 * the item will be ignored.
570 * @param formatNumber index of the subformat.
571 * @param format the format to be set.
574 virtual void setFormat(int32_t formatNumber
, const Format
& format
);
577 * Gets format names. This function returns formatNames in StringEnumerations
578 * which can be used with getFormat() and setFormat() to export formattable
579 * array from current MessageFormat to another. It is the caller's responsibility
580 * to delete the returned formatNames.
581 * @param status output param set to success/failure code.
584 virtual StringEnumeration
* getFormatNames(UErrorCode
& status
);
587 * Gets subformat pointer for given format name.
588 * This function supports both named and numbered
589 * arguments. If numbered, the formatName is the
590 * corresponding UnicodeStrings (e.g. "0", "1", "2"...).
591 * The returned Format object should not be deleted by the caller,
592 * nor should the ponter of other object . The pointer and its
593 * contents remain valid only until the next call to any method
594 * of this class is made with this object.
595 * @param formatName the name or number specifying a format
596 * @param status output param set to success/failure code.
599 virtual Format
* getFormat(const UnicodeString
& formatName
, UErrorCode
& status
);
602 * Sets one subformat for given format name.
603 * See the class description about format name.
604 * This function supports both named and numbered
605 * arguments-- if numbered, the formatName is the
606 * corresponding UnicodeStrings (e.g. "0", "1", "2"...).
607 * If there is no matched formatName or wrong type,
608 * the item will be ignored.
609 * @param formatName Name of the subformat.
610 * @param format the format to be set.
611 * @param status output param set to success/failure code.
614 virtual void setFormat(const UnicodeString
& formatName
, const Format
& format
, UErrorCode
& status
);
617 * Sets one subformat for given format name.
618 * See the class description about format name.
619 * This function supports both named and numbered
620 * arguments-- if numbered, the formatName is the
621 * corresponding UnicodeStrings (e.g. "0", "1", "2"...).
622 * If there is no matched formatName or wrong type,
623 * the item will be ignored.
624 * The caller should not delete the Format object after this call.
625 * @param formatName Name of the subformat.
626 * @param formatToAdopt Format to be adopted.
627 * @param status output param set to success/failure code.
630 virtual void adoptFormat(const UnicodeString
& formatName
, Format
* formatToAdopt
, UErrorCode
& status
);
633 * Gets an array of subformats of this object. The returned array
634 * should not be deleted by the caller, nor should the pointers
635 * within the array. The array and its contents remain valid only
636 * until the next call to this format. See the class description
637 * about format numbering.
639 * @param count output parameter to receive the size of the array
640 * @return an array of count Format* objects, or NULL if out of
641 * memory. Any or all of the array elements may be NULL.
644 virtual const Format
** getFormats(int32_t& count
) const;
647 using Format::format
;
650 * Formats the given array of arguments into a user-readable string.
651 * Does not take ownership of the Formattable* array or its contents.
653 * <p>If this format uses named arguments, appendTo is unchanged and
654 * status is set to U_ILLEGAL_ARGUMENT_ERROR.
656 * @param source An array of objects to be formatted.
657 * @param count The number of elements of 'source'.
658 * @param appendTo Output parameter to receive result.
659 * Result is appended to existing contents.
660 * @param ignore Not used; inherited from base class API.
661 * @param status Input/output error code. If the
662 * pattern cannot be parsed, set to failure code.
663 * @return Reference to 'appendTo' parameter.
666 UnicodeString
& format(const Formattable
* source
,
668 UnicodeString
& appendTo
,
669 FieldPosition
& ignore
,
670 UErrorCode
& status
) const;
673 * Formats the given array of arguments into a user-readable string
674 * using the given pattern.
676 * <p>If this format uses named arguments, appendTo is unchanged and
677 * status is set to U_ILLEGAL_ARGUMENT_ERROR.
679 * @param pattern The pattern.
680 * @param arguments An array of objects to be formatted.
681 * @param count The number of elements of 'source'.
682 * @param appendTo Output parameter to receive result.
683 * Result is appended to existing contents.
684 * @param status Input/output error code. If the
685 * pattern cannot be parsed, set to failure code.
686 * @return Reference to 'appendTo' parameter.
689 static UnicodeString
& format(const UnicodeString
& pattern
,
690 const Formattable
* arguments
,
692 UnicodeString
& appendTo
,
696 * Formats the given array of arguments into a user-readable
697 * string. The array must be stored within a single Formattable
698 * object of type kArray. If the Formattable object type is not of
699 * type kArray, then returns a failing UErrorCode.
701 * <p>If this format uses named arguments, appendTo is unchanged and
702 * status is set to U_ILLEGAL_ARGUMENT_ERROR.
704 * @param obj A Formattable of type kArray containing
705 * arguments to be formatted.
706 * @param appendTo Output parameter to receive result.
707 * Result is appended to existing contents.
708 * @param pos On input: an alignment field, if desired.
709 * On output: the offsets of the alignment field.
710 * @param status Input/output error code. If the
711 * pattern cannot be parsed, set to failure code.
712 * @return Reference to 'appendTo' parameter.
715 virtual UnicodeString
& format(const Formattable
& obj
,
716 UnicodeString
& appendTo
,
718 UErrorCode
& status
) const;
721 * Formats the given array of arguments into a user-defined argument name
722 * array. This function supports both named and numbered
723 * arguments-- if numbered, the formatName is the
724 * corresponding UnicodeStrings (e.g. "0", "1", "2"...).
726 * @param argumentNames argument name array
727 * @param arguments An array of objects to be formatted.
728 * @param count The number of elements of 'argumentNames' and
729 * arguments. The number of argumentNames and arguments
731 * @param appendTo Output parameter to receive result.
732 * Result is appended to existing contents.
733 * @param status Input/output error code. If the
734 * pattern cannot be parsed, set to failure code.
735 * @return Reference to 'appendTo' parameter.
738 UnicodeString
& format(const UnicodeString
* argumentNames
,
739 const Formattable
* arguments
,
741 UnicodeString
& appendTo
,
742 UErrorCode
& status
) const;
744 * Parses the given string into an array of output arguments.
746 * @param source String to be parsed.
747 * @param pos On input, starting position for parse. On output,
748 * final position after parse. Unchanged if parse
750 * @param count Output parameter to receive the number of arguments
752 * @return an array of parsed arguments. The caller owns both
753 * the array and its contents.
756 virtual Formattable
* parse(const UnicodeString
& source
,
758 int32_t& count
) const;
761 * Parses the given string into an array of output arguments.
763 * <p>If this format uses named arguments, status is set to
764 * U_ARGUMENT_TYPE_MISMATCH.
766 * @param source String to be parsed.
767 * @param count Output param to receive size of returned array.
768 * @param status Input/output error code. If the
769 * pattern cannot be parsed, set to failure code.
770 * @return an array of parsed arguments. The caller owns both
771 * the array and its contents. Returns NULL if status is not U_ZERO_ERROR.
775 virtual Formattable
* parse(const UnicodeString
& source
,
777 UErrorCode
& status
) const;
780 * Parses the given string into an array of output arguments
781 * stored within a single Formattable of type kArray.
783 * @param source The string to be parsed into an object.
784 * @param result Formattable to be set to the parse result.
785 * If parse fails, return contents are undefined.
786 * @param pos On input, starting position for parse. On output,
787 * final position after parse. Unchanged if parse
791 virtual void parseObject(const UnicodeString
& source
,
793 ParsePosition
& pos
) const;
796 * Convert an 'apostrophe-friendly' pattern into a standard
797 * pattern. Standard patterns treat all apostrophes as
798 * quotes, which is problematic in some languages, e.g.
799 * French, where apostrophe is commonly used. This utility
800 * assumes that only an unpaired apostrophe immediately before
801 * a brace is a true quote. Other unpaired apostrophes are paired,
802 * and the resulting standard pattern string is returned.
804 * <p><b>Note</b> it is not guaranteed that the returned pattern
805 * is indeed a valid pattern. The only effect is to convert
806 * between patterns having different quoting semantics.
808 * @param pattern the 'apostrophe-friendly' patttern to convert
809 * @param status Input/output error code. If the pattern
810 * cannot be parsed, the failure code is set.
811 * @return the standard equivalent of the original pattern
814 static UnicodeString
autoQuoteApostrophe(const UnicodeString
& pattern
,
819 * Returns true if this MessageFormat uses named arguments,
820 * and false otherwise. See class description.
822 * @return true if named arguments are used.
825 UBool
usesNamedArguments() const;
828 #ifndef U_HIDE_INTERNAL_API
830 * This API is for ICU internal use only.
831 * Please do not use it.
833 * Returns argument types count in the parsed pattern.
834 * Used to distinguish pattern "{0} d" and "d".
836 * @return The number of formattable types in the pattern
839 int32_t getArgTypeCount() const;
840 #endif /* U_HIDE_INTERNAL_API */
843 * Returns a unique class ID POLYMORPHICALLY. Pure virtual override.
844 * This method is to implement a simple version of RTTI, since not all
845 * C++ compilers support genuine RTTI. Polymorphic operator==() and
846 * clone() methods call this method.
848 * @return The class ID for this object. All objects of a
849 * given class have the same class ID. Objects of
850 * other classes have different class IDs.
853 virtual UClassID
getDynamicClassID(void) const;
856 * Return the class ID for this class. This is useful only for
857 * comparing to a return value from getDynamicClassID(). For example:
859 * . Base* polymorphic_pointer = createPolymorphicObject();
860 * . if (polymorphic_pointer->getDynamicClassID() ==
861 * . Derived::getStaticClassID()) ...
863 * @return The class ID for all objects of this class.
866 static UClassID U_EXPORT2
getStaticClassID(void);
868 #ifndef U_HIDE_INTERNAL_API
870 * Compares two Format objects. This is used for constructing the hash
873 * @param left pointer to a Format object. Must not be NULL.
874 * @param right pointer to a Format object. Must not be NULL.
876 * @return whether the two objects are the same
879 static UBool
equalFormats(const void* left
, const void* right
);
880 #endif /* U_HIDE_INTERNAL_API */
885 MessagePattern msgPattern
;
886 Format
** formatAliases
; // see getFormats
887 int32_t formatAliasesCapacity
;
889 MessageFormat(); // default constructor not implemented
892 * This provider helps defer instantiation of a PluralRules object
893 * until we actually need to select a keyword.
894 * For example, if the number matches an explicit-value selector like "=1"
895 * we do not need any PluralRules.
897 class U_I18N_API PluralSelectorProvider
: public PluralFormat::PluralSelector
{
899 PluralSelectorProvider(const MessageFormat
&mf
, UPluralType type
);
900 virtual ~PluralSelectorProvider();
901 virtual UnicodeString
select(void *ctx
, double number
, UErrorCode
& ec
) const;
905 const MessageFormat
&msgFormat
;
911 * A MessageFormat formats an array of arguments. Each argument
912 * has an expected type, based on the pattern. For example, if
913 * the pattern contains the subformat "{3,number,integer}", then
914 * we expect argument 3 to have type Formattable::kLong. This
915 * array needs to grow dynamically if the MessageFormat is
918 Formattable::Type
* argTypes
;
919 int32_t argTypeCount
;
920 int32_t argTypeCapacity
;
923 * TRUE if there are different argTypes for the same argument.
924 * This only matters when the MessageFormat is used in the plain C (umsg_xxx) API
925 * where the pattern argTypes determine how the va_arg list is read.
927 UBool hasArgTypeConflicts
;
929 // Variable-size array management
930 UBool
allocateArgTypes(int32_t capacity
, UErrorCode
& status
);
933 * Default Format objects used when no format is specified and a
934 * numeric or date argument is formatted. These are volatile
935 * cache objects maintained only for performance. They do not
936 * participate in operator=(), copy constructor(), nor
939 NumberFormat
* defaultNumberFormat
;
940 DateFormat
* defaultDateFormat
;
942 UHashtable
* cachedFormatters
;
943 UHashtable
* customFormatArgStarts
;
945 PluralSelectorProvider pluralProvider
;
946 PluralSelectorProvider ordinalProvider
;
949 * Method to retrieve default formats (or NULL on failure).
950 * These are semantically const, but may modify *this.
952 const NumberFormat
* getDefaultNumberFormat(UErrorCode
&) const;
953 const DateFormat
* getDefaultDateFormat(UErrorCode
&) const;
956 * Finds the word s, in the keyword list and returns the located index.
957 * @param s the keyword to be searched for.
958 * @param list the list of keywords to be searched with.
959 * @return the index of the list which matches the keyword s.
961 static int32_t findKeyword( const UnicodeString
& s
,
962 const char16_t * const *list
);
965 * Thin wrapper around the format(... AppendableWrapper ...) variant.
966 * Wraps the destination UnicodeString into an AppendableWrapper and
967 * supplies default values for some other parameters.
969 UnicodeString
& format(const Formattable
* arguments
,
970 const UnicodeString
*argumentNames
,
972 UnicodeString
& appendTo
,
974 UErrorCode
& status
) const;
977 * Formats the arguments and writes the result into the
978 * AppendableWrapper, updates the field position.
980 * @param msgStart Index to msgPattern part to start formatting from.
981 * @param plNumber NULL except when formatting a plural argument sub-message
982 * where a '#' is replaced by the format string for this number.
983 * @param arguments The formattable objects array. (Must not be NULL.)
984 * @param argumentNames NULL if numbered values are used. Otherwise the same
985 * length as "arguments", and each entry is the name of the
986 * corresponding argument in "arguments".
987 * @param cnt The length of arguments (and of argumentNames if that is not NULL).
988 * @param appendTo Output parameter to receive the result.
989 * The result string is appended to existing contents.
990 * @param pos Field position status.
991 * @param success The error code status.
993 void format(int32_t msgStart
,
994 const void *plNumber
,
995 const Formattable
* arguments
,
996 const UnicodeString
*argumentNames
,
998 AppendableWrapper
& appendTo
,
1000 UErrorCode
& success
) const;
1002 UnicodeString
getArgName(int32_t partIndex
);
1004 void setArgStartFormat(int32_t argStart
, Format
* formatter
, UErrorCode
& status
);
1006 void setCustomArgStartFormat(int32_t argStart
, Format
* formatter
, UErrorCode
& status
);
1008 int32_t nextTopLevelArgStart(int32_t partIndex
) const;
1010 UBool
argNameMatches(int32_t partIndex
, const UnicodeString
& argName
, int32_t argNumber
);
1012 void cacheExplicitFormats(UErrorCode
& status
);
1014 int32_t skipLeadingSpaces(UnicodeString
& style
);
1016 Format
* createAppropriateFormat(UnicodeString
& type
,
1017 UnicodeString
& style
,
1018 Formattable::Type
& formattableType
,
1019 UParseError
& parseError
,
1022 const Formattable
* getArgFromListByName(const Formattable
* arguments
,
1023 const UnicodeString
*argumentNames
,
1024 int32_t cnt
, UnicodeString
& name
) const;
1026 Formattable
* parse(int32_t msgStart
,
1027 const UnicodeString
& source
,
1030 UErrorCode
& ec
) const;
1032 FieldPosition
* updateMetaData(AppendableWrapper
& dest
, int32_t prevLength
,
1033 FieldPosition
* fp
, const Formattable
* argId
) const;
1036 * Finds the "other" sub-message.
1037 * @param partIndex the index of the first PluralFormat argument style part.
1038 * @return the "other" sub-message start part index.
1040 int32_t findOtherSubMessage(int32_t partIndex
) const;
1043 * Returns the ARG_START index of the first occurrence of the plural number in a sub-message.
1044 * Returns -1 if it is a REPLACE_NUMBER.
1045 * Returns 0 if there is neither.
1047 int32_t findFirstPluralNumberArg(int32_t msgStart
, const UnicodeString
&argName
) const;
1049 Format
* getCachedFormatter(int32_t argumentNumber
) const;
1051 UnicodeString
getLiteralStringUntilNextArgument(int32_t from
) const;
1053 void copyObjects(const MessageFormat
& that
, UErrorCode
& ec
);
1055 void formatComplexSubMessage(int32_t msgStart
,
1056 const void *plNumber
,
1057 const Formattable
* arguments
,
1058 const UnicodeString
*argumentNames
,
1060 AppendableWrapper
& appendTo
,
1061 UErrorCode
& success
) const;
1064 * Convenience method that ought to be in NumberFormat
1066 NumberFormat
* createIntegerFormat(const Locale
& locale
, UErrorCode
& status
) const;
1069 * Returns array of argument types in the parsed pattern
1070 * for use in C API. Only for the use of umsg_vformat(). Not
1071 * for public consumption.
1072 * @param listCount Output parameter to receive the size of array
1073 * @return The array of formattable types in the pattern
1075 const Formattable::Type
* getArgTypeList(int32_t& listCount
) const {
1076 listCount
= argTypeCount
;
1081 * Resets the internal MessagePattern, and other associated caches.
1083 void resetPattern();
1086 * A DummyFormatter that we use solely to store a NULL value. UHash does
1087 * not support storing NULL values.
1089 class U_I18N_API DummyFormat
: public Format
{
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
,
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
&,
1106 ParsePosition
&) const;
1109 friend class MessageFormatAdapter
; // getFormatTypeList() access
1114 #endif /* #if !UCONFIG_NO_FORMATTING */
1116 #endif /* U_SHOW_CPLUSPLUS_API */