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 <edward.welbourne@qt.io>
Reviewed-by: Jason McDonald <macadder1@gmail.com>
(cherry picked from commit a14d3a49f0abed1d430edb96a272ca05a73f7dc5)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Marc Mutz 2023-11-29 09:31:31 +01:00 committed by Qt Cherry-pick Bot
parent 5d2fab5663
commit 23ea13f9d9

View File

@ -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<char[]>(256);
auto buffer = std::make_unique<char[]>(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: