From fd681d3e85d944feed76063555d7122383c70f51 Mon Sep 17 00:00:00 2001 From: Sona Kurazyan Date: Mon, 1 Aug 2022 13:35:33 +0200 Subject: [PATCH] Move q{,Utf8, Utf16}Printable to qstring.h Task-number: QTBUG-99313 Change-Id: I76ef84e4c90ab04a3e04c165ba46800e27ea6122 Reviewed-by: Edward Welbourne Reviewed-by: Qt CI Bot --- .../code/src_corelib_global_qglobal.cpp | 10 --- .../code/src_corelib_text_qstring.cpp | 8 +++ src/corelib/global/qglobal.cpp | 61 ------------------- src/corelib/global/qglobal.h | 18 ------ src/corelib/text/qstring.cpp | 61 +++++++++++++++++++ src/corelib/text/qstring.h | 17 ++++++ 6 files changed, 86 insertions(+), 89 deletions(-) diff --git a/src/corelib/doc/snippets/code/src_corelib_global_qglobal.cpp b/src/corelib/doc/snippets/code/src_corelib_global_qglobal.cpp index d31b62e488..4d6f566bf8 100644 --- a/src/corelib/doc/snippets/code/src_corelib_global_qglobal.cpp +++ b/src/corelib/doc/snippets/code/src_corelib_global_qglobal.cpp @@ -480,16 +480,6 @@ QString result(int type, int n) } //! [qttrid_n_noop] -//! [37] -qWarning("%s: %s", qUtf8Printable(key), qUtf8Printable(value)); -//! [37] - - -//! [qUtf16Printable] -qWarning("%ls: %ls", qUtf16Printable(key), qUtf16Printable(value)); -//! [qUtf16Printable] - - //! [38] struct Point2D { diff --git a/src/corelib/doc/snippets/code/src_corelib_text_qstring.cpp b/src/corelib/doc/snippets/code/src_corelib_text_qstring.cpp index e56cb4cdc7..2c195fd24f 100644 --- a/src/corelib/doc/snippets/code/src_corelib_text_qstring.cpp +++ b/src/corelib/doc/snippets/code/src_corelib_text_qstring.cpp @@ -69,3 +69,11 @@ if (node.hasAttribute(QStringLiteral(u"http-contents-length"))) //... //! [11] if (attribute.name() == "http-contents-length"_L1) //... //! [11] + +//! [qUtf8Printable] +qWarning("%s: %s", qUtf8Printable(key), qUtf8Printable(value)); +//! [qUtf8Printable] + +//! [qUtf16Printable] +qWarning("%ls: %ls", qUtf16Printable(key), qUtf16Printable(value)); +//! [qUtf16Printable] diff --git a/src/corelib/global/qglobal.cpp b/src/corelib/global/qglobal.cpp index 25dcbbb97e..075a9b6926 100644 --- a/src/corelib/global/qglobal.cpp +++ b/src/corelib/global/qglobal.cpp @@ -3494,67 +3494,6 @@ void qAbort() directive. */ -/*! - \macro const char *qPrintable(const QString &str) - \relates - - Returns \a str as a \c{const char *}. This is equivalent to - \a{str}.toLocal8Bit().constData(). - - The char pointer will be invalid after the statement in which - qPrintable() is used. This is because the array returned by - QString::toLocal8Bit() will fall out of scope. - - \note qDebug(), qInfo(), qWarning(), qCritical(), qFatal() expect - %s arguments to be UTF-8 encoded, while qPrintable() converts to - local 8-bit encoding. Therefore qUtf8Printable() should be used - for logging strings instead of qPrintable(). - - \sa qUtf8Printable() -*/ - -/*! - \macro const char *qUtf8Printable(const QString &str) - \relates - \since 5.4 - - Returns \a str as a \c{const char *}. This is equivalent to - \a{str}.toUtf8().constData(). - - The char pointer will be invalid after the statement in which - qUtf8Printable() is used. This is because the array returned by - QString::toUtf8() will fall out of scope. - - Example: - - \snippet code/src_corelib_global_qglobal.cpp 37 - - \sa qPrintable(), qDebug(), qInfo(), qWarning(), qCritical(), qFatal() -*/ - -/*! - \macro const wchar_t *qUtf16Printable(const QString &str) - \relates - \since 5.7 - - Returns \a str as a \c{const ushort *}, but cast to a \c{const wchar_t *} - to avoid warnings. This is equivalent to \a{str}.utf16() plus some casting. - - The only useful thing you can do with the return value of this macro is to - pass it to QString::asprintf() for use in a \c{%ls} conversion. In particular, - the return value is \e{not} a valid \c{const wchar_t*}! - - In general, the pointer will be invalid after the statement in which - qUtf16Printable() is used. This is because the pointer may have been - obtained from a temporary expression, which will fall out of scope. - - Example: - - \snippet code/src_corelib_global_qglobal.cpp qUtf16Printable - - \sa qPrintable(), qDebug(), qInfo(), qWarning(), qCritical(), qFatal() -*/ - /*! \macro Q_DECLARE_TYPEINFO(Type, Flags) \relates diff --git a/src/corelib/global/qglobal.h b/src/corelib/global/qglobal.h index d1207498af..705438cea6 100644 --- a/src/corelib/global/qglobal.h +++ b/src/corelib/global/qglobal.h @@ -633,24 +633,6 @@ Q_CORE_EXPORT Q_DECL_CONST_FUNCTION bool qSharedBuild() noexcept; # define QT_DEBUG #endif -// QtPrivate::asString defined in qstring.h -#ifndef qPrintable -# define qPrintable(string) QtPrivate::asString(string).toLocal8Bit().constData() -#endif - -#ifndef qUtf8Printable -# define qUtf8Printable(string) QtPrivate::asString(string).toUtf8().constData() -#endif - -/* - Wrap QString::utf16() with enough casts to allow passing it - to QString::asprintf("%ls") without warnings. -*/ -#ifndef qUtf16Printable -# define qUtf16Printable(string) \ - static_cast(static_cast(QtPrivate::asString(string).utf16())) -#endif - class QString; Q_DECL_COLD_FUNCTION Q_CORE_EXPORT QString qt_error_string(int errorCode = -1); diff --git a/src/corelib/text/qstring.cpp b/src/corelib/text/qstring.cpp index e8dd1a6ccd..56d1bdf605 100644 --- a/src/corelib/text/qstring.cpp +++ b/src/corelib/text/qstring.cpp @@ -11082,4 +11082,65 @@ float QStringView::toFloat(bool *ok) const \sa erase */ +/*! + \macro const char *qPrintable(const QString &str) + \relates QString + + Returns \a str as a \c{const char *}. This is equivalent to + \a{str}.toLocal8Bit().constData(). + + The char pointer will be invalid after the statement in which + qPrintable() is used. This is because the array returned by + QString::toLocal8Bit() will fall out of scope. + + \note qDebug(), qInfo(), qWarning(), qCritical(), qFatal() expect + %s arguments to be UTF-8 encoded, while qPrintable() converts to + local 8-bit encoding. Therefore qUtf8Printable() should be used + for logging strings instead of qPrintable(). + + \sa qUtf8Printable() +*/ + +/*! + \macro const char *qUtf8Printable(const QString &str) + \relates QString + \since 5.4 + + Returns \a str as a \c{const char *}. This is equivalent to + \a{str}.toUtf8().constData(). + + The char pointer will be invalid after the statement in which + qUtf8Printable() is used. This is because the array returned by + QString::toUtf8() will fall out of scope. + + Example: + + \snippet code/src_corelib_text_qstring.cpp qUtf8Printable + + \sa qPrintable(), qDebug(), qInfo(), qWarning(), qCritical(), qFatal() +*/ + +/*! + \macro const wchar_t *qUtf16Printable(const QString &str) + \relates QString + \since 5.7 + + Returns \a str as a \c{const ushort *}, but cast to a \c{const wchar_t *} + to avoid warnings. This is equivalent to \a{str}.utf16() plus some casting. + + The only useful thing you can do with the return value of this macro is to + pass it to QString::asprintf() for use in a \c{%ls} conversion. In particular, + the return value is \e{not} a valid \c{const wchar_t*}! + + In general, the pointer will be invalid after the statement in which + qUtf16Printable() is used. This is because the pointer may have been + obtained from a temporary expression, which will fall out of scope. + + Example: + + \snippet code/src_corelib_text_qstring.cpp qUtf16Printable + + \sa qPrintable(), qDebug(), qInfo(), qWarning(), qCritical(), qFatal() +*/ + QT_END_NAMESPACE diff --git a/src/corelib/text/qstring.h b/src/corelib/text/qstring.h index d426b55e76..697233e443 100644 --- a/src/corelib/text/qstring.h +++ b/src/corelib/text/qstring.h @@ -1532,6 +1532,23 @@ inline const QString &asString(const QString &s) { return s; } inline QString &&asString(QString &&s) { return std::move(s); } } +#ifndef qPrintable +# define qPrintable(string) QtPrivate::asString(string).toLocal8Bit().constData() +#endif + +#ifndef qUtf8Printable +# define qUtf8Printable(string) QtPrivate::asString(string).toUtf8().constData() +#endif + +/* + Wrap QString::utf16() with enough casts to allow passing it + to QString::asprintf("%ls") without warnings. +*/ +#ifndef qUtf16Printable +# define qUtf16Printable(string) \ + static_cast(static_cast(QtPrivate::asString(string).utf16())) +#endif + // // QStringView::arg() implementation //