File indexing completed on 2024-04-28 13:39:48

0001 /*
0002 SPDX-FileCopyrightText: 2001-2004,2009 Otto Bruggeman <bruggie@gmail.com>
0003 SPDX-FileCopyrightText: 2001-2003 John Firebaugh <jfirebaugh@kde.org>
0004 
0005 SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #include "diffhunk.h"
0009 
0010 #include <komparediffdebug.h>
0011 
0012 using namespace Diff2;
0013 
0014 DiffHunk::DiffHunk(int sourceLine, int destinationLine, const QString& function, Type type) :
0015     m_sourceLine(sourceLine),
0016     m_destinationLine(destinationLine),
0017     m_function(function),
0018     m_type(type)
0019 {
0020 }
0021 
0022 DiffHunk::~DiffHunk()
0023 {
0024 }
0025 
0026 void DiffHunk::add(Difference* diff)
0027 {
0028     m_differences.append(diff);
0029 }
0030 
0031 int DiffHunk::sourceLineCount() const
0032 {
0033     DifferenceListConstIterator diffIt = m_differences.begin();
0034     DifferenceListConstIterator dEnd   = m_differences.end();
0035 
0036     int lineCount = 0;
0037 
0038     for (; diffIt != dEnd; ++diffIt)
0039         lineCount += (*diffIt)->sourceLineCount();
0040 
0041     return lineCount;
0042 }
0043 
0044 int DiffHunk::destinationLineCount() const
0045 {
0046     DifferenceListConstIterator diffIt = m_differences.begin();
0047     DifferenceListConstIterator dEnd   = m_differences.end();
0048 
0049     int lineCount = 0;
0050 
0051     for (; diffIt != dEnd; ++diffIt)
0052         lineCount += (*diffIt)->destinationLineCount();
0053 
0054     return lineCount;
0055 }
0056 
0057 QString DiffHunk::recreateHunk() const
0058 {
0059     QString hunk;
0060     QString differences;
0061 
0062     // recreate body
0063     DifferenceListConstIterator diffIt = m_differences.begin();
0064     DifferenceListConstIterator dEnd   = m_differences.end();
0065 
0066     int slc = 0; // source line count
0067     int dlc = 0; // destination line count
0068     for (; diffIt != dEnd; ++diffIt)
0069     {
0070         switch ((*diffIt)->type())
0071         {
0072         case Difference::Unchanged:
0073         case Difference::Change:
0074             slc += (*diffIt)->sourceLineCount();
0075             dlc += (*diffIt)->destinationLineCount();
0076             break;
0077         case Difference::Insert:
0078             dlc += (*diffIt)->destinationLineCount();
0079             break;
0080         case Difference::Delete:
0081             slc += (*diffIt)->sourceLineCount();
0082             break;
0083         }
0084         differences += (*diffIt)->recreateDifference();
0085     }
0086 
0087     // recreate header
0088     hunk += QStringLiteral("@@ -%1,%3 +%2,%4 @@")
0089             .arg(m_sourceLine)
0090             .arg(m_destinationLine)
0091             .arg(slc)
0092             .arg(dlc);
0093 
0094     if (!m_function.isEmpty())
0095         hunk += QLatin1Char(' ') + m_function;
0096 
0097     hunk += QLatin1Char('\n');
0098 
0099     hunk += differences;
0100 
0101     qCDebug(LIBKOMPAREDIFF2) << hunk;
0102     return hunk;
0103 }