From f8128f280999e21ba291c899ce669d77803b976c Mon Sep 17 00:00:00 2001 From: Edward Welbourne Date: Tue, 14 Jan 2025 19:38:41 +0100 Subject: [PATCH] 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 --- src/corelib/text/qlocale.cpp | 40 ++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/src/corelib/text/qlocale.cpp b/src/corelib/text/qlocale.cpp index 6757c4bf0b7..9d9e990e5a4 100644 --- a/src/corelib/text/qlocale.cpp +++ b/src/corelib/text/qlocale.cpp @@ -5075,18 +5075,18 @@ QStringList QLocale::uiLanguages(TagSeparator separator) const continue; } - const auto prior = QString::fromLatin1(id.name(sep)); + const QByteArray prior = id.name(sep); if (isSystem && i < uiLanguages.size()) { // 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: - || 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: j = i + 1; } else { // Plain locale or empty system uiLanguages; just append. - if (!uiLanguages.contains(prior)) - uiLanguages.append(prior); + if (!uiLanguages.contains(QLatin1StringView(prior))) + uiLanguages.append(QString::fromLatin1(prior)); j = uiLanguages.size(); } @@ -5094,9 +5094,10 @@ QStringList QLocale::uiLanguages(TagSeparator separator) const const QLocaleId min = max.withLikelySubtagsRemoved(); // Include minimal version (last) unless it's what our locale is derived from: - if (auto name = QString::fromLatin1(min.name(sep)); name != prior) { - uiLanguages.insert(j, name); - gatherTruncations(name); + if (const QByteArray name = min.name(sep); name != prior) { + QString sName = QString::fromLatin1(name); + uiLanguages.insert(j, sName); + gatherTruncations(sName); } else if (!isSystem && min == id) { --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: id.script_id = 0; if (id != min && id.withLikelySubtagsAdded() == max) { - if (auto name = QString::fromLatin1(id.name(sep)); name != prior) { - uiLanguages.insert(j, name); - gatherTruncations(name); + if (const QByteArray name = id.name(sep); name != prior) { + auto sName = QString::fromLatin1(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: id.territory_id = max.territory_id; if (id != max && id.withLikelySubtagsAdded() == max) { - if (auto name = QString::fromLatin1(id.name(sep)); name != prior) { - uiLanguages.insert(j, name); - gatherTruncations(name); + if (const QByteArray name = id.name(sep); name != prior) { + auto sName = QString::fromLatin1(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: if (max != min && max != id) { - if (auto name = QString::fromLatin1(max.name(sep)); name != prior) { - uiLanguages.insert(j, name); - gatherTruncations(name); + if (const QByteArray name = max.name(sep); name != prior) { + auto sName = QString::fromLatin1(name); + uiLanguages.insert(j, sName); + gatherTruncations(sName); } } }