]>
Commit | Line | Data |
---|---|---|
b75a7d8f A |
1 | /* |
2 | ******************************************************************************** | |
46f4442e | 3 | * Copyright (C) 1997-2008, International Business Machines |
b75a7d8f A |
4 | * Corporation and others. All Rights Reserved. |
5 | ******************************************************************************** | |
6 | * | |
7 | * File CHOICFMT.H | |
8 | * | |
9 | * Modification History: | |
10 | * | |
11 | * Date Name Description | |
12 | * 02/19/97 aliu Converted from java. | |
13 | * 03/20/97 helena Finished first cut of implementation and got rid | |
14 | * of nextDouble/previousDouble and replaced with | |
15 | * boolean array. | |
16 | * 4/10/97 aliu Clean up. Modified to work on AIX. | |
17 | * 8/6/97 nos Removed overloaded constructor, member var 'buffer'. | |
18 | * 07/22/98 stephen Removed operator!= (implemented in Format) | |
19 | ******************************************************************************** | |
20 | */ | |
21 | ||
22 | #ifndef CHOICFMT_H | |
23 | #define CHOICFMT_H | |
24 | ||
25 | #include "unicode/utypes.h" | |
26 | ||
73c04bcf A |
27 | /** |
28 | * \file | |
29 | * \brief C++ API: Choice Format. | |
30 | */ | |
31 | ||
b75a7d8f A |
32 | #if !UCONFIG_NO_FORMATTING |
33 | ||
34 | #include "unicode/unistr.h" | |
35 | #include "unicode/numfmt.h" | |
36 | #include "unicode/fieldpos.h" | |
37 | #include "unicode/format.h" | |
38 | ||
39 | U_NAMESPACE_BEGIN | |
40 | ||
41 | class MessageFormat; | |
42 | ||
43 | /** | |
44 | * ChoiceFormat converts between ranges of numeric values | |
45 | * and string names for those ranges. A <code>ChoiceFormat</code> splits | |
46 | * the real number line <code>-Inf</code> to <code>+Inf</code> into two | |
47 | * or more contiguous ranges. Each range is mapped to a | |
48 | * string. <code>ChoiceFormat</code> is generally used in a | |
49 | * <code>MessageFormat</code> for displaying grammatically correct | |
50 | * plurals such as "There are 2 files."</p> | |
51 | * | |
52 | * <p>There are two methods of defining a <code>ChoiceFormat</code>; both | |
53 | * are equivalent. The first is by using a string pattern. This is the | |
54 | * preferred method in most cases. The second method is through direct | |
55 | * specification of the arrays that make up the | |
56 | * <code>ChoiceFormat</code>.</p> | |
57 | * | |
58 | * <p><strong>Patterns</strong></p> | |
59 | * | |
60 | * <p>In most cases, the preferred way to define a | |
61 | * <code>ChoiceFormat</code> is with a pattern. Here is an example of a | |
62 | * <code>ChoiceFormat</code> pattern:</p> | |
63 | * | |
64 | * \htmlonly<pre> 0≤are no files|1≤is one file|1<are many files</pre>\endhtmlonly | |
65 | * | |
66 | * <p>or equivalently,</p> | |
67 | * | |
73c04bcf | 68 | * \htmlonly<pre> 0#are no files|1#is one file|1<are many files</pre>\endhtmlonly |
b75a7d8f A |
69 | * |
70 | * <p>The pattern consists of a number or <em>range specifiers</em> | |
71 | * separated by vertical bars '|' (U+007C). There is no | |
72 | * vertical bar after the last range. Each range specifier is of the | |
73 | * form:</p> | |
74 | * | |
75 | * \htmlonly<blockquote><em>Number Separator String</em></blockquote>\endhtmlonly | |
76 | * | |
77 | * <p><em>Number</em> is a floating point number that can be parsed by a | |
78 | * default <code>NumberFormat</code> for the US locale. It gives the | |
79 | * lower limit of this range. The lower limit is either inclusive or | |
80 | * exclusive, depending on the <em>separator</em>. The upper limit is | |
81 | * given by the lower limit of the next range. The Unicode infinity | |
82 | * sign \htmlonly∞ \endhtmlonly (U+221E) is recognized for positive infinity. It may be preceded by | |
83 | * '-' (U+002D) to indicate negative infinity.</p> | |
84 | * | |
85 | * <p><em>String</em> is the format string for this range, with special | |
86 | * characters enclosed in single quotes (<code>'The # | |
87 | * sign'</code>). Single quotes themselves are indicated by two single | |
88 | * quotes in a row (<code>'o''clock'</code>).</p> | |
89 | * | |
90 | * <p><em>Separator</em> is one of the following single characters: | |
91 | * | |
92 | * <ul> | |
93 | * <li>\htmlonly'≤' \endhtmlonly (U+2264) or '#' (U+0023) | |
94 | * indicates that the lower limit given by <em>Number</em> is | |
95 | * inclusive. (The two characters are equivalent to ChoiceFormat.) | |
96 | * This means that the limit value <em>Number</em> belongs to this | |
97 | * range. Another way of saying this is that the corresponding | |
98 | * closure is <code>FALSE</code>.</li> | |
99 | * | |
100 | * <li>'<' (U+003C) indicates that the lower limit given by | |
101 | * <em>Number</em> is exclusive. This means that the value | |
102 | * <em>Number</em> belongs to the prior range.</li> Another way of | |
103 | * saying this is that the corresponding closure is | |
104 | * <code>TRUE</code>. | |
105 | * </ul> | |
106 | * | |
107 | * <p>See below for more information about closures.</p> | |
108 | * | |
109 | * <p><strong>Arrays</strong></p> | |
110 | * | |
111 | * <p>A <code>ChoiceFormat</code> defining <code>n</code> intervals | |
112 | * (<code>n</code> >= 2) is specified by three arrays of | |
113 | * <code>n</code> items: | |
114 | * | |
115 | * <ul> | |
116 | * <li><code>double limits[]</code> gives the start of each | |
117 | * interval. This must be a non-decreasing list of values, none of | |
118 | * which may be <code>NaN</code>.</li> | |
119 | * <li><code>UBool closures[]</code> determines whether each limit | |
120 | * value is contained in the interval below it or in the interval | |
121 | * above it. If <code>closures[i]</code> is <code>FALSE</code>, then | |
122 | * <code>limits[i]</code> is a member of interval | |
123 | * <code>i</code>. Otherwise it is a member of interval | |
124 | * <code>i+1</code>. If no closures array is specified, this is | |
125 | * equivalent to having all closures be <code>FALSE</code>. Closures | |
126 | * allow one to specify half-open, open, or closed intervals.</li> | |
127 | * <li><code>UnicodeString formats[]</code> gives the string label | |
128 | * associated with each interval.</li> | |
129 | * </ul> | |
130 | * | |
131 | * <p><strong>Formatting and Parsing</strong></p> | |
132 | * | |
133 | * <p>During formatting, a number is converted to a | |
134 | * string. <code>ChoiceFormat</code> accomplishes this by mapping the | |
135 | * number to an interval using the following rule. Given a number | |
136 | * <code>X</code> and and index value <code>j</code> in the range | |
137 | * <code>0..n-1</code>, where <code>n</code> is the number of ranges:</p> | |
138 | * | |
374ca955 | 139 | * \htmlonly<blockquote>\endhtmlonly<code>X</code> matches <code>j</code> if and only if |
b75a7d8f | 140 | * <code>limit[j] <= X < limit[j+1]</code> |
374ca955 | 141 | * \htmlonly</blockquote>\endhtmlonly |
b75a7d8f A |
142 | * |
143 | * <p>(This assumes that all closures are <code>FALSE</code>. If some | |
144 | * closures are <code>TRUE</code> then the relations must be changed to | |
145 | * <code><=</code> or <code><</code> as appropriate.) If there is | |
146 | * no match, then either the first or last index is used, depending on | |
147 | * whether the number is too low or too high. Once a number is mapped to | |
148 | * an interval <code>j</code>, the string <code>formats[j]</code> is | |
149 | * output.</p> | |
150 | * | |
151 | * <p>During parsing, a string is converted to a | |
152 | * number. <code>ChoiceFormat</code> finds the element | |
153 | * <code>formats[j]</code> equal to the string, and returns | |
154 | * <code>limits[j]</code> as the parsed value.</p> | |
155 | * | |
156 | * <p><strong>Notes</strong></p> | |
157 | * | |
158 | * <p>The first limit value does not define a range boundary. For | |
73c04bcf | 159 | * example, in the pattern \htmlonly"<code>1.0#a|2.0#b</code>"\endhtmlonly, the |
b75a7d8f A |
160 | * intervals are [-Inf, 2.0) and [2.0, +Inf]. It appears that the first |
161 | * interval should be [1.0, 2.0). However, since all values that are too | |
162 | * small are mapped to range zero, the first interval is effectively | |
163 | * [-Inf, 2.0). However, the first limit value <em>is</em> used during | |
164 | * formatting. In this example, <code>parse("a")</code> returns | |
165 | * 1.0.</p> | |
166 | * | |
167 | * <p>There are no gaps between intervals and the entire number line is | |
168 | * covered. A <code>ChoiceFormat</code> maps <em>all</em> possible | |
169 | * double values to a finite set of intervals.</p> | |
170 | * | |
171 | * <p>The non-number <code>NaN</code> is mapped to interval zero during | |
172 | * formatting.</p> | |
173 | * | |
174 | * <p><strong>Examples</strong></p> | |
175 | * | |
176 | * <p>Here is an example of two arrays that map the number | |
177 | * <code>1..7</code> to the English day of the week abbreviations | |
178 | * <code>Sun..Sat</code>. No closures array is given; this is the same as | |
179 | * specifying all closures to be <code>FALSE</code>.</p> | |
180 | * | |
181 | * <pre> {1,2,3,4,5,6,7}, | |
182 | * {"Sun","Mon","Tue","Wed","Thur","Fri","Sat"}</pre> | |
183 | * | |
184 | * <p>Here is an example that maps the ranges [-Inf, 1), [1, 1], and (1, | |
185 | * +Inf] to three strings. That is, the number line is split into three | |
186 | * ranges: x < 1.0, x = 1.0, and x > 1.0.</p> | |
187 | * | |
188 | * <pre> {0, 1, 1}, | |
189 | * {FALSE, FALSE, TRUE}, | |
190 | * {"no files", "one file", "many files"}</pre> | |
191 | * | |
192 | * <p>Here is a simple example that shows formatting and parsing: </p> | |
193 | * | |
194 | * \code | |
195 | * #include <unicode/choicfmt.h> | |
196 | * #include <unicode/unistr.h> | |
197 | * #include <iostream.h> | |
198 | * | |
199 | * int main(int argc, char *argv[]) { | |
200 | * double limits[] = {1,2,3,4,5,6,7}; | |
201 | * UnicodeString monthNames[] = { | |
202 | * "Sun","Mon","Tue","Wed","Thu","Fri","Sat"}; | |
203 | * ChoiceFormat fmt(limits, monthNames, 7); | |
204 | * UnicodeString str; | |
205 | * char buf[256]; | |
206 | * for (double x = 1.0; x <= 8.0; x += 1.0) { | |
207 | * fmt.format(x, str); | |
208 | * str.extract(0, str.length(), buf, 256, ""); | |
209 | * str.truncate(0); | |
210 | * cout << x << " -> " | |
211 | * << buf << endl; | |
212 | * } | |
213 | * cout << endl; | |
214 | * return 0; | |
215 | * } | |
216 | * \endcode | |
217 | * | |
218 | * <p>Here is a more complex example using a <code>ChoiceFormat</code> | |
219 | * constructed from a pattern together with a | |
220 | * <code>MessageFormat</code>.</p> | |
221 | * | |
222 | * \code | |
223 | * #include <unicode/choicfmt.h> | |
224 | * #include <unicode/msgfmt.h> | |
225 | * #include <unicode/unistr.h> | |
226 | * #include <iostream.h> | |
227 | * | |
228 | * int main(int argc, char *argv[]) { | |
229 | * UErrorCode status = U_ZERO_ERROR; | |
230 | * double filelimits[] = {0,1,2}; | |
231 | * UnicodeString filepart[] = | |
232 | * {"are no files","is one file","are {0} files"}; | |
233 | * ChoiceFormat* fileform = new ChoiceFormat(filelimits, filepart, 3 ); | |
234 | * Format* testFormats[] = | |
235 | * {fileform, NULL, NumberFormat::createInstance(status)}; | |
236 | * MessageFormat pattform("There {0} on {1}", status ); | |
237 | * pattform.adoptFormats(testFormats, 3); | |
238 | * Formattable testArgs[] = {0L, "Disk A"}; | |
239 | * FieldPosition fp(0); | |
240 | * UnicodeString str; | |
241 | * char buf[256]; | |
242 | * for (int32_t i = 0; i < 4; ++i) { | |
243 | * Formattable fInt(i); | |
244 | * testArgs[0] = fInt; | |
245 | * pattform.format(testArgs, 2, str, fp, status ); | |
246 | * str.extract(0, str.length(), buf, ""); | |
247 | * str.truncate(0); | |
248 | * cout << "Output for i=" << i << " : " << buf << endl; | |
249 | * } | |
250 | * cout << endl; | |
251 | * return 0; | |
252 | * } | |
253 | * \endcode | |
374ca955 A |
254 | * |
255 | * <p><em>User subclasses are not supported.</em> While clients may write | |
256 | * subclasses, such code will not necessarily work and will not be | |
257 | * guaranteed to work stably from release to release. | |
b75a7d8f A |
258 | */ |
259 | class U_I18N_API ChoiceFormat: public NumberFormat { | |
260 | public: | |
261 | /** | |
262 | * Construct a new ChoiceFormat with the limits and the corresponding formats | |
263 | * based on the pattern. | |
264 | * | |
265 | * @param pattern Pattern used to construct object. | |
266 | * @param status Output param to receive success code. If the | |
267 | * pattern cannot be parsed, set to failure code. | |
268 | * @stable ICU 2.0 | |
269 | */ | |
270 | ChoiceFormat(const UnicodeString& pattern, | |
271 | UErrorCode& status); | |
272 | ||
273 | ||
274 | /** | |
275 | * Construct a new ChoiceFormat with the given limits and formats. Copy | |
276 | * the limits and formats instead of adopting them. | |
277 | * | |
278 | * @param limits Array of limit values. | |
279 | * @param formats Array of formats. | |
280 | * @param count Size of 'limits' and 'formats' arrays. | |
281 | * @stable ICU 2.0 | |
282 | */ | |
283 | ||
284 | ChoiceFormat(const double* limits, | |
285 | const UnicodeString* formats, | |
286 | int32_t count ); | |
287 | ||
288 | /** | |
289 | * Construct a new ChoiceFormat with the given limits and formats. | |
290 | * Copy the limits and formats (instead of adopting them). By | |
291 | * default, each limit in the array specifies the inclusive lower | |
292 | * bound of its range, and the exclusive upper bound of the previous | |
293 | * range. However, if the isLimitOpen element corresponding to a | |
294 | * limit is TRUE, then the limit is the exclusive lower bound of its | |
295 | * range, and the inclusive upper bound of the previous range. | |
296 | * @param limits Array of limit values | |
297 | * @param closures Array of booleans specifying whether each | |
298 | * element of 'limits' is open or closed. If FALSE, then the | |
299 | * corresponding limit is a member of the range above it. If TRUE, | |
300 | * then the limit belongs to the range below it. | |
301 | * @param formats Array of formats | |
302 | * @param count Size of 'limits', 'closures', and 'formats' arrays | |
374ca955 | 303 | * @stable ICU 2.4 |
b75a7d8f A |
304 | */ |
305 | ChoiceFormat(const double* limits, | |
306 | const UBool* closures, | |
307 | const UnicodeString* formats, | |
308 | int32_t count); | |
309 | ||
310 | /** | |
311 | * Copy constructor. | |
312 | * | |
313 | * @param that ChoiceFormat object to be copied from | |
314 | * @stable ICU 2.0 | |
315 | */ | |
316 | ChoiceFormat(const ChoiceFormat& that); | |
317 | ||
318 | /** | |
319 | * Assignment operator. | |
320 | * | |
321 | * @param that ChoiceFormat object to be copied | |
322 | * @stable ICU 2.0 | |
323 | */ | |
324 | const ChoiceFormat& operator=(const ChoiceFormat& that); | |
325 | ||
326 | /** | |
327 | * Destructor. | |
328 | * @stable ICU 2.0 | |
329 | */ | |
330 | virtual ~ChoiceFormat(); | |
331 | ||
332 | /** | |
333 | * Clone this Format object polymorphically. The caller owns the | |
334 | * result and should delete it when done. | |
335 | * | |
336 | * @return a copy of this object | |
337 | * @stable ICU 2.0 | |
338 | */ | |
339 | virtual Format* clone(void) const; | |
340 | ||
341 | /** | |
342 | * Return true if the given Format objects are semantically equal. | |
343 | * Objects of different subclasses are considered unequal. | |
344 | * | |
345 | * @param other ChoiceFormat object to be compared | |
346 | * @return true if other is the same as this. | |
347 | * @stable ICU 2.0 | |
348 | */ | |
349 | virtual UBool operator==(const Format& other) const; | |
350 | ||
351 | /** | |
352 | * Sets the pattern. | |
353 | * @param pattern The pattern to be applied. | |
354 | * @param status Output param set to success/failure code on | |
355 | * exit. If the pattern is invalid, this will be | |
356 | * set to a failure result. | |
357 | * @stable ICU 2.0 | |
358 | */ | |
359 | virtual void applyPattern(const UnicodeString& pattern, | |
360 | UErrorCode& status); | |
361 | ||
362 | /** | |
363 | * Sets the pattern. | |
364 | * @param pattern The pattern to be applied. | |
365 | * @param parseError Struct to recieve information on position | |
366 | * of error if an error is encountered | |
367 | * @param status Output param set to success/failure code on | |
368 | * exit. If the pattern is invalid, this will be | |
369 | * set to a failure result. | |
370 | * @stable ICU 2.0 | |
371 | */ | |
372 | virtual void applyPattern(const UnicodeString& pattern, | |
373 | UParseError& parseError, | |
374 | UErrorCode& status); | |
375 | /** | |
376 | * Gets the pattern. | |
377 | * | |
378 | * @param pattern Output param which will recieve the pattern | |
379 | * Previous contents are deleted. | |
380 | * @return A reference to 'pattern' | |
381 | * @stable ICU 2.0 | |
382 | */ | |
383 | virtual UnicodeString& toPattern(UnicodeString &pattern) const; | |
384 | ||
b75a7d8f A |
385 | /** |
386 | * Set the choices to be used in formatting. | |
387 | * | |
388 | * @param limitsToCopy Contains the top value that you want | |
389 | * parsed with that format,and should be in | |
390 | * ascending sorted order. When formatting X, | |
391 | * the choice will be the i, where limit[i] | |
392 | * <= X < limit[i+1]. | |
393 | * @param formatsToCopy The format strings you want to use for each limit. | |
394 | * @param count The size of the above arrays. | |
395 | * @stable ICU 2.0 | |
396 | */ | |
397 | virtual void setChoices(const double* limitsToCopy, | |
398 | const UnicodeString* formatsToCopy, | |
399 | int32_t count ); | |
400 | ||
401 | /** | |
402 | * Set the choices to be used in formatting. See class description | |
403 | * for documenatation of the limits, closures, and formats arrays. | |
404 | * @param limits Array of limits | |
405 | * @param closures Array of limit booleans | |
406 | * @param formats Array of format string | |
407 | * @param count The size of the above arrays | |
374ca955 | 408 | * @stable ICU 2.4 |
b75a7d8f A |
409 | */ |
410 | virtual void setChoices(const double* limits, | |
411 | const UBool* closures, | |
412 | const UnicodeString* formats, | |
413 | int32_t count); | |
414 | ||
415 | /** | |
416 | * Get the limits passed in the constructor. | |
417 | * | |
418 | * @param count The size of the limits arrays | |
419 | * @return the limits. | |
420 | * @stable ICU 2.0 | |
421 | */ | |
422 | virtual const double* getLimits(int32_t& count) const; | |
423 | ||
424 | /** | |
425 | * Get the limit booleans passed in the constructor. The caller | |
426 | * must not delete the result. | |
427 | * | |
428 | * @param count The size of the arrays | |
429 | * @return the closures | |
374ca955 | 430 | * @stable ICU 2.4 |
b75a7d8f A |
431 | */ |
432 | virtual const UBool* getClosures(int32_t& count) const; | |
433 | ||
434 | /** | |
435 | * Get the formats passed in the constructor. | |
436 | * | |
437 | * @param count The size of the arrays | |
438 | * @return the formats. | |
439 | * @stable ICU 2.0 | |
440 | */ | |
441 | virtual const UnicodeString* getFormats(int32_t& count) const; | |
442 | ||
443 | /** | |
444 | * Format a double or long number using this object's choices. | |
445 | * | |
446 | * @param number The value to be formatted. | |
447 | * @param appendTo Output parameter to receive result. | |
448 | * Result is appended to existing contents. | |
449 | * @param pos On input: an alignment field, if desired. | |
450 | * On output: the offsets of the alignment field. | |
451 | * @return Reference to 'appendTo' parameter. | |
452 | * @stable ICU 2.0 | |
453 | */ | |
454 | virtual UnicodeString& format(double number, | |
455 | UnicodeString& appendTo, | |
456 | FieldPosition& pos) const; | |
457 | /** | |
458 | * Format a int_32t number using this object's choices. | |
459 | * | |
460 | * @param number The value to be formatted. | |
461 | * @param appendTo Output parameter to receive result. | |
462 | * Result is appended to existing contents. | |
463 | * @param pos On input: an alignment field, if desired. | |
464 | * On output: the offsets of the alignment field. | |
465 | * @return Reference to 'appendTo' parameter. | |
466 | * @stable ICU 2.0 | |
467 | */ | |
468 | virtual UnicodeString& format(int32_t number, | |
469 | UnicodeString& appendTo, | |
470 | FieldPosition& pos) const; | |
374ca955 A |
471 | |
472 | /** | |
473 | * Format an int64_t number using this object's choices. | |
474 | * | |
475 | * @param number The value to be formatted. | |
476 | * @param appendTo Output parameter to receive result. | |
477 | * Result is appended to existing contents. | |
478 | * @param pos On input: an alignment field, if desired. | |
479 | * On output: the offsets of the alignment field. | |
480 | * @return Reference to 'appendTo' parameter. | |
73c04bcf | 481 | * @stable ICU 2.8 |
374ca955 A |
482 | */ |
483 | virtual UnicodeString& format(int64_t number, | |
484 | UnicodeString& appendTo, | |
485 | FieldPosition& pos) const; | |
486 | ||
b75a7d8f A |
487 | /** |
488 | * Format an array of objects using this object's choices. | |
489 | * | |
490 | * @param objs The array of objects to be formatted. | |
491 | * @param cnt The size of objs. | |
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 success Output param set to success/failure code on | |
497 | * exit. | |
498 | * @return Reference to 'appendTo' parameter. | |
499 | * @stable ICU 2.0 | |
500 | */ | |
501 | virtual UnicodeString& format(const Formattable* objs, | |
502 | int32_t cnt, | |
503 | UnicodeString& appendTo, | |
504 | FieldPosition& pos, | |
505 | UErrorCode& success) const; | |
506 | /** | |
507 | * Format an object using this object's choices. | |
508 | * | |
509 | * | |
510 | * @param obj The object to be formatted. | |
511 | * @param appendTo Output parameter to receive result. | |
512 | * Result is appended to existing contents. | |
513 | * @param pos On input: an alignment field, if desired. | |
514 | * On output: the offsets of the alignment field. | |
515 | * @param status Output param set to success/failure code on | |
516 | * exit. | |
517 | * @return Reference to 'appendTo' parameter. | |
518 | * @stable ICU 2.0 | |
519 | */ | |
520 | virtual UnicodeString& format(const Formattable& obj, | |
521 | UnicodeString& appendTo, | |
522 | FieldPosition& pos, | |
523 | UErrorCode& status) const; | |
524 | ||
525 | /** | |
526 | * Redeclared NumberFormat method. | |
527 | * | |
528 | * @param obj The object to be formatted. | |
529 | * @param appendTo Output parameter to receive result. | |
530 | * Result is appended to existing contents. | |
531 | * @param status Output param set to success/failure code on | |
532 | * exit. | |
533 | * @return Reference to 'appendTo' parameter. | |
534 | * @stable ICU 2.0 | |
535 | */ | |
536 | UnicodeString& format(const Formattable& obj, | |
537 | UnicodeString& appendTo, | |
538 | UErrorCode& status) const; | |
539 | ||
540 | /** | |
541 | * Redeclared NumberFormat method. | |
542 | * Format a double number. These methods call the NumberFormat | |
543 | * pure virtual format() methods with the default FieldPosition. | |
544 | * | |
545 | * @param number The value to be formatted. | |
546 | * @param appendTo Output parameter to receive result. | |
547 | * Result is appended to existing contents. | |
548 | * @return Reference to 'appendTo' parameter. | |
549 | * @stable ICU 2.0 | |
550 | */ | |
551 | UnicodeString& format( double number, | |
552 | UnicodeString& appendTo) const; | |
553 | ||
554 | /** | |
555 | * Redeclared NumberFormat method. | |
556 | * Format a long number. These methods call the NumberFormat | |
557 | * pure virtual format() methods with the default FieldPosition. | |
558 | * | |
559 | * @param number The value to be formatted. | |
560 | * @param appendTo Output parameter to receive result. | |
561 | * Result is appended to existing contents. | |
562 | * @return Reference to 'appendTo' parameter. | |
563 | * @stable ICU 2.0 | |
564 | */ | |
565 | UnicodeString& format( int32_t number, | |
566 | UnicodeString& appendTo) const; | |
567 | ||
568 | /** | |
569 | * Return a long if possible (e.g. within range LONG_MAX, | |
570 | * LONG_MAX], and with no decimals), otherwise a double. If | |
571 | * IntegerOnly is set, will stop at a decimal point (or equivalent; | |
572 | * e.g. for rational numbers "1 2/3", will stop after the 1). | |
573 | * <P> | |
574 | * If no object can be parsed, parsePosition is unchanged, and NULL is | |
575 | * returned. | |
576 | * | |
577 | * @param text The text to be parsed. | |
578 | * @param result Formattable to be set to the parse result. | |
579 | * If parse fails, return contents are undefined. | |
580 | * @param parsePosition The position to start parsing at on input. | |
581 | * On output, moved to after the last successfully | |
582 | * parse character. On parse failure, does not change. | |
583 | * @see NumberFormat::isParseIntegerOnly | |
584 | * @stable ICU 2.0 | |
585 | */ | |
586 | virtual void parse(const UnicodeString& text, | |
587 | Formattable& result, | |
588 | ParsePosition& parsePosition) const; | |
589 | ||
590 | /** | |
591 | * Return a long if possible (e.g. within range LONG_MAX, | |
592 | * LONG_MAX], and with no decimals), otherwise a double. If | |
593 | * IntegerOnly is set, will stop at a decimal point (or equivalent; | |
594 | * e.g. for rational numbers "1 2/3", will stop after the 1). | |
595 | * <P> | |
596 | * If no object can be parsed, parsePosition is unchanged, and NULL is | |
597 | * returned. | |
598 | * | |
599 | * @param text The text to be parsed. | |
600 | * @param result Formattable to be set to the parse result. | |
601 | * If parse fails, return contents are undefined. | |
602 | * @param status Output param with the formatted string. | |
603 | * @see NumberFormat::isParseIntegerOnly | |
604 | * @stable ICU 2.0 | |
605 | */ | |
606 | virtual void parse(const UnicodeString& text, | |
607 | Formattable& result, | |
608 | UErrorCode& status) const; | |
609 | ||
610 | ||
611 | public: | |
612 | /** | |
613 | * Returns a unique class ID POLYMORPHICALLY. Pure virtual override. | |
614 | * This method is to implement a simple version of RTTI, since not all | |
615 | * C++ compilers support genuine RTTI. Polymorphic operator==() and | |
616 | * clone() methods call this method. | |
617 | * | |
618 | * @return The class ID for this object. All objects of a | |
619 | * given class have the same class ID. Objects of | |
620 | * other classes have different class IDs. | |
621 | * @stable ICU 2.0 | |
622 | */ | |
623 | virtual UClassID getDynamicClassID(void) const; | |
624 | ||
625 | /** | |
626 | * Return the class ID for this class. This is useful only for | |
627 | * comparing to a return value from getDynamicClassID(). For example: | |
628 | * <pre> | |
629 | * . Base* polymorphic_pointer = createPolymorphicObject(); | |
630 | * . if (polymorphic_pointer->getDynamicClassID() == | |
631 | * . Derived::getStaticClassID()) ... | |
632 | * </pre> | |
633 | * @return The class ID for all objects of this class. | |
634 | * @stable ICU 2.0 | |
635 | */ | |
374ca955 | 636 | static UClassID U_EXPORT2 getStaticClassID(void); |
b75a7d8f A |
637 | |
638 | private: | |
639 | // static cache management (thread-safe) | |
640 | // static NumberFormat* getNumberFormat(UErrorCode &status); // call this function to 'check out' a numberformat from the cache. | |
641 | // static void releaseNumberFormat(NumberFormat *adopt); // call this function to 'return' the number format to the cache. | |
642 | ||
643 | /** | |
644 | * Converts a string to a double value using a default NumberFormat object | |
645 | * which is static (shared by all ChoiceFormat instances). | |
646 | * @param string the string to be converted with. | |
647 | * @return the converted double number. | |
648 | */ | |
649 | static double stod(const UnicodeString& string); | |
650 | ||
651 | /** | |
652 | * Converts a double value to a string using a default NumberFormat object | |
653 | * which is static (shared by all ChoiceFormat instances). | |
654 | * @param value the double number to be converted with. | |
655 | * @param string the result string. | |
656 | * @return the converted string. | |
657 | */ | |
658 | static UnicodeString& dtos(double value, UnicodeString& string); | |
659 | ||
b75a7d8f A |
660 | ChoiceFormat(); // default constructor not implemented |
661 | ||
662 | /** | |
663 | * Construct a new ChoiceFormat with the limits and the corresponding formats | |
664 | * based on the pattern. | |
665 | * | |
666 | * @param newPattern Pattern used to construct object. | |
667 | * @param parseError Struct to recieve information on position | |
668 | * of error if an error is encountered. | |
669 | * @param status Output param to receive success code. If the | |
670 | * pattern cannot be parsed, set to failure code. | |
671 | * @stable ICU 2.0 | |
672 | */ | |
673 | ChoiceFormat(const UnicodeString& newPattern, | |
674 | UParseError& parseError, | |
675 | UErrorCode& status); | |
676 | ||
677 | friend class MessageFormat; | |
678 | /** | |
679 | * Each ChoiceFormat divides the range -Inf..+Inf into fCount | |
680 | * intervals. The intervals are: | |
681 | * | |
682 | * 0: fChoiceLimits[0]..fChoiceLimits[1] | |
683 | * 1: fChoiceLimits[1]..fChoiceLimits[2] | |
684 | * ... | |
685 | * fCount-2: fChoiceLimits[fCount-2]..fChoiceLimits[fCount-1] | |
686 | * fCount-1: fChoiceLimits[fCount-1]..+Inf | |
687 | * | |
688 | * Interval 0 is special; during formatting (mapping numbers to | |
689 | * strings), it also contains all numbers less than | |
690 | * fChoiceLimits[0], as well as NaN values. | |
691 | * | |
692 | * Interval i maps to and from string fChoiceFormats[i]. When | |
693 | * parsing (mapping strings to numbers), then intervals map to | |
694 | * their lower limit, that is, interval i maps to fChoiceLimit[i]. | |
695 | * | |
696 | * The intervals may be closed, half open, or open. This affects | |
697 | * formatting but does not affect parsing. Interval i is affected | |
698 | * by fClosures[i] and fClosures[i+1]. If fClosures[i] | |
699 | * is FALSE, then the value fChoiceLimits[i] is in interval i. | |
700 | * That is, intervals i and i are: | |
701 | * | |
702 | * i-1: ... x < fChoiceLimits[i] | |
703 | * i: fChoiceLimits[i] <= x ... | |
704 | * | |
705 | * If fClosures[i] is TRUE, then the value fChoiceLimits[i] is | |
706 | * in interval i-1. That is, intervals i-1 and i are: | |
707 | * | |
708 | * i-1: ... x <= fChoiceLimits[i] | |
709 | * i: fChoiceLimits[i] < x ... | |
710 | * | |
711 | * Because of the nature of interval 0, fClosures[0] has no | |
712 | * effect. | |
713 | ||
714 | */ | |
715 | double* fChoiceLimits; | |
716 | UBool* fClosures; | |
717 | UnicodeString* fChoiceFormats; | |
718 | int32_t fCount; | |
719 | }; | |
720 | ||
b75a7d8f A |
721 | inline UnicodeString& |
722 | ChoiceFormat::format(const Formattable& obj, | |
723 | UnicodeString& appendTo, | |
724 | UErrorCode& status) const { | |
725 | // Don't use Format:: - use immediate base class only, | |
726 | // in case immediate base modifies behavior later. | |
727 | return NumberFormat::format(obj, appendTo, status); | |
728 | } | |
729 | ||
730 | inline UnicodeString& | |
731 | ChoiceFormat::format(double number, | |
732 | UnicodeString& appendTo) const { | |
733 | return NumberFormat::format(number, appendTo); | |
734 | } | |
735 | ||
736 | inline UnicodeString& | |
737 | ChoiceFormat::format(int32_t number, | |
738 | UnicodeString& appendTo) const { | |
739 | return NumberFormat::format(number, appendTo); | |
740 | } | |
741 | U_NAMESPACE_END | |
742 | ||
743 | #endif /* #if !UCONFIG_NO_FORMATTING */ | |
744 | ||
745 | #endif // _CHOICFMT | |
746 | //eof |