cutter/src/utils/HexAsciiHighlighter.cpp
Paul I bebc2ec36d More c++11 (#478)
* Replace 0 and Q_NULLPTR with nullptr
* Use c++11 foreach
2018-05-04 09:58:32 +02:00

50 lines
1.5 KiB
C++

#include <QtGui>
#include "utils/HexAsciiHighlighter.h"
AsciiHighlighter::AsciiHighlighter(QTextDocument *parent)
: QSyntaxHighlighter(parent)
{
HighlightingRule rule;
asciiFormat.setForeground(QColor(65, 131, 215));
rule.pattern = QRegExp("\\b[A-Za-z0-9]+\\b");
rule.format = asciiFormat;
highlightingRules.append(rule);
commentStartExpression = QRegExp("/\\*");
commentEndExpression = QRegExp("\\*/");
}
void AsciiHighlighter::highlightBlock(const QString &text)
{
for (const HighlightingRule &rule : highlightingRules) {
QRegExp expression(rule.pattern);
int index = expression.indexIn(text);
while (index >= 0) {
int length = expression.matchedLength();
setFormat(index, length, rule.format);
index = expression.indexIn(text, index + length);
}
}
setCurrentBlockState(0);
int startIndex = 0;
if (previousBlockState() != 1)
startIndex = commentStartExpression.indexIn(text);
while (startIndex >= 0) {
int endIndex = commentEndExpression.indexIn(text, startIndex);
int commentLength;
if (endIndex == -1) {
setCurrentBlockState(1);
commentLength = text.length() - startIndex;
} else {
commentLength = endIndex - startIndex
+ commentEndExpression.matchedLength();
}
setFormat(startIndex, commentLength, multiLineCommentFormat);
startIndex = commentStartExpression.indexIn(text, startIndex + commentLength);
}
}