aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/icu/i18n/number_simple.cpp
blob: 7daeb66dc2afba71c32218607fc29d1fcd2dda81 (plain) (blame)
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
// © 2017 and later: Unicode, Inc. and others.
// License & terms of use: http://www.unicode.org/copyright.html

#include "unicode/utypes.h"

#if !UCONFIG_NO_FORMATTING

#include "unicode/numberformatter.h"
#include "unicode/simplenumberformatter.h"
#include "number_formatimpl.h"
#include "number_utils.h"
#include "number_patternmodifier.h"
#include "number_utypes.h"

using namespace icu;
using namespace icu::number;
using namespace icu::number::impl;


SimpleNumber
SimpleNumber::forInt64(int64_t value, UErrorCode& status) {
    if (U_FAILURE(status)) {
        return {};
    }
    auto* results = new UFormattedNumberData();
    if (results == nullptr) {
        status = U_MEMORY_ALLOCATION_ERROR;
        return {};
    }
    results->quantity.setToLong(value);
    return SimpleNumber(results, status);
}

SimpleNumber::SimpleNumber(UFormattedNumberData* data, UErrorCode& status) : fData(data) {
    if (U_FAILURE(status)) {
        return;
    }
    if (fData == nullptr) {
        status = U_ILLEGAL_ARGUMENT_ERROR;
        return;
    }
    if (fData->quantity.isNegative()) {
        fSign = UNUM_SIMPLE_NUMBER_MINUS_SIGN;
    } else {
        fSign = UNUM_SIMPLE_NUMBER_NO_SIGN;
    }
}

void SimpleNumber::cleanup() {
    delete fData;
    fData = nullptr;
}

void SimpleNumber::multiplyByPowerOfTen(int32_t power, UErrorCode& status) {
    if (U_FAILURE(status)) {
        return;
    }
    if (fData == nullptr) {
        status = U_INVALID_STATE_ERROR;
        return;
    }
    fData->quantity.adjustMagnitude(power);
}

void SimpleNumber::roundTo(int32_t position, UNumberFormatRoundingMode roundingMode, UErrorCode& status) {
    if (U_FAILURE(status)) {
        return;
    }
    if (fData == nullptr) {
        status = U_INVALID_STATE_ERROR;
        return;
    }
    fData->quantity.roundToMagnitude(position, roundingMode, status);
}

void SimpleNumber::setMinimumIntegerDigits(uint32_t position, UErrorCode& status) {
    if (U_FAILURE(status)) {
        return;
    }
    if (fData == nullptr) {
        status = U_INVALID_STATE_ERROR;
        return;
    }
    fData->quantity.decreaseMinIntegerTo(position);
    fData->quantity.increaseMinIntegerTo(position);
}

void SimpleNumber::setMinimumFractionDigits(uint32_t position, UErrorCode& status) {
    if (U_FAILURE(status)) {
        return;
    }
    if (fData == nullptr) {
        status = U_INVALID_STATE_ERROR;
        return;
    }
    fData->quantity.setMinFraction(position);
}

void SimpleNumber::setMaximumIntegerDigits(uint32_t position, UErrorCode& status) {
    if (U_FAILURE(status)) {
        return;
    }
    if (fData == nullptr) {
        status = U_INVALID_STATE_ERROR;
        return;
    }
    fData->quantity.decreaseMinIntegerTo(position);
    fData->quantity.applyMaxInteger(position);
}

void SimpleNumber::truncateStart(uint32_t position, UErrorCode& status) {
    setMaximumIntegerDigits(position, status);
}

void SimpleNumber::setSign(USimpleNumberSign sign, UErrorCode& status) {
    if (U_FAILURE(status)) {
        return;
    }
    if (fData == nullptr) {
        status = U_INVALID_STATE_ERROR;
        return;
    }
    fSign = sign;
}


void SimpleNumberFormatter::cleanup() {
    delete fOwnedSymbols;
    delete fMicros;
    delete fPatternModifier;
    fOwnedSymbols = nullptr;
    fMicros = nullptr;
    fPatternModifier = nullptr;
}

SimpleNumberFormatter SimpleNumberFormatter::forLocale(const icu::Locale &locale, UErrorCode &status) {
    return SimpleNumberFormatter::forLocaleAndGroupingStrategy(locale, UNUM_GROUPING_AUTO, status);
}

SimpleNumberFormatter SimpleNumberFormatter::forLocaleAndGroupingStrategy(
        const icu::Locale &locale,
        UNumberGroupingStrategy groupingStrategy,
        UErrorCode &status) {
    SimpleNumberFormatter retval;
    retval.fOwnedSymbols = new DecimalFormatSymbols(locale, status);
    if (U_FAILURE(status)) {
        return retval;
    }
    if (retval.fOwnedSymbols == nullptr) {
        status = U_MEMORY_ALLOCATION_ERROR;
        return retval;
    }
    retval.initialize(locale, *retval.fOwnedSymbols, groupingStrategy, status);
    return retval;
}


SimpleNumberFormatter SimpleNumberFormatter::forLocaleAndSymbolsAndGroupingStrategy(
        const icu::Locale &locale,
        const DecimalFormatSymbols &symbols,
        UNumberGroupingStrategy groupingStrategy,
        UErrorCode &status) {
    SimpleNumberFormatter retval;
    retval.initialize(locale, symbols, groupingStrategy, status);
    return retval;
}


void SimpleNumberFormatter::initialize(
        const icu::Locale &locale,
        const DecimalFormatSymbols &symbols,
        UNumberGroupingStrategy groupingStrategy,
        UErrorCode &status) {
    if (U_FAILURE(status)) {
        return;
    }

    fMicros = new SimpleMicroProps();
    if (fMicros == nullptr) {
        status = U_MEMORY_ALLOCATION_ERROR;
        return;
    }
    fMicros->symbols = &symbols;

    const auto* pattern = utils::getPatternForStyle(
        locale,
        symbols.getNumberingSystemName(),
        CLDR_PATTERN_STYLE_DECIMAL,
        status);
    if (U_FAILURE(status)) {
        return;
    }

    ParsedPatternInfo patternInfo;
    PatternParser::parseToPatternInfo(UnicodeString(pattern), patternInfo, status);
    if (U_FAILURE(status)) {
        return;
    }

    auto grouper = Grouper::forStrategy(groupingStrategy);
    grouper.setLocaleData(patternInfo, locale);
    fMicros->grouping = grouper;

    MutablePatternModifier patternModifier(false);
    patternModifier.setPatternInfo(&patternInfo, kUndefinedField);
    patternModifier.setPatternAttributes(UNUM_SIGN_EXCEPT_ZERO, false, false);
    patternModifier.setSymbols(fMicros->symbols, {}, UNUM_UNIT_WIDTH_SHORT, nullptr, status);

    fPatternModifier = new AdoptingSignumModifierStore(patternModifier.createImmutableForPlural(StandardPlural::COUNT, status));

    fGroupingStrategy = groupingStrategy;
}

FormattedNumber SimpleNumberFormatter::format(SimpleNumber value, UErrorCode &status) const {
    formatImpl(value.fData, value.fSign, status);

    // Do not save the results object if we encountered a failure.
    if (U_SUCCESS(status)) {
        auto* temp = value.fData;
        value.fData = nullptr;
        return FormattedNumber(temp);
    } else {
        return FormattedNumber(status);
    }
}

void SimpleNumberFormatter::formatImpl(UFormattedNumberData* data, USimpleNumberSign sign, UErrorCode &status) const {
    if (U_FAILURE(status)) {
        return;
    }
    if (data == nullptr) {
        status = U_ILLEGAL_ARGUMENT_ERROR;
        return;
    }
    if (fPatternModifier == nullptr || fMicros == nullptr) {
        status = U_INVALID_STATE_ERROR;
        return;
    }

    Signum signum;
    if (sign == UNUM_SIMPLE_NUMBER_MINUS_SIGN) {
        signum = SIGNUM_NEG;
    } else if (sign == UNUM_SIMPLE_NUMBER_PLUS_SIGN) {
        signum = SIGNUM_POS;
    } else {
        signum = SIGNUM_POS_ZERO;
    }

    const Modifier* modifier = (*fPatternModifier)[signum];
    auto length = NumberFormatterImpl::writeNumber(
        *fMicros,
        data->quantity,
        data->getStringRef(),
        0,
        status);
    length += modifier->apply(data->getStringRef(), 0, length, status);
    data->getStringRef().writeTerminator(status);
}

#endif /* #if !UCONFIG_NO_FORMATTING */