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 }
0 commit comments