jpayne@69
|
1 // © 2016 and later: Unicode, Inc. and others.
|
jpayne@69
|
2 // License & terms of use: http://www.unicode.org/copyright.html
|
jpayne@69
|
3 /*
|
jpayne@69
|
4 ********************************************************************************
|
jpayne@69
|
5 * Copyright (C) 1997-2013, International Business Machines
|
jpayne@69
|
6 * Corporation and others. All Rights Reserved.
|
jpayne@69
|
7 ********************************************************************************
|
jpayne@69
|
8 *
|
jpayne@69
|
9 * File CHOICFMT.H
|
jpayne@69
|
10 *
|
jpayne@69
|
11 * Modification History:
|
jpayne@69
|
12 *
|
jpayne@69
|
13 * Date Name Description
|
jpayne@69
|
14 * 02/19/97 aliu Converted from java.
|
jpayne@69
|
15 * 03/20/97 helena Finished first cut of implementation and got rid
|
jpayne@69
|
16 * of nextDouble/previousDouble and replaced with
|
jpayne@69
|
17 * boolean array.
|
jpayne@69
|
18 * 4/10/97 aliu Clean up. Modified to work on AIX.
|
jpayne@69
|
19 * 8/6/97 nos Removed overloaded constructor, member var 'buffer'.
|
jpayne@69
|
20 * 07/22/98 stephen Removed operator!= (implemented in Format)
|
jpayne@69
|
21 ********************************************************************************
|
jpayne@69
|
22 */
|
jpayne@69
|
23
|
jpayne@69
|
24 #ifndef CHOICFMT_H
|
jpayne@69
|
25 #define CHOICFMT_H
|
jpayne@69
|
26
|
jpayne@69
|
27 #include "unicode/utypes.h"
|
jpayne@69
|
28
|
jpayne@69
|
29 #if U_SHOW_CPLUSPLUS_API
|
jpayne@69
|
30
|
jpayne@69
|
31 /**
|
jpayne@69
|
32 * \file
|
jpayne@69
|
33 * \brief C++ API: Choice Format.
|
jpayne@69
|
34 */
|
jpayne@69
|
35
|
jpayne@69
|
36 #if !UCONFIG_NO_FORMATTING
|
jpayne@69
|
37
|
jpayne@69
|
38 #include "unicode/fieldpos.h"
|
jpayne@69
|
39 #include "unicode/format.h"
|
jpayne@69
|
40 #include "unicode/messagepattern.h"
|
jpayne@69
|
41 #include "unicode/numfmt.h"
|
jpayne@69
|
42 #include "unicode/unistr.h"
|
jpayne@69
|
43
|
jpayne@69
|
44 #ifndef U_HIDE_DEPRECATED_API
|
jpayne@69
|
45
|
jpayne@69
|
46 U_NAMESPACE_BEGIN
|
jpayne@69
|
47
|
jpayne@69
|
48 class MessageFormat;
|
jpayne@69
|
49
|
jpayne@69
|
50 /**
|
jpayne@69
|
51 * ChoiceFormat converts between ranges of numeric values and strings for those ranges.
|
jpayne@69
|
52 * The strings must conform to the MessageFormat pattern syntax.
|
jpayne@69
|
53 *
|
jpayne@69
|
54 * <p><em><code>ChoiceFormat</code> is probably not what you need.
|
jpayne@69
|
55 * Please use <code>MessageFormat</code>
|
jpayne@69
|
56 * with <code>plural</code> arguments for proper plural selection,
|
jpayne@69
|
57 * and <code>select</code> arguments for simple selection among a fixed set of choices!</em></p>
|
jpayne@69
|
58 *
|
jpayne@69
|
59 * <p>A <code>ChoiceFormat</code> splits
|
jpayne@69
|
60 * the real number line \htmlonly<code>-∞</code> to
|
jpayne@69
|
61 * <code>+∞</code>\endhtmlonly into two
|
jpayne@69
|
62 * or more contiguous ranges. Each range is mapped to a
|
jpayne@69
|
63 * string.</p>
|
jpayne@69
|
64 *
|
jpayne@69
|
65 * <p><code>ChoiceFormat</code> was originally intended
|
jpayne@69
|
66 * for displaying grammatically correct
|
jpayne@69
|
67 * plurals such as "There is one file." vs. "There are 2 files."
|
jpayne@69
|
68 * <em>However,</em> plural rules for many languages
|
jpayne@69
|
69 * are too complex for the capabilities of ChoiceFormat,
|
jpayne@69
|
70 * and its requirement of specifying the precise rules for each message
|
jpayne@69
|
71 * is unmanageable for translators.</p>
|
jpayne@69
|
72 *
|
jpayne@69
|
73 * <p>There are two methods of defining a <code>ChoiceFormat</code>; both
|
jpayne@69
|
74 * are equivalent. The first is by using a string pattern. This is the
|
jpayne@69
|
75 * preferred method in most cases. The second method is through direct
|
jpayne@69
|
76 * specification of the arrays that logically make up the
|
jpayne@69
|
77 * <code>ChoiceFormat</code>.</p>
|
jpayne@69
|
78 *
|
jpayne@69
|
79 * <p>Note: Typically, choice formatting is done (if done at all) via <code>MessageFormat</code>
|
jpayne@69
|
80 * with a <code>choice</code> argument type,
|
jpayne@69
|
81 * rather than using a stand-alone <code>ChoiceFormat</code>.</p>
|
jpayne@69
|
82 *
|
jpayne@69
|
83 * <h5>Patterns and Their Interpretation</h5>
|
jpayne@69
|
84 *
|
jpayne@69
|
85 * <p>The pattern string defines the range boundaries and the strings for each number range.
|
jpayne@69
|
86 * Syntax:
|
jpayne@69
|
87 * <pre>
|
jpayne@69
|
88 * choiceStyle = number separator message ('|' number separator message)*
|
jpayne@69
|
89 * number = normal_number | ['-'] \htmlonly∞\endhtmlonly (U+221E, infinity)
|
jpayne@69
|
90 * normal_number = double value (unlocalized ASCII string)
|
jpayne@69
|
91 * separator = less_than | less_than_or_equal
|
jpayne@69
|
92 * less_than = '<'
|
jpayne@69
|
93 * less_than_or_equal = '#' | \htmlonly≤\endhtmlonly (U+2264)
|
jpayne@69
|
94 * message: see {@link MessageFormat}
|
jpayne@69
|
95 * </pre>
|
jpayne@69
|
96 * Pattern_White_Space between syntax elements is ignored, except
|
jpayne@69
|
97 * around each range's sub-message.</p>
|
jpayne@69
|
98 *
|
jpayne@69
|
99 * <p>Each numeric sub-range extends from the current range's number
|
jpayne@69
|
100 * to the next range's number.
|
jpayne@69
|
101 * The number itself is included in its range if a <code>less_than_or_equal</code> sign is used,
|
jpayne@69
|
102 * and excluded from its range (and instead included in the previous range)
|
jpayne@69
|
103 * if a <code>less_than</code> sign is used.</p>
|
jpayne@69
|
104 *
|
jpayne@69
|
105 * <p>When a <code>ChoiceFormat</code> is constructed from
|
jpayne@69
|
106 * arrays of numbers, closure flags and strings,
|
jpayne@69
|
107 * they are interpreted just like
|
jpayne@69
|
108 * the sequence of <code>(number separator string)</code> in an equivalent pattern string.
|
jpayne@69
|
109 * <code>closure[i]==TRUE</code> corresponds to a <code>less_than</code> separator sign.
|
jpayne@69
|
110 * The equivalent pattern string will be constructed automatically.</p>
|
jpayne@69
|
111 *
|
jpayne@69
|
112 * <p>During formatting, a number is mapped to the first range
|
jpayne@69
|
113 * where the number is not greater than the range's upper limit.
|
jpayne@69
|
114 * That range's message string is returned. A NaN maps to the very first range.</p>
|
jpayne@69
|
115 *
|
jpayne@69
|
116 * <p>During parsing, a range is selected for the longest match of
|
jpayne@69
|
117 * any range's message. That range's number is returned, ignoring the separator/closure.
|
jpayne@69
|
118 * Only a simple string match is performed, without parsing of arguments that
|
jpayne@69
|
119 * might be specified in the message strings.</p>
|
jpayne@69
|
120 *
|
jpayne@69
|
121 * <p>Note that the first range's number is ignored in formatting
|
jpayne@69
|
122 * but may be returned from parsing.</p>
|
jpayne@69
|
123 *
|
jpayne@69
|
124 * <h5>Examples</h5>
|
jpayne@69
|
125 *
|
jpayne@69
|
126 * <p>Here is an example of two arrays that map the number
|
jpayne@69
|
127 * <code>1..7</code> to the English day of the week abbreviations
|
jpayne@69
|
128 * <code>Sun..Sat</code>. No closures array is given; this is the same as
|
jpayne@69
|
129 * specifying all closures to be <code>FALSE</code>.</p>
|
jpayne@69
|
130 *
|
jpayne@69
|
131 * <pre> {1,2,3,4,5,6,7},
|
jpayne@69
|
132 * {"Sun","Mon","Tue","Wed","Thur","Fri","Sat"}</pre>
|
jpayne@69
|
133 *
|
jpayne@69
|
134 * <p>Here is an example that maps the ranges [-Inf, 1), [1, 1], and (1,
|
jpayne@69
|
135 * +Inf] to three strings. That is, the number line is split into three
|
jpayne@69
|
136 * ranges: x < 1.0, x = 1.0, and x > 1.0.
|
jpayne@69
|
137 * (The round parentheses in the notation above indicate an exclusive boundary,
|
jpayne@69
|
138 * like the turned bracket in European notation: [-Inf, 1) == [-Inf, 1[ )</p>
|
jpayne@69
|
139 *
|
jpayne@69
|
140 * <pre> {0, 1, 1},
|
jpayne@69
|
141 * {FALSE, FALSE, TRUE},
|
jpayne@69
|
142 * {"no files", "one file", "many files"}</pre>
|
jpayne@69
|
143 *
|
jpayne@69
|
144 * <p>Here is an example that shows formatting and parsing: </p>
|
jpayne@69
|
145 *
|
jpayne@69
|
146 * \code
|
jpayne@69
|
147 * #include <unicode/choicfmt.h>
|
jpayne@69
|
148 * #include <unicode/unistr.h>
|
jpayne@69
|
149 * #include <iostream.h>
|
jpayne@69
|
150 *
|
jpayne@69
|
151 * int main(int argc, char *argv[]) {
|
jpayne@69
|
152 * double limits[] = {1,2,3,4,5,6,7};
|
jpayne@69
|
153 * UnicodeString monthNames[] = {
|
jpayne@69
|
154 * "Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
|
jpayne@69
|
155 * ChoiceFormat fmt(limits, monthNames, 7);
|
jpayne@69
|
156 * UnicodeString str;
|
jpayne@69
|
157 * char buf[256];
|
jpayne@69
|
158 * for (double x = 1.0; x <= 8.0; x += 1.0) {
|
jpayne@69
|
159 * fmt.format(x, str);
|
jpayne@69
|
160 * str.extract(0, str.length(), buf, 256, "");
|
jpayne@69
|
161 * str.truncate(0);
|
jpayne@69
|
162 * cout << x << " -> "
|
jpayne@69
|
163 * << buf << endl;
|
jpayne@69
|
164 * }
|
jpayne@69
|
165 * cout << endl;
|
jpayne@69
|
166 * return 0;
|
jpayne@69
|
167 * }
|
jpayne@69
|
168 * \endcode
|
jpayne@69
|
169 *
|
jpayne@69
|
170 * <p><em>User subclasses are not supported.</em> While clients may write
|
jpayne@69
|
171 * subclasses, such code will not necessarily work and will not be
|
jpayne@69
|
172 * guaranteed to work stably from release to release.
|
jpayne@69
|
173 *
|
jpayne@69
|
174 * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments.
|
jpayne@69
|
175 */
|
jpayne@69
|
176 class U_I18N_API ChoiceFormat: public NumberFormat {
|
jpayne@69
|
177 public:
|
jpayne@69
|
178 /**
|
jpayne@69
|
179 * Constructs a new ChoiceFormat from the pattern string.
|
jpayne@69
|
180 *
|
jpayne@69
|
181 * @param pattern Pattern used to construct object.
|
jpayne@69
|
182 * @param status Output param to receive success code. If the
|
jpayne@69
|
183 * pattern cannot be parsed, set to failure code.
|
jpayne@69
|
184 * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments.
|
jpayne@69
|
185 */
|
jpayne@69
|
186 ChoiceFormat(const UnicodeString& pattern,
|
jpayne@69
|
187 UErrorCode& status);
|
jpayne@69
|
188
|
jpayne@69
|
189
|
jpayne@69
|
190 /**
|
jpayne@69
|
191 * Constructs a new ChoiceFormat with the given limits and message strings.
|
jpayne@69
|
192 * All closure flags default to <code>FALSE</code>,
|
jpayne@69
|
193 * equivalent to <code>less_than_or_equal</code> separators.
|
jpayne@69
|
194 *
|
jpayne@69
|
195 * Copies the limits and formats instead of adopting them.
|
jpayne@69
|
196 *
|
jpayne@69
|
197 * @param limits Array of limit values.
|
jpayne@69
|
198 * @param formats Array of formats.
|
jpayne@69
|
199 * @param count Size of 'limits' and 'formats' arrays.
|
jpayne@69
|
200 * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments.
|
jpayne@69
|
201 */
|
jpayne@69
|
202 ChoiceFormat(const double* limits,
|
jpayne@69
|
203 const UnicodeString* formats,
|
jpayne@69
|
204 int32_t count );
|
jpayne@69
|
205
|
jpayne@69
|
206 /**
|
jpayne@69
|
207 * Constructs a new ChoiceFormat with the given limits, closure flags and message strings.
|
jpayne@69
|
208 *
|
jpayne@69
|
209 * Copies the limits and formats instead of adopting them.
|
jpayne@69
|
210 *
|
jpayne@69
|
211 * @param limits Array of limit values
|
jpayne@69
|
212 * @param closures Array of booleans specifying whether each
|
jpayne@69
|
213 * element of 'limits' is open or closed. If FALSE, then the
|
jpayne@69
|
214 * corresponding limit number is a member of its range.
|
jpayne@69
|
215 * If TRUE, then the limit number belongs to the previous range it.
|
jpayne@69
|
216 * @param formats Array of formats
|
jpayne@69
|
217 * @param count Size of 'limits', 'closures', and 'formats' arrays
|
jpayne@69
|
218 * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments.
|
jpayne@69
|
219 */
|
jpayne@69
|
220 ChoiceFormat(const double* limits,
|
jpayne@69
|
221 const UBool* closures,
|
jpayne@69
|
222 const UnicodeString* formats,
|
jpayne@69
|
223 int32_t count);
|
jpayne@69
|
224
|
jpayne@69
|
225 /**
|
jpayne@69
|
226 * Copy constructor.
|
jpayne@69
|
227 *
|
jpayne@69
|
228 * @param that ChoiceFormat object to be copied from
|
jpayne@69
|
229 * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments.
|
jpayne@69
|
230 */
|
jpayne@69
|
231 ChoiceFormat(const ChoiceFormat& that);
|
jpayne@69
|
232
|
jpayne@69
|
233 /**
|
jpayne@69
|
234 * Assignment operator.
|
jpayne@69
|
235 *
|
jpayne@69
|
236 * @param that ChoiceFormat object to be copied
|
jpayne@69
|
237 * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments.
|
jpayne@69
|
238 */
|
jpayne@69
|
239 const ChoiceFormat& operator=(const ChoiceFormat& that);
|
jpayne@69
|
240
|
jpayne@69
|
241 /**
|
jpayne@69
|
242 * Destructor.
|
jpayne@69
|
243 * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments.
|
jpayne@69
|
244 */
|
jpayne@69
|
245 virtual ~ChoiceFormat();
|
jpayne@69
|
246
|
jpayne@69
|
247 /**
|
jpayne@69
|
248 * Clones this Format object. The caller owns the
|
jpayne@69
|
249 * result and must delete it when done.
|
jpayne@69
|
250 *
|
jpayne@69
|
251 * @return a copy of this object
|
jpayne@69
|
252 * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments.
|
jpayne@69
|
253 */
|
jpayne@69
|
254 virtual ChoiceFormat* clone() const;
|
jpayne@69
|
255
|
jpayne@69
|
256 /**
|
jpayne@69
|
257 * Returns true if the given Format objects are semantically equal.
|
jpayne@69
|
258 * Objects of different subclasses are considered unequal.
|
jpayne@69
|
259 *
|
jpayne@69
|
260 * @param other ChoiceFormat object to be compared
|
jpayne@69
|
261 * @return true if other is the same as this.
|
jpayne@69
|
262 * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments.
|
jpayne@69
|
263 */
|
jpayne@69
|
264 virtual UBool operator==(const Format& other) const;
|
jpayne@69
|
265
|
jpayne@69
|
266 /**
|
jpayne@69
|
267 * Sets the pattern.
|
jpayne@69
|
268 * @param pattern The pattern to be applied.
|
jpayne@69
|
269 * @param status Output param set to success/failure code on
|
jpayne@69
|
270 * exit. If the pattern is invalid, this will be
|
jpayne@69
|
271 * set to a failure result.
|
jpayne@69
|
272 * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments.
|
jpayne@69
|
273 */
|
jpayne@69
|
274 virtual void applyPattern(const UnicodeString& pattern,
|
jpayne@69
|
275 UErrorCode& status);
|
jpayne@69
|
276
|
jpayne@69
|
277 /**
|
jpayne@69
|
278 * Sets the pattern.
|
jpayne@69
|
279 * @param pattern The pattern to be applied.
|
jpayne@69
|
280 * @param parseError Struct to receive information on position
|
jpayne@69
|
281 * of error if an error is encountered
|
jpayne@69
|
282 * @param status Output param set to success/failure code on
|
jpayne@69
|
283 * exit. If the pattern is invalid, this will be
|
jpayne@69
|
284 * set to a failure result.
|
jpayne@69
|
285 * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments.
|
jpayne@69
|
286 */
|
jpayne@69
|
287 virtual void applyPattern(const UnicodeString& pattern,
|
jpayne@69
|
288 UParseError& parseError,
|
jpayne@69
|
289 UErrorCode& status);
|
jpayne@69
|
290 /**
|
jpayne@69
|
291 * Gets the pattern.
|
jpayne@69
|
292 *
|
jpayne@69
|
293 * @param pattern Output param which will receive the pattern
|
jpayne@69
|
294 * Previous contents are deleted.
|
jpayne@69
|
295 * @return A reference to 'pattern'
|
jpayne@69
|
296 * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments.
|
jpayne@69
|
297 */
|
jpayne@69
|
298 virtual UnicodeString& toPattern(UnicodeString &pattern) const;
|
jpayne@69
|
299
|
jpayne@69
|
300 /**
|
jpayne@69
|
301 * Sets the choices to be used in formatting.
|
jpayne@69
|
302 * For details see the constructor with the same parameter list.
|
jpayne@69
|
303 *
|
jpayne@69
|
304 * @param limitsToCopy Contains the top value that you want
|
jpayne@69
|
305 * parsed with that format,and should be in
|
jpayne@69
|
306 * ascending sorted order. When formatting X,
|
jpayne@69
|
307 * the choice will be the i, where limit[i]
|
jpayne@69
|
308 * <= X < limit[i+1].
|
jpayne@69
|
309 * @param formatsToCopy The format strings you want to use for each limit.
|
jpayne@69
|
310 * @param count The size of the above arrays.
|
jpayne@69
|
311 * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments.
|
jpayne@69
|
312 */
|
jpayne@69
|
313 virtual void setChoices(const double* limitsToCopy,
|
jpayne@69
|
314 const UnicodeString* formatsToCopy,
|
jpayne@69
|
315 int32_t count );
|
jpayne@69
|
316
|
jpayne@69
|
317 /**
|
jpayne@69
|
318 * Sets the choices to be used in formatting.
|
jpayne@69
|
319 * For details see the constructor with the same parameter list.
|
jpayne@69
|
320 *
|
jpayne@69
|
321 * @param limits Array of limits
|
jpayne@69
|
322 * @param closures Array of limit booleans
|
jpayne@69
|
323 * @param formats Array of format string
|
jpayne@69
|
324 * @param count The size of the above arrays
|
jpayne@69
|
325 * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments.
|
jpayne@69
|
326 */
|
jpayne@69
|
327 virtual void setChoices(const double* limits,
|
jpayne@69
|
328 const UBool* closures,
|
jpayne@69
|
329 const UnicodeString* formats,
|
jpayne@69
|
330 int32_t count);
|
jpayne@69
|
331
|
jpayne@69
|
332 /**
|
jpayne@69
|
333 * Returns NULL and 0.
|
jpayne@69
|
334 * Before ICU 4.8, this used to return the choice limits array.
|
jpayne@69
|
335 *
|
jpayne@69
|
336 * @param count Will be set to 0.
|
jpayne@69
|
337 * @return NULL
|
jpayne@69
|
338 * @deprecated ICU 4.8 Use the MessagePattern class to analyze a ChoiceFormat pattern.
|
jpayne@69
|
339 */
|
jpayne@69
|
340 virtual const double* getLimits(int32_t& count) const;
|
jpayne@69
|
341
|
jpayne@69
|
342 /**
|
jpayne@69
|
343 * Returns NULL and 0.
|
jpayne@69
|
344 * Before ICU 4.8, this used to return the limit booleans array.
|
jpayne@69
|
345 *
|
jpayne@69
|
346 * @param count Will be set to 0.
|
jpayne@69
|
347 * @return NULL
|
jpayne@69
|
348 * @deprecated ICU 4.8 Use the MessagePattern class to analyze a ChoiceFormat pattern.
|
jpayne@69
|
349 */
|
jpayne@69
|
350 virtual const UBool* getClosures(int32_t& count) const;
|
jpayne@69
|
351
|
jpayne@69
|
352 /**
|
jpayne@69
|
353 * Returns NULL and 0.
|
jpayne@69
|
354 * Before ICU 4.8, this used to return the array of choice strings.
|
jpayne@69
|
355 *
|
jpayne@69
|
356 * @param count Will be set to 0.
|
jpayne@69
|
357 * @return NULL
|
jpayne@69
|
358 * @deprecated ICU 4.8 Use the MessagePattern class to analyze a ChoiceFormat pattern.
|
jpayne@69
|
359 */
|
jpayne@69
|
360 virtual const UnicodeString* getFormats(int32_t& count) const;
|
jpayne@69
|
361
|
jpayne@69
|
362
|
jpayne@69
|
363 using NumberFormat::format;
|
jpayne@69
|
364
|
jpayne@69
|
365 /**
|
jpayne@69
|
366 * Formats a double number using this object's choices.
|
jpayne@69
|
367 *
|
jpayne@69
|
368 * @param number The value to be formatted.
|
jpayne@69
|
369 * @param appendTo Output parameter to receive result.
|
jpayne@69
|
370 * Result is appended to existing contents.
|
jpayne@69
|
371 * @param pos On input: an alignment field, if desired.
|
jpayne@69
|
372 * On output: the offsets of the alignment field.
|
jpayne@69
|
373 * @return Reference to 'appendTo' parameter.
|
jpayne@69
|
374 * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments.
|
jpayne@69
|
375 */
|
jpayne@69
|
376 virtual UnicodeString& format(double number,
|
jpayne@69
|
377 UnicodeString& appendTo,
|
jpayne@69
|
378 FieldPosition& pos) const;
|
jpayne@69
|
379 /**
|
jpayne@69
|
380 * Formats an int32_t number using this object's choices.
|
jpayne@69
|
381 *
|
jpayne@69
|
382 * @param number The value to be formatted.
|
jpayne@69
|
383 * @param appendTo Output parameter to receive result.
|
jpayne@69
|
384 * Result is appended to existing contents.
|
jpayne@69
|
385 * @param pos On input: an alignment field, if desired.
|
jpayne@69
|
386 * On output: the offsets of the alignment field.
|
jpayne@69
|
387 * @return Reference to 'appendTo' parameter.
|
jpayne@69
|
388 * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments.
|
jpayne@69
|
389 */
|
jpayne@69
|
390 virtual UnicodeString& format(int32_t number,
|
jpayne@69
|
391 UnicodeString& appendTo,
|
jpayne@69
|
392 FieldPosition& pos) const;
|
jpayne@69
|
393
|
jpayne@69
|
394 /**
|
jpayne@69
|
395 * Formats an int64_t number using this object's choices.
|
jpayne@69
|
396 *
|
jpayne@69
|
397 * @param number The value to be formatted.
|
jpayne@69
|
398 * @param appendTo Output parameter to receive result.
|
jpayne@69
|
399 * Result is appended to existing contents.
|
jpayne@69
|
400 * @param pos On input: an alignment field, if desired.
|
jpayne@69
|
401 * On output: the offsets of the alignment field.
|
jpayne@69
|
402 * @return Reference to 'appendTo' parameter.
|
jpayne@69
|
403 * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments.
|
jpayne@69
|
404 */
|
jpayne@69
|
405 virtual UnicodeString& format(int64_t number,
|
jpayne@69
|
406 UnicodeString& appendTo,
|
jpayne@69
|
407 FieldPosition& pos) const;
|
jpayne@69
|
408
|
jpayne@69
|
409 /**
|
jpayne@69
|
410 * Formats an array of objects using this object's choices.
|
jpayne@69
|
411 *
|
jpayne@69
|
412 * @param objs The array of objects to be formatted.
|
jpayne@69
|
413 * @param cnt The size of objs.
|
jpayne@69
|
414 * @param appendTo Output parameter to receive result.
|
jpayne@69
|
415 * Result is appended to existing contents.
|
jpayne@69
|
416 * @param pos On input: an alignment field, if desired.
|
jpayne@69
|
417 * On output: the offsets of the alignment field.
|
jpayne@69
|
418 * @param success Output param set to success/failure code on
|
jpayne@69
|
419 * exit.
|
jpayne@69
|
420 * @return Reference to 'appendTo' parameter.
|
jpayne@69
|
421 * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments.
|
jpayne@69
|
422 */
|
jpayne@69
|
423 virtual UnicodeString& format(const Formattable* objs,
|
jpayne@69
|
424 int32_t cnt,
|
jpayne@69
|
425 UnicodeString& appendTo,
|
jpayne@69
|
426 FieldPosition& pos,
|
jpayne@69
|
427 UErrorCode& success) const;
|
jpayne@69
|
428
|
jpayne@69
|
429 using NumberFormat::parse;
|
jpayne@69
|
430
|
jpayne@69
|
431 /**
|
jpayne@69
|
432 * Looks for the longest match of any message string on the input text and,
|
jpayne@69
|
433 * if there is a match, sets the result object to the corresponding range's number.
|
jpayne@69
|
434 *
|
jpayne@69
|
435 * If no string matches, then the parsePosition is unchanged.
|
jpayne@69
|
436 *
|
jpayne@69
|
437 * @param text The text to be parsed.
|
jpayne@69
|
438 * @param result Formattable to be set to the parse result.
|
jpayne@69
|
439 * If parse fails, return contents are undefined.
|
jpayne@69
|
440 * @param parsePosition The position to start parsing at on input.
|
jpayne@69
|
441 * On output, moved to after the last successfully
|
jpayne@69
|
442 * parse character. On parse failure, does not change.
|
jpayne@69
|
443 * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments.
|
jpayne@69
|
444 */
|
jpayne@69
|
445 virtual void parse(const UnicodeString& text,
|
jpayne@69
|
446 Formattable& result,
|
jpayne@69
|
447 ParsePosition& parsePosition) const;
|
jpayne@69
|
448
|
jpayne@69
|
449 /**
|
jpayne@69
|
450 * Returns a unique class ID POLYMORPHICALLY. Part of ICU's "poor man's RTTI".
|
jpayne@69
|
451 *
|
jpayne@69
|
452 * @return The class ID for this object. All objects of a
|
jpayne@69
|
453 * given class have the same class ID. Objects of
|
jpayne@69
|
454 * other classes have different class IDs.
|
jpayne@69
|
455 * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments.
|
jpayne@69
|
456 */
|
jpayne@69
|
457 virtual UClassID getDynamicClassID(void) const;
|
jpayne@69
|
458
|
jpayne@69
|
459 /**
|
jpayne@69
|
460 * Returns the class ID for this class. This is useful only for
|
jpayne@69
|
461 * comparing to a return value from getDynamicClassID(). For example:
|
jpayne@69
|
462 * <pre>
|
jpayne@69
|
463 * . Base* polymorphic_pointer = createPolymorphicObject();
|
jpayne@69
|
464 * . if (polymorphic_pointer->getDynamicClassID() ==
|
jpayne@69
|
465 * . Derived::getStaticClassID()) ...
|
jpayne@69
|
466 * </pre>
|
jpayne@69
|
467 * @return The class ID for all objects of this class.
|
jpayne@69
|
468 * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments.
|
jpayne@69
|
469 */
|
jpayne@69
|
470 static UClassID U_EXPORT2 getStaticClassID(void);
|
jpayne@69
|
471
|
jpayne@69
|
472 private:
|
jpayne@69
|
473 /**
|
jpayne@69
|
474 * Converts a double value to a string.
|
jpayne@69
|
475 * @param value the double number to be converted.
|
jpayne@69
|
476 * @param string the result string.
|
jpayne@69
|
477 * @return the converted string.
|
jpayne@69
|
478 */
|
jpayne@69
|
479 static UnicodeString& dtos(double value, UnicodeString& string);
|
jpayne@69
|
480
|
jpayne@69
|
481 ChoiceFormat(); // default constructor not implemented
|
jpayne@69
|
482
|
jpayne@69
|
483 /**
|
jpayne@69
|
484 * Construct a new ChoiceFormat with the limits and the corresponding formats
|
jpayne@69
|
485 * based on the pattern.
|
jpayne@69
|
486 *
|
jpayne@69
|
487 * @param newPattern Pattern used to construct object.
|
jpayne@69
|
488 * @param parseError Struct to receive information on position
|
jpayne@69
|
489 * of error if an error is encountered.
|
jpayne@69
|
490 * @param status Output param to receive success code. If the
|
jpayne@69
|
491 * pattern cannot be parsed, set to failure code.
|
jpayne@69
|
492 */
|
jpayne@69
|
493 ChoiceFormat(const UnicodeString& newPattern,
|
jpayne@69
|
494 UParseError& parseError,
|
jpayne@69
|
495 UErrorCode& status);
|
jpayne@69
|
496
|
jpayne@69
|
497 friend class MessageFormat;
|
jpayne@69
|
498
|
jpayne@69
|
499 virtual void setChoices(const double* limits,
|
jpayne@69
|
500 const UBool* closures,
|
jpayne@69
|
501 const UnicodeString* formats,
|
jpayne@69
|
502 int32_t count,
|
jpayne@69
|
503 UErrorCode &errorCode);
|
jpayne@69
|
504
|
jpayne@69
|
505 /**
|
jpayne@69
|
506 * Finds the ChoiceFormat sub-message for the given number.
|
jpayne@69
|
507 * @param pattern A MessagePattern.
|
jpayne@69
|
508 * @param partIndex the index of the first ChoiceFormat argument style part.
|
jpayne@69
|
509 * @param number a number to be mapped to one of the ChoiceFormat argument's intervals
|
jpayne@69
|
510 * @return the sub-message start part index.
|
jpayne@69
|
511 */
|
jpayne@69
|
512 static int32_t findSubMessage(const MessagePattern &pattern, int32_t partIndex, double number);
|
jpayne@69
|
513
|
jpayne@69
|
514 static double parseArgument(
|
jpayne@69
|
515 const MessagePattern &pattern, int32_t partIndex,
|
jpayne@69
|
516 const UnicodeString &source, ParsePosition &pos);
|
jpayne@69
|
517
|
jpayne@69
|
518 /**
|
jpayne@69
|
519 * Matches the pattern string from the end of the partIndex to
|
jpayne@69
|
520 * the beginning of the limitPartIndex,
|
jpayne@69
|
521 * including all syntax except SKIP_SYNTAX,
|
jpayne@69
|
522 * against the source string starting at sourceOffset.
|
jpayne@69
|
523 * If they match, returns the length of the source string match.
|
jpayne@69
|
524 * Otherwise returns -1.
|
jpayne@69
|
525 */
|
jpayne@69
|
526 static int32_t matchStringUntilLimitPart(
|
jpayne@69
|
527 const MessagePattern &pattern, int32_t partIndex, int32_t limitPartIndex,
|
jpayne@69
|
528 const UnicodeString &source, int32_t sourceOffset);
|
jpayne@69
|
529
|
jpayne@69
|
530 /**
|
jpayne@69
|
531 * Some of the ChoiceFormat constructors do not have a UErrorCode paramater.
|
jpayne@69
|
532 * We need _some_ way to provide one for the MessagePattern constructor.
|
jpayne@69
|
533 * Alternatively, the MessagePattern could be a pointer field, but that is
|
jpayne@69
|
534 * not nice either.
|
jpayne@69
|
535 */
|
jpayne@69
|
536 UErrorCode constructorErrorCode;
|
jpayne@69
|
537
|
jpayne@69
|
538 /**
|
jpayne@69
|
539 * The MessagePattern which contains the parsed structure of the pattern string.
|
jpayne@69
|
540 *
|
jpayne@69
|
541 * Starting with ICU 4.8, the MessagePattern contains a sequence of
|
jpayne@69
|
542 * numeric/selector/message parts corresponding to the parsed pattern.
|
jpayne@69
|
543 * For details see the MessagePattern class API docs.
|
jpayne@69
|
544 */
|
jpayne@69
|
545 MessagePattern msgPattern;
|
jpayne@69
|
546
|
jpayne@69
|
547 /**
|
jpayne@69
|
548 * Docs & fields from before ICU 4.8, before MessagePattern was used.
|
jpayne@69
|
549 * Commented out, and left only for explanation of semantics.
|
jpayne@69
|
550 * --------
|
jpayne@69
|
551 * Each ChoiceFormat divides the range -Inf..+Inf into fCount
|
jpayne@69
|
552 * intervals. The intervals are:
|
jpayne@69
|
553 *
|
jpayne@69
|
554 * 0: fChoiceLimits[0]..fChoiceLimits[1]
|
jpayne@69
|
555 * 1: fChoiceLimits[1]..fChoiceLimits[2]
|
jpayne@69
|
556 * ...
|
jpayne@69
|
557 * fCount-2: fChoiceLimits[fCount-2]..fChoiceLimits[fCount-1]
|
jpayne@69
|
558 * fCount-1: fChoiceLimits[fCount-1]..+Inf
|
jpayne@69
|
559 *
|
jpayne@69
|
560 * Interval 0 is special; during formatting (mapping numbers to
|
jpayne@69
|
561 * strings), it also contains all numbers less than
|
jpayne@69
|
562 * fChoiceLimits[0], as well as NaN values.
|
jpayne@69
|
563 *
|
jpayne@69
|
564 * Interval i maps to and from string fChoiceFormats[i]. When
|
jpayne@69
|
565 * parsing (mapping strings to numbers), then intervals map to
|
jpayne@69
|
566 * their lower limit, that is, interval i maps to fChoiceLimit[i].
|
jpayne@69
|
567 *
|
jpayne@69
|
568 * The intervals may be closed, half open, or open. This affects
|
jpayne@69
|
569 * formatting but does not affect parsing. Interval i is affected
|
jpayne@69
|
570 * by fClosures[i] and fClosures[i+1]. If fClosures[i]
|
jpayne@69
|
571 * is FALSE, then the value fChoiceLimits[i] is in interval i.
|
jpayne@69
|
572 * That is, intervals i and i are:
|
jpayne@69
|
573 *
|
jpayne@69
|
574 * i-1: ... x < fChoiceLimits[i]
|
jpayne@69
|
575 * i: fChoiceLimits[i] <= x ...
|
jpayne@69
|
576 *
|
jpayne@69
|
577 * If fClosures[i] is TRUE, then the value fChoiceLimits[i] is
|
jpayne@69
|
578 * in interval i-1. That is, intervals i-1 and i are:
|
jpayne@69
|
579 *
|
jpayne@69
|
580 * i-1: ... x <= fChoiceLimits[i]
|
jpayne@69
|
581 * i: fChoiceLimits[i] < x ...
|
jpayne@69
|
582 *
|
jpayne@69
|
583 * Because of the nature of interval 0, fClosures[0] has no
|
jpayne@69
|
584 * effect.
|
jpayne@69
|
585 */
|
jpayne@69
|
586 // double* fChoiceLimits;
|
jpayne@69
|
587 // UBool* fClosures;
|
jpayne@69
|
588 // UnicodeString* fChoiceFormats;
|
jpayne@69
|
589 // int32_t fCount;
|
jpayne@69
|
590 };
|
jpayne@69
|
591
|
jpayne@69
|
592
|
jpayne@69
|
593 U_NAMESPACE_END
|
jpayne@69
|
594
|
jpayne@69
|
595 #endif // U_HIDE_DEPRECATED_API
|
jpayne@69
|
596 #endif /* #if !UCONFIG_NO_FORMATTING */
|
jpayne@69
|
597
|
jpayne@69
|
598 #endif /* U_SHOW_CPLUSPLUS_API */
|
jpayne@69
|
599
|
jpayne@69
|
600 #endif // CHOICFMT_H
|
jpayne@69
|
601 //eof
|