]> git.saurik.com Git - apple/icu.git/blame_incremental - icuSources/i18n/digitaffixesandpadding.h
ICU-59180.0.1.tar.gz
[apple/icu.git] / icuSources / i18n / digitaffixesandpadding.h
... / ...
CommitLineData
1// © 2016 and later: Unicode, Inc. and others.
2// License & terms of use: http://www.unicode.org/copyright.html
3/*
4*******************************************************************************
5* Copyright (C) 2015, International Business Machines
6* Corporation and others. All Rights Reserved.
7*******************************************************************************
8* digitaffixesandpadding.h
9*
10* created on: 2015jan06
11* created by: Travis Keep
12*/
13
14#ifndef __DIGITAFFIXESANDPADDING_H__
15#define __DIGITAFFIXESANDPADDING_H__
16
17#include "unicode/utypes.h"
18
19#if !UCONFIG_NO_FORMATTING
20
21#include "unicode/uobject.h"
22#include "pluralaffix.h"
23
24U_NAMESPACE_BEGIN
25
26class DigitList;
27class ValueFormatter;
28class UnicodeString;
29class FieldPositionHandler;
30class PluralRules;
31class VisibleDigitsWithExponent;
32
33/**
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.
38 */
39class U_I18N_API DigitAffixesAndPadding : public UMemory {
40public:
41
42/**
43 * Equivalent to DecimalFormat EPadPosition, but redeclared here to prevent
44 * depending on DecimalFormat which would cause a circular dependency.
45 */
46enum EPadPosition {
47 kPadBeforePrefix,
48 kPadAfterPrefix,
49 kPadBeforeSuffix,
50 kPadAfterSuffix
51};
52
53/**
54 * The positive prefix
55 */
56PluralAffix fPositivePrefix;
57
58/**
59 * The positive suffix
60 */
61PluralAffix fPositiveSuffix;
62
63/**
64 * The negative suffix
65 */
66PluralAffix fNegativePrefix;
67
68/**
69 * The negative suffix
70 */
71PluralAffix fNegativeSuffix;
72
73/**
74 * The padding position
75 */
76EPadPosition fPadPosition;
77
78/**
79 * The padding character.
80 */
81UChar32 fPadChar;
82
83/**
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
88 * inserts no padding.
89 */
90int32_t fWidth;
91
92/**
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.
96 */
97DigitAffixesAndPadding()
98 : fPadPosition(kPadBeforePrefix), fPadChar(0x2a), fWidth(0) { }
99
100/**
101 * Returns TRUE if this object is equal to rhs.
102 */
103UBool 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);
111}
112
113/**
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
116 * variations.
117 */
118UBool needsPluralRules() const;
119
120/**
121 * Formats value and appends to appendTo.
122 *
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.
130 */
131UnicodeString &format(
132 const VisibleDigitsWithExponent &value,
133 const ValueFormatter &formatter,
134 FieldPositionHandler &handler,
135 const PluralRules *optPluralRules,
136 UnicodeString &appendTo,
137 UErrorCode &status) const;
138
139/**
140 * For testing only.
141 */
142UnicodeString &format(
143 DigitList &value,
144 const ValueFormatter &formatter,
145 FieldPositionHandler &handler,
146 const PluralRules *optPluralRules,
147 UnicodeString &appendTo,
148 UErrorCode &status) const;
149
150/**
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.
154 *
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.
162 */
163UnicodeString &formatInt32(
164 int32_t value,
165 const ValueFormatter &formatter,
166 FieldPositionHandler &handler,
167 const PluralRules *optPluralRules,
168 UnicodeString &appendTo,
169 UErrorCode &status) const;
170
171private:
172UnicodeString &appendPadding(int32_t paddingCount, UnicodeString &appendTo) const;
173
174};
175
176
177U_NAMESPACE_END
178#endif /* #if !UCONFIG_NO_FORMATTING */
179#endif // __DIGITAFFIXANDPADDING_H__