3 /* Analyze file differences for GNU DIFF.
5 Copyright (C) 1988, 1989, 1992, 1993, 1994, 1995, 1998, 2001, 2002
6 Free Software Foundation, Inc.
8 This file is part of GNU DIFF.
10 GNU DIFF is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2, or (at your option)
15 GNU DIFF is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; see the file COPYING.
22 If not, write to the Free Software Foundation,
23 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
25 /* The basic algorithm is described in:
26 "An O(ND) Difference Algorithm and its Variations", Eugene Myers,
27 Algorithmica Vol. 1 No. 2, 1986, pp. 251-266;
28 see especially section 4.2, which describes the variation used below.
29 Unless the --minimal option is specified, this code uses the TOO_EXPENSIVE
30 heuristic, by Paul Eggert, to limit the cost to O(N**1.5 log N)
31 at the price of producing suboptimal output for large inputs with
34 The basic algorithm was independently discovered as described in:
35 "Algorithms for Approximate String Matching", E. Ukkonen,
36 Information and Control Vol. 64, 1985, pp. 100-118. */
44 static lin
*xvec
, *yvec
; /* Vectors being compared. */
45 static lin
*fdiag
; /* Vector, indexed by diagonal, containing
46 1 + the X coordinate of the point furthest
47 along the given diagonal in the forward
48 search of the edit matrix. */
49 static lin
*bdiag
; /* Vector, indexed by diagonal, containing
50 the X coordinate of the point furthest
51 along the given diagonal in the backward
52 search of the edit matrix. */
53 static lin too_expensive
; /* Edit scripts longer than this are too
54 expensive to compute. */
56 #define SNAKE_LIMIT 20 /* Snakes bigger than this are considered `big'. */
60 lin xmid
, ymid
; /* Midpoints of this partition. */
61 bool lo_minimal
; /* Nonzero if low half will be analyzed minimally. */
62 bool hi_minimal
; /* Likewise for high half. */
65 /* Find the midpoint of the shortest edit script for a specified
66 portion of the two files.
68 Scan from the beginnings of the files, and simultaneously from the ends,
69 doing a breadth-first search through the space of edit-sequence.
70 When the two searches meet, we have found the midpoint of the shortest
73 If FIND_MINIMAL is nonzero, find the minimal edit script regardless
74 of expense. Otherwise, if the search is too expensive, use
75 heuristics to stop the search and report a suboptimal answer.
77 Set PART->(xmid,ymid) to the midpoint (XMID,YMID). The diagonal number
78 XMID - YMID equals the number of inserted lines minus the number
79 of deleted lines (counting only lines before the midpoint).
80 Return the approximate edit cost; this is the total number of
81 lines inserted or deleted (counting only lines before the midpoint),
82 unless a heuristic is used to terminate the search prematurely.
84 Set PART->lo_minimal to true iff the minimal edit script for the
85 left half of the partition is known; similarly for PART->hi_minimal.
87 This function assumes that the first lines of the specified portions
88 of the two files do not match, and likewise that the last lines do not
89 match. The caller must trim matching lines from the beginning and end
90 of the portions it is going to specify.
92 If we return the "wrong" partitions,
93 the worst this can do is cause suboptimal diff output.
94 It cannot cause incorrect diff output. */
97 diag (lin xoff
, lin xlim
, lin yoff
, lin ylim
, bool find_minimal
,
98 struct partition
*part
)
100 lin
*const fd
= fdiag
; /* Give the compiler a chance. */
101 lin
*const bd
= bdiag
; /* Additional help for the compiler. */
102 lin
const *const xv
= xvec
; /* Still more help for the compiler. */
103 lin
const *const yv
= yvec
; /* And more and more . . . */
104 lin
const dmin
= xoff
- ylim
; /* Minimum valid diagonal. */
105 lin
const dmax
= xlim
- yoff
; /* Maximum valid diagonal. */
106 lin
const fmid
= xoff
- yoff
; /* Center diagonal of top-down search. */
107 lin
const bmid
= xlim
- ylim
; /* Center diagonal of bottom-up search. */
108 lin fmin
= fmid
, fmax
= fmid
; /* Limits of top-down search. */
109 lin bmin
= bmid
, bmax
= bmid
; /* Limits of bottom-up search. */
111 bool odd
= (fmid
- bmid
) & 1; /* True if southeast corner is on an odd
112 diagonal with respect to the northwest. */
119 lin d
; /* Active diagonal. */
122 /* Extend the top-down search by an edit step in each diagonal. */
123 fmin
> dmin
? fd
[--fmin
- 1] = -1 : ++fmin
;
124 fmax
< dmax
? fd
[++fmax
+ 1] = -1 : --fmax
;
125 for (d
= fmax
; d
>= fmin
; d
-= 2)
127 lin x
, y
, oldx
, tlo
= fd
[d
- 1], thi
= fd
[d
+ 1];
135 while (x
< xlim
&& y
< ylim
&& xv
[x
] == yv
[y
])
137 if (x
- oldx
> SNAKE_LIMIT
)
140 if (odd
&& bmin
<= d
&& d
<= bmax
&& bd
[d
] <= x
)
144 part
->lo_minimal
= part
->hi_minimal
= 1;
149 /* Similarly extend the bottom-up search. */
150 bmin
> dmin
? bd
[--bmin
- 1] = LIN_MAX
: ++bmin
;
151 bmax
< dmax
? bd
[++bmax
+ 1] = LIN_MAX
: --bmax
;
152 for (d
= bmax
; d
>= bmin
; d
-= 2)
154 lin x
, y
, oldx
, tlo
= bd
[d
- 1], thi
= bd
[d
+ 1];
162 while (x
> xoff
&& y
> yoff
&& xv
[x
- 1] == yv
[y
- 1])
164 if (oldx
- x
> SNAKE_LIMIT
)
167 if (!odd
&& fmin
<= d
&& d
<= fmax
&& x
<= fd
[d
])
171 part
->lo_minimal
= part
->hi_minimal
= 1;
179 /* Heuristic: check occasionally for a diagonal that has made
180 lots of progress compared with the edit distance.
181 If we have any such, find the one that has made the most
182 progress and return it as if it had succeeded.
184 With this heuristic, for files with a constant small density
185 of changes, the algorithm is linear in the file size. */
187 if (200 < c
&& big_snake
&& speed_large_files
)
192 for (d
= fmax
; d
>= fmin
; d
-= 2)
197 lin v
= (x
- xoff
) * 2 - dd
;
198 if (v
> 12 * (c
+ (dd
< 0 ? -dd
: dd
)))
201 && xoff
+ SNAKE_LIMIT
<= x
&& x
< xlim
202 && yoff
+ SNAKE_LIMIT
<= y
&& y
< ylim
)
204 /* We have a good enough best diagonal;
205 now insist that it end with a significant snake. */
208 for (k
= 1; xv
[x
- k
] == yv
[y
- k
]; k
++)
209 if (k
== SNAKE_LIMIT
)
221 part
->lo_minimal
= 1;
222 part
->hi_minimal
= 0;
227 for (d
= bmax
; d
>= bmin
; d
-= 2)
232 lin v
= (xlim
- x
) * 2 + dd
;
233 if (v
> 12 * (c
+ (dd
< 0 ? -dd
: dd
)))
236 && xoff
< x
&& x
<= xlim
- SNAKE_LIMIT
237 && yoff
< y
&& y
<= ylim
- SNAKE_LIMIT
)
239 /* We have a good enough best diagonal;
240 now insist that it end with a significant snake. */
243 for (k
= 0; xv
[x
+ k
] == yv
[y
+ k
]; k
++)
244 if (k
== SNAKE_LIMIT
- 1)
256 part
->lo_minimal
= 0;
257 part
->hi_minimal
= 1;
262 /* Heuristic: if we've gone well beyond the call of duty,
263 give up and report halfway between our best results so far. */
264 if (c
>= too_expensive
)
269 fxbest
= bxbest
= 0; /* Pacify `gcc -Wall'. */
271 /* Find forward diagonal that maximizes X + Y. */
273 for (d
= fmax
; d
>= fmin
; d
-= 2)
275 lin x
= MIN (fd
[d
], xlim
);
278 x
= ylim
+ d
, y
= ylim
;
286 /* Find backward diagonal that minimizes X + Y. */
288 for (d
= bmax
; d
>= bmin
; d
-= 2)
290 lin x
= MAX (xoff
, bd
[d
]);
293 x
= yoff
+ d
, y
= yoff
;
301 /* Use the better of the two diagonals. */
302 if ((xlim
+ ylim
) - bxybest
< fxybest
- (xoff
+ yoff
))
305 part
->ymid
= fxybest
- fxbest
;
306 part
->lo_minimal
= 1;
307 part
->hi_minimal
= 0;
312 part
->ymid
= bxybest
- bxbest
;
313 part
->lo_minimal
= 0;
314 part
->hi_minimal
= 1;
321 /* Compare in detail contiguous subsequences of the two files
322 which are known, as a whole, to match each other.
324 The results are recorded in the vectors files[N].changed, by
325 storing 1 in the element for each line that is an insertion or deletion.
327 The subsequence of file 0 is [XOFF, XLIM) and likewise for file 1.
329 Note that XLIM, YLIM are exclusive bounds.
330 All line numbers are origin-0 and discarded lines are not counted.
332 If FIND_MINIMAL, find a minimal difference no matter how
336 compareseq (lin xoff
, lin xlim
, lin yoff
, lin ylim
, bool find_minimal
)
338 lin
* const xv
= xvec
; /* Help the compiler. */
339 lin
* const yv
= yvec
;
341 /* Slide down the bottom initial diagonal. */
342 while (xoff
< xlim
&& yoff
< ylim
&& xv
[xoff
] == yv
[yoff
])
344 /* Slide up the top initial diagonal. */
345 while (xlim
> xoff
&& ylim
> yoff
&& xv
[xlim
- 1] == yv
[ylim
- 1])
348 /* Handle simple cases. */
351 files
[1].changed
[files
[1].realindexes
[yoff
++]] = 1;
352 else if (yoff
== ylim
)
354 files
[0].changed
[files
[0].realindexes
[xoff
++]] = 1;
358 struct partition part
;
360 /* Find a point of correspondence in the middle of the files. */
362 c
= diag (xoff
, xlim
, yoff
, ylim
, find_minimal
, &part
);
366 /* This should be impossible, because it implies that
367 one of the two subsequences is empty,
368 and that case was handled above without calling `diag'.
369 Let's verify that this is true. */
372 /* The two subsequences differ by a single insert or delete;
373 record it and we are done. */
374 if (part
.xmid
- part
.ymid
< xoff
- yoff
)
375 files
[1].changed
[files
[1].realindexes
[part
.ymid
- 1]] = 1;
377 files
[0].changed
[files
[0].realindexes
[part
.xmid
]] = 1;
382 /* Use the partitions to split this problem into subproblems. */
383 compareseq (xoff
, part
.xmid
, yoff
, part
.ymid
, part
.lo_minimal
);
384 compareseq (part
.xmid
, xlim
, part
.ymid
, ylim
, part
.hi_minimal
);
389 /* Discard lines from one file that have no matches in the other file.
391 A line which is discarded will not be considered by the actual
392 comparison algorithm; it will be as if that line were not in the file.
393 The file's `realindexes' table maps virtual line numbers
394 (which don't count the discarded lines) into real line numbers;
395 this is how the actual comparison algorithm produces results
396 that are comprehensible when the discarded lines are counted.
398 When we discard a line, we also mark it as a deletion or insertion
399 so that it will be printed in the output. */
402 discard_confusing_lines (struct file_data filevec
[])
410 /* Allocate our results. */
411 p
= xmalloc ((filevec
[0].buffered_lines
+ filevec
[1].buffered_lines
)
413 for (f
= 0; f
< 2; f
++)
415 filevec
[f
].undiscarded
= p
; p
+= filevec
[f
].buffered_lines
;
416 filevec
[f
].realindexes
= p
; p
+= filevec
[f
].buffered_lines
;
419 /* Set up equiv_count[F][I] as the number of lines in file F
420 that fall in equivalence class I. */
422 p
= zalloc (filevec
[0].equiv_max
* (2 * sizeof *p
));
424 equiv_count
[1] = p
+ filevec
[0].equiv_max
;
426 for (i
= 0; i
< filevec
[0].buffered_lines
; ++i
)
427 ++equiv_count
[0][filevec
[0].equivs
[i
]];
428 for (i
= 0; i
< filevec
[1].buffered_lines
; ++i
)
429 ++equiv_count
[1][filevec
[1].equivs
[i
]];
431 /* Set up tables of which lines are going to be discarded. */
433 discarded
[0] = zalloc (filevec
[0].buffered_lines
434 + filevec
[1].buffered_lines
);
435 discarded
[1] = discarded
[0] + filevec
[0].buffered_lines
;
437 /* Mark to be discarded each line that matches no line of the other file.
438 If a line matches many lines, mark it as provisionally discardable. */
440 for (f
= 0; f
< 2; f
++)
442 size_t end
= filevec
[f
].buffered_lines
;
443 char *discards
= discarded
[f
];
444 lin
*counts
= equiv_count
[1 - f
];
445 lin
*equivs
= filevec
[f
].equivs
;
447 size_t tem
= end
/ 64;
449 /* Multiply MANY by approximate square root of number of lines.
450 That is the threshold for provisionally discardable lines. */
451 while ((tem
= tem
>> 2) > 0)
454 for (i
= 0; i
< end
; i
++)
459 nmatch
= counts
[equivs
[i
]];
462 else if (nmatch
> many
)
467 /* Don't really discard the provisional lines except when they occur
468 in a run of discardables, with nonprovisionals at the beginning
471 for (f
= 0; f
< 2; f
++)
473 lin end
= filevec
[f
].buffered_lines
;
474 register char *discards
= discarded
[f
];
476 for (i
= 0; i
< end
; i
++)
478 /* Cancel provisional discards not in middle of run of discards. */
479 if (discards
[i
] == 2)
481 else if (discards
[i
] != 0)
483 /* We have found a nonprovisional discard. */
488 /* Find end of this run of discardable lines.
489 Count how many are provisionally discardable. */
490 for (j
= i
; j
< end
; j
++)
492 if (discards
[j
] == 0)
494 if (discards
[j
] == 2)
498 /* Cancel provisional discards at end, and shrink the run. */
499 while (j
> i
&& discards
[j
- 1] == 2)
500 discards
[--j
] = 0, --provisional
;
502 /* Now we have the length of a run of discardable lines
503 whose first and last are not provisional. */
506 /* If 1/4 of the lines in the run are provisional,
507 cancel discarding of all provisional lines in the run. */
508 if (provisional
* 4 > length
)
511 if (discards
[--j
] == 2)
518 lin tem
= length
>> 2;
520 /* MINIMUM is approximate square root of LENGTH/4.
521 A subrun of two or more provisionals can stand
522 when LENGTH is at least 16.
523 A subrun of 4 or more can stand when LENGTH >= 64. */
524 while (0 < (tem
>>= 2))
528 /* Cancel any subrun of MINIMUM or more provisionals
529 within the larger run. */
530 for (j
= 0, consec
= 0; j
< length
; j
++)
531 if (discards
[i
+ j
] != 2)
533 else if (minimum
== ++consec
)
534 /* Back up to start of subrun, to cancel it all. */
536 else if (minimum
< consec
)
539 /* Scan from beginning of run
540 until we find 3 or more nonprovisionals in a row
541 or until the first nonprovisional at least 8 lines in.
542 Until that point, cancel any provisionals. */
543 for (j
= 0, consec
= 0; j
< length
; j
++)
545 if (j
>= 8 && discards
[i
+ j
] == 1)
547 if (discards
[i
+ j
] == 2)
548 consec
= 0, discards
[i
+ j
] = 0;
549 else if (discards
[i
+ j
] == 0)
557 /* I advances to the last line of the run. */
560 /* Same thing, from end. */
561 for (j
= 0, consec
= 0; j
< length
; j
++)
563 if (j
>= 8 && discards
[i
- j
] == 1)
565 if (discards
[i
- j
] == 2)
566 consec
= 0, discards
[i
- j
] = 0;
567 else if (discards
[i
- j
] == 0)
579 /* Actually discard the lines. */
580 for (f
= 0; f
< 2; f
++)
582 char *discards
= discarded
[f
];
583 lin end
= filevec
[f
].buffered_lines
;
585 for (i
= 0; i
< end
; ++i
)
586 if (minimal
|| discards
[i
] == 0)
588 filevec
[f
].undiscarded
[j
] = filevec
[f
].equivs
[i
];
589 filevec
[f
].realindexes
[j
++] = i
;
592 filevec
[f
].changed
[i
] = 1;
593 filevec
[f
].nondiscarded_lines
= j
;
597 free (equiv_count
[0]);
600 /* Adjust inserts/deletes of identical lines to join changes
603 We do something when a run of changed lines include a
604 line at one end and have an excluded, identical line at the other.
605 We are free to choose which identical line is included.
606 `compareseq' usually chooses the one at the beginning,
607 but usually it is cleaner to consider the following identical line
608 to be the "change". */
611 shift_boundaries (struct file_data filevec
[])
615 for (f
= 0; f
< 2; f
++)
617 bool *changed
= filevec
[f
].changed
;
618 bool const *other_changed
= filevec
[1 - f
].changed
;
619 lin
const *equivs
= filevec
[f
].equivs
;
622 lin i_end
= filevec
[f
].buffered_lines
;
626 lin runlength
, start
, corresponding
;
628 /* Scan forwards to find beginning of another run of changes.
629 Also keep track of the corresponding point in the other file. */
631 while (i
< i_end
&& !changed
[i
])
633 while (other_changed
[j
++])
643 /* Find the end of this run of changes. */
647 while (other_changed
[j
])
652 /* Record the length of this run of changes, so that
653 we can later determine whether the run has grown. */
654 runlength
= i
- start
;
656 /* Move the changed region back, so long as the
657 previous unchanged line matches the last changed one.
658 This merges with previous changed regions. */
660 while (start
&& equivs
[start
- 1] == equivs
[i
- 1])
662 changed
[--start
] = 1;
664 while (changed
[start
- 1])
666 while (other_changed
[--j
])
670 /* Set CORRESPONDING to the end of the changed run, at the last
671 point where it corresponds to a changed run in the other file.
672 CORRESPONDING == I_END means no such point has been found. */
673 corresponding
= other_changed
[j
- 1] ? i
: i_end
;
675 /* Move the changed region forward, so long as the
676 first changed line matches the following unchanged one.
677 This merges with following changed regions.
678 Do this second, so that if there are no merges,
679 the changed region is moved forward as far as possible. */
681 while (i
!= i_end
&& equivs
[start
] == equivs
[i
])
683 changed
[start
++] = 0;
687 while (other_changed
[++j
])
691 while (runlength
!= i
- start
);
693 /* If possible, move the fully-merged run of changes
694 back to a corresponding run in the other file. */
696 while (corresponding
< i
)
698 changed
[--start
] = 1;
700 while (other_changed
[--j
])
707 /* Cons an additional entry onto the front of an edit script OLD.
708 LINE0 and LINE1 are the first affected lines in the two files (origin 0).
709 DELETED is the number of lines deleted here from file 0.
710 INSERTED is the number of lines inserted here in file 1.
712 If DELETED is 0 then LINE0 is the number of the line before
713 which the insertion was done; vice versa for INSERTED and LINE1. */
715 static struct change
*
716 add_change (lin line0
, lin line1
, lin deleted
, lin inserted
,
719 struct change
*new = xmalloc (sizeof *new);
723 new->inserted
= inserted
;
724 new->deleted
= deleted
;
729 /* Scan the tables of which lines are inserted and deleted,
730 producing an edit script in reverse order. */
732 static struct change
*
733 build_reverse_script (struct file_data
const filevec
[])
735 struct change
*script
= 0;
736 bool *changed0
= filevec
[0].changed
;
737 bool *changed1
= filevec
[1].changed
;
738 lin len0
= filevec
[0].buffered_lines
;
739 lin len1
= filevec
[1].buffered_lines
;
741 /* Note that changedN[len0] does exist, and is 0. */
745 while (i0
< len0
|| i1
< len1
)
747 if (changed0
[i0
] | changed1
[i1
])
749 lin line0
= i0
, line1
= i1
;
751 /* Find # lines changed here in each file. */
752 while (changed0
[i0
]) ++i0
;
753 while (changed1
[i1
]) ++i1
;
755 /* Record this change. */
756 script
= add_change (line0
, line1
, i0
- line0
, i1
- line1
, script
);
759 /* We have reached lines in the two files that match each other. */
766 /* Scan the tables of which lines are inserted and deleted,
767 producing an edit script in forward order. */
769 static struct change
*
770 build_script (struct file_data
const filevec
[])
772 struct change
*script
= 0;
773 bool *changed0
= filevec
[0].changed
;
774 bool *changed1
= filevec
[1].changed
;
775 lin i0
= filevec
[0].buffered_lines
, i1
= filevec
[1].buffered_lines
;
777 /* Note that changedN[-1] does exist, and is 0. */
779 while (i0
>= 0 || i1
>= 0)
781 if (changed0
[i0
- 1] | changed1
[i1
- 1])
783 lin line0
= i0
, line1
= i1
;
785 /* Find # lines changed here in each file. */
786 while (changed0
[i0
- 1]) --i0
;
787 while (changed1
[i1
- 1]) --i1
;
789 /* Record this change. */
790 script
= add_change (i0
, i1
, line0
- i0
, line1
- i1
, script
);
793 /* We have reached lines in the two files that match each other. */
800 /* If CHANGES, briefly report that two files differed.
801 Return 2 if trouble, CHANGES otherwise. */
803 briefly_report (int changes
, struct file_data
const filevec
[])
807 char const *label0
= file_label
[0] ? file_label
[0] : filevec
[0].name
;
808 char const *label1
= file_label
[1] ? file_label
[1] : filevec
[1].name
;
811 message ("Files %s and %s differ\n", label0
, label1
);
814 message ("Binary files %s and %s differ\n", label0
, label1
);
822 /* Report the differences of two files. */
824 diff_2_files (struct comparison
*cmp
)
828 struct change
*e
, *p
;
829 struct change
*script
;
833 /* If we have detected that either file is binary,
834 compare the two files as binary. This can happen
835 only when the first chunk is read.
836 Also, --brief without any --ignore-* options means
837 we can speed things up by treating the files as binary. */
839 if (read_files (cmp
->file
, files_can_be_treated_as_binary
))
841 /* Files with different lengths must be different. */
842 if (cmp
->file
[0].stat
.st_size
!= cmp
->file
[1].stat
.st_size
843 && (cmp
->file
[0].desc
< 0 || S_ISREG (cmp
->file
[0].stat
.st_mode
))
844 && (cmp
->file
[1].desc
< 0 || S_ISREG (cmp
->file
[1].stat
.st_mode
)))
847 /* Standard input equals itself. */
848 else if (cmp
->file
[0].desc
== cmp
->file
[1].desc
)
852 /* Scan both files, a buffer at a time, looking for a difference. */
854 /* Allocate same-sized buffers for both files. */
855 size_t lcm_max
= PTRDIFF_MAX
- 1;
857 buffer_lcm (sizeof (word
),
858 buffer_lcm (STAT_BLOCKSIZE (cmp
->file
[0].stat
),
859 STAT_BLOCKSIZE (cmp
->file
[1].stat
),
862 for (f
= 0; f
< 2; f
++)
863 cmp
->file
[f
].buffer
= xrealloc (cmp
->file
[f
].buffer
, buffer_size
);
865 for (;; cmp
->file
[0].buffered
= cmp
->file
[1].buffered
= 0)
867 /* Read a buffer's worth from both files. */
868 for (f
= 0; f
< 2; f
++)
869 if (0 <= cmp
->file
[f
].desc
)
870 file_block_read (&cmp
->file
[f
],
871 buffer_size
- cmp
->file
[f
].buffered
);
873 /* If the buffers differ, the files differ. */
874 if (cmp
->file
[0].buffered
!= cmp
->file
[1].buffered
875 || memcmp (cmp
->file
[0].buffer
,
877 cmp
->file
[0].buffered
))
883 /* If we reach end of file, the files are the same. */
884 if (cmp
->file
[0].buffered
!= buffer_size
)
892 changes
= briefly_report (changes
, cmp
->file
);
896 /* Allocate vectors for the results of comparison:
897 a flag for each line of each file, saying whether that line
898 is an insertion or deletion.
899 Allocate an extra element, always 0, at each end of each vector. */
901 size_t s
= cmp
->file
[0].buffered_lines
+ cmp
->file
[1].buffered_lines
+ 4;
902 bool *flag_space
= zalloc (s
* sizeof *flag_space
);
903 cmp
->file
[0].changed
= flag_space
+ 1;
904 cmp
->file
[1].changed
= flag_space
+ cmp
->file
[0].buffered_lines
+ 3;
906 /* Some lines are obviously insertions or deletions
907 because they don't match anything. Detect them now, and
908 avoid even thinking about them in the main comparison algorithm. */
910 discard_confusing_lines (cmp
->file
);
912 /* Now do the main comparison algorithm, considering just the
913 undiscarded lines. */
915 xvec
= cmp
->file
[0].undiscarded
;
916 yvec
= cmp
->file
[1].undiscarded
;
917 diags
= (cmp
->file
[0].nondiscarded_lines
918 + cmp
->file
[1].nondiscarded_lines
+ 3);
919 fdiag
= xmalloc (diags
* (2 * sizeof *fdiag
));
920 bdiag
= fdiag
+ diags
;
921 fdiag
+= cmp
->file
[1].nondiscarded_lines
+ 1;
922 bdiag
+= cmp
->file
[1].nondiscarded_lines
+ 1;
924 /* Set TOO_EXPENSIVE to be approximate square root of input size,
925 bounded below by 256. */
927 for (; diags
!= 0; diags
>>= 2)
929 too_expensive
= MAX (256, too_expensive
);
931 files
[0] = cmp
->file
[0];
932 files
[1] = cmp
->file
[1];
934 compareseq (0, cmp
->file
[0].nondiscarded_lines
,
935 0, cmp
->file
[1].nondiscarded_lines
, minimal
);
937 free (fdiag
- (cmp
->file
[1].nondiscarded_lines
+ 1));
939 /* Modify the results slightly to make them prettier
940 in cases where that can validly be done. */
942 shift_boundaries (cmp
->file
);
944 /* Get the results of comparison in the form of a chain
945 of `struct change's -- an edit script. */
947 if (output_style
== OUTPUT_ED
)
948 script
= build_reverse_script (cmp
->file
);
950 script
= build_script (cmp
->file
);
952 /* Set CHANGES if we had any diffs.
953 If some changes are ignored, we must scan the script to decide. */
954 if (ignore_blank_lines
|| ignore_regexp
.fastmap
)
956 struct change
*next
= script
;
959 while (next
&& changes
== 0)
961 struct change
*this, *end
;
962 lin first0
, last0
, first1
, last1
;
964 /* Find a set of changes that belong together. */
966 end
= find_change (next
);
968 /* Disconnect them from the rest of the changes, making them
969 a hunk, and remember the rest for next iteration. */
973 /* Determine whether this hunk is really a difference. */
974 if (analyze_hunk (this, &first0
, &last0
, &first1
, &last1
))
977 /* Reconnect the script so it will all be freed properly. */
982 changes
= (script
!= 0);
985 changes
= briefly_report (changes
, cmp
->file
);
988 if (changes
| !no_diff_means_no_output
)
990 /* Record info for starting up output,
991 to be used if and when we have some output to print. */
992 setup_output (file_label
[0] ? file_label
[0] : cmp
->file
[0].name
,
993 file_label
[1] ? file_label
[1] : cmp
->file
[1].name
,
996 switch (output_style
)
999 print_context_script (script
, 0);
1002 case OUTPUT_UNIFIED
:
1003 print_context_script (script
, 1);
1007 print_ed_script (script
);
1010 case OUTPUT_FORWARD_ED
:
1011 pr_forward_ed_script (script
);
1015 print_rcs_script (script
);
1019 print_normal_script (script
);
1023 print_ifdef_script (script
);
1027 print_sdiff_script (script
);
1038 free (cmp
->file
[0].undiscarded
);
1042 for (f
= 0; f
< 2; f
++)
1044 free (cmp
->file
[f
].equivs
);
1045 free (cmp
->file
[f
].linbuf
+ cmp
->file
[f
].linbuf_base
);
1048 for (e
= script
; e
; e
= p
)
1054 if (! ROBUST_OUTPUT_STYLE (output_style
))
1055 for (f
= 0; f
< 2; ++f
)
1056 if (cmp
->file
[f
].missing_newline
)
1058 error (0, 0, "%s: %s\n",
1059 file_label
[f
] ? file_label
[f
] : cmp
->file
[f
].name
,
1060 _("No newline at end of file"));
1065 if (cmp
->file
[0].buffer
!= cmp
->file
[1].buffer
)
1066 free (cmp
->file
[0].buffer
);
1067 free (cmp
->file
[1].buffer
);