Add QStringView::toWCharArray() to match QString

QCollator needs it to add support for QStringView.
In any case, it extends the mirror of QString's API.
Naturally, we can reimplement QString's version using it.

Change-Id: I5a23a3f2a98c7d59597b5e935542a93764b5e350
Reviewed-by: Paul Wicking <paul.wicking@qt.io>
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
This commit is contained in:
Edward Welbourne 2019-03-06 14:22:06 +01:00
parent 01a5434252
commit e89fbd8c3a
4 changed files with 35 additions and 7 deletions

View File

@ -2086,7 +2086,7 @@ int QString::toUcs4_helper(const ushort *uc, int length, uint *out)
\note This function does not append a null character to the array.
\sa utf16(), toUcs4(), toLatin1(), toUtf8(), toLocal8Bit(), toStdWString()
\sa utf16(), toUcs4(), toLatin1(), toUtf8(), toLocal8Bit(), toStdWString(), QStringView::toWCharArray()
*/
/*! \fn QString::QString(const QString &other)

View File

@ -1021,12 +1021,7 @@ QT_WARNING_DISABLE_INTEL(111) // "statement is unreachable"
inline int QString::toWCharArray(wchar_t *array) const
{
if (sizeof(wchar_t) == sizeof(QChar)) {
memcpy(array, d->data(), sizeof(QChar) * size());
return size();
} else {
return toUcs4_helper(d->data(), size(), reinterpret_cast<uint *>(array));
}
return QStringView(*this).toWCharArray(array);
}
QT_WARNING_POP

View File

@ -38,6 +38,7 @@
****************************************************************************/
#include "qstringview.h"
#include "qstring.h"
QT_BEGIN_NAMESPACE
@ -794,4 +795,34 @@ QT_BEGIN_NAMESPACE
\sa QString::isRightToLeft()
*/
/*!
\since 5.14
Transcribes this string into the given \a array.
Caller is responsible for ensuring \a array is large enough to hold the \t
wchar_t encoding of this string (allocating the array with the same length
as the string is always sufficient). The array is encoded in UTF-16 on
platforms where \t wchar_t is 2 bytes wide (e.g. Windows); otherwise (Unix
systems), \t wchar_t is assumed to be 4 bytes wide and the data is written
in UCS-4.
\note This function writes no null terminator to the end of \a array.
Returns the number of \t wchar_t entries written to \a array.
\sa QString::toWCharArray()
*/
int QStringView::toWCharArray(wchar_t *array) const
{
if (sizeof(wchar_t) == sizeof(QChar)) {
memcpy(array, data(), sizeof(QChar) * size());
return size();
} else {
return QString::toUcs4_helper(reinterpret_cast<const ushort *>(data()), int(size()),
reinterpret_cast<uint *>(array));
}
}
QT_END_NAMESPACE

View File

@ -272,6 +272,8 @@ public:
Q_REQUIRED_RESULT bool isRightToLeft() const Q_DECL_NOTHROW
{ return QtPrivate::isRightToLeft(*this); }
Q_REQUIRED_RESULT Q_CORE_EXPORT int toWCharArray(wchar_t *array) const;
//
// STL compatibility API:
//