diff --git a/src/corelib/tools/qstring.cpp b/src/corelib/tools/qstring.cpp index 857b559aa4..da010eca0c 100644 --- a/src/corelib/tools/qstring.cpp +++ b/src/corelib/tools/qstring.cpp @@ -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) diff --git a/src/corelib/tools/qstring.h b/src/corelib/tools/qstring.h index 23972d196c..e9a205dfdf 100644 --- a/src/corelib/tools/qstring.h +++ b/src/corelib/tools/qstring.h @@ -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(array)); - } + return QStringView(*this).toWCharArray(array); } QT_WARNING_POP diff --git a/src/corelib/tools/qstringview.cpp b/src/corelib/tools/qstringview.cpp index b97e989110..c863ca7ce2 100644 --- a/src/corelib/tools/qstringview.cpp +++ b/src/corelib/tools/qstringview.cpp @@ -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(data()), int(size()), + reinterpret_cast(array)); + } +} + QT_END_NAMESPACE diff --git a/src/corelib/tools/qstringview.h b/src/corelib/tools/qstringview.h index 2e95c2b218..4a900b5e89 100644 --- a/src/corelib/tools/qstringview.h +++ b/src/corelib/tools/qstringview.h @@ -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: //