From 656bcc4cf0976015f017f6792d5ae3525f638101 Mon Sep 17 00:00:00 2001 From: jkobus Date: Mon, 6 Oct 2014 11:18:51 +0200 Subject: [PATCH] DiffEditor: Use lambda for the common initialization Change-Id: I834754f7d52f29257e822464132d5fe24dbf5ec7 Reviewed-by: Orgad Shaneh --- src/plugins/diffeditor/diffutils.cpp | 46 ++++++++++++---------------- 1 file changed, 20 insertions(+), 26 deletions(-) diff --git a/src/plugins/diffeditor/diffutils.cpp b/src/plugins/diffeditor/diffutils.cpp index 50c524e2d6c..bcf5930b3bc 100644 --- a/src/plugins/diffeditor/diffutils.cpp +++ b/src/plugins/diffeditor/diffutils.cpp @@ -1037,20 +1037,26 @@ static QList readGitPatch(const QString &patch, bool ignoreWhitespace, QList fileDataList; - int simpleGitPos = simpleGitRegExp.indexIn(patch); - int similarityPos = similarityRegExp.indexIn(patch); - - bool simpleGitMatched = false; - int pos = -1; - if (simpleGitPos < 0) { - pos = similarityPos; - } else if (similarityPos < 0) { - pos = simpleGitPos; - simpleGitMatched = true; - } else { + bool simpleGitMatched; + int pos = 0; + auto calculateGitMatchAndPosition = [&]() { + const int simpleGitPos = simpleGitRegExp.indexIn(patch, pos, QRegExp::CaretAtOffset); + const int similarityPos = similarityRegExp.indexIn(patch, pos, QRegExp::CaretAtOffset); + if (simpleGitPos < 0) { + pos = similarityPos; + simpleGitMatched = false; + return; + } else if (similarityPos < 0) { + pos = simpleGitPos; + simpleGitMatched = true; + return; + } pos = qMin(simpleGitPos, similarityPos); simpleGitMatched = (pos == simpleGitPos); - } + }; + + // Set both pos and simpleGitMatched according to the first match: + calculateGitMatchAndPosition(); if (pos >= 0) { // git style patch readOk = true; @@ -1104,20 +1110,8 @@ static QList readGitPatch(const QString &patch, bool ignoreWhitespace, break; // either copy or rename, otherwise broken } - simpleGitPos = simpleGitRegExp.indexIn(patch, pos, QRegExp::CaretAtOffset); - similarityPos = similarityRegExp.indexIn(patch, pos, QRegExp::CaretAtOffset); - - simpleGitMatched = false; - pos = -1; - if (simpleGitPos < 0) { - pos = similarityPos; - } else if (similarityPos < 0) { - pos = simpleGitPos; - simpleGitMatched = true; - } else { - pos = qMin(simpleGitPos, similarityPos); - simpleGitMatched = (pos == simpleGitPos); - } + // give both pos and simpleGitMatched a new value for the next match + calculateGitMatchAndPosition(); } while (pos != -1); if (endOfLastHeader > 0 && readOk) {