#include <choicfmt.h>
Inheritance diagram for ChoiceFormat:
Public Methods | |
ChoiceFormat (const UnicodeString& newPattern, UErrorCode& status) | |
Construct a new ChoiceFormat with the limits and the corresponding formats based on the pattern. More... | |
ChoiceFormat (const double* limits, const UnicodeString* formats, int32_t count ) | |
Construct a new ChoiceFormat with the given limits and formats. More... | |
ChoiceFormat (const ChoiceFormat&) | |
Copy constructor. More... | |
const ChoiceFormat& | operator= (const ChoiceFormat&) |
Assignment operator. More... | |
virtual | ~ChoiceFormat () |
Destructor. More... | |
virtual Format* | clone (void) const |
Clone this Format object polymorphically. More... | |
virtual UBool | operator== (const Format& other) const |
Return true if the given Format objects are semantically equal. More... | |
virtual void | applyPattern (const UnicodeString& pattern, UErrorCode& status) |
Sets the pattern. More... | |
virtual UnicodeString& | toPattern (UnicodeString &pattern) const |
Gets the pattern. More... | |
virtual void | adoptChoices (double* limitsToAdopt, UnicodeString* formatsToAdopt, int32_t count ) |
Set the choices to be used in formatting. More... | |
virtual void | setChoices (const double* limitsToCopy, const UnicodeString* formatsToCopy, int32_t count ) |
Set the choices to be used in formatting. More... | |
virtual const double* | getLimits (int32_t& count) const |
Get the limits passed in the constructor. More... | |
virtual const UnicodeString* | getFormats (int32_t& count) const |
Get the formats passed in the constructor. More... | |
virtual UnicodeString& | format (double number, UnicodeString& toAppendTo, FieldPosition& pos) const |
Format a double or long number using this object's choices. More... | |
virtual UnicodeString& | format (int32_t number, UnicodeString& toAppendTo, FieldPosition& pos) const |
Format a int_32t number using this object's choices. More... | |
virtual UnicodeString& | format (const Formattable* objs, int32_t cnt, UnicodeString& toAppendTo, FieldPosition& pos, UErrorCode& success) const |
Format an array of objects using this object's choices. More... | |
virtual UnicodeString& | format (const Formattable& obj, UnicodeString& toAppendTo, FieldPosition& pos, UErrorCode& status) const |
Format an object using this object's choices. More... | |
UnicodeString& | format (const Formattable& obj, UnicodeString& result, UErrorCode& status) const |
Redeclared NumberFormat method. More... | |
UnicodeString& | format ( double number, UnicodeString& output) const |
Redeclared NumberFormat method. More... | |
UnicodeString& | format ( int32_t number, UnicodeString& output) const |
Redeclared NumberFormat method. More... | |
virtual void | parse (const UnicodeString& text, Formattable& result, ParsePosition& parsePosition) const |
Return a long if possible (e.g. More... | |
virtual void | parse (const UnicodeString& text, Formattable& result, UErrorCode& status) const |
Parse a string as a numeric value, and return a Formattable numeric object. More... | |
virtual UClassID | getDynamicClassID (void) const |
Returns a unique class ID POLYMORPHICALLY. More... | |
Static Public Methods | |
UClassID | getStaticClassID (void) |
Return the class ID for this class. More... | |
double | nextDouble (double d, UBool positive) |
double | nextDouble (double d ) |
Finds the least double greater than d. More... | |
double | previousDouble (double d ) |
Finds the greatest double less than d. More... | |
Private Attributes | |
double* | fChoiceLimits |
UnicodeString* | fChoiceFormats |
int32_t | fCount |
Static Private Methods | |
NumberFormat* | getNumberFormat (UErrorCode &status) |
void | releaseNumberFormat (NumberFormat *adopt) |
double | stod (const UnicodeString& string, UErrorCode& status) |
Converts a string to a double value using a default NumberFormat object which is static (shared by all ChoiceFormat instances). More... | |
UnicodeString& | dtos (double value, UnicodeString& string, UErrorCode& status) |
Converts a double value to a string using a default NumberFormat object which is static (shared by all ChoiceFormat instances). More... | |
Static Private Attributes | |
NumberFormat* | fgNumberFormat |
char | fgClassID |
It is generally used in a MessageFormat for doing things like plurals. The choice is specified with an ascending list of doubles, where each item specifies a half-open interval up to the next item:
If there is no match, then either the first or last index is used, depending on whether the number is too low or too high. The length of the array of formats must be the same as the length of the array of limits. For example,X matches j if and only if limit[j] <= X < limit[j+1]
(nextDouble can be used to get the next higher double, to make the half-open interval.){1,2,3,4,5,6,7}, {"Sun","Mon","Tue","Wed","Thur","Fri","Sat"} {0, 1, ChoiceFormat::nextDouble(1)}, {"no files", "one file", "many files"}
Here is a simple example that shows formatting and parsing:
Here is a more complex example, with a pattern format.void SimpleChoiceExample( void ) { double limits[] = {1,2,3,4,5,6,7}; UnicodeString monthNames[] = {"Sun","Mon","Tue","Wed","Thur","Fri","Sat"}; ChoiceFormat* form = new ChoiceFormat(limits, monthNames, 7 ); ParsePosition* status = new ParsePosition(0); UnicodeString str; FieldPosition f1(0), f2(0); for (double i = 0.0; i <= 8.0; ++i) { status->setIndex(0); Formattable parseResult; str.remove(); cout << i << " -> " << form->format(i,str, f1) << " -> " << parseResult << endl; } delete form; delete status; cout << endl; }
ChoiceFormat objects may be converted to and from patterns. The syntax of these patterns is [TODO fill in this section with detail]. Here is an example of a ChoiceFormat pattern:void ComplexChoiceExample( void ) { double filelimits[] = {0,1,2}; UnicodeString filepart[] = {"are no files","is one file","are {2} files"}; ChoiceFormat* fileform = new ChoiceFormat(filelimits, filepart, 3 ); UErrorCode success = U_ZERO_ERROR; const Format* testFormats[] = { fileform, NULL, NumberFormat::createInstance(success) }; MessageFormat* pattform = new MessageFormat("There {0} on {1}", success ); pattform->setFormats( testFormats, 3 ); Formattable testArgs[] = {0L, "Disk_A", 0L}; FieldPosition fp(0); UnicodeString str; for (int32_t i = 0; i < 4; ++i) { Formattable fInt(i); testArgs[0] = fInt; testArgs[2] = testArgs[0]; str.remove(); pattform->format(testArgs, 3, str, fp, success ); cout << "Output for i=" << i << " : " << str << endl; } delete pattform; cout << endl; }
You can either do this programmatically, as in the above example, or by using a pattern (see ChoiceFormat for more information) as in:
Here the notation is:"0#are no files|1#is one file|1<are many files"
Each limit value is followed by a string, which is terminated by a vertical bar character ("|"), except for the last string, which is terminated by the end of the string.<number> "#" Specifies a limit value. <number> "<" Specifies a limit of nextDouble(<number>). <number> ">" Specifies a limit of previousDouble(<number>).
Definition at line 133 of file choicfmt.h.
|
Construct a new ChoiceFormat with the limits and the corresponding formats based on the pattern.
|
|
Construct a new ChoiceFormat with the given limits and formats. Copy the limits and formats instead of adopting them.
|
|
Copy constructor.
|
|
Destructor.
|
|
Set the choices to be used in formatting. The arrays are adopted and should not be deleted by the caller.
|
|
Sets the pattern.
|
|
Clone this Format object polymorphically. The caller owns the result and should delete it when done.
Reimplemented from Format. |
|
Converts a double value to a string using a default NumberFormat object which is static (shared by all ChoiceFormat instances). @param value the double number to be converted with. @param string the result string. @param status error code. @return the converted string. |
|
Redeclared NumberFormat method.
Reimplemented from NumberFormat. Definition at line 467 of file choicfmt.h. |
|
Redeclared NumberFormat method.
Reimplemented from NumberFormat. Definition at line 461 of file choicfmt.h. |
|
Redeclared NumberFormat method.
Reimplemented from NumberFormat. Definition at line 452 of file choicfmt.h. |
|
Format an object using this object's choices.
Reimplemented from NumberFormat. |
|
Format an array of objects using this object's choices.
|
|
Format a int_32t number using this object's choices.
Reimplemented from NumberFormat. |
|
Format a double or long number using this object's choices.
Reimplemented from NumberFormat. |
|
Returns a unique class ID POLYMORPHICALLY. Pure virtual override. This method is to implement a simple version of RTTI, since not all C++ compilers support genuine RTTI. Polymorphic operator==() and clone() methods call this method.
Reimplemented from NumberFormat. Definition at line 436 of file choicfmt.h. |
|
Get the formats passed in the constructor.
|
|
Get the limits passed in the constructor.
|
|
|
|
Return the class ID for this class. This is useful only for comparing to a return value from getDynamicClassID(). For example: . Base* polymorphic_pointer = createPolymorphicObject(); . if (polymorphic_pointer->getDynamicClassID() == . Derived::getStaticClassID()) ...
Reimplemented from NumberFormat. Definition at line 374 of file choicfmt.h. |
|
Finds the least double greater than d. If NaN, returns same value. Used to make half-open intervals.
Definition at line 441 of file choicfmt.h. |
|
Referenced by nextDouble(), and previousDouble(). |
|
Assignment operator.
|
|
Return true if the given Format objects are semantically equal. Objects of different subclasses are considered unequal.
Reimplemented from NumberFormat. |
|
Parse a string as a numeric value, and return a Formattable numeric object. This method parses integers only if IntegerOnly is set.
Reimplemented from NumberFormat. |
|
Return a long if possible (e.g. within range LONG_MAX, LONG_MAX], and with no decimals), otherwise a double. If IntegerOnly is set, will stop at a decimal point (or equivalent; e.g. for rational numbers "1 2/3", will stop after the 1). If no object can be parsed, parsePosition is unchanged, and NULL is returned.
Reimplemented from NumberFormat. |
|
Finds the greatest double less than d. If NaN, returns same value.
Definition at line 446 of file choicfmt.h. |
|
|
|
Set the choices to be used in formatting.
|
|
Converts a string to a double value using a default NumberFormat object which is static (shared by all ChoiceFormat instances).
|
|
Gets the pattern.
|
|
Definition at line 431 of file choicfmt.h. |
|
Definition at line 430 of file choicfmt.h. |
|
Definition at line 432 of file choicfmt.h. |
|
Reimplemented from NumberFormat. Definition at line 428 of file choicfmt.h. |
|
Definition at line 427 of file choicfmt.h. |