diff --git a/src/CommentParser.cpp b/src/CommentParser.cpp index 7f34c2a..180142e 100644 --- a/src/CommentParser.cpp +++ b/src/CommentParser.cpp @@ -1,167 +1,167 @@ /** * Copyright (C) 2019 Michael Reeves * * This file is part of KDiff3. * * KDiff3 is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 2 of the License, or * (at your option) any later version. * * KDiff3 is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with KDiff3. If not, see . */ #include "CommentParser.h" #include #include #include #include void DefaultCommentParser::processChar(const QString &line, const QChar &inChar) { if(!bIsEscaped) { switch(inChar.unicode()) { case '\\': if(bInString) bIsEscaped = true; break; case '\'': case '"': if(!inComment()) { if(!bInString) { bInString = true; mStartChar = inChar; } else if(mStartChar == inChar) { bInString = false; } } break; case '/': if(bInString) break; if(!inComment() && mLastChar == '/') { mCommentType = singleLine; - mIsPureComment = line.startsWith(QLatin1String("//")) ? yes : no; + mIsPureComment = line.startsWith(QLatin1String("//")); lastComment.startOffset = offset - 1; } else if(mLastChar == '*' && mCommentType == multiLine) { //ending multi-line comment mCommentType = none; lastComment.endOffset = offset + 1; //include last char in offset comments.push_back(lastComment); if(!isFirstLine) - mIsPureComment = line.endsWith(QLatin1String("*/")) ? yes : mIsPureComment; + mIsPureComment = line.endsWith(QLatin1String("*/")) ? true : mIsPureComment; } break; case '*': if(bInString) break; if(mLastChar == '/' && !inComment()) { mCommentType = multiLine; - mIsPureComment = line.startsWith(QLatin1String("/*")) ? yes : mIsPureComment; + mIsPureComment = line.startsWith(QLatin1String("/*")) ? true : mIsPureComment; isFirstLine = true; lastComment.startOffset = offset - 1; } break; case '\n': if(mCommentType == singleLine) { mCommentType = none; lastComment.endOffset = offset; comments.push_back(lastComment); } if(mCommentType == multiLine && !isFirstLine) { - mIsPureComment = yes; + mIsPureComment = true; } if(lastComment.startOffset > 0 && lastComment.endOffset == 0) { lastComment.endOffset = offset; comments.push_back(lastComment); } isFirstLine = false; break; default: if(inComment()) { break; } - mIsPureComment = no; + mIsPureComment = false; break; } mLastChar = inChar; } else { bIsEscaped = false; mLastChar = QChar(); } ++offset; }; /* Find comments if any and set is pure comment flag it it has nothing but whitespace and comments. */ void DefaultCommentParser::processLine(const QString &line) { offset = line.indexOf(QRegularExpression("[\\S]", QRegularExpression::UseUnicodePropertiesOption)); lastComment.startOffset = lastComment.endOffset = 0; //reset these for each line comments.clear(); //remove trailing and ending spaces. const QString trimmedLine = line.trimmed(); for(const QChar &c : trimmedLine) { processChar(trimmedLine, c); } processChar(trimmedLine, '\n'); } /* Modifies the input data, and replaces comments with whitespace when the line contains other data too. */ void DefaultCommentParser::removeComment(QString &line) { if(isPureComment() || lastComment.startOffset == lastComment.endOffset) return; for(const CommentRange &range : comments) { /* Q_ASSERT isn't useful during auto testing as it causes the QTest not to show the actual line that the test failed on. */ #ifndef AUTOTEST Q_ASSERT(range.endOffset <= line.length() && range.startOffset <= line.length()); Q_ASSERT(range.endOffset >= range.startOffset); #endif qint32 size = range.endOffset - range.startOffset; line.replace(range.startOffset, size, QString(" ").repeated(size)); } } diff --git a/src/CommentParser.h b/src/CommentParser.h index 4d76c11..3de0f2f 100644 --- a/src/CommentParser.h +++ b/src/CommentParser.h @@ -1,79 +1,77 @@ /** * Copyright (C) 2019 Michael Reeves * * This file is part of KDiff3. * * KDiff3 is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 2 of the License, or * (at your option) any later version. * * KDiff3 is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with KDiff3. If not, see . */ #ifndef COMMENTPARSER_H #define COMMENTPARSER_H #include #include #include class CommentParser { public: virtual void processChar(const QString &line, const QChar &inChar) = 0; virtual void processLine(const QString &line) = 0; virtual void removeComment(QString &line) = 0; virtual bool inComment() const = 0; virtual bool isPureComment() const = 0; virtual ~CommentParser(){}; }; class DefaultCommentParser : public CommentParser { private: typedef enum {none, singleLine, multiLine}CommentType; - typedef enum {no, yes, unknown}TriState; - public: void processLine(const QString &line) override; inline bool inComment() const override { return mCommentType != none; }; - inline bool isPureComment() const override { return mIsPureComment == yes; }; + inline bool isPureComment() const override { return mIsPureComment; }; void removeComment(QString &line) override; protected: friend class CommentParserTest; void processChar(const QString &line, const QChar &inChar) override; //For tests only. inline bool isEscaped(){ return bIsEscaped; } inline bool inString(){ return bInString; } private: QChar mLastChar, mStartChar; struct CommentRange { qint32 startOffset = 0; qint32 endOffset = 0; }; qint32 offset = -1; CommentRange lastComment; std::vector comments; bool isFirstLine = false; - TriState mIsPureComment = unknown; + bool mIsPureComment = false; bool bInString = false; bool bIsEscaped = false; CommentType mCommentType = none; }; #endif // !COMMENTPASER_H