From 23ea13f9d930363f0b697ef04fa45540707b6316 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Wed, 29 Nov 2023 09:31:31 +0100 Subject: [PATCH] QTest::toPrettyUnicode: remove magic numbers Derive them from the chosen output buffer size instead, itself a symbolic constant. Pick-to: 6.5 Change-Id: I33aa351ba358b106b448f886b92e952e53bc75f9 Reviewed-by: Edward Welbourne Reviewed-by: Jason McDonald (cherry picked from commit a14d3a49f0abed1d430edb96a272ca05a73f7dc5) Reviewed-by: Qt Cherry-pick Bot --- src/testlib/qtestcase.cpp | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/testlib/qtestcase.cpp b/src/testlib/qtestcase.cpp index b04648eea8f..10fac1c1e6b 100644 --- a/src/testlib/qtestcase.cpp +++ b/src/testlib/qtestcase.cpp @@ -1647,26 +1647,31 @@ char *toPrettyCString(const char *p, qsizetype length) } /*! + \fn char *toPrettyUnicode(QStringView string) \internal Returns the same QString but with only the ASCII characters still shown; everything else is replaced with \c {\uXXXX}. Similar to QDebug::putString(). */ + +constexpr qsizetype PrettyUnicodeMaxOutputSize = 256; + char *toPrettyUnicode(QStringView string) { auto p = string.utf16(); auto length = string.size(); // keep it simple for the vast majority of cases bool trimmed = false; - auto buffer = std::make_unique(256); + auto buffer = std::make_unique(PrettyUnicodeMaxOutputSize); const auto end = p + length; char *dst = buffer.get(); *dst++ = '"'; for ( ; p != end; ++p) { - if (dst - buffer.get() > 245) { - // plus the quote, the three dots and NUL, it's 250, 251 or 255 + // escape sequence, closing quote, the three dots and NUL + constexpr qsizetype MaxIncrement = sizeof(R"(\uXXXX"...)"); // includes NUL + if (dst - buffer.get() > PrettyUnicodeMaxOutputSize - MaxIncrement) { trimmed = true; break; } @@ -1677,7 +1682,6 @@ char *toPrettyUnicode(QStringView string) } // write as an escape sequence - // this means we may advance dst to buffer.data() + 246 or 250 *dst++ = '\\'; switch (*p) { case 0x22: