summaryrefslogtreecommitdiff
path: root/deps/icu-small/source/i18n/digitformatter.h
blob: 54a54c3639a629ddf630d39fb0082a3c2812794d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
// © 2016 and later: Unicode, Inc. and others.
// License & terms of use: http://www.unicode.org/copyright.html
/*
*******************************************************************************
* Copyright (C) 2015, International Business Machines
* Corporation and others.  All Rights Reserved.
*******************************************************************************
* digitformatter.h
*
* created on: 2015jan06
* created by: Travis Keep
*/

#ifndef __DIGITFORMATTER_H__
#define __DIGITFORMATTER_H__

#include "unicode/uobject.h"

#if !UCONFIG_NO_FORMATTING

#include "unicode/utypes.h"
#include "unicode/unistr.h"
#include "digitaffix.h"

U_NAMESPACE_BEGIN

class DecimalFormatSymbols;
class DigitList;
class DigitGrouping;
class DigitInterval;
class UnicodeString;
class FieldPositionHandler;
class IntDigitCountRange;
class VisibleDigits;
class VisibleDigitsWithExponent;

/**
 * Various options for formatting in fixed point.
 */
class U_I18N_API DigitFormatterOptions : public UMemory {
    public:
    DigitFormatterOptions() : fAlwaysShowDecimal(FALSE) { }

    /**
     * Returns TRUE if this object equals rhs.
     */
    UBool equals(const DigitFormatterOptions &rhs) const {
        return (
            fAlwaysShowDecimal == rhs.fAlwaysShowDecimal);
    }

    /**
     * Returns TRUE if these options allow for fast formatting of
     * integers.
     */
    UBool isFastFormattable() const {
        return (fAlwaysShowDecimal == FALSE);
    }

    /**
     * If TRUE, show the decimal separator even when there are no fraction
     * digits. default is FALSE.
     */
    UBool fAlwaysShowDecimal;
};

/**
 * Various options for formatting an integer.
 */
class U_I18N_API DigitFormatterIntOptions : public UMemory {
    public:
    DigitFormatterIntOptions() : fAlwaysShowSign(FALSE) { }

    /**
     * Returns TRUE if this object equals rhs.
     */
    UBool equals(const DigitFormatterIntOptions &rhs) const {
        return (fAlwaysShowSign == rhs.fAlwaysShowSign);
    }

    /**
     * If TRUE, always prefix the integer with its sign even if the number is
     * positive. Default is FALSE.
     */
    UBool fAlwaysShowSign;
};

/**
 * Options for formatting in scientific notation.
 */
class U_I18N_API SciFormatterOptions : public UMemory {
    public:

    /**
     * Returns TRUE if this object equals rhs.
     */
    UBool equals(const SciFormatterOptions &rhs) const {
        return (fMantissa.equals(rhs.fMantissa) &&
                fExponent.equals(rhs.fExponent));
    }

    /**
     * Options for formatting the mantissa.
     */
    DigitFormatterOptions fMantissa;

    /**
     * Options for formatting the exponent.
     */
    DigitFormatterIntOptions fExponent;
};


/**
 * Does fixed point formatting.
 *
 * This class only does fixed point formatting. It does no rounding before
 * formatting.
 */
class U_I18N_API DigitFormatter : public UMemory {
public:

/**
 * Decimal separator is period (.), Plus sign is plus (+),
 * minus sign is minus (-), grouping separator is comma (,), digits are 0-9.
 */
DigitFormatter();

/**
 * Let symbols determine the digits, decimal separator,
 * plus and mius sign, grouping separator, and possibly other settings.
 */
DigitFormatter(const DecimalFormatSymbols &symbols);

/**
 * Change what this instance uses for digits, decimal separator,
 * plus and mius sign, grouping separator, and possibly other settings
 * according to symbols.
 */
void setDecimalFormatSymbols(const DecimalFormatSymbols &symbols);

/**
 * Change what this instance uses for digits, decimal separator,
 * plus and mius sign, grouping separator, and possibly other settings
 * according to symbols in the context of monetary amounts.
 */
void setDecimalFormatSymbolsForMonetary(const DecimalFormatSymbols &symbols);

/**
 * Fixed point formatting.
 *
 * @param positiveDigits the value to format
 *  Negative sign can be present, but it won't show.
 * @param grouping controls how digit grouping is done
 * @param options formatting options
 * @param handler records field positions
 * @param appendTo formatted value appended here.
 * @return appendTo
 */
UnicodeString &format(
        const VisibleDigits &positiveDigits,
        const DigitGrouping &grouping,
        const DigitFormatterOptions &options,
        FieldPositionHandler &handler,
        UnicodeString &appendTo) const;

/**
 * formats in scientifc notation.
 * @param positiveDigits the value to format.
 *  Negative sign can be present, but it won't show.
 * @param options formatting options
 * @param handler records field positions.
 * @param appendTo formatted value appended here.
 */
UnicodeString &format(
        const VisibleDigitsWithExponent &positiveDigits,
        const SciFormatterOptions &options,
        FieldPositionHandler &handler,
        UnicodeString &appendTo) const;

/**
 * Fixed point formatting of integers.
 * Always performed with no grouping and no decimal point.
 *
 * @param positiveValue the value to format must be positive.
 * @param range specifies minimum and maximum number of digits.
 * @param handler records field positions
 * @param appendTo formatted value appended here.
 * @return appendTo
 */
UnicodeString &formatPositiveInt32(
        int32_t positiveValue,
        const IntDigitCountRange &range,
        FieldPositionHandler &handler,
        UnicodeString &appendTo) const;

/**
 * Counts how many code points are needed for fixed formatting.
 *   If digits is negative, the negative sign is not included in the count.
 */
int32_t countChar32(
        const VisibleDigits &digits,
        const DigitGrouping &grouping,
        const DigitFormatterOptions &options) const;

/**
 * Counts how many code points are needed for scientific formatting.
 *   If digits is negative, the negative sign is not included in the count.
 */
int32_t countChar32(
        const VisibleDigitsWithExponent &digits,
        const SciFormatterOptions &options) const;

/**
 * Returns TRUE if this object equals rhs.
 */
UBool equals(const DigitFormatter &rhs) const;

private:
UChar32 fLocalizedDigits[10];
UnicodeString fGroupingSeparator;
UnicodeString fDecimal;
UnicodeString fNegativeSign;
UnicodeString fPositiveSign;
DigitAffix fInfinity;
DigitAffix fNan;
UBool fIsStandardDigits;
UnicodeString fExponent;
UBool isStandardDigits() const;

UnicodeString &formatDigits(
        const uint8_t *digits,
        int32_t count,
        const IntDigitCountRange &range,
        int32_t intField,
        FieldPositionHandler &handler,
        UnicodeString &appendTo) const;

void setOtherDecimalFormatSymbols(const DecimalFormatSymbols &symbols);

int32_t countChar32(
        const VisibleDigits &exponent,
        const DigitInterval &mantissaInterval,
        const SciFormatterOptions &options) const;

UnicodeString &formatNaN(
        FieldPositionHandler &handler,
        UnicodeString &appendTo) const {
    return fNan.format(handler, appendTo);
}

int32_t countChar32ForNaN() const {
    return fNan.toString().countChar32();
}

UnicodeString &formatInfinity(
        FieldPositionHandler &handler,
        UnicodeString &appendTo) const {
    return fInfinity.format(handler, appendTo);
}

int32_t countChar32ForInfinity() const {
    return fInfinity.toString().countChar32();
}

UnicodeString &formatExponent(
        const VisibleDigits &digits,
        const DigitFormatterIntOptions &options,
        int32_t signField,
        int32_t intField,
        FieldPositionHandler &handler,
        UnicodeString &appendTo) const;

int32_t countChar32(
        const DigitGrouping &grouping,
        const DigitInterval &interval,
        const DigitFormatterOptions &options) const;

int32_t countChar32ForExponent(
        const VisibleDigits &exponent,
        const DigitFormatterIntOptions &options) const;

};


U_NAMESPACE_END
#endif /* #if !UCONFIG_NO_FORMATTING */
#endif  // __DIGITFORMATTER_H__