summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAnton Kudryavtsev <a.kudryavtsev@netris.ru>2016-08-18 11:57:26 +0300
committerAnton Kudryavtsev <a.kudryavtsev@netris.ru>2016-09-15 15:55:57 +0000
commit6aa935cd92deb035739e8fea329ca2c4a435de54 (patch)
treea9a3ea43af619a7a89ce39f15ea603747767a9f2
parent251d6094abf423f96aa0f10921defb4c6a805d14 (diff)
Use QString::fromLatin1() less to avoid string allocations
QString::fromLatin1 always allocates memory, but there are cases where we can avoid/reduce allocations or/and reduce text size, e.g.: QStringBuilder expressions Fix: replace QString::fromLatin1 with QL1S QString::fromLatin1().arg(String) pattern Fix: replace with QStringBuilder Overloaded functions with QL1S arg Fix: replace QString::fromLatin1 with QL1S In rare cases if there is no overloaded function with QL1S and we have deal with string literal, replace QString::fromLatin1 with QStringLiteral. Change-Id: Iabe1a3cc0830f40ef78a0548afa4368583c31def Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
-rw-r--r--src/corelib/mimetypes/qmimeprovider.cpp9
-rw-r--r--src/dbus/qdbusinternalfilters.cpp6
-rw-r--r--src/gui/image/qimagereader.cpp2
-rw-r--r--src/gui/kernel/qkeysequence.cpp10
-rw-r--r--src/gui/text/qfontdatabase.cpp2
-rw-r--r--src/gui/text/qtextodfwriter.cpp2
-rw-r--r--src/network/access/qhttpnetworkconnection.cpp4
-rw-r--r--src/network/kernel/qnetworkinterface_unix.cpp2
-rw-r--r--src/network/kernel/qnetworkproxy_generic.cpp2
-rw-r--r--src/platformsupport/devicediscovery/qdevicediscovery_static.cpp2
-rw-r--r--src/plugins/platforms/haiku/qhaikuclipboard.cpp2
-rw-r--r--src/plugins/platforms/xcb/qxcbmime.cpp2
-rw-r--r--src/plugins/sqldrivers/db2/qsql_db2.cpp3
-rw-r--r--src/printsupport/dialogs/qprintpreviewdialog.cpp2
-rw-r--r--src/widgets/accessible/qaccessiblewidgets.cpp2
-rw-r--r--src/widgets/kernel/qaction.cpp2
-rw-r--r--src/widgets/widgets/qlcdnumber.cpp4
-rw-r--r--src/widgets/widgets/qpushbutton.cpp2
18 files changed, 31 insertions, 29 deletions
diff --git a/src/corelib/mimetypes/qmimeprovider.cpp b/src/corelib/mimetypes/qmimeprovider.cpp
index e25eb4272d..a798d9eff6 100644
--- a/src/corelib/mimetypes/qmimeprovider.cpp
+++ b/src/corelib/mimetypes/qmimeprovider.cpp
@@ -572,10 +572,9 @@ void QMimeBinaryProvider::loadMimeTypePrivate(QMimeTypePrivate &data)
const QString file = data.name + QLatin1String(".xml");
// shared-mime-info since 1.3 lowercases the xml files
- QStringList mimeFiles = QStandardPaths::locateAll(QStandardPaths::GenericDataLocation, QString::fromLatin1("mime/") + file.toLower());
- if (mimeFiles.isEmpty()) {
- mimeFiles = QStandardPaths::locateAll(QStandardPaths::GenericDataLocation, QString::fromLatin1("mime/") + file); // pre-1.3
- }
+ QStringList mimeFiles = QStandardPaths::locateAll(QStandardPaths::GenericDataLocation, QLatin1String("mime/") + file.toLower());
+ if (mimeFiles.isEmpty())
+ mimeFiles = QStandardPaths::locateAll(QStandardPaths::GenericDataLocation, QLatin1String("mime/") + file); // pre-1.3
if (mimeFiles.isEmpty()) {
qWarning() << "No file found for" << file << ", even though update-mime-info said it would exist.\n"
"Either it was just removed, or the directory doesn't have executable permission..."
@@ -807,7 +806,7 @@ bool QMimeXMLProvider::load(const QString &fileName, QString *errorMessage)
QFile file(fileName);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
if (errorMessage)
- *errorMessage = QString::fromLatin1("Cannot open %1: %2").arg(fileName, file.errorString());
+ *errorMessage = QLatin1String("Cannot open ") + fileName + QLatin1String(": ") + file.errorString();
return false;
}
diff --git a/src/dbus/qdbusinternalfilters.cpp b/src/dbus/qdbusinternalfilters.cpp
index ad6b06da23..03e04bb2f5 100644
--- a/src/dbus/qdbusinternalfilters.cpp
+++ b/src/dbus/qdbusinternalfilters.cpp
@@ -116,8 +116,7 @@ static QString generateSubObjectXml(QObject *object)
for ( ; it != end; ++it) {
QString name = (*it)->objectName();
if (!name.isEmpty() && QDBusUtil::isValidPartOfObjectPath(name))
- retval += QString::fromLatin1(" <node name=\"%1\"/>\n")
- .arg(name);
+ retval += QLatin1String(" <node name=\"") + name + QLatin1String("\"/>\n");
}
return retval;
}
@@ -192,8 +191,7 @@ QString qDBusIntrospectObject(const QDBusConnectionPrivate::ObjectTreeNode &node
node.children.constEnd();
for ( ; it != end; ++it)
if (it->obj || !it->children.isEmpty())
- xml_data += QString::fromLatin1(" <node name=\"%1\"/>\n")
- .arg(it->name);
+ xml_data += QLatin1String(" <node name=\"") + it->name + QLatin1String("\"/>\n");
}
xml_data += QLatin1String("</node>\n");
diff --git a/src/gui/image/qimagereader.cpp b/src/gui/image/qimagereader.cpp
index 0320d032b0..4390e46fde 100644
--- a/src/gui/image/qimagereader.cpp
+++ b/src/gui/image/qimagereader.cpp
@@ -569,7 +569,7 @@ bool QImageReaderPrivate::initHandler()
do {
file->setFileName(fileName + QLatin1Char('.')
- + QString::fromLatin1(extensions.at(currentExtension++).constData()));
+ + QLatin1String(extensions.at(currentExtension++).constData()));
file->open(QIODevice::ReadOnly);
} while (!file->isOpen() && currentExtension < extensions.size());
diff --git a/src/gui/kernel/qkeysequence.cpp b/src/gui/kernel/qkeysequence.cpp
index 9a75f4bc83..d59bd63caf 100644
--- a/src/gui/kernel/qkeysequence.cpp
+++ b/src/gui/kernel/qkeysequence.cpp
@@ -1211,9 +1211,13 @@ QString QKeySequence::encodeString(int key)
static inline void addKey(QString &str, const QString &theKey, QKeySequence::SequenceFormat format)
{
- if (!str.isEmpty())
- str += (format == QKeySequence::NativeText) ? QCoreApplication::translate("QShortcut", "+")
- : QString::fromLatin1("+");
+ if (!str.isEmpty()) {
+ if (format == QKeySequence::NativeText)
+ str += QCoreApplication::translate("QShortcut", "+");
+ else
+ str += QLatin1Char('+');
+ }
+
str += theKey;
}
diff --git a/src/gui/text/qfontdatabase.cpp b/src/gui/text/qfontdatabase.cpp
index a3a3f20b18..7fcac9b9f2 100644
--- a/src/gui/text/qfontdatabase.cpp
+++ b/src/gui/text/qfontdatabase.cpp
@@ -2439,7 +2439,7 @@ int QFontDatabasePrivate::addAppFont(const QByteArray &fontData, const QString &
}
if (font.fileName.isEmpty() && !fontData.isEmpty())
- font.fileName = QString::fromLatin1(":qmemoryfonts/") + QString::number(i);
+ font.fileName = QLatin1String(":qmemoryfonts/") + QString::number(i);
registerFont(&font);
if (font.families.isEmpty())
diff --git a/src/gui/text/qtextodfwriter.cpp b/src/gui/text/qtextodfwriter.cpp
index 73d2e545ba..e228b3c840 100644
--- a/src/gui/text/qtextodfwriter.cpp
+++ b/src/gui/text/qtextodfwriter.cpp
@@ -63,7 +63,7 @@ QT_BEGIN_NAMESPACE
static QString pixelToPoint(qreal pixels)
{
// we hardcode 96 DPI, we do the same in the ODF importer to have a perfect roundtrip.
- return QString::number(pixels * 72 / 96) + QString::fromLatin1("pt");
+ return QString::number(pixels * 72 / 96) + QLatin1String("pt");
}
// strategies
diff --git a/src/network/access/qhttpnetworkconnection.cpp b/src/network/access/qhttpnetworkconnection.cpp
index 74fc23957c..8e5cfd9497 100644
--- a/src/network/access/qhttpnetworkconnection.cpp
+++ b/src/network/access/qhttpnetworkconnection.cpp
@@ -319,9 +319,9 @@ void QHttpNetworkConnectionPrivate::prepareRequest(HttpMessagePair &messagePair)
if (systemLocale == QLatin1String("C"))
acceptLanguage = QString::fromLatin1("en,*");
else if (systemLocale.startsWith(QLatin1String("en-")))
- acceptLanguage = QString::fromLatin1("%1,*").arg(systemLocale);
+ acceptLanguage = systemLocale + QLatin1String(",*");
else
- acceptLanguage = QString::fromLatin1("%1,en,*").arg(systemLocale);
+ acceptLanguage = systemLocale + QLatin1String(",en,*");
request.setHeaderField("Accept-Language", acceptLanguage.toLatin1());
}
diff --git a/src/network/kernel/qnetworkinterface_unix.cpp b/src/network/kernel/qnetworkinterface_unix.cpp
index da53ccfe70..2fa98c0e07 100644
--- a/src/network/kernel/qnetworkinterface_unix.cpp
+++ b/src/network/kernel/qnetworkinterface_unix.cpp
@@ -488,7 +488,7 @@ static QList<QNetworkInterfacePrivate *> interfaceListing()
interfaces = createInterfaces(interfaceListing);
for (ifaddrs *ptr = interfaceListing; ptr; ptr = ptr->ifa_next) {
// Find the interface
- QString name = QString::fromLatin1(ptr->ifa_name);
+ QLatin1String name(ptr->ifa_name);
QNetworkInterfacePrivate *iface = 0;
QList<QNetworkInterfacePrivate *>::Iterator if_it = interfaces.begin();
for ( ; if_it != interfaces.end(); ++if_it)
diff --git a/src/network/kernel/qnetworkproxy_generic.cpp b/src/network/kernel/qnetworkproxy_generic.cpp
index e69870a98c..3ff0cc5168 100644
--- a/src/network/kernel/qnetworkproxy_generic.cpp
+++ b/src/network/kernel/qnetworkproxy_generic.cpp
@@ -79,7 +79,7 @@ static bool ignoreProxyFor(const QNetworkProxyQuery &query)
if (!peerHostName.startsWith('.'))
peerHostName.prepend('.');
- if (peerHostName.endsWith(QString::fromLatin1(token)))
+ if (peerHostName.endsWith(QLatin1String(token)))
return true;
}
diff --git a/src/platformsupport/devicediscovery/qdevicediscovery_static.cpp b/src/platformsupport/devicediscovery/qdevicediscovery_static.cpp
index 83a9343a6a..5c72dbe7e2 100644
--- a/src/platformsupport/devicediscovery/qdevicediscovery_static.cpp
+++ b/src/platformsupport/devicediscovery/qdevicediscovery_static.cpp
@@ -135,7 +135,7 @@ bool QDeviceDiscoveryStatic::checkDeviceType(const QString &device)
qCDebug(lcDD) << "doing static device discovery for " << device;
- if ((m_types & Device_DRM) && device.contains(QString::fromLatin1(QT_DRM_DEVICE_PREFIX))) {
+ if ((m_types & Device_DRM) && device.contains(QLatin1String(QT_DRM_DEVICE_PREFIX))) {
QT_CLOSE(fd);
return true;
}
diff --git a/src/plugins/platforms/haiku/qhaikuclipboard.cpp b/src/plugins/platforms/haiku/qhaikuclipboard.cpp
index 0194a0415a..774da4432a 100644
--- a/src/plugins/platforms/haiku/qhaikuclipboard.cpp
+++ b/src/plugins/platforms/haiku/qhaikuclipboard.cpp
@@ -91,7 +91,7 @@ QMimeData *QHaikuClipboard::mimeData(QClipboard::Mode mode)
const status_t status = clipboard->FindData(name, B_MIME_TYPE, &data, &dataLen);
if (dataLen && (status == B_OK)) {
- const QString format = QString::fromLatin1(name);
+ const QLatin1String format(name);
if (format == QLatin1String("text/plain")) {
m_systemMimeData->setText(QString::fromLocal8Bit(reinterpret_cast<const char*>(data), dataLen));
} else if (format == QLatin1String("text/html")) {
diff --git a/src/plugins/platforms/xcb/qxcbmime.cpp b/src/plugins/platforms/xcb/qxcbmime.cpp
index 749906d1e8..f71c5464d0 100644
--- a/src/plugins/platforms/xcb/qxcbmime.cpp
+++ b/src/plugins/platforms/xcb/qxcbmime.cpp
@@ -173,7 +173,7 @@ QVariant QXcbMime::mimeConvertToFormat(QXcbConnection *connection, xcb_atom_t a,
// qDebug() << "mimeConvertDataToFormat" << format << atomName << data;
if (!encoding.isEmpty()
- && atomName == format + QLatin1String(";charset=") + QString::fromLatin1(encoding)) {
+ && atomName == format + QLatin1String(";charset=") + QLatin1String(encoding)) {
#ifndef QT_NO_TEXTCODEC
if (requestedType == QVariant::String) {
diff --git a/src/plugins/sqldrivers/db2/qsql_db2.cpp b/src/plugins/sqldrivers/db2/qsql_db2.cpp
index 4ccc3aca9e..9408f464e0 100644
--- a/src/plugins/sqldrivers/db2/qsql_db2.cpp
+++ b/src/plugins/sqldrivers/db2/qsql_db2.cpp
@@ -1665,7 +1665,8 @@ QString QDB2Driver::formatValue(const QSqlField &field, bool trimStrings) const
}
case QVariant::ByteArray: {
QByteArray ba = field.value().toByteArray();
- QString res = QString::fromLatin1("BLOB(X'");
+ QString res;
+ res += QLatin1String("BLOB(X'");
static const char hexchars[] = "0123456789abcdef";
for (int i = 0; i < ba.size(); ++i) {
uchar s = (uchar) ba[i];
diff --git a/src/printsupport/dialogs/qprintpreviewdialog.cpp b/src/printsupport/dialogs/qprintpreviewdialog.cpp
index 435f8869c4..c996e0d556 100644
--- a/src/printsupport/dialogs/qprintpreviewdialog.cpp
+++ b/src/printsupport/dialogs/qprintpreviewdialog.cpp
@@ -334,7 +334,7 @@ void QPrintPreviewDialogPrivate::init(QPrinter *_printer)
QString caption = QCoreApplication::translate("QPrintPreviewDialog", "Print Preview");
if (!printer->docName().isEmpty())
- caption += QString::fromLatin1(": ") + printer->docName();
+ caption += QLatin1String(": ") + printer->docName();
q->setWindowTitle(caption);
if (!printer->isValid()
diff --git a/src/widgets/accessible/qaccessiblewidgets.cpp b/src/widgets/accessible/qaccessiblewidgets.cpp
index 95888924fb..b814762168 100644
--- a/src/widgets/accessible/qaccessiblewidgets.cpp
+++ b/src/widgets/accessible/qaccessiblewidgets.cpp
@@ -830,7 +830,7 @@ QString QAccessibleTextWidget::attributes(int offset, int *startOffset, int *end
family = family.replace('=', QLatin1String("\\="));
family = family.replace(';', QLatin1String("\\;"));
family = family.replace('\"', QLatin1String("\\\""));
- attrs["font-family"] = QString::fromLatin1("\"%1\"").arg(family);
+ attrs["font-family"] = QLatin1Char('"') + family + QLatin1Char('"');
}
int fontSize = int(charFormatFont.pointSize());
diff --git a/src/widgets/kernel/qaction.cpp b/src/widgets/kernel/qaction.cpp
index 72aae647e0..e0700d877e 100644
--- a/src/widgets/kernel/qaction.cpp
+++ b/src/widgets/kernel/qaction.cpp
@@ -63,7 +63,7 @@ QT_BEGIN_NAMESPACE
*/
static QString qt_strippedText(QString s)
{
- s.remove( QString::fromLatin1("...") );
+ s.remove(QStringLiteral("..."));
for (int i = 0; i < s.size(); ++i) {
if (s.at(i) == QLatin1Char('&'))
s.remove(i, 1);
diff --git a/src/widgets/widgets/qlcdnumber.cpp b/src/widgets/widgets/qlcdnumber.cpp
index 2703e5d8a3..aa052ef5cb 100644
--- a/src/widgets/widgets/qlcdnumber.cpp
+++ b/src/widgets/widgets/qlcdnumber.cpp
@@ -188,8 +188,8 @@ static QString int2string(int num, int base, int ndigits, bool *oflow)
} while (n != 0);
len = ndigits - len;
if (len > 0)
- s.fill(QLatin1Char(' '), len);
- s += QString::fromLatin1(p);
+ s += QString(len, QLatin1Char(' '));
+ s += QLatin1String(p);
}
break;
}
diff --git a/src/widgets/widgets/qpushbutton.cpp b/src/widgets/widgets/qpushbutton.cpp
index 5608a4aff9..bf10b9fb75 100644
--- a/src/widgets/widgets/qpushbutton.cpp
+++ b/src/widgets/widgets/qpushbutton.cpp
@@ -415,7 +415,7 @@ QSize QPushButton::sizeHint() const
QString s(text());
bool empty = s.isEmpty();
if (empty)
- s = QString::fromLatin1("XXXX");
+ s = QStringLiteral("XXXX");
QFontMetrics fm = fontMetrics();
QSize sz = fm.size(Qt::TextShowMnemonic, s);
if(!empty || !w)