From 2661d6145ebc06af811f96cdc48b3b63f6ba97b4 Mon Sep 17 00:00:00 2001 From: Roy Qu Date: Wed, 19 Oct 2022 19:44:15 +0800 Subject: [PATCH] speed up cpp highlighter --- libs/qsynedit/qsynedit/highlighter/cpp.cpp | 303 +++++++++++---------- libs/qsynedit/qsynedit/highlighter/cpp.h | 3 +- 2 files changed, 154 insertions(+), 152 deletions(-) diff --git a/libs/qsynedit/qsynedit/highlighter/cpp.cpp b/libs/qsynedit/qsynedit/highlighter/cpp.cpp index e7f81575..8bc5497e 100644 --- a/libs/qsynedit/qsynedit/highlighter/cpp.cpp +++ b/libs/qsynedit/qsynedit/highlighter/cpp.cpp @@ -293,18 +293,18 @@ void CppHighlighter::andSymbolProc() void CppHighlighter::ansiCppProc() { mTokenId = TokenId::Comment; - if (mLine[mRun]==0) { + if (mRun>=mLineSize) { nullProc(); if ( (mRun<1) || (mLine[mRun-1]!='\\')) { mRange.state = RangeState::rsUnknown; return; } } - while (mLine[mRun]!=0) { + while (mRun=mLineSize) { nullProc(); return; } - while (mLine[mRun]!=0) { + while (mRun<=mLineSize) { switch(mLine[mRun].unicode()) { case '*': if (mLine[mRun+1] == '/') { @@ -327,7 +327,7 @@ void CppHighlighter::ansiCProc() } else if (mRange.state == RangeState::rsAnsiCAsmBlock){ mRange.state = RangeState::rsAsmBlock; } else if (mRange.state == RangeState::rsDirectiveComment && - mLine[mRun] != 0 && mLine[mRun]!='\r' && mLine[mRun]!='\n') { + mRun= mLineSize) { nullProc(); return; } @@ -518,7 +518,7 @@ void CppHighlighter::directiveEndProc() } break; case '\\': // yet another line? - if (mLine[mRun+1] == 0) { + if (mRun == mLineSize-1) { mRun+=1; mRange.state = RangeState::rsMultiLineDirective; return; @@ -526,7 +526,7 @@ void CppHighlighter::directiveEndProc() break; } mRun+=1; - } while (mLine[mRun]!=0); + } while (mRun < mLineSize); } void CppHighlighter::equalProc() @@ -570,7 +570,7 @@ void CppHighlighter::identProc() while (isIdentChar(mLine[wordEnd])) { wordEnd+=1; } - QString word = mLineString.mid(mRun,wordEnd-mRun); + QString word = mLine.mid(mRun,wordEnd-mRun); mRun=wordEnd; if (isKeyword(word)) { mTokenId = TokenId::Key; @@ -680,7 +680,7 @@ void CppHighlighter::numberProc() mRun+=1; mTokenId = TokenId::Number; bool shouldExit = false; - while (mLine[mRun]!=0) { + while (mRun= mLineSize) { mRange.state = RangeState::rsUnknown; return; } mTokenId = TokenId::String; mRange.state = RangeState::rsString; - while (mLine[mRun]!=0) { + while (mRun < mLineSize) { if (mLine[mRun]=='"') { mRun+=1; break; } if (mLine[mRun].unicode()=='\\') { + if (mRun == mLineSize-1) { + mRun+=1; + mRange.state = RangeState::rsMultiLineString; + return; + } switch(mLine[mRun+1].unicode()) { case '\'': case '"': @@ -1223,10 +1229,6 @@ void CppHighlighter::stringProc() case 'U': mRange.state = RangeState::rsStringEscapeSeq; return; - case 0: - mRun+=1; - mRange.state = RangeState::rsMultiLineString; - return; } } mRun+=1; @@ -1238,7 +1240,7 @@ void CppHighlighter::stringStartProc() { mTokenId = TokenId::String; mRun += 1; - if (mLine[mRun]==0) { + if (mRun>=mLineSize) { mRange.state = RangeState::rsUnknown; return; } @@ -1272,120 +1274,121 @@ void CppHighlighter::xorSymbolProc() void CppHighlighter::processChar() { - switch(mLine[mRun].unicode()) { - case '&': - andSymbolProc(); - break; - case '\'': - asciiCharProc(); - break; - case '@': - atSymbolProc(); - break; - case '}': - braceCloseProc(); - break; - case '{': - braceOpenProc(); - break; - case '\r': - case '\n': - spaceProc(); - break; - case ':': - colonProc(); - break; - case ',': - commaProc(); - break; - case '#': - directiveProc(); - break; - case '=': - equalProc(); - break; - case '>': - greaterProc(); - break; - case '?': - questionProc(); - break; - case '<': - lowerProc(); - break; - case '-': - minusProc(); - break; - case '%': - modSymbolProc(); - break; - case '!': - notSymbolProc(); - break; - case '\\': - backSlashProc(); - break; - case 0: + if (mRun>=mLineSize) { nullProc(); - break; - case '0': - case '1': - case '2': - case '3': - case '4': - case '5': - case '6': - case '7': - case '8': - case '9': - numberProc(); - break; - case '|': - orSymbolProc(); - break; - case '+': - plusProc(); - break; - case '.': - pointProc(); - break; - case ')': - roundCloseProc(); - break; - case '(': - roundOpenProc(); - break; - case ';': - semiColonProc(); - break; - case '/': - slashProc(); - break; - case ']': - squareCloseProc(); - break; - case '[': - squareOpenProc(); - break; - case '*': - starProc(); - break; - case '"': - stringStartProc(); - break; - case '~': - tildeProc(); - break; - case '^': - xorSymbolProc(); - break; - default: - if (isIdentChar(mLine[mRun])) { - identProc(); - } else if (isSpaceChar(mLine[mRun])) { + } else { + switch(mLine[mRun].unicode()) { + case '&': + andSymbolProc(); + break; + case '\'': + asciiCharProc(); + break; + case '@': + atSymbolProc(); + break; + case '}': + braceCloseProc(); + break; + case '{': + braceOpenProc(); + break; + case '\r': + case '\n': spaceProc(); - } else { - unknownProc(); + break; + case ':': + colonProc(); + break; + case ',': + commaProc(); + break; + case '#': + directiveProc(); + break; + case '=': + equalProc(); + break; + case '>': + greaterProc(); + break; + case '?': + questionProc(); + break; + case '<': + lowerProc(); + break; + case '-': + minusProc(); + break; + case '%': + modSymbolProc(); + break; + case '!': + notSymbolProc(); + break; + case '\\': + backSlashProc(); + break; + case '0': + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + case '8': + case '9': + numberProc(); + break; + case '|': + orSymbolProc(); + break; + case '+': + plusProc(); + break; + case '.': + pointProc(); + break; + case ')': + roundCloseProc(); + break; + case '(': + roundOpenProc(); + break; + case ';': + semiColonProc(); + break; + case '/': + slashProc(); + break; + case ']': + squareCloseProc(); + break; + case '[': + squareOpenProc(); + break; + case '*': + starProc(); + break; + case '"': + stringStartProc(); + break; + case '~': + tildeProc(); + break; + case '^': + xorSymbolProc(); + break; + default: + if (isIdentChar(mLine[mRun])) { + identProc(); + } else if (isSpaceChar(mLine[mRun])) { + spaceProc(); + } else { + unknownProc(); + } } } } @@ -1443,7 +1446,7 @@ bool CppHighlighter::eol() const QString CppHighlighter::getToken() const { - return mLineString.mid(mTokenPos,mRun-mTokenPos); + return mLine.mid(mTokenPos,mRun-mTokenPos); } PHighlighterAttribute CppHighlighter::getTokenAttribute() const @@ -1563,8 +1566,8 @@ void CppHighlighter::next() void CppHighlighter::setLine(const QString &newLine, int lineNumber) { - mLineString = newLine; - mLine = mLineString.data(); + mLine = newLine; + mLineSize = mLine.size(); mLineNumber = lineNumber; mRun = 0; mRange.leftBraces = 0; diff --git a/libs/qsynedit/qsynedit/highlighter/cpp.h b/libs/qsynedit/qsynedit/highlighter/cpp.h index 09be1c6f..20a273ed 100644 --- a/libs/qsynedit/qsynedit/highlighter/cpp.h +++ b/libs/qsynedit/qsynedit/highlighter/cpp.h @@ -154,8 +154,7 @@ private: bool mAsmStart; HighlighterState mRange; // SynRangeState mSpaceRange; - QString mLineString; - QChar* mLine; + QString mLine; int mLineSize; int mRun; int mStringLen;