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 <thiago.macieira@intel.com>
(cherry picked from commit be3bf632e1cf80b16475f8353e4753b38317626b)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Ahmad Samir 2024-10-16 12:36:33 +03:00 committed by Qt Cherry-pick Bot
parent ae6b9c0c63
commit db1ed43050
3 changed files with 49 additions and 9 deletions

View File

@ -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;

View File

@ -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<const QChar *>(utf16), size); }
QString &setUtf16(const char16_t *utf16, qsizetype size)
{ return setUnicode(reinterpret_cast<const QChar *>(utf16), size); }
#if !QT_CORE_REMOVED_SINCE(6, 9)
Q_WEAK_OVERLOAD
#endif
QString &setUtf16(const ushort *autf16, qsizetype asize)
{ return setUnicode(reinterpret_cast<const QChar *>(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<const QChar *>(autf16), asize); }
QChar &QString::operator[](qsizetype i)
{ verify(i, 1); return data()[i]; }
QChar &QString::front() { return operator[](0); }

View File

@ -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<const QChar *>(ptr));
QVERIFY(str.isDetached());
QCOMPARE(str, QString(ptr, 1));
QCOMPARE(str, QString(reinterpret_cast<const QChar *>(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);
}
}