| 1 |
/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 |
#ifndef intl_components_Collator_h_ |
| 5 |
#define intl_components_Collator_h_ |
| 6 |
|
| 7 |
#ifndef JS_STANDALONE |
| 8 |
# include "gtest/MozGtestFriend.h" |
| 9 |
#endif |
| 10 |
|
| 11 |
#include "unicode/ucol.h" |
| 12 |
|
| 13 |
#include "mozilla/Compiler.h" |
| 14 |
#include "mozilla/intl/ICU4CGlue.h" |
| 15 |
#include "mozilla/intl/ICUError.h" |
| 16 |
#include "mozilla/Result.h" |
| 17 |
#include "mozilla/Span.h" |
| 18 |
|
| 19 |
namespace mozilla::intl { |
| 20 |
|
| 21 |
class Collator final { |
| 22 |
public: |
| 23 |
/** |
| 24 |
* Construct from a raw UCollator. This is public so that the UniquePtr can |
| 25 |
* access it. |
| 26 |
*/ |
| 27 |
explicit Collator(UCollator* aCollator); |
| 28 |
|
| 29 |
// Do not allow copy as this class owns the ICU resource. Move is not |
| 30 |
// currently implemented, but a custom move operator could be created if |
| 31 |
// needed. |
| 32 |
Collator(const Collator&) = delete; |
| 33 |
Collator& operator=(const Collator&) = delete; |
| 34 |
|
| 35 |
/** |
| 36 |
* Attempt to initialize a new collator. |
| 37 |
*/ |
| 38 |
static Result<UniquePtr<Collator>, ICUError> TryCreate(const char* aLocale); |
| 39 |
|
| 40 |
~Collator(); |
| 41 |
|
| 42 |
/** |
| 43 |
* Get a sort key with the provided UTF-16 string, and store the sort key into |
| 44 |
* the provided buffer of byte array. |
| 45 |
* Every sort key ends with 0x00, and the terminating 0x00 byte is counted |
| 46 |
* when calculating the length of buffer. For the purpose of other byte |
| 47 |
* values, check the "Special Byte Values" document from ICU. |
| 48 |
* |
| 49 |
* https://icu.unicode.org/design/collation/bytes |
| 50 |
*/ |
| 51 |
template <typename B> |
| 52 |
ICUResult GetSortKey(Span<const char16_t> aString, B& aBuffer) const { |
| 53 |
return FillBufferWithICUCall( |
| 54 |
aBuffer, |
| 55 |
[this, aString](uint8_t* target, int32_t length, UErrorCode* status) { |
| 56 |
// ucol_getSortKey doesn't use the error code to report |
| 57 |
// U_BUFFER_OVERFLOW_ERROR, instead it uses the return value to |
| 58 |
// indicate the desired length to store the key. So we update the |
| 59 |
// UErrorCode accordingly to let FillBufferWithICUCall resize the |
| 60 |
// buffer. |
| 61 |
int32_t len = ucol_getSortKey(mCollator.GetConst(), aString.data(), |
| 62 |
static_cast<int32_t>(aString.size()), |
| 63 |
target, length); |
| 64 |
if (len == 0) { |
| 65 |
// Returns 0 means there's an internal error. |
| 66 |
*status = U_INTERNAL_PROGRAM_ERROR; |
| 67 |
} else if (len > length) { |
| 68 |
*status = U_BUFFER_OVERFLOW_ERROR; |
| 69 |
} else { |
| 70 |
*status = U_ZERO_ERROR; |
| 71 |
} |
| 72 |
return len; |
| 73 |
}); |
| 74 |
} |
| 75 |
|
| 76 |
int32_t CompareStrings(Span<const char16_t> aSource, |
| 77 |
Span<const char16_t> aTarget) const; |
| 78 |
|
| 79 |
int32_t CompareSortKeys(Span<const uint8_t> aKey1, |
| 80 |
Span<const uint8_t> aKey2) const; |
| 81 |
|
| 82 |
/** |
| 83 |
* Determine how casing affects sorting. These options map to ECMA 402 |
| 84 |
* collator options. |
| 85 |
* |
| 86 |
* https://tc39.es/ecma402/#sec-initializecollator |
| 87 |
*/ |
| 88 |
enum class CaseFirst { |
| 89 |
// Sort upper case first. |
| 90 |
Upper, |
| 91 |
// Sort lower case first. |
| 92 |
Lower, |
| 93 |
// Orders upper and lower case letters in accordance to their tertiary |
| 94 |
// weights. |
| 95 |
False, |
| 96 |
}; |
| 97 |
|
| 98 |
/** |
| 99 |
* Which differences in the strings should lead to differences in collation |
| 100 |
* comparisons. |
| 101 |
* |
| 102 |
* This setting needs to be ECMA 402 compliant. |
| 103 |
* https://tc39.es/ecma402/#sec-collator-comparestrings |
| 104 |
*/ |
| 105 |
enum class Sensitivity { |
| 106 |
// Only strings that differ in base letters compare as unequal. |
| 107 |
// Examples: a ≠ b, a = á, a = A. |
| 108 |
Base, |
| 109 |
// Only strings that differ in base letters or accents and other diacritic |
| 110 |
// marks compare as unequal. |
| 111 |
// Examples: a ≠ b, a ≠ á, a = A. |
| 112 |
Accent, |
| 113 |
// Only strings that differ in base letters or case compare as unequal. |
| 114 |
// Examples: a ≠ b, a = á, a ≠ A. |
| 115 |
Case, |
| 116 |
// Strings that differ in base letters, accents and other diacritic marks, |
| 117 |
// or case compare as unequal. Other differences may also be taken into |
| 118 |
// consideration. |
| 119 |
// Examples: a ≠ b, a ≠ á, a ≠ A. |
| 120 |
Variant, |
| 121 |
}; |
| 122 |
|
| 123 |
/** |
| 124 |
* These options map to ECMA 402 collator options. Make sure the defaults map |
| 125 |
* to the default initialized values of ECMA 402. |
| 126 |
* |
| 127 |
* https://tc39.es/ecma402/#sec-initializecollator |
| 128 |
*/ |
| 129 |
struct Options { |
| 130 |
Sensitivity sensitivity = Sensitivity::Variant; |
| 131 |
CaseFirst caseFirst = CaseFirst::False; |
| 132 |
bool ignorePunctuation = false; |
| 133 |
bool numeric = false; |
| 134 |
}; |
| 135 |
|
| 136 |
/** |
| 137 |
* Change the configuraton of the options. |
| 138 |
*/ |
| 139 |
ICUResult SetOptions(const Options& aOptions, |
| 140 |
const Maybe<Options&> aPrevOptions = Nothing()); |
| 141 |
|
| 142 |
/** |
| 143 |
* Return the case first option of this collator. |
| 144 |
*/ |
| 145 |
Result<CaseFirst, ICUError> GetCaseFirst() const; |
| 146 |
|
| 147 |
/** |
| 148 |
* Return the "ignores punctuation" option of this collator. |
| 149 |
*/ |
| 150 |
Result<bool, ICUError> GetIgnorePunctuation() const; |
| 151 |
|
| 152 |
/** |
| 153 |
* Map keywords to their BCP 47 equivalents. |
| 154 |
*/ |
| 155 |
static SpanResult<char> KeywordValueToBcp47Extension(const char* aKeyword, |
| 156 |
int32_t aLength); |
| 157 |
|
| 158 |
enum class CommonlyUsed : bool { |
| 159 |
/** |
| 160 |
* Select all possible values, even when not commonly used by a locale. |
| 161 |
*/ |
| 162 |
No, |
| 163 |
|
| 164 |
/** |
| 165 |
* Only select the values which are commonly used by a locale. |
| 166 |
*/ |
| 167 |
Yes, |
| 168 |
}; |
| 169 |
|
| 170 |
using Bcp47ExtEnumeration = |
| 171 |
Enumeration<char, SpanResult<char>, |
| 172 |
Collator::KeywordValueToBcp47Extension>; |
| 173 |
|
| 174 |
/** |
| 175 |
* Returns an iterator of collator locale extensions in the preferred order. |
| 176 |
* These extensions can be used in BCP 47 locales. For instance this |
| 177 |
* iterator could return "phonebk" and could be appled to the German locale |
| 178 |
* "de" as "de-co-phonebk" for a phonebook-style collation. |
| 179 |
* |
| 180 |
* The collation extensions can be found here: |
| 181 |
* http://cldr.unicode.org/core-spec/#Key_Type_Definitions |
| 182 |
*/ |
| 183 |
static Result<Bcp47ExtEnumeration, ICUError> GetBcp47KeywordValuesForLocale( |
| 184 |
const char* aLocale, CommonlyUsed aCommonlyUsed = CommonlyUsed::No); |
| 185 |
|
| 186 |
/** |
| 187 |
* Returns an iterator over all possible collator locale extensions. |
| 188 |
* These extensions can be used in BCP 47 locales. For instance this |
| 189 |
* iterator could return "phonebk" and could be appled to the German locale |
| 190 |
* "de" as "de-co-phonebk" for a phonebook-style collation. |
| 191 |
* |
| 192 |
* The collation extensions can be found here: |
| 193 |
* http://cldr.unicode.org/core-spec/#Key_Type_Definitions |
| 194 |
*/ |
| 195 |
static Result<Bcp47ExtEnumeration, ICUError> GetBcp47KeywordValues(); |
| 196 |
|
| 197 |
/** |
| 198 |
* Returns an iterator over all supported collator locales. |
| 199 |
* |
| 200 |
* The returned strings are ICU locale identifiers and NOT BCP 47 language |
| 201 |
* tags. |
| 202 |
* |
| 203 |
* Also see <https://unicode-org.github.io/icu/userguide/locale>. |
| 204 |
*/ |
| 205 |
static auto GetAvailableLocales() { |
| 206 |
return AvailableLocalesEnumeration<ucol_countAvailable, |
| 207 |
ucol_getAvailable>(); |
| 208 |
} |
| 209 |
|
| 210 |
private: |
| 211 |
/** |
| 212 |
* Toggle features, or use the default setting. |
| 213 |
*/ |
| 214 |
enum class Feature { |
| 215 |
// Turn the feature off. |
| 216 |
On, |
| 217 |
// Turn the feature off. |
| 218 |
Off, |
| 219 |
// Use the default setting for the feature. |
| 220 |
Default, |
| 221 |
}; |
| 222 |
|
| 223 |
static constexpr auto ToUColAttributeValue(Feature aFeature) { |
| 224 |
switch (aFeature) { |
| 225 |
case Collator::Feature::On: |
| 226 |
return UCOL_ON; |
| 227 |
case Collator::Feature::Off: |
| 228 |
return UCOL_OFF; |
| 229 |
case Collator::Feature::Default: |
| 230 |
return UCOL_DEFAULT; |
| 231 |
} |
| 232 |
#if MOZ_IS_GCC |
| 233 |
# if !MOZ_GCC_VERSION_AT_LEAST(9, 1, 0) |
| 234 |
return UCOL_DEFAULT; |
| 235 |
# else |
| 236 |
MOZ_CRASH("invalid collator feature"); |
| 237 |
# endif |
| 238 |
#else |
| 239 |
MOZ_CRASH("invalid collator feature"); |
| 240 |
#endif |
| 241 |
} |
| 242 |
|
| 243 |
/** |
| 244 |
* Attribute for handling variable elements. |
| 245 |
*/ |
| 246 |
enum class AlternateHandling { |
| 247 |
// Treats all the codepoints with non-ignorable primary weights in the |
| 248 |
// same way (default) |
| 249 |
NonIgnorable, |
| 250 |
// Causes codepoints with primary weights that are equal or below the |
| 251 |
// variable top value to be ignored on primary level and moved to the |
| 252 |
// quaternary level. |
| 253 |
Shifted, |
| 254 |
Default, |
| 255 |
}; |
| 256 |
|
| 257 |
/** |
| 258 |
* The strength attribute. |
| 259 |
* |
| 260 |
* The usual strength for most locales (except Japanese) is tertiary. |
| 261 |
* |
| 262 |
* Quaternary strength is useful when combined with shifted setting for |
| 263 |
* alternate handling attribute and for JIS X 4061 collation, when it is used |
| 264 |
* to distinguish between Katakana and Hiragana. Otherwise, quaternary level |
| 265 |
* is affected only by the number of non-ignorable code points in the string. |
| 266 |
* |
| 267 |
* Identical strength is rarely useful, as it amounts to codepoints of the NFD |
| 268 |
* form of the string. |
| 269 |
*/ |
| 270 |
enum class Strength { |
| 271 |
// Primary collation strength. |
| 272 |
Primary, |
| 273 |
// Secondary collation strength. |
| 274 |
Secondary, |
| 275 |
// Tertiary collation strength. |
| 276 |
Tertiary, |
| 277 |
// Quaternary collation strength. |
| 278 |
Quaternary, |
| 279 |
// Identical collation strength. |
| 280 |
Identical, |
| 281 |
Default, |
| 282 |
}; |
| 283 |
|
| 284 |
/** |
| 285 |
* Configure the Collation::Strength |
| 286 |
*/ |
| 287 |
void SetStrength(Strength strength); |
| 288 |
|
| 289 |
/** |
| 290 |
* Configure Collation::AlternateHandling. |
| 291 |
*/ |
| 292 |
ICUResult SetAlternateHandling(AlternateHandling aAlternateHandling); |
| 293 |
|
| 294 |
/** |
| 295 |
* Controls whether an extra case level (positioned before the third level) is |
| 296 |
* generated or not. |
| 297 |
* |
| 298 |
* Contents of the case level are affected by the value of CaseFirst |
| 299 |
* attribute. A simple way to ignore accent differences in a string is to set |
| 300 |
* the strength to Primary and enable case level. |
| 301 |
*/ |
| 302 |
ICUResult SetCaseLevel(Feature aFeature); |
| 303 |
|
| 304 |
/** |
| 305 |
* When turned on, this attribute makes substrings of digits sort according to |
| 306 |
* their numeric values. |
| 307 |
* |
| 308 |
* This is a way to get '100' to sort AFTER '2'. Note that the longest digit |
| 309 |
* substring that can be treated as a single unit is 254 digits (not counting |
| 310 |
* leading zeros). If a digit substring is longer than that, the digits beyond |
| 311 |
* the limit will be treated as a separate digit substring. |
| 312 |
* |
| 313 |
* A "digit" in this sense is a code point with General_Category=Nd, which |
| 314 |
* does not include circled numbers, roman numerals, etc. Only a contiguous |
| 315 |
* digit substring is considered, that is, non-negative integers without |
| 316 |
* separators. There is no support for plus/minus signs, decimals, exponents, |
| 317 |
* etc. |
| 318 |
*/ |
| 319 |
ICUResult SetNumericCollation(Feature aFeature); |
| 320 |
|
| 321 |
/** |
| 322 |
* Controls whether the normalization check and necessary normalizations are |
| 323 |
* performed. |
| 324 |
* |
| 325 |
* When off (default), no normalization check is performed. The correctness of |
| 326 |
* the result is guaranteed only if the input data is in so-called FCD form |
| 327 |
* When set to on, an incremental check is performed to see whether the input |
| 328 |
* data is in the FCD form. If the data is not in the FCD form, incremental |
| 329 |
* NFD normalization is performed. |
| 330 |
*/ |
| 331 |
ICUResult SetNormalizationMode(Feature aFeature); |
| 332 |
|
| 333 |
/** |
| 334 |
* Configure Collation::CaseFirst. |
| 335 |
*/ |
| 336 |
ICUResult SetCaseFirst(CaseFirst aCaseFirst); |
| 337 |
|
| 338 |
#ifndef JS_STANDALONE |
| 339 |
FRIEND_TEST(IntlCollator, SetAttributesInternal); |
| 340 |
#endif |
| 341 |
|
| 342 |
ICUPointer<UCollator> mCollator = ICUPointer<UCollator>(nullptr); |
| 343 |
Maybe<Sensitivity> mLastStrategy = Nothing(); |
| 344 |
}; |
| 345 |
|
| 346 |
} // namespace mozilla::intl |
| 347 |
|
| 348 |
#endif |