From e89fbd8c3aa50a24e5fc02ab710ccca67fce98e2 Mon Sep 17 00:00:00 2001 From: Edward Welbourne Date: Wed, 6 Mar 2019 14:22:06 +0100 Subject: 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 Reviewed-by: Qt CI Bot --- src/corelib/tools/qstringview.cpp | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) (limited to 'src/corelib/tools/qstringview.cpp') diff --git a/src/corelib/tools/qstringview.cpp b/src/corelib/tools/qstringview.cpp index b97e989110a..c863ca7ce22 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 -- cgit v1.2.3