From 5fca6c51ce91c98ebe4da14838a1651e40a7d904 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Thu, 17 Jul 2025 08:31:43 +0200 Subject: Prefer QTextStreamPrivate::write(QStringView) over (ptr, n) [1/2]: casts Replace calls that needed to reinterpret_cast arguments in order to call write(const QChar *, qsizetype) with calls that use QStringView. The QStringView ctor hides the casts. In both cases, the ranges are statically known to be valid (one is over a local buffer and the other over contents we've already inspected), so the old code cannot have depended on the write(p, n) behavior, inherited from QString::append(p, n), of silently ignoring invalid ranges. So this change is safe. Requires changing an if() into an if constexpr(): while the reinterpret_cast to QChar* compiles even for uchar* input, the QStringView ctor would not. This is in preparation of removing the write(p, n) overload completely. Task-number: QTBUG-138520 Pick-to: 6.10 6.9 6.8 6.5 Change-Id: Ic4bd0ca8743fe5d2cfc08e2caa3f2cbdad29abc3 Reviewed-by: Edward Welbourne --- src/corelib/io/qdebug.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src/corelib/io/qdebug.cpp') diff --git a/src/corelib/io/qdebug.cpp b/src/corelib/io/qdebug.cpp index 5d6b5e06be6..4065853d658 100644 --- a/src/corelib/io/qdebug.cpp +++ b/src/corelib/io/qdebug.cpp @@ -215,14 +215,14 @@ static inline void putEscapedString(QTextStreamPrivate *d, const Char *begin, si lastWasHexEscape = false; } - if (sizeof(Char) == sizeof(QChar)) { + if constexpr (sizeof(Char) == sizeof(QChar)) { // Surrogate characters are category Cs (Other_Surrogate), so isPrintable = false for them qsizetype runLength = 0; while (p + runLength != end && isPrintable(p[runLength]) && p[runLength] != '\\' && p[runLength] != '"') ++runLength; if (runLength) { - d->write(reinterpret_cast(p), runLength); + d->write(QStringView{p, runLength}); p += runLength - 1; continue; } @@ -298,7 +298,7 @@ static inline void putEscapedString(QTextStreamPrivate *d, const Char *begin, si buf[5] = toHexUpper(*p); buflen = 6; } - d->write(reinterpret_cast(buf), buflen); + d->write(QStringView{buf, buflen}); } d->write(quote); -- cgit v1.2.3