QTest: Print hex instead of octal for QByteArray QCOMPARE failures
Change-Id: Ia0aac2f09e9245339951ffff13c65b2234d01ad0 Reviewed-by: Oswald Buddenhagen <oswald.buddenhagen@theqtcompany.com>
This commit is contained in:
parent
7acc7a8eb3
commit
7858838293
@ -88,6 +88,7 @@
|
|||||||
QT_BEGIN_NAMESPACE
|
QT_BEGIN_NAMESPACE
|
||||||
|
|
||||||
using QtMiscUtils::toHexUpper;
|
using QtMiscUtils::toHexUpper;
|
||||||
|
using QtMiscUtils::fromHex;
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
\namespace QTest
|
\namespace QTest
|
||||||
@ -2184,7 +2185,7 @@ char *toHexRepresentation(const char *ba, int length)
|
|||||||
/*!
|
/*!
|
||||||
\internal
|
\internal
|
||||||
Returns the same QByteArray but with only the ASCII characters still shown;
|
Returns the same QByteArray but with only the ASCII characters still shown;
|
||||||
everything else is replaced with \c {\OOO}.
|
everything else is replaced with \c {\xHH}.
|
||||||
*/
|
*/
|
||||||
char *toPrettyCString(const char *p, int length)
|
char *toPrettyCString(const char *p, int length)
|
||||||
{
|
{
|
||||||
@ -2193,14 +2194,30 @@ char *toPrettyCString(const char *p, int length)
|
|||||||
const char *end = p + length;
|
const char *end = p + length;
|
||||||
char *dst = buffer.data();
|
char *dst = buffer.data();
|
||||||
|
|
||||||
|
bool lastWasHexEscape = false;
|
||||||
*dst++ = '"';
|
*dst++ = '"';
|
||||||
for ( ; p != end; ++p) {
|
for ( ; p != end; ++p) {
|
||||||
|
// we can add:
|
||||||
|
// 1 byte: a single character
|
||||||
|
// 2 bytes: a simple escape sequence (\n)
|
||||||
|
// 3 bytes: "" and a character
|
||||||
|
// 4 bytes: an hex escape sequence (\xHH)
|
||||||
if (dst - buffer.data() > 246) {
|
if (dst - buffer.data() > 246) {
|
||||||
// plus the the quote, the three dots and NUL, it's 251, 252 or 255
|
// plus the the quote, the three dots and NUL, it's 255 in the worst case
|
||||||
trimmed = true;
|
trimmed = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// check if we need to insert "" to break an hex escape sequence
|
||||||
|
if (Q_UNLIKELY(lastWasHexEscape)) {
|
||||||
|
if (fromHex(*p) != -1) {
|
||||||
|
// yes, insert it
|
||||||
|
*dst++ = '"';
|
||||||
|
*dst++ = '"';
|
||||||
|
}
|
||||||
|
lastWasHexEscape = false;
|
||||||
|
}
|
||||||
|
|
||||||
if (*p < 0x7f && *p >= 0x20 && *p != '\\' && *p != '"') {
|
if (*p < 0x7f && *p >= 0x20 && *p != '\\' && *p != '"') {
|
||||||
*dst++ = *p;
|
*dst++ = *p;
|
||||||
continue;
|
continue;
|
||||||
@ -2230,10 +2247,12 @@ char *toPrettyCString(const char *p, int length)
|
|||||||
*dst++ = 't';
|
*dst++ = 't';
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
// write as octal
|
// print as hex escape
|
||||||
*dst++ = '0' + ((uchar(*p) >> 6) & 7);
|
*dst++ = 'x';
|
||||||
*dst++ = '0' + ((uchar(*p) >> 3) & 7);
|
*dst++ = toHexUpper(uchar(*p) >> 4);
|
||||||
*dst++ = '0' + ((uchar(*p)) & 7);
|
*dst++ = toHexUpper(uchar(*p));
|
||||||
|
lastWasHexEscape = true;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user