26 UErrorCode status = U_ZERO_ERROR;
28 collator = ucol_open(name.constData(), &status);
29 if (U_FAILURE(status)) {
30 qWarning(
"Could not create collator: %d", status);
37 ucol_setAttribute(
collator, UCOL_NORMALIZATION_MODE, UCOL_ON, &status);
46 if (options.testFlag(Opt::DiacriticInsensitive)) {
48 status = U_ZERO_ERROR;
49 ucol_setAttribute(
collator, UCOL_STRENGTH, UCOL_PRIMARY, &status);
50 if (U_FAILURE(status))
51 qWarning(
"ucol_setAttribute: Diacritic and case insensitivity failed: %d", status);
53 if (!options.testFlag(Opt::CaseInsensitive)) {
55 status = U_ZERO_ERROR;
56 ucol_setAttribute(
collator, UCOL_CASE_LEVEL, UCOL_ON, &status);
57 if (U_FAILURE(status)) {
58 qWarning(
"ucol_setAttribute: Diacritic insensitivity with case distinction failed:"
63 const UColAttributeValue strength
64 = options.testFlag(Opt::CaseInsensitive) ? UCOL_SECONDARY : UCOL_DEFAULT_STRENGTH;
66 status = U_ZERO_ERROR;
67 ucol_setAttribute(
collator, UCOL_STRENGTH, strength, &status);
68 if (U_FAILURE(status))
69 qWarning(
"ucol_setAttribute: Case sensitivity failed: %d", status);
72 status = U_ZERO_ERROR;
73 ucol_setAttribute(
collator, UCOL_NUMERIC_COLLATION,
74 options.testFlag(Opt::NumericSort) ? UCOL_ON : UCOL_OFF, &status);
75 if (U_FAILURE(status))
76 qWarning(
"ucol_setAttribute: numeric collation failed: %d", status);
78 status = U_ZERO_ERROR;
79 ucol_setAttribute(
collator, UCOL_ALTERNATE_HANDLING,
80 options.testFlag(Opt::IgnorePunctuation) ? UCOL_SHIFTED
81 : UCOL_NON_IGNORABLE, &status);
82 if (U_FAILURE(status))
83 qWarning(
"ucol_setAttribute: Alternate handling failed: %d", status);
95int QCollator::compare(QStringView s1, QStringView s2)
const
98 return s2.size() ? -1 : 0;
103 d =
new QCollatorPrivate(QLocale().collation());
105 d->ensureInitialized();
109 return ucol_strcoll(d->collator,
110 reinterpret_cast<
const UChar *>(s1.data()), s1.size(),
111 reinterpret_cast<
const UChar *>(s2.data()), s2.size());
114 return QtPrivate::compareStrings(s1, s2, caseSensitivity());
117QCollatorSortKey QCollator::sortKey(
const QString &string)
const
120 d =
new QCollatorPrivate(QLocale().collation());
122 d->ensureInitialized();
125 return QCollatorPrivate::sortKeyFromData(string.toUtf8());
128 QByteArray result(16 + string.size() + (string.size() >> 2), Qt::Uninitialized);
130 int size = ucol_getSortKey(d->collator, (
const UChar *)string.constData(),
131 string.size(), (uint8_t *)result.data(), result.size());
132 if (size > result.size()) {
134 size = ucol_getSortKey(d->collator, (
const UChar *)string.constData(),
135 string.size(), (uint8_t *)result.data(), result.size());
137 result.truncate(size);
138 return QCollatorPrivate::sortKeyFromData(std::move(result));
141 return QCollatorPrivate::sortKeyFromData(QByteArray());