1 /*
2 *******************************************************************************
3 * Copyright (C) 2015, International Business Machines
4 * Corporation and others.  All Rights Reserved.
5 *******************************************************************************
6 * digitaffixesandpadding.h
7 *
8 * created on: 2015jan06
9 * created by: Travis Keep
10 */
11 
12 #ifndef __DIGITAFFIXESANDPADDING_H__
13 #define __DIGITAFFIXESANDPADDING_H__
14 
15 #include "unicode/utypes.h"
16 
17 #if !UCONFIG_NO_FORMATTING
18 
19 #include "unicode/uobject.h"
20 #include "pluralaffix.h"
21 
22 U_NAMESPACE_BEGIN
23 
24 class DigitList;
25 class ValueFormatter;
26 class UnicodeString;
27 class FieldPositionHandler;
28 class PluralRules;
29 class VisibleDigitsWithExponent;
30 
31 /**
32  * A formatter of numbers. This class can format any numerical value
33  * except for not a number (NaN), positive infinity, and negative infinity.
34  * This class manages prefixes, suffixes, and padding but delegates the
35  * formatting of actual positive values to a ValueFormatter.
36  */
37 class U_I18N_API DigitAffixesAndPadding : public UMemory {
38 public:
39 
40 /**
41  * Equivalent to DecimalFormat EPadPosition, but redeclared here to prevent
42  * depending on DecimalFormat which would cause a circular dependency.
43  */
44 enum EPadPosition {
45     kPadBeforePrefix,
46     kPadAfterPrefix,
47     kPadBeforeSuffix,
48     kPadAfterSuffix
49 };
50 
51 /**
52  * The positive prefix
53  */
54 PluralAffix fPositivePrefix;
55 
56 /**
57  * The positive suffix
58  */
59 PluralAffix fPositiveSuffix;
60 
61 /**
62  * The negative suffix
63  */
64 PluralAffix fNegativePrefix;
65 
66 /**
67  * The negative suffix
68  */
69 PluralAffix fNegativeSuffix;
70 
71 /**
72  * The padding position
73  */
74 EPadPosition fPadPosition;
75 
76 /**
77  * The padding character.
78  */
79 UChar32 fPadChar;
80 
81 /**
82  * The field width in code points. The format method inserts instances of
83  * the padding character as needed in the desired padding position so that
84  * the entire formatted string contains this many code points. If the
85  * formatted string already exceeds this many code points, the format method
86  * inserts no padding.
87  */
88 int32_t fWidth;
89 
90 /**
91  * Pad position is before prefix; padding character is '*' field width is 0.
92  * The affixes are all the empty string with no annotated fields with just
93  * the 'other' plural variation.
94  */
DigitAffixesAndPadding()95 DigitAffixesAndPadding()
96         : fPadPosition(kPadBeforePrefix), fPadChar(0x2a), fWidth(0) { }
97 
98 /**
99  * Returns TRUE if this object is equal to rhs.
100  */
equals(const DigitAffixesAndPadding & rhs)101 UBool equals(const DigitAffixesAndPadding &rhs) const {
102     return (fPositivePrefix.equals(rhs.fPositivePrefix) &&
103             fPositiveSuffix.equals(rhs.fPositiveSuffix) &&
104             fNegativePrefix.equals(rhs.fNegativePrefix) &&
105             fNegativeSuffix.equals(rhs.fNegativeSuffix) &&
106             fPadPosition == rhs.fPadPosition &&
107             fWidth == rhs.fWidth &&
108             fPadChar == rhs.fPadChar);
109 }
110 
111 /**
112  * Returns TRUE if a plural rules instance is needed to complete the
113  * formatting by detecting if any of the affixes have multiple plural
114  * variations.
115  */
116 UBool needsPluralRules() const;
117 
118 /**
119  * Formats value and appends to appendTo.
120  *
121  * @param value the value to format. May be NaN or ininite.
122  * @param formatter handles the details of formatting the actual value.
123  * @param handler records field positions
124  * @param optPluralRules the plural rules, but may be NULL if
125  *   needsPluralRules returns FALSE.
126  * @appendTo formatted string appended here.
127  * @status any error returned here.
128  */
129 UnicodeString &format(
130         const VisibleDigitsWithExponent &value,
131         const ValueFormatter &formatter,
132         FieldPositionHandler &handler,
133         const PluralRules *optPluralRules,
134         UnicodeString &appendTo,
135         UErrorCode &status) const;
136 
137 /**
138  * For testing only.
139  */
140 UnicodeString &format(
141         DigitList &value,
142         const ValueFormatter &formatter,
143         FieldPositionHandler &handler,
144         const PluralRules *optPluralRules,
145         UnicodeString &appendTo,
146         UErrorCode &status) const;
147 
148 /**
149  * Formats a 32-bit integer and appends to appendTo. When formatting an
150  * integer, this method is preferred to plain format as it can run
151  * several times faster under certain conditions.
152  *
153  * @param value the value to format.
154  * @param formatter handles the details of formatting the actual value.
155  * @param handler records field positions
156  * @param optPluralRules the plural rules, but may be NULL if
157  *   needsPluralRules returns FALSE.
158  * @appendTo formatted string appended here.
159  * @status any error returned here.
160  */
161 UnicodeString &formatInt32(
162         int32_t value,
163         const ValueFormatter &formatter,
164         FieldPositionHandler &handler,
165         const PluralRules *optPluralRules,
166         UnicodeString &appendTo,
167         UErrorCode &status) const;
168 
169 private:
170 UnicodeString &appendPadding(int32_t paddingCount, UnicodeString &appendTo) const;
171 
172 };
173 
174 
175 U_NAMESPACE_END
176 #endif /* #if !UCONFIG_NO_FORMATTING */
177 #endif  // __DIGITAFFIXANDPADDING_H__
178