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

#include "unicode/utypes.h"

#if !UCONFIG_NO_FORMATTING

// Allow implicit conversion from char16_t* to UnicodeString for this file:
// Helpful in toString methods and elsewhere.
#define UNISTR_FROM_STRING_EXPLICIT

#include "fphdlimp.h"
#include "number_utypes.h"
#include "numparse_types.h"
#include "formattedval_impl.h"
#include "numrange_impl.h"
#include "number_decnum.h"
#include "unicode/numberrangeformatter.h"
#include "unicode/unumberrangeformatter.h"

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


U_NAMESPACE_BEGIN
namespace number {
namespace impl {

/**
 * Implementation class for UNumberRangeFormatter. Wraps a LocalizedRangeNumberFormatter.
 */
struct UNumberRangeFormatterData : public UMemory,
        // Magic number as ASCII == "NRF" (NumberRangeFormatter)
        public IcuCApiHelper<UNumberRangeFormatter, UNumberRangeFormatterData, 0x4E524600> {
    LocalizedNumberRangeFormatter fFormatter;
};

struct UFormattedNumberRangeImpl;

// Magic number as ASCII == "FDN" (FormatteDNumber)
typedef IcuCApiHelper<UFormattedNumberRange, UFormattedNumberRangeImpl, 0x46444E00> UFormattedNumberRangeApiHelper;

struct UFormattedNumberRangeImpl : public UFormattedValueImpl, public UFormattedNumberRangeApiHelper {
    UFormattedNumberRangeImpl();
    ~UFormattedNumberRangeImpl();

    FormattedNumberRange fImpl;
    UFormattedNumberRangeData fData;
};

UFormattedNumberRangeImpl::UFormattedNumberRangeImpl()
        : fImpl(&fData) {
    fFormattedValue = &fImpl;
}

UFormattedNumberRangeImpl::~UFormattedNumberRangeImpl() {
    // Disown the data from fImpl so it doesn't get deleted twice
    fImpl.fData = nullptr;
}

} // namespace impl
} // namespace number
U_NAMESPACE_END


UPRV_FORMATTED_VALUE_CAPI_NO_IMPLTYPE_AUTO_IMPL(
    UFormattedNumberRange,
    UFormattedNumberRangeImpl,
    UFormattedNumberRangeApiHelper,
    unumrf)


const UFormattedNumberRangeData* number::impl::validateUFormattedNumberRange(
        const UFormattedNumberRange* uresult, UErrorCode& status) {
    auto* result = UFormattedNumberRangeApiHelper::validate(uresult, status);
    if (U_FAILURE(status)) {
        return nullptr;
    }
    return &result->fData;
}


U_CAPI UNumberRangeFormatter* U_EXPORT2
unumrf_openForSkeletonWithCollapseAndIdentityFallback(
        const UChar* skeleton,
        int32_t skeletonLen,
        UNumberRangeCollapse collapse,
        UNumberRangeIdentityFallback identityFallback,
        const char* locale,
        UParseError* perror,
        UErrorCode* ec) {
    auto* impl = new UNumberRangeFormatterData();
    if (impl == nullptr) {
        *ec = U_MEMORY_ALLOCATION_ERROR;
        return nullptr;
    }
    // Readonly-alias constructor (first argument is whether we are NUL-terminated)
    UnicodeString skeletonString(skeletonLen == -1, skeleton, skeletonLen);
    impl->fFormatter = NumberRangeFormatter::withLocale(locale)
        .numberFormatterBoth(NumberFormatter::forSkeleton(skeletonString, *perror, *ec))
        .collapse(collapse)
        .identityFallback(identityFallback);
    return impl->exportForC();
}

U_CAPI void U_EXPORT2
unumrf_formatDoubleRange(
        const UNumberRangeFormatter* uformatter,
        double first,
        double second,
        UFormattedNumberRange* uresult,
        UErrorCode* ec) {
    const UNumberRangeFormatterData* formatter = UNumberRangeFormatterData::validate(uformatter, *ec);
    auto* result = UFormattedNumberRangeApiHelper::validate(uresult, *ec);
    if (U_FAILURE(*ec)) { return; }

    result->fData.resetString();
    result->fData.quantity1.clear();
    result->fData.quantity2.clear();
    result->fData.quantity1.setToDouble(first);
    result->fData.quantity2.setToDouble(second);
    formatter->fFormatter.formatImpl(result->fData, first == second, *ec);
}

U_CAPI void U_EXPORT2
unumrf_formatDecimalRange(
        const UNumberRangeFormatter* uformatter,
        const char* first, int32_t firstLen,
        const char* second, int32_t secondLen,
        UFormattedNumberRange* uresult,
        UErrorCode* ec) {
    const UNumberRangeFormatterData* formatter = UNumberRangeFormatterData::validate(uformatter, *ec);
    auto* result = UFormattedNumberRangeApiHelper::validate(uresult, *ec);
    if (U_FAILURE(*ec)) { return; }

    result->fData.resetString();
    result->fData.quantity1.clear();
    result->fData.quantity2.clear();
    result->fData.quantity1.setToDecNumber({first, firstLen}, *ec);
    result->fData.quantity2.setToDecNumber({second, secondLen}, *ec);
    formatter->fFormatter.formatImpl(result->fData, first == second, *ec);
}

U_CAPI UNumberRangeIdentityResult U_EXPORT2
unumrf_resultGetIdentityResult(
        const UFormattedNumberRange* uresult,
        UErrorCode* ec) {
    auto* result = UFormattedNumberRangeApiHelper::validate(uresult, *ec);
    if (U_FAILURE(*ec)) {
        return UNUM_IDENTITY_RESULT_COUNT;
    }
    return result->fData.identityResult;
}

U_CAPI int32_t U_EXPORT2
unumrf_resultGetFirstDecimalNumber(
        const UFormattedNumberRange* uresult,
        char* dest,
        int32_t destCapacity,
        UErrorCode* ec) {
    const auto* result = UFormattedNumberRangeApiHelper::validate(uresult, *ec);
    if (U_FAILURE(*ec)) {
        return 0;
    }
    DecNum decnum;
    return result->fData.quantity1.toDecNum(decnum, *ec)
        .toCharString(*ec)
        .extract(dest, destCapacity, *ec);
}

U_CAPI int32_t U_EXPORT2
unumrf_resultGetSecondDecimalNumber(
        const UFormattedNumberRange* uresult,
        char* dest,
        int32_t destCapacity,
        UErrorCode* ec) {
    const auto* result = UFormattedNumberRangeApiHelper::validate(uresult, *ec);
    if (U_FAILURE(*ec)) {
        return 0;
    }
    DecNum decnum;
    return result->fData.quantity2
        .toDecNum(decnum, *ec)
        .toCharString(*ec)
        .extract(dest, destCapacity, *ec);
}

U_CAPI void U_EXPORT2
unumrf_close(UNumberRangeFormatter* f) {
    UErrorCode localStatus = U_ZERO_ERROR;
    const UNumberRangeFormatterData* impl = UNumberRangeFormatterData::validate(f, localStatus);
    delete impl;
}


#endif /* #if !UCONFIG_NO_FORMATTING */