diff options
author | Marc Mutz <marc.mutz@qt.io> | 2023-11-29 09:31:31 +0100 |
---|---|---|
committer | Marc Mutz <marc.mutz@qt.io> | 2023-12-06 10:46:54 +0100 |
commit | a14d3a49f0abed1d430edb96a272ca05a73f7dc5 (patch) | |
tree | be202b3349f8368dde552a81e2319c0a3548658e /src/testlib | |
parent | ba44a714067468684ffabe0c9f37bf1cb0785c3c (diff) |
QTest::toPrettyUnicode: remove magic numbers
Derive them from the chosen output buffer size instead, itself a
symbolic constant.
Pick-to: 6.6 6.5
Change-Id: I33aa351ba358b106b448f886b92e952e53bc75f9
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Reviewed-by: Jason McDonald <macadder1@gmail.com>
Diffstat (limited to 'src/testlib')
-rw-r--r-- | src/testlib/qtestcase.cpp | 12 |
1 files changed, 8 insertions, 4 deletions
diff --git a/src/testlib/qtestcase.cpp b/src/testlib/qtestcase.cpp index 999aea0182..0d91adc2c6 100644 --- a/src/testlib/qtestcase.cpp +++ b/src/testlib/qtestcase.cpp @@ -1688,26 +1688,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; } @@ -1718,7 +1723,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: |