speed up cpp highlighter

This commit is contained in:
Roy Qu 2022-10-19 19:44:15 +08:00
parent acbe5a1c94
commit 2661d6145e
2 changed files with 154 additions and 152 deletions

View File

@ -293,18 +293,18 @@ void CppHighlighter::andSymbolProc()
void CppHighlighter::ansiCppProc() void CppHighlighter::ansiCppProc()
{ {
mTokenId = TokenId::Comment; mTokenId = TokenId::Comment;
if (mLine[mRun]==0) { if (mRun>=mLineSize) {
nullProc(); nullProc();
if ( (mRun<1) || (mLine[mRun-1]!='\\')) { if ( (mRun<1) || (mLine[mRun-1]!='\\')) {
mRange.state = RangeState::rsUnknown; mRange.state = RangeState::rsUnknown;
return; return;
} }
} }
while (mLine[mRun]!=0) { while (mRun<mLineSize) {
mRun+=1; mRun+=1;
} }
mRange.state = RangeState::rsCppCommentEnded; mRange.state = RangeState::rsCppCommentEnded;
if (mLine[mRun-1] == '\\' && mLine[mRun]==0) { // continues on next line if (mLine[mRun-1] == '\\' && mRun == mLineSize-1) { // continues on next line
mRange.state = RangeState::rsCppComment; mRange.state = RangeState::rsCppComment;
} }
} }
@ -313,11 +313,11 @@ void CppHighlighter::ansiCProc()
{ {
bool finishProcess = false; bool finishProcess = false;
mTokenId = TokenId::Comment; mTokenId = TokenId::Comment;
if (mLine[mRun].unicode() == 0) { if (mRun>=mLineSize) {
nullProc(); nullProc();
return; return;
} }
while (mLine[mRun]!=0) { while (mRun<=mLineSize) {
switch(mLine[mRun].unicode()) { switch(mLine[mRun].unicode()) {
case '*': case '*':
if (mLine[mRun+1] == '/') { if (mLine[mRun+1] == '/') {
@ -327,7 +327,7 @@ void CppHighlighter::ansiCProc()
} else if (mRange.state == RangeState::rsAnsiCAsmBlock){ } else if (mRange.state == RangeState::rsAnsiCAsmBlock){
mRange.state = RangeState::rsAsmBlock; mRange.state = RangeState::rsAsmBlock;
} else if (mRange.state == RangeState::rsDirectiveComment && } else if (mRange.state == RangeState::rsDirectiveComment &&
mLine[mRun] != 0 && mLine[mRun]!='\r' && mLine[mRun]!='\n') { mRun<mLineSize && mLine[mRun]!='\r' && mLine[mRun]!='\n') {
mRange.state = RangeState::rsMultiLineDirective; mRange.state = RangeState::rsMultiLineDirective;
} else { } else {
mRange.state = RangeState::rsUnknown; mRange.state = RangeState::rsUnknown;
@ -354,7 +354,7 @@ void CppHighlighter::asciiCharProc()
} }
} }
mRun+=1; mRun+=1;
} while (mLine[mRun]!=0 && mLine[mRun]!='\''); } while (mRun < mLineSize && mLine[mRun]!='\'');
if (mLine[mRun] == '\'') if (mLine[mRun] == '\'')
mRun+=1; mRun+=1;
mRange.state = RangeState::rsUnknown; mRange.state = RangeState::rsUnknown;
@ -434,7 +434,7 @@ void CppHighlighter::commaProc()
void CppHighlighter::directiveProc() void CppHighlighter::directiveProc()
{ {
QString preContents = mLineString.left(mRun).trimmed(); QString preContents = mLine.left(mRun).trimmed();
if (!preContents.isEmpty()) { // '#' is not first non-space char on the line, treat it as an invalid char if (!preContents.isEmpty()) { // '#' is not first non-space char on the line, treat it as an invalid char
mTokenId = TokenId::Unknown; mTokenId = TokenId::Unknown;
mRun+=1; mRun+=1;
@ -443,17 +443,17 @@ void CppHighlighter::directiveProc()
mTokenId = TokenId::Directive; mTokenId = TokenId::Directive;
mRun+=1; mRun+=1;
//skip spaces //skip spaces
while (mLine[mRun]!=0 && isSpaceChar(mLine[mRun])) { while (mRun < mLineSize && isSpaceChar(mLine[mRun])) {
mRun+=1; mRun+=1;
} }
QString directive; QString directive;
while (mLine[mRun]!=0 && isIdentChar(mLine[mRun])) { while (mRun < mLineSize && isIdentChar(mLine[mRun])) {
directive+=mLine[mRun]; directive+=mLine[mRun];
mRun+=1; mRun+=1;
} }
if (directive == "define") { if (directive == "define") {
while(mLine[mRun]!=0 && isSpaceChar(mLine[mRun])) while(mRun < mLineSize && isSpaceChar(mLine[mRun]))
mRun++; mRun++;
mRange.state = RangeState::rsDefineIdentifier; mRange.state = RangeState::rsDefineIdentifier;
return; return;
@ -464,7 +464,7 @@ void CppHighlighter::directiveProc()
void CppHighlighter::defineIdentProc() void CppHighlighter::defineIdentProc()
{ {
mTokenId = TokenId::Identifier; mTokenId = TokenId::Identifier;
while(mLine[mRun]!=0 && isIdentChar(mLine[mRun])) while(mRun < mLineSize && isIdentChar(mLine[mRun]))
mRun++; mRun++;
mRange.state = RangeState::rsDefineRemaining; mRange.state = RangeState::rsDefineRemaining;
} }
@ -485,7 +485,7 @@ void CppHighlighter::defineRemainingProc()
} }
break; break;
case '\\': // yet another line? case '\\': // yet another line?
if (mLine[mRun+1] == 0) { if (mRun == mLineSize-1) {
mRun+=1; mRun+=1;
mRange.state = RangeState::rsMultiLineDirective; mRange.state = RangeState::rsMultiLineDirective;
return; return;
@ -493,14 +493,14 @@ void CppHighlighter::defineRemainingProc()
break; break;
} }
mRun+=1; mRun+=1;
} while (mLine[mRun]!=0); } while (mRun<mLineSize);
mRange.state=RangeState::rsUnknown; mRange.state=RangeState::rsUnknown;
} }
void CppHighlighter::directiveEndProc() void CppHighlighter::directiveEndProc()
{ {
mTokenId = TokenId::Directive; mTokenId = TokenId::Directive;
if (mLine[mRun] == 0) { if (mRun >= mLineSize) {
nullProc(); nullProc();
return; return;
} }
@ -518,7 +518,7 @@ void CppHighlighter::directiveEndProc()
} }
break; break;
case '\\': // yet another line? case '\\': // yet another line?
if (mLine[mRun+1] == 0) { if (mRun == mLineSize-1) {
mRun+=1; mRun+=1;
mRange.state = RangeState::rsMultiLineDirective; mRange.state = RangeState::rsMultiLineDirective;
return; return;
@ -526,7 +526,7 @@ void CppHighlighter::directiveEndProc()
break; break;
} }
mRun+=1; mRun+=1;
} while (mLine[mRun]!=0); } while (mRun < mLineSize);
} }
void CppHighlighter::equalProc() void CppHighlighter::equalProc()
@ -570,7 +570,7 @@ void CppHighlighter::identProc()
while (isIdentChar(mLine[wordEnd])) { while (isIdentChar(mLine[wordEnd])) {
wordEnd+=1; wordEnd+=1;
} }
QString word = mLineString.mid(mRun,wordEnd-mRun); QString word = mLine.mid(mRun,wordEnd-mRun);
mRun=wordEnd; mRun=wordEnd;
if (isKeyword(word)) { if (isKeyword(word)) {
mTokenId = TokenId::Key; mTokenId = TokenId::Key;
@ -680,7 +680,7 @@ void CppHighlighter::numberProc()
mRun+=1; mRun+=1;
mTokenId = TokenId::Number; mTokenId = TokenId::Number;
bool shouldExit = false; bool shouldExit = false;
while (mLine[mRun]!=0) { while (mRun<mLineSize) {
switch(mLine[mRun].unicode()) { switch(mLine[mRun].unicode()) {
case '\'': case '\'':
if (mTokenId != TokenId::Number) { if (mTokenId != TokenId::Number) {
@ -911,7 +911,7 @@ void CppHighlighter::rawStringProc()
mTokenId = TokenId::RawString; mTokenId = TokenId::RawString;
mRange.state = RangeState::rsRawString; mRange.state = RangeState::rsRawString;
while (mLine[mRun]!=0) { while (mRun<mLineSize) {
if ((!noEscaping) && (mLine[mRun]=='"')) { if ((!noEscaping) && (mLine[mRun]=='"')) {
mRun+=1; mRun+=1;
break; break;
@ -981,7 +981,7 @@ void CppHighlighter::slashProc()
mRange.state = RangeState::rsAnsiC; mRange.state = RangeState::rsAnsiC;
} }
mRun += 2; mRun += 2;
if (mLine[mRun]!=0) if (mRun < mLineSize)
ansiCProc(); ansiCProc();
break; break;
case '=': case '=':
@ -998,7 +998,7 @@ void CppHighlighter::slashProc()
void CppHighlighter::backSlashProc() void CppHighlighter::backSlashProc()
{ {
if (mLine[mRun+1]==0) { if (mRun+1==mLineSize-1) {
mTokenId = TokenId::Symbol; mTokenId = TokenId::Symbol;
mExtTokenId = ExtTokenId::BackSlash; mExtTokenId = ExtTokenId::BackSlash;
} else { } else {
@ -1051,18 +1051,23 @@ void CppHighlighter::starProc()
void CppHighlighter::stringEndProc() void CppHighlighter::stringEndProc()
{ {
mTokenId = TokenId::String; mTokenId = TokenId::String;
if (mLine[mRun]==0) { if (mRun>=mLineSize) {
nullProc(); nullProc();
return; return;
} }
mRange.state = RangeState::rsUnknown; mRange.state = RangeState::rsUnknown;
while (mLine[mRun]!=0) { while (mRun<mLineSize) {
if (mLine[mRun]=='"') { if (mLine[mRun]=='"') {
mRun += 1; mRun += 1;
break; break;
} }
if (mLine[mRun].unicode()=='\\') { if (mLine[mRun].unicode()=='\\') {
if (mRun == mLineSize-1) {
mRun+=1;
mRange.state = RangeState::rsMultiLineString;
return;
}
switch(mLine[mRun+1].unicode()) { switch(mLine[mRun+1].unicode()) {
case '\'': case '\'':
case '"': case '"':
@ -1090,10 +1095,6 @@ void CppHighlighter::stringEndProc()
case 'U': case 'U':
mRange.state = RangeState::rsMultiLineStringEscapeSeq; mRange.state = RangeState::rsMultiLineStringEscapeSeq;
return; return;
case 0:
mRun+=1;
mRange.state = RangeState::rsMultiLineString;
return;
} }
} }
mRun += 1; mRun += 1;
@ -1184,18 +1185,23 @@ void CppHighlighter::stringEscapeSeqProc()
void CppHighlighter::stringProc() void CppHighlighter::stringProc()
{ {
if (mLine[mRun] == 0) { if (mRun >= mLineSize) {
mRange.state = RangeState::rsUnknown; mRange.state = RangeState::rsUnknown;
return; return;
} }
mTokenId = TokenId::String; mTokenId = TokenId::String;
mRange.state = RangeState::rsString; mRange.state = RangeState::rsString;
while (mLine[mRun]!=0) { while (mRun < mLineSize) {
if (mLine[mRun]=='"') { if (mLine[mRun]=='"') {
mRun+=1; mRun+=1;
break; break;
} }
if (mLine[mRun].unicode()=='\\') { if (mLine[mRun].unicode()=='\\') {
if (mRun == mLineSize-1) {
mRun+=1;
mRange.state = RangeState::rsMultiLineString;
return;
}
switch(mLine[mRun+1].unicode()) { switch(mLine[mRun+1].unicode()) {
case '\'': case '\'':
case '"': case '"':
@ -1223,10 +1229,6 @@ void CppHighlighter::stringProc()
case 'U': case 'U':
mRange.state = RangeState::rsStringEscapeSeq; mRange.state = RangeState::rsStringEscapeSeq;
return; return;
case 0:
mRun+=1;
mRange.state = RangeState::rsMultiLineString;
return;
} }
} }
mRun+=1; mRun+=1;
@ -1238,7 +1240,7 @@ void CppHighlighter::stringStartProc()
{ {
mTokenId = TokenId::String; mTokenId = TokenId::String;
mRun += 1; mRun += 1;
if (mLine[mRun]==0) { if (mRun>=mLineSize) {
mRange.state = RangeState::rsUnknown; mRange.state = RangeState::rsUnknown;
return; return;
} }
@ -1272,120 +1274,121 @@ void CppHighlighter::xorSymbolProc()
void CppHighlighter::processChar() void CppHighlighter::processChar()
{ {
switch(mLine[mRun].unicode()) { if (mRun>=mLineSize) {
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:
nullProc(); nullProc();
break; } else {
case '0': switch(mLine[mRun].unicode()) {
case '1': case '&':
case '2': andSymbolProc();
case '3': break;
case '4': case '\'':
case '5': asciiCharProc();
case '6': break;
case '7': case '@':
case '8': atSymbolProc();
case '9': break;
numberProc(); case '}':
break; braceCloseProc();
case '|': break;
orSymbolProc(); case '{':
break; braceOpenProc();
case '+': break;
plusProc(); case '\r':
break; case '\n':
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(); spaceProc();
} else { break;
unknownProc(); 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 QString CppHighlighter::getToken() const
{ {
return mLineString.mid(mTokenPos,mRun-mTokenPos); return mLine.mid(mTokenPos,mRun-mTokenPos);
} }
PHighlighterAttribute CppHighlighter::getTokenAttribute() const PHighlighterAttribute CppHighlighter::getTokenAttribute() const
@ -1563,8 +1566,8 @@ void CppHighlighter::next()
void CppHighlighter::setLine(const QString &newLine, int lineNumber) void CppHighlighter::setLine(const QString &newLine, int lineNumber)
{ {
mLineString = newLine; mLine = newLine;
mLine = mLineString.data(); mLineSize = mLine.size();
mLineNumber = lineNumber; mLineNumber = lineNumber;
mRun = 0; mRun = 0;
mRange.leftBraces = 0; mRange.leftBraces = 0;

View File

@ -154,8 +154,7 @@ private:
bool mAsmStart; bool mAsmStart;
HighlighterState mRange; HighlighterState mRange;
// SynRangeState mSpaceRange; // SynRangeState mSpaceRange;
QString mLineString; QString mLine;
QChar* mLine;
int mLineSize; int mLineSize;
int mRun; int mRun;
int mStringLen; int mStringLen;