/*
*******************************************************************************
-* Copyright (C) 1997-2003, International Business Machines Corporation and others.
+* Copyright (C) 1997-2006, International Business Machines Corporation and others.
* All Rights Reserved.
*******************************************************************************
*/
#include "unicode/utypes.h"
-#if defined(U_INT64_T_UNAVAILABLE) || UCONFIG_NO_FORMATTING
+/**
+ * \file
+ * \brief C++ API: Rule Based Number Format
+ */
+
+/**
+ * \def U_HAVE_RBNF
+ * This will be 0 if RBNF support is not included in ICU
+ * and 1 if it is.
+ *
+ * @stable ICU 2.4
+ */
+#if UCONFIG_NO_FORMATTING
#define U_HAVE_RBNF 0
#else
#define U_HAVE_RBNF 1
#include "unicode/locid.h"
#include "unicode/numfmt.h"
#include "unicode/unistr.h"
+#include "unicode/strenum.h"
U_NAMESPACE_BEGIN
class NFRuleSet;
+class LocalizationInfo;
-/** Tags for the predefined rulesets. */
+/**
+ * Tags for the predefined rulesets.
+ *
+ * @stable ICU 2.2
+ */
enum URBNFRuleSetTag {
URBNF_SPELLOUT,
URBNF_ORDINAL,
#endif
/**
- * \brief C++ API: RuleBasedNumberFormat
- *
- * <h2> Rule Based Number Format C++ API </h2>
- *
- * <p>A class that formats numbers according to a set of rules. This number formatter is
+ * The RuleBasedNumberFormat class formats numbers according to a set of rules. This number formatter is
* typically used for spelling out numeric values in words (e.g., 25,3476 as
* "twenty-five thousand three hundred seventy-six" or "vingt-cinq mille trois
* cents soixante-seize" or
* "fünfundzwanzigtausenddreihundertsechsundsiebzig"), but can also be used for
* other complicated formatting tasks, such as formatting a number of seconds as hours,
- * minutes and seconds (e.g., 3,730 as "1:02:10").</p>
+ * minutes and seconds (e.g., 3,730 as "1:02:10").
*
* <p>The resources contain three predefined formatters for each locale: spellout, which
* spells out a value in words (123 is "one hundred twenty-three"); ordinal, which
*
* <table border="0" width="100%">
* <tr>
- * <td><< thousand >></strong></td>
+ * <td><strong><< thousand >></strong></td>
* <td>[the rule whose base value is 1,000 is applicable to 25,340]</td>
* </tr>
* <tr>
* <tr>
* <td>nothing</td>
* <td>Perform the mathematical operation on the number, and format the result using the rule
- * set containing the current rule, except:<ul>
+ * set containing the current rule, except:
+ * <ul>
* <li>You can't have an empty substitution descriptor with a == substitution.</li>
* <li>If you omit the substitution descriptor in a >> substitution in a fraction rule,
* format the result one digit at a time using the rule set containing the current rule.</li>
* <p>See the resource data and the demo program for annotated examples of real rule sets
* using these features.</p>
*
+ * <p><em>User subclasses are not supported.</em> While clients may write
+ * subclasses, such code will not necessarily work and will not be
+ * guaranteed to work stably from release to release.
+ *
+ * <p><b>Localizations</b></p>
+ * <p>Constructors are available that allow the specification of localizations for the
+ * public rule sets (and also allow more control over what public rule sets are available).
+ * Localization data is represented as a textual description. The description represents
+ * an array of arrays of string. The first element is an array of the public rule set names,
+ * each of these must be one of the public rule set names that appear in the rules. Only
+ * names in this array will be treated as public rule set names by the API. Each subsequent
+ * element is an array of localizations of these names. The first element of one of these
+ * subarrays is the locale name, and the remaining elements are localizations of the
+ * public rule set names, in the same order as they were listed in the first arrray.</p>
+ * <p>In the syntax, angle brackets '<', '>' are used to delimit the arrays, and comma ',' is used
+ * to separate elements of an array. Whitespace is ignored, unless quoted.</p>
+ * <p>For example:<pre>
+ * < < %foo, %bar, %baz >,
+ * < en, Foo, Bar, Baz >,
+ * < fr, 'le Foo', 'le Bar', 'le Baz' >
+ * < zh, \\u7532, \\u4e59, \\u4e19 > >
+ * </pre></p>
* @author Richard Gillam
* @see NumberFormat
* @see DecimalFormat
// constructors
//-----------------------------------------------------------------------
+ /**
+ * Creates a RuleBasedNumberFormat that behaves according to the description
+ * passed in. The formatter uses the default locale.
+ * @param rules A description of the formatter's desired behavior.
+ * See the class documentation for a complete explanation of the description
+ * syntax.
+ * @param perror The parse error if an error was encountered.
+ * @param status The status indicating whether the constructor succeeded.
+ * @stable ICU 3.2
+ */
+ RuleBasedNumberFormat(const UnicodeString& rules, UParseError& perror, UErrorCode& status);
+
+ /**
+ * Creates a RuleBasedNumberFormat that behaves according to the description
+ * passed in. The formatter uses the default locale.
+ * <p>
+ * The localizations data provides information about the public
+ * rule sets and their localized display names for different
+ * locales. The first element in the list is an array of the names
+ * of the public rule sets. The first element in this array is
+ * the initial default ruleset. The remaining elements in the
+ * list are arrays of localizations of the names of the public
+ * rule sets. Each of these is one longer than the initial array,
+ * with the first String being the ULocale ID, and the remaining
+ * Strings being the localizations of the rule set names, in the
+ * same order as the initial array. Arrays are NULL-terminated.
+ * @param rules A description of the formatter's desired behavior.
+ * See the class documentation for a complete explanation of the description
+ * syntax.
+ * @param localizations the localization information.
+ * names in the description. These will be copied by the constructor.
+ * @param perror The parse error if an error was encountered.
+ * @param status The status indicating whether the constructor succeeded.
+ * @stable ICU 3.2
+ */
+ RuleBasedNumberFormat(const UnicodeString& rules, const UnicodeString& localizations,
+ UParseError& perror, UErrorCode& status);
+
/**
* Creates a RuleBasedNumberFormat that behaves according to the rules
* passed in. The formatter uses the specified locale to determine the
* @param rules The formatter rules.
* See the class documentation for a complete explanation of the rule
* syntax.
- * @param locale A locale, that governs which characters are used for
- * formatting values in numerals, and which characters are equivalent in
+ * @param locale A locale that governs which characters are used for
+ * formatting values in numerals and which characters are equivalent in
* lenient parsing.
* @param perror The parse error if an error was encountered.
* @param status The status indicating whether the constructor succeeded.
* @stable ICU 2.0
*/
- RuleBasedNumberFormat(const UnicodeString& rules, const Locale& locale,
+ RuleBasedNumberFormat(const UnicodeString& rules, const Locale& locale,
UParseError& perror, UErrorCode& status);
+ /**
+ * Creates a RuleBasedNumberFormat that behaves according to the description
+ * passed in. The formatter uses the default locale.
+ * <p>
+ * The localizations data provides information about the public
+ * rule sets and their localized display names for different
+ * locales. The first element in the list is an array of the names
+ * of the public rule sets. The first element in this array is
+ * the initial default ruleset. The remaining elements in the
+ * list are arrays of localizations of the names of the public
+ * rule sets. Each of these is one longer than the initial array,
+ * with the first String being the ULocale ID, and the remaining
+ * Strings being the localizations of the rule set names, in the
+ * same order as the initial array. Arrays are NULL-terminated.
+ * @param rules A description of the formatter's desired behavior.
+ * See the class documentation for a complete explanation of the description
+ * syntax.
+ * @param localizations a list of localizations for the rule set
+ * names in the description. These will be copied by the constructor.
+ * @param locale A locale that governs which characters are used for
+ * formatting values in numerals and which characters are equivalent in
+ * lenient parsing.
+ * @param perror The parse error if an error was encountered.
+ * @param status The status indicating whether the constructor succeeded.
+ * @stable ICU 3.2
+ */
+ RuleBasedNumberFormat(const UnicodeString& rules, const UnicodeString& localizations,
+ const Locale& locale, UParseError& perror, UErrorCode& status);
+
/**
* Creates a RuleBasedNumberFormat from a predefined ruleset. The selector
* code choosed among three possible predefined formats: spellout, ordinal,
*/
virtual UnicodeString getRules() const;
+ /**
+ * Return the number of public rule set names.
+ * @return the number of public rule set names.
+ * @stable ICU 2.0
+ */
+ virtual int32_t getNumberOfRuleSetNames() const;
+
/**
* Return the name of the index'th public ruleSet. If index is not valid,
* the function returns null.
virtual UnicodeString getRuleSetName(int32_t index) const;
/**
- * Return the number of public rule set names.
- * @return the number of public rule set names.
- * @stable ICU 2.0
+ * Return the number of locales for which we have localized rule set display names.
+ * @return the number of locales for which we have localized rule set display names.
+ * @stable ICU 3.2
*/
- virtual int32_t getNumberOfRuleSetNames() const;
+ virtual int32_t getNumberOfRuleSetDisplayNameLocales(void) const;
+
+ /**
+ * Return the index'th display name locale.
+ * @param index the index of the locale
+ * @param status set to a failure code when this function fails
+ * @return the locale
+ * @see #getNumberOfRuleSetDisplayNameLocales
+ * @stable ICU 3.2
+ */
+ virtual Locale getRuleSetDisplayNameLocale(int32_t index, UErrorCode& status) const;
+
+ /**
+ * Return the rule set display names for the provided locale. These are in the same order
+ * as those returned by getRuleSetName. The locale is matched against the locales for
+ * which there is display name data, using normal fallback rules. If no locale matches,
+ * the default display names are returned. (These are the internal rule set names minus
+ * the leading '%'.)
+ * @param index the index of the rule set
+ * @param locale the locale (returned by getRuleSetDisplayNameLocales) for which the localized
+ * display name is desired
+ * @return the display name for the given index, which might be bogus if there is an error
+ * @see #getRuleSetName
+ * @stable ICU 3.2
+ */
+ virtual UnicodeString getRuleSetDisplayName(int32_t index,
+ const Locale& locale = Locale::getDefault());
+
+ /**
+ * Return the rule set display name for the provided rule set and locale.
+ * The locale is matched against the locales for which there is display name data, using
+ * normal fallback rules. If no locale matches, the default display name is returned.
+ * @return the display name for the rule set
+ * @stable ICU 3.2
+ * @see #getRuleSetDisplayName
+ */
+ virtual UnicodeString getRuleSetDisplayName(const UnicodeString& ruleSetName,
+ const Locale& locale = Locale::getDefault());
/**
* Formats the specified 32-bit number using the default ruleset.
* @param parsePosition On entry, contains the position of the first character
* in "text" to examine. On exit, has been updated to contain the position
* of the first character in "text" that wasn't consumed by the parse.
- * @see #setLenientParseMode
+ * @see #setLenient
* @stable ICU 2.0
*/
virtual void parse(const UnicodeString& text,
Formattable& result,
ParsePosition& parsePosition) const;
-
+
/**
* Redeclared Format method.
* @param text The string to parse
* Returns true if lenient-parse mode is turned on. Lenient parsing is off
* by default.
* @return true if lenient-parse mode is turned on.
- * @see #setLenientParseMode
+ * @see #setLenient
* @stable ICU 2.0
*/
virtual inline UBool isLenient(void) const;
* to the initial default rule set. If the rule set is not a public rule set name,
* U_ILLEGAL_ARGUMENT_ERROR is returned in status.
* @param ruleSetName the name of the rule set, or null to reset the initial default.
- * @param status Output param set to failure code when a problem occurs.
- * @draft ICU 2.6
+ * @param status set to failure code when a problem occurs.
+ * @stable ICU 2.6
*/
virtual void setDefaultRuleSet(const UnicodeString& ruleSetName, UErrorCode& status);
-private:
- RuleBasedNumberFormat(); // default constructor not implemented
-
- void init(const UnicodeString& rules, UParseError& perror, UErrorCode& status);
- void dispose();
- void stripWhitespace(UnicodeString& src);
- void initDefaultRuleSet();
- void format(double number, NFRuleSet& ruleSet);
- NFRuleSet* findRuleSet(const UnicodeString& name, UErrorCode& status) const;
+ /**
+ * Return the name of the current default rule set. If the current rule set is
+ * not public, returns a bogus (and empty) UnicodeString.
+ * @return the name of the current default rule set
+ * @stable ICU 3.0
+ */
+ virtual UnicodeString getDefaultRuleSetName() const;
- /* friend access */
- friend class NFSubstitution;
- friend class NFRule;
- friend class FractionalPartSubstitution;
+public:
+ /**
+ * ICU "poor man's RTTI", returns a UClassID for this class.
+ *
+ * @stable ICU 2.8
+ */
+ static UClassID U_EXPORT2 getStaticClassID(void);
- inline NFRuleSet * getDefaultRuleSet() const;
- Collator * getCollator() const;
- DecimalFormatSymbols * getDecimalFormatSymbols() const;
+ /**
+ * ICU "poor man's RTTI", returns a UClassID for the actual class.
+ *
+ * @stable ICU 2.8
+ */
+ virtual UClassID getDynamicClassID(void) const;
private:
- static const char fgClassID;
+ RuleBasedNumberFormat(); // default constructor not implemented
-public:
- static UClassID getStaticClassID(void) { return (UClassID)&fgClassID; }
- virtual UClassID getDynamicClassID(void) const { return getStaticClassID(); }
+ // this will ref the localizations if they are not NULL
+ // caller must deref to get adoption
+ RuleBasedNumberFormat(const UnicodeString& description, LocalizationInfo* localizations,
+ const Locale& locale, UParseError& perror, UErrorCode& status);
+
+ void init(const UnicodeString& rules, LocalizationInfo* localizations, UParseError& perror, UErrorCode& status);
+ void dispose();
+ void stripWhitespace(UnicodeString& src);
+ void initDefaultRuleSet();
+ void format(double number, NFRuleSet& ruleSet);
+ NFRuleSet* findRuleSet(const UnicodeString& name, UErrorCode& status) const;
+
+ /* friend access */
+ friend class NFSubstitution;
+ friend class NFRule;
+ friend class FractionalPartSubstitution;
+
+ inline NFRuleSet * getDefaultRuleSet() const;
+ Collator * getCollator() const;
+ DecimalFormatSymbols * getDecimalFormatSymbols() const;
private:
NFRuleSet **ruleSets;
DecimalFormatSymbols* decimalFormatSymbols;
UBool lenient;
UnicodeString* lenientParseRules;
+ LocalizationInfo* localizations;
};
// ---------------
#if !UCONFIG_NO_COLLATION
-inline UBool
-RuleBasedNumberFormat::isLenient(void) const {
- return lenient;
+inline UBool
+RuleBasedNumberFormat::isLenient(void) const {
+ return lenient;
}
#endif
-inline NFRuleSet*
-RuleBasedNumberFormat::getDefaultRuleSet() const {
- return defaultRuleSet;
+inline NFRuleSet*
+RuleBasedNumberFormat::getDefaultRuleSet() const {
+ return defaultRuleSet;
}
U_NAMESPACE_END