]>
Commit | Line | Data |
---|---|---|
b75a7d8f A |
1 | /* |
2 | * Copyright (C) {1997-2003}, International Business Machines Corporation and others. All Rights Reserved. | |
3 | ******************************************************************************** | |
4 | * | |
5 | * File MSGFMT.H | |
6 | * | |
7 | * Modification History: | |
8 | * | |
9 | * Date Name Description | |
10 | * 02/19/97 aliu Converted from java. | |
11 | * 03/20/97 helena Finished first cut of implementation. | |
12 | * 07/22/98 stephen Removed operator!= (defined in Format) | |
13 | * 08/19/2002 srl Removing Javaisms | |
14 | ******************************************************************************** | |
15 | */ | |
16 | ||
17 | #ifndef MSGFMT_H | |
18 | #define MSGFMT_H | |
19 | ||
20 | #include "unicode/utypes.h" | |
21 | ||
22 | #if !UCONFIG_NO_FORMATTING | |
23 | ||
24 | #include "unicode/format.h" | |
25 | #include "unicode/locid.h" | |
26 | #include "unicode/parseerr.h" | |
27 | ||
28 | U_NAMESPACE_BEGIN | |
29 | ||
30 | class NumberFormat; | |
31 | class DateFormat; | |
32 | ||
33 | /** | |
34 | * A MessageFormat produces concatenated messages in a | |
35 | * language-neutral way. It should be used for all string | |
36 | * concatenations that are visible to end users. | |
37 | * <P> | |
38 | * A MessageFormat contains an array of <EM>subformats</EM> arranged | |
39 | * within a <EM>template string</EM>. Together, the subformats and | |
40 | * template string determine how the MessageFormat will operate during | |
41 | * formatting and parsing. | |
42 | * <P> | |
43 | * Typically, both the subformats and the template string are | |
44 | * specified at once in a <EM>pattern</EM>. By using different | |
45 | * patterns for different locales, messages may be localized. | |
46 | * <P> | |
47 | * During formatting, the MessageFormat takes an array of arguments | |
48 | * and produces a user-readable string. Each argument is a | |
49 | * Formattable object; they may be passed in in an array, or as a | |
50 | * single Formattable object which itself contains an array. Each | |
51 | * argument is matched up with its corresponding subformat, which then | |
52 | * formats it into a string. The resultant strings are then assembled | |
53 | * within the string template of the MessageFormat to produce the | |
54 | * final output string. | |
55 | * <P> | |
56 | * During parsing, an input string is matched against the string | |
57 | * template of the MessageFormat to produce an array of Formattable | |
58 | * objects. Plain text of the template string is matched directly | |
59 | * against intput text. At each position in the template string where | |
60 | * a subformat is located, the subformat is called to parse the | |
61 | * corresponding segment of input text to produce an output argument. | |
62 | * In this way, an array of arguments is created which together | |
63 | * constitute the parse result. | |
64 | * <P> | |
65 | * Parsing may fail or produce unexpected results in a number of | |
66 | * circumstances. | |
67 | * <UL> | |
68 | * <LI>If one of the arguments does not occur in the pattern, it | |
69 | * will be returned as a default Formattable. | |
70 | * <LI>If the format of an argument is loses information, such as with | |
71 | * a choice format where a large number formats to "many", then the | |
72 | * parse may not correspond to the originally formatted argument. | |
73 | * <LI>MessageFormat does not handle ChoiceFormat recursion during | |
74 | * parsing; such parses will fail. | |
75 | * <LI>Parsing will not always find a match (or the correct match) if | |
76 | * some part of the parse is ambiguous. For example, if the pattern | |
77 | * "{1},{2}" is used with the string arguments {"a,b", "c"}, it will | |
78 | * format as "a,b,c". When the result is parsed, it will return {"a", | |
79 | * "b,c"}. | |
80 | * <LI>If a single argument is formatted more than once in the string, | |
81 | * then the rightmost subformat in the pattern string will produce the | |
82 | * parse result; prior subformats with the same argument index will | |
83 | * have no effect. | |
84 | * </UL> | |
85 | * Here are some examples of usage: | |
86 | * <P> | |
87 | * Example 1: | |
88 | * <pre> | |
89 | * \code | |
90 | * UErrorCode success = U_ZERO_ERROR; | |
91 | * GregorianCalendar cal(success); | |
92 | * Formattable arguments[] = { | |
93 | * 7L, | |
94 | * Formattable( (Date) cal.getTime(success), Formattable::kIsDate), | |
95 | * "a disturbance in the Force" | |
96 | * }; | |
97 | * | |
98 | * UnicodeString result; | |
99 | * MessageFormat::format( | |
100 | * "At {1,time} on {1,date}, there was {2} on planet {0,number}.", | |
101 | * arguments, 3, result, success ); | |
102 | * | |
103 | * cout << "result: " << result << endl; | |
104 | * //<output>: At 4:34:20 PM on 23-Mar-98, there was a disturbance | |
105 | * // in the Force on planet 7. | |
106 | * \endcode | |
107 | * </pre> | |
108 | * Typically, the message format will come from resources, and the | |
109 | * arguments will be dynamically set at runtime. | |
110 | * <P> | |
111 | * Example 2: | |
112 | * <pre> | |
113 | * \code | |
114 | * success = U_ZERO_ERROR; | |
115 | * Formattable testArgs[] = {3L, "MyDisk"}; | |
116 | * | |
117 | * MessageFormat form( | |
118 | * "The disk \"{1}\" contains {0} file(s).", success ); | |
119 | * | |
120 | * UnicodeString string; | |
121 | * FieldPosition fpos = 0; | |
122 | * cout << "format: " << form.format(testArgs, 2, string, fpos, success ) << endl; | |
123 | * | |
124 | * // output, with different testArgs: | |
125 | * // output: The disk "MyDisk" contains 0 file(s). | |
126 | * // output: The disk "MyDisk" contains 1 file(s). | |
127 | * // output: The disk "MyDisk" contains 1,273 file(s). | |
128 | * \endcode | |
129 | * </pre> | |
130 | * | |
131 | * The pattern is of the following form. Legend: | |
132 | * <pre> | |
133 | * \code | |
134 | * {optional item} | |
135 | * (group that may be repeated)* | |
136 | * \endcode | |
137 | * </pre> | |
138 | * Do not confuse optional items with items inside quotes braces, such | |
139 | * as this: "{". Quoted braces are literals. | |
140 | * <pre> | |
141 | * \code | |
142 | * messageFormatPattern := string ( "{" messageFormatElement "}" string )* | |
143 | * | |
144 | * messageFormatElement := argumentIndex { "," elementFormat } | |
145 | * | |
146 | * elementFormat := "time" { "," datetimeStyle } | |
147 | * | "date" { "," datetimeStyle } | |
148 | * | "number" { "," numberStyle } | |
149 | * | "choice" "," choiceStyle | |
150 | * | |
151 | * datetimeStyle := "short" | |
152 | * | "medium" | |
153 | * | "long" | |
154 | * | "full" | |
155 | * | dateFormatPattern | |
156 | * | |
157 | * numberStyle := "currency" | |
158 | * | "percent" | |
159 | * | "integer" | |
160 | * | numberFormatPattern | |
161 | * | |
162 | * choiceStyle := choiceFormatPattern | |
163 | * \endcode | |
164 | * </pre> | |
165 | * If there is no elementFormat, then the argument must be a string, | |
166 | * which is substituted. If there is no dateTimeStyle or numberStyle, | |
167 | * then the default format is used (e.g. NumberFormat::createInstance(), | |
168 | * DateFormat::createTimeInstance(DateFormat::kDefault, ...) or DateFormat::createDateInstance(DateFormat::kDefault, ...). For | |
169 | * a ChoiceFormat, the pattern must always be specified, since there | |
170 | * is no default. | |
171 | * <P> | |
172 | * In strings, single quotes can be used to quote syntax characters. | |
173 | * A literal single quote is represented by '', both within and outside | |
174 | * of single-quoted segments. Inside a | |
175 | * messageFormatElement, quotes are <EM>not</EM> removed. For example, | |
176 | * {1,number,$'#',##} will produce a number format with the pound-sign | |
177 | * quoted, with a result such as: "$#31,45". | |
178 | * <P> | |
179 | * If a pattern is used, then unquoted braces in the pattern, if any, | |
180 | * must match: that is, "ab {0} de" and "ab '}' de" are ok, but "ab | |
181 | * {0'}' de" and "ab } de" are not. | |
182 | * <P> | |
183 | * The argumentIndex is a non-negative integer, which corresponds to the | |
184 | * index of the arguments presented in an array to be formatted. The | |
185 | * first argument has argumentIndex 0. | |
186 | * <P> | |
187 | * It is acceptable to have unused arguments in the array. With missing | |
188 | * arguments or arguments that are not of the right class for the | |
189 | * specified format, a failing UErrorCode result is set. | |
190 | * <P> | |
191 | * For more sophisticated patterns, you can use a ChoiceFormat to get | |
192 | * output: | |
193 | * <pre> | |
194 | * \code | |
195 | * UErrorCode success = U_ZERO_ERROR; | |
196 | * MessageFormat* form("The disk \"{1}\" contains {0}.", success); | |
197 | * double filelimits[] = {0,1,2}; | |
198 | * UnicodeString filepart[] = {"no files","one file","{0,number} files"}; | |
199 | * ChoiceFormat* fileform = new ChoiceFormat(filelimits, filepart, 3); | |
200 | * form.setFormat(1, *fileform); // NOT zero, see below | |
201 | * | |
202 | * Formattable testArgs[] = {1273L, "MyDisk"}; | |
203 | * | |
204 | * UnicodeString string; | |
205 | * FieldPosition fpos = 0; | |
206 | * cout << form.format(testArgs, 2, string, fpos, success) << endl; | |
207 | * | |
208 | * // output, with different testArgs | |
209 | * // output: The disk "MyDisk" contains no files. | |
210 | * // output: The disk "MyDisk" contains one file. | |
211 | * // output: The disk "MyDisk" contains 1,273 files. | |
212 | * \endcode | |
213 | * </pre> | |
214 | * You can either do this programmatically, as in the above example, | |
215 | * or by using a pattern (see ChoiceFormat for more information) as in: | |
216 | * <pre> | |
217 | * \code | |
218 | * form.applyPattern( | |
219 | * "There {0,choice,0#are no files|1#is one file|1<are {0,number,integer} files}."); | |
220 | * \endcode | |
221 | * </pre> | |
222 | * <P> | |
223 | * <EM>Note:</EM> As we see above, the string produced by a ChoiceFormat in | |
224 | * MessageFormat is treated specially; occurences of '{' are used to | |
225 | * indicated subformats, and cause recursion. If you create both a | |
226 | * MessageFormat and ChoiceFormat programmatically (instead of using | |
227 | * the string patterns), then be careful not to produce a format that | |
228 | * recurses on itself, which will cause an infinite loop. | |
229 | * <P> | |
230 | * <EM>Note:<EM>Subformats are numbered by their order in the pattern. | |
231 | * This is <EM>not</EM> the same as the argumentIndex. | |
232 | * <pre> | |
233 | * \code | |
234 | * For example: with "abc{2}def{3}ghi{0}...", | |
235 | * | |
236 | * format0 affects the first variable {2} | |
237 | * format1 affects the second variable {3} | |
238 | * format2 affects the second variable {0} | |
239 | * \endcode | |
240 | * </pre> | |
241 | */ | |
242 | class U_I18N_API MessageFormat : public Format { | |
243 | public: | |
244 | /** | |
245 | * Enum type for kMaxFormat. | |
246 | * @obsolete ICU 3.0. The 10-argument limit was removed as of ICU 2.6, | |
247 | * rendering this enum type obsolete. | |
248 | */ | |
249 | enum EFormatNumber { | |
250 | /** | |
251 | * The maximum number of arguments. | |
252 | * @obsolete ICU 3.0. The 10-argument limit was removed as of ICU 2.6, | |
253 | * rendering this constant obsolete. | |
254 | */ | |
255 | kMaxFormat = 10 | |
256 | }; | |
257 | ||
258 | /** | |
259 | * Constructs a new MessageFormat using the given pattern and the | |
260 | * default locale. | |
261 | * | |
262 | * @param pattern Pattern used to construct object. | |
263 | * @param status Input/output error code. If the | |
264 | * pattern cannot be parsed, set to failure code. | |
265 | * @stable ICU 2.0 | |
266 | */ | |
267 | MessageFormat(const UnicodeString& pattern, | |
268 | UErrorCode &status); | |
269 | ||
270 | /** | |
271 | * Constructs a new MessageFormat using the given pattern and locale. | |
272 | * @param pattern Pattern used to construct object. | |
273 | * @param newLocale The locale to use for formatting dates and numbers. | |
274 | * @param status Input/output error code. If the | |
275 | * pattern cannot be parsed, set to failure code. | |
276 | * @stable ICU 2.0 | |
277 | */ | |
278 | MessageFormat(const UnicodeString& pattern, | |
279 | const Locale& newLocale, | |
280 | UErrorCode& status); | |
281 | /** | |
282 | * Constructs a new MessageFormat using the given pattern and locale. | |
283 | * @param pattern Pattern used to construct object. | |
284 | * @param newLocale The locale to use for formatting dates and numbers. | |
285 | * @param parseError Struct to recieve information on position | |
286 | * of error within the pattern. | |
287 | * @param status Input/output error code. If the | |
288 | * pattern cannot be parsed, set to failure code. | |
289 | * @stable ICU 2.0 | |
290 | */ | |
291 | MessageFormat(const UnicodeString& pattern, | |
292 | const Locale& newLocale, | |
293 | UParseError& parseError, | |
294 | UErrorCode& status); | |
295 | /** | |
296 | * Constructs a new MessageFormat from an existing one. | |
297 | * @stable ICU 2.0 | |
298 | */ | |
299 | MessageFormat(const MessageFormat&); | |
300 | ||
301 | /** | |
302 | * Assignment operator. | |
303 | * @stable ICU 2.0 | |
304 | */ | |
305 | const MessageFormat& operator=(const MessageFormat&); | |
306 | ||
307 | /** | |
308 | * Destructor. | |
309 | * @stable ICU 2.0 | |
310 | */ | |
311 | virtual ~MessageFormat(); | |
312 | ||
313 | /** | |
314 | * Clones this Format object polymorphically. The caller owns the | |
315 | * result and should delete it when done. | |
316 | * @stable ICU 2.0 | |
317 | */ | |
318 | virtual Format* clone(void) const; | |
319 | ||
320 | /** | |
321 | * Returns true if the given Format objects are semantically equal. | |
322 | * Objects of different subclasses are considered unequal. | |
323 | * @param other the object to be compared with. | |
324 | * @return true if the given Format objects are semantically equal. | |
325 | * @stable ICU 2.0 | |
326 | */ | |
327 | virtual UBool operator==(const Format& other) const; | |
328 | ||
329 | /** | |
330 | * Sets the locale. This locale is used for fetching default number or date | |
331 | * format information. | |
332 | * @param theLocale the new locale value to be set. | |
333 | * @stable ICU 2.0 | |
334 | */ | |
335 | virtual void setLocale(const Locale& theLocale); | |
336 | ||
337 | /** | |
338 | * Gets the locale. This locale is used for fetching default number or date | |
339 | * format information. | |
340 | * @return the locale of the object. | |
341 | * @stable ICU 2.0 | |
342 | */ | |
343 | virtual const Locale& getLocale(void) const; | |
344 | ||
345 | /** | |
346 | * Applies the given pattern string to this message format. | |
347 | * | |
348 | * @param pattern The pattern to be applied. | |
349 | * @param status Input/output error code. If the | |
350 | * pattern cannot be parsed, set to failure code. | |
351 | * @stable ICU 2.0 | |
352 | */ | |
353 | virtual void applyPattern(const UnicodeString& pattern, | |
354 | UErrorCode& status); | |
355 | /** | |
356 | * Applies the given pattern string to this message format. | |
357 | * | |
358 | * @param pattern The pattern to be applied. | |
359 | * @param parseError Struct to recieve information on position | |
360 | * of error within pattern. | |
361 | * @param status Input/output error code. If the | |
362 | * pattern cannot be parsed, set to failure code. | |
363 | * @stable ICU 2.0 | |
364 | */ | |
365 | virtual void applyPattern(const UnicodeString& pattern, | |
366 | UParseError& parseError, | |
367 | UErrorCode& status); | |
368 | ||
369 | /** | |
370 | * Returns a pattern that can be used to recreate this object. | |
371 | * | |
372 | * @param appendTo Output parameter to receive the pattern. | |
373 | * Result is appended to existing contents. | |
374 | * @return Reference to 'appendTo' parameter. | |
375 | * @stable ICU 2.0 | |
376 | */ | |
377 | virtual UnicodeString& toPattern(UnicodeString& appendTo) const; | |
378 | ||
379 | /** | |
380 | * Sets subformats. | |
381 | * See the class description about format numbering. | |
382 | * The caller should not delete the Format objects after this call. | |
383 | * <EM>The array formatsToAdopt is not itself adopted.</EM> Its | |
384 | * ownership is retained by the caller. If the call fails because | |
385 | * memory cannot be allocated, then the formats will be deleted | |
386 | * by this method, and this object will remain unchanged. | |
387 | * | |
388 | * @stable ICU 2.0 | |
389 | * @param formatsToAdopt the format to be adopted. | |
390 | * @param count the size of the array. | |
391 | */ | |
392 | virtual void adoptFormats(Format** formatsToAdopt, int32_t count); | |
393 | ||
394 | /** | |
395 | * Sets subformats. | |
396 | * See the class description about format numbering. | |
397 | * Each item in the array is cloned into the internal array. | |
398 | * If the call fails because memory cannot be allocated, then this | |
399 | * object will remain unchanged. | |
400 | * | |
401 | * @stable ICU 2.0 | |
402 | * @param newFormats the new format to be set. | |
403 | * @param cnt the size of the array. | |
404 | */ | |
405 | virtual void setFormats(const Format** newFormats,int32_t cnt); | |
406 | ||
407 | ||
408 | /** | |
409 | * Sets one subformat. | |
410 | * See the class description about format numbering. | |
411 | * The caller should not delete the Format object after this call. | |
412 | * If the number is over the number of formats already set, | |
413 | * the item will be deleted and ignored. | |
414 | * @stable ICU 2.0 | |
415 | * @param formatNumber index of the subformat. | |
416 | * @param formatToAdopt the format to be adopted. | |
417 | */ | |
418 | virtual void adoptFormat(int32_t formatNumber, Format* formatToAdopt); | |
419 | ||
420 | /** | |
421 | * Sets one subformat. | |
422 | * See the class description about format numbering. | |
423 | * If the number is over the number of formats already set, | |
424 | * the item will be ignored. | |
425 | * @param formatNumber index of the subformat. | |
426 | * @param format the format to be set. | |
427 | * @stable ICU 2.0 | |
428 | */ | |
429 | virtual void setFormat(int32_t formatNumber, const Format& format); | |
430 | ||
431 | /** | |
432 | * Gets an array of subformats of this object. The returned array | |
433 | * should not be deleted by the caller, nor should the pointers | |
434 | * within the array. The array and its contents remain valid only | |
435 | * until the next call to any method of this class is made with | |
436 | * this object. See the class description about format numbering. | |
437 | * @param count output parameter to receive the size of the array | |
438 | * @return an array of count Format* objects, or NULL if out of | |
439 | * memory. Any or all of the array elements may be NULL. | |
440 | * @stable ICU 2.0 | |
441 | */ | |
442 | virtual const Format** getFormats(int32_t& count) const; | |
443 | ||
444 | /** | |
445 | * Formats the given array of arguments into a user-readable string. | |
446 | * Does not take ownership of the Formattable* array or its contents. | |
447 | * | |
448 | * @param source An array of objects to be formatted. | |
449 | * @param count The number of elements of 'source'. | |
450 | * @param appendTo Output parameter to receive result. | |
451 | * Result is appended to existing contents. | |
452 | * @param ignore Not used; inherited from base class API. | |
453 | * @param status Input/output error code. If the | |
454 | * pattern cannot be parsed, set to failure code. | |
455 | * @return Reference to 'appendTo' parameter. | |
456 | * @stable ICU 2.0 | |
457 | */ | |
458 | UnicodeString& format( const Formattable* source, | |
459 | int32_t count, | |
460 | UnicodeString& appendTo, | |
461 | FieldPosition& ignore, | |
462 | UErrorCode& status) const; | |
463 | ||
464 | /** | |
465 | * Formats the given array of arguments into a user-readable string | |
466 | * using the given pattern. | |
467 | * | |
468 | * @param pattern The pattern. | |
469 | * @param arguments An array of objects to be formatted. | |
470 | * @param count The number of elements of 'source'. | |
471 | * @param appendTo Output parameter to receive result. | |
472 | * Result is appended to existing contents. | |
473 | * @param status Input/output error code. If the | |
474 | * pattern cannot be parsed, set to failure code. | |
475 | * @return Reference to 'appendTo' parameter. | |
476 | * @stable ICU 2.0 | |
477 | */ | |
478 | static UnicodeString& format( const UnicodeString& pattern, | |
479 | const Formattable* arguments, | |
480 | int32_t count, | |
481 | UnicodeString& appendTo, | |
482 | UErrorCode& status); | |
483 | ||
484 | /** | |
485 | * Formats the given array of arguments into a user-readable | |
486 | * string. The array must be stored within a single Formattable | |
487 | * object of type kArray. If the Formattable object type is not of | |
488 | * type kArray, then returns a failing UErrorCode. | |
489 | * | |
490 | * @param obj A Formattable of type kArray containing | |
491 | * arguments to be formatted. | |
492 | * @param appendTo Output parameter to receive result. | |
493 | * Result is appended to existing contents. | |
494 | * @param pos On input: an alignment field, if desired. | |
495 | * On output: the offsets of the alignment field. | |
496 | * @param status Input/output error code. If the | |
497 | * pattern cannot be parsed, set to failure code. | |
498 | * @return Reference to 'appendTo' parameter. | |
499 | * @stable ICU 2.0 | |
500 | */ | |
501 | virtual UnicodeString& format(const Formattable& obj, | |
502 | UnicodeString& appendTo, | |
503 | FieldPosition& pos, | |
504 | UErrorCode& status) const; | |
505 | ||
506 | /** | |
507 | * Formats the given array of arguments into a user-readable | |
508 | * string. The array must be stored within a single Formattable | |
509 | * object of type kArray. If the Formattable object type is not of | |
510 | * type kArray, then returns a failing UErrorCode. | |
511 | * | |
512 | * @param obj The object to format | |
513 | * @param appendTo Output parameter to receive result. | |
514 | * Result is appended to existing contents. | |
515 | * @param status Input/output error code. If the | |
516 | * pattern cannot be parsed, set to failure code. | |
517 | * @return Reference to 'appendTo' parameter. | |
518 | * @stable ICU 2.0 | |
519 | */ | |
520 | UnicodeString& format(const Formattable& obj, | |
521 | UnicodeString& appendTo, | |
522 | UErrorCode& status) const; | |
523 | ||
524 | /** | |
525 | * Parses the given string into an array of output arguments. | |
526 | * | |
527 | * @param source String to be parsed. | |
528 | * @param pos On input, starting position for parse. On output, | |
529 | * final position after parse. Unchanged if parse | |
530 | * fails. | |
531 | * @param count Output parameter to receive the number of arguments | |
532 | * parsed. | |
533 | * @return an array of parsed arguments. The caller owns both | |
534 | * the array and its contents. | |
535 | * @stable ICU 2.0 | |
536 | */ | |
537 | virtual Formattable* parse( const UnicodeString& source, | |
538 | ParsePosition& pos, | |
539 | int32_t& count) const; | |
540 | ||
541 | /** | |
542 | * Parses the given string into an array of output arguments. | |
543 | * | |
544 | * @param source String to be parsed. | |
545 | * @param count Output param to receive size of returned array. | |
546 | * @param status Input/output error code. If the | |
547 | * pattern cannot be parsed, set to failure code. | |
548 | * @return an array of parsed arguments. The caller owns both | |
549 | * the array and its contents. | |
550 | * @stable ICU 2.0 | |
551 | */ | |
552 | virtual Formattable* parse( const UnicodeString& source, | |
553 | int32_t& count, | |
554 | UErrorCode& status) const; | |
555 | ||
556 | /** | |
557 | * Parses the given string into an array of output arguments | |
558 | * stored within a single Formattable of type kArray. | |
559 | * | |
560 | * @param source The string to be parsed into an object. | |
561 | * @param result Formattable to be set to the parse result. | |
562 | * If parse fails, return contents are undefined. | |
563 | * @param pos On input, starting position for parse. On output, | |
564 | * final position after parse. Unchanged if parse | |
565 | * fails. | |
566 | * @stable ICU 2.0 | |
567 | */ | |
568 | virtual void parseObject(const UnicodeString& source, | |
569 | Formattable& result, | |
570 | ParsePosition& pos) const; | |
571 | ||
572 | /** | |
573 | * Returns a unique class ID POLYMORPHICALLY. Pure virtual override. | |
574 | * This method is to implement a simple version of RTTI, since not all | |
575 | * C++ compilers support genuine RTTI. Polymorphic operator==() and | |
576 | * clone() methods call this method. | |
577 | * | |
578 | * @return The class ID for this object. All objects of a | |
579 | * given class have the same class ID. Objects of | |
580 | * other classes have different class IDs. | |
581 | * @stable ICU 2.0 | |
582 | */ | |
583 | virtual UClassID getDynamicClassID(void) const; | |
584 | ||
585 | /** | |
586 | * Return the class ID for this class. This is useful only for | |
587 | * comparing to a return value from getDynamicClassID(). For example: | |
588 | * <pre> | |
589 | * . Base* polymorphic_pointer = createPolymorphicObject(); | |
590 | * . if (polymorphic_pointer->getDynamicClassID() == | |
591 | * . Derived::getStaticClassID()) ... | |
592 | * </pre> | |
593 | * @return The class ID for all objects of this class. | |
594 | * @stable ICU 2.0 | |
595 | */ | |
596 | static inline UClassID getStaticClassID(void); | |
597 | ||
598 | private: | |
599 | static const char fgClassID; | |
600 | ||
601 | Locale fLocale; | |
602 | UnicodeString fPattern; | |
603 | Format** formatAliases; // see getFormats | |
604 | int32_t formatAliasesCapacity; | |
605 | ||
606 | MessageFormat(); // default constructor not implemented | |
607 | ||
608 | /** | |
609 | * A structure representing one subformat of this MessageFormat. | |
610 | * Each subformat has a Format object, an offset into the plain | |
611 | * pattern text fPattern, and an argument number. The argument | |
612 | * number corresponds to the array of arguments to be formatted. | |
613 | * @internal | |
614 | */ | |
615 | class Subformat { | |
616 | public: | |
617 | /** | |
618 | * @internal | |
619 | */ | |
620 | Format* format; // formatter | |
621 | /** | |
622 | * @internal | |
623 | */ | |
624 | int32_t offset; // offset into fPattern | |
625 | /** | |
626 | * @internal | |
627 | */ | |
628 | int32_t arg; // 0-based argument number | |
629 | ||
630 | /** | |
631 | * Clone that.format and assign it to this.format | |
632 | * Do NOT delete this.format | |
633 | * @internal | |
634 | */ | |
635 | Subformat& operator=(const Subformat& that) { | |
636 | format = that.format ? that.format->clone() : NULL; | |
637 | offset = that.offset; | |
638 | arg = that.arg; | |
639 | return *this; | |
640 | } | |
641 | ||
642 | /** | |
643 | * @internal | |
644 | */ | |
645 | UBool operator==(const Subformat& that) const { | |
646 | // Do cheap comparisons first | |
647 | return offset == that.offset && | |
648 | arg == that.arg && | |
649 | ((format == that.format) || // handles NULL | |
650 | (*format == *that.format)); | |
651 | } | |
652 | ||
653 | /** | |
654 | * @internal | |
655 | */ | |
656 | UBool operator!=(const Subformat& that) const { | |
657 | return !operator==(that); | |
658 | } | |
659 | }; | |
660 | ||
661 | /** | |
662 | * A MessageFormat contains an array of subformats. This array | |
663 | * needs to grow dynamically if the MessageFormat is modified. | |
664 | */ | |
665 | Subformat* subformats; | |
666 | int32_t subformatCount; | |
667 | int32_t subformatCapacity; | |
668 | ||
669 | /** | |
670 | * A MessageFormat formats an array of arguments. Each argument | |
671 | * has an expected type, based on the pattern. For example, if | |
672 | * the pattern contains the subformat "{3,number,integer}", then | |
673 | * we expect argument 3 to have type Formattable::kLong. This | |
674 | * array needs to grow dynamically if the MessageFormat is | |
675 | * modified. | |
676 | */ | |
677 | Formattable::Type* argTypes; | |
678 | int32_t argTypeCount; | |
679 | int32_t argTypeCapacity; | |
680 | ||
681 | // Variable-size array management | |
682 | UBool allocateSubformats(int32_t capacity); | |
683 | UBool allocateArgTypes(int32_t capacity); | |
684 | ||
685 | /** | |
686 | * Default Format objects used when no format is specified and a | |
687 | * numeric or date argument is formatted. These are volatile | |
688 | * cache objects maintained only for performance. They do not | |
689 | * participate in operator=(), copy constructor(), nor | |
690 | * operator==(). | |
691 | */ | |
692 | NumberFormat* defaultNumberFormat; | |
693 | DateFormat* defaultDateFormat; | |
694 | ||
695 | /** | |
696 | * Method to retrieve default formats (or NULL on failure). | |
697 | * These are semantically const, but may modify *this. | |
698 | */ | |
699 | const NumberFormat* getDefaultNumberFormat(UErrorCode&) const; | |
700 | const DateFormat* getDefaultDateFormat(UErrorCode&) const; | |
701 | ||
702 | /** | |
703 | * Finds the word s, in the keyword list and returns the located index. | |
704 | * @param s the keyword to be searched for. | |
705 | * @param list the list of keywords to be searched with. | |
706 | * @return the index of the list which matches the keyword s. | |
707 | */ | |
708 | static int32_t findKeyword( const UnicodeString& s, | |
709 | const UChar * const *list); | |
710 | ||
711 | /** | |
712 | * Formats the array of arguments and copies the result into the | |
713 | * result buffer, updates the field position. | |
714 | * | |
715 | * @param arguments The formattable objects array. | |
716 | * @param cnt The array count. | |
717 | * @param appendTo Output parameter to receive result. | |
718 | * Result is appended to existing contents. | |
719 | * @param status Field position status. | |
720 | * @param recursionProtection | |
721 | * Initially zero. Bits 0..9 are used to indicate | |
722 | * that a parameter has already been seen, to | |
723 | * avoid recursion. Currently unused. | |
724 | * @param success The error code status. | |
725 | * @return Reference to 'appendTo' parameter. | |
726 | */ | |
727 | UnicodeString& format( const Formattable* arguments, | |
728 | int32_t cnt, | |
729 | UnicodeString& appendTo, | |
730 | FieldPosition& status, | |
731 | int32_t recursionProtection, | |
732 | UErrorCode& success) const; | |
733 | ||
734 | void makeFormat(int32_t offsetNumber, | |
735 | UnicodeString* segments, | |
736 | UParseError& parseError, | |
737 | UErrorCode& success); | |
738 | ||
739 | /** | |
740 | * Convenience method that ought to be in NumberFormat | |
741 | */ | |
742 | NumberFormat* createIntegerFormat(const Locale& locale, UErrorCode& status) const; | |
743 | ||
744 | /** | |
745 | * Checks the range of the source text to quote the special | |
746 | * characters, { and ' and copy to target buffer. | |
747 | * @param source | |
748 | * @param start the text offset to start the process of in the source string | |
749 | * @param end the text offset to end the process of in the source string | |
750 | * @param appendTo Output parameter to receive result. | |
751 | * Result is appended to existing contents. | |
752 | */ | |
753 | static void copyAndFixQuotes(const UnicodeString& appendTo, int32_t start, int32_t end, UnicodeString& target); | |
754 | ||
755 | /** | |
756 | * Returns array of argument types in the parsed pattern | |
757 | * for use in C API. Only for the use of umsg_vformat(). Not | |
758 | * for public consumption. | |
759 | * @param listCount Output parameter to receive the size of array | |
760 | * @return The array of formattable types in the pattern | |
761 | * @internal | |
762 | */ | |
763 | const Formattable::Type* getArgTypeList(int32_t& listCount) const { | |
764 | listCount = argTypeCount; | |
765 | return argTypes; | |
766 | } | |
767 | ||
768 | friend class MessageFormatAdapter; // getFormatTypeList() access | |
769 | }; | |
770 | ||
771 | inline UClassID | |
772 | MessageFormat::getStaticClassID(void) | |
773 | { return (UClassID)&fgClassID; } | |
774 | ||
775 | inline UClassID | |
776 | MessageFormat::getDynamicClassID() const | |
777 | { return MessageFormat::getStaticClassID(); } | |
778 | ||
779 | ||
780 | inline UnicodeString& | |
781 | MessageFormat::format(const Formattable& obj, | |
782 | UnicodeString& appendTo, | |
783 | UErrorCode& status) const { | |
784 | return Format::format(obj, appendTo, status); | |
785 | } | |
786 | U_NAMESPACE_END | |
787 | ||
788 | #endif /* #if !UCONFIG_NO_FORMATTING */ | |
789 | ||
790 | #endif // _MSGFMT | |
791 | //eof |