From 89bf65d9bb85c533cbdca60a0cc01f865b90cb10 Mon Sep 17 00:00:00 2001 From: Albert S Date: Sat, 6 Aug 2022 09:35:00 +0200 Subject: [PATCH] gui: PreviewGeneratorPlaintext: Add MAX_SNIPPETS const, remove redundant loop --- gui/previewgeneratorplaintext.cpp | 12 ++++-------- gui/previewgeneratorplaintext.h | 3 +++ 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/gui/previewgeneratorplaintext.cpp b/gui/previewgeneratorplaintext.cpp index 0ad7757..e05c3ad 100644 --- a/gui/previewgeneratorplaintext.cpp +++ b/gui/previewgeneratorplaintext.cpp @@ -14,14 +14,13 @@ QString PreviewGeneratorPlainText::generatePreviewText(QString content, RenderCo QHash countmap; - const unsigned int maxSnippets = 7; unsigned int currentSnippets = 0; for(QString &word : config.wordsToHighlight) { int lastPos = 0; int index = content.indexOf(word, lastPos, Qt::CaseInsensitive); - while(index != -1 && currentSnippets < maxSnippets) + while(index != -1 && currentSnippets < MAX_SNIPPETS) { countmap[word] = countmap.value(word, 0) + 1; @@ -57,17 +56,14 @@ QString PreviewGeneratorPlainText::generatePreviewText(QString content, RenderCo ++i; } - for(QString &word : config.wordsToHighlight) - { - resulText.replace(word, "" + word + "", Qt::CaseInsensitive); - } - QString header = "" + fileName + " "; for(QString &word : config.wordsToHighlight) { + resulText.replace(word, "" + word + "", Qt::CaseInsensitive); header += word + ": " + QString::number(countmap[word]) + " "; } - if(currentSnippets == maxSnippets) + + if(currentSnippets == MAX_SNIPPETS) { header += "(truncated)"; } diff --git a/gui/previewgeneratorplaintext.h b/gui/previewgeneratorplaintext.h index ee99279..648ebfb 100644 --- a/gui/previewgeneratorplaintext.h +++ b/gui/previewgeneratorplaintext.h @@ -4,6 +4,9 @@ class PreviewGeneratorPlainText : public PreviewGenerator { + protected: + const unsigned int MAX_SNIPPETS = 7; + public: using PreviewGenerator::PreviewGenerator; QString generatePreviewText(QString content, RenderConfig config, QString fileName);