diff options
| author | Marc Mutz <marc.mutz@qt.io> | 2025-07-17 08:31:43 +0200 |
|---|---|---|
| committer | Marc Mutz <marc.mutz@qt.io> | 2025-07-19 07:54:12 +0200 |
| commit | 5fca6c51ce91c98ebe4da14838a1651e40a7d904 (patch) | |
| tree | 736306a2998381546612e19d609cd988439a44d0 /src/corelib/io/qdebug.cpp | |
| parent | c1e9af8d6c05ff38818a86055d0e58683548da70 (diff) | |
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 <edward.welbourne@qt.io>
Diffstat (limited to 'src/corelib/io/qdebug.cpp')
| -rw-r--r-- | src/corelib/io/qdebug.cpp | 6 |
1 files changed, 3 insertions, 3 deletions
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<const QChar *>(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<QChar *>(buf), buflen); + d->write(QStringView{buf, buflen}); } d->write(quote); |
