2007-12-17 01:39:55 +00:00
|
|
|
/*
|
|
|
|
*******************************************************************************
|
2009-03-19 04:03:31 +00:00
|
|
|
* Copyright (C) 2008-2009, International Business Machines Corporation and
|
2007-12-17 01:39:55 +00:00
|
|
|
* others. All Rights Reserved.
|
|
|
|
*******************************************************************************
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* File PLURRULE.H
|
|
|
|
*
|
|
|
|
* Modification History:*
|
|
|
|
* Date Name Description
|
|
|
|
*
|
|
|
|
********************************************************************************
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef PLURRULE
|
|
|
|
#define PLURRULE
|
|
|
|
|
2007-12-17 19:30:03 +00:00
|
|
|
#include "unicode/utypes.h"
|
|
|
|
|
2007-12-17 01:39:55 +00:00
|
|
|
/**
|
|
|
|
* \file
|
|
|
|
* \brief C++ API: PluralRules object
|
|
|
|
*/
|
2007-12-17 19:30:03 +00:00
|
|
|
|
2007-12-17 01:39:55 +00:00
|
|
|
#if !UCONFIG_NO_FORMATTING
|
|
|
|
|
|
|
|
#include "unicode/format.h"
|
|
|
|
|
|
|
|
U_NAMESPACE_BEGIN
|
|
|
|
|
|
|
|
class Hashtable;
|
|
|
|
class RuleChain;
|
|
|
|
class RuleParser;
|
|
|
|
|
2007-12-17 19:30:03 +00:00
|
|
|
/**
|
|
|
|
* Defines rules for mapping positive long values onto a small set of
|
|
|
|
* keywords. Rules are constructed from a text description, consisting
|
|
|
|
* of a series of keywords and conditions. The {@link #select} method
|
|
|
|
* examines each condition in order and returns the keyword for the
|
|
|
|
* first condition that matches the number. If none match,
|
|
|
|
* default rule(other) is returned.
|
|
|
|
*
|
|
|
|
* Examples:<pre>
|
|
|
|
* "one: n is 1; few: n in 2..4"</pre>
|
|
|
|
* This defines two rules, for 'one' and 'few'. The condition for
|
|
|
|
* 'one' is "n is 1" which means that the number must be equal to
|
|
|
|
* 1 for this condition to pass. The condition for 'few' is
|
|
|
|
* "n in 2..4" which means that the number must be between 2 and
|
|
|
|
* 4 inclusive for this condition to pass. All other numbers
|
|
|
|
* are assigned the keyword "other" by the default rule.
|
|
|
|
* </p><pre>
|
|
|
|
* "zero: n is 0; one: n is 1; zero: n mod 100 in 1..19"</pre>
|
|
|
|
* This illustrates that the same keyword can be defined multiple times.
|
|
|
|
* Each rule is examined in order, and the first keyword whose condition
|
|
|
|
* passes is the one returned. Also notes that a modulus is applied
|
|
|
|
* to n in the last rule. Thus its condition holds for 119, 219, 319...
|
|
|
|
* </p><pre>
|
|
|
|
* "one: n is 1; few: n mod 10 in 2..4 and n mod 100 not in 12..14"</pre>
|
|
|
|
* This illustrates conjunction and negation. The condition for 'few'
|
|
|
|
* has two parts, both of which must be met: "n mod 10 in 2..4" and
|
|
|
|
* "n mod 100 not in 12..14". The first part applies a modulus to n
|
|
|
|
* before the test as in the previous example. The second part applies
|
|
|
|
* a different modulus and also uses negation, thus it matches all
|
|
|
|
* numbers _not_ in 12, 13, 14, 112, 113, 114, 212, 213, 214...
|
2008-05-21 21:34:54 +00:00
|
|
|
* </p>
|
2008-06-26 17:51:59 +00:00
|
|
|
* <p>
|
|
|
|
* Syntax:<pre>
|
2008-09-27 01:12:32 +00:00
|
|
|
* \code
|
2008-06-26 17:51:59 +00:00
|
|
|
* rules = rule (';' rule)*
|
|
|
|
* rule = keyword ':' condition
|
|
|
|
* keyword = <identifier>
|
|
|
|
* condition = and_condition ('or' and_condition)*
|
|
|
|
* and_condition = relation ('and' relation)*
|
|
|
|
* relation = is_relation | in_relation | within_relation | 'n' <EOL>
|
|
|
|
* is_relation = expr 'is' ('not')? value
|
|
|
|
* in_relation = expr ('not')? 'in' range
|
|
|
|
* within_relation = expr ('not')? 'within' range
|
|
|
|
* expr = 'n' ('mod' value)?
|
|
|
|
* value = digit+
|
|
|
|
* digit = 0|1|2|3|4|5|6|7|8|9
|
|
|
|
* range = value'..'value
|
2008-09-27 01:12:32 +00:00
|
|
|
* \endcode
|
2008-06-26 17:51:59 +00:00
|
|
|
* </pre></p>
|
|
|
|
* <p>
|
|
|
|
* The difference between 'in' and 'within' is that 'in' only includes
|
|
|
|
* integers in the specified range, while 'within' includes all values.</p>
|
|
|
|
* <p>
|
2007-12-17 19:30:03 +00:00
|
|
|
* Keywords
|
|
|
|
* could be defined by users or from ICU locale data. There are 6
|
|
|
|
* predefined values in ICU - 'zero', 'one', 'two', 'few', 'many' and
|
|
|
|
* 'other'. Callers need to check the value of keyword returned by
|
|
|
|
* {@link #select} method.
|
2008-05-21 21:34:54 +00:00
|
|
|
* </p>
|
2007-12-17 19:30:03 +00:00
|
|
|
*
|
|
|
|
* Examples:<pre>
|
|
|
|
* UnicodeString keyword = pl->select(number);
|
|
|
|
* if (keyword== UnicodeString("one") {
|
|
|
|
* ...
|
|
|
|
* }
|
|
|
|
* else if ( ... )
|
2008-05-21 21:34:54 +00:00
|
|
|
* </pre>
|
2007-12-17 19:30:03 +00:00
|
|
|
*/
|
2007-12-17 01:39:55 +00:00
|
|
|
class U_I18N_API PluralRules : public UObject {
|
|
|
|
public:
|
2007-12-17 19:30:03 +00:00
|
|
|
|
2007-12-17 01:39:55 +00:00
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
* @param status Output param set to success/failure code on exit, which
|
|
|
|
* must not indicate a failure before the function call.
|
2007-12-17 19:30:03 +00:00
|
|
|
*
|
2009-03-19 04:03:31 +00:00
|
|
|
* @stable ICU 4.0
|
2007-12-17 01:39:55 +00:00
|
|
|
*/
|
|
|
|
PluralRules(UErrorCode& status);
|
2007-12-17 19:30:03 +00:00
|
|
|
|
2007-12-17 01:39:55 +00:00
|
|
|
/**
|
|
|
|
* Copy constructor.
|
2009-03-19 04:03:31 +00:00
|
|
|
* @stable ICU 4.0
|
2007-12-17 01:39:55 +00:00
|
|
|
*/
|
|
|
|
PluralRules(const PluralRules& other);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Destructor.
|
2009-03-19 04:03:31 +00:00
|
|
|
* @stable ICU 4.0
|
2007-12-17 01:39:55 +00:00
|
|
|
*/
|
|
|
|
virtual ~PluralRules();
|
2007-12-17 19:30:03 +00:00
|
|
|
|
2007-12-17 01:39:55 +00:00
|
|
|
/**
|
|
|
|
* Clone
|
2009-03-19 04:03:31 +00:00
|
|
|
* @stable ICU 4.0
|
2007-12-17 01:39:55 +00:00
|
|
|
*/
|
|
|
|
PluralRules* clone() const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Assignment operator.
|
2009-03-19 04:03:31 +00:00
|
|
|
* @stable ICU 4.0
|
2007-12-17 01:39:55 +00:00
|
|
|
*/
|
|
|
|
PluralRules& operator=(const PluralRules&);
|
2007-12-17 19:30:03 +00:00
|
|
|
|
2007-12-17 01:39:55 +00:00
|
|
|
/**
|
2007-12-17 19:30:03 +00:00
|
|
|
* Creates a PluralRules from a description if it is parsable, otherwise
|
2007-12-17 01:39:55 +00:00
|
|
|
* returns null.
|
2007-12-17 19:30:03 +00:00
|
|
|
*
|
2007-12-17 01:39:55 +00:00
|
|
|
* @param description rule description
|
|
|
|
* @param status Output param set to success/failure code on exit, which
|
|
|
|
* must not indicate a failure before the function call.
|
|
|
|
* @return new PluralRules pointer. NULL if there is an error.
|
2009-03-19 04:03:31 +00:00
|
|
|
* @stable ICU 4.0
|
2007-12-17 01:39:55 +00:00
|
|
|
*/
|
|
|
|
static PluralRules* U_EXPORT2 createRules(const UnicodeString& description,
|
|
|
|
UErrorCode& status);
|
2007-12-17 19:30:03 +00:00
|
|
|
|
2007-12-17 01:39:55 +00:00
|
|
|
/**
|
|
|
|
* The default rules that accept any number.
|
2007-12-17 19:30:03 +00:00
|
|
|
*
|
2007-12-17 01:39:55 +00:00
|
|
|
* @param status Output param set to success/failure code on exit, which
|
|
|
|
* must not indicate a failure before the function call.
|
|
|
|
* @return new PluralRules pointer. NULL if there is an error.
|
2009-03-19 04:03:31 +00:00
|
|
|
* @stable ICU 4.0
|
2007-12-17 01:39:55 +00:00
|
|
|
*/
|
|
|
|
static PluralRules* U_EXPORT2 createDefaultRules(UErrorCode& status);
|
2007-12-17 19:30:03 +00:00
|
|
|
|
2007-12-17 01:39:55 +00:00
|
|
|
/**
|
|
|
|
* Provides access to the predefined <code>PluralRules</code> for a given
|
|
|
|
* locale.
|
2007-12-17 19:30:03 +00:00
|
|
|
*
|
2007-12-17 01:39:55 +00:00
|
|
|
* @param locale The locale for which a <code>PluralRules</code> object is
|
|
|
|
* returned.
|
|
|
|
* @param status Output param set to success/failure code on exit, which
|
|
|
|
* must not indicate a failure before the function call.
|
|
|
|
* @return The predefined <code>PluralRules</code> object pointer for
|
|
|
|
* this locale. If there's no predefined rules for this locale,
|
|
|
|
* the rules for the closest parent in the locale hierarchy
|
|
|
|
* that has one will be returned. The final fallback always
|
|
|
|
* returns the default 'other' rules.
|
2009-03-19 04:03:31 +00:00
|
|
|
* @stable ICU 4.0
|
2007-12-17 01:39:55 +00:00
|
|
|
*/
|
|
|
|
static PluralRules* U_EXPORT2 forLocale(const Locale& locale, UErrorCode& status);
|
2008-06-26 17:51:59 +00:00
|
|
|
|
2007-12-17 01:39:55 +00:00
|
|
|
/**
|
|
|
|
* Given a number, returns the keyword of the first rule that applies to
|
|
|
|
* the number. This function can be used with isKeyword* functions to
|
|
|
|
* determine the keyword for default plural rules.
|
2007-12-17 19:30:03 +00:00
|
|
|
*
|
2007-12-17 01:39:55 +00:00
|
|
|
* @param number The number for which the rule has to be determined.
|
|
|
|
* @return The keyword of the selected rule.
|
2009-03-19 04:03:31 +00:00
|
|
|
* @stable ICU 4.0
|
2007-12-17 01:39:55 +00:00
|
|
|
*/
|
2007-12-17 19:30:03 +00:00
|
|
|
UnicodeString select(int32_t number) const;
|
2008-06-26 17:51:59 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Given a number, returns the keyword of the first rule that applies to
|
|
|
|
* the number. This function can be used with isKeyword* functions to
|
|
|
|
* determine the keyword for default plural rules.
|
|
|
|
*
|
|
|
|
* @param number The number for which the rule has to be determined.
|
|
|
|
* @return The keyword of the selected rule.
|
2009-03-19 04:03:31 +00:00
|
|
|
* @stable ICU 4.0
|
2008-06-26 17:51:59 +00:00
|
|
|
*/
|
|
|
|
UnicodeString select(double number) const;
|
2007-12-17 19:30:03 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a list of all rule keywords used in this <code>PluralRules</code>
|
|
|
|
* object. The rule 'other' is always present by default.
|
|
|
|
*
|
|
|
|
* @param status Output param set to success/failure code on exit, which
|
|
|
|
* must not indicate a failure before the function call.
|
|
|
|
* @return StringEnumeration with the keywords.
|
|
|
|
* The caller must delete the object.
|
2009-03-19 04:03:31 +00:00
|
|
|
* @stable ICU 4.0
|
2007-12-17 19:30:03 +00:00
|
|
|
*/
|
|
|
|
StringEnumeration* getKeywords(UErrorCode& status) const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns TRUE if the given keyword is defined in this
|
|
|
|
* <code>PluralRules</code> object.
|
|
|
|
*
|
|
|
|
* @param keyword the input keyword.
|
|
|
|
* @return TRUE if the input keyword is defined.
|
|
|
|
* Otherwise, return FALSE.
|
2009-03-19 04:03:31 +00:00
|
|
|
* @stable ICU 4.0
|
2007-12-17 19:30:03 +00:00
|
|
|
*/
|
|
|
|
UBool isKeyword(const UnicodeString& keyword) const;
|
2007-12-17 01:39:55 +00:00
|
|
|
|
|
|
|
|
2007-12-17 19:30:03 +00:00
|
|
|
/**
|
|
|
|
* Returns keyword for default plural form.
|
|
|
|
*
|
|
|
|
* @return keyword for default plural form.
|
|
|
|
* @internal 4.0
|
2009-03-19 04:03:31 +00:00
|
|
|
* @stable ICU 4.0
|
2007-12-17 19:30:03 +00:00
|
|
|
*/
|
|
|
|
UnicodeString getKeywordOther() const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compares the equality of two PluralRules objects.
|
|
|
|
*
|
|
|
|
* @param other The other PluralRules object to be compared with.
|
|
|
|
* @return True if the given PluralRules is the same as this
|
|
|
|
* PluralRules; false otherwise.
|
2009-03-19 04:03:31 +00:00
|
|
|
* @stable ICU 4.0
|
2007-12-17 19:30:03 +00:00
|
|
|
*/
|
|
|
|
virtual UBool operator==(const PluralRules& other) const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compares the inequality of two PluralRules objects.
|
|
|
|
*
|
|
|
|
* @param other The PluralRules object to be compared with.
|
|
|
|
* @return True if the given PluralRules is not the same as this
|
|
|
|
* PluralRules; false otherwise.
|
2009-03-19 04:03:31 +00:00
|
|
|
* @stable ICU 4.0
|
2007-12-17 19:30:03 +00:00
|
|
|
*/
|
|
|
|
UBool operator!=(const PluralRules& other) const {return !operator==(other);}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ICU "poor man's RTTI", returns a UClassID for this class.
|
|
|
|
*
|
2009-03-19 04:03:31 +00:00
|
|
|
* @stable ICU 4.0
|
2007-12-17 19:30:03 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
static UClassID U_EXPORT2 getStaticClassID(void);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ICU "poor man's RTTI", returns a UClassID for the actual class.
|
|
|
|
*
|
2009-03-19 04:03:31 +00:00
|
|
|
* @stable ICU 4.0
|
2007-12-17 19:30:03 +00:00
|
|
|
*/
|
|
|
|
virtual UClassID getDynamicClassID() const;
|
|
|
|
|
|
|
|
|
|
|
|
private:
|
2007-12-17 01:39:55 +00:00
|
|
|
Hashtable *fLocaleStringsHash;
|
2008-01-21 21:51:09 +00:00
|
|
|
UnicodeString mLocaleName;
|
|
|
|
RuleChain *mRules;
|
|
|
|
RuleParser *mParser;
|
2007-12-17 19:30:03 +00:00
|
|
|
|
2007-12-17 01:39:55 +00:00
|
|
|
PluralRules(); // default constructor not implemented
|
2007-12-17 19:30:03 +00:00
|
|
|
int32_t getRepeatLimit() const;
|
2007-12-29 05:51:50 +00:00
|
|
|
void parseDescription(UnicodeString& ruleData, RuleChain& rules, UErrorCode &status);
|
2007-12-17 01:39:55 +00:00
|
|
|
void getNextLocale(const UnicodeString& localeData, int32_t* curIndex, UnicodeString& localeName);
|
2008-06-19 20:23:49 +00:00
|
|
|
void addRules(RuleChain& rules);
|
2007-12-17 01:39:55 +00:00
|
|
|
int32_t getNumberValue(const UnicodeString& token) const;
|
2008-06-19 20:23:49 +00:00
|
|
|
UnicodeString getRuleFromResource(const Locale& locale, UErrorCode& status);
|
2007-12-17 19:30:03 +00:00
|
|
|
|
2007-12-17 01:39:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
U_NAMESPACE_END
|
|
|
|
|
|
|
|
#endif /* #if !UCONFIG_NO_FORMATTING */
|
|
|
|
|
|
|
|
#endif // _PLURRULE
|
|
|
|
//eof
|