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
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "mozilla/intl/NumberFormat.h"
#include "NumberFormatFields.h"
#include "NumberFormatterSkeleton.h"
#include "ScopedICUObject.h"
#include "unicode/unumberformatter.h"
#include "unicode/upluralrules.h"
namespace mozilla::intl {
/*static*/ Result<UniquePtr<NumberFormat>, ICUError> NumberFormat::TryCreate(
std::string_view aLocale, const NumberFormatOptions& aOptions) {
UniquePtr<NumberFormat> nf = MakeUnique<NumberFormat>();
Result<Ok, ICUError> result = nf->initialize(aLocale, aOptions);
if (result.isOk()) {
return nf;
}
return Err(result.unwrapErr());
}
NumberFormat::~NumberFormat() {
if (mFormattedNumber) {
unumf_closeResult(mFormattedNumber);
}
if (mNumberFormatter) {
unumf_close(mNumberFormatter);
}
}
Result<Ok, ICUError> NumberFormat::initialize(
std::string_view aLocale, const NumberFormatOptions& aOptions) {
mFormatForUnit = aOptions.mUnit.isSome();
NumberFormatterSkeleton skeleton(aOptions);
mNumberFormatter = skeleton.toFormatter(aLocale);
if (mNumberFormatter) {
UErrorCode status = U_ZERO_ERROR;
mFormattedNumber = unumf_openResult(&status);
if (U_FAILURE(status)) {
return Err(ToICUError(status));
}
return Ok();
}
return Err(ICUError::InternalError);
}
Result<std::u16string_view, ICUError> NumberFormat::formatToParts(
double number, NumberPartVector& parts) const {
if (!formatInternal(number)) {
return Err(ICUError::InternalError);
}
bool isNegative = !std::isnan(number) && IsNegative(number);
return FormatResultToParts(mFormattedNumber, Some(number), isNegative,
mFormatForUnit, parts);
}
Result<std::u16string_view, ICUError> NumberFormat::formatToParts(
int64_t number, NumberPartVector& parts) const {
if (!formatInternal(number)) {
return Err(ICUError::InternalError);
}
return FormatResultToParts(mFormattedNumber, Nothing(), number < 0,
mFormatForUnit, parts);
}
Result<std::u16string_view, ICUError> NumberFormat::formatToParts(
std::string_view number, NumberPartVector& parts) const {
if (!formatInternal(number)) {
return Err(ICUError::InternalError);
}
// Non-finite numbers aren't currently supported here. If we ever need to
// support those, the |Maybe<double>| argument must be computed here.
MOZ_ASSERT(number != "Infinity");
MOZ_ASSERT(number != "+Infinity");
MOZ_ASSERT(number != "-Infinity");
MOZ_ASSERT(number != "NaN");
bool isNegative = !number.empty() && number[0] == '-';
return FormatResultToParts(mFormattedNumber, Nothing(), isNegative,
mFormatForUnit, parts);
}
Result<int32_t, ICUError> NumberFormat::selectFormatted(
double number, char16_t* keyword, int32_t keywordSize,
UPluralRules* pluralRules) const {
MOZ_ASSERT(keyword && pluralRules);
UErrorCode status = U_ZERO_ERROR;
MOZ_TRY(format(number));
int32_t utf16KeywordLength = uplrules_selectFormatted(
pluralRules, mFormattedNumber, keyword, keywordSize, &status);
if (U_FAILURE(status)) {
return Err(ToICUError(status));
}
return utf16KeywordLength;
}
bool NumberFormat::formatInternal(double number) const {
// ICU incorrectly formats NaN values with the sign bit set, as if they
// were negative. Replace all NaNs with a single pattern with sign bit
// unset ("positive", that is) until ICU is fixed.
if (MOZ_UNLIKELY(std::isnan(number))) {
number = SpecificNaN<double>(0, 1);
}
UErrorCode status = U_ZERO_ERROR;
unumf_formatDouble(mNumberFormatter, number, mFormattedNumber, &status);
return U_SUCCESS(status);
}
bool NumberFormat::formatInternal(int64_t number) const {
UErrorCode status = U_ZERO_ERROR;
unumf_formatInt(mNumberFormatter, number, mFormattedNumber, &status);
return U_SUCCESS(status);
}
bool NumberFormat::formatInternal(std::string_view number) const {
UErrorCode status = U_ZERO_ERROR;
unumf_formatDecimal(mNumberFormatter, number.data(), number.size(),
mFormattedNumber, &status);
return U_SUCCESS(status);
}
Result<std::u16string_view, ICUError> NumberFormat::formatResult() const {
UErrorCode status = U_ZERO_ERROR;
const UFormattedValue* formattedValue =
unumf_resultAsValue(mFormattedNumber, &status);
if (U_FAILURE(status)) {
return Err(ToICUError(status));
}
int32_t utf16Length;
const char16_t* utf16Str =
ufmtval_getString(formattedValue, &utf16Length, &status);
if (U_FAILURE(status)) {
return Err(ToICUError(status));
}
return std::u16string_view(utf16Str, static_cast<size_t>(utf16Length));
}
} // namespace mozilla::intl
|