X-Git-Url: https://git.saurik.com/apple/icu.git/blobdiff_plain/57a6839dcb3bba09e8228b822b290604668416fe..a01113dcd0f39d5da295ef82785beff9ed86fe38:/icuSources/i18n/quantityformatter.h diff --git a/icuSources/i18n/quantityformatter.h b/icuSources/i18n/quantityformatter.h index 5dceaf65..046eec75 100644 --- a/icuSources/i18n/quantityformatter.h +++ b/icuSources/i18n/quantityformatter.h @@ -1,6 +1,8 @@ +// © 2016 and later: Unicode, Inc. and others. +// License & terms of use: http://www.unicode.org/copyright.html /* ****************************************************************************** -* Copyright (C) 2014, International Business Machines +* Copyright (C) 2014-2016, International Business Machines * Corporation and others. All Rights Reserved. ****************************************************************************** * quantityformatter.h @@ -12,15 +14,25 @@ #include "unicode/utypes.h" #include "unicode/uobject.h" +#if !UCONFIG_NO_FORMATTING + +#include "standardplural.h" + U_NAMESPACE_BEGIN -class SimplePatternFormatter; +class SimpleFormatter; class UnicodeString; class PluralRules; class NumberFormat; class Formattable; class FieldPosition; +namespace number { +namespace impl { +class NumberStringBuilder; +} +} + /** * A plural aware formatter that is good for expressing a single quantity and * a unit. @@ -35,7 +47,6 @@ class FieldPosition; * */ class U_I18N_API QuantityFormatter : public UMemory { -// TODO(Travis Keep): Add test for copy constructor, assignment, and reset. public: /** * Default constructor. @@ -63,17 +74,14 @@ public: void reset(); /** - * Adds a plural variant. - * - * @param variant "zero", "one", "two", "few", "many", "other" - * @param rawPattern the pattern for the variant e.g "{0} meters" - * @param status any error returned here. - * @return TRUE on success; FALSE otherwise. - */ - UBool add( - const char *variant, - const UnicodeString &rawPattern, - UErrorCode &status); + * Adds a plural variant if there is none yet for the plural form. + * + * @param variant "zero", "one", "two", "few", "many", "other" + * @param rawPattern the pattern for the variant e.g "{0} meters" + * @param status any error returned here. + * @return TRUE on success; FALSE if status was set to a non zero error. + */ + UBool addIfAbsent(const char *variant, const UnicodeString &rawPattern, UErrorCode &status); /** * returns TRUE if this object has at least the "other" variant. @@ -81,29 +89,82 @@ public: UBool isValid() const; /** - * Formats a quantity with this object appending the result to appendTo. + * Gets the pattern formatter that would be used for a particular variant. + * If isValid() returns TRUE, this method is guaranteed to return a + * non-NULL value. + */ + const SimpleFormatter *getByVariant(const char *variant) const; + + /** + * Formats a number with this object appending the result to appendTo. * At least the "other" variant must be added to this object for this * method to work. * - * @param quantity the single quantity. - * @param fmt formats the quantity + * @param number the single number. + * @param fmt formats the number * @param rules computes the plural variant to use. * @param appendTo result appended here. * @param status any error returned here. * @return appendTo */ UnicodeString &format( - const Formattable &quantity, + const Formattable &number, const NumberFormat &fmt, const PluralRules &rules, UnicodeString &appendTo, FieldPosition &pos, UErrorCode &status) const; + /** + * Selects the standard plural form for the number/formatter/rules. + * TODO(13591): Remove this method. + */ + static StandardPlural::Form selectPlural( + const Formattable &number, + const NumberFormat &fmt, + const PluralRules &rules, + UnicodeString &formattedNumber, + FieldPosition &pos, + UErrorCode &status); + + /** + * Formats a quantity and selects its plural form. The output is appended + * to a NumberStringBuilder in order to retain field information. + * + * @param quantity The number to format. + * @param fmt The formatter to use to format the number. + * @param rules The rules to use to select the plural form of the + * formatted number. + * @param output Where to append the result of the format operation. + * @param pluralForm Output variable populated with the plural form of the + * formatted number. + * @param status Set if an error occurs. + */ + static void formatAndSelect( + double quantity, + const NumberFormat& fmt, + const PluralRules& rules, + number::impl::NumberStringBuilder& output, + StandardPlural::Form& pluralForm, + UErrorCode& status); + + /** + * Formats the pattern with the value and adjusts the FieldPosition. + * TODO: Remove? + */ + static UnicodeString &format( + const SimpleFormatter &pattern, + const UnicodeString &value, + UnicodeString &appendTo, + FieldPosition &pos, + UErrorCode &status); + private: - SimplePatternFormatter *formatters[6]; + SimpleFormatter *formatters[StandardPlural::COUNT]; }; U_NAMESPACE_END #endif + +#endif