|
||||
Warning, file /include/unicode/choicfmt.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
0001 // © 2016 and later: Unicode, Inc. and others. 0002 // License & terms of use: http://www.unicode.org/copyright.html 0003 /* 0004 ******************************************************************************** 0005 * Copyright (C) 1997-2013, International Business Machines 0006 * Corporation and others. All Rights Reserved. 0007 ******************************************************************************** 0008 * 0009 * File CHOICFMT.H 0010 * 0011 * Modification History: 0012 * 0013 * Date Name Description 0014 * 02/19/97 aliu Converted from java. 0015 * 03/20/97 helena Finished first cut of implementation and got rid 0016 * of nextDouble/previousDouble and replaced with 0017 * boolean array. 0018 * 4/10/97 aliu Clean up. Modified to work on AIX. 0019 * 8/6/97 nos Removed overloaded constructor, member var 'buffer'. 0020 * 07/22/98 stephen Removed operator!= (implemented in Format) 0021 ******************************************************************************** 0022 */ 0023 0024 #ifndef CHOICFMT_H 0025 #define CHOICFMT_H 0026 0027 #include "unicode/utypes.h" 0028 0029 #if U_SHOW_CPLUSPLUS_API 0030 0031 /** 0032 * \file 0033 * \brief C++ API: Choice Format. 0034 */ 0035 0036 #if !UCONFIG_NO_FORMATTING 0037 0038 #include "unicode/fieldpos.h" 0039 #include "unicode/format.h" 0040 #include "unicode/messagepattern.h" 0041 #include "unicode/numfmt.h" 0042 #include "unicode/unistr.h" 0043 0044 #ifndef U_HIDE_DEPRECATED_API 0045 0046 U_NAMESPACE_BEGIN 0047 0048 class MessageFormat; 0049 0050 /** 0051 * ChoiceFormat converts between ranges of numeric values and strings for those ranges. 0052 * The strings must conform to the MessageFormat pattern syntax. 0053 * 0054 * <p><em><code>ChoiceFormat</code> is probably not what you need. 0055 * Please use <code>MessageFormat</code> 0056 * with <code>plural</code> arguments for proper plural selection, 0057 * and <code>select</code> arguments for simple selection among a fixed set of choices!</em></p> 0058 * 0059 * <p>A <code>ChoiceFormat</code> splits 0060 * the real number line \htmlonly<code>-∞</code> to 0061 * <code>+∞</code>\endhtmlonly into two 0062 * or more contiguous ranges. Each range is mapped to a 0063 * string.</p> 0064 * 0065 * <p><code>ChoiceFormat</code> was originally intended 0066 * for displaying grammatically correct 0067 * plurals such as "There is one file." vs. "There are 2 files." 0068 * <em>However,</em> plural rules for many languages 0069 * are too complex for the capabilities of ChoiceFormat, 0070 * and its requirement of specifying the precise rules for each message 0071 * is unmanageable for translators.</p> 0072 * 0073 * <p>There are two methods of defining a <code>ChoiceFormat</code>; both 0074 * are equivalent. The first is by using a string pattern. This is the 0075 * preferred method in most cases. The second method is through direct 0076 * specification of the arrays that logically make up the 0077 * <code>ChoiceFormat</code>.</p> 0078 * 0079 * <p>Note: Typically, choice formatting is done (if done at all) via <code>MessageFormat</code> 0080 * with a <code>choice</code> argument type, 0081 * rather than using a stand-alone <code>ChoiceFormat</code>.</p> 0082 * 0083 * <h5>Patterns and Their Interpretation</h5> 0084 * 0085 * <p>The pattern string defines the range boundaries and the strings for each number range. 0086 * Syntax: 0087 * <pre> 0088 * choiceStyle = number separator message ('|' number separator message)* 0089 * number = normal_number | ['-'] \htmlonly∞\endhtmlonly (U+221E, infinity) 0090 * normal_number = double value (unlocalized ASCII string) 0091 * separator = less_than | less_than_or_equal 0092 * less_than = '<' 0093 * less_than_or_equal = '#' | \htmlonly≤\endhtmlonly (U+2264) 0094 * message: see {@link MessageFormat} 0095 * </pre> 0096 * Pattern_White_Space between syntax elements is ignored, except 0097 * around each range's sub-message.</p> 0098 * 0099 * <p>Each numeric sub-range extends from the current range's number 0100 * to the next range's number. 0101 * The number itself is included in its range if a <code>less_than_or_equal</code> sign is used, 0102 * and excluded from its range (and instead included in the previous range) 0103 * if a <code>less_than</code> sign is used.</p> 0104 * 0105 * <p>When a <code>ChoiceFormat</code> is constructed from 0106 * arrays of numbers, closure flags and strings, 0107 * they are interpreted just like 0108 * the sequence of <code>(number separator string)</code> in an equivalent pattern string. 0109 * <code>closure[i]==true</code> corresponds to a <code>less_than</code> separator sign. 0110 * The equivalent pattern string will be constructed automatically.</p> 0111 * 0112 * <p>During formatting, a number is mapped to the first range 0113 * where the number is not greater than the range's upper limit. 0114 * That range's message string is returned. A NaN maps to the very first range.</p> 0115 * 0116 * <p>During parsing, a range is selected for the longest match of 0117 * any range's message. That range's number is returned, ignoring the separator/closure. 0118 * Only a simple string match is performed, without parsing of arguments that 0119 * might be specified in the message strings.</p> 0120 * 0121 * <p>Note that the first range's number is ignored in formatting 0122 * but may be returned from parsing.</p> 0123 * 0124 * <h5>Examples</h5> 0125 * 0126 * <p>Here is an example of two arrays that map the number 0127 * <code>1..7</code> to the English day of the week abbreviations 0128 * <code>Sun..Sat</code>. No closures array is given; this is the same as 0129 * specifying all closures to be <code>false</code>.</p> 0130 * 0131 * <pre> {1,2,3,4,5,6,7}, 0132 * {"Sun","Mon","Tue","Wed","Thur","Fri","Sat"}</pre> 0133 * 0134 * <p>Here is an example that maps the ranges [-Inf, 1), [1, 1], and (1, 0135 * +Inf] to three strings. That is, the number line is split into three 0136 * ranges: x < 1.0, x = 1.0, and x > 1.0. 0137 * (The round parentheses in the notation above indicate an exclusive boundary, 0138 * like the turned bracket in European notation: [-Inf, 1) == [-Inf, 1[ )</p> 0139 * 0140 * <pre> {0, 1, 1}, 0141 * {false, false, true}, 0142 * {"no files", "one file", "many files"}</pre> 0143 * 0144 * <p>Here is an example that shows formatting and parsing: </p> 0145 * 0146 * \code 0147 * #include <unicode/choicfmt.h> 0148 * #include <unicode/unistr.h> 0149 * #include <iostream.h> 0150 * 0151 * int main(int argc, char *argv[]) { 0152 * double limits[] = {1,2,3,4,5,6,7}; 0153 * UnicodeString monthNames[] = { 0154 * "Sun","Mon","Tue","Wed","Thu","Fri","Sat"}; 0155 * ChoiceFormat fmt(limits, monthNames, 7); 0156 * UnicodeString str; 0157 * char buf[256]; 0158 * for (double x = 1.0; x <= 8.0; x += 1.0) { 0159 * fmt.format(x, str); 0160 * str.extract(0, str.length(), buf, 256, ""); 0161 * str.truncate(0); 0162 * cout << x << " -> " 0163 * << buf << endl; 0164 * } 0165 * cout << endl; 0166 * return 0; 0167 * } 0168 * \endcode 0169 * 0170 * <p><em>User subclasses are not supported.</em> While clients may write 0171 * subclasses, such code will not necessarily work and will not be 0172 * guaranteed to work stably from release to release. 0173 * 0174 * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments. 0175 */ 0176 class U_I18N_API ChoiceFormat: public NumberFormat { 0177 public: 0178 /** 0179 * Constructs a new ChoiceFormat from the pattern string. 0180 * 0181 * @param pattern Pattern used to construct object. 0182 * @param status Output param to receive success code. If the 0183 * pattern cannot be parsed, set to failure code. 0184 * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments. 0185 */ 0186 ChoiceFormat(const UnicodeString& pattern, 0187 UErrorCode& status); 0188 0189 0190 /** 0191 * Constructs a new ChoiceFormat with the given limits and message strings. 0192 * All closure flags default to <code>false</code>, 0193 * equivalent to <code>less_than_or_equal</code> separators. 0194 * 0195 * Copies the limits and formats instead of adopting them. 0196 * 0197 * @param limits Array of limit values. 0198 * @param formats Array of formats. 0199 * @param count Size of 'limits' and 'formats' arrays. 0200 * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments. 0201 */ 0202 ChoiceFormat(const double* limits, 0203 const UnicodeString* formats, 0204 int32_t count ); 0205 0206 /** 0207 * Constructs a new ChoiceFormat with the given limits, closure flags and message strings. 0208 * 0209 * Copies the limits and formats instead of adopting them. 0210 * 0211 * @param limits Array of limit values 0212 * @param closures Array of booleans specifying whether each 0213 * element of 'limits' is open or closed. If false, then the 0214 * corresponding limit number is a member of its range. 0215 * If true, then the limit number belongs to the previous range it. 0216 * @param formats Array of formats 0217 * @param count Size of 'limits', 'closures', and 'formats' arrays 0218 * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments. 0219 */ 0220 ChoiceFormat(const double* limits, 0221 const UBool* closures, 0222 const UnicodeString* formats, 0223 int32_t count); 0224 0225 /** 0226 * Copy constructor. 0227 * 0228 * @param that ChoiceFormat object to be copied from 0229 * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments. 0230 */ 0231 ChoiceFormat(const ChoiceFormat& that); 0232 0233 /** 0234 * Assignment operator. 0235 * 0236 * @param that ChoiceFormat object to be copied 0237 * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments. 0238 */ 0239 const ChoiceFormat& operator=(const ChoiceFormat& that); 0240 0241 /** 0242 * Destructor. 0243 * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments. 0244 */ 0245 virtual ~ChoiceFormat(); 0246 0247 /** 0248 * Clones this Format object. The caller owns the 0249 * result and must delete it when done. 0250 * 0251 * @return a copy of this object 0252 * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments. 0253 */ 0254 virtual ChoiceFormat* clone() const override; 0255 0256 /** 0257 * Returns true if the given Format objects are semantically equal. 0258 * Objects of different subclasses are considered unequal. 0259 * 0260 * @param other ChoiceFormat object to be compared 0261 * @return true if other is the same as this. 0262 * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments. 0263 */ 0264 virtual bool operator==(const Format& other) const override; 0265 0266 /** 0267 * Sets the pattern. 0268 * @param pattern The pattern to be applied. 0269 * @param status Output param set to success/failure code on 0270 * exit. If the pattern is invalid, this will be 0271 * set to a failure result. 0272 * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments. 0273 */ 0274 virtual void applyPattern(const UnicodeString& pattern, 0275 UErrorCode& status); 0276 0277 /** 0278 * Sets the pattern. 0279 * @param pattern The pattern to be applied. 0280 * @param parseError Struct to receive information on position 0281 * of error if an error is encountered 0282 * @param status Output param set to success/failure code on 0283 * exit. If the pattern is invalid, this will be 0284 * set to a failure result. 0285 * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments. 0286 */ 0287 virtual void applyPattern(const UnicodeString& pattern, 0288 UParseError& parseError, 0289 UErrorCode& status); 0290 /** 0291 * Gets the pattern. 0292 * 0293 * @param pattern Output param which will receive the pattern 0294 * Previous contents are deleted. 0295 * @return A reference to 'pattern' 0296 * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments. 0297 */ 0298 virtual UnicodeString& toPattern(UnicodeString &pattern) const; 0299 0300 /** 0301 * Sets the choices to be used in formatting. 0302 * For details see the constructor with the same parameter list. 0303 * 0304 * @param limitsToCopy Contains the top value that you want 0305 * parsed with that format,and should be in 0306 * ascending sorted order. When formatting X, 0307 * the choice will be the i, where limit[i] 0308 * <= X < limit[i+1]. 0309 * @param formatsToCopy The format strings you want to use for each limit. 0310 * @param count The size of the above arrays. 0311 * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments. 0312 */ 0313 virtual void setChoices(const double* limitsToCopy, 0314 const UnicodeString* formatsToCopy, 0315 int32_t count ); 0316 0317 /** 0318 * Sets the choices to be used in formatting. 0319 * For details see the constructor with the same parameter list. 0320 * 0321 * @param limits Array of limits 0322 * @param closures Array of limit booleans 0323 * @param formats Array of format string 0324 * @param count The size of the above arrays 0325 * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments. 0326 */ 0327 virtual void setChoices(const double* limits, 0328 const UBool* closures, 0329 const UnicodeString* formats, 0330 int32_t count); 0331 0332 /** 0333 * Returns nullptr and 0. 0334 * Before ICU 4.8, this used to return the choice limits array. 0335 * 0336 * @param count Will be set to 0. 0337 * @return nullptr 0338 * @deprecated ICU 4.8 Use the MessagePattern class to analyze a ChoiceFormat pattern. 0339 */ 0340 virtual const double* getLimits(int32_t& count) const; 0341 0342 /** 0343 * Returns nullptr and 0. 0344 * Before ICU 4.8, this used to return the limit booleans array. 0345 * 0346 * @param count Will be set to 0. 0347 * @return nullptr 0348 * @deprecated ICU 4.8 Use the MessagePattern class to analyze a ChoiceFormat pattern. 0349 */ 0350 virtual const UBool* getClosures(int32_t& count) const; 0351 0352 /** 0353 * Returns nullptr and 0. 0354 * Before ICU 4.8, this used to return the array of choice strings. 0355 * 0356 * @param count Will be set to 0. 0357 * @return nullptr 0358 * @deprecated ICU 4.8 Use the MessagePattern class to analyze a ChoiceFormat pattern. 0359 */ 0360 virtual const UnicodeString* getFormats(int32_t& count) const; 0361 0362 0363 using NumberFormat::format; 0364 0365 /** 0366 * Formats a double number using this object's choices. 0367 * 0368 * @param number The value to be formatted. 0369 * @param appendTo Output parameter to receive result. 0370 * Result is appended to existing contents. 0371 * @param pos On input: an alignment field, if desired. 0372 * On output: the offsets of the alignment field. 0373 * @return Reference to 'appendTo' parameter. 0374 * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments. 0375 */ 0376 virtual UnicodeString& format(double number, 0377 UnicodeString& appendTo, 0378 FieldPosition& pos) const override; 0379 /** 0380 * Formats an int32_t number using this object's choices. 0381 * 0382 * @param number The value to be formatted. 0383 * @param appendTo Output parameter to receive result. 0384 * Result is appended to existing contents. 0385 * @param pos On input: an alignment field, if desired. 0386 * On output: the offsets of the alignment field. 0387 * @return Reference to 'appendTo' parameter. 0388 * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments. 0389 */ 0390 virtual UnicodeString& format(int32_t number, 0391 UnicodeString& appendTo, 0392 FieldPosition& pos) const override; 0393 0394 /** 0395 * Formats an int64_t number using this object's choices. 0396 * 0397 * @param number The value to be formatted. 0398 * @param appendTo Output parameter to receive result. 0399 * Result is appended to existing contents. 0400 * @param pos On input: an alignment field, if desired. 0401 * On output: the offsets of the alignment field. 0402 * @return Reference to 'appendTo' parameter. 0403 * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments. 0404 */ 0405 virtual UnicodeString& format(int64_t number, 0406 UnicodeString& appendTo, 0407 FieldPosition& pos) const override; 0408 0409 /** 0410 * Formats an array of objects using this object's choices. 0411 * 0412 * @param objs The array of objects to be formatted. 0413 * @param cnt The size of objs. 0414 * @param appendTo Output parameter to receive result. 0415 * Result is appended to existing contents. 0416 * @param pos On input: an alignment field, if desired. 0417 * On output: the offsets of the alignment field. 0418 * @param success Output param set to success/failure code on 0419 * exit. 0420 * @return Reference to 'appendTo' parameter. 0421 * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments. 0422 */ 0423 virtual UnicodeString& format(const Formattable* objs, 0424 int32_t cnt, 0425 UnicodeString& appendTo, 0426 FieldPosition& pos, 0427 UErrorCode& success) const; 0428 0429 using NumberFormat::parse; 0430 0431 /** 0432 * Looks for the longest match of any message string on the input text and, 0433 * if there is a match, sets the result object to the corresponding range's number. 0434 * 0435 * If no string matches, then the parsePosition is unchanged. 0436 * 0437 * @param text The text to be parsed. 0438 * @param result Formattable to be set to the parse result. 0439 * If parse fails, return contents are undefined. 0440 * @param parsePosition The position to start parsing at on input. 0441 * On output, moved to after the last successfully 0442 * parse character. On parse failure, does not change. 0443 * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments. 0444 */ 0445 virtual void parse(const UnicodeString& text, 0446 Formattable& result, 0447 ParsePosition& parsePosition) const override; 0448 0449 /** 0450 * Returns a unique class ID POLYMORPHICALLY. Part of ICU's "poor man's RTTI". 0451 * 0452 * @return The class ID for this object. All objects of a 0453 * given class have the same class ID. Objects of 0454 * other classes have different class IDs. 0455 * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments. 0456 */ 0457 virtual UClassID getDynamicClassID(void) const override; 0458 0459 /** 0460 * Returns the class ID for this class. This is useful only for 0461 * comparing to a return value from getDynamicClassID(). For example: 0462 * <pre> 0463 * . Base* polymorphic_pointer = createPolymorphicObject(); 0464 * . if (polymorphic_pointer->getDynamicClassID() == 0465 * . Derived::getStaticClassID()) ... 0466 * </pre> 0467 * @return The class ID for all objects of this class. 0468 * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments. 0469 */ 0470 static UClassID U_EXPORT2 getStaticClassID(void); 0471 0472 private: 0473 /** 0474 * Converts a double value to a string. 0475 * @param value the double number to be converted. 0476 * @param string the result string. 0477 * @return the converted string. 0478 */ 0479 static UnicodeString& dtos(double value, UnicodeString& string); 0480 0481 ChoiceFormat() = delete; // default constructor not implemented 0482 0483 /** 0484 * Construct a new ChoiceFormat with the limits and the corresponding formats 0485 * based on the pattern. 0486 * 0487 * @param newPattern Pattern used to construct object. 0488 * @param parseError Struct to receive information on position 0489 * of error if an error is encountered. 0490 * @param status Output param to receive success code. If the 0491 * pattern cannot be parsed, set to failure code. 0492 */ 0493 ChoiceFormat(const UnicodeString& newPattern, 0494 UParseError& parseError, 0495 UErrorCode& status); 0496 0497 friend class MessageFormat; 0498 0499 virtual void setChoices(const double* limits, 0500 const UBool* closures, 0501 const UnicodeString* formats, 0502 int32_t count, 0503 UErrorCode &errorCode); 0504 0505 /** 0506 * Finds the ChoiceFormat sub-message for the given number. 0507 * @param pattern A MessagePattern. 0508 * @param partIndex the index of the first ChoiceFormat argument style part. 0509 * @param number a number to be mapped to one of the ChoiceFormat argument's intervals 0510 * @return the sub-message start part index. 0511 */ 0512 static int32_t findSubMessage(const MessagePattern &pattern, int32_t partIndex, double number); 0513 0514 static double parseArgument( 0515 const MessagePattern &pattern, int32_t partIndex, 0516 const UnicodeString &source, ParsePosition &pos); 0517 0518 /** 0519 * Matches the pattern string from the end of the partIndex to 0520 * the beginning of the limitPartIndex, 0521 * including all syntax except SKIP_SYNTAX, 0522 * against the source string starting at sourceOffset. 0523 * If they match, returns the length of the source string match. 0524 * Otherwise returns -1. 0525 */ 0526 static int32_t matchStringUntilLimitPart( 0527 const MessagePattern &pattern, int32_t partIndex, int32_t limitPartIndex, 0528 const UnicodeString &source, int32_t sourceOffset); 0529 0530 /** 0531 * Some of the ChoiceFormat constructors do not have a UErrorCode parameter. 0532 * We need _some_ way to provide one for the MessagePattern constructor. 0533 * Alternatively, the MessagePattern could be a pointer field, but that is 0534 * not nice either. 0535 */ 0536 UErrorCode constructorErrorCode; 0537 0538 /** 0539 * The MessagePattern which contains the parsed structure of the pattern string. 0540 * 0541 * Starting with ICU 4.8, the MessagePattern contains a sequence of 0542 * numeric/selector/message parts corresponding to the parsed pattern. 0543 * For details see the MessagePattern class API docs. 0544 */ 0545 MessagePattern msgPattern; 0546 0547 /** 0548 * Docs & fields from before ICU 4.8, before MessagePattern was used. 0549 * Commented out, and left only for explanation of semantics. 0550 * -------- 0551 * Each ChoiceFormat divides the range -Inf..+Inf into fCount 0552 * intervals. The intervals are: 0553 * 0554 * 0: fChoiceLimits[0]..fChoiceLimits[1] 0555 * 1: fChoiceLimits[1]..fChoiceLimits[2] 0556 * ... 0557 * fCount-2: fChoiceLimits[fCount-2]..fChoiceLimits[fCount-1] 0558 * fCount-1: fChoiceLimits[fCount-1]..+Inf 0559 * 0560 * Interval 0 is special; during formatting (mapping numbers to 0561 * strings), it also contains all numbers less than 0562 * fChoiceLimits[0], as well as NaN values. 0563 * 0564 * Interval i maps to and from string fChoiceFormats[i]. When 0565 * parsing (mapping strings to numbers), then intervals map to 0566 * their lower limit, that is, interval i maps to fChoiceLimit[i]. 0567 * 0568 * The intervals may be closed, half open, or open. This affects 0569 * formatting but does not affect parsing. Interval i is affected 0570 * by fClosures[i] and fClosures[i+1]. If fClosures[i] 0571 * is false, then the value fChoiceLimits[i] is in interval i. 0572 * That is, intervals i and i are: 0573 * 0574 * i-1: ... x < fChoiceLimits[i] 0575 * i: fChoiceLimits[i] <= x ... 0576 * 0577 * If fClosures[i] is true, then the value fChoiceLimits[i] is 0578 * in interval i-1. That is, intervals i-1 and i are: 0579 * 0580 * i-1: ... x <= fChoiceLimits[i] 0581 * i: fChoiceLimits[i] < x ... 0582 * 0583 * Because of the nature of interval 0, fClosures[0] has no 0584 * effect. 0585 */ 0586 // double* fChoiceLimits; 0587 // UBool* fClosures; 0588 // UnicodeString* fChoiceFormats; 0589 // int32_t fCount; 0590 }; 0591 0592 0593 U_NAMESPACE_END 0594 0595 #endif // U_HIDE_DEPRECATED_API 0596 #endif /* #if !UCONFIG_NO_FORMATTING */ 0597 0598 #endif /* U_SHOW_CPLUSPLUS_API */ 0599 0600 #endif // CHOICFMT_H 0601 //eof
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |