From ac769d1de89f890586db524a307300f37c11d768 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Fri, 15 Jun 2018 04:41:41 -0400 Subject: [PATCH] Style: use QStringLiteral - In many cases, using QLatin1String is a de-optimization, when applied to a C string literal. Kevin Kofler pointed out that those should basically all be QStringLiteral, instead. (Compile tests with -O3 show that in the optimized object file, the code size difference is negligible). - Drop the explicit constructor entirely in cases where we're calling QProcess::execute(), for consistency. - Do a little less messing around in the mapping of keyboard locales to keyboard map names. --- src/calamares/CalamaresApplication.cpp | 8 ++-- src/modules/keyboard/KeyboardPage.cpp | 20 +++++---- src/modules/locale/GeoIPJSON.cpp | 2 +- src/modules/locale/GeoIPTests.cpp | 58 +++++++++++++------------- src/modules/locale/GeoIPXML.cpp | 2 +- src/modules/locale/test_geoip.cpp | 4 +- 6 files changed, 48 insertions(+), 46 deletions(-) diff --git a/src/calamares/CalamaresApplication.cpp b/src/calamares/CalamaresApplication.cpp index 2f61749b0..1e1d90f42 100644 --- a/src/calamares/CalamaresApplication.cpp +++ b/src/calamares/CalamaresApplication.cpp @@ -49,10 +49,10 @@ CalamaresApplication::CalamaresApplication( int& argc, char* argv[] ) // //, so we end up with ~/.cache/Calamares/calamares/ // which is excessively squidly. // - // setOrganizationName( QLatin1String( CALAMARES_ORGANIZATION_NAME ) ); - setOrganizationDomain( QLatin1String( CALAMARES_ORGANIZATION_DOMAIN ) ); - setApplicationName( QLatin1String( CALAMARES_APPLICATION_NAME ) ); - setApplicationVersion( QLatin1String( CALAMARES_VERSION ) ); + // setOrganizationName( QStringLiteral( CALAMARES_ORGANIZATION_NAME ) ); + setOrganizationDomain( QStringLiteral( CALAMARES_ORGANIZATION_DOMAIN ) ); + setApplicationName( QStringLiteral( CALAMARES_APPLICATION_NAME ) ); + setApplicationVersion( QStringLiteral( CALAMARES_VERSION ) ); cDebug() << "Calamares version:" << CALAMARES_VERSION; diff --git a/src/modules/keyboard/KeyboardPage.cpp b/src/modules/keyboard/KeyboardPage.cpp index 9e4b7e3ae..a9a8c02a7 100644 --- a/src/modules/keyboard/KeyboardPage.cpp +++ b/src/modules/keyboard/KeyboardPage.cpp @@ -95,8 +95,7 @@ KeyboardPage::KeyboardPage( QWidget* parent ) QString model = m_models.value( text, "pc105" ); // Set Xorg keyboard model - QProcess::execute( QLatin1Literal( "setxkbmap" ), - QStringList() << "-model" << model ); + QProcess::execute( "setxkbmap", QStringList{ "-model", model } ); } ); CALAMARES_RETRANSLATE( ui->retranslateUi( this ); ) @@ -356,11 +355,15 @@ KeyboardPage::onActivate() lang.replace( '-', '_' ); // Normalize separators } - if ( !lang.isEmpty() && specialCaseMap.contains( lang.toStdString() ) ) + if ( !lang.isEmpty() ) { - QLatin1String newLang( specialCaseMap.value( lang.toStdString() ).c_str() ); - cDebug() << " .. special case language" << lang << '>' << newLang; - lang = newLang; + std::string lang_s = lang.toStdString(); + if ( specialCaseMap.contains( lang_s ) ) + { + QString newLang = QString::fromStdString( specialCaseMap.value( lang_s ) ); + cDebug() << " .. special case language" << lang << "becomes" << newLang; + lang = newLang; + } } if ( !lang.isEmpty() ) { @@ -478,9 +481,8 @@ KeyboardPage::onListVariantCurrentItemChanged( QListWidgetItem* current, QListWi connect( &m_setxkbmapTimer, &QTimer::timeout, this, [=] { - QProcess::execute( QLatin1Literal( "setxkbmap" ), - xkbmap_args( QStringList(), layout, variant ) ); - cDebug() << "xkbmap selection changed to: " << layout << "-" << variant; + QProcess::execute( "setxkbmap", xkbmap_args( QStringList(), layout, variant ) ); + cDebug() << "xkbmap selection changed to: " << layout << '-' << variant; m_setxkbmapTimer.disconnect( this ); } ); m_setxkbmapTimer.start( QApplication::keyboardInputInterval() ); diff --git a/src/modules/locale/GeoIPJSON.cpp b/src/modules/locale/GeoIPJSON.cpp index ee99e6c11..b4daf2084 100644 --- a/src/modules/locale/GeoIPJSON.cpp +++ b/src/modules/locale/GeoIPJSON.cpp @@ -28,7 +28,7 @@ #include GeoIPJSON::GeoIPJSON(const QString& attribute) - : GeoIP( attribute.isEmpty() ? QLatin1String( "time_zone" ) : attribute ) + : GeoIP( attribute.isEmpty() ? QStringLiteral( "time_zone" ) : attribute ) { } diff --git a/src/modules/locale/GeoIPTests.cpp b/src/modules/locale/GeoIPTests.cpp index d9c0a5898..af114611e 100644 --- a/src/modules/locale/GeoIPTests.cpp +++ b/src/modules/locale/GeoIPTests.cpp @@ -53,15 +53,15 @@ GeoIPTests::testJSON() GeoIPJSON handler; auto tz = handler.processReply( json_data_attribute ); - QCOMPARE( tz.first, QLatin1String( "Europe" ) ); - QCOMPARE( tz.second, QLatin1String( "Amsterdam" ) ); + QCOMPARE( tz.first, QStringLiteral( "Europe" ) ); + QCOMPARE( tz.second, QStringLiteral( "Amsterdam" ) ); // JSON is quite tolerant tz = handler.processReply( "time_zone: \"Europe/Brussels\"" ); - QCOMPARE( tz.second, QLatin1String( "Brussels" ) ); + QCOMPARE( tz.second, QStringLiteral( "Brussels" ) ); tz = handler.processReply( "time_zone: America/New_York\n" ); - QCOMPARE( tz.first, QLatin1String( "America" ) ); + QCOMPARE( tz.first, QStringLiteral( "America" ) ); } void GeoIPTests::testJSONalt() @@ -72,8 +72,8 @@ void GeoIPTests::testJSONalt() QCOMPARE( tz.first, QString() ); // Not found tz = handler.processReply( "tarifa: 12\nzona_de_hora: Europe/Madrid" ); - QCOMPARE( tz.first, QLatin1String( "Europe" ) ); - QCOMPARE( tz.second, QLatin1String( "Madrid" ) ); + QCOMPARE( tz.first, QStringLiteral( "Europe" ) ); + QCOMPARE( tz.second, QStringLiteral( "Madrid" ) ); } void @@ -122,8 +122,8 @@ GeoIPTests::testXML() GeoIPXML handler; auto tz = handler.processReply( xml_data_ubiquity ); - QCOMPARE( tz.first, QLatin1String( "Europe" ) ); - QCOMPARE( tz.second, QLatin1String( "Amsterdam" ) ); + QCOMPARE( tz.first, QStringLiteral( "Europe" ) ); + QCOMPARE( tz.second, QStringLiteral( "Amsterdam" ) ); #endif } @@ -137,8 +137,8 @@ GeoIPTests::testXML2() GeoIPXML handler; auto tz = handler.processReply( data ); - QCOMPARE( tz.first, QLatin1String( "America" ) ); - QCOMPARE( tz.second, QLatin1String( "North_Dakota/Beulah" ) ); // Without space + QCOMPARE( tz.first, QStringLiteral( "America" ) ); + QCOMPARE( tz.second, QStringLiteral( "North_Dakota/Beulah" ) ); // Without space #endif } @@ -149,8 +149,8 @@ void GeoIPTests::testXMLalt() GeoIPXML handler( "ZT" ); auto tz = handler.processReply( "Moon/Dark_side" ); - QCOMPARE( tz.first, QLatin1String( "Moon" ) ); - QCOMPARE( tz.second, QLatin1String( "Dark_side" ) ); + QCOMPARE( tz.first, QStringLiteral( "Moon" ) ); + QCOMPARE( tz.second, QStringLiteral( "Dark_side" ) ); #endif } @@ -172,26 +172,26 @@ GeoIPTests::testXMLbad() void GeoIPTests::testSplitTZ() { - auto tz = GeoIP::splitTZString( QLatin1String("Moon/Dark_side") ); - QCOMPARE( tz.first, QLatin1String("Moon") ); - QCOMPARE( tz.second, QLatin1String("Dark_side") ); + auto tz = GeoIP::splitTZString( QStringLiteral("Moon/Dark_side") ); + QCOMPARE( tz.first, QStringLiteral("Moon") ); + QCOMPARE( tz.second, QStringLiteral("Dark_side") ); // Some providers return weirdly escaped data - tz = GeoIP::splitTZString( QLatin1String("America\\/NewYork") ); - QCOMPARE( tz.first, QLatin1String("America") ); - QCOMPARE( tz.second, QLatin1String("NewYork") ); // That's not actually the zone name + tz = GeoIP::splitTZString( QStringLiteral("America\\/NewYork") ); + QCOMPARE( tz.first, QStringLiteral("America") ); + QCOMPARE( tz.second, QStringLiteral("NewYork") ); // That's not actually the zone name // Check that bogus data fails tz = GeoIP::splitTZString( QString() ); QCOMPARE( tz.first, QString() ); - tz = GeoIP::splitTZString( QLatin1String("America.NewYork") ); + tz = GeoIP::splitTZString( QStringLiteral("America.NewYork") ); QCOMPARE( tz.first, QString() ); // Check that three-level is split properly and space is replaced - tz = GeoIP::splitTZString( QLatin1String("America/North Dakota/Beulah") ); - QCOMPARE( tz.first, QLatin1String("America") ); - QCOMPARE( tz.second, QLatin1String("North_Dakota/Beulah") ); + tz = GeoIP::splitTZString( QStringLiteral("America/North Dakota/Beulah") ); + QCOMPARE( tz.first, QStringLiteral("America") ); + QCOMPARE( tz.second, QStringLiteral("North_Dakota/Beulah") ); } @@ -221,7 +221,7 @@ synchronous_get( const char* urlstring ) void GeoIPTests::testGet() { - if ( !QProcessEnvironment::systemEnvironment().contains( QLatin1String("TEST_HTTP_GET") ) ) + if ( !QProcessEnvironment::systemEnvironment().contains( QStringLiteral("TEST_HTTP_GET") ) ) { qDebug() << "Skipping HTTP GET tests"; return; @@ -232,8 +232,8 @@ void GeoIPTests::testGet() auto default_tz = default_handler.processReply( synchronous_get( "https://geoip.kde.org/v1/calamares" ) ); // This is bogus, because the test isn't always run by me - // QCOMPARE( default_tz.first, QLatin1String("Europe") ); - // QCOMPARE( default_tz.second, QLatin1String("Amsterdam") ); + // QCOMPARE( default_tz.first, QStringLiteral("Europe") ); + // QCOMPARE( default_tz.second, QStringLiteral("Amsterdam") ); QVERIFY( !default_tz.first.isEmpty() ); QVERIFY( !default_tz.second.isEmpty() ); @@ -242,12 +242,12 @@ void GeoIPTests::testGet() // services don't agree on the location of where the test is run. CHECK_GET( JSON, QString(), "https://geoip.kde.org/v1/calamares" ) // Check it's consistent CHECK_GET( JSON, QString(), "http://freegeoip.net/json/" ) // Original FreeGeoIP service - CHECK_GET( JSON, QLatin1String("timezone"), "https://ipapi.co/json" ) // Different JSON - CHECK_GET( JSON, QLatin1String("timezone"), "http://ip-api.com/json" ) + CHECK_GET( JSON, QStringLiteral("timezone"), "https://ipapi.co/json" ) // Different JSON + CHECK_GET( JSON, QStringLiteral("timezone"), "http://ip-api.com/json" ) - CHECK_GET( JSON, QLatin1String("location.time_zone"), "http://geoip.nekudo.com/api/" ) // 2-level JSON + CHECK_GET( JSON, QStringLiteral("location.time_zone"), "http://geoip.nekudo.com/api/" ) // 2-level JSON - CHECK_GET( JSON, QLatin1String("Location.TimeZone"), "https://geoip.kde.org/debug" ) // 2-level JSON + CHECK_GET( JSON, QStringLiteral("Location.TimeZone"), "https://geoip.kde.org/debug" ) // 2-level JSON #ifdef HAVE_XML CHECK_GET( XML, QString(), "http://geoip.ubuntu.com/lookup" ) // Ubiquity's XML format diff --git a/src/modules/locale/GeoIPXML.cpp b/src/modules/locale/GeoIPXML.cpp index 0fb5359f3..bd675c2ef 100644 --- a/src/modules/locale/GeoIPXML.cpp +++ b/src/modules/locale/GeoIPXML.cpp @@ -24,7 +24,7 @@ #include GeoIPXML::GeoIPXML( const QString& element ) - : GeoIP( element.isEmpty() ? QLatin1String( "TimeZone" ) : element ) + : GeoIP( element.isEmpty() ? QStringLiteral( "TimeZone" ) : element ) { } diff --git a/src/modules/locale/test_geoip.cpp b/src/modules/locale/test_geoip.cpp index 7b6584f0c..5ba43f72e 100644 --- a/src/modules/locale/test_geoip.cpp +++ b/src/modules/locale/test_geoip.cpp @@ -38,10 +38,10 @@ int main(int argc, char** argv) } GeoIP* handler = nullptr; - if ( QLatin1String( "json" ) == argv[1] ) + if ( QStringLiteral( "json" ) == argv[1] ) handler = new GeoIPJSON; #ifdef HAVE_XML - else if ( QLatin1String( "xml" ) == argv[1] ) + else if ( QStringLiteral( "xml" ) == argv[1] ) handler = new GeoIPXML; #endif