From cf2f10268461d7e0dbd8331fbd1c83bf66c4b8c6 Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Tue, 27 Aug 2024 10:36:55 -0700 Subject: [PATCH] Make QString::fromRawData() inline And make it and the QByteArray equivalent use the QArrayDataPointer function of the same name, which hides the const_cast. Change-Id: I15d7b43afb6269eba411fffd496bca3f13e86a8e Reviewed-by: Marc Mutz --- src/corelib/compat/removed_api.cpp | 1 + src/corelib/text/qbytearray.h | 2 +- src/corelib/text/qstring.cpp | 4 ---- src/corelib/text/qstring.h | 9 ++++++++- 4 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/corelib/compat/removed_api.cpp b/src/corelib/compat/removed_api.cpp index 9efef0b958a..01a899418a2 100644 --- a/src/corelib/compat/removed_api.cpp +++ b/src/corelib/compat/removed_api.cpp @@ -1427,6 +1427,7 @@ QNoDebug QMessageLogger::noDebug() const noexcept return QNoDebug(); } +#include "qstring.h" // inlined API // #include "qotherheader.h" // // implement removed functions from qotherheader.h diff --git a/src/corelib/text/qbytearray.h b/src/corelib/text/qbytearray.h index 45f216b6a7d..969f1252199 100644 --- a/src/corelib/text/qbytearray.h +++ b/src/corelib/text/qbytearray.h @@ -408,7 +408,7 @@ public: [[nodiscard]] static QByteArray number(double, char format = 'g', int precision = 6); [[nodiscard]] static QByteArray fromRawData(const char *data, qsizetype size) { - return QByteArray(DataPointer(nullptr, const_cast(data), size)); + return QByteArray(DataPointer::fromRawData(data, size)); } class FromBase64Result; diff --git a/src/corelib/text/qstring.cpp b/src/corelib/text/qstring.cpp index d1f70005437..9cb4e7e1377 100644 --- a/src/corelib/text/qstring.cpp +++ b/src/corelib/text/qstring.cpp @@ -9411,10 +9411,6 @@ QString::iterator QString::erase(QString::const_iterator first, QString::const_i \fn QString QString::fromRawData(const QChar *unicode, qsizetype size) \overload */ -QString QString::fromRawData(const QChar *unicode, qsizetype size) -{ - return QString(DataPointer::fromRawData(const_cast(reinterpret_cast(unicode)), size)); -} /*! \since 4.7 diff --git a/src/corelib/text/qstring.h b/src/corelib/text/qstring.h index a4c35cc4a6d..8687142cd2d 100644 --- a/src/corelib/text/qstring.h +++ b/src/corelib/text/qstring.h @@ -784,8 +784,9 @@ public: static QString fromUcs4(const char32_t *, qsizetype size = -1); static QString fromRawData(const char16_t *unicode, qsizetype size) { - return QString(DataPointer(nullptr, const_cast(unicode), size)); + return QString(DataPointer::fromRawData(unicode, size)); } + QT_CORE_INLINE_SINCE(6, 10) static QString fromRawData(const QChar *, qsizetype size); #if QT_DEPRECATED_SINCE(6, 0) @@ -1651,6 +1652,12 @@ qsizetype QString::lastIndexOf(QChar ch, qsizetype from, Qt::CaseSensitivity cs) return qToStringViewIgnoringNull(*this).lastIndexOf(ch, from, cs); } #endif +#if QT_CORE_INLINE_IMPL_SINCE(6, 10) +QString QString::fromRawData(const QChar *unicode, qsizetype size) +{ + return fromRawData(reinterpret_cast(unicode), size); +} +#endif namespace QtPrivate { // used by qPrintable() and qUtf8Printable() macros