1 // Copyright (C) 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 ********************************************************************************
5 * Copyright (C) 1997-2013, International Business Machines
6 * Corporation and others. All Rights Reserved.
7 ********************************************************************************
11 * Modification History:
13 * Date Name Description
14 * 02/19/97 aliu Converted from java.
15 * 03/20/97 helena Finished first cut of implementation and got rid
16 * of nextDouble/previousDouble and replaced with
18 * 4/10/97 aliu Clean up. Modified to work on AIX.
19 * 8/6/97 nos Removed overloaded constructor, member var 'buffer'.
20 * 07/22/98 stephen Removed operator!= (implemented in Format)
21 ********************************************************************************
27 #include "unicode/utypes.h"
31 * \brief C++ API: Choice Format.
34 #if !UCONFIG_NO_FORMATTING
35 #ifndef U_HIDE_DEPRECATED_API
37 #include "unicode/fieldpos.h"
38 #include "unicode/format.h"
39 #include "unicode/messagepattern.h"
40 #include "unicode/numfmt.h"
41 #include "unicode/unistr.h"
48 * ChoiceFormat converts between ranges of numeric values and strings for those ranges.
49 * The strings must conform to the MessageFormat pattern syntax.
51 * <p><em><code>ChoiceFormat</code> is probably not what you need.
52 * Please use <code>MessageFormat</code>
53 * with <code>plural</code> arguments for proper plural selection,
54 * and <code>select</code> arguments for simple selection among a fixed set of choices!</em></p>
56 * <p>A <code>ChoiceFormat</code> splits
57 * the real number line \htmlonly<code>-∞</code> to
58 * <code>+∞</code>\endhtmlonly into two
59 * or more contiguous ranges. Each range is mapped to a
62 * <p><code>ChoiceFormat</code> was originally intended
63 * for displaying grammatically correct
64 * plurals such as "There is one file." vs. "There are 2 files."
65 * <em>However,</em> plural rules for many languages
66 * are too complex for the capabilities of ChoiceFormat,
67 * and its requirement of specifying the precise rules for each message
68 * is unmanageable for translators.</p>
70 * <p>There are two methods of defining a <code>ChoiceFormat</code>; both
71 * are equivalent. The first is by using a string pattern. This is the
72 * preferred method in most cases. The second method is through direct
73 * specification of the arrays that logically make up the
74 * <code>ChoiceFormat</code>.</p>
76 * <p>Note: Typically, choice formatting is done (if done at all) via <code>MessageFormat</code>
77 * with a <code>choice</code> argument type,
78 * rather than using a stand-alone <code>ChoiceFormat</code>.</p>
80 * <h5>Patterns and Their Interpretation</h5>
82 * <p>The pattern string defines the range boundaries and the strings for each number range.
85 * choiceStyle = number separator message ('|' number separator message)*
86 * number = normal_number | ['-'] \htmlonly∞\endhtmlonly (U+221E, infinity)
87 * normal_number = double value (unlocalized ASCII string)
88 * separator = less_than | less_than_or_equal
90 * less_than_or_equal = '#' | \htmlonly≤\endhtmlonly (U+2264)
91 * message: see {@link MessageFormat}
93 * Pattern_White_Space between syntax elements is ignored, except
94 * around each range's sub-message.</p>
96 * <p>Each numeric sub-range extends from the current range's number
97 * to the next range's number.
98 * The number itself is included in its range if a <code>less_than_or_equal</code> sign is used,
99 * and excluded from its range (and instead included in the previous range)
100 * if a <code>less_than</code> sign is used.</p>
102 * <p>When a <code>ChoiceFormat</code> is constructed from
103 * arrays of numbers, closure flags and strings,
104 * they are interpreted just like
105 * the sequence of <code>(number separator string)</code> in an equivalent pattern string.
106 * <code>closure[i]==TRUE</code> corresponds to a <code>less_than</code> separator sign.
107 * The equivalent pattern string will be constructed automatically.</p>
109 * <p>During formatting, a number is mapped to the first range
110 * where the number is not greater than the range's upper limit.
111 * That range's message string is returned. A NaN maps to the very first range.</p>
113 * <p>During parsing, a range is selected for the longest match of
114 * any range's message. That range's number is returned, ignoring the separator/closure.
115 * Only a simple string match is performed, without parsing of arguments that
116 * might be specified in the message strings.</p>
118 * <p>Note that the first range's number is ignored in formatting
119 * but may be returned from parsing.</p>
123 * <p>Here is an example of two arrays that map the number
124 * <code>1..7</code> to the English day of the week abbreviations
125 * <code>Sun..Sat</code>. No closures array is given; this is the same as
126 * specifying all closures to be <code>FALSE</code>.</p>
128 * <pre> {1,2,3,4,5,6,7},
129 * {"Sun","Mon","Tue","Wed","Thur","Fri","Sat"}</pre>
131 * <p>Here is an example that maps the ranges [-Inf, 1), [1, 1], and (1,
132 * +Inf] to three strings. That is, the number line is split into three
133 * ranges: x < 1.0, x = 1.0, and x > 1.0.
134 * (The round parentheses in the notation above indicate an exclusive boundary,
135 * like the turned bracket in European notation: [-Inf, 1) == [-Inf, 1[ )</p>
138 * {FALSE, FALSE, TRUE},
139 * {"no files", "one file", "many files"}</pre>
141 * <p>Here is an example that shows formatting and parsing: </p>
144 * #include <unicode/choicfmt.h>
145 * #include <unicode/unistr.h>
146 * #include <iostream.h>
148 * int main(int argc, char *argv[]) {
149 * double limits[] = {1,2,3,4,5,6,7};
150 * UnicodeString monthNames[] = {
151 * "Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
152 * ChoiceFormat fmt(limits, monthNames, 7);
155 * for (double x = 1.0; x <= 8.0; x += 1.0) {
156 * fmt.format(x, str);
157 * str.extract(0, str.length(), buf, 256, "");
159 * cout << x << " -> "
167 * <p><em>User subclasses are not supported.</em> While clients may write
168 * subclasses, such code will not necessarily work and will not be
169 * guaranteed to work stably from release to release.
171 * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments.
173 class U_I18N_API ChoiceFormat: public NumberFormat {
176 * Constructs a new ChoiceFormat from the pattern string.
178 * @param pattern Pattern used to construct object.
179 * @param status Output param to receive success code. If the
180 * pattern cannot be parsed, set to failure code.
181 * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments.
183 ChoiceFormat(const UnicodeString& pattern,
188 * Constructs a new ChoiceFormat with the given limits and message strings.
189 * All closure flags default to <code>FALSE</code>,
190 * equivalent to <code>less_than_or_equal</code> separators.
192 * Copies the limits and formats instead of adopting them.
194 * @param limits Array of limit values.
195 * @param formats Array of formats.
196 * @param count Size of 'limits' and 'formats' arrays.
197 * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments.
199 ChoiceFormat(const double* limits,
200 const UnicodeString* formats,
204 * Constructs a new ChoiceFormat with the given limits, closure flags and message strings.
206 * Copies the limits and formats instead of adopting them.
208 * @param limits Array of limit values
209 * @param closures Array of booleans specifying whether each
210 * element of 'limits' is open or closed. If FALSE, then the
211 * corresponding limit number is a member of its range.
212 * If TRUE, then the limit number belongs to the previous range it.
213 * @param formats Array of formats
214 * @param count Size of 'limits', 'closures', and 'formats' arrays
215 * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments.
217 ChoiceFormat(const double* limits,
218 const UBool* closures,
219 const UnicodeString* formats,
225 * @param that ChoiceFormat object to be copied from
226 * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments.
228 ChoiceFormat(const ChoiceFormat& that);
231 * Assignment operator.
233 * @param that ChoiceFormat object to be copied
234 * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments.
236 const ChoiceFormat& operator=(const ChoiceFormat& that);
240 * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments.
242 virtual ~ChoiceFormat();
245 * Clones this Format object. The caller owns the
246 * result and must delete it when done.
248 * @return a copy of this object
249 * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments.
251 virtual Format* clone(void) const;
254 * Returns true if the given Format objects are semantically equal.
255 * Objects of different subclasses are considered unequal.
257 * @param other ChoiceFormat object to be compared
258 * @return true if other is the same as this.
259 * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments.
261 virtual UBool operator==(const Format& other) const;
265 * @param pattern The pattern to be applied.
266 * @param status Output param set to success/failure code on
267 * exit. If the pattern is invalid, this will be
268 * set to a failure result.
269 * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments.
271 virtual void applyPattern(const UnicodeString& pattern,
276 * @param pattern The pattern to be applied.
277 * @param parseError Struct to receive information on position
278 * of error if an error is encountered
279 * @param status Output param set to success/failure code on
280 * exit. If the pattern is invalid, this will be
281 * set to a failure result.
282 * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments.
284 virtual void applyPattern(const UnicodeString& pattern,
285 UParseError& parseError,
290 * @param pattern Output param which will receive the pattern
291 * Previous contents are deleted.
292 * @return A reference to 'pattern'
293 * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments.
295 virtual UnicodeString& toPattern(UnicodeString &pattern) const;
298 * Sets the choices to be used in formatting.
299 * For details see the constructor with the same parameter list.
301 * @param limitsToCopy Contains the top value that you want
302 * parsed with that format,and should be in
303 * ascending sorted order. When formatting X,
304 * the choice will be the i, where limit[i]
305 * <= X < limit[i+1].
306 * @param formatsToCopy The format strings you want to use for each limit.
307 * @param count The size of the above arrays.
308 * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments.
310 virtual void setChoices(const double* limitsToCopy,
311 const UnicodeString* formatsToCopy,
315 * Sets the choices to be used in formatting.
316 * For details see the constructor with the same parameter list.
318 * @param limits Array of limits
319 * @param closures Array of limit booleans
320 * @param formats Array of format string
321 * @param count The size of the above arrays
322 * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments.
324 virtual void setChoices(const double* limits,
325 const UBool* closures,
326 const UnicodeString* formats,
330 * Returns NULL and 0.
331 * Before ICU 4.8, this used to return the choice limits array.
333 * @param count Will be set to 0.
335 * @deprecated ICU 4.8 Use the MessagePattern class to analyze a ChoiceFormat pattern.
337 virtual const double* getLimits(int32_t& count) const;
340 * Returns NULL and 0.
341 * Before ICU 4.8, this used to return the limit booleans array.
343 * @param count Will be set to 0.
345 * @deprecated ICU 4.8 Use the MessagePattern class to analyze a ChoiceFormat pattern.
347 virtual const UBool* getClosures(int32_t& count) const;
350 * Returns NULL and 0.
351 * Before ICU 4.8, this used to return the array of choice strings.
353 * @param count Will be set to 0.
355 * @deprecated ICU 4.8 Use the MessagePattern class to analyze a ChoiceFormat pattern.
357 virtual const UnicodeString* getFormats(int32_t& count) const;
360 using NumberFormat::format;
363 * Formats a double number using this object's choices.
365 * @param number The value to be formatted.
366 * @param appendTo Output parameter to receive result.
367 * Result is appended to existing contents.
368 * @param pos On input: an alignment field, if desired.
369 * On output: the offsets of the alignment field.
370 * @return Reference to 'appendTo' parameter.
371 * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments.
373 virtual UnicodeString& format(double number,
374 UnicodeString& appendTo,
375 FieldPosition& pos) const;
377 * Formats an int32_t number using this object's choices.
379 * @param number The value to be formatted.
380 * @param appendTo Output parameter to receive result.
381 * Result is appended to existing contents.
382 * @param pos On input: an alignment field, if desired.
383 * On output: the offsets of the alignment field.
384 * @return Reference to 'appendTo' parameter.
385 * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments.
387 virtual UnicodeString& format(int32_t number,
388 UnicodeString& appendTo,
389 FieldPosition& pos) const;
392 * Formats an int64_t number using this object's choices.
394 * @param number The value to be formatted.
395 * @param appendTo Output parameter to receive result.
396 * Result is appended to existing contents.
397 * @param pos On input: an alignment field, if desired.
398 * On output: the offsets of the alignment field.
399 * @return Reference to 'appendTo' parameter.
400 * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments.
402 virtual UnicodeString& format(int64_t number,
403 UnicodeString& appendTo,
404 FieldPosition& pos) const;
407 * Formats an array of objects using this object's choices.
409 * @param objs The array of objects to be formatted.
410 * @param cnt The size of objs.
411 * @param appendTo Output parameter to receive result.
412 * Result is appended to existing contents.
413 * @param pos On input: an alignment field, if desired.
414 * On output: the offsets of the alignment field.
415 * @param success Output param set to success/failure code on
417 * @return Reference to 'appendTo' parameter.
418 * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments.
420 virtual UnicodeString& format(const Formattable* objs,
422 UnicodeString& appendTo,
424 UErrorCode& success) const;
426 using NumberFormat::parse;
429 * Looks for the longest match of any message string on the input text and,
430 * if there is a match, sets the result object to the corresponding range's number.
432 * If no string matches, then the parsePosition is unchanged.
434 * @param text The text to be parsed.
435 * @param result Formattable to be set to the parse result.
436 * If parse fails, return contents are undefined.
437 * @param parsePosition The position to start parsing at on input.
438 * On output, moved to after the last successfully
439 * parse character. On parse failure, does not change.
440 * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments.
442 virtual void parse(const UnicodeString& text,
444 ParsePosition& parsePosition) const;
447 * Returns a unique class ID POLYMORPHICALLY. Part of ICU's "poor man's RTTI".
449 * @return The class ID for this object. All objects of a
450 * given class have the same class ID. Objects of
451 * other classes have different class IDs.
452 * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments.
454 virtual UClassID getDynamicClassID(void) const;
457 * Returns the class ID for this class. This is useful only for
458 * comparing to a return value from getDynamicClassID(). For example:
460 * . Base* polymorphic_pointer = createPolymorphicObject();
461 * . if (polymorphic_pointer->getDynamicClassID() ==
462 * . Derived::getStaticClassID()) ...
464 * @return The class ID for all objects of this class.
465 * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments.
467 static UClassID U_EXPORT2 getStaticClassID(void);
471 * Converts a double value to a string.
472 * @param value the double number to be converted.
473 * @param string the result string.
474 * @return the converted string.
476 static UnicodeString& dtos(double value, UnicodeString& string);
478 ChoiceFormat(); // default constructor not implemented
481 * Construct a new ChoiceFormat with the limits and the corresponding formats
482 * based on the pattern.
484 * @param newPattern Pattern used to construct object.
485 * @param parseError Struct to receive information on position
486 * of error if an error is encountered.
487 * @param status Output param to receive success code. If the
488 * pattern cannot be parsed, set to failure code.
490 ChoiceFormat(const UnicodeString& newPattern,
491 UParseError& parseError,
494 friend class MessageFormat;
496 virtual void setChoices(const double* limits,
497 const UBool* closures,
498 const UnicodeString* formats,
500 UErrorCode &errorCode);
503 * Finds the ChoiceFormat sub-message for the given number.
504 * @param pattern A MessagePattern.
505 * @param partIndex the index of the first ChoiceFormat argument style part.
506 * @param number a number to be mapped to one of the ChoiceFormat argument's intervals
507 * @return the sub-message start part index.
509 static int32_t findSubMessage(const MessagePattern &pattern, int32_t partIndex, double number);
511 static double parseArgument(
512 const MessagePattern &pattern, int32_t partIndex,
513 const UnicodeString &source, ParsePosition &pos);
516 * Matches the pattern string from the end of the partIndex to
517 * the beginning of the limitPartIndex,
518 * including all syntax except SKIP_SYNTAX,
519 * against the source string starting at sourceOffset.
520 * If they match, returns the length of the source string match.
521 * Otherwise returns -1.
523 static int32_t matchStringUntilLimitPart(
524 const MessagePattern &pattern, int32_t partIndex, int32_t limitPartIndex,
525 const UnicodeString &source, int32_t sourceOffset);
528 * Some of the ChoiceFormat constructors do not have a UErrorCode paramater.
529 * We need _some_ way to provide one for the MessagePattern constructor.
530 * Alternatively, the MessagePattern could be a pointer field, but that is
533 UErrorCode constructorErrorCode;
536 * The MessagePattern which contains the parsed structure of the pattern string.
538 * Starting with ICU 4.8, the MessagePattern contains a sequence of
539 * numeric/selector/message parts corresponding to the parsed pattern.
540 * For details see the MessagePattern class API docs.
542 MessagePattern msgPattern;
545 * Docs & fields from before ICU 4.8, before MessagePattern was used.
546 * Commented out, and left only for explanation of semantics.
548 * Each ChoiceFormat divides the range -Inf..+Inf into fCount
549 * intervals. The intervals are:
551 * 0: fChoiceLimits[0]..fChoiceLimits[1]
552 * 1: fChoiceLimits[1]..fChoiceLimits[2]
554 * fCount-2: fChoiceLimits[fCount-2]..fChoiceLimits[fCount-1]
555 * fCount-1: fChoiceLimits[fCount-1]..+Inf
557 * Interval 0 is special; during formatting (mapping numbers to
558 * strings), it also contains all numbers less than
559 * fChoiceLimits[0], as well as NaN values.
561 * Interval i maps to and from string fChoiceFormats[i]. When
562 * parsing (mapping strings to numbers), then intervals map to
563 * their lower limit, that is, interval i maps to fChoiceLimit[i].
565 * The intervals may be closed, half open, or open. This affects
566 * formatting but does not affect parsing. Interval i is affected
567 * by fClosures[i] and fClosures[i+1]. If fClosures[i]
568 * is FALSE, then the value fChoiceLimits[i] is in interval i.
569 * That is, intervals i and i are:
571 * i-1: ... x < fChoiceLimits[i]
572 * i: fChoiceLimits[i] <= x ...
574 * If fClosures[i] is TRUE, then the value fChoiceLimits[i] is
575 * in interval i-1. That is, intervals i-1 and i are:
577 * i-1: ... x <= fChoiceLimits[i]
578 * i: fChoiceLimits[i] < x ...
580 * Because of the nature of interval 0, fClosures[0] has no
583 // double* fChoiceLimits;
585 // UnicodeString* fChoiceFormats;
592 #endif // U_HIDE_DEPRECATED_API
593 #endif /* #if !UCONFIG_NO_FORMATTING */