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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
|
/* **************************************************************************
This file is part of KBabel
Copyright (C) 2001 by Matthias Kiefer <kiefer@kde.org>
charDiff algorithm by Wolfram Diestel <wolfram@steloj.de>
wordDiff algorithm by Nick Shaforostoff <shafff@ukr.net>
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 TQt library by Trolltech AS, Norway (or with modified versions
of TQt that use the same license as TQt), 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
TQt. 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 <kdebug.h>
LCSprinter::LCSprinter(const TQStringList &s_1, const TQStringList &s_2, TQValueVector<LCSMarker> *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<bound; ++index)
{
resultString.append("<KBABELADD>");
resultString.append(*it2);
++it2;
resultString.append("</KBABELADD>");
}
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("<KBABELDEL>");
resultString.append(*it1);
++it1;
resultString.append("</KBABELDEL>");
return;
}
else
{
printLCS(index-1);
resultString.append("<KBABELADD>");
resultString.append(*it2);
++it2;
resultString.append("</KBABELADD>");
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<l1 )
{
if (str1[i]==TQChar(0x003C) && (str1[i+1]==TQChar(0x002F) || str1[i+1].isLetter()) ) // 0x003C="<", 0x002F="/"
{// skip tag
while(i<l1 && str1[i]!=TQChar(0x003E)) temp += str1[i++];
if (i++ <l1) temp += TQChar(0x003E);
s1.append(temp);
temp.truncate(0);
}
while ( i<l1 && str1[i].isLetter() )
{
temp += str1[i++];
}
if (!temp.isEmpty())
{
s1.append(temp);
temp.truncate(0);
};
if (str1[i]==TQChar(0x003C) && (str1[i+1]==TQChar(0x002F) || str1[i+1].isLetter()) ) // 0x003C="<", 0x002F="/"
{// skip tag
while(i<l1 && str1[i]!=TQChar(0x003E)) temp += str1[i++];
if (i++ <l1) temp += TQChar(0x003E);
s1.append(temp);
temp.truncate(0);
}
while ( i<l1 && !(str1[i].isLetter() || str1[i]==TQChar(0x003C)) )
{
temp += str1[i++];
}
if (!temp.isEmpty())
{
s1.append(temp);
temp.truncate(0);
};
}
i=0;
while ( i<l2 )
{
if (str2[i]==TQChar(0x003C) && (str2[i+1]==TQChar(0x002F) || str2[i+1].isLetter()) ) // 0x003C="<", 0x002F="/"
{// skip tag
while(i<l2 && str2[i]!=TQChar(0x003E)) temp += str2[i++];
if (i++ <l2) temp += TQChar(0x003E);
s2.append(temp);
temp.truncate(0);
}
while ( i<l2 && str2[i].isLetter() )
{
temp += str2[i++];
}
if (!temp.isEmpty())
{
s2.append(temp);
temp.truncate(0);
};
//FIXME bounds
if (str2[i]==TQChar(0x003C) && (str2[i+1]==TQChar(0x002F) || str2[i+1].isLetter()) ) // 0x003C="<", 0x002F="/"
{// skip tag
while(i<l2 && str2[i]!=TQChar(0x003E)) temp += str2[i++];
if (i++ <l2) temp += TQChar(0x003E);
s2.append(temp);
temp.truncate(0);
}
while ( i<l2 && !(str2[i].isLetter() || str2[i]==TQChar(0x003C)) )
{
temp += str2[i++];
}
if (!temp.isEmpty())
{
s2.append(temp);
temp.truncate(0);
};
}
*/
while ( i<l1 )
{
while ( i<l1 && str1[i].isLetter() )
{
temp += str1[i++];
}
if (!temp.isEmpty())
{
s1.append(temp);
temp.truncate(0);
};
while ( i<l1 && !str1[i].isLetter() )
{
s1.append(TQString(str1[i++]));
}
}
i=0;
while ( i<l2 )
{
while ( i<l2 && str2[i].isLetter() )
{
temp += str2[i++];
}
if (!temp.isEmpty())
{
s2.append(temp);
temp.truncate(0);
};
while ( i<l2 && !str2[i].isLetter() )
{
s2.append(TQString(str2[i++]));
}
}
uint mX = s1.count();
uint nY = s2.count();
uint mT = mX+1;
uint nT = nY+1;
TQValueVector<LCSMarker> b(mT*nT, NOTHING);
TQValueVector<uint> c(mT*nT, 0);
// calculate the LCS
b[0] = FINAL;
uint index_cache;
TQStringList::iterator it1, it2;
for (i=1, it1 = s1.begin(); i<mT; ++i, ++it1)
{
for (j=1, it2 = s2.begin(); j<nT; ++j, ++it2)
{
index_cache = i*nT+j;
if ((*it1)==(*it2))
{
c[index_cache] = c[index_cache-nT-1] + 1;
b[index_cache] = ARROW_UP_LEFT;
}
else if (c[index_cache-nT] >= 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<nY; j++) {
// fprintf(stderr,"[%3d]%1d.",i*nT+j,(uint) b[i*nT+j]);
fprintf(stderr,"%1d.",(uint) b[i*nT+j]);
}
fprintf(stderr,"%1d\n",(uint) b[i*nT+j]);
}
fprintf(stderr,"\n");
// print the c table
fprintf(stderr,"c:\n");
for (i=0; i<=mX; i++) {
for (j=0; j<nY; j++) {
fprintf(stderr,"%2d.",(uint) c[i*nY+j]);
}
fprintf(stderr,"%2d\n",(uint) c[i*nY+j]);
}
fprintf(stderr,"\n");
*/
return printer.getString();
}
/*
// old algorithm by Wolfram Diestel <wolfram@steloj.de>
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 <len2))
{
int p1;
int p2;
for(p1 = 0, p2 = x; p1 <= x; p1++, p2--)
{
if( pos1+p1+n >= 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 += "<KBABELDEL>";
resultstr += s1.mid(pos1,p1);
resultstr += "</KBABELDEL>";
pos1 += p1;
}
if (p2 > 0 )
{
resultstr += "<KBABELADD>";
resultstr += s2.mid(pos2,p2);
resultstr += "</KBABELADD>";
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 += "<KBABELDEL>";
resultstr += s1.mid(pos1,len1-pos1-n);
resultstr += "</KBABELDEL>";
removed=true;
}
if(pos2+n < len2)
{
resultstr += "<KBABELADD>";
resultstr += s2.mid(pos2,len2-pos2-n);
resultstr += "</KBABELADD>";
removed=true;
}
if( !removed )
{
resultstr.truncate( resultstr.length()-3 );
}
return resultstr;
}
*/
|