debugger: saner handling of unprintable chars in Locals&Expressions

Change-Id: Ia257356ec297dfa7766354d92538886ba2492b55
Reviewed-on: http://codereview.qt.nokia.com/2566
Reviewed-by: hjk <qthjk@ovi.com>
This commit is contained in:
hjk
2011-08-03 14:36:07 +02:00
committed by hjk
parent fabacc36e6
commit 2ce13e520d
3 changed files with 44 additions and 23 deletions

View File

@@ -79,7 +79,6 @@ static int generationCounter = 0;
QHash<QByteArray, int> WatchHandler::m_watcherNames; QHash<QByteArray, int> WatchHandler::m_watcherNames;
QHash<QByteArray, int> WatchHandler::m_typeFormats; QHash<QByteArray, int> WatchHandler::m_typeFormats;
int WatchHandler::m_unprintableBase = 0;
static QByteArray stripTemplate(const QByteArray &ba) static QByteArray stripTemplate(const QByteArray &ba)
{ {
@@ -87,6 +86,20 @@ static QByteArray stripTemplate(const QByteArray &ba)
return pos == -1 ? ba : ba.left(pos); return pos == -1 ? ba : ba.left(pos);
} }
static int m_unprintableBase = 8;
void WatchHandler::setUnprintableBase(int base)
{
m_unprintableBase = base;
emitAllChanged();
}
int WatchHandler::unprintableBase()
{
return m_unprintableBase;
}
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
// //
// WatchItem // WatchItem
@@ -327,7 +340,7 @@ template <class IntType> QString reformatInteger(IntType value, int format)
} }
// Format printable (char-type) characters // Format printable (char-type) characters
static inline QString reformatCharacter(int code, int format) static QString reformatCharacter(int code, int format)
{ {
const QString codeS = reformatInteger(code, format); const QString codeS = reformatInteger(code, format);
if (code < 0) // Append unsigned value. if (code < 0) // Append unsigned value.
@@ -347,7 +360,26 @@ static inline QString reformatCharacter(int code, int format)
return codeS; return codeS;
} }
static inline QString formattedValue(const WatchData &data, int format) static QString quoteUnprintable(const QString &str)
{
if (WatchHandler::unprintableBase() == 0)
return str;
QString encoded;
foreach (const QChar c, str) {
if (c.isPrint()) {
encoded += c;
} else if (WatchHandler::unprintableBase() == 8) {
encoded += QString("\\%1")
.arg(c.unicode(), 3, 8, QLatin1Char('0'));
} else {
encoded += QString("\\u%1")
.arg(c.unicode(), 4, 16, QLatin1Char('0'));
}
}
return encoded;
}
static QString formattedValue(const WatchData &data, int format)
{ {
if (isIntType(data.type)) { if (isIntType(data.type)) {
if (data.value.isEmpty()) if (data.value.isEmpty())
@@ -379,7 +411,6 @@ static inline QString formattedValue(const WatchData &data, int format)
} }
QString result = data.value; QString result = data.value;
result.replace(QLatin1Char('\n'), QLatin1String("\\n"));
if (result.startsWith(QLatin1Char('<'))) { if (result.startsWith(QLatin1Char('<'))) {
if (result == QLatin1String("<Edit>")) if (result == QLatin1String("<Edit>"))
result = WatchHandler::tr("<Edit>"); result = WatchHandler::tr("<Edit>");
@@ -405,7 +436,7 @@ static inline QString formattedValue(const WatchData &data, int format)
} }
} }
return result; return quoteUnprintable(result);
} }
// Get a pointer address from pointer values reported by the debugger. // Get a pointer address from pointer values reported by the debugger.
@@ -674,21 +705,7 @@ QVariant WatchModel::data(const QModelIndex &idx, int role) const
default: default:
break; break;
} }
if (WatchHandler::m_unprintableBase == 0)
return result; return result;
QString encoded;
foreach (const QChar c, result) {
if (c.isPrint()) {
encoded += c;
} else if (WatchHandler::m_unprintableBase == 8) {
encoded += QString("\\%1")
.arg(c.unicode(), 3, 8, QLatin1Char('0'));
} else {
encoded += QString("\\u%1")
.arg(c.unicode(), 4, 16, QLatin1Char('0'));
}
}
return encoded;
} }
case Qt::ToolTipRole: case Qt::ToolTipRole:

View File

@@ -180,8 +180,8 @@ public:
void addTypeFormats(const QByteArray &type, const QStringList &formats); void addTypeFormats(const QByteArray &type, const QStringList &formats);
void setUnprintableBase(int base) { m_unprintableBase = base; } void setUnprintableBase(int base);
int unprintableBase() const { return m_unprintableBase; } static int unprintableBase();
QByteArray watcherName(const QByteArray &exp); QByteArray watcherName(const QByteArray &exp);
void synchronizeWatchers(); void synchronizeWatchers();
@@ -217,7 +217,6 @@ private:
WatchModel *m_watchers; WatchModel *m_watchers;
WatchModel *m_tooltips; WatchModel *m_tooltips;
DebuggerEngine *m_engine; DebuggerEngine *m_engine;
static int m_unprintableBase;
}; };
} // namespace Internal } // namespace Internal

View File

@@ -1642,6 +1642,11 @@ void testQString()
{ {
QString str = "Hello "; QString str = "Hello ";
str += " big, "; str += " big, ";
str += "\t";
str += "\r";
str += "\n";
str += QLatin1Char(0);
str += QLatin1Char(1);
str += " fat "; str += " fat ";
str += " World "; str += " World ";
str += " World "; str += " World ";