From 4b278fd9989b5a4a1140a2af30d2852cd12e506e Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Fri, 14 Feb 2025 08:50:10 -0800 Subject: [PATCH] QString: add fromRawData with a char16_t pointer And I've updated the documentation to use char16_t too, making this the primary (recommended) overload over the QChar one. [ChangeLog][QtCore][QString] Added fromRawData() overload taking char16_t* (was: only QChar*). Change-Id: Iad8dd905a494706d72c4fffd40ded1cbf7122597 Reviewed-by: Marc Mutz --- src/corelib/doc/snippets/qstring/main.cpp | 5 ++--- src/corelib/text/qstring.cpp | 8 ++++++++ src/corelib/text/qstring.h | 4 ++++ 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/corelib/doc/snippets/qstring/main.cpp b/src/corelib/doc/snippets/qstring/main.cpp index 5e3fa647dd1..578c7ae53aa 100644 --- a/src/corelib/doc/snippets/qstring/main.cpp +++ b/src/corelib/doc/snippets/qstring/main.cpp @@ -344,12 +344,11 @@ void Widget::fromRawDataFunction() { //! [22] QRegularExpression pattern("\u00A4"); - static const QChar unicode[] = { + static const char16_t unicode[] = { 0x005A, 0x007F, 0x00A4, 0x0060, 0x1009, 0x0020, 0x0020}; - qsizetype size = sizeof(unicode) / sizeof(QChar); - QString str = QString::fromRawData(unicode, size); + QString str = QString::fromRawData(unicode, std::size(unicode)); if (str.contains(pattern) { // ... //! [22] //! [23] diff --git a/src/corelib/text/qstring.cpp b/src/corelib/text/qstring.cpp index e60f7ff6554..d1f70005437 100644 --- a/src/corelib/text/qstring.cpp +++ b/src/corelib/text/qstring.cpp @@ -9378,6 +9378,9 @@ QString::iterator QString::erase(QString::const_iterator first, QString::const_i */ /*! + \fn QString QString::fromRawData(const char16_t *unicode, qsizetype size) + \since 6.10 + Constructs a QString that uses the first \a size Unicode characters in the array \a unicode. The data in \a unicode is \e not copied. The caller must be able to guarantee that \a unicode will @@ -9403,6 +9406,11 @@ QString::iterator QString::erase(QString::const_iterator first, QString::const_i \sa fromUtf16(), setRawData(), data(), constData(), nullTerminate(), nullTerminated() */ + +/*! + \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)); diff --git a/src/corelib/text/qstring.h b/src/corelib/text/qstring.h index 6157f1fa8ec..a4c35cc4a6d 100644 --- a/src/corelib/text/qstring.h +++ b/src/corelib/text/qstring.h @@ -782,6 +782,10 @@ public: } static QString fromUtf16(const char16_t *, qsizetype size = -1); 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)); + } static QString fromRawData(const QChar *, qsizetype size); #if QT_DEPRECATED_SINCE(6, 0)