Skip to content

Commit 99f4169

Browse files
committed
Updated README.md and small redesign WsjcppDiffTextRow
1 parent 816fba1 commit 99f4169

File tree

10 files changed

+139
-26
lines changed

10 files changed

+139
-26
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
.wsjcpp/*
22
tmp/*
33
wsjcpp-diff-text
4+
.vscode/
45

56
# Prerequisites
67
*.d

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ addons:
1818
# Build steps
1919
script:
2020
- ./build_simple.sh
21+
- ./wsjcpp-diff-text data/file1.txt data/file2.txt
2122
- cd unit-tests.wsjcpp
2223
- ./build_simple.sh
2324
- ./unit-tests

README.md

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,49 @@
55
Algorithm for like a diff texts
66

77

8-
## Install
8+
## Integrate to your project
99

1010
```
1111
wsjcpp install "https://github.com/wsjcpp/wsjcpp-diff-text:master"
1212
```
1313

14-
## Use
14+
Or copy files to project:
15+
16+
* src.wsjcpp/wsjcpp_core/wsjcpp_core.h
17+
* src.wsjcpp/wsjcpp_core/wsjcpp_core.cpp
18+
* src/wsjcpp_diff_text.h
19+
* src/wsjcpp_diff_text.cpp
20+
21+
## Examples
22+
23+
```
24+
#include <wsjcpp_core.h>
25+
#include <wsjcpp_diff_text.h>
26+
27+
...
28+
29+
std::string sFileContent1;
30+
std::string sFileContent2;
31+
WsjcppCore::readTextFile("data/file1.txt", sFileContent1);
32+
WsjcppCore::readTextFile("data/file1.txt", sFileContent2);
33+
34+
std::vector<WsjcppDiffTextRow *> vResult;
35+
WsjcppDiffText::compare(sFileContent1, sFileContent2, vResult);
36+
for (int i = 0; i < vResult.size(); i++) {
37+
std::cout <<
38+
vResult[i]->key << " => line (" << vResult[i]->getNumberOfLine() << "): "
39+
<< vResult[i]->line << std::endl;
40+
41+
}
42+
```
43+
44+
example output:
45+
```
46+
!del => line (1): 22222
47+
!add => line (6): 71111
48+
!add => line (10): 11111
49+
```
50+
51+
1552

1653

data/file1.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
11111
2+
22222
3+
33333
4+
44444
5+
55555
6+
66666
7+
77777
8+
88888
9+
99999
10+
10000

data/file2.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
11111
2+
33333
3+
44444
4+
55555
5+
66666
6+
77777
7+
71111
8+
88888
9+
99999
10+
10000
11+
11111

src/main.cpp

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,49 @@
22
#include <iostream>
33
#include <algorithm>
44
#include <wsjcpp_core.h>
5+
#include <wsjcpp_diff_text.h>
56

67
int main(int argc, const char* argv[]) {
8+
if (argc != 3) {
9+
std::cout << "Usage: " << argv[0] << " file1 file2" << std::endl;
10+
return -1;
11+
}
12+
713
std::string TAG = "MAIN";
814
std::string appName = std::string(WSJCPP_NAME);
915
std::string appVersion = std::string(WSJCPP_VERSION);
1016
if (!WsjcppCore::dirExists(".logs")) {
1117
WsjcppCore::makeDir(".logs");
1218
}
13-
WsjcppLog::setPrefixLogFile("wsjcpp");
19+
WsjcppLog::setPrefixLogFile("wsjcpp-diff-text");
1420
WsjcppLog::setLogDirectory(".logs");
15-
// TODO your code here
21+
22+
std::string sFileName1(argv[1]);
23+
std::string sFileName2(argv[2]);
24+
25+
if (!WsjcppCore::fileExists(sFileName1)) {
26+
WsjcppLog::err(TAG, "file1 not found '" + sFileName1 + "'");
27+
return -1;
28+
}
29+
30+
if (!WsjcppCore::fileExists(sFileName2)) {
31+
WsjcppLog::err(TAG, "file2 not found '" + sFileName2 + "'");
32+
return -1;
33+
}
34+
35+
std::string sFileContent1;
36+
std::string sFileContent2;
37+
WsjcppCore::readTextFile(sFileName1, sFileContent1);
38+
WsjcppCore::readTextFile(sFileName2, sFileContent2);
39+
40+
std::vector<WsjcppDiffTextRow *> vResult;
41+
WsjcppDiffText::compare(sFileContent1, sFileContent2, vResult);
42+
for (int i = 0; i < vResult.size(); i++) {
43+
std::cout <<
44+
vResult[i]->key << " => line (" << vResult[i]->getNumberOfLine() << "): "
45+
<< vResult[i]->line << std::endl;
46+
47+
}
1648
return 0;
1749
}
1850

src/wsjcpp_diff_text.cpp

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,35 @@
55
// ---------------------------------------------------------------------
66
// WsjcppDiffTextRow
77

8-
WsjcppDiffTextRow::WsjcppDiffTextRow(int id, std::string key, std::string line) {
9-
this->id = id;
8+
WsjcppDiffTextRow::WsjcppDiffTextRow(int nNumberOfLine, std::string key, std::string line) {
9+
m_nNumberOfLine = nNumberOfLine;
1010
this->key = key;
1111
this->line = line;
1212
}
1313

14+
// ---------------------------------------------------------------------
15+
16+
int WsjcppDiffTextRow::getNumberOfLine() {
17+
return m_nNumberOfLine;
18+
}
19+
1420
// ---------------------------------------------------------------------
1521
// WsjcppDiffText
1622

17-
void WsjcppDiffText::compare(std::string &txt1, std::string &txt2, std::vector<WsjcppDiffTextRow *> &arr) {
23+
void WsjcppDiffText::compare(
24+
const std::string &sText1,
25+
const std::string &sText2,
26+
std::vector<WsjcppDiffTextRow *> &vOutput
27+
) {
1828
std::vector<std::string> list1;
19-
std::istringstream isTxt1(txt1);
29+
std::istringstream isTxt1(sText1);
2030
std::string sLine = "";
2131
while (getline(isTxt1, sLine, '\n')) {
2232
list1.push_back(sLine);
2333
}
2434

2535
std::vector<std::string> list2;
26-
std::istringstream isTxt2(txt2);
36+
std::istringstream isTxt2(sText2);
2737
sLine = "";
2838
while (getline(isTxt2, sLine, '\n')) {
2939
list2.push_back(sLine);
@@ -43,7 +53,7 @@ void WsjcppDiffText::compare(std::string &txt1, std::string &txt2, std::vector<W
4353
for (int k = j + 1; k < len2; ++k) {
4454
if (list1[i] == list2[k]) {
4555
while (j<k) {
46-
arr.push_back(new WsjcppDiffTextRow(j, sWord.at(0), list2.at(j)));
56+
vOutput.push_back(new WsjcppDiffTextRow(j, sWord.at(0), list2.at(j)));
4757
j++;
4858
}
4959
goto exit;
@@ -53,24 +63,24 @@ void WsjcppDiffText::compare(std::string &txt1, std::string &txt2, std::vector<W
5363
for (int k=i+1;k<len1;++k) {
5464
if (list1[k]==list2[j]) {
5565
while (i<k) {
56-
arr.push_back(new WsjcppDiffTextRow(i, sWord.at(1), list1.at(i)));
66+
vOutput.push_back(new WsjcppDiffTextRow(i, sWord.at(1), list1.at(i)));
5767
i++;
5868
}
5969
goto exit;
6070
}
6171
}
62-
arr.push_back(new WsjcppDiffTextRow(i, list1.at(i), list2.at(j)));
72+
vOutput.push_back(new WsjcppDiffTextRow(i, list1.at(i), list2.at(j)));
6373
exit:;
6474
}
6575
i++, j++;
6676
}
6777
//work with the end of the texts
6878
while (j<len2) {
69-
arr.push_back(new WsjcppDiffTextRow(j, sWord.at(0), list2.at(j)));
79+
vOutput.push_back(new WsjcppDiffTextRow(j, sWord.at(0), list2.at(j)));
7080
j++;
7181
}
7282
while (i<len1) {
73-
arr.push_back(new WsjcppDiffTextRow(i, sWord.at(1), list1.at(i)));
83+
vOutput.push_back(new WsjcppDiffTextRow(i, sWord.at(1), list1.at(i)));
7484
i++;
7585
}
7686
}
@@ -84,8 +94,8 @@ void WsjcppDiffText::merge(
8494
std::vector<WsjcppDiffTextRow *> &arr1,
8595
std::vector<WsjcppDiffTextRow *> &arr2
8696
) {
87-
compare(txt1, txt2, arr1);
88-
compare(txt1, curtxt, arr2);
97+
WsjcppDiffText::compare(txt1, txt2, arr1);
98+
WsjcppDiffText::compare(txt1, curtxt, arr2);
8999
for (unsigned int i=0;i<arr2.size();++i) {
90100
for (unsigned int j=0;j<arr1.size();++j) {
91101
//delete of matches and 'del'/'add' overlays from the first vector
@@ -115,16 +125,14 @@ void WsjcppDiffText::merge(
115125
}
116126
}
117127
}
118-
//merge and sort vectors
128+
// merge and sort vectors
119129
arr1.reserve(arr1.size()+arr2.size());
120130
arr1.insert(arr1.end(),arr2.begin(),arr2.end());
121131
for (unsigned int i=0; i < arr1.size(); ++i) {
122132
for (unsigned int j = arr1.size()-1; j > i; --j) {
123-
if (arr1.at(j-1)->id > arr1.at(j)->id) {
133+
if (arr1.at(j-1)->getNumberOfLine() > arr1.at(j)->getNumberOfLine()) {
124134
// TODO redesign
125-
std::swap(arr1.at(j-1)->id, arr1.at(j)->id);
126-
std::swap(arr1.at(j-1)->key, arr1.at(j)->key);
127-
std::swap(arr1.at(j-1)->line, arr1.at(j)->line);
135+
std::swap(arr1.at(j-1), arr1.at(j));
128136
}
129137
}
130138
}

src/wsjcpp_diff_text.h

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,26 @@
77

88
class WsjcppDiffTextRow {
99
public:
10-
WsjcppDiffTextRow(int id, std::string key, std::string line);
11-
int id;
10+
WsjcppDiffTextRow(
11+
int nNumberOfLine,
12+
std::string key,
13+
std::string line
14+
);
15+
int getNumberOfLine();
16+
1217
std::string key;
1318
std::string line;
19+
private:
20+
int m_nNumberOfLine;
1421
};
1522

1623
class WsjcppDiffText {
1724
public:
18-
static void compare(std::string &txt1, std::string &txt2, std::vector<WsjcppDiffTextRow *> &arr);
25+
static void compare(
26+
const std::string &sText1,
27+
const std::string &sText2,
28+
std::vector<WsjcppDiffTextRow *> &vOutput
29+
);
1930
static void merge(
2031
std::string &curtxt,
2132
std::string &txt1,

unit-tests.wsjcpp/src/unit_test_basic.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,8 @@ bool UnitTestBasic::run() {
166166

167167
unsigned int Success = 0;
168168
for (int i = 0; i < 8; ++i) {
169-
int id1 = arr1.at(i)->id;
170-
int id2 = arr3.at(i)->id;
169+
int id1 = arr1.at(i)->getNumberOfLine();
170+
int id2 = arr3.at(i)->getNumberOfLine();
171171
std::string key1 = arr1.at(i)->key;
172172
std::string key2 = arr3.at(i)->key;
173173
std::string line1 = arr1.at(i)->line;

wsjcpp.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ cmake_minimum_required: "3.0"
44
cmake_cxx_standard: "11"
55
description: "Diff texts"
66
authors:
7+
- name: "Danil Dudkin"
8+
79
- name: "Evgenii Sopov"
810
911

0 commit comments

Comments
 (0)