QStringView: two fixes for newly-added toWCharArray()
Amends e89fbd8c3a
.
- While QString::data() never returns nullptr, QStringView::data()
may, which makes calling QStringView{}.toWCharArray() UB on Windows
(since memcpy's 2nd argument must never be nullptr, even if the size
is zero). Fix by protecting the memcpy call.
- QStringView, by design, does not use out-of-line member functions,
because calling these forces the QStringView object onto the stack.
Fix by making inline.
Also use the more efficient qToStringViewIgnoringNull(), as the result
does not depend on QString::isNull() (no characters are written
either way), and add a missing article to the function's docs.
Change-Id: I5d6b31361522812b0db8303b93c43d4b9ed11933
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
This commit is contained in:
parent
adab531771
commit
93b845464f
@ -1122,7 +1122,19 @@ QT_WARNING_DISABLE_INTEL(111) // "statement is unreachable"
|
||||
|
||||
inline int QString::toWCharArray(wchar_t *array) const
|
||||
{
|
||||
return QStringView(*this).toWCharArray(array);
|
||||
return qToStringViewIgnoringNull(*this).toWCharArray(array);
|
||||
}
|
||||
|
||||
int QStringView::toWCharArray(wchar_t *array) const
|
||||
{
|
||||
if (sizeof(wchar_t) == sizeof(QChar)) {
|
||||
if (auto src = data())
|
||||
memcpy(array, src, sizeof(QChar) * size());
|
||||
return size();
|
||||
} else {
|
||||
return QString::toUcs4_helper(reinterpret_cast<const ushort *>(data()), int(size()),
|
||||
reinterpret_cast<uint *>(array));
|
||||
}
|
||||
}
|
||||
|
||||
QT_WARNING_POP
|
||||
|
@ -867,11 +867,12 @@ QT_BEGIN_NAMESPACE
|
||||
*/
|
||||
|
||||
/*!
|
||||
\fn QStringView::toWCharArray(wchar_t *array) const
|
||||
\since 5.14
|
||||
|
||||
Transcribes this string into the given \a array.
|
||||
|
||||
Caller is responsible for ensuring \a array is large enough to hold the
|
||||
The caller is responsible for ensuring \a array is large enough to hold the
|
||||
\c 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 \c wchar_t is 2 bytes wide (e.g. Windows); otherwise (Unix
|
||||
@ -885,15 +886,4 @@ QT_BEGIN_NAMESPACE
|
||||
\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
|
||||
|
@ -294,7 +294,7 @@ public:
|
||||
Q_REQUIRED_RESULT bool isRightToLeft() const noexcept
|
||||
{ return QtPrivate::isRightToLeft(*this); }
|
||||
|
||||
Q_REQUIRED_RESULT Q_CORE_EXPORT int toWCharArray(wchar_t *array) const;
|
||||
Q_REQUIRED_RESULT inline int toWCharArray(wchar_t *array) const; // defined in qstring.h
|
||||
|
||||
//
|
||||
// STL compatibility API:
|
||||
|
Loading…
Reference in New Issue
Block a user