/* ************************************************************************** This file is part of KBabel Copyright (C) 2001 by Matthias Kiefer charDiff algorithm by Wolfram Diestel wordDiff algorithm by Nick Shaforostoff This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. In addition, as a special exception, the copyright holders give permission to link the code of this program with any edition of the Qt library by Trolltech AS, Norway (or with modified versions of Qt that use the same license as Qt), and distribute linked combinations including the two. You must obey the GNU General Public License in all respects for all of the code used other than Qt. If you modify this file, you may extend this exception to your version of the file, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version. ************************************************************************** */ #include "diff.h" #include LCSprinter::LCSprinter(const TQStringList &s_1, const TQStringList &s_2, TQValueVector *b_, const uint nT_, uint index):s1(s_1),s2(s_2),b(b_),nT(nT_) { it1=s1.begin(); it2=s2.begin(); printLCS(index); } void LCSprinter::printLCS(uint index) { //fprintf(stderr,"%2d. %2d. %2d. %2d\n",(uint)(*b)[index],nT,index%nT, index); if (index % nT == 0 || index < nT) { //original LCS algo doesnt have to deal with ins before first common uint bound = index%nT; for (index=0; index"); resultString.append(*it2); ++it2; resultString.append(""); } return; } if (ARROW_UP_LEFT == (*b)[index]) { printLCS(index-nT-1); resultString.append(*it1); ++it1; ++it2; return; } else if (ARROW_UP == (*b)[index]) { printLCS(index-nT); resultString.append(""); resultString.append(*it1); ++it1; resultString.append(""); return; } else { printLCS(index-1); resultString.append(""); resultString.append(*it2); ++it2; resultString.append(""); return; } } TQString wordDiff(const TQString& str1, const TQString& str2) { //separate punctuation marks etc from words as _only_ they may have changed TQStringList s1, s2; uint i=0; uint j=0; uint l1=str1.length(); uint l2=str2.length(); TQString temp; temp.reserve(16); /* while ( i b(mT*nT, NOTHING); TQValueVector c(mT*nT, 0); // calculate the LCS b[0] = FINAL; uint index_cache; TQStringList::iterator it1, it2; for (i=1, it1 = s1.begin(); i= c[index_cache-1]) { c[index_cache] = c[index_cache-nT]; b[index_cache] = ARROW_UP; } else { c[index_cache] = c[index_cache-1]; b[index_cache] = ARROW_LEFT; } } } c.clear(); LCSprinter printer(s1, s2, &b, nT, index_cache); /*//debug fprintf(stderr,"b:\n"); for (i=0; i<=mT; i++) { for (j=0; j TQString Diff::charDiff(TQString s1, TQString s2) { int n = 3; s1+="xxx"; s2+="xxx"; int pos1=0; int pos2=0; int len1 = s1.length(); int len2 = s2.length(); TQString resultstr; bool found = true; bool swap = false; while (found && pos1+n < len1 && pos2+n < len2) { // while strings are equal, increase positions int len = 0; while (pos1+len < len1 && pos2+len < len2 && (s1[pos1+len] == s2[pos2+len])) { len++; } // write first result resultstr += s1.mid(pos1,len); pos1 += len; pos2 += len; // If strings are not equal try to find equal substrings of // length n. For this increase positions alternating like // 0 1, 1 0, 0 2, 1 1, 2 0, 0 3, 1 2, 2 1, 3 0 etc. int x = 1; found = false; // If one position is already at the end stop processing if (pos1+n >= len1 || pos2+n >= len2) { break; } // evtl. exchange if (len2 - pos2 < len1 - pos1) { swap=true; int pos = pos1; pos1 = pos2; pos2 = pos; TQString s = s1; s1 = s2; s2 = s; int len = len1; len1 = len2; len2 = len; } while( !found && (pos1+x < len1 || pos2+x = len1 || pos2+p2+n >= len2) break; if(s1.mid(pos1+p1,n) == s2.mid(pos2+p2,n)) { found = true; // evtl. exchange back if(swap) { int pos = pos1; pos1 = pos2; pos2 = pos; TQString s = s1; s1 = s2; s2 = s; int len = len1; len1 = len2; len2 = len; int p = p1; p1 = p2; p2 = p; swap=false; } // write current result if (p1 > 0) { resultstr += ""; resultstr += s1.mid(pos1,p1); resultstr += ""; pos1 += p1; } if (p2 > 0 ) { resultstr += ""; resultstr += s2.mid(pos2,p2); resultstr += ""; pos2 += p2; } break; } } x++; } } bool removed=false; // evtl. exchange back if(swap) { int pos = pos1; pos1 = pos2; pos2 = pos; TQString s = s1; s1 = s2; s2 = s; int len = len1; len1 = len2; len2 = len; swap=false; } // if there is a rest, add it if(pos1+n < len1) { resultstr += ""; resultstr += s1.mid(pos1,len1-pos1-n); resultstr += ""; removed=true; } if(pos2+n < len2) { resultstr += ""; resultstr += s2.mid(pos2,len2-pos2-n); resultstr += ""; removed=true; } if( !removed ) { resultstr.truncate( resultstr.length()-3 ); } return resultstr; } */