joachim99@52
|
1 /* Analyze file differences for GNU DIFF.
|
joachim99@52
|
2
|
joachim99@52
|
3 Modified for KDiff3 by Joachim Eibl 2003.
|
joachim99@52
|
4
|
joachim99@52
|
5 Copyright (C) 1988, 1989, 1992, 1993, 1994, 1995, 1998, 2001, 2002
|
joachim99@52
|
6 Free Software Foundation, Inc.
|
joachim99@52
|
7
|
joachim99@52
|
8 This file is part of GNU DIFF.
|
joachim99@52
|
9
|
joachim99@52
|
10 GNU DIFF is free software; you can redistribute it and/or modify
|
joachim99@52
|
11 it under the terms of the GNU General Public License as published by
|
joachim99@52
|
12 the Free Software Foundation; either version 2, or (at your option)
|
joachim99@52
|
13 any later version.
|
joachim99@52
|
14
|
joachim99@52
|
15 GNU DIFF is distributed in the hope that it will be useful,
|
joachim99@52
|
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
|
joachim99@52
|
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
joachim99@52
|
18 GNU General Public License for more details.
|
joachim99@52
|
19
|
joachim99@52
|
20 You should have received a copy of the GNU General Public License
|
joachim99@52
|
21 along with this program; see the file COPYING.
|
joachim99@52
|
22 If not, write to the Free Software Foundation,
|
joachim99@52
|
23 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
|
joachim99@52
|
24
|
joachim99@52
|
25 /* The basic algorithm is described in:
|
joachim99@52
|
26 "An O(ND) Difference Algorithm and its Variations", Eugene Myers,
|
joachim99@52
|
27 Algorithmica Vol. 1 No. 2, 1986, pp. 251-266;
|
joachim99@52
|
28 see especially section 4.2, which describes the variation used below.
|
joachim99@52
|
29 Unless the --minimal option is specified, this code uses the TOO_EXPENSIVE
|
joachim99@52
|
30 heuristic, by Paul Eggert, to limit the cost to O(N**1.5 log N)
|
joachim99@52
|
31 at the price of producing suboptimal output for large inputs with
|
joachim99@52
|
32 many differences.
|
joachim99@52
|
33
|
joachim99@52
|
34 The basic algorithm was independently discovered as described in:
|
joachim99@52
|
35 "Algorithms for Approximate String Matching", E. Ukkonen,
|
joachim99@52
|
36 Information and Control Vol. 64, 1985, pp. 100-118. */
|
joachim99@52
|
37
|
joachim99@52
|
38 #define GDIFF_MAIN
|
joachim99@52
|
39
|
joachim99@52
|
40 #include "gnudiff_diff.h"
|
joachim99@52
|
41 //#include <error.h>
|
joachim99@52
|
42 #include <stdlib.h>
|
joachim99@52
|
43 #include "gnudiff_xalloc.h"
|
joachim99@52
|
44
|
joachim99@52
|
45 static lin *xvec, *yvec; /* Vectors being compared. */
|
joachim99@52
|
46 static lin *fdiag; /* Vector, indexed by diagonal, containing
|
joachim99@52
|
47 1 + the X coordinate of the point furthest
|
joachim99@52
|
48 along the given diagonal in the forward
|
joachim99@52
|
49 search of the edit matrix. */
|
joachim99@52
|
50 static lin *bdiag; /* Vector, indexed by diagonal, containing
|
joachim99@52
|
51 the X coordinate of the point furthest
|
joachim99@52
|
52 along the given diagonal in the backward
|
joachim99@52
|
53 search of the edit matrix. */
|
joachim99@52
|
54 static lin too_expensive; /* Edit scripts longer than this are too
|
joachim99@52
|
55 expensive to compute. */
|
joachim99@52
|
56
|
joachim99@52
|
57 #define SNAKE_LIMIT 20 /* Snakes bigger than this are considered `big'. */
|
joachim99@52
|
58 namespace GnuDiff {
|
joachim99@52
|
59
|
joachim99@52
|
60 struct partition
|
joachim99@52
|
61 {
|
joachim99@52
|
62 lin xmid, ymid; /* Midpoints of this partition. */
|
joachim99@52
|
63 bool lo_minimal; /* Nonzero if low half will be analyzed minimally. */
|
joachim99@52
|
64 bool hi_minimal; /* Likewise for high half. */
|
joachim99@52
|
65 };
|
joachim99@52
|
66
|
joachim99@52
|
67 /* Find the midpoint of the shortest edit script for a specified
|
joachim99@52
|
68 portion of the two files.
|
joachim99@52
|
69
|
joachim99@52
|
70 Scan from the beginnings of the files, and simultaneously from the ends,
|
joachim99@52
|
71 doing a breadth-first search through the space of edit-sequence.
|
joachim99@52
|
72 When the two searches meet, we have found the midpoint of the shortest
|
joachim99@52
|
73 edit sequence.
|
joachim99@52
|
74
|
joachim99@52
|
75 If FIND_MINIMAL is nonzero, find the minimal edit script regardless
|
joachim99@52
|
76 of expense. Otherwise, if the search is too expensive, use
|
joachim99@52
|
77 heuristics to stop the search and report a suboptimal answer.
|
joachim99@52
|
78
|
joachim99@52
|
79 Set PART->(xmid,ymid) to the midpoint (XMID,YMID). The diagonal number
|
joachim99@52
|
80 XMID - YMID equals the number of inserted lines minus the number
|
joachim99@52
|
81 of deleted lines (counting only lines before the midpoint).
|
joachim99@52
|
82 Return the approximate edit cost; this is the total number of
|
joachim99@52
|
83 lines inserted or deleted (counting only lines before the midpoint),
|
joachim99@52
|
84 unless a heuristic is used to terminate the search prematurely.
|
joachim99@52
|
85
|
joachim99@52
|
86 Set PART->lo_minimal to true iff the minimal edit script for the
|
joachim99@52
|
87 left half of the partition is known; similarly for PART->hi_minimal.
|
joachim99@52
|
88
|
joachim99@52
|
89 This function assumes that the first lines of the specified portions
|
joachim99@52
|
90 of the two files do not match, and likewise that the last lines do not
|
joachim99@52
|
91 match. The caller must trim matching lines from the beginning and end
|
joachim99@52
|
92 of the portions it is going to specify.
|
joachim99@52
|
93
|
joachim99@52
|
94 If we return the "wrong" partitions,
|
joachim99@52
|
95 the worst this can do is cause suboptimal diff output.
|
joachim99@52
|
96 It cannot cause incorrect diff output. */
|
joachim99@52
|
97
|
joachim99@52
|
98 static lin
|
joachim99@52
|
99 diag (lin xoff, lin xlim, lin yoff, lin ylim, bool find_minimal,
|
joachim99@52
|
100 struct partition *part)
|
joachim99@52
|
101 {
|
joachim99@52
|
102 lin *const fd = fdiag; /* Give the compiler a chance. */
|
joachim99@52
|
103 lin *const bd = bdiag; /* Additional help for the compiler. */
|
joachim99@52
|
104 lin const *const xv = xvec; /* Still more help for the compiler. */
|
joachim99@52
|
105 lin const *const yv = yvec; /* And more and more . . . */
|
joachim99@52
|
106 lin const dmin = xoff - ylim; /* Minimum valid diagonal. */
|
joachim99@52
|
107 lin const dmax = xlim - yoff; /* Maximum valid diagonal. */
|
joachim99@52
|
108 lin const fmid = xoff - yoff; /* Center diagonal of top-down search. */
|
joachim99@52
|
109 lin const bmid = xlim - ylim; /* Center diagonal of bottom-up search. */
|
joachim99@52
|
110 lin fmin = fmid, fmax = fmid; /* Limits of top-down search. */
|
joachim99@52
|
111 lin bmin = bmid, bmax = bmid; /* Limits of bottom-up search. */
|
joachim99@52
|
112 lin c; /* Cost. */
|
joachim99@52
|
113 bool odd = (fmid - bmid) & 1; /* True if southeast corner is on an odd
|
joachim99@52
|
114 diagonal with respect to the northwest. */
|
joachim99@52
|
115
|
joachim99@52
|
116 fd[fmid] = xoff;
|
joachim99@52
|
117 bd[bmid] = xlim;
|
joachim99@52
|
118
|
joachim99@52
|
119 for (c = 1;; ++c)
|
joachim99@52
|
120 {
|
joachim99@52
|
121 lin d; /* Active diagonal. */
|
joachim99@52
|
122 bool big_snake = 0;
|
joachim99@52
|
123
|
joachim99@52
|
124 /* Extend the top-down search by an edit step in each diagonal. */
|
joachim99@52
|
125 fmin > dmin ? fd[--fmin - 1] = -1 : ++fmin;
|
joachim99@52
|
126 fmax < dmax ? fd[++fmax + 1] = -1 : --fmax;
|
joachim99@52
|
127 for (d = fmax; d >= fmin; d -= 2)
|
joachim99@52
|
128 {
|
joachim99@52
|
129 lin x, y, oldx, tlo = fd[d - 1], thi = fd[d + 1];
|
joachim99@52
|
130
|
joachim99@52
|
131 if (tlo >= thi)
|
joachim99@52
|
132 x = tlo + 1;
|
joachim99@52
|
133 else
|
joachim99@52
|
134 x = thi;
|
joachim99@52
|
135 oldx = x;
|
joachim99@52
|
136 y = x - d;
|
joachim99@52
|
137 while (x < xlim && y < ylim && xv[x] == yv[y])
|
joachim99@52
|
138 ++x, ++y;
|
joachim99@52
|
139 if (x - oldx > SNAKE_LIMIT)
|
joachim99@52
|
140 big_snake = 1;
|
joachim99@52
|
141 fd[d] = x;
|
joachim99@52
|
142 if (odd && bmin <= d && d <= bmax && bd[d] <= x)
|
joachim99@52
|
143 {
|
joachim99@52
|
144 part->xmid = x;
|
joachim99@52
|
145 part->ymid = y;
|
joachim99@52
|
146 part->lo_minimal = part->hi_minimal = 1;
|
joachim99@52
|
147 return 2 * c - 1;
|
joachim99@52
|
148 }
|
joachim99@52
|
149 }
|
joachim99@52
|
150
|
joachim99@52
|
151 /* Similarly extend the bottom-up search. */
|
joachim99@52
|
152 bmin > dmin ? bd[--bmin - 1] = LIN_MAX : ++bmin;
|
joachim99@52
|
153 bmax < dmax ? bd[++bmax + 1] = LIN_MAX : --bmax;
|
joachim99@52
|
154 for (d = bmax; d >= bmin; d -= 2)
|
joachim99@52
|
155 {
|
joachim99@52
|
156 lin x, y, oldx, tlo = bd[d - 1], thi = bd[d + 1];
|
joachim99@52
|
157
|
joachim99@52
|
158 if (tlo < thi)
|
joachim99@52
|
159 x = tlo;
|
joachim99@52
|
160 else
|
joachim99@52
|
161 x = thi - 1;
|
joachim99@52
|
162 oldx = x;
|
joachim99@52
|
163 y = x - d;
|
joachim99@52
|
164 while (x > xoff && y > yoff && xv[x - 1] == yv[y - 1])
|
joachim99@52
|
165 --x, --y;
|
joachim99@52
|
166 if (oldx - x > SNAKE_LIMIT)
|
joachim99@52
|
167 big_snake = 1;
|
joachim99@52
|
168 bd[d] = x;
|
joachim99@52
|
169 if (!odd && fmin <= d && d <= fmax && x <= fd[d])
|
joachim99@52
|
170 {
|
joachim99@52
|
171 part->xmid = x;
|
joachim99@52
|
172 part->ymid = y;
|
joachim99@52
|
173 part->lo_minimal = part->hi_minimal = 1;
|
joachim99@52
|
174 return 2 * c;
|
joachim99@52
|
175 }
|
joachim99@52
|
176 }
|
joachim99@52
|
177
|
joachim99@52
|
178 if (find_minimal)
|
joachim99@52
|
179 continue;
|
joachim99@52
|
180
|
joachim99@52
|
181 /* Heuristic: check occasionally for a diagonal that has made
|
joachim99@52
|
182 lots of progress compared with the edit distance.
|
joachim99@52
|
183 If we have any such, find the one that has made the most
|
joachim99@52
|
184 progress and return it as if it had succeeded.
|
joachim99@52
|
185
|
joachim99@52
|
186 With this heuristic, for files with a constant small density
|
joachim99@52
|
187 of changes, the algorithm is linear in the file size. */
|
joachim99@52
|
188
|
joachim99@52
|
189 if (200 < c && big_snake && speed_large_files)
|
joachim99@52
|
190 {
|
joachim99@52
|
191 lin best;
|
joachim99@52
|
192
|
joachim99@52
|
193 best = 0;
|
joachim99@52
|
194 for (d = fmax; d >= fmin; d -= 2)
|
joachim99@52
|
195 {
|
joachim99@52
|
196 lin dd = d - fmid;
|
joachim99@52
|
197 lin x = fd[d];
|
joachim99@52
|
198 lin y = x - d;
|
joachim99@52
|
199 lin v = (x - xoff) * 2 - dd;
|
joachim99@52
|
200 if (v > 12 * (c + (dd < 0 ? -dd : dd)))
|
joachim99@52
|
201 {
|
joachim99@52
|
202 if (v > best
|
joachim99@52
|
203 && xoff + SNAKE_LIMIT <= x && x < xlim
|
joachim99@52
|
204 && yoff + SNAKE_LIMIT <= y && y < ylim)
|
joachim99@52
|
205 {
|
joachim99@52
|
206 /* We have a good enough best diagonal;
|
joachim99@52
|
207 now insist that it end with a significant snake. */
|
joachim99@52
|
208 int k;
|
joachim99@52
|
209
|
joachim99@52
|
210 for (k = 1; xv[x - k] == yv[y - k]; k++)
|
joachim99@52
|
211 if (k == SNAKE_LIMIT)
|
joachim99@52
|
212 {
|
joachim99@52
|
213 best = v;
|
joachim99@52
|
214 part->xmid = x;
|
joachim99@52
|
215 part->ymid = y;
|
joachim99@52
|
216 break;
|
joachim99@52
|
217 }
|
joachim99@52
|
218 }
|
joachim99@52
|
219 }
|
joachim99@52
|
220 }
|
joachim99@52
|
221 if (best > 0)
|
joachim99@52
|
222 {
|
joachim99@52
|
223 part->lo_minimal = 1;
|
joachim99@52
|
224 part->hi_minimal = 0;
|
joachim99@52
|
225 return 2 * c - 1;
|
joachim99@52
|
226 }
|
joachim99@52
|
227
|
joachim99@52
|
228 best = 0;
|
joachim99@52
|
229 for (d = bmax; d >= bmin; d -= 2)
|
joachim99@52
|
230 {
|
joachim99@52
|
231 lin dd = d - bmid;
|
joachim99@52
|
232 lin x = bd[d];
|
joachim99@52
|
233 lin y = x - d;
|
joachim99@52
|
234 lin v = (xlim - x) * 2 + dd;
|
joachim99@52
|
235 if (v > 12 * (c + (dd < 0 ? -dd : dd)))
|
joachim99@52
|
236 {
|
joachim99@52
|
237 if (v > best
|
joachim99@52
|
238 && xoff < x && x <= xlim - SNAKE_LIMIT
|
joachim99@52
|
239 && yoff < y && y <= ylim - SNAKE_LIMIT)
|
joachim99@52
|
240 {
|
joachim99@52
|
241 /* We have a good enough best diagonal;
|
joachim99@52
|
242 now insist that it end with a significant snake. */
|
joachim99@52
|
243 int k;
|
joachim99@52
|
244
|
joachim99@52
|
245 for (k = 0; xv[x + k] == yv[y + k]; k++)
|
joachim99@52
|
246 if (k == SNAKE_LIMIT - 1)
|
joachim99@52
|
247 {
|
joachim99@52
|
248 best = v;
|
joachim99@52
|
249 part->xmid = x;
|
joachim99@52
|
250 part->ymid = y;
|
joachim99@52
|
251 break;
|
joachim99@52
|
252 }
|
joachim99@52
|
253 }
|
joachim99@52
|
254 }
|
joachim99@52
|
255 }
|
joachim99@52
|
256 if (best > 0)
|
joachim99@52
|
257 {
|
joachim99@52
|
258 part->lo_minimal = 0;
|
joachim99@52
|
259 part->hi_minimal = 1;
|
joachim99@52
|
260 return 2 * c - 1;
|
joachim99@52
|
261 }
|
joachim99@52
|
262 }
|
joachim99@52
|
263
|
joachim99@52
|
264 /* Heuristic: if we've gone well beyond the call of duty,
|
joachim99@52
|
265 give up and report halfway between our best results so far. */
|
joachim99@52
|
266 if (c >= too_expensive)
|
joachim99@52
|
267 {
|
joachim99@52
|
268 lin fxybest, fxbest;
|
joachim99@52
|
269 lin bxybest, bxbest;
|
joachim99@52
|
270
|
joachim99@52
|
271 fxbest = bxbest = 0; /* Pacify `gcc -Wall'. */
|
joachim99@52
|
272
|
joachim99@52
|
273 /* Find forward diagonal that maximizes X + Y. */
|
joachim99@52
|
274 fxybest = -1;
|
joachim99@52
|
275 for (d = fmax; d >= fmin; d -= 2)
|
joachim99@52
|
276 {
|
joachim99@52
|
277 lin x = MIN (fd[d], xlim);
|
joachim99@52
|
278 lin y = x - d;
|
joachim99@52
|
279 if (ylim < y)
|
joachim99@52
|
280 x = ylim + d, y = ylim;
|
joachim99@52
|
281 if (fxybest < x + y)
|
joachim99@52
|
282 {
|
joachim99@52
|
283 fxybest = x + y;
|
joachim99@52
|
284 fxbest = x;
|
joachim99@52
|
285 }
|
joachim99@52
|
286 }
|
joachim99@52
|
287
|
joachim99@52
|
288 /* Find backward diagonal that minimizes X + Y. */
|
joachim99@52
|
289 bxybest = LIN_MAX;
|
joachim99@52
|
290 for (d = bmax; d >= bmin; d -= 2)
|
joachim99@52
|
291 {
|
joachim99@52
|
292 lin x = MAX (xoff, bd[d]);
|
joachim99@52
|
293 lin y = x - d;
|
joachim99@52
|
294 if (y < yoff)
|
joachim99@52
|
295 x = yoff + d, y = yoff;
|
joachim99@52
|
296 if (x + y < bxybest)
|
joachim99@52
|
297 {
|
joachim99@52
|
298 bxybest = x + y;
|
joachim99@52
|
299 bxbest = x;
|
joachim99@52
|
300 }
|
joachim99@52
|
301 }
|
joachim99@52
|
302
|
joachim99@52
|
303 /* Use the better of the two diagonals. */
|
joachim99@52
|
304 if ((xlim + ylim) - bxybest < fxybest - (xoff + yoff))
|
joachim99@52
|
305 {
|
joachim99@52
|
306 part->xmid = fxbest;
|
joachim99@52
|
307 part->ymid = fxybest - fxbest;
|
joachim99@52
|
308 part->lo_minimal = 1;
|
joachim99@52
|
309 part->hi_minimal = 0;
|
joachim99@52
|
310 }
|
joachim99@52
|
311 else
|
joachim99@52
|
312 {
|
joachim99@52
|
313 part->xmid = bxbest;
|
joachim99@52
|
314 part->ymid = bxybest - bxbest;
|
joachim99@52
|
315 part->lo_minimal = 0;
|
joachim99@52
|
316 part->hi_minimal = 1;
|
joachim99@52
|
317 }
|
joachim99@52
|
318 return 2 * c - 1;
|
joachim99@52
|
319 }
|
joachim99@52
|
320 }
|
joachim99@52
|
321 }
|
joachim99@52
|
322
|
joachim99@52
|
323 /* Compare in detail contiguous subsequences of the two files
|
joachim99@52
|
324 which are known, as a whole, to match each other.
|
joachim99@52
|
325
|
joachim99@52
|
326 The results are recorded in the vectors files[N].changed, by
|
joachim99@52
|
327 storing 1 in the element for each line that is an insertion or deletion.
|
joachim99@52
|
328
|
joachim99@52
|
329 The subsequence of file 0 is [XOFF, XLIM) and likewise for file 1.
|
joachim99@52
|
330
|
joachim99@52
|
331 Note that XLIM, YLIM are exclusive bounds.
|
joachim99@52
|
332 All line numbers are origin-0 and discarded lines are not counted.
|
joachim99@52
|
333
|
joachim99@52
|
334 If FIND_MINIMAL, find a minimal difference no matter how
|
joachim99@52
|
335 expensive it is. */
|
joachim99@52
|
336
|
joachim99@52
|
337 static void
|
joachim99@52
|
338 compareseq (lin xoff, lin xlim, lin yoff, lin ylim, bool find_minimal)
|
joachim99@52
|
339 {
|
joachim99@52
|
340 lin * const xv = xvec; /* Help the compiler. */
|
joachim99@52
|
341 lin * const yv = yvec;
|
joachim99@52
|
342
|
joachim99@52
|
343 /* Slide down the bottom initial diagonal. */
|
joachim99@52
|
344 while (xoff < xlim && yoff < ylim && xv[xoff] == yv[yoff])
|
joachim99@52
|
345 ++xoff, ++yoff;
|
joachim99@52
|
346 /* Slide up the top initial diagonal. */
|
joachim99@52
|
347 while (xlim > xoff && ylim > yoff && xv[xlim - 1] == yv[ylim - 1])
|
joachim99@52
|
348 --xlim, --ylim;
|
joachim99@52
|
349
|
joachim99@52
|
350 /* Handle simple cases. */
|
joachim99@52
|
351 if (xoff == xlim)
|
joachim99@52
|
352 while (yoff < ylim)
|
joachim99@52
|
353 files[1].changed[files[1].realindexes[yoff++]] = 1;
|
joachim99@52
|
354 else if (yoff == ylim)
|
joachim99@52
|
355 while (xoff < xlim)
|
joachim99@52
|
356 files[0].changed[files[0].realindexes[xoff++]] = 1;
|
joachim99@52
|
357 else
|
joachim99@52
|
358 {
|
joachim99@52
|
359 lin c;
|
joachim99@52
|
360 struct partition part;
|
joachim99@52
|
361
|
joachim99@52
|
362 /* Find a point of correspondence in the middle of the files. */
|
joachim99@52
|
363
|
joachim99@52
|
364 c = diag (xoff, xlim, yoff, ylim, find_minimal, &part);
|
joachim99@52
|
365
|
joachim99@52
|
366 if (c == 1)
|
joachim99@52
|
367 {
|
joachim99@52
|
368 /* This should be impossible, because it implies that
|
joachim99@52
|
369 one of the two subsequences is empty,
|
joachim99@52
|
370 and that case was handled above without calling `diag'.
|
joachim99@52
|
371 Let's verify that this is true. */
|
joachim99@52
|
372 abort ();
|
joachim99@52
|
373 #if 0
|
joachim99@52
|
374 /* The two subsequences differ by a single insert or delete;
|
joachim99@52
|
375 record it and we are done. */
|
joachim99@52
|
376 if (part.xmid - part.ymid < xoff - yoff)
|
joachim99@52
|
377 files[1].changed[files[1].realindexes[part.ymid - 1]] = 1;
|
joachim99@52
|
378 else
|
joachim99@52
|
379 files[0].changed[files[0].realindexes[part.xmid]] = 1;
|
joachim99@52
|
380 #endif
|
joachim99@52
|
381 }
|
joachim99@52
|
382 else
|
joachim99@52
|
383 {
|
joachim99@52
|
384 /* Use the partitions to split this problem into subproblems. */
|
joachim99@52
|
385 compareseq (xoff, part.xmid, yoff, part.ymid, part.lo_minimal);
|
joachim99@52
|
386 compareseq (part.xmid, xlim, part.ymid, ylim, part.hi_minimal);
|
joachim99@52
|
387 }
|
joachim99@52
|
388 }
|
joachim99@52
|
389 }
|
joachim99@52
|
390
|
joachim99@52
|
391 /* Discard lines from one file that have no matches in the other file.
|
joachim99@52
|
392
|
joachim99@52
|
393 A line which is discarded will not be considered by the actual
|
joachim99@52
|
394 comparison algorithm; it will be as if that line were not in the file.
|
joachim99@52
|
395 The file's `realindexes' table maps virtual line numbers
|
joachim99@52
|
396 (which don't count the discarded lines) into real line numbers;
|
joachim99@52
|
397 this is how the actual comparison algorithm produces results
|
joachim99@52
|
398 that are comprehensible when the discarded lines are counted.
|
joachim99@52
|
399
|
joachim99@52
|
400 When we discard a line, we also mark it as a deletion or insertion
|
joachim99@52
|
401 so that it will be printed in the output. */
|
joachim99@52
|
402
|
joachim99@52
|
403 static void
|
joachim99@52
|
404 discard_confusing_lines (struct file_data filevec[])
|
joachim99@52
|
405 {
|
joachim99@52
|
406 int f;
|
joachim99@52
|
407 lin i;
|
joachim99@52
|
408 char *discarded[2];
|
joachim99@52
|
409 lin *equiv_count[2];
|
joachim99@52
|
410 lin *p;
|
joachim99@52
|
411
|
joachim99@52
|
412 /* Allocate our results. */
|
joachim99@52
|
413 p = (lin*)xmalloc ((filevec[0].buffered_lines + filevec[1].buffered_lines)
|
joachim99@52
|
414 * (2 * sizeof *p));
|
joachim99@52
|
415 for (f = 0; f < 2; f++)
|
joachim99@52
|
416 {
|
joachim99@52
|
417 filevec[f].undiscarded = p; p += filevec[f].buffered_lines;
|
joachim99@52
|
418 filevec[f].realindexes = p; p += filevec[f].buffered_lines;
|
joachim99@52
|
419 }
|
joachim99@52
|
420
|
joachim99@52
|
421 /* Set up equiv_count[F][I] as the number of lines in file F
|
joachim99@52
|
422 that fall in equivalence class I. */
|
joachim99@52
|
423
|
joachim99@52
|
424 p = (lin*)zalloc (filevec[0].equiv_max * (2 * sizeof *p));
|
joachim99@52
|
425 equiv_count[0] = p;
|
joachim99@52
|
426 equiv_count[1] = p + filevec[0].equiv_max;
|
joachim99@52
|
427
|
joachim99@52
|
428 for (i = 0; i < filevec[0].buffered_lines; ++i)
|
joachim99@52
|
429 ++equiv_count[0][filevec[0].equivs[i]];
|
joachim99@52
|
430 for (i = 0; i < filevec[1].buffered_lines; ++i)
|
joachim99@52
|
431 ++equiv_count[1][filevec[1].equivs[i]];
|
joachim99@52
|
432
|
joachim99@52
|
433 /* Set up tables of which lines are going to be discarded. */
|
joachim99@52
|
434
|
joachim99@52
|
435 discarded[0] = (char*)zalloc (filevec[0].buffered_lines
|
joachim99@52
|
436 + filevec[1].buffered_lines);
|
joachim99@52
|
437 discarded[1] = discarded[0] + filevec[0].buffered_lines;
|
joachim99@52
|
438
|
joachim99@52
|
439 /* Mark to be discarded each line that matches no line of the other file.
|
joachim99@52
|
440 If a line matches many lines, mark it as provisionally discardable. */
|
joachim99@52
|
441
|
joachim99@52
|
442 for (f = 0; f < 2; f++)
|
joachim99@52
|
443 {
|
joachim99@52
|
444 size_t end = filevec[f].buffered_lines;
|
joachim99@52
|
445 char *discards = discarded[f];
|
joachim99@52
|
446 lin *counts = equiv_count[1 - f];
|
joachim99@52
|
447 lin *equivs = filevec[f].equivs;
|
joachim99@52
|
448 size_t many = 5;
|
joachim99@52
|
449 size_t tem = end / 64;
|
joachim99@52
|
450
|
joachim99@52
|
451 /* Multiply MANY by approximate square root of number of lines.
|
joachim99@52
|
452 That is the threshold for provisionally discardable lines. */
|
joachim99@52
|
453 while ((tem = tem >> 2) > 0)
|
joachim99@52
|
454 many *= 2;
|
joachim99@52
|
455
|
joachim99@52
|
456 for (i = 0; i < (int)end; i++)
|
joachim99@52
|
457 {
|
joachim99@52
|
458 lin nmatch;
|
joachim99@52
|
459 if (equivs[i] == 0)
|
joachim99@52
|
460 continue;
|
joachim99@52
|
461 nmatch = counts[equivs[i]];
|
joachim99@52
|
462 if (nmatch == 0)
|
joachim99@52
|
463 discards[i] = 1;
|
joachim99@52
|
464 else if (nmatch > (int)many)
|
joachim99@52
|
465 discards[i] = 2;
|
joachim99@52
|
466 }
|
joachim99@52
|
467 }
|
joachim99@52
|
468
|
joachim99@52
|
469 /* Don't really discard the provisional lines except when they occur
|
joachim99@52
|
470 in a run of discardables, with nonprovisionals at the beginning
|
joachim99@52
|
471 and end. */
|
joachim99@52
|
472
|
joachim99@52
|
473 for (f = 0; f < 2; f++)
|
joachim99@52
|
474 {
|
joachim99@52
|
475 lin end = filevec[f].buffered_lines;
|
joachim99@52
|
476 register char *discards = discarded[f];
|
joachim99@52
|
477
|
joachim99@52
|
478 for (i = 0; i < end; i++)
|
joachim99@52
|
479 {
|
joachim99@52
|
480 /* Cancel provisional discards not in middle of run of discards. */
|
joachim99@52
|
481 if (discards[i] == 2)
|
joachim99@52
|
482 discards[i] = 0;
|
joachim99@52
|
483 else if (discards[i] != 0)
|
joachim99@52
|
484 {
|
joachim99@52
|
485 /* We have found a nonprovisional discard. */
|
joachim99@52
|
486 register lin j;
|
joachim99@52
|
487 lin length;
|
joachim99@52
|
488 lin provisional = 0;
|
joachim99@52
|
489
|
joachim99@52
|
490 /* Find end of this run of discardable lines.
|
joachim99@52
|
491 Count how many are provisionally discardable. */
|
joachim99@52
|
492 for (j = i; j < end; j++)
|
joachim99@52
|
493 {
|
joachim99@52
|
494 if (discards[j] == 0)
|
joachim99@52
|
495 break;
|
joachim99@52
|
496 if (discards[j] == 2)
|
joachim99@52
|
497 ++provisional;
|
joachim99@52
|
498 }
|
joachim99@52
|
499
|
joachim99@52
|
500 /* Cancel provisional discards at end, and shrink the run. */
|
joachim99@52
|
501 while (j > i && discards[j - 1] == 2)
|
joachim99@52
|
502 discards[--j] = 0, --provisional;
|
joachim99@52
|
503
|
joachim99@52
|
504 /* Now we have the length of a run of discardable lines
|
joachim99@52
|
505 whose first and last are not provisional. */
|
joachim99@52
|
506 length = j - i;
|
joachim99@52
|
507
|
joachim99@52
|
508 /* If 1/4 of the lines in the run are provisional,
|
joachim99@52
|
509 cancel discarding of all provisional lines in the run. */
|
joachim99@52
|
510 if (provisional * 4 > length)
|
joachim99@52
|
511 {
|
joachim99@52
|
512 while (j > i)
|
joachim99@52
|
513 if (discards[--j] == 2)
|
joachim99@52
|
514 discards[j] = 0;
|
joachim99@52
|
515 }
|
joachim99@52
|
516 else
|
joachim99@52
|
517 {
|
joachim99@52
|
518 register lin consec;
|
joachim99@52
|
519 lin minimum = 1;
|
joachim99@52
|
520 lin tem = length >> 2;
|
joachim99@52
|
521
|
joachim99@52
|
522 /* MINIMUM is approximate square root of LENGTH/4.
|
joachim99@52
|
523 A subrun of two or more provisionals can stand
|
joachim99@52
|
524 when LENGTH is at least 16.
|
joachim99@52
|
525 A subrun of 4 or more can stand when LENGTH >= 64. */
|
joachim99@52
|
526 while (0 < (tem >>= 2))
|
joachim99@52
|
527 minimum <<= 1;
|
joachim99@52
|
528 minimum++;
|
joachim99@52
|
529
|
joachim99@52
|
530 /* Cancel any subrun of MINIMUM or more provisionals
|
joachim99@52
|
531 within the larger run. */
|
joachim99@52
|
532 for (j = 0, consec = 0; j < length; j++)
|
joachim99@52
|
533 if (discards[i + j] != 2)
|
joachim99@52
|
534 consec = 0;
|
joachim99@52
|
535 else if (minimum == ++consec)
|
joachim99@52
|
536 /* Back up to start of subrun, to cancel it all. */
|
joachim99@52
|
537 j -= consec;
|
joachim99@52
|
538 else if (minimum < consec)
|
joachim99@52
|
539 discards[i + j] = 0;
|
joachim99@52
|
540
|
joachim99@52
|
541 /* Scan from beginning of run
|
joachim99@52
|
542 until we find 3 or more nonprovisionals in a row
|
joachim99@52
|
543 or until the first nonprovisional at least 8 lines in.
|
joachim99@52
|
544 Until that point, cancel any provisionals. */
|
joachim99@52
|
545 for (j = 0, consec = 0; j < length; j++)
|
joachim99@52
|
546 {
|
joachim99@52
|
547 if (j >= 8 && discards[i + j] == 1)
|
joachim99@52
|
548 break;
|
joachim99@52
|
549 if (discards[i + j] == 2)
|
joachim99@52
|
550 consec = 0, discards[i + j] = 0;
|
joachim99@52
|
551 else if (discards[i + j] == 0)
|
joachim99@52
|
552 consec = 0;
|
joachim99@52
|
553 else
|
joachim99@52
|
554 consec++;
|
joachim99@52
|
555 if (consec == 3)
|
joachim99@52
|
556 break;
|
joachim99@52
|
557 }
|
joachim99@52
|
558
|
joachim99@52
|
559 /* I advances to the last line of the run. */
|
joachim99@52
|
560 i += length - 1;
|
joachim99@52
|
561
|
joachim99@52
|
562 /* Same thing, from end. */
|
joachim99@52
|
563 for (j = 0, consec = 0; j < length; j++)
|
joachim99@52
|
564 {
|
joachim99@52
|
565 if (j >= 8 && discards[i - j] == 1)
|
joachim99@52
|
566 break;
|
joachim99@52
|
567 if (discards[i - j] == 2)
|
joachim99@52
|
568 consec = 0, discards[i - j] = 0;
|
joachim99@52
|
569 else if (discards[i - j] == 0)
|
joachim99@52
|
570 consec = 0;
|
joachim99@52
|
571 else
|
joachim99@52
|
572 consec++;
|
joachim99@52
|
573 if (consec == 3)
|
joachim99@52
|
574 break;
|
joachim99@52
|
575 }
|
joachim99@52
|
576 }
|
joachim99@52
|
577 }
|
joachim99@52
|
578 }
|
joachim99@52
|
579 }
|
joachim99@52
|
580
|
joachim99@52
|
581 /* Actually discard the lines. */
|
joachim99@52
|
582 for (f = 0; f < 2; f++)
|
joachim99@52
|
583 {
|
joachim99@52
|
584 char *discards = discarded[f];
|
joachim99@52
|
585 lin end = filevec[f].buffered_lines;
|
joachim99@52
|
586 lin j = 0;
|
joachim99@52
|
587 for (i = 0; i < end; ++i)
|
joachim99@52
|
588 if (minimal || discards[i] == 0)
|
joachim99@52
|
589 {
|
joachim99@52
|
590 filevec[f].undiscarded[j] = filevec[f].equivs[i];
|
joachim99@52
|
591 filevec[f].realindexes[j++] = i;
|
joachim99@52
|
592 }
|
joachim99@52
|
593 else
|
joachim99@52
|
594 filevec[f].changed[i] = 1;
|
joachim99@52
|
595 filevec[f].nondiscarded_lines = j;
|
joachim99@52
|
596 }
|
joachim99@52
|
597
|
joachim99@52
|
598 free (discarded[0]);
|
joachim99@52
|
599 free (equiv_count[0]);
|
joachim99@52
|
600 }
|
joachim99@52
|
601
|
joachim99@52
|
602 /* Adjust inserts/deletes of identical lines to join changes
|
joachim99@52
|
603 as much as possible.
|
joachim99@52
|
604
|
joachim99@52
|
605 We do something when a run of changed lines include a
|
joachim99@52
|
606 line at one end and have an excluded, identical line at the other.
|
joachim99@52
|
607 We are free to choose which identical line is included.
|
joachim99@52
|
608 `compareseq' usually chooses the one at the beginning,
|
joachim99@52
|
609 but usually it is cleaner to consider the following identical line
|
joachim99@52
|
610 to be the "change". */
|
joachim99@52
|
611
|
joachim99@52
|
612 static void
|
joachim99@52
|
613 shift_boundaries (struct file_data filevec[])
|
joachim99@52
|
614 {
|
joachim99@52
|
615 int f;
|
joachim99@52
|
616
|
joachim99@52
|
617 for (f = 0; f < 2; f++)
|
joachim99@52
|
618 {
|
joachim99@52
|
619 bool *changed = filevec[f].changed;
|
joachim99@52
|
620 bool const *other_changed = filevec[1 - f].changed;
|
joachim99@52
|
621 lin const *equivs = filevec[f].equivs;
|
joachim99@52
|
622 lin i = 0;
|
joachim99@52
|
623 lin j = 0;
|
joachim99@52
|
624 lin i_end = filevec[f].buffered_lines;
|
joachim99@52
|
625
|
joachim99@52
|
626 while (1)
|
joachim99@52
|
627 {
|
joachim99@52
|
628 lin runlength, start, corresponding;
|
joachim99@52
|
629
|
joachim99@52
|
630 /* Scan forwards to find beginning of another run of changes.
|
joachim99@52
|
631 Also keep track of the corresponding point in the other file. */
|
joachim99@52
|
632
|
joachim99@52
|
633 while (i < i_end && !changed[i])
|
joachim99@52
|
634 {
|
joachim99@52
|
635 while (other_changed[j++])
|
joachim99@52
|
636 continue;
|
joachim99@52
|
637 i++;
|
joachim99@52
|
638 }
|
joachim99@52
|
639
|
joachim99@52
|
640 if (i == i_end)
|
joachim99@52
|
641 break;
|
joachim99@52
|
642
|
joachim99@52
|
643 start = i;
|
joachim99@52
|
644
|
joachim99@52
|
645 /* Find the end of this run of changes. */
|
joachim99@52
|
646
|
joachim99@52
|
647 while (changed[++i])
|
joachim99@52
|
648 continue;
|
joachim99@52
|
649 while (other_changed[j])
|
joachim99@52
|
650 j++;
|
joachim99@52
|
651
|
joachim99@52
|
652 do
|
joachim99@52
|
653 {
|
joachim99@52
|
654 /* Record the length of this run of changes, so that
|
joachim99@52
|
655 we can later determine whether the run has grown. */
|
joachim99@52
|
656 runlength = i - start;
|
joachim99@52
|
657
|
joachim99@52
|
658 /* Move the changed region back, so long as the
|
joachim99@52
|
659 previous unchanged line matches the last changed one.
|
joachim99@52
|
660 This merges with previous changed regions. */
|
joachim99@52
|
661
|
joachim99@52
|
662 while (start && equivs[start - 1] == equivs[i - 1])
|
joachim99@52
|
663 {
|
joachim99@52
|
664 changed[--start] = 1;
|
joachim99@52
|
665 changed[--i] = 0;
|
joachim99@52
|
666 while (changed[start - 1])
|
joachim99@52
|
667 start--;
|
joachim99@52
|
668 while (other_changed[--j])
|
joachim99@52
|
669 continue;
|
joachim99@52
|
670 }
|
joachim99@52
|
671
|
joachim99@52
|
672 /* Set CORRESPONDING to the end of the changed run, at the last
|
joachim99@52
|
673 point where it corresponds to a changed run in the other file.
|
joachim99@52
|
674 CORRESPONDING == I_END means no such point has been found. */
|
joachim99@52
|
675 corresponding = other_changed[j - 1] ? i : i_end;
|
joachim99@52
|
676
|
joachim99@52
|
677 /* Move the changed region forward, so long as the
|
joachim99@52
|
678 first changed line matches the following unchanged one.
|
joachim99@52
|
679 This merges with following changed regions.
|
joachim99@52
|
680 Do this second, so that if there are no merges,
|
joachim99@52
|
681 the changed region is moved forward as far as possible. */
|
joachim99@52
|
682
|
joachim99@52
|
683 while (i != i_end && equivs[start] == equivs[i])
|
joachim99@52
|
684 {
|
joachim99@52
|
685 changed[start++] = 0;
|
joachim99@52
|
686 changed[i++] = 1;
|
joachim99@52
|
687 while (changed[i])
|
joachim99@52
|
688 i++;
|
joachim99@52
|
689 while (other_changed[++j])
|
joachim99@52
|
690 corresponding = i;
|
joachim99@52
|
691 }
|
joachim99@52
|
692 }
|
joachim99@52
|
693 while (runlength != i - start);
|
joachim99@52
|
694
|
joachim99@52
|
695 /* If possible, move the fully-merged run of changes
|
joachim99@52
|
696 back to a corresponding run in the other file. */
|
joachim99@52
|
697
|
joachim99@52
|
698 while (corresponding < i)
|
joachim99@52
|
699 {
|
joachim99@52
|
700 changed[--start] = 1;
|
joachim99@52
|
701 changed[--i] = 0;
|
joachim99@52
|
702 while (other_changed[--j])
|
joachim99@52
|
703 continue;
|
joachim99@52
|
704 }
|
joachim99@52
|
705 }
|
joachim99@52
|
706 }
|
joachim99@52
|
707 }
|
joachim99@52
|
708
|
joachim99@52
|
709 /* Cons an additional entry onto the front of an edit script OLD.
|
joachim99@52
|
710 LINE0 and LINE1 are the first affected lines in the two files (origin 0).
|
joachim99@52
|
711 DELETED is the number of lines deleted here from file 0.
|
joachim99@52
|
712 INSERTED is the number of lines inserted here in file 1.
|
joachim99@52
|
713
|
joachim99@52
|
714 If DELETED is 0 then LINE0 is the number of the line before
|
joachim99@52
|
715 which the insertion was done; vice versa for INSERTED and LINE1. */
|
joachim99@52
|
716
|
joachim99@52
|
717 static struct change *
|
joachim99@52
|
718 add_change (lin line0, lin line1, lin deleted, lin inserted,
|
joachim99@52
|
719 struct change *old)
|
joachim99@52
|
720 {
|
joachim99@52
|
721 struct change *newChange = (change*) xmalloc (sizeof *newChange);
|
joachim99@52
|
722
|
joachim99@52
|
723 newChange->line0 = line0;
|
joachim99@52
|
724 newChange->line1 = line1;
|
joachim99@52
|
725 newChange->inserted = inserted;
|
joachim99@52
|
726 newChange->deleted = deleted;
|
joachim99@52
|
727 newChange->link = old;
|
joachim99@52
|
728 return newChange;
|
joachim99@52
|
729 }
|
joachim99@52
|
730
|
joachim99@52
|
731 /* Scan the tables of which lines are inserted and deleted,
|
joachim99@52
|
732 producing an edit script in reverse order. */
|
joachim99@52
|
733
|
joachim99@52
|
734 static struct change *
|
joachim99@52
|
735 build_reverse_script (struct file_data const filevec[])
|
joachim99@52
|
736 {
|
joachim99@52
|
737 struct change *script = 0;
|
joachim99@52
|
738 bool *changed0 = filevec[0].changed;
|
joachim99@52
|
739 bool *changed1 = filevec[1].changed;
|
joachim99@52
|
740 lin len0 = filevec[0].buffered_lines;
|
joachim99@52
|
741 lin len1 = filevec[1].buffered_lines;
|
joachim99@52
|
742
|
joachim99@52
|
743 /* Note that changedN[len0] does exist, and is 0. */
|
joachim99@52
|
744
|
joachim99@52
|
745 lin i0 = 0, i1 = 0;
|
joachim99@52
|
746
|
joachim99@52
|
747 while (i0 < len0 || i1 < len1)
|
joachim99@52
|
748 {
|
joachim99@52
|
749 if (changed0[i0] | changed1[i1])
|
joachim99@52
|
750 {
|
joachim99@52
|
751 lin line0 = i0, line1 = i1;
|
joachim99@52
|
752
|
joachim99@52
|
753 /* Find # lines changed here in each file. */
|
joachim99@52
|
754 while (changed0[i0]) ++i0;
|
joachim99@52
|
755 while (changed1[i1]) ++i1;
|
joachim99@52
|
756
|
joachim99@52
|
757 /* Record this change. */
|
joachim99@52
|
758 script = add_change (line0, line1, i0 - line0, i1 - line1, script);
|
joachim99@52
|
759 }
|
joachim99@52
|
760
|
joachim99@52
|
761 /* We have reached lines in the two files that match each other. */
|
joachim99@52
|
762 i0++, i1++;
|
joachim99@52
|
763 }
|
joachim99@52
|
764
|
joachim99@52
|
765 return script;
|
joachim99@52
|
766 }
|
joachim99@52
|
767
|
joachim99@52
|
768 /* Scan the tables of which lines are inserted and deleted,
|
joachim99@52
|
769 producing an edit script in forward order. */
|
joachim99@52
|
770
|
joachim99@52
|
771 static struct change *
|
joachim99@52
|
772 build_script (struct file_data const filevec[])
|
joachim99@52
|
773 {
|
joachim99@52
|
774 struct change *script = 0;
|
joachim99@52
|
775 bool *changed0 = filevec[0].changed;
|
joachim99@52
|
776 bool *changed1 = filevec[1].changed;
|
joachim99@52
|
777 lin i0 = filevec[0].buffered_lines, i1 = filevec[1].buffered_lines;
|
joachim99@52
|
778
|
joachim99@52
|
779 /* Note that changedN[-1] does exist, and is 0. */
|
joachim99@52
|
780
|
joachim99@52
|
781 while (i0 >= 0 || i1 >= 0)
|
joachim99@52
|
782 {
|
joachim99@52
|
783 if (changed0[i0 - 1] | changed1[i1 - 1])
|
joachim99@52
|
784 {
|
joachim99@52
|
785 lin line0 = i0, line1 = i1;
|
joachim99@52
|
786
|
joachim99@52
|
787 /* Find # lines changed here in each file. */
|
joachim99@52
|
788 while (changed0[i0 - 1]) --i0;
|
joachim99@52
|
789 while (changed1[i1 - 1]) --i1;
|
joachim99@52
|
790
|
joachim99@52
|
791 /* Record this change. */
|
joachim99@52
|
792 script = add_change (i0, i1, line0 - i0, line1 - i1, script);
|
joachim99@52
|
793 }
|
joachim99@52
|
794
|
joachim99@52
|
795 /* We have reached lines in the two files that match each other. */
|
joachim99@52
|
796 i0--, i1--;
|
joachim99@52
|
797 }
|
joachim99@52
|
798
|
joachim99@52
|
799 return script;
|
joachim99@52
|
800 }
|
joachim99@52
|
801
|
joachim99@52
|
802
|
joachim99@52
|
803 /* Report the differences of two files. */
|
joachim99@52
|
804 struct change* diff_2_files (struct comparison *cmp)
|
joachim99@52
|
805 {
|
joachim99@52
|
806 lin diags;
|
joachim99@52
|
807 int f;
|
joachim99@52
|
808 //struct change *e, *p;
|
joachim99@52
|
809 struct change *script;
|
joachim99@52
|
810 int changes;
|
joachim99@52
|
811
|
joachim99@52
|
812 read_files (cmp->file, files_can_be_treated_as_binary);
|
joachim99@52
|
813
|
joachim99@52
|
814 {
|
joachim99@52
|
815 /* Allocate vectors for the results of comparison:
|
joachim99@52
|
816 a flag for each line of each file, saying whether that line
|
joachim99@52
|
817 is an insertion or deletion.
|
joachim99@52
|
818 Allocate an extra element, always 0, at each end of each vector. */
|
joachim99@52
|
819
|
joachim99@52
|
820 size_t s = cmp->file[0].buffered_lines + cmp->file[1].buffered_lines + 4;
|
joachim99@52
|
821 bool *flag_space = (bool*)zalloc (s * sizeof(*flag_space));
|
joachim99@52
|
822 cmp->file[0].changed = flag_space + 1;
|
joachim99@52
|
823 cmp->file[1].changed = flag_space + cmp->file[0].buffered_lines + 3;
|
joachim99@52
|
824
|
joachim99@52
|
825 /* Some lines are obviously insertions or deletions
|
joachim99@52
|
826 because they don't match anything. Detect them now, and
|
joachim99@52
|
827 avoid even thinking about them in the main comparison algorithm. */
|
joachim99@52
|
828
|
joachim99@52
|
829 discard_confusing_lines (cmp->file);
|
joachim99@52
|
830
|
joachim99@52
|
831 /* Now do the main comparison algorithm, considering just the
|
joachim99@52
|
832 undiscarded lines. */
|
joachim99@52
|
833
|
joachim99@52
|
834 xvec = cmp->file[0].undiscarded;
|
joachim99@52
|
835 yvec = cmp->file[1].undiscarded;
|
joachim99@52
|
836 diags = (cmp->file[0].nondiscarded_lines
|
joachim99@52
|
837 + cmp->file[1].nondiscarded_lines + 3);
|
joachim99@52
|
838 fdiag = (lin*)xmalloc (diags * (2 * sizeof *fdiag));
|
joachim99@52
|
839 bdiag = fdiag + diags;
|
joachim99@52
|
840 fdiag += cmp->file[1].nondiscarded_lines + 1;
|
joachim99@52
|
841 bdiag += cmp->file[1].nondiscarded_lines + 1;
|
joachim99@52
|
842
|
joachim99@52
|
843 /* Set TOO_EXPENSIVE to be approximate square root of input size,
|
joachim99@52
|
844 bounded below by 256. */
|
joachim99@52
|
845 too_expensive = 1;
|
joachim99@52
|
846 for (; diags != 0; diags >>= 2)
|
joachim99@52
|
847 too_expensive <<= 1;
|
joachim99@52
|
848 too_expensive = MAX (256, too_expensive);
|
joachim99@52
|
849
|
joachim99@52
|
850 files[0] = cmp->file[0];
|
joachim99@52
|
851 files[1] = cmp->file[1];
|
joachim99@52
|
852
|
joachim99@52
|
853 compareseq (0, cmp->file[0].nondiscarded_lines,
|
joachim99@52
|
854 0, cmp->file[1].nondiscarded_lines, minimal);
|
joachim99@52
|
855
|
joachim99@52
|
856 free (fdiag - (cmp->file[1].nondiscarded_lines + 1));
|
joachim99@52
|
857
|
joachim99@52
|
858 /* Modify the results slightly to make them prettier
|
joachim99@52
|
859 in cases where that can validly be done. */
|
joachim99@52
|
860
|
joachim99@52
|
861 shift_boundaries (cmp->file);
|
joachim99@52
|
862
|
joachim99@52
|
863 /* Get the results of comparison in the form of a chain
|
joachim99@52
|
864 of `struct change's -- an edit script. */
|
joachim99@52
|
865
|
joachim99@52
|
866 if (output_style == OUTPUT_ED)
|
joachim99@52
|
867 script = build_reverse_script (cmp->file);
|
joachim99@52
|
868 else
|
joachim99@52
|
869 script = build_script (cmp->file);
|
joachim99@52
|
870
|
joachim99@52
|
871 changes = (script != 0);
|
joachim99@52
|
872
|
joachim99@52
|
873 free (cmp->file[0].undiscarded);
|
joachim99@52
|
874
|
joachim99@52
|
875 free (flag_space);
|
joachim99@52
|
876
|
joachim99@52
|
877 for (f = 0; f < 2; f++)
|
joachim99@52
|
878 {
|
joachim99@52
|
879 free (cmp->file[f].equivs);
|
joachim99@52
|
880 free (cmp->file[f].linbuf + cmp->file[f].linbuf_base);
|
joachim99@52
|
881 }
|
joachim99@52
|
882 }
|
joachim99@52
|
883
|
joachim99@52
|
884 return script;
|
joachim99@52
|
885 }
|
joachim99@52
|
886
|
joachim99@52
|
887 inline bool isWhite( char c )
|
joachim99@52
|
888 {
|
joachim99@52
|
889 return c==' ' || c=='\t' || c=='\r';
|
joachim99@52
|
890 }
|
joachim99@52
|
891
|
joachim99@52
|
892 /* Compare two lines (typically one from each input file)
|
joachim99@52
|
893 according to the command line options.
|
joachim99@52
|
894 For efficiency, this is invoked only when the lines do not match exactly
|
joachim99@52
|
895 but an option like -i might cause us to ignore the difference.
|
joachim99@52
|
896 Return nonzero if the lines differ. */
|
joachim99@52
|
897
|
joachim99@52
|
898 bool
|
joachim99@52
|
899 lines_differ (char const *s1, char const *s2)
|
joachim99@52
|
900 {
|
joachim99@52
|
901 register unsigned char const *t1 = (unsigned char const *) s1;
|
joachim99@52
|
902 register unsigned char const *t2 = (unsigned char const *) s2;
|
joachim99@52
|
903 size_t column = 0;
|
joachim99@52
|
904
|
joachim99@52
|
905 while (1)
|
joachim99@52
|
906 {
|
joachim99@52
|
907 register unsigned char c1 = *t1++;
|
joachim99@52
|
908 register unsigned char c2 = *t2++;
|
joachim99@52
|
909
|
joachim99@52
|
910 /* Test for exact char equality first, since it's a common case. */
|
joachim99@52
|
911 if (c1 != c2)
|
joachim99@52
|
912 {
|
joachim99@52
|
913 while ( bIgnoreWhiteSpace && isWhite( c1 ) ||
|
joachim99@52
|
914 bIgnoreNumbers && (isdigit( c1 ) || c1=='-' || c1=='.' ))
|
joachim99@52
|
915 c1 = *t1++;
|
joachim99@52
|
916
|
joachim99@52
|
917 while ( bIgnoreWhiteSpace && isWhite( c2 ) ||
|
joachim99@52
|
918 bIgnoreNumbers && (isdigit( c2 ) || c2=='-' || c2=='.' ))
|
joachim99@52
|
919 c2 = *t2++;
|
joachim99@52
|
920
|
joachim99@52
|
921 #if 0
|
joachim99@52
|
922 switch (ignore_white_space)
|
joachim99@52
|
923 {
|
joachim99@52
|
924 case IGNORE_ALL_SPACE:
|
joachim99@52
|
925 /* For -w, just skip past any white space. */
|
joachim99@52
|
926 while (ISSPACE (c1) && c1 != '\n') c1 = *t1++;
|
joachim99@52
|
927 while (ISSPACE (c2) && c2 != '\n') c2 = *t2++;
|
joachim99@52
|
928 break;
|
joachim99@52
|
929
|
joachim99@52
|
930 case IGNORE_SPACE_CHANGE:
|
joachim99@52
|
931 /* For -b, advance past any sequence of white space in
|
joachim99@52
|
932 line 1 and consider it just one space, or nothing at
|
joachim99@52
|
933 all if it is at the end of the line. */
|
joachim99@52
|
934 if (ISSPACE (c1))
|
joachim99@52
|
935 {
|
joachim99@52
|
936 while (c1 != '\n')
|
joachim99@52
|
937 {
|
joachim99@52
|
938 c1 = *t1++;
|
joachim99@52
|
939 if (! ISSPACE (c1))
|
joachim99@52
|
940 {
|
joachim99@52
|
941 --t1;
|
joachim99@52
|
942 c1 = ' ';
|
joachim99@52
|
943 break;
|
joachim99@52
|
944 }
|
joachim99@52
|
945 }
|
joachim99@52
|
946 }
|
joachim99@52
|
947
|
joachim99@52
|
948 /* Likewise for line 2. */
|
joachim99@52
|
949 if (ISSPACE (c2))
|
joachim99@52
|
950 {
|
joachim99@52
|
951 while (c2 != '\n')
|
joachim99@52
|
952 {
|
joachim99@52
|
953 c2 = *t2++;
|
joachim99@52
|
954 if (! ISSPACE (c2))
|
joachim99@52
|
955 {
|
joachim99@52
|
956 --t2;
|
joachim99@52
|
957 c2 = ' ';
|
joachim99@52
|
958 break;
|
joachim99@52
|
959 }
|
joachim99@52
|
960 }
|
joachim99@52
|
961 }
|
joachim99@52
|
962
|
joachim99@52
|
963 if (c1 != c2)
|
joachim99@52
|
964 {
|
joachim99@52
|
965 /* If we went too far when doing the simple test
|
joachim99@52
|
966 for equality, go back to the first non-white-space
|
joachim99@52
|
967 character in both sides and try again. */
|
joachim99@52
|
968 if (c2 == ' ' && c1 != '\n'
|
joachim99@52
|
969 && (unsigned char const *) s1 + 1 < t1
|
joachim99@52
|
970 && ISSPACE (t1[-2]))
|
joachim99@52
|
971 {
|
joachim99@52
|
972 --t1;
|
joachim99@52
|
973 continue;
|
joachim99@52
|
974 }
|
joachim99@52
|
975 if (c1 == ' ' && c2 != '\n'
|
joachim99@52
|
976 && (unsigned char const *) s2 + 1 < t2
|
joachim99@52
|
977 && ISSPACE (t2[-2]))
|
joachim99@52
|
978 {
|
joachim99@52
|
979 --t2;
|
joachim99@52
|
980 continue;
|
joachim99@52
|
981 }
|
joachim99@52
|
982 }
|
joachim99@52
|
983
|
joachim99@52
|
984 break;
|
joachim99@52
|
985
|
joachim99@52
|
986 case IGNORE_TAB_EXPANSION:
|
joachim99@52
|
987 if ((c1 == ' ' && c2 == '\t')
|
joachim99@52
|
988 || (c1 == '\t' && c2 == ' '))
|
joachim99@52
|
989 {
|
joachim99@52
|
990 size_t column2 = column;
|
joachim99@52
|
991 for (;; c1 = *t1++)
|
joachim99@52
|
992 {
|
joachim99@52
|
993 if (c1 == ' ')
|
joachim99@52
|
994 column++;
|
joachim99@52
|
995 else if (c1 == '\t')
|
joachim99@52
|
996 column += TAB_WIDTH - column % TAB_WIDTH;
|
joachim99@52
|
997 else
|
joachim99@52
|
998 break;
|
joachim99@52
|
999 }
|
joachim99@52
|
1000 for (;; c2 = *t2++)
|
joachim99@52
|
1001 {
|
joachim99@52
|
1002 if (c2 == ' ')
|
joachim99@52
|
1003 column2++;
|
joachim99@52
|
1004 else if (c2 == '\t')
|
joachim99@52
|
1005 column2 += TAB_WIDTH - column2 % TAB_WIDTH;
|
joachim99@52
|
1006 else
|
joachim99@52
|
1007 break;
|
joachim99@52
|
1008 }
|
joachim99@52
|
1009 if (column != column2)
|
joachim99@52
|
1010 return 1;
|
joachim99@52
|
1011 }
|
joachim99@52
|
1012 break;
|
joachim99@52
|
1013
|
joachim99@52
|
1014 case IGNORE_NO_WHITE_SPACE:
|
joachim99@52
|
1015 break;
|
joachim99@52
|
1016 }
|
joachim99@52
|
1017 #endif
|
joachim99@52
|
1018 /* Lowercase all letters if -i is specified. */
|
joachim99@52
|
1019
|
joachim99@52
|
1020 if (ignore_case)
|
joachim99@52
|
1021 {
|
joachim99@52
|
1022 c1 = TOLOWER (c1);
|
joachim99@52
|
1023 c2 = TOLOWER (c2);
|
joachim99@52
|
1024 }
|
joachim99@52
|
1025
|
joachim99@52
|
1026 if (c1 != c2)
|
joachim99@52
|
1027 break;
|
joachim99@52
|
1028 }
|
joachim99@52
|
1029 if (c1 == '\n')
|
joachim99@52
|
1030 return 0;
|
joachim99@52
|
1031
|
joachim99@52
|
1032 column += c1 == '\t' ? TAB_WIDTH - column % TAB_WIDTH : 1;
|
joachim99@52
|
1033 }
|
joachim99@52
|
1034
|
joachim99@52
|
1035 return 1;
|
joachim99@52
|
1036 }
|
joachim99@52
|
1037 } // namespace GnuDiff
|