"Fossies" - the Fresh Open Source Software Archive

Member "cutter-1.8.2/src/common/HexAsciiHighlighter.cpp" (20 May 2019, 1575 Bytes) of package /linux/privat/cutter-1.8.2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "HexAsciiHighlighter.cpp" see the Fossies "Dox" file reference documentation.

    1 #include <QtGui>
    2 
    3 #include "common/HexAsciiHighlighter.h"
    4 
    5 AsciiHighlighter::AsciiHighlighter(QTextDocument *parent)
    6     : QSyntaxHighlighter(parent)
    7 {
    8     HighlightingRule rule;
    9 
   10     asciiFormat.setForeground(QColor(65, 131, 215));
   11     rule.pattern = QRegExp("\\b[A-Za-z0-9]+\\b");
   12     rule.format = asciiFormat;
   13     highlightingRules.append(rule);
   14 
   15     commentStartExpression = QRegExp("/\\*");
   16     commentEndExpression = QRegExp("\\*/");
   17 }
   18 
   19 void AsciiHighlighter::highlightBlock(const QString &text)
   20 {
   21     for (const HighlightingRule &rule : highlightingRules) {
   22         QRegExp expression(rule.pattern);
   23         int index = expression.indexIn(text);
   24         while (index >= 0) {
   25             int length = expression.matchedLength();
   26             setFormat(index, length, rule.format);
   27             index = expression.indexIn(text, index + length);
   28         }
   29     }
   30     setCurrentBlockState(0);
   31 
   32     int startIndex = 0;
   33     if (previousBlockState() != 1)
   34         startIndex = commentStartExpression.indexIn(text);
   35 
   36     while (startIndex >= 0) {
   37         int endIndex = commentEndExpression.indexIn(text, startIndex);
   38         int commentLength;
   39         if (endIndex == -1) {
   40             setCurrentBlockState(1);
   41             commentLength = text.length() - startIndex;
   42         } else {
   43             commentLength = endIndex - startIndex
   44                             + commentEndExpression.matchedLength();
   45         }
   46         setFormat(startIndex, commentLength, multiLineCommentFormat);
   47         startIndex = commentStartExpression.indexIn(text, startIndex + commentLength);
   48     }
   49 }