From 97f622e09417a99e7b9164d13fe75c43ba3cc7d6 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Tue, 28 Jan 2020 15:46:28 +0100 Subject: [PATCH 1/3] [locale] Use standard algorithms --- src/modules/locale/LocalePage.cpp | 30 +++++++++++++----------------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/src/modules/locale/LocalePage.cpp b/src/modules/locale/LocalePage.cpp index c1df7c81c..4de30c99f 100644 --- a/src/modules/locale/LocalePage.cpp +++ b/src/modules/locale/LocalePage.cpp @@ -227,27 +227,23 @@ LocalePage::init( const QString& initialRegion, const QString& initialZone, cons // Assuming we have a list of supported locales, we usually only want UTF-8 ones // because it's not 1995. - for ( auto it = m_localeGenLines.begin(); it != m_localeGenLines.end(); ) - { - if ( !it->contains( "UTF-8", Qt::CaseInsensitive ) && !it->contains( "utf8", Qt::CaseInsensitive ) ) - { - it = m_localeGenLines.erase( it ); - } - else - { - ++it; - } - } + auto notUtf8 = []( const QString& s ) { + return !s.contains( "UTF-8", Qt::CaseInsensitive ) && !s.contains( "utf8", Qt::CaseInsensitive ); + }; + auto it = std::remove_if( m_localeGenLines.begin(), m_localeGenLines.end(), notUtf8 ); + m_localeGenLines.erase( it, m_localeGenLines.end() ); // We strip " UTF-8" from "en_US.UTF-8 UTF-8" because it's redundant redundant. - for ( auto it = m_localeGenLines.begin(); it != m_localeGenLines.end(); ++it ) - { - if ( it->endsWith( " UTF-8" ) ) + // Also simplify whitespace. + auto unredundant = []( QString& s ) { + if ( s.endsWith( " UTF-8" ) ) { - it->chop( 6 ); + s.chop( 6 ); } - *it = it->simplified(); - } + s = s.simplified(); + }; + std::for_each( m_localeGenLines.begin(), m_localeGenLines.end(), unredundant ); + updateGlobalStorage(); } From 49690fc6817599c4d094d455d73175d88d6c93f0 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Tue, 28 Jan 2020 16:01:59 +0100 Subject: [PATCH 2/3] [locale] Remove superfluous code - the list is already filtered for UTF-8, so this is redundant - this *incidentally* fixes the problem with Assamese and Asturian, since Assamese (as_IN) was having its only entry removed, after which it would match Asturian (ast_ES) --- src/modules/locale/LocaleConfiguration.cpp | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/src/modules/locale/LocaleConfiguration.cpp b/src/modules/locale/LocaleConfiguration.cpp index 3377b11f3..da8d4d0f3 100644 --- a/src/modules/locale/LocaleConfiguration.cpp +++ b/src/modules/locale/LocaleConfiguration.cpp @@ -71,23 +71,6 @@ LocaleConfiguration::fromLanguageAndLocation( const QString& languageLocale, { lang = linesForLanguage.first(); } - else - { - QStringList linesForLanguageUtf; - // FIXME: this might be useless if we already filter out non-UTF8 locales - foreach ( QString line, linesForLanguage ) - { - if ( line.contains( "UTF-8", Qt::CaseInsensitive ) || line.contains( "utf8", Qt::CaseInsensitive ) ) - { - linesForLanguageUtf.append( line ); - } - } - - if ( linesForLanguageUtf.length() == 1 ) - { - lang = linesForLanguageUtf.first(); - } - } // lang could still be empty if we found multiple locales that satisfy myLanguage From a09593e4a258d5dd5f596d1779d5a65a3a2df6d7 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Tue, 28 Jan 2020 17:35:54 +0100 Subject: [PATCH 3/3] [locale] Tighten up language-matching - Drop plain startsWith() matching, since we now have "as" and "ast" as supported languages, where one name is a prefix of the other. --- src/modules/locale/LocaleConfiguration.cpp | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/src/modules/locale/LocaleConfiguration.cpp b/src/modules/locale/LocaleConfiguration.cpp index da8d4d0f3..f1810fb83 100644 --- a/src/modules/locale/LocaleConfiguration.cpp +++ b/src/modules/locale/LocaleConfiguration.cpp @@ -18,7 +18,11 @@ */ #include "LocaleConfiguration.h" + +#include "utils/Logger.h" + #include +#include LocaleConfiguration::LocaleConfiguration() : explicit_lang( false ) @@ -53,14 +57,9 @@ LocaleConfiguration::fromLanguageAndLocation( const QString& languageLocale, { QString language = languageLocale.split( '_' ).first(); - QStringList linesForLanguage; - for ( const QString& line : availableLocales ) - { - if ( line.startsWith( language ) ) - { - linesForLanguage.append( line ); - } - } + // Either an exact match, or the whole language part matches + // (followed by . or _ + QStringList linesForLanguage = availableLocales.filter( QRegularExpression( language + "[._]" ) ); QString lang; if ( linesForLanguage.length() == 0 || languageLocale.isEmpty() )