michael@0: /* michael@0: ******************************************************************************** michael@0: * Copyright (C) 1997-2013, International Business Machines michael@0: * Corporation and others. All Rights Reserved. michael@0: ******************************************************************************** michael@0: * michael@0: * File CHOICFMT.H michael@0: * michael@0: * Modification History: michael@0: * michael@0: * Date Name Description michael@0: * 02/19/97 aliu Converted from java. michael@0: * 03/20/97 helena Finished first cut of implementation and got rid michael@0: * of nextDouble/previousDouble and replaced with michael@0: * boolean array. michael@0: * 4/10/97 aliu Clean up. Modified to work on AIX. michael@0: * 8/6/97 nos Removed overloaded constructor, member var 'buffer'. michael@0: * 07/22/98 stephen Removed operator!= (implemented in Format) michael@0: ******************************************************************************** michael@0: */ michael@0: michael@0: #ifndef CHOICFMT_H michael@0: #define CHOICFMT_H michael@0: michael@0: #include "unicode/utypes.h" michael@0: michael@0: /** michael@0: * \file michael@0: * \brief C++ API: Choice Format. michael@0: */ michael@0: michael@0: #if !UCONFIG_NO_FORMATTING michael@0: #ifndef U_HIDE_DEPRECATED_API michael@0: michael@0: #include "unicode/fieldpos.h" michael@0: #include "unicode/format.h" michael@0: #include "unicode/messagepattern.h" michael@0: #include "unicode/numfmt.h" michael@0: #include "unicode/unistr.h" michael@0: michael@0: U_NAMESPACE_BEGIN michael@0: michael@0: class MessageFormat; michael@0: michael@0: /** michael@0: * ChoiceFormat converts between ranges of numeric values and strings for those ranges. michael@0: * The strings must conform to the MessageFormat pattern syntax. michael@0: * michael@0: *
ChoiceFormat
is probably not what you need.
michael@0: * Please use MessageFormat
michael@0: * with plural
arguments for proper plural selection,
michael@0: * and select
arguments for simple selection among a fixed set of choices!
A ChoiceFormat
splits
michael@0: * the real number line \htmlonly-∞
to
michael@0: * +∞
\endhtmlonly into two
michael@0: * or more contiguous ranges. Each range is mapped to a
michael@0: * string.
ChoiceFormat
was originally intended
michael@0: * for displaying grammatically correct
michael@0: * plurals such as "There is one file." vs. "There are 2 files."
michael@0: * However, plural rules for many languages
michael@0: * are too complex for the capabilities of ChoiceFormat,
michael@0: * and its requirement of specifying the precise rules for each message
michael@0: * is unmanageable for translators.
There are two methods of defining a ChoiceFormat
; both
michael@0: * are equivalent. The first is by using a string pattern. This is the
michael@0: * preferred method in most cases. The second method is through direct
michael@0: * specification of the arrays that logically make up the
michael@0: * ChoiceFormat
.
Note: Typically, choice formatting is done (if done at all) via MessageFormat
michael@0: * with a choice
argument type,
michael@0: * rather than using a stand-alone ChoiceFormat
.
The pattern string defines the range boundaries and the strings for each number range. michael@0: * Syntax: michael@0: *
michael@0: * choiceStyle = number separator message ('|' number separator message)* michael@0: * number = normal_number | ['-'] \htmlonly∞\endhtmlonly (U+221E, infinity) michael@0: * normal_number = double value (unlocalized ASCII string) michael@0: * separator = less_than | less_than_or_equal michael@0: * less_than = '<' michael@0: * less_than_or_equal = '#' | \htmlonly≤\endhtmlonly (U+2264) michael@0: * message: see {@link MessageFormat} michael@0: *michael@0: * Pattern_White_Space between syntax elements is ignored, except michael@0: * around each range's sub-message. michael@0: * michael@0: *
Each numeric sub-range extends from the current range's number
michael@0: * to the next range's number.
michael@0: * The number itself is included in its range if a less_than_or_equal
sign is used,
michael@0: * and excluded from its range (and instead included in the previous range)
michael@0: * if a less_than
sign is used.
When a ChoiceFormat
is constructed from
michael@0: * arrays of numbers, closure flags and strings,
michael@0: * they are interpreted just like
michael@0: * the sequence of (number separator string)
in an equivalent pattern string.
michael@0: * closure[i]==TRUE
corresponds to a less_than
separator sign.
michael@0: * The equivalent pattern string will be constructed automatically.
During formatting, a number is mapped to the first range michael@0: * where the number is not greater than the range's upper limit. michael@0: * That range's message string is returned. A NaN maps to the very first range.
michael@0: * michael@0: *During parsing, a range is selected for the longest match of michael@0: * any range's message. That range's number is returned, ignoring the separator/closure. michael@0: * Only a simple string match is performed, without parsing of arguments that michael@0: * might be specified in the message strings.
michael@0: * michael@0: *Note that the first range's number is ignored in formatting michael@0: * but may be returned from parsing.
michael@0: * michael@0: *Here is an example of two arrays that map the number
michael@0: * 1..7
to the English day of the week abbreviations
michael@0: * Sun..Sat
. No closures array is given; this is the same as
michael@0: * specifying all closures to be FALSE
.
{1,2,3,4,5,6,7}, michael@0: * {"Sun","Mon","Tue","Wed","Thur","Fri","Sat"}michael@0: * michael@0: *
Here is an example that maps the ranges [-Inf, 1), [1, 1], and (1, michael@0: * +Inf] to three strings. That is, the number line is split into three michael@0: * ranges: x < 1.0, x = 1.0, and x > 1.0. michael@0: * (The round parentheses in the notation above indicate an exclusive boundary, michael@0: * like the turned bracket in European notation: [-Inf, 1) == [-Inf, 1[ )
michael@0: * michael@0: *{0, 1, 1}, michael@0: * {FALSE, FALSE, TRUE}, michael@0: * {"no files", "one file", "many files"}michael@0: * michael@0: *
Here is an example that shows formatting and parsing:
michael@0: * michael@0: * \code michael@0: * #includeUser subclasses are not supported. While clients may write
michael@0: * subclasses, such code will not necessarily work and will not be
michael@0: * guaranteed to work stably from release to release.
michael@0: *
michael@0: * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments.
michael@0: */
michael@0: class U_I18N_API ChoiceFormat: public NumberFormat {
michael@0: public:
michael@0: /**
michael@0: * Constructs a new ChoiceFormat from the pattern string.
michael@0: *
michael@0: * @param pattern Pattern used to construct object.
michael@0: * @param status Output param to receive success code. If the
michael@0: * pattern cannot be parsed, set to failure code.
michael@0: * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments.
michael@0: */
michael@0: ChoiceFormat(const UnicodeString& pattern,
michael@0: UErrorCode& status);
michael@0:
michael@0:
michael@0: /**
michael@0: * Constructs a new ChoiceFormat with the given limits and message strings.
michael@0: * All closure flags default to FALSE
,
michael@0: * equivalent to less_than_or_equal
separators.
michael@0: *
michael@0: * Copies the limits and formats instead of adopting them.
michael@0: *
michael@0: * @param limits Array of limit values.
michael@0: * @param formats Array of formats.
michael@0: * @param count Size of 'limits' and 'formats' arrays.
michael@0: * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments.
michael@0: */
michael@0: ChoiceFormat(const double* limits,
michael@0: const UnicodeString* formats,
michael@0: int32_t count );
michael@0:
michael@0: /**
michael@0: * Constructs a new ChoiceFormat with the given limits, closure flags and message strings.
michael@0: *
michael@0: * Copies the limits and formats instead of adopting them.
michael@0: *
michael@0: * @param limits Array of limit values
michael@0: * @param closures Array of booleans specifying whether each
michael@0: * element of 'limits' is open or closed. If FALSE, then the
michael@0: * corresponding limit number is a member of its range.
michael@0: * If TRUE, then the limit number belongs to the previous range it.
michael@0: * @param formats Array of formats
michael@0: * @param count Size of 'limits', 'closures', and 'formats' arrays
michael@0: * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments.
michael@0: */
michael@0: ChoiceFormat(const double* limits,
michael@0: const UBool* closures,
michael@0: const UnicodeString* formats,
michael@0: int32_t count);
michael@0:
michael@0: /**
michael@0: * Copy constructor.
michael@0: *
michael@0: * @param that ChoiceFormat object to be copied from
michael@0: * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments.
michael@0: */
michael@0: ChoiceFormat(const ChoiceFormat& that);
michael@0:
michael@0: /**
michael@0: * Assignment operator.
michael@0: *
michael@0: * @param that ChoiceFormat object to be copied
michael@0: * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments.
michael@0: */
michael@0: const ChoiceFormat& operator=(const ChoiceFormat& that);
michael@0:
michael@0: /**
michael@0: * Destructor.
michael@0: * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments.
michael@0: */
michael@0: virtual ~ChoiceFormat();
michael@0:
michael@0: /**
michael@0: * Clones this Format object. The caller owns the
michael@0: * result and must delete it when done.
michael@0: *
michael@0: * @return a copy of this object
michael@0: * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments.
michael@0: */
michael@0: virtual Format* clone(void) const;
michael@0:
michael@0: /**
michael@0: * Returns true if the given Format objects are semantically equal.
michael@0: * Objects of different subclasses are considered unequal.
michael@0: *
michael@0: * @param other ChoiceFormat object to be compared
michael@0: * @return true if other is the same as this.
michael@0: * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments.
michael@0: */
michael@0: virtual UBool operator==(const Format& other) const;
michael@0:
michael@0: /**
michael@0: * Sets the pattern.
michael@0: * @param pattern The pattern to be applied.
michael@0: * @param status Output param set to success/failure code on
michael@0: * exit. If the pattern is invalid, this will be
michael@0: * set to a failure result.
michael@0: * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments.
michael@0: */
michael@0: virtual void applyPattern(const UnicodeString& pattern,
michael@0: UErrorCode& status);
michael@0:
michael@0: /**
michael@0: * Sets the pattern.
michael@0: * @param pattern The pattern to be applied.
michael@0: * @param parseError Struct to receive information on position
michael@0: * of error if an error is encountered
michael@0: * @param status Output param set to success/failure code on
michael@0: * exit. If the pattern is invalid, this will be
michael@0: * set to a failure result.
michael@0: * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments.
michael@0: */
michael@0: virtual void applyPattern(const UnicodeString& pattern,
michael@0: UParseError& parseError,
michael@0: UErrorCode& status);
michael@0: /**
michael@0: * Gets the pattern.
michael@0: *
michael@0: * @param pattern Output param which will receive the pattern
michael@0: * Previous contents are deleted.
michael@0: * @return A reference to 'pattern'
michael@0: * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments.
michael@0: */
michael@0: virtual UnicodeString& toPattern(UnicodeString &pattern) const;
michael@0:
michael@0: /**
michael@0: * Sets the choices to be used in formatting.
michael@0: * For details see the constructor with the same parameter list.
michael@0: *
michael@0: * @param limitsToCopy Contains the top value that you want
michael@0: * parsed with that format,and should be in
michael@0: * ascending sorted order. When formatting X,
michael@0: * the choice will be the i, where limit[i]
michael@0: * <= X < limit[i+1].
michael@0: * @param formatsToCopy The format strings you want to use for each limit.
michael@0: * @param count The size of the above arrays.
michael@0: * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments.
michael@0: */
michael@0: virtual void setChoices(const double* limitsToCopy,
michael@0: const UnicodeString* formatsToCopy,
michael@0: int32_t count );
michael@0:
michael@0: /**
michael@0: * Sets the choices to be used in formatting.
michael@0: * For details see the constructor with the same parameter list.
michael@0: *
michael@0: * @param limits Array of limits
michael@0: * @param closures Array of limit booleans
michael@0: * @param formats Array of format string
michael@0: * @param count The size of the above arrays
michael@0: * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments.
michael@0: */
michael@0: virtual void setChoices(const double* limits,
michael@0: const UBool* closures,
michael@0: const UnicodeString* formats,
michael@0: int32_t count);
michael@0:
michael@0: /**
michael@0: * Returns NULL and 0.
michael@0: * Before ICU 4.8, this used to return the choice limits array.
michael@0: *
michael@0: * @param count Will be set to 0.
michael@0: * @return NULL
michael@0: * @deprecated ICU 4.8 Use the MessagePattern class to analyze a ChoiceFormat pattern.
michael@0: */
michael@0: virtual const double* getLimits(int32_t& count) const;
michael@0:
michael@0: /**
michael@0: * Returns NULL and 0.
michael@0: * Before ICU 4.8, this used to return the limit booleans array.
michael@0: *
michael@0: * @param count Will be set to 0.
michael@0: * @return NULL
michael@0: * @deprecated ICU 4.8 Use the MessagePattern class to analyze a ChoiceFormat pattern.
michael@0: */
michael@0: virtual const UBool* getClosures(int32_t& count) const;
michael@0:
michael@0: /**
michael@0: * Returns NULL and 0.
michael@0: * Before ICU 4.8, this used to return the array of choice strings.
michael@0: *
michael@0: * @param count Will be set to 0.
michael@0: * @return NULL
michael@0: * @deprecated ICU 4.8 Use the MessagePattern class to analyze a ChoiceFormat pattern.
michael@0: */
michael@0: virtual const UnicodeString* getFormats(int32_t& count) const;
michael@0:
michael@0:
michael@0: using NumberFormat::format;
michael@0:
michael@0: /**
michael@0: * Formats a double number using this object's choices.
michael@0: *
michael@0: * @param number The value to be formatted.
michael@0: * @param appendTo Output parameter to receive result.
michael@0: * Result is appended to existing contents.
michael@0: * @param pos On input: an alignment field, if desired.
michael@0: * On output: the offsets of the alignment field.
michael@0: * @return Reference to 'appendTo' parameter.
michael@0: * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments.
michael@0: */
michael@0: virtual UnicodeString& format(double number,
michael@0: UnicodeString& appendTo,
michael@0: FieldPosition& pos) const;
michael@0: /**
michael@0: * Formats an int32_t number using this object's choices.
michael@0: *
michael@0: * @param number The value to be formatted.
michael@0: * @param appendTo Output parameter to receive result.
michael@0: * Result is appended to existing contents.
michael@0: * @param pos On input: an alignment field, if desired.
michael@0: * On output: the offsets of the alignment field.
michael@0: * @return Reference to 'appendTo' parameter.
michael@0: * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments.
michael@0: */
michael@0: virtual UnicodeString& format(int32_t number,
michael@0: UnicodeString& appendTo,
michael@0: FieldPosition& pos) const;
michael@0:
michael@0: /**
michael@0: * Formats an int64_t number using this object's choices.
michael@0: *
michael@0: * @param number The value to be formatted.
michael@0: * @param appendTo Output parameter to receive result.
michael@0: * Result is appended to existing contents.
michael@0: * @param pos On input: an alignment field, if desired.
michael@0: * On output: the offsets of the alignment field.
michael@0: * @return Reference to 'appendTo' parameter.
michael@0: * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments.
michael@0: */
michael@0: virtual UnicodeString& format(int64_t number,
michael@0: UnicodeString& appendTo,
michael@0: FieldPosition& pos) const;
michael@0:
michael@0: /**
michael@0: * Formats an array of objects using this object's choices.
michael@0: *
michael@0: * @param objs The array of objects to be formatted.
michael@0: * @param cnt The size of objs.
michael@0: * @param appendTo Output parameter to receive result.
michael@0: * Result is appended to existing contents.
michael@0: * @param pos On input: an alignment field, if desired.
michael@0: * On output: the offsets of the alignment field.
michael@0: * @param success Output param set to success/failure code on
michael@0: * exit.
michael@0: * @return Reference to 'appendTo' parameter.
michael@0: * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments.
michael@0: */
michael@0: virtual UnicodeString& format(const Formattable* objs,
michael@0: int32_t cnt,
michael@0: UnicodeString& appendTo,
michael@0: FieldPosition& pos,
michael@0: UErrorCode& success) const;
michael@0:
michael@0: using NumberFormat::parse;
michael@0:
michael@0: /**
michael@0: * Looks for the longest match of any message string on the input text and,
michael@0: * if there is a match, sets the result object to the corresponding range's number.
michael@0: *
michael@0: * If no string matches, then the parsePosition is unchanged.
michael@0: *
michael@0: * @param text The text to be parsed.
michael@0: * @param result Formattable to be set to the parse result.
michael@0: * If parse fails, return contents are undefined.
michael@0: * @param parsePosition The position to start parsing at on input.
michael@0: * On output, moved to after the last successfully
michael@0: * parse character. On parse failure, does not change.
michael@0: * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments.
michael@0: */
michael@0: virtual void parse(const UnicodeString& text,
michael@0: Formattable& result,
michael@0: ParsePosition& parsePosition) const;
michael@0:
michael@0: /**
michael@0: * Returns a unique class ID POLYMORPHICALLY. Part of ICU's "poor man's RTTI".
michael@0: *
michael@0: * @return The class ID for this object. All objects of a
michael@0: * given class have the same class ID. Objects of
michael@0: * other classes have different class IDs.
michael@0: * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments.
michael@0: */
michael@0: virtual UClassID getDynamicClassID(void) const;
michael@0:
michael@0: /**
michael@0: * Returns the class ID for this class. This is useful only for
michael@0: * comparing to a return value from getDynamicClassID(). For example:
michael@0: *
michael@0: * . Base* polymorphic_pointer = createPolymorphicObject(); michael@0: * . if (polymorphic_pointer->getDynamicClassID() == michael@0: * . Derived::getStaticClassID()) ... michael@0: *michael@0: * @return The class ID for all objects of this class. michael@0: * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments. michael@0: */ michael@0: static UClassID U_EXPORT2 getStaticClassID(void); michael@0: michael@0: private: michael@0: /** michael@0: * Converts a double value to a string. michael@0: * @param value the double number to be converted. michael@0: * @param string the result string. michael@0: * @return the converted string. michael@0: */ michael@0: static UnicodeString& dtos(double value, UnicodeString& string); michael@0: michael@0: ChoiceFormat(); // default constructor not implemented michael@0: michael@0: /** michael@0: * Construct a new ChoiceFormat with the limits and the corresponding formats michael@0: * based on the pattern. michael@0: * michael@0: * @param newPattern Pattern used to construct object. michael@0: * @param parseError Struct to receive information on position michael@0: * of error if an error is encountered. michael@0: * @param status Output param to receive success code. If the michael@0: * pattern cannot be parsed, set to failure code. michael@0: */ michael@0: ChoiceFormat(const UnicodeString& newPattern, michael@0: UParseError& parseError, michael@0: UErrorCode& status); michael@0: michael@0: friend class MessageFormat; michael@0: michael@0: virtual void setChoices(const double* limits, michael@0: const UBool* closures, michael@0: const UnicodeString* formats, michael@0: int32_t count, michael@0: UErrorCode &errorCode); michael@0: michael@0: /** michael@0: * Finds the ChoiceFormat sub-message for the given number. michael@0: * @param pattern A MessagePattern. michael@0: * @param partIndex the index of the first ChoiceFormat argument style part. michael@0: * @param number a number to be mapped to one of the ChoiceFormat argument's intervals michael@0: * @return the sub-message start part index. michael@0: */ michael@0: static int32_t findSubMessage(const MessagePattern &pattern, int32_t partIndex, double number); michael@0: michael@0: static double parseArgument( michael@0: const MessagePattern &pattern, int32_t partIndex, michael@0: const UnicodeString &source, ParsePosition &pos); michael@0: michael@0: /** michael@0: * Matches the pattern string from the end of the partIndex to michael@0: * the beginning of the limitPartIndex, michael@0: * including all syntax except SKIP_SYNTAX, michael@0: * against the source string starting at sourceOffset. michael@0: * If they match, returns the length of the source string match. michael@0: * Otherwise returns -1. michael@0: */ michael@0: static int32_t matchStringUntilLimitPart( michael@0: const MessagePattern &pattern, int32_t partIndex, int32_t limitPartIndex, michael@0: const UnicodeString &source, int32_t sourceOffset); michael@0: michael@0: /** michael@0: * Some of the ChoiceFormat constructors do not have a UErrorCode paramater. michael@0: * We need _some_ way to provide one for the MessagePattern constructor. michael@0: * Alternatively, the MessagePattern could be a pointer field, but that is michael@0: * not nice either. michael@0: */ michael@0: UErrorCode constructorErrorCode; michael@0: michael@0: /** michael@0: * The MessagePattern which contains the parsed structure of the pattern string. michael@0: * michael@0: * Starting with ICU 4.8, the MessagePattern contains a sequence of michael@0: * numeric/selector/message parts corresponding to the parsed pattern. michael@0: * For details see the MessagePattern class API docs. michael@0: */ michael@0: MessagePattern msgPattern; michael@0: michael@0: /** michael@0: * Docs & fields from before ICU 4.8, before MessagePattern was used. michael@0: * Commented out, and left only for explanation of semantics. michael@0: * -------- michael@0: * Each ChoiceFormat divides the range -Inf..+Inf into fCount michael@0: * intervals. The intervals are: michael@0: * michael@0: * 0: fChoiceLimits[0]..fChoiceLimits[1] michael@0: * 1: fChoiceLimits[1]..fChoiceLimits[2] michael@0: * ... michael@0: * fCount-2: fChoiceLimits[fCount-2]..fChoiceLimits[fCount-1] michael@0: * fCount-1: fChoiceLimits[fCount-1]..+Inf michael@0: * michael@0: * Interval 0 is special; during formatting (mapping numbers to michael@0: * strings), it also contains all numbers less than michael@0: * fChoiceLimits[0], as well as NaN values. michael@0: * michael@0: * Interval i maps to and from string fChoiceFormats[i]. When michael@0: * parsing (mapping strings to numbers), then intervals map to michael@0: * their lower limit, that is, interval i maps to fChoiceLimit[i]. michael@0: * michael@0: * The intervals may be closed, half open, or open. This affects michael@0: * formatting but does not affect parsing. Interval i is affected michael@0: * by fClosures[i] and fClosures[i+1]. If fClosures[i] michael@0: * is FALSE, then the value fChoiceLimits[i] is in interval i. michael@0: * That is, intervals i and i are: michael@0: * michael@0: * i-1: ... x < fChoiceLimits[i] michael@0: * i: fChoiceLimits[i] <= x ... michael@0: * michael@0: * If fClosures[i] is TRUE, then the value fChoiceLimits[i] is michael@0: * in interval i-1. That is, intervals i-1 and i are: michael@0: * michael@0: * i-1: ... x <= fChoiceLimits[i] michael@0: * i: fChoiceLimits[i] < x ... michael@0: * michael@0: * Because of the nature of interval 0, fClosures[0] has no michael@0: * effect. michael@0: */ michael@0: // double* fChoiceLimits; michael@0: // UBool* fClosures; michael@0: // UnicodeString* fChoiceFormats; michael@0: // int32_t fCount; michael@0: }; michael@0: michael@0: michael@0: U_NAMESPACE_END michael@0: michael@0: #endif // U_HIDE_DEPRECATED_API michael@0: #endif /* #if !UCONFIG_NO_FORMATTING */ michael@0: michael@0: #endif // CHOICFMT_H michael@0: //eof