-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwsjcpp_diff_text.cpp
More file actions
155 lines (139 loc) · 4.82 KB
/
Copy pathwsjcpp_diff_text.cpp
File metadata and controls
155 lines (139 loc) · 4.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include "wsjcpp_diff_text.h"
#include <sstream>
// ---------------------------------------------------------------------
// WsjcppDiffTextRow
WsjcppDiffTextRow::WsjcppDiffTextRow(
int nNumberOfLine,
const std::string &sKey,
const std::string &sLine
) {
m_nNumberOfLine = nNumberOfLine;
m_sKey = sKey;
m_sLine = sLine;
}
// ---------------------------------------------------------------------
int WsjcppDiffTextRow::getNumberOfLine() {
return m_nNumberOfLine;
}
// ---------------------------------------------------------------------
std::string WsjcppDiffTextRow::getKey() {
return m_sKey;
}
// ---------------------------------------------------------------------
std::string WsjcppDiffTextRow::getLine() {
return m_sLine;
}
// ---------------------------------------------------------------------
// WsjcppDiffText
void WsjcppDiffText::compare(
const std::string &sText1,
const std::string &sText2,
std::vector<WsjcppDiffTextRow> &vOutput
) {
std::vector<std::string> list1;
std::istringstream isTxt1(sText1);
std::string sLine = "";
while (getline(isTxt1, sLine, '\n')) {
list1.push_back(sLine);
}
std::vector<std::string> list2;
std::istringstream isTxt2(sText2);
sLine = "";
while (getline(isTxt2, sLine, '\n')) {
list2.push_back(sLine);
}
std::vector<std::string> sWord;
sWord.push_back("!add");
sWord.push_back("!del");
int len1 = list1.size();
int len2 = list2.size();
int i = 0, j = 0;
//main comparisons
while ((i<len1) && (j<len2)) {
if (list1[i] != list2[j]) {
// checkout for added rows
for (int k = j + 1; k < len2; ++k) {
if (list1[i] == list2[k]) {
while (j<k) {
vOutput.push_back(WsjcppDiffTextRow(j, sWord.at(0), list2.at(j)));
j++;
}
goto exit;
}
}
// checkout for deleted rows
for (int k=i+1;k<len1;++k) {
if (list1[k]==list2[j]) {
while (i<k) {
vOutput.push_back(WsjcppDiffTextRow(i, sWord.at(1), list1.at(i)));
i++;
}
goto exit;
}
}
vOutput.push_back(WsjcppDiffTextRow(i, list1.at(i), list2.at(j)));
exit:;
}
i++, j++;
}
//work with the end of the texts
while (j < len2) {
vOutput.push_back(WsjcppDiffTextRow(j, sWord.at(0), list2.at(j)));
j++;
}
while (i < len1) {
vOutput.push_back(WsjcppDiffTextRow(i, sWord.at(1), list1.at(i)));
i++;
}
}
// ---------------------------------------------------------------------
void WsjcppDiffText::merge(
std::string &curtxt,
std::string &txt1,
std::string &txt2,
std::vector<WsjcppDiffTextRow> &arr1,
std::vector<WsjcppDiffTextRow> &arr2
) {
WsjcppDiffText::compare(txt1, txt2, arr1);
WsjcppDiffText::compare(txt1, curtxt, arr2);
for (unsigned int i=0;i<arr2.size();++i) {
for (unsigned int j=0;j<arr1.size();++j) {
//delete of matches and 'del'/'add' overlays from the first vector
bool bLinesEqual = arr2[i].getLine() == arr1[j].getLine();
bool bKeysEqual = arr2[i].getKey() == arr1[j].getLine(); // TODO why comparing key and line ???
std::string sKey1 = arr1[j].getKey();
std::string sKey2 = arr2[i].getKey();
if ((bLinesEqual && (sKey1 == sKey2 || sKey1 == "!add"))
|| (bKeysEqual && (sKey1 == "!del")))
{
arr1.erase(arr1.begin()+j);
break;
}
}
}
for (unsigned int i = 0; i < arr1.size(); ++i) {
for (unsigned int j = 0; j < arr2.size(); ++j) {
//delete of del overlays from the second vector and update of priority
bool bLinesEqual = arr1[i].getKey() == arr2.at(j).getLine(); // TODO check why comparing key and line here ?
bool bKeysEqual = arr1.at(i).getKey() == arr2.at(j).getKey();
std::string sKey = arr2.at(j).getKey();
if ((bLinesEqual && (sKey == "!del"))
|| (bKeysEqual && (sKey != "!add") && (sKey != "!del")))
{
arr2.erase(arr2.begin()+j);
break;
}
}
}
// merge and sort vectors
arr1.reserve(arr1.size()+arr2.size());
arr1.insert(arr1.end(),arr2.begin(),arr2.end());
for (unsigned int i=0; i < arr1.size(); ++i) {
for (unsigned int j = arr1.size()-1; j > i; --j) {
if (arr1.at(j-1).getNumberOfLine() > arr1.at(j).getNumberOfLine()) {
// TODO redesign
std::swap(arr1.at(j-1), arr1.at(j));
}
}
}
}