+// © 2016 and later: Unicode, Inc. and others.
+// License & terms of use: http://www.unicode.org/copyright.html
/*
*******************************************************************************
-* Copyright (C) 1997-2004, International Business Machines Corporation and others.
+* Copyright (C) 1997-2015, International Business Machines Corporation and others.
* All Rights Reserved.
*******************************************************************************
*/
#include "unicode/utypes.h"
+/**
+ * \file
+ * \brief C++ API: Rule Based Number Format
+ */
+
/**
* \def U_HAVE_RBNF
* This will be 0 if RBNF support is not included in ICU
*
* @stable ICU 2.4
*/
-#if defined(U_INT64_T_UNAVAILABLE) || UCONFIG_NO_FORMATTING
+#if UCONFIG_NO_FORMATTING
#define U_HAVE_RBNF 0
#else
#define U_HAVE_RBNF 1
-#include "unicode/coll.h"
#include "unicode/dcfmtsym.h"
#include "unicode/fmtable.h"
#include "unicode/locid.h"
#include "unicode/numfmt.h"
#include "unicode/unistr.h"
#include "unicode/strenum.h"
+#include "unicode/brkiter.h"
+#include "unicode/upluralrules.h"
+#if U_SHOW_CPLUSPLUS_API
U_NAMESPACE_BEGIN
+class NFRule;
class NFRuleSet;
class LocalizationInfo;
+class PluralFormat;
+class RuleBasedCollator;
/**
* Tags for the predefined rulesets.
URBNF_SPELLOUT,
URBNF_ORDINAL,
URBNF_DURATION,
+ URBNF_NUMBERING_SYSTEM,
+#ifndef U_HIDE_DEPRECATED_API
+ /**
+ * One more than the highest normal URBNFRuleSetTag value.
+ * @deprecated ICU 58 The numeric value may change over time, see ICU ticket #12420.
+ */
URBNF_COUNT
+#endif // U_HIDE_DEPRECATED_API
};
-#if UCONFIG_NO_COLLATION
-class Collator;
-#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
* </tr>
* <tr>
* <td>x.x:</td>
- * <td>The rule is an <em>improper fraction rule.</em></td>
+ * <td>The rule is an <em>improper fraction rule</em>. If the full stop in
+ * the middle of the rule name is replaced with the decimal point
+ * that is used in the language or DecimalFormatSymbols, then that rule will
+ * have precedence when formatting and parsing this rule. For example, some
+ * languages use the comma, and can thus be written as x,x instead. For example,
+ * you can use "x.x: << point >>;x,x: << comma >>;" to
+ * handle the decimal point that matches the language's natural spelling of
+ * the punctuation of either the full stop or comma.</td>
* </tr>
* <tr>
* <td>0.x:</td>
- * <td>The rule is a <em>proper fraction rule.</em></td>
+ * <td>The rule is a <em>proper fraction rule</em>. If the full stop in
+ * the middle of the rule name is replaced with the decimal point
+ * that is used in the language or DecimalFormatSymbols, then that rule will
+ * have precedence when formatting and parsing this rule. For example, some
+ * languages use the comma, and can thus be written as 0,x instead. For example,
+ * you can use "0.x: point >>;0,x: comma >>;" to
+ * handle the decimal point that matches the language's natural spelling of
+ * the punctuation of either the full stop or comma.</td>
* </tr>
* <tr>
* <td>x.0:</td>
- * <td>The rule is a <em>master rule.</em></td>
+ * <td>The rule is a <em>master rule</em>. If the full stop in
+ * the middle of the rule name is replaced with the decimal point
+ * that is used in the language or DecimalFormatSymbols, then that rule will
+ * have precedence when formatting and parsing this rule. For example, some
+ * languages use the comma, and can thus be written as x,0 instead. For example,
+ * you can use "x.0: << point;x,0: << comma;" to
+ * handle the decimal point that matches the language's natural spelling of
+ * the punctuation of either the full stop or comma.</td>
* </tr>
* <tr>
+ * <td>Inf:</td>
+ * <td>The rule for infinity.</td>
+ * </tr>
+ * <tr>
+ * <td>NaN:</td>
+ * <td>The rule for an IEEE 754 NaN (not a number).</td>
+ * </tr>
+ * <tr>
+ * <tr>
* <td><em>nothing</em></td>
* <td>If the rule's rule descriptor is left out, the base value is one plus the
* preceding rule's base value (or zero if this is the first rule in the list) in a normal
* <td>in rule in fraction rule set</td>
* <td>Omit the optional text if multiplying the number by the rule's base value yields 1.</td>
* </tr>
+ * <tr>
+ * <td width="37">$(cardinal,<i>plural syntax</i>)$</td>
+ * <td width="23"></td>
+ * <td width="165" valign="top">in all rule sets</td>
+ * <td>This provides the ability to choose a word based on the number divided by the radix to the power of the
+ * exponent of the base value for the specified locale, which is normally equivalent to the << value.
+ * This uses the cardinal plural rules from PluralFormat. All strings used in the plural format are treated
+ * as the same base value for parsing.</td>
+ * </tr>
+ * <tr>
+ * <td width="37">$(ordinal,<i>plural syntax</i>)$</td>
+ * <td width="23"></td>
+ * <td width="165" valign="top">in all rule sets</td>
+ * <td>This provides the ability to choose a word based on the number divided by the radix to the power of the
+ * exponent of the base value for the specified locale, which is normally equivalent to the << value.
+ * This uses the ordinal plural rules from PluralFormat. All strings used in the plural format are treated
+ * as the same base value for parsing.</td>
+ * </tr>
* </table>
*
* <p>The substitution descriptor (i.e., the text between the token characters) may take one
* <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' >
+ * < < %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
+ * @see PluralFormat
+ * @see PluralRules
* @stable ICU 2.0
*/
class U_I18N_API RuleBasedNumberFormat : public NumberFormat {
* syntax.
* @param perror The parse error if an error was encountered.
* @param status The status indicating whether the constructor succeeded.
- * @draft ICU 3.2
+ * @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.
+ * 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
* 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.
- * @draft ICU 3.2
+ * @stable ICU 3.2
*/
RuleBasedNumberFormat(const UnicodeString& rules, const UnicodeString& localizations,
UParseError& perror, UErrorCode& status);
/**
* Creates a RuleBasedNumberFormat that behaves according to the description
- * passed in. The formatter uses the default locale.
+ * 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
* lenient parsing.
* @param perror The parse error if an error was encountered.
* @param status The status indicating whether the constructor succeeded.
- * @draft ICU 3.2
+ * @stable ICU 3.2
*/
RuleBasedNumberFormat(const UnicodeString& rules, const UnicodeString& localizations,
const Locale& locale, UParseError& perror, UErrorCode& status);
* code choosed among three possible predefined formats: spellout, ordinal,
* and duration.
* @param tag A selector code specifying which kind of formatter to create for that
- * locale. There are three legal values: URBNF_SPELLOUT, which creates a formatter that
+ * locale. There are four legal values: URBNF_SPELLOUT, which creates a formatter that
* spells out a value in words in the desired language, URBNF_ORDINAL, which attaches
* an ordinal suffix from the desired language to the end of a number (e.g. "123rd"),
- * and URBNF_DURATION, which formats a duration in seconds as hours, minutes, and seconds.
+ * URBNF_DURATION, which formats a duration in seconds as hours, minutes, and seconds always rounding down,
+ * and URBNF_NUMBERING_SYSTEM, which is used to invoke rules for alternate numbering
+ * systems such as the Hebrew numbering system, or for Roman Numerals, etc.
* @param locale The locale for the formatter.
* @param status The status indicating whether the constructor succeeded.
* @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.
- * @draft ICU 3.2
+ * @stable ICU 3.2
*/
virtual int32_t getNumberOfRuleSetDisplayNameLocales(void) const;
* @param status set to a failure code when this function fails
* @return the locale
* @see #getNumberOfRuleSetDisplayNameLocales
- * @draft ICU 3.2
+ * @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,
+ * 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
* display name is desired
* @return the display name for the given index, which might be bogus if there is an error
* @see #getRuleSetName
- * @draft ICU 3.2
+ * @stable ICU 3.2
*/
- virtual UnicodeString getRuleSetDisplayName(int32_t index,
+ virtual UnicodeString getRuleSetDisplayName(int32_t index,
const Locale& locale = Locale::getDefault());
/**
- * Return the rule set display name for the provided rule set and locale.
+ * 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
- * @draft ICU 3.2
- * @see #getRuleSetDisplayNames
+ * @stable ICU 3.2
+ * @see #getRuleSetDisplayName
*/
- virtual UnicodeString getRuleSetDisplayName(const UnicodeString& ruleSetName,
+ virtual UnicodeString getRuleSetDisplayName(const UnicodeString& ruleSetName,
const Locale& locale = Locale::getDefault());
+
+ using NumberFormat::format;
+
/**
* Formats the specified 32-bit number using the default ruleset.
* @param number The number to format.
FieldPosition& pos,
UErrorCode& status) const;
- /**
- * Formats the specified number using the default ruleset.
- * @param obj The number to format.
- * @param toAppendTo the string that will hold the (appended) result
- * @param pos the fieldposition
- * @param status the status
- * @return A textual representation of the number.
- * @stable ICU 2.0
- */
- virtual UnicodeString& format(const Formattable& obj,
- UnicodeString& toAppendTo,
- FieldPosition& pos,
- UErrorCode& status) const;
- /**
- * Redeclared Format method.
- * @param obj the object to be formatted.
- * @param result Output param which will receive the formatted string.
- * @param status Output param set to success/failure code
- * @return A reference to 'result'.
- * @stable ICU 2.0
- */
- UnicodeString& format(const Formattable& obj,
- UnicodeString& result,
- UErrorCode& status) const;
+protected:
+ /**
+ * Format a decimal number.
+ * The number is a DigitList wrapper onto a floating point decimal number.
+ * The default implementation in NumberFormat converts the decimal number
+ * to a double and formats that. Subclasses of NumberFormat that want
+ * to specifically handle big decimal numbers must override this method.
+ * class DecimalFormat does so.
+ *
+ * @param number The number, a DigitList format Decimal Floating Point.
+ * @param appendTo Output parameter to receive result.
+ * Result is appended to existing contents.
+ * @param posIter On return, can be used to iterate over positions
+ * of fields generated by this format call.
+ * @param status Output param filled with success/failure status.
+ * @return Reference to 'appendTo' parameter.
+ * @internal
+ */
+ virtual UnicodeString& format(const DigitList &number,
+ UnicodeString& appendTo,
+ FieldPositionIterator* posIter,
+ UErrorCode& status) const;
- /**
- * Redeclared NumberFormat method.
- * @param number the double value to be formatted.
- * @param output Output param which will receive the formatted string.
- * @return A reference to 'output'.
- * @stable ICU 2.0
- */
- UnicodeString& format(double number,
- UnicodeString& output) const;
+ /**
+ * Format a decimal number.
+ * The number is a DigitList wrapper onto a floating point decimal number.
+ * The default implementation in NumberFormat converts the decimal number
+ * to a double and formats that. Subclasses of NumberFormat that want
+ * to specifically handle big decimal numbers must override this method.
+ * class DecimalFormat does so.
+ *
+ * @param number The number, a DigitList format Decimal Floating Point.
+ * @param appendTo Output parameter to receive result.
+ * Result is appended to existing contents.
+ * @param pos On input: an alignment field, if desired.
+ * On output: the offsets of the alignment field.
+ * @param status Output param filled with success/failure status.
+ * @return Reference to 'appendTo' parameter.
+ * @internal
+ */
+ virtual UnicodeString& format(const DigitList &number,
+ UnicodeString& appendTo,
+ FieldPosition& pos,
+ UErrorCode& status) const;
+public:
- /**
- * Redeclared NumberFormat method.
- * @param number the long value to be formatted.
- * @param output Output param which will receive the formatted string.
- * @return A reference to 'output'.
- * @stable ICU 2.0
- */
- UnicodeString& format(int32_t number,
- UnicodeString& output) const;
+ using NumberFormat::parse;
/**
* Parses the specfied string, beginning at the specified position, according
* @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
- * @param result the result of the parse, either a double or a long.
- * @param status Output param set to failure code when a problem occurs.
- * @stable ICU 2.0
- */
- virtual inline void parse(const UnicodeString& text,
- Formattable& result,
- UErrorCode& status) const;
-
#if !UCONFIG_NO_COLLATION
/**
* 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;
* 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
- * @draft ICU 3.0
+ * @stable ICU 3.0
*/
virtual UnicodeString getDefaultRuleSetName() const;
+ /**
+ * Set a particular UDisplayContext value in the formatter, such as
+ * UDISPCTX_CAPITALIZATION_FOR_STANDALONE. Note: For getContext, see
+ * NumberFormat.
+ * @param value The UDisplayContext value to set.
+ * @param status Input/output status. If at entry this indicates a failure
+ * status, the function will do nothing; otherwise this will be
+ * updated with any new status from the function.
+ * @stable ICU 53
+ */
+ virtual void setContext(UDisplayContext value, UErrorCode& status);
+
public:
/**
* ICU "poor man's RTTI", returns a UClassID for this class.
*/
virtual UClassID getDynamicClassID(void) const;
+ /**
+ * Sets the decimal format symbols, which is generally not changed
+ * by the programmer or user. The formatter takes ownership of
+ * symbolsToAdopt; the client must not delete it.
+ *
+ * @param symbolsToAdopt DecimalFormatSymbols to be adopted.
+ * @stable ICU 49
+ */
+ virtual void adoptDecimalFormatSymbols(DecimalFormatSymbols* symbolsToAdopt);
+
+ /**
+ * Sets the decimal format symbols, which is generally not changed
+ * by the programmer or user. A clone of the symbols is created and
+ * the symbols is _not_ adopted; the client is still responsible for
+ * deleting it.
+ *
+ * @param symbols DecimalFormatSymbols.
+ * @stable ICU 49
+ */
+ virtual void setDecimalFormatSymbols(const DecimalFormatSymbols& symbols);
+
private:
RuleBasedNumberFormat(); // default constructor not implemented
// this will ref the localizations if they are not NULL
- // caller must deref to get adoption
- RuleBasedNumberFormat(const UnicodeString& description, LocalizationInfo* localizations,
+ // 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 initCapitalizationContextInfo(const Locale& thelocale);
void dispose();
void stripWhitespace(UnicodeString& src);
void initDefaultRuleSet();
/* friend access */
friend class NFSubstitution;
friend class NFRule;
+ friend class NFRuleSet;
friend class FractionalPartSubstitution;
inline NFRuleSet * getDefaultRuleSet() const;
- Collator * getCollator() const;
- DecimalFormatSymbols * getDecimalFormatSymbols() const;
+ const RuleBasedCollator * getCollator() const;
+ DecimalFormatSymbols * initializeDecimalFormatSymbols(UErrorCode &status);
+ const DecimalFormatSymbols * getDecimalFormatSymbols() const;
+ NFRule * initializeDefaultInfinityRule(UErrorCode &status);
+ const NFRule * getDefaultInfinityRule() const;
+ NFRule * initializeDefaultNaNRule(UErrorCode &status);
+ const NFRule * getDefaultNaNRule() const;
+ PluralFormat *createPluralFormat(UPluralType pluralType, const UnicodeString &pattern, UErrorCode& status) const;
+ UnicodeString& adjustForCapitalizationContext(int32_t startPos, UnicodeString& currentResult, UErrorCode& status) const;
+ UnicodeString& format(int64_t number, NFRuleSet *ruleSet, UnicodeString& toAppendTo, UErrorCode& status) const;
private:
NFRuleSet **ruleSets;
+ UnicodeString* ruleSetDescriptions;
+ int32_t numRuleSets;
NFRuleSet *defaultRuleSet;
Locale locale;
- Collator* collator;
+ RuleBasedCollator* collator;
DecimalFormatSymbols* decimalFormatSymbols;
+ NFRule *defaultInfinityRule;
+ NFRule *defaultNaNRule;
UBool lenient;
UnicodeString* lenientParseRules;
LocalizationInfo* localizations;
+ UnicodeString originalDescription;
+ UBool capitalizationInfoSet;
+ UBool capitalizationForUIListMenu;
+ UBool capitalizationForStandAlone;
+ BreakIterator* capitalizationBrkIter;
};
// ---------------
-inline UnicodeString&
-RuleBasedNumberFormat::format(const Formattable& obj,
- UnicodeString& result,
- UErrorCode& status) const
-{
- // Don't use Format:: - use immediate base class only,
- // in case immediate base modifies behavior later.
- // dlf - the above comment is bogus, if there were a reason to modify
- // it, it would be virtual, and there's no reason because it is
- // a one-line macro in NumberFormat anyway, just like this one.
- return NumberFormat::format(obj, result, status);
-}
-
-inline UnicodeString&
-RuleBasedNumberFormat::format(double number, UnicodeString& output) const {
- FieldPosition pos(0);
- return format(number, output, pos);
-}
-
-inline UnicodeString&
-RuleBasedNumberFormat::format(int32_t number, UnicodeString& output) const {
- FieldPosition pos(0);
- return format(number, output, pos);
-}
-
-inline void
-RuleBasedNumberFormat::parse(const UnicodeString& text, Formattable& result, UErrorCode& status) const
-{
- NumberFormat::parse(text, result, status);
-}
-
#if !UCONFIG_NO_COLLATION
inline UBool
}
U_NAMESPACE_END
+#endif // U_SHOW_CPLUSPLUS_API
/* U_HAVE_RBNF */
#endif