Skip to content

Commit 24cc48c

Browse files
committed
add line-based diff, fork diff project
1 parent b12f631 commit 24cc48c

File tree

3 files changed

+61
-37
lines changed

3 files changed

+61
-37
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@
1515

1616
# qt-creator build directories:
1717
build-*
18-
*.pro.user
18+
*.pro.user
19+
/debug

.gitmodules

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
url = git@github.com:tomv564/edbee-lib
44
[submodule "diff-match-patch-cpp-stl"]
55
path = diff-match-patch-cpp-stl
6-
url = https://github.com/leutloff/diff-match-patch-cpp-stl.git
6+
url = https://github.com/tomv564/diff-match-patch-cpp-stl.git

app/example2.cpp

+58-35
Original file line numberDiff line numberDiff line change
@@ -38,47 +38,62 @@ string getFileContents(const char* path)
3838
return NULL;
3939
}
4040

41-
void summarizeLines(QVector<QVector<stringdiff::Diff*>> lookup) {
41+
void summarizeLines(QVector<QVector<stringdiff::Diff>> lookup) {
4242

43-
for (int line = 0; line < lookup.size(); ++line) {
44-
45-
QVector<stringdiff::Diff*> diffs = lookup.at(line);
46-
47-
48-
for (int i = 0; i < diffs.size(); ++i) {
49-
stringdiff::Diff* diff = diffs.at(i);
50-
if (diff != NULL) {
51-
qDebug() << "line" << line << "diff" << i << "op" << (*diff).operation;
52-
}
53-
}
43+
for (int line = 0; line < lookup.count(); ++line) {
5444

45+
QVector<stringdiff::Diff> diffs = lookup.at(line);
46+
47+
for (int i = 0; i < diffs.count(); ++i) {
48+
stringdiff::Diff diff = diffs.at(i);
49+
qDebug() << "line" << line << "diff" << i << "op" << diff.operation;
50+
}
5551

5652
}
5753
}
5854

59-
QVector<QVector<stringdiff::Diff*>> createDiffLookup(list<stringdiff::Diff> diffs, stringdiff::Operation operation) {
60-
int lineNumber = 0;
55+
QVector<QVector<stringdiff::Diff>> createDiffLookup(list<stringdiff::Diff> diffs, stringdiff::Operation operation) {
56+
int lineIndex = 0;
6157

6258
// we want pointers to the diffs, not the original object.
63-
QVector<QVector<stringdiff::Diff*>> diffsPerLine(100, QVector<stringdiff::Diff*>());
59+
QVector<QVector<stringdiff::Diff>> diffsPerLine(100, QVector<stringdiff::Diff>());
6460
qDebug() << "Diff lookup for operation" << operation;
6561

6662
// iterator over each diff
67-
for (list<stringdiff::Diff>::iterator it=diffs.begin(); it != diffs.end(); ++it)
63+
auto end = diffs.end();
64+
for (list<stringdiff::Diff>::iterator it=diffs.begin(); it != end; ++it)
6865
{
69-
qDebug() << it->text.c_str();
66+
qDebug() << it->text.c_str();
7067
bool isRelevant = it->operation == stringdiff::EQUAL || it->operation == operation; // no change or delete/insert
7168
int lineCount = (int) count(it->text.begin(), it->text.end(), '\n');
72-
qDebug() << lineCount+1 << "lines of" << it->operation << ", relevant is" << isRelevant;
73-
74-
if (isRelevant) {
75-
for (int lineOffset = 0; lineOffset <= lineCount; ++lineOffset) {
76-
int currentLine = lineNumber + lineOffset;
69+
//qDebug() << it->operation << ", relevant is" << isRelevant;
70+
71+
if (isRelevant) {
72+
stringdiff::Diff diff = *it;
73+
74+
if (lineCount == 0)
75+
{
76+
diffsPerLine[lineIndex].append(diff);
77+
}
78+
else
79+
{
80+
for (int offset = 0; offset < lineCount; ++offset)
81+
{
82+
diffsPerLine[lineIndex].append(diff);
83+
lineIndex = lineIndex + 1;
84+
qDebug() << "line" << lineIndex << it->strOperation(it->operation).c_str();
85+
}
86+
}
87+
88+
}
89+
90+
//for (int lineOffset = 0; lineOffset < lineCount; ++lineOffset) {
91+
// int currentLine = lineNumber + lineOffset;
7792

7893
// qDebug() << "Trying line" << currentLine;
79-
stringdiff::Diff* diff = &*it;
80-
diffsPerLine[currentLine].append(diff);
81-
qDebug() << "appended" << diff->operation;
94+
// stringdiff::Diff diff = *it;
95+
// diffsPerLine[currentLine].append(diff);
96+
//qDebug() << "appended" << diff.operation;
8297
// QVector<stringdiff::Diff*> diffsAtLine = diffsPerLine.at(currentLine);
8398
// diffsAtLine.append(diff);
8499
// qDebug() << diffsAtLine;
@@ -91,9 +106,9 @@ QVector<QVector<stringdiff::Diff*>> createDiffLookup(list<stringdiff::Diff> diff
91106
// // int currentLine = lineNumber + lineOffset;
92107
// // if (diffsPerLine[currentLine] == NULL) { diffsPerLine[currentLine]
93108
// qDebug() << "Line " << lineNumber + lineOffset << " change " << it->operation;
94-
}
95-
lineNumber += lineCount;
96-
}
109+
// }
110+
//lineNumber += lineCount;
111+
//}
97112
}
98113

99114
return diffsPerLine;
@@ -137,11 +152,18 @@ int main(int argc, char *argv[])
137152
return 1;
138153
}
139154

140-
// string leftContent = "<html>\n\t<title>Yo</title>\n\t<body>\n\t\t<h1>Hello Left</h1>\n\t</body>\n</html>\n";
141-
// string rightContent = "<html>\n\t<body>\n\t\t<h1>Hello Right</h1>\n\t</body>\n\t<footer>fin</footer>\n</html>\n";
142-
//
143-
string leftContent = "asdf";
144-
string rightContent = "asdf";
155+
//string leftContent = "<html>\n\t<title>Yo</title>\n\t<body>\n\t\t<h1>Hello Left</h1>\n\t</body>\n</html>\n";
156+
//string rightContent = "<html>\n\t<body>\n\t\t<h1>Hello Right</h1>\n\t</body>\n\t<footer>fin</footer>\n</html>\n";
157+
158+
//string leftContent = "Mary had a little lamb,\nwhose fleece was white as snow.\n\n\nIt followed her to school one day,\nschool one day, school one day,\nwhich was against the rules.\n\n";
159+
//string rightContent = "Mary had a little lamb,\nwhose fleece was red as snow.\n\n\And everywhere that Mary went,\nthe lamb was sure to go.\n\nIt followed her to school one day,\nwhich was against the rules.\n\n";
160+
161+
string leftContent = "Mary had a little lamb,\nwhose fleece was white as snow.\n\n";
162+
string rightContent = "Mary had a little lamb,\nwhose fleece was red as snow.\n\nAnd everywhere that Mary went,\nthe lamb was sure to go.\n";
163+
164+
////
165+
//string leftContent = "asdf\nyo";
166+
//string rightContent = "asdf\nbe";
145167

146168
// if (argc > 2) {
147169
//
@@ -160,7 +182,7 @@ int main(int argc, char *argv[])
160182

161183
// get the minimal diff
162184
diff_match_patch<string> dmp;
163-
auto diffs = dmp.diff_main(leftContent, rightContent);
185+
auto diffs = dmp.diff_lines(leftContent, rightContent);
164186

165187
auto deletionLookup = createDiffLookup(diffs, stringdiff::DELETE);
166188
summarizeLines(deletionLookup);
@@ -217,7 +239,8 @@ int main(int argc, char *argv[])
217239
// show the window
218240
// next create the main window and the editor
219241
QMainWindow win;
242+
win.setMinimumSize(800, 600);
220243
win.setCentralWidget( splitter );
221-
win.show();
244+
win.show();
222245
return a.exec();
223246
}

0 commit comments

Comments
 (0)