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
289
290
291
292
293
294
295
|
/* 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 <algorithm>
#include <string.h>
#include "mozilla/intl/Collator.h"
namespace mozilla::intl {
Collator::Collator(UCollator* aCollator) : mCollator(aCollator) {
MOZ_ASSERT(aCollator);
}
Collator::~Collator() {
if (mCollator.GetMut()) {
ucol_close(mCollator.GetMut());
}
}
Result<UniquePtr<Collator>, ICUError> Collator::TryCreate(const char* aLocale) {
UErrorCode status = U_ZERO_ERROR;
UCollator* collator = ucol_open(IcuLocale(aLocale), &status);
if (U_FAILURE(status)) {
return Err(ToICUError(status));
}
return MakeUnique<Collator>(collator);
};
int32_t Collator::CompareStrings(Span<const char16_t> aSource,
Span<const char16_t> aTarget) const {
switch (ucol_strcoll(mCollator.GetConst(), aSource.data(),
static_cast<int32_t>(aSource.size()), aTarget.data(),
static_cast<int32_t>(aTarget.size()))) {
case UCOL_LESS:
return -1;
case UCOL_EQUAL:
return 0;
case UCOL_GREATER:
return 1;
}
MOZ_ASSERT_UNREACHABLE("ucol_strcoll returned bad UCollationResult");
return 0;
}
int32_t Collator::CompareSortKeys(Span<const uint8_t> aKey1,
Span<const uint8_t> aKey2) const {
size_t minLength = std::min(aKey1.Length(), aKey2.Length());
int32_t tmpResult = strncmp((const char*)aKey1.Elements(),
(const char*)aKey2.Elements(), minLength);
if (tmpResult < 0) {
return -1;
}
if (tmpResult > 0) {
return 1;
}
if (aKey1.Length() > minLength) {
// First string contains second one, so comes later, hence return > 0.
return 1;
}
if (aKey2.Length() > minLength) {
// First string is a substring of second one, so comes earlier,
// hence return < 0.
return -1;
}
return 0;
}
static UColAttributeValue CaseFirstToICU(Collator::CaseFirst caseFirst) {
switch (caseFirst) {
case Collator::CaseFirst::False:
return UCOL_OFF;
case Collator::CaseFirst::Upper:
return UCOL_UPPER_FIRST;
case Collator::CaseFirst::Lower:
return UCOL_LOWER_FIRST;
}
MOZ_ASSERT_UNREACHABLE();
return UCOL_DEFAULT;
}
void Collator::SetStrength(Collator::Strength aStrength) {
UColAttributeValue strength;
switch (aStrength) {
case Collator::Strength::Default:
strength = UCOL_DEFAULT_STRENGTH;
break;
case Collator::Strength::Primary:
strength = UCOL_PRIMARY;
break;
case Collator::Strength::Secondary:
strength = UCOL_SECONDARY;
break;
case Collator::Strength::Tertiary:
strength = UCOL_TERTIARY;
break;
case Collator::Strength::Quaternary:
strength = UCOL_QUATERNARY;
break;
case Collator::Strength::Identical:
strength = UCOL_IDENTICAL;
break;
}
ucol_setStrength(mCollator.GetMut(), strength);
}
ICUResult Collator::SetCaseLevel(Collator::Feature aFeature) {
UErrorCode status = U_ZERO_ERROR;
ucol_setAttribute(mCollator.GetMut(), UCOL_CASE_LEVEL,
ToUColAttributeValue(aFeature), &status);
return ToICUResult(status);
}
ICUResult Collator::SetAlternateHandling(
Collator::AlternateHandling aAlternateHandling) {
UErrorCode status = U_ZERO_ERROR;
UColAttributeValue handling;
switch (aAlternateHandling) {
case Collator::AlternateHandling::NonIgnorable:
handling = UCOL_NON_IGNORABLE;
break;
case Collator::AlternateHandling::Shifted:
handling = UCOL_SHIFTED;
break;
case Collator::AlternateHandling::Default:
handling = UCOL_DEFAULT;
break;
}
ucol_setAttribute(mCollator.GetMut(), UCOL_ALTERNATE_HANDLING, handling,
&status);
return ToICUResult(status);
}
ICUResult Collator::SetNumericCollation(Collator::Feature aFeature) {
UErrorCode status = U_ZERO_ERROR;
ucol_setAttribute(mCollator.GetMut(), UCOL_NUMERIC_COLLATION,
ToUColAttributeValue(aFeature), &status);
return ToICUResult(status);
}
ICUResult Collator::SetNormalizationMode(Collator::Feature aFeature) {
UErrorCode status = U_ZERO_ERROR;
ucol_setAttribute(mCollator.GetMut(), UCOL_NORMALIZATION_MODE,
ToUColAttributeValue(aFeature), &status);
return ToICUResult(status);
}
ICUResult Collator::SetCaseFirst(Collator::CaseFirst aCaseFirst) {
UErrorCode status = U_ZERO_ERROR;
ucol_setAttribute(mCollator.GetMut(), UCOL_CASE_FIRST,
CaseFirstToICU(aCaseFirst), &status);
return ToICUResult(status);
}
ICUResult Collator::SetOptions(const Options& aOptions,
const Maybe<Options&> aPrevOptions) {
if (aPrevOptions &&
// Check the equality of the previous options.
aPrevOptions->sensitivity == aOptions.sensitivity &&
aPrevOptions->caseFirst == aOptions.caseFirst &&
aPrevOptions->ignorePunctuation == aOptions.ignorePunctuation &&
aPrevOptions->numeric == aOptions.numeric) {
return Ok();
}
Collator::Strength strength = Collator::Strength::Default;
Collator::Feature caseLevel = Collator::Feature::Off;
switch (aOptions.sensitivity) {
case Collator::Sensitivity::Base:
strength = Collator::Strength::Primary;
break;
case Collator::Sensitivity::Accent:
strength = Collator::Strength::Secondary;
break;
case Collator::Sensitivity::Case:
caseLevel = Collator::Feature::On;
strength = Collator::Strength::Primary;
break;
case Collator::Sensitivity::Variant:
strength = Collator::Strength::Tertiary;
break;
}
SetStrength(strength);
ICUResult result = Ok();
// According to the ICU team, UCOL_SHIFTED causes punctuation to be
// ignored. Looking at Unicode Technical Report 35, Unicode Locale Data
// Markup Language, "shifted" causes whitespace and punctuation to be
// ignored - that's a bit more than asked for, but there's no way to get
// less.
result = this->SetAlternateHandling(
aOptions.ignorePunctuation ? Collator::AlternateHandling::Shifted
: Collator::AlternateHandling::NonIgnorable);
if (result.isErr()) {
return result;
}
result = SetCaseLevel(caseLevel);
if (result.isErr()) {
return result;
}
result = SetNumericCollation(aOptions.numeric ? Collator::Feature::On
: Collator::Feature::Off);
if (result.isErr()) {
return result;
}
// Normalization is always on to meet the canonical equivalence requirement.
result = SetNormalizationMode(Collator::Feature::On);
if (result.isErr()) {
return result;
}
result = SetCaseFirst(aOptions.caseFirst);
if (result.isErr()) {
return result;
}
return Ok();
}
Result<Collator::CaseFirst, ICUError> Collator::GetCaseFirst() const {
UErrorCode status = U_ZERO_ERROR;
UColAttributeValue caseFirst =
ucol_getAttribute(mCollator.GetConst(), UCOL_CASE_FIRST, &status);
if (U_FAILURE(status)) {
return Err(ToICUError(status));
}
if (caseFirst == UCOL_OFF) {
return CaseFirst::False;
}
if (caseFirst == UCOL_UPPER_FIRST) {
return CaseFirst::Upper;
}
MOZ_ASSERT(caseFirst == UCOL_LOWER_FIRST);
return CaseFirst::Lower;
}
Result<bool, ICUError> Collator::GetIgnorePunctuation() const {
UErrorCode status = U_ZERO_ERROR;
UColAttributeValue alternateHandling =
ucol_getAttribute(mCollator.GetConst(), UCOL_ALTERNATE_HANDLING, &status);
if (U_FAILURE(status)) {
return Err(ToICUError(status));
}
MOZ_ASSERT(alternateHandling == UCOL_SHIFTED ||
alternateHandling == UCOL_NON_IGNORABLE);
return alternateHandling == UCOL_SHIFTED;
}
/* static */
Result<Collator::Bcp47ExtEnumeration, ICUError>
Collator::GetBcp47KeywordValuesForLocale(const char* aLocale,
CommonlyUsed aCommonlyUsed) {
UErrorCode status = U_ZERO_ERROR;
UEnumeration* enumeration = ucol_getKeywordValuesForLocale(
"collation", aLocale, static_cast<bool>(aCommonlyUsed), &status);
if (U_SUCCESS(status)) {
return Bcp47ExtEnumeration(enumeration);
}
return Err(ToICUError(status));
}
/* static */
Result<Collator::Bcp47ExtEnumeration, ICUError>
Collator::GetBcp47KeywordValues() {
UErrorCode status = U_ZERO_ERROR;
UEnumeration* enumeration = ucol_getKeywordValues("collation", &status);
if (U_SUCCESS(status)) {
return Bcp47ExtEnumeration(enumeration);
}
return Err(ToICUError(status));
}
/* static */
SpanResult<char> Collator::KeywordValueToBcp47Extension(const char* aKeyword,
int32_t aLength) {
if (aKeyword == nullptr) {
return Err(InternalError{});
}
return MakeStringSpan(uloc_toUnicodeLocaleType("co", aKeyword));
}
} // namespace mozilla::intl
|