From db1ed43050eb2fd43e07cb5de00d4c0d096e29c8 Mon Sep 17 00:00:00 2001 From: Ahmad Samir Date: Wed, 16 Oct 2024 12:36:33 +0300 Subject: [PATCH] QString: add {setUtf16,setUnicode}(const char16_t*) overloads This is a first step for adding QT_NO_INTEGRAL_STRINGS in the next commit. Mark setUtf16(const ushort *) as obsolete. Use the weak overload workaround, so that the call isn't ambiguous if it's called on a nullptr. [ChangeLog][QtCore][QString] Added setUtf16(const char16_t *) and setUnicode(const char16_t *) overloads. Task-number: QTBUG-125871 Change-Id: I25d12c19876466c235c0d9928aae6fd332836bf5 Reviewed-by: Thiago Macieira (cherry picked from commit be3bf632e1cf80b16475f8353e4753b38317626b) Reviewed-by: Qt Cherry-pick Bot --- src/corelib/text/qstring.cpp | 20 +++++++++++++--- src/corelib/text/qstring.h | 14 ++++++++--- .../auto/corelib/text/qstring/tst_qstring.cpp | 24 ++++++++++++++++--- 3 files changed, 49 insertions(+), 9 deletions(-) diff --git a/src/corelib/text/qstring.cpp b/src/corelib/text/qstring.cpp index 99958f69821..76d3c6b94ec 100644 --- a/src/corelib/text/qstring.cpp +++ b/src/corelib/text/qstring.cpp @@ -6143,7 +6143,16 @@ QString& QString::setUnicode(const QChar *unicode, qsizetype size) } /*! - \fn QString &QString::setUtf16(const ushort *unicode, qsizetype size) + \fn QString::setUnicode(const char16_t *unicode, qsizetype size) + \overload + \since 6.9 + + \sa unicode(), setUtf16() +*/ + +/*! + \fn QString::setUtf16(const char16_t *unicode, qsizetype size) + \since 6.9 Resizes the string to \a size characters and copies \a unicode into the string. @@ -6157,6 +6166,11 @@ QString& QString::setUnicode(const QChar *unicode, qsizetype size) \sa utf16(), setUnicode() */ +/*! + \fn QString &QString::setUtf16(const ushort *unicode, qsizetype size) + \obsolete Use the \c char16_t overload instead. +*/ + /*! \fn QString QString::simplified() const @@ -7586,8 +7600,8 @@ QString QString::vasprintf(const char *cformat, va_list ap) } case 's': { if (length_mod == lm_l) { - const ushort *buff = va_arg(ap, const ushort*); - const ushort *ch = buff; + const char16_t *buff = va_arg(ap, const char16_t*); + const auto *ch = buff; while (precision != 0 && *ch != 0) { ++ch; --precision; diff --git a/src/corelib/text/qstring.h b/src/corelib/text/qstring.h index f3759718bc8..0cf01ff2c3a 100644 --- a/src/corelib/text/qstring.h +++ b/src/corelib/text/qstring.h @@ -768,7 +768,17 @@ public: QString &setRawData(const QChar *unicode, qsizetype size); QString &setUnicode(const QChar *unicode, qsizetype size); - inline QString &setUtf16(const ushort *utf16, qsizetype size); // ### Qt 7 char16_t + Q_WEAK_OVERLOAD + QString &setUnicode(const char16_t *utf16, qsizetype size) + { return setUnicode(reinterpret_cast(utf16), size); } + QString &setUtf16(const char16_t *utf16, qsizetype size) + { return setUnicode(reinterpret_cast(utf16), size); } + +#if !QT_CORE_REMOVED_SINCE(6, 9) + Q_WEAK_OVERLOAD +#endif + QString &setUtf16(const ushort *autf16, qsizetype asize) + { return setUnicode(reinterpret_cast(autf16), asize); } int compare(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept; int compare(QLatin1StringView other, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept; @@ -1417,8 +1427,6 @@ void QString::squeeze() d.clearFlag(Data::CapacityReserved); } -QString &QString::setUtf16(const ushort *autf16, qsizetype asize) -{ return setUnicode(reinterpret_cast(autf16), asize); } QChar &QString::operator[](qsizetype i) { verify(i, 1); return data()[i]; } QChar &QString::front() { return operator[](0); } diff --git a/tests/auto/corelib/text/qstring/tst_qstring.cpp b/tests/auto/corelib/text/qstring/tst_qstring.cpp index 051161d1f92..8390ecd0325 100644 --- a/tests/auto/corelib/text/qstring/tst_qstring.cpp +++ b/tests/auto/corelib/text/qstring/tst_qstring.cpp @@ -5937,14 +5937,15 @@ void tst_QString::setRawData() void tst_QString::setUnicode() { const QChar ptr[] = { u'ሴ', QChar(0x0000) }; + const char16_t utf16[] = { u'ሴ', 0x0000 }; QTest::ThrowOnFailEnabler throwOnFail; auto doTest = [](const auto ptr, QString &str) mutable { - // make sure that the data is copied - QVERIFY(str.constData() != ptr); + // make sure that the data was copied + QCOMPARE_NE(str.constData(), reinterpret_cast(ptr)); QVERIFY(str.isDetached()); - QCOMPARE(str, QString(ptr, 1)); + QCOMPARE(str, QString(reinterpret_cast(ptr), 1)); // make sure that the string is resized, even if the data is nullptr str = u"test"_s; @@ -5959,6 +5960,23 @@ void tst_QString::setUnicode() QVERIFY(!str.isDetached()); str.setUnicode(ptr, 1); doTest(ptr, str); + str.setUnicode(nullptr, 0); + } + + { + QString str; + QVERIFY(!str.isDetached()); + str.setUnicode(utf16, 1); + doTest(utf16, str); + str.setUnicode(nullptr, 0); + } + + { + QString str; + QVERIFY(!str.isDetached()); + str.setUtf16(utf16, 1); + doTest(utf16, str); + str.setUtf16(nullptr, 0); } }