1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 *******************************************************************************
5 * Copyright (C) 2015, International Business Machines
6 * Corporation and others. All Rights Reserved.
7 *******************************************************************************
8 * digitaffixesandpadding.h
10 * created on: 2015jan06
11 * created by: Travis Keep
14 #ifndef __DIGITAFFIXESANDPADDING_H__
15 #define __DIGITAFFIXESANDPADDING_H__
17 #include "unicode/utypes.h"
19 #if !UCONFIG_NO_FORMATTING
21 #include "unicode/uobject.h"
22 #include "pluralaffix.h"
29 class FieldPositionHandler
;
31 class VisibleDigitsWithExponent
;
34 * A formatter of numbers. This class can format any numerical value
35 * except for not a number (NaN), positive infinity, and negative infinity.
36 * This class manages prefixes, suffixes, and padding but delegates the
37 * formatting of actual positive values to a ValueFormatter.
39 class U_I18N_API DigitAffixesAndPadding
: public UMemory
{
43 * Equivalent to DecimalFormat EPadPosition, but redeclared here to prevent
44 * depending on DecimalFormat which would cause a circular dependency.
56 PluralAffix fPositivePrefix
;
61 PluralAffix fPositiveSuffix
;
66 PluralAffix fNegativePrefix
;
71 PluralAffix fNegativeSuffix
;
74 * The padding position
76 EPadPosition fPadPosition
;
79 * The padding character.
84 * The field width in code points. The format method inserts instances of
85 * the padding character as needed in the desired padding position so that
86 * the entire formatted string contains this many code points. If the
87 * formatted string already exceeds this many code points, the format method
93 * Pad position is before prefix; padding character is '*' field width is 0.
94 * The affixes are all the empty string with no annotated fields with just
95 * the 'other' plural variation.
97 DigitAffixesAndPadding()
98 : fPadPosition(kPadBeforePrefix
), fPadChar(0x2a), fWidth(0) { }
101 * Returns TRUE if this object is equal to rhs.
103 UBool
equals(const DigitAffixesAndPadding
&rhs
) const {
104 return (fPositivePrefix
.equals(rhs
.fPositivePrefix
) &&
105 fPositiveSuffix
.equals(rhs
.fPositiveSuffix
) &&
106 fNegativePrefix
.equals(rhs
.fNegativePrefix
) &&
107 fNegativeSuffix
.equals(rhs
.fNegativeSuffix
) &&
108 fPadPosition
== rhs
.fPadPosition
&&
109 fWidth
== rhs
.fWidth
&&
110 fPadChar
== rhs
.fPadChar
);
114 * Returns TRUE if a plural rules instance is needed to complete the
115 * formatting by detecting if any of the affixes have multiple plural
118 UBool
needsPluralRules() const;
121 * Formats value and appends to appendTo.
123 * @param value the value to format. May be NaN or ininite.
124 * @param formatter handles the details of formatting the actual value.
125 * @param handler records field positions
126 * @param optPluralRules the plural rules, but may be NULL if
127 * needsPluralRules returns FALSE.
128 * @appendTo formatted string appended here.
129 * @status any error returned here.
131 UnicodeString
&format(
132 const VisibleDigitsWithExponent
&value
,
133 const ValueFormatter
&formatter
,
134 FieldPositionHandler
&handler
,
135 const PluralRules
*optPluralRules
,
136 UnicodeString
&appendTo
,
137 UErrorCode
&status
) const;
142 UnicodeString
&format(
144 const ValueFormatter
&formatter
,
145 FieldPositionHandler
&handler
,
146 const PluralRules
*optPluralRules
,
147 UnicodeString
&appendTo
,
148 UErrorCode
&status
) const;
151 * Formats a 32-bit integer and appends to appendTo. When formatting an
152 * integer, this method is preferred to plain format as it can run
153 * several times faster under certain conditions.
155 * @param value the value to format.
156 * @param formatter handles the details of formatting the actual value.
157 * @param handler records field positions
158 * @param optPluralRules the plural rules, but may be NULL if
159 * needsPluralRules returns FALSE.
160 * @appendTo formatted string appended here.
161 * @status any error returned here.
163 UnicodeString
&formatInt32(
165 const ValueFormatter
&formatter
,
166 FieldPositionHandler
&handler
,
167 const PluralRules
*optPluralRules
,
168 UnicodeString
&appendTo
,
169 UErrorCode
&status
) const;
172 UnicodeString
&appendPadding(int32_t paddingCount
, UnicodeString
&appendTo
) const;
178 #endif /* #if !UCONFIG_NO_FORMATTING */
179 #endif // __DIGITAFFIXANDPADDING_H__