KompareDiff2

diffhunk.cpp
1/*
2SPDX-FileCopyrightText: 2001-2004,2009 Otto Bruggeman <bruggie@gmail.com>
3SPDX-FileCopyrightText: 2001-2003 John Firebaugh <jfirebaugh@kde.org>
4
5SPDX-License-Identifier: GPL-2.0-or-later
6*/
7
8#include "diffhunk.h"
9
10#include <komparediffdebug.h>
11
12using namespace Diff2;
13
14DiffHunk::DiffHunk(int sourceLine, int destinationLine, const QString& function, Type type) :
15 m_sourceLine(sourceLine),
16 m_destinationLine(destinationLine),
17 m_function(function),
18 m_type(type)
19{
20}
21
22DiffHunk::~DiffHunk()
23{
24}
25
26void DiffHunk::add(Difference* diff)
27{
28 m_differences.append(diff);
29}
30
31int DiffHunk::sourceLineCount() const
32{
33 DifferenceListConstIterator diffIt = m_differences.begin();
34 DifferenceListConstIterator dEnd = m_differences.end();
35
36 int lineCount = 0;
37
38 for (; diffIt != dEnd; ++diffIt)
39 lineCount += (*diffIt)->sourceLineCount();
40
41 return lineCount;
42}
43
44int DiffHunk::destinationLineCount() const
45{
46 DifferenceListConstIterator diffIt = m_differences.begin();
47 DifferenceListConstIterator dEnd = m_differences.end();
48
49 int lineCount = 0;
50
51 for (; diffIt != dEnd; ++diffIt)
52 lineCount += (*diffIt)->destinationLineCount();
53
54 return lineCount;
55}
56
57QString DiffHunk::recreateHunk() const
58{
59 QString hunk;
60 QString differences;
61
62 // recreate body
63 DifferenceListConstIterator diffIt = m_differences.begin();
64 DifferenceListConstIterator dEnd = m_differences.end();
65
66 int slc = 0; // source line count
67 int dlc = 0; // destination line count
68 for (; diffIt != dEnd; ++diffIt)
69 {
70 switch ((*diffIt)->type())
71 {
72 case Difference::Unchanged:
73 case Difference::Change:
74 slc += (*diffIt)->sourceLineCount();
75 dlc += (*diffIt)->destinationLineCount();
76 break;
77 case Difference::Insert:
78 dlc += (*diffIt)->destinationLineCount();
79 break;
80 case Difference::Delete:
81 slc += (*diffIt)->sourceLineCount();
82 break;
83 }
84 differences += (*diffIt)->recreateDifference();
85 }
86
87 // recreate header
88 hunk += QStringLiteral("@@ -%1,%3 +%2,%4 @@")
89 .arg(m_sourceLine)
90 .arg(m_destinationLine)
91 .arg(slc)
92 .arg(dlc);
93
94 if (!m_function.isEmpty())
95 hunk += QLatin1Char(' ') + m_function;
96
97 hunk += QLatin1Char('\n');
98
99 hunk += differences;
100
101 qCDebug(LIBKOMPAREDIFF2) << hunk;
102 return hunk;
103}
A difference.
Definition difference.h:124
Type type(const QSqlDatabase &db)
Diff2 namespace.
void append(QList< T > &&value)
iterator begin()
iterator end()
QString arg(Args &&... args) const const
bool isEmpty() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Apr 27 2024 22:10:24 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.