QDebugStateSaver: Fix trailing space issues

~QDebug() removes any trailing space if autoInsertSpaces() is true.
However, if one uses QDebugStateSaver the global autoInsertSpaces might
be false, but a space was added by a custom operator<<.

Explicitly check for this in QDebugStateSaverPrivate::restoreState. Remove
any trailing space if the local state asks for adding trailing spaces, but
the original one doesn't. Add a trailing space if the local state doesn't
ask for one, but the global state does.

Change-Id: I243b5c76d5ed2c1ec4820da35ab6e254da1551d9
Reviewed-by: David Faure <david.faure@kdab.com>
This commit is contained in:
Kai Koehne 2014-07-22 11:58:55 +02:00
parent 66c47292bd
commit e668837b9c
2 changed files with 31 additions and 1 deletions

View File

@ -438,10 +438,18 @@ public:
}
void restoreState()
{
const bool currentSpaces = m_dbg.autoInsertSpaces();
if (currentSpaces && !m_spaces)
if (m_dbg.stream->buffer.endsWith(QLatin1Char(' ')))
m_dbg.stream->buffer.chop(1);
m_dbg.setAutoInsertSpaces(m_spaces);
m_dbg.stream->ts.d_ptr->params = m_streamParams;
if (m_dbg.stream->context.version > 1)
m_dbg.stream->flags = m_flags;
if (!currentSpaces && m_spaces)
m_dbg.stream->ts << ' ';
}
QDebug &m_dbg;
@ -475,7 +483,6 @@ QDebugStateSaver::QDebugStateSaver(QDebug &dbg)
QDebugStateSaver::~QDebugStateSaver()
{
d->restoreState();
d->m_dbg.maybeSpace();
}
QT_END_NAMESPACE

View File

@ -252,6 +252,17 @@ void tst_QDebug::debugNoQuotes() const
void tst_QDebug::stateSaver() const
{
MessageHandlerSetter mhs(myMessageHandler);
{
QDebug d = qDebug();
d << 42;
{
QDebugStateSaver saver(d);
d << 43;
}
d << 44;
}
QCOMPARE(s_msg, QString::fromLatin1("42 43 44"));
{
QDebug d = qDebug();
{
@ -271,6 +282,18 @@ void tst_QDebug::stateSaver() const
d << QStringLiteral("World");
}
QCOMPARE(s_msg, QString::fromLatin1("Hello \"World\""));
{
QDebug d = qDebug();
d.noquote().nospace() << QStringLiteral("Hello") << hex << 42;
{
QDebugStateSaver saver(d);
d.resetFormat();
d << QStringLiteral("World") << 42;
}
d << QStringLiteral("!") << 42;
}
QCOMPARE(s_msg, QString::fromLatin1("Hello2a\"World\" 42!2a"));
}
void tst_QDebug::veryLongWarningMessage() const