q{,Utf8}Printable: avoid creating a copy of a QString

We have this QString() constructor call to permit things that convert to
QString but aren't QString to be used in qPrintable, like a
QStringBuilder-powered fast operator+ expression, like:

    qPrintable(string1 + ": " + string2)

Unfortunately, it meant that we unnecessarily created a QString copy if
the input was already QString.

Change-Id: Iecab8770aa5840aba8edfffd1516bc94cec791a9
Reviewed-by: Olivier Goffart (Woboq GmbH) <ogoffart@woboq.com>
Reviewed-by: Oswald Buddenhagen <oswald.buddenhagen@qt.io>
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
bb10
Thiago Macieira 2018-02-25 17:41:11 -08:00
parent 9b68dc19bf
commit daa7f5375c
2 changed files with 9 additions and 2 deletions

View File

@ -747,12 +747,13 @@ Q_CORE_EXPORT Q_DECL_CONST_FUNCTION bool qSharedBuild() Q_DECL_NOTHROW;
# define QT_DEBUG
#endif
// QtPrivate::asString defined in qstring.h
#ifndef qPrintable
# define qPrintable(string) QString(string).toLocal8Bit().constData()
# define qPrintable(string) QtPrivate::asString(string).toLocal8Bit().constData()
#endif
#ifndef qUtf8Printable
# define qUtf8Printable(string) QString(string).toUtf8().constData()
# define qUtf8Printable(string) QtPrivate::asString(string).toUtf8().constData()
#endif
/*

View File

@ -1857,6 +1857,12 @@ QT_DEPRECATED inline QString escape(const QString &plain) {
#endif
}
namespace QtPrivate {
// used by qPrintable() and qUtf8Printable() macros
inline const QString &asString(const QString &s) { return s; }
inline QString &&asString(QString &&s) { return std::move(s); }
}
QT_END_NAMESPACE
#if defined(QT_USE_FAST_OPERATOR_PLUS) || defined(QT_USE_QSTRINGBUILDER)