QLocale::uiLanguages(): only QString-ify the locale names we do add

We can test whether the candidate is already in the list by exploiting
the comparison of QLatin1StringView with QString (and related
QStringList::compare() overload), so we don't need to convert the
QByteArray to QString unless we're actually going to add it.

(This does lead to the prior name being converted twice, but the
second of these is about to go away.)

Change-Id: I8e47cbb4c9e44ad8ef13d04e930a4619845418ef
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Edward Welbourne 2025-01-14 19:38:41 +01:00
parent 952ec8db3a
commit f8128f2809

View File

@ -5075,18 +5075,18 @@ QStringList QLocale::uiLanguages(TagSeparator separator) const
continue; continue;
} }
const auto prior = QString::fromLatin1(id.name(sep)); const QByteArray prior = id.name(sep);
if (isSystem && i < uiLanguages.size()) { if (isSystem && i < uiLanguages.size()) {
// Adding likely-adjusted forms to system locale's list. // Adding likely-adjusted forms to system locale's list.
Q_ASSERT(uiLanguages.at(i) == prior Q_ASSERT(uiLanguages.at(i) == QLatin1StringView(prior)
// A legacy code may get mapped to an ID with a different name: // A legacy code may get mapped to an ID with a different name:
|| QLatin1String(QLocaleId::fromName(uiLanguages.at(i)).name(sep)) == prior); || QLocaleId::fromName(uiLanguages.at(i)).name(sep) == prior);
// Insert just after the entry we're supplementing: // Insert just after the entry we're supplementing:
j = i + 1; j = i + 1;
} else { } else {
// Plain locale or empty system uiLanguages; just append. // Plain locale or empty system uiLanguages; just append.
if (!uiLanguages.contains(prior)) if (!uiLanguages.contains(QLatin1StringView(prior)))
uiLanguages.append(prior); uiLanguages.append(QString::fromLatin1(prior));
j = uiLanguages.size(); j = uiLanguages.size();
} }
@ -5094,9 +5094,10 @@ QStringList QLocale::uiLanguages(TagSeparator separator) const
const QLocaleId min = max.withLikelySubtagsRemoved(); const QLocaleId min = max.withLikelySubtagsRemoved();
// Include minimal version (last) unless it's what our locale is derived from: // Include minimal version (last) unless it's what our locale is derived from:
if (auto name = QString::fromLatin1(min.name(sep)); name != prior) { if (const QByteArray name = min.name(sep); name != prior) {
uiLanguages.insert(j, name); QString sName = QString::fromLatin1(name);
gatherTruncations(name); uiLanguages.insert(j, sName);
gatherTruncations(sName);
} else if (!isSystem && min == id) { } else if (!isSystem && min == id) {
--j; // Put more specific forms *before* minimal entry. --j; // Put more specific forms *before* minimal entry.
} }
@ -5105,9 +5106,10 @@ QStringList QLocale::uiLanguages(TagSeparator separator) const
// Include scriptless version if likely-equivalent and distinct: // Include scriptless version if likely-equivalent and distinct:
id.script_id = 0; id.script_id = 0;
if (id != min && id.withLikelySubtagsAdded() == max) { if (id != min && id.withLikelySubtagsAdded() == max) {
if (auto name = QString::fromLatin1(id.name(sep)); name != prior) { if (const QByteArray name = id.name(sep); name != prior) {
uiLanguages.insert(j, name); auto sName = QString::fromLatin1(name);
gatherTruncations(name); uiLanguages.insert(j, sName);
gatherTruncations(sName);
} }
} }
} }
@ -5118,19 +5120,21 @@ QStringList QLocale::uiLanguages(TagSeparator separator) const
// Include version with territory if likely-equivalent and distinct: // Include version with territory if likely-equivalent and distinct:
id.territory_id = max.territory_id; id.territory_id = max.territory_id;
if (id != max && id.withLikelySubtagsAdded() == max) { if (id != max && id.withLikelySubtagsAdded() == max) {
if (auto name = QString::fromLatin1(id.name(sep)); name != prior) { if (const QByteArray name = id.name(sep); name != prior) {
uiLanguages.insert(j, name); auto sName = QString::fromLatin1(name);
gatherTruncations(name); uiLanguages.insert(j, sName);
gatherTruncations(sName);
} }
} }
} }
gatherTruncations(prior); // After trimmed forms, before max. gatherTruncations(QString::fromLatin1(prior)); // After trimmed forms, before max.
// Include version with all likely sub-tags (first) if distinct from the rest: // Include version with all likely sub-tags (first) if distinct from the rest:
if (max != min && max != id) { if (max != min && max != id) {
if (auto name = QString::fromLatin1(max.name(sep)); name != prior) { if (const QByteArray name = max.name(sep); name != prior) {
uiLanguages.insert(j, name); auto sName = QString::fromLatin1(name);
gatherTruncations(name); uiLanguages.insert(j, sName);
gatherTruncations(sName);
} }
} }
} }