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
|
/* 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/PluralRules.h"
#include "mozilla/intl/ICU4CGlue.h"
#include "mozilla/intl/NumberFormat.h"
#include "mozilla/intl/NumberRangeFormat.h"
#include "mozilla/Utf8.h"
#include "mozilla/PodOperations.h"
#include "mozilla/Span.h"
#include "ScopedICUObject.h"
#include "unicode/unum.h"
#include "unicode/upluralrules.h"
#include "unicode/ustring.h"
namespace mozilla::intl {
PluralRules::PluralRules(UPluralRules*& aPluralRules,
UniquePtr<NumberFormat>&& aNumberFormat,
UniquePtr<NumberRangeFormat>&& aNumberRangeFormat)
: mPluralRules(aPluralRules),
mNumberFormat(std::move(aNumberFormat)),
mNumberRangeFormat(std::move(aNumberRangeFormat)) {
MOZ_ASSERT(aPluralRules);
aPluralRules = nullptr;
}
Result<UniquePtr<PluralRules>, ICUError> PluralRules::TryCreate(
const std::string_view aLocale, const PluralRulesOptions& aOptions) {
auto numberFormat =
NumberFormat::TryCreate(aLocale, aOptions.ToNumberFormatOptions());
if (numberFormat.isErr()) {
return Err(numberFormat.unwrapErr());
}
auto numberRangeFormat = NumberRangeFormat::TryCreate(
aLocale, aOptions.ToNumberRangeFormatOptions());
if (numberRangeFormat.isErr()) {
return Err(numberRangeFormat.unwrapErr());
}
UErrorCode status = U_ZERO_ERROR;
auto pluralType = aOptions.mPluralType == PluralRules::Type::Cardinal
? UPLURAL_TYPE_CARDINAL
: UPLURAL_TYPE_ORDINAL;
UPluralRules* pluralRules = uplrules_openForType(
AssertNullTerminatedString(aLocale), pluralType, &status);
if (U_FAILURE(status)) {
return Err(ToICUError(status));
}
return UniquePtr<PluralRules>(new PluralRules(
pluralRules, numberFormat.unwrap(), numberRangeFormat.unwrap()));
}
Result<PluralRules::Keyword, ICUError> PluralRules::Select(
const double aNumber) const {
char16_t keyword[MAX_KEYWORD_LENGTH];
auto lengthResult = mNumberFormat->selectFormatted(
aNumber, keyword, MAX_KEYWORD_LENGTH, mPluralRules);
if (lengthResult.isErr()) {
return Err(lengthResult.unwrapErr());
}
return KeywordFromUtf16(Span(keyword, lengthResult.unwrap()));
}
Result<PluralRules::Keyword, ICUError> PluralRules::SelectRange(
double aStart, double aEnd) const {
char16_t keyword[MAX_KEYWORD_LENGTH];
auto lengthResult = mNumberRangeFormat->selectForRange(
aStart, aEnd, keyword, MAX_KEYWORD_LENGTH, mPluralRules);
if (lengthResult.isErr()) {
return Err(lengthResult.unwrapErr());
}
return KeywordFromUtf16(Span(keyword, lengthResult.unwrap()));
}
Result<EnumSet<PluralRules::Keyword>, ICUError> PluralRules::Categories()
const {
UErrorCode status = U_ZERO_ERROR;
UEnumeration* enumeration = uplrules_getKeywords(mPluralRules, &status);
if (U_FAILURE(status)) {
return Err(ToICUError(status));
}
ScopedICUObject<UEnumeration, uenum_close> closeEnum(enumeration);
EnumSet<PluralRules::Keyword> set;
while (true) {
int32_t keywordLength;
const char* keyword = uenum_next(enumeration, &keywordLength, &status);
if (U_FAILURE(status)) {
return Err(ToICUError(status));
}
if (!keyword) {
break;
}
set += KeywordFromAscii(Span(keyword, keywordLength));
}
return set;
}
PluralRules::Keyword PluralRules::KeywordFromUtf16(
Span<const char16_t> aKeyword) {
static constexpr auto kZero = MakeStringSpan(u"zero");
static constexpr auto kOne = MakeStringSpan(u"one");
static constexpr auto kTwo = MakeStringSpan(u"two");
static constexpr auto kFew = MakeStringSpan(u"few");
static constexpr auto kMany = MakeStringSpan(u"many");
if (aKeyword == kZero) {
return PluralRules::Keyword::Zero;
}
if (aKeyword == kOne) {
return PluralRules::Keyword::One;
}
if (aKeyword == kTwo) {
return PluralRules::Keyword::Two;
}
if (aKeyword == kFew) {
return PluralRules::Keyword::Few;
}
if (aKeyword == kMany) {
return PluralRules::Keyword::Many;
}
MOZ_ASSERT(aKeyword == MakeStringSpan(u"other"));
return PluralRules::Keyword::Other;
}
PluralRules::Keyword PluralRules::KeywordFromAscii(Span<const char> aKeyword) {
static constexpr auto kZero = MakeStringSpan("zero");
static constexpr auto kOne = MakeStringSpan("one");
static constexpr auto kTwo = MakeStringSpan("two");
static constexpr auto kFew = MakeStringSpan("few");
static constexpr auto kMany = MakeStringSpan("many");
if (aKeyword == kZero) {
return PluralRules::Keyword::Zero;
}
if (aKeyword == kOne) {
return PluralRules::Keyword::One;
}
if (aKeyword == kTwo) {
return PluralRules::Keyword::Two;
}
if (aKeyword == kFew) {
return PluralRules::Keyword::Few;
}
if (aKeyword == kMany) {
return PluralRules::Keyword::Many;
}
MOZ_ASSERT(aKeyword == MakeStringSpan("other"));
return PluralRules::Keyword::Other;
}
PluralRules::~PluralRules() {
if (mPluralRules) {
uplrules_close(mPluralRules);
mPluralRules = nullptr;
}
}
} // namespace mozilla::intl
|