2 * Copyright (C) 2007-2013, International Business Machines Corporation and
3 * others. All Rights Reserved.
4 ********************************************************************************
8 * Modification History:
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 *******************************************************************************/
20 #include "unicode/utypes.h"
24 * \brief C++ API: Formats messages in a language-neutral way.
27 #if !UCONFIG_NO_FORMATTING
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"
37 // Forward declaration.
39 typedef struct UHashtable UHashtable
; /**< @internal */
44 class AppendableWrapper
;
49 * <p>MessageFormat prepares strings for display to users,
50 * with optional arguments (variables/placeholders).
51 * The arguments can occur in any order, which is necessary for translation
52 * into languages with different grammars.
54 * <p>A MessageFormat is constructed from a <em>pattern</em> string
55 * with arguments in {curly braces} which will be replaced by formatted values.
57 * <p><code>MessageFormat</code> differs from the other <code>Format</code>
58 * classes in that you create a <code>MessageFormat</code> object with one
59 * of its constructors (not with a <code>createInstance</code> style factory
60 * method). Factory methods aren't necessary because <code>MessageFormat</code>
61 * itself doesn't implement locale-specific behavior. Any locale-specific
62 * behavior is defined by the pattern that you provide and the
63 * subformats used for inserted arguments.
65 * <p>Arguments can be named (using identifiers) or numbered (using small ASCII-digit integers).
66 * Some of the API methods work only with argument numbers and throw an exception
67 * if the pattern has named arguments (see {@link #usesNamedArguments()}).
69 * <p>An argument might not specify any format type. In this case,
70 * a Number value is formatted with a default (for the locale) NumberFormat,
71 * a Date value is formatted with a default (for the locale) DateFormat,
72 * and for any other value its toString() value is used.
74 * <p>An argument might specify a "simple" type for which the specified
75 * Format object is created, cached and used.
77 * <p>An argument might have a "complex" type with nested MessageFormat sub-patterns.
78 * During formatting, one of these sub-messages is selected according to the argument value
79 * and recursively formatted.
81 * <p>After construction, a custom Format object can be set for
82 * a top-level argument, overriding the default formatting and parsing behavior
84 * However, custom formatting can be achieved more simply by writing
85 * a typeless argument in the pattern string
86 * and supplying it with a preformatted string value.
88 * <p>When formatting, MessageFormat takes a collection of argument values
89 * and writes an output string.
90 * The argument values may be passed as an array
91 * (when the pattern contains only numbered arguments)
92 * or as an array of names and and an array of arguments (which works for both named
93 * and numbered arguments).
95 * <p>Each argument is matched with one of the input values by array index or argument name
96 * and formatted according to its pattern specification
97 * (or using a custom Format object if one was set).
98 * A numbered pattern argument is matched with an argument name that contains that number
99 * as an ASCII-decimal-digit string (without leading zero).
101 * <h4><a name="patterns">Patterns and Their Interpretation</a></h4>
103 * <code>MessageFormat</code> uses patterns of the following form:
105 * message = messageText (argument messageText)*
106 * argument = noneArg | simpleArg | complexArg
107 * complexArg = choiceArg | pluralArg | selectArg | selectordinalArg
109 * noneArg = '{' argNameOrNumber '}'
110 * simpleArg = '{' argNameOrNumber ',' argType [',' argStyle] '}'
111 * choiceArg = '{' argNameOrNumber ',' "choice" ',' choiceStyle '}'
112 * pluralArg = '{' argNameOrNumber ',' "plural" ',' pluralStyle '}'
113 * selectArg = '{' argNameOrNumber ',' "select" ',' selectStyle '}'
114 * selectordinalArg = '{' argNameOrNumber ',' "selectordinal" ',' pluralStyle '}'
116 * choiceStyle: see {@link ChoiceFormat}
117 * pluralStyle: see {@link PluralFormat}
118 * selectStyle: see {@link SelectFormat}
120 * argNameOrNumber = argName | argNumber
121 * argName = [^[[:Pattern_Syntax:][:Pattern_White_Space:]]]+
122 * argNumber = '0' | ('1'..'9' ('0'..'9')*)
124 * argType = "number" | "date" | "time" | "spellout" | "ordinal" | "duration"
125 * argStyle = "short" | "medium" | "long" | "full" | "integer" | "currency" | "percent" | argStyleText
129 * <li>messageText can contain quoted literal strings including syntax characters.
130 * A quoted literal string begins with an ASCII apostrophe and a syntax character
131 * (usually a {curly brace}) and continues until the next single apostrophe.
132 * A double ASCII apostrohpe inside or outside of a quoted string represents
133 * one literal apostrophe.
134 * <li>Quotable syntax characters are the {curly braces} in all messageText parts,
135 * plus the '#' sign in a messageText immediately inside a pluralStyle,
136 * and the '|' symbol in a messageText immediately inside a choiceStyle.
137 * <li>See also {@link #UMessagePatternApostropheMode}
138 * <li>In argStyleText, every single ASCII apostrophe begins and ends quoted literal text,
139 * and unquoted {curly braces} must occur in matched pairs.
142 * <p>Recommendation: Use the real apostrophe (single quote) character
143 * \htmlonly’\endhtmlonly (U+2019) for
144 * human-readable text, and use the ASCII apostrophe ' (U+0027)
145 * only in program syntax, like quoting in MessageFormat.
146 * See the annotations for U+0027 Apostrophe in The Unicode Standard.
148 * <p>The <code>choice</code> argument type is deprecated.
149 * Use <code>plural</code> arguments for proper plural selection,
150 * and <code>select</code> arguments for simple selection among a fixed set of choices.
152 * <p>The <code>argType</code> and <code>argStyle</code> values are used to create
153 * a <code>Format</code> instance for the format element. The following
154 * table shows how the values map to Format instances. Combinations not
155 * shown in the table are illegal. Any <code>argStyleText</code> must
156 * be a valid pattern string for the Format subclass used.
158 * <p><table border=1>
162 * <th>resulting Format object
164 * <td colspan=2><i>(none)</i>
165 * <td><code>null</code>
167 * <td rowspan=5><code>number</code>
169 * <td><code>NumberFormat.createInstance(getLocale(), status)</code>
171 * <td><code>integer</code>
172 * <td><code>NumberFormat.createInstance(getLocale(), kNumberStyle, status)</code>
174 * <td><code>currency</code>
175 * <td><code>NumberFormat.createCurrencyInstance(getLocale(), status)</code>
177 * <td><code>percent</code>
178 * <td><code>NumberFormat.createPercentInstance(getLocale(), status)</code>
180 * <td><i>argStyleText</i>
181 * <td><code>new DecimalFormat(argStyleText, new DecimalFormatSymbols(getLocale(), status), status)</code>
183 * <td rowspan=6><code>date</code>
185 * <td><code>DateFormat.createDateInstance(kDefault, getLocale(), status)</code>
187 * <td><code>short</code>
188 * <td><code>DateFormat.createDateInstance(kShort, getLocale(), status)</code>
190 * <td><code>medium</code>
191 * <td><code>DateFormat.createDateInstance(kDefault, getLocale(), status)</code>
193 * <td><code>long</code>
194 * <td><code>DateFormat.createDateInstance(kLong, getLocale(), status)</code>
196 * <td><code>full</code>
197 * <td><code>DateFormat.createDateInstance(kFull, getLocale(), status)</code>
199 * <td><i>argStyleText</i>
200 * <td><code>new SimpleDateFormat(argStyleText, getLocale(), status)
202 * <td rowspan=6><code>time</code>
204 * <td><code>DateFormat.createTimeInstance(kDefault, getLocale(), status)</code>
206 * <td><code>short</code>
207 * <td><code>DateFormat.createTimeInstance(kShort, getLocale(), status)</code>
209 * <td><code>medium</code>
210 * <td><code>DateFormat.createTimeInstance(kDefault, getLocale(), status)</code>
212 * <td><code>long</code>
213 * <td><code>DateFormat.createTimeInstance(kLong, getLocale(), status)</code>
215 * <td><code>full</code>
216 * <td><code>DateFormat.createTimeInstance(kFull, getLocale(), status)</code>
218 * <td><i>argStyleText</i>
219 * <td><code>new SimpleDateFormat(argStyleText, getLocale(), status)
221 * <td><code>spellout</code>
222 * <td><i>argStyleText (optional)</i>
223 * <td><code>new RuleBasedNumberFormat(URBNF_SPELLOUT, getLocale(), status)
224 * <br/> .setDefaultRuleset(argStyleText, status);</code>
226 * <td><code>ordinal</code>
227 * <td><i>argStyleText (optional)</i>
228 * <td><code>new RuleBasedNumberFormat(URBNF_ORDINAL, getLocale(), status)
229 * <br/> .setDefaultRuleset(argStyleText, status);</code>
231 * <td><code>duration</code>
232 * <td><i>argStyleText (optional)</i>
233 * <td><code>new RuleBasedNumberFormat(URBNF_DURATION, getLocale(), status)
234 * <br/> .setDefaultRuleset(argStyleText, status);</code>
238 * <h4>Usage Information</h4>
240 * <p>Here are some examples of usage:
245 * UErrorCode success = U_ZERO_ERROR;
246 * GregorianCalendar cal(success);
247 * Formattable arguments[] = {
249 * Formattable( (Date) cal.getTime(success), Formattable::kIsDate),
250 * "a disturbance in the Force"
253 * UnicodeString result;
254 * MessageFormat::format(
255 * "At {1,time} on {1,date}, there was {2} on planet {0,number}.",
256 * arguments, 3, result, success );
258 * cout << "result: " << result << endl;
259 * //<output>: At 4:34:20 PM on 23-Mar-98, there was a disturbance
260 * // in the Force on planet 7.
264 * Typically, the message format will come from resources, and the
265 * arguments will be dynamically set at runtime.
271 * success = U_ZERO_ERROR;
272 * Formattable testArgs[] = {3L, "MyDisk"};
274 * MessageFormat form(
275 * "The disk \"{1}\" contains {0} file(s).", success );
277 * UnicodeString string;
278 * FieldPosition fpos = 0;
279 * cout << "format: " << form.format(testArgs, 2, string, fpos, success ) << endl;
281 * // output, with different testArgs:
282 * // output: The disk "MyDisk" contains 0 file(s).
283 * // output: The disk "MyDisk" contains 1 file(s).
284 * // output: The disk "MyDisk" contains 1,273 file(s).
289 * <p>For messages that include plural forms, you can use a plural argument:
292 * success = U_ZERO_ERROR;
293 * MessageFormat msgFmt(
294 * "{num_files, plural, "
295 * "=0{There are no files on disk \"{disk_name}\".}"
296 * "=1{There is one file on disk \"{disk_name}\".}"
297 * "other{There are # files on disk \"{disk_name}\".}}",
300 * FieldPosition fpos = 0;
301 * Formattable testArgs[] = {0L, "MyDisk"};
302 * UnicodeString testArgsNames[] = {"num_files", "disk_name"};
303 * UnicodeString result;
304 * cout << msgFmt.format(testArgs, testArgsNames, 2, result, fpos, 0, success);
306 * cout << msgFmt.format(testArgs, testArgsNames, 2, result, fpos, 0, success);
309 * There are no files on disk "MyDisk".
310 * There are 3 files on "MyDisk".
312 * See {@link PluralFormat} and {@link PluralRules} for details.
314 * <h4><a name="synchronization">Synchronization</a></h4>
316 * <p>MessageFormats are not synchronized.
317 * It is recommended to create separate format instances for each thread.
318 * If multiple threads access a format concurrently, it must be synchronized
323 class U_I18N_API MessageFormat
: public Format
{
325 #ifndef U_HIDE_OBSOLETE_API
327 * Enum type for kMaxFormat.
328 * @obsolete ICU 3.0. The 10-argument limit was removed as of ICU 2.6,
329 * rendering this enum type obsolete.
333 * The maximum number of arguments.
334 * @obsolete ICU 3.0. The 10-argument limit was removed as of ICU 2.6,
335 * rendering this constant obsolete.
339 #endif /* U_HIDE_OBSOLETE_API */
342 * Constructs a new MessageFormat using the given pattern and the
345 * @param pattern Pattern used to construct object.
346 * @param status Input/output error code. If the
347 * pattern cannot be parsed, set to failure code.
350 MessageFormat(const UnicodeString
& pattern
,
354 * Constructs a new MessageFormat using the given pattern and locale.
355 * @param pattern Pattern used to construct object.
356 * @param newLocale The locale to use for formatting dates and numbers.
357 * @param status Input/output error code. If the
358 * pattern cannot be parsed, set to failure code.
361 MessageFormat(const UnicodeString
& pattern
,
362 const Locale
& newLocale
,
365 * Constructs a new MessageFormat using the given pattern and locale.
366 * @param pattern Pattern used to construct object.
367 * @param newLocale The locale to use for formatting dates and numbers.
368 * @param parseError Struct to receive information on the position
369 * of an error within the pattern.
370 * @param status Input/output error code. If the
371 * pattern cannot be parsed, set to failure code.
374 MessageFormat(const UnicodeString
& pattern
,
375 const Locale
& newLocale
,
376 UParseError
& parseError
,
379 * Constructs a new MessageFormat from an existing one.
382 MessageFormat(const MessageFormat
&);
385 * Assignment operator.
388 const MessageFormat
& operator=(const MessageFormat
&);
394 virtual ~MessageFormat();
397 * Clones this Format object polymorphically. The caller owns the
398 * result and should delete it when done.
401 virtual Format
* clone(void) const;
404 * Returns true if the given Format objects are semantically equal.
405 * Objects of different subclasses are considered unequal.
406 * @param other the object to be compared with.
407 * @return true if the given Format objects are semantically equal.
410 virtual UBool
operator==(const Format
& other
) const;
413 * Sets the locale to be used for creating argument Format objects.
414 * @param theLocale the new locale value to be set.
417 virtual void setLocale(const Locale
& theLocale
);
420 * Gets the locale used for creating argument Format objects.
421 * format information.
422 * @return the locale of the object.
425 virtual const Locale
& getLocale(void) const;
428 * Applies the given pattern string to this message format.
430 * @param pattern The pattern to be applied.
431 * @param status Input/output error code. If the
432 * pattern cannot be parsed, set to failure code.
435 virtual void applyPattern(const UnicodeString
& pattern
,
438 * Applies the given pattern string to this message format.
440 * @param pattern The pattern to be applied.
441 * @param parseError Struct to receive information on the position
442 * of an error within the pattern.
443 * @param status Input/output error code. If the
444 * pattern cannot be parsed, set to failure code.
447 virtual void applyPattern(const UnicodeString
& pattern
,
448 UParseError
& parseError
,
452 * Sets the UMessagePatternApostropheMode and the pattern used by this message format.
453 * Parses the pattern and caches Format objects for simple argument types.
454 * Patterns and their interpretation are specified in the
455 * <a href="#patterns">class description</a>.
457 * This method is best used only once on a given object to avoid confusion about the mode,
458 * and after constructing the object with an empty pattern string to minimize overhead.
460 * @param pattern The pattern to be applied.
461 * @param aposMode The new apostrophe mode.
462 * @param parseError Struct to receive information on the position
463 * 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 UMessagePatternApostropheMode aposMode
,
471 UParseError
* parseError
,
475 * @return this instance's UMessagePatternApostropheMode.
478 UMessagePatternApostropheMode
getApostropheMode() const {
479 return msgPattern
.getApostropheMode();
483 * Returns a pattern that can be used to recreate this object.
485 * @param appendTo Output parameter to receive the pattern.
486 * Result is appended to existing contents.
487 * @return Reference to 'appendTo' parameter.
490 virtual UnicodeString
& toPattern(UnicodeString
& appendTo
) const;
494 * See the class description about format numbering.
495 * The caller should not delete the Format objects after this call.
496 * <EM>The array formatsToAdopt is not itself adopted.</EM> Its
497 * ownership is retained by the caller. If the call fails because
498 * memory cannot be allocated, then the formats will be deleted
499 * by this method, and this object will remain unchanged.
501 * <p>If this format uses named arguments, the new formats are discarded
502 * and this format remains unchanged.
505 * @param formatsToAdopt the format to be adopted.
506 * @param count the size of the array.
508 virtual void adoptFormats(Format
** formatsToAdopt
, int32_t count
);
512 * See the class description about format numbering.
513 * Each item in the array is cloned into the internal array.
514 * If the call fails because memory cannot be allocated, then this
515 * object will remain unchanged.
517 * <p>If this format uses named arguments, the new formats are discarded
518 * and this format remains unchanged.
521 * @param newFormats the new format to be set.
522 * @param cnt the size of the array.
524 virtual void setFormats(const Format
** newFormats
, int32_t cnt
);
528 * Sets one subformat.
529 * See the class description about format numbering.
530 * The caller should not delete the Format object after this call.
531 * If the number is over the number of formats already set,
532 * the item will be deleted and ignored.
534 * <p>If this format uses named arguments, the new format is discarded
535 * and this format remains unchanged.
538 * @param formatNumber index of the subformat.
539 * @param formatToAdopt the format to be adopted.
541 virtual void adoptFormat(int32_t formatNumber
, Format
* formatToAdopt
);
544 * Sets one subformat.
545 * See the class description about format numbering.
546 * If the number is over the number of formats already set,
547 * the item will be ignored.
548 * @param formatNumber index of the subformat.
549 * @param format the format to be set.
552 virtual void setFormat(int32_t formatNumber
, const Format
& format
);
555 * Gets format names. This function returns formatNames in StringEnumerations
556 * which can be used with getFormat() and setFormat() to export formattable
557 * array from current MessageFormat to another. It is the caller's responsibility
558 * to delete the returned formatNames.
559 * @param status output param set to success/failure code.
562 virtual StringEnumeration
* getFormatNames(UErrorCode
& status
);
565 * Gets subformat pointer for given format name.
566 * This function supports both named and numbered
567 * arguments. If numbered, the formatName is the
568 * corresponding UnicodeStrings (e.g. "0", "1", "2"...).
569 * The returned Format object should not be deleted by the caller,
570 * nor should the ponter of other object . The pointer and its
571 * contents remain valid only until the next call to any method
572 * of this class is made with this object.
573 * @param formatName the name or number specifying a format
574 * @param status output param set to success/failure code.
577 virtual Format
* getFormat(const UnicodeString
& formatName
, UErrorCode
& status
);
580 * Sets one subformat for given format name.
581 * See the class description about format name.
582 * This function supports both named and numbered
583 * arguments-- if numbered, the formatName is the
584 * corresponding UnicodeStrings (e.g. "0", "1", "2"...).
585 * If there is no matched formatName or wrong type,
586 * the item will be ignored.
587 * @param formatName Name of the subformat.
588 * @param format the format to be set.
589 * @param status output param set to success/failure code.
592 virtual void setFormat(const UnicodeString
& formatName
, const Format
& format
, UErrorCode
& status
);
595 * Sets one subformat for given format name.
596 * See the class description about format name.
597 * This function supports both named and numbered
598 * arguments-- if numbered, the formatName is the
599 * corresponding UnicodeStrings (e.g. "0", "1", "2"...).
600 * If there is no matched formatName or wrong type,
601 * the item will be ignored.
602 * The caller should not delete the Format object after this call.
603 * @param formatName Name of the subformat.
604 * @param formatToAdopt Format to be adopted.
605 * @param status output param set to success/failure code.
608 virtual void adoptFormat(const UnicodeString
& formatName
, Format
* formatToAdopt
, UErrorCode
& status
);
611 * Gets an array of subformats of this object. The returned array
612 * should not be deleted by the caller, nor should the pointers
613 * within the array. The array and its contents remain valid only
614 * until the next call to this format. See the class description
615 * about format numbering.
617 * @param count output parameter to receive the size of the array
618 * @return an array of count Format* objects, or NULL if out of
619 * memory. Any or all of the array elements may be NULL.
622 virtual const Format
** getFormats(int32_t& count
) const;
625 using Format::format
;
628 * Formats the given array of arguments into a user-readable string.
629 * Does not take ownership of the Formattable* array or its contents.
631 * <p>If this format uses named arguments, appendTo is unchanged and
632 * status is set to U_ILLEGAL_ARGUMENT_ERROR.
634 * @param source An array of objects to be formatted.
635 * @param count The number of elements of 'source'.
636 * @param appendTo Output parameter to receive result.
637 * Result is appended to existing contents.
638 * @param ignore Not used; inherited from base class API.
639 * @param status Input/output error code. If the
640 * pattern cannot be parsed, set to failure code.
641 * @return Reference to 'appendTo' parameter.
644 UnicodeString
& format(const Formattable
* source
,
646 UnicodeString
& appendTo
,
647 FieldPosition
& ignore
,
648 UErrorCode
& status
) const;
651 * Formats the given array of arguments into a user-readable string
652 * using the given pattern.
654 * <p>If this format uses named arguments, appendTo is unchanged and
655 * status is set to U_ILLEGAL_ARGUMENT_ERROR.
657 * @param pattern The pattern.
658 * @param arguments An array of objects to be formatted.
659 * @param count The number of elements of 'source'.
660 * @param appendTo Output parameter to receive result.
661 * Result is appended to existing contents.
662 * @param status Input/output error code. If the
663 * pattern cannot be parsed, set to failure code.
664 * @return Reference to 'appendTo' parameter.
667 static UnicodeString
& format(const UnicodeString
& pattern
,
668 const Formattable
* arguments
,
670 UnicodeString
& appendTo
,
674 * Formats the given array of arguments into a user-readable
675 * string. The array must be stored within a single Formattable
676 * object of type kArray. If the Formattable object type is not of
677 * type kArray, then returns a failing UErrorCode.
679 * <p>If this format uses named arguments, appendTo is unchanged and
680 * status is set to U_ILLEGAL_ARGUMENT_ERROR.
682 * @param obj A Formattable of type kArray containing
683 * arguments to be formatted.
684 * @param appendTo Output parameter to receive result.
685 * Result is appended to existing contents.
686 * @param pos On input: an alignment field, if desired.
687 * On output: the offsets of the alignment field.
688 * @param status Input/output error code. If the
689 * pattern cannot be parsed, set to failure code.
690 * @return Reference to 'appendTo' parameter.
693 virtual UnicodeString
& format(const Formattable
& obj
,
694 UnicodeString
& appendTo
,
696 UErrorCode
& status
) const;
699 * Formats the given array of arguments into a user-defined argument name
700 * array. This function supports both named and numbered
701 * arguments-- if numbered, the formatName is the
702 * corresponding UnicodeStrings (e.g. "0", "1", "2"...).
704 * @param argumentNames argument name array
705 * @param arguments An array of objects to be formatted.
706 * @param count The number of elements of 'argumentNames' and
707 * arguments. The number of argumentNames and arguments
709 * @param appendTo Output parameter to receive result.
710 * Result is appended to existing contents.
711 * @param status Input/output error code. If the
712 * pattern cannot be parsed, set to failure code.
713 * @return Reference to 'appendTo' parameter.
716 UnicodeString
& format(const UnicodeString
* argumentNames
,
717 const Formattable
* arguments
,
719 UnicodeString
& appendTo
,
720 UErrorCode
& status
) const;
722 * Parses the given string into an array of output arguments.
724 * @param source String to be parsed.
725 * @param pos On input, starting position for parse. On output,
726 * final position after parse. Unchanged if parse
728 * @param count Output parameter to receive the number of arguments
730 * @return an array of parsed arguments. The caller owns both
731 * the array and its contents.
734 virtual Formattable
* parse(const UnicodeString
& source
,
736 int32_t& count
) const;
739 * Parses the given string into an array of output arguments.
741 * <p>If this format uses named arguments, status is set to
742 * U_ARGUMENT_TYPE_MISMATCH.
744 * @param source String to be parsed.
745 * @param count Output param to receive size of returned array.
746 * @param status Input/output error code. If the
747 * pattern cannot be parsed, set to failure code.
748 * @return an array of parsed arguments. The caller owns both
749 * the array and its contents. Returns NULL if status is not U_ZERO_ERROR.
753 virtual Formattable
* parse(const UnicodeString
& source
,
755 UErrorCode
& status
) const;
758 * Parses the given string into an array of output arguments
759 * stored within a single Formattable of type kArray.
761 * @param source The string to be parsed into an object.
762 * @param result Formattable to be set to the parse result.
763 * If parse fails, return contents are undefined.
764 * @param pos On input, starting position for parse. On output,
765 * final position after parse. Unchanged if parse
769 virtual void parseObject(const UnicodeString
& source
,
771 ParsePosition
& pos
) const;
774 * Convert an 'apostrophe-friendly' pattern into a standard
775 * pattern. Standard patterns treat all apostrophes as
776 * quotes, which is problematic in some languages, e.g.
777 * French, where apostrophe is commonly used. This utility
778 * assumes that only an unpaired apostrophe immediately before
779 * a brace is a true quote. Other unpaired apostrophes are paired,
780 * and the resulting standard pattern string is returned.
782 * <p><b>Note</b> it is not guaranteed that the returned pattern
783 * is indeed a valid pattern. The only effect is to convert
784 * between patterns having different quoting semantics.
786 * @param pattern the 'apostrophe-friendly' patttern to convert
787 * @param status Input/output error code. If the pattern
788 * cannot be parsed, the failure code is set.
789 * @return the standard equivalent of the original pattern
792 static UnicodeString
autoQuoteApostrophe(const UnicodeString
& pattern
,
797 * Returns true if this MessageFormat uses named arguments,
798 * and false otherwise. See class description.
800 * @return true if named arguments are used.
803 UBool
usesNamedArguments() const;
806 #ifndef U_HIDE_INTERNAL_API
808 * This API is for ICU internal use only.
809 * Please do not use it.
811 * Returns argument types count in the parsed pattern.
812 * Used to distinguish pattern "{0} d" and "d".
814 * @return The number of formattable types in the pattern
817 int32_t getArgTypeCount() const;
818 #endif /* U_HIDE_INTERNAL_API */
821 * Returns a unique class ID POLYMORPHICALLY. Pure virtual override.
822 * This method is to implement a simple version of RTTI, since not all
823 * C++ compilers support genuine RTTI. Polymorphic operator==() and
824 * clone() methods call this method.
826 * @return The class ID for this object. All objects of a
827 * given class have the same class ID. Objects of
828 * other classes have different class IDs.
831 virtual UClassID
getDynamicClassID(void) const;
834 * Return the class ID for this class. This is useful only for
835 * comparing to a return value from getDynamicClassID(). For example:
837 * . Base* polymorphic_pointer = createPolymorphicObject();
838 * . if (polymorphic_pointer->getDynamicClassID() ==
839 * . Derived::getStaticClassID()) ...
841 * @return The class ID for all objects of this class.
844 static UClassID U_EXPORT2
getStaticClassID(void);
846 #ifndef U_HIDE_INTERNAL_API
848 * Compares two Format objects. This is used for constructing the hash
851 * @param left pointer to a Format object. Must not be NULL.
852 * @param right pointer to a Format object. Must not be NULL.
854 * @return whether the two objects are the same
857 static UBool
equalFormats(const void* left
, const void* right
);
858 #endif /* U_HIDE_INTERNAL_API */
863 MessagePattern msgPattern
;
864 Format
** formatAliases
; // see getFormats
865 int32_t formatAliasesCapacity
;
867 MessageFormat(); // default constructor not implemented
870 * This provider helps defer instantiation of a PluralRules object
871 * until we actually need to select a keyword.
872 * For example, if the number matches an explicit-value selector like "=1"
873 * we do not need any PluralRules.
875 class U_I18N_API PluralSelectorProvider
: public PluralFormat::PluralSelector
{
877 PluralSelectorProvider(const MessageFormat
&mf
, UPluralType type
);
878 virtual ~PluralSelectorProvider();
879 virtual UnicodeString
select(void *ctx
, double number
, UErrorCode
& ec
) const;
883 const MessageFormat
&msgFormat
;
889 * A MessageFormat formats an array of arguments. Each argument
890 * has an expected type, based on the pattern. For example, if
891 * the pattern contains the subformat "{3,number,integer}", then
892 * we expect argument 3 to have type Formattable::kLong. This
893 * array needs to grow dynamically if the MessageFormat is
896 Formattable::Type
* argTypes
;
897 int32_t argTypeCount
;
898 int32_t argTypeCapacity
;
901 * TRUE if there are different argTypes for the same argument.
902 * This only matters when the MessageFormat is used in the plain C (umsg_xxx) API
903 * where the pattern argTypes determine how the va_arg list is read.
905 UBool hasArgTypeConflicts
;
907 // Variable-size array management
908 UBool
allocateArgTypes(int32_t capacity
, UErrorCode
& status
);
911 * Default Format objects used when no format is specified and a
912 * numeric or date argument is formatted. These are volatile
913 * cache objects maintained only for performance. They do not
914 * participate in operator=(), copy constructor(), nor
917 NumberFormat
* defaultNumberFormat
;
918 DateFormat
* defaultDateFormat
;
920 UHashtable
* cachedFormatters
;
921 UHashtable
* customFormatArgStarts
;
923 PluralSelectorProvider pluralProvider
;
924 PluralSelectorProvider ordinalProvider
;
927 * Method to retrieve default formats (or NULL on failure).
928 * These are semantically const, but may modify *this.
930 const NumberFormat
* getDefaultNumberFormat(UErrorCode
&) const;
931 const DateFormat
* getDefaultDateFormat(UErrorCode
&) const;
934 * Finds the word s, in the keyword list and returns the located index.
935 * @param s the keyword to be searched for.
936 * @param list the list of keywords to be searched with.
937 * @return the index of the list which matches the keyword s.
939 static int32_t findKeyword( const UnicodeString
& s
,
940 const UChar
* const *list
);
943 * Thin wrapper around the format(... AppendableWrapper ...) variant.
944 * Wraps the destination UnicodeString into an AppendableWrapper and
945 * supplies default values for some other parameters.
947 UnicodeString
& format(const Formattable
* arguments
,
948 const UnicodeString
*argumentNames
,
950 UnicodeString
& appendTo
,
952 UErrorCode
& status
) const;
955 * Formats the arguments and writes the result into the
956 * AppendableWrapper, updates the field position.
958 * @param msgStart Index to msgPattern part to start formatting from.
959 * @param plNumber NULL except when formatting a plural argument sub-message
960 * where a '#' is replaced by the format string for this number.
961 * @param arguments The formattable objects array. (Must not be NULL.)
962 * @param argumentNames NULL if numbered values are used. Otherwise the same
963 * length as "arguments", and each entry is the name of the
964 * corresponding argument in "arguments".
965 * @param cnt The length of arguments (and of argumentNames if that is not NULL).
966 * @param appendTo Output parameter to receive the result.
967 * The result string is appended to existing contents.
968 * @param pos Field position status.
969 * @param success The error code status.
971 void format(int32_t msgStart
,
972 const void *plNumber
,
973 const Formattable
* arguments
,
974 const UnicodeString
*argumentNames
,
976 AppendableWrapper
& appendTo
,
978 UErrorCode
& success
) const;
980 UnicodeString
getArgName(int32_t partIndex
);
982 void setArgStartFormat(int32_t argStart
, Format
* formatter
, UErrorCode
& status
);
984 void setCustomArgStartFormat(int32_t argStart
, Format
* formatter
, UErrorCode
& status
);
986 int32_t nextTopLevelArgStart(int32_t partIndex
) const;
988 UBool
argNameMatches(int32_t partIndex
, const UnicodeString
& argName
, int32_t argNumber
);
990 void cacheExplicitFormats(UErrorCode
& status
);
992 Format
* createAppropriateFormat(UnicodeString
& type
,
993 UnicodeString
& style
,
994 Formattable::Type
& formattableType
,
995 UParseError
& parseError
,
998 const Formattable
* getArgFromListByName(const Formattable
* arguments
,
999 const UnicodeString
*argumentNames
,
1000 int32_t cnt
, UnicodeString
& name
) const;
1002 Formattable
* parse(int32_t msgStart
,
1003 const UnicodeString
& source
,
1006 UErrorCode
& ec
) const;
1008 FieldPosition
* updateMetaData(AppendableWrapper
& dest
, int32_t prevLength
,
1009 FieldPosition
* fp
, const Formattable
* argId
) const;
1012 * Finds the "other" sub-message.
1013 * @param partIndex the index of the first PluralFormat argument style part.
1014 * @return the "other" sub-message start part index.
1016 int32_t findOtherSubMessage(int32_t partIndex
) const;
1019 * Returns the ARG_START index of the first occurrence of the plural number in a sub-message.
1020 * Returns -1 if it is a REPLACE_NUMBER.
1021 * Returns 0 if there is neither.
1023 int32_t findFirstPluralNumberArg(int32_t msgStart
, const UnicodeString
&argName
) const;
1025 Format
* getCachedFormatter(int32_t argumentNumber
) const;
1027 UnicodeString
getLiteralStringUntilNextArgument(int32_t from
) const;
1029 void copyObjects(const MessageFormat
& that
, UErrorCode
& ec
);
1031 void formatComplexSubMessage(int32_t msgStart
,
1032 const void *plNumber
,
1033 const Formattable
* arguments
,
1034 const UnicodeString
*argumentNames
,
1036 AppendableWrapper
& appendTo
,
1037 UErrorCode
& success
) const;
1040 * Convenience method that ought to be in NumberFormat
1042 NumberFormat
* createIntegerFormat(const Locale
& locale
, UErrorCode
& status
) const;
1045 * Returns array of argument types in the parsed pattern
1046 * for use in C API. Only for the use of umsg_vformat(). Not
1047 * for public consumption.
1048 * @param listCount Output parameter to receive the size of array
1049 * @return The array of formattable types in the pattern
1051 const Formattable::Type
* getArgTypeList(int32_t& listCount
) const {
1052 listCount
= argTypeCount
;
1057 * Resets the internal MessagePattern, and other associated caches.
1059 void resetPattern();
1062 * A DummyFormatter that we use solely to store a NULL value. UHash does
1063 * not support storing NULL values.
1065 class U_I18N_API DummyFormat
: public Format
{
1067 virtual UBool
operator==(const Format
&) const;
1068 virtual Format
* clone() const;
1069 virtual UnicodeString
& format(const Formattable
& obj
,
1070 UnicodeString
& appendTo
,
1071 UErrorCode
& status
) const;
1072 virtual UnicodeString
& format(const Formattable
&,
1073 UnicodeString
& appendTo
,
1075 UErrorCode
& status
) const;
1076 virtual UnicodeString
& format(const Formattable
& obj
,
1077 UnicodeString
& appendTo
,
1078 FieldPositionIterator
* posIter
,
1079 UErrorCode
& status
) const;
1080 virtual void parseObject(const UnicodeString
&,
1082 ParsePosition
&) const;
1085 friend class MessageFormatAdapter
; // getFormatTypeList() access
1090 #endif /* #if !UCONFIG_NO_FORMATTING */