1 /* Analyze file differences for GNU DIFF.
2 Copyright (C) 1988, 1989, 1992, 1993 Free Software Foundation, Inc.
4 This file is part of GNU DIFF.
6 GNU DIFF is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU DIFF is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU DIFF; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
20 /* The basic algorithm is described in:
21 "An O(ND) Difference Algorithm and its Variations", Eugene Myers,
22 Algorithmica Vol. 1 No. 2, 1986, pp. 251-266;
23 see especially section 4.2, which describes the variation used below.
24 Unless the --minimal option is specified, this code uses the TOO_EXPENSIVE
25 heuristic, by Paul Eggert, to limit the cost to O(N**1.5 log N)
26 at the price of producing suboptimal output for large inputs with
29 The basic algorithm was independently discovered as described in:
30 "Algorithms for Approximate String Matching", E. Ukkonen,
31 Information and Control Vol. 64, 1985, pp. 100-118. */
36 extern int no_discards;
38 static int *xvec, *yvec; /* Vectors being compared. */
39 static int *fdiag; /* Vector, indexed by diagonal, containing
40 1 + the X coordinate of the point furthest
41 along the given diagonal in the forward
42 search of the edit matrix. */
43 static int *bdiag; /* Vector, indexed by diagonal, containing
44 the X coordinate of the point furthest
45 along the given diagonal in the backward
46 search of the edit matrix. */
47 static int too_expensive; /* Edit scripts longer than this are too
48 expensive to compute. */
50 #define SNAKE_LIMIT 20 /* Snakes bigger than this are considered `big'. */
54 int xmid, ymid; /* Midpoints of this partition. */
55 int lo_minimal; /* Nonzero if low half will be analyzed minimally. */
56 int hi_minimal; /* Likewise for high half. */
59 static int diag PARAMS((int, int, int, int, int, struct partition *));
60 static struct change *add_change PARAMS((int, int, int, int, struct change *));
61 static struct change *build_reverse_script PARAMS((struct file_data const[]));
62 static struct change *build_script PARAMS((struct file_data const[]));
63 static void briefly_report PARAMS((int, struct file_data const[]));
64 static void compareseq PARAMS((int, int, int, int, int));
65 static void discard_confusing_lines PARAMS((struct file_data[]));
66 static void shift_boundaries PARAMS((struct file_data[]));
68 /* Find the midpoint of the shortest edit script for a specified
69 portion of the two files.
71 Scan from the beginnings of the files, and simultaneously from the ends,
72 doing a breadth-first search through the space of edit-sequence.
73 When the two searches meet, we have found the midpoint of the shortest
76 If MINIMAL is nonzero, find the minimal edit script regardless
77 of expense. Otherwise, if the search is too expensive, use
78 heuristics to stop the search and report a suboptimal answer.
80 Set PART->(XMID,YMID) to the midpoint (XMID,YMID). The diagonal number
81 XMID - YMID equals the number of inserted lines minus the number
82 of deleted lines (counting only lines before the midpoint).
83 Return the approximate edit cost; this is the total number of
84 lines inserted or deleted (counting only lines before the midpoint),
85 unless a heuristic is used to terminate the search prematurely.
87 Set PART->LEFT_MINIMAL to nonzero iff the minimal edit script for the
88 left half of the partition is known; similarly for PART->RIGHT_MINIMAL.
90 This function assumes that the first lines of the specified portions
91 of the two files do not match, and likewise that the last lines do not
92 match. The caller must trim matching lines from the beginning and end
93 of the portions it is going to specify.
95 If we return the "wrong" partitions,
96 the worst this can do is cause suboptimal diff output.
97 It cannot cause incorrect diff output. */
100 diag (xoff, xlim, yoff, ylim, minimal, part)
101 int xoff, xlim, yoff, ylim, minimal;
102 struct partition *part;
104 int *const fd = fdiag; /* Give the compiler a chance. */
105 int *const bd = bdiag; /* Additional help for the compiler. */
106 int const *const xv = xvec; /* Still more help for the compiler. */
107 int const *const yv = yvec; /* And more and more . . . */
108 int const dmin = xoff - ylim; /* Minimum valid diagonal. */
109 int const dmax = xlim - yoff; /* Maximum valid diagonal. */
110 int const fmid = xoff - yoff; /* Center diagonal of top-down search. */
111 int const bmid = xlim - ylim; /* Center diagonal of bottom-up search. */
112 int fmin = fmid, fmax = fmid; /* Limits of top-down search. */
113 int bmin = bmid, bmax = bmid; /* Limits of bottom-up search. */
115 int odd = (fmid - bmid) & 1; /* True if southeast corner is on an odd
116 diagonal with respect to the northwest. */
123 int d; /* Active diagonal. */
126 /* Extend the top-down search by an edit step in each diagonal. */
127 fmin > dmin ? fd[--fmin - 1] = -1 : ++fmin;
128 fmax < dmax ? fd[++fmax + 1] = -1 : --fmax;
129 for (d = fmax; d >= fmin; d -= 2)
131 int x, y, oldx, tlo = fd[d - 1], thi = fd[d + 1];
139 while (x < xlim && y < ylim && xv[x] == yv[y])
141 if (x - oldx > SNAKE_LIMIT)
144 if (odd && bmin <= d && d <= bmax && bd[d] <= x)
148 part->lo_minimal = part->hi_minimal = 1;
153 /* Similarly extend the bottom-up search. */
154 bmin > dmin ? bd[--bmin - 1] = INT_MAX : ++bmin;
155 bmax < dmax ? bd[++bmax + 1] = INT_MAX : --bmax;
156 for (d = bmax; d >= bmin; d -= 2)
158 int x, y, oldx, tlo = bd[d - 1], thi = bd[d + 1];
166 while (x > xoff && y > yoff && xv[x - 1] == yv[y - 1])
168 if (oldx - x > SNAKE_LIMIT)
171 if (!odd && fmin <= d && d <= fmax && x <= fd[d])
175 part->lo_minimal = part->hi_minimal = 1;
183 /* Heuristic: check occasionally for a diagonal that has made
184 lots of progress compared with the edit distance.
185 If we have any such, find the one that has made the most
186 progress and return it as if it had succeeded.
188 With this heuristic, for files with a constant small density
189 of changes, the algorithm is linear in the file size. */
191 if (c > 200 && big_snake && heuristic)
196 for (d = fmax; d >= fmin; d -= 2)
201 int v = (x - xoff) * 2 - dd;
202 if (v > 12 * (c + (dd < 0 ? -dd : dd)))
205 && xoff + SNAKE_LIMIT <= x && x < xlim
206 && yoff + SNAKE_LIMIT <= y && y < ylim)
208 /* We have a good enough best diagonal;
209 now insist that it end with a significant snake. */
212 for (k = 1; xv[x - k] == yv[y - k]; k++)
213 if (k == SNAKE_LIMIT)
225 part->lo_minimal = 1;
226 part->hi_minimal = 0;
231 for (d = bmax; d >= bmin; d -= 2)
236 int v = (xlim - x) * 2 + dd;
237 if (v > 12 * (c + (dd < 0 ? -dd : dd)))
240 && xoff < x && x <= xlim - SNAKE_LIMIT
241 && yoff < y && y <= ylim - SNAKE_LIMIT)
243 /* We have a good enough best diagonal;
244 now insist that it end with a significant snake. */
247 for (k = 0; xv[x + k] == yv[y + k]; k++)
248 if (k == SNAKE_LIMIT - 1)
260 part->lo_minimal = 0;
261 part->hi_minimal = 1;
266 /* Heuristic: if we've gone well beyond the call of duty,
267 give up and report halfway between our best results so far. */
268 if (c >= too_expensive)
273 fxbest = bxbest = 0; /* Pacify `gcc -Wall'. */
275 /* Find forward diagonal that maximizes X + Y. */
277 for (d = fmax; d >= fmin; d -= 2)
279 int x = min (fd[d], xlim);
282 x = ylim + d, y = ylim;
290 /* Find backward diagonal that minimizes X + Y. */
292 for (d = bmax; d >= bmin; d -= 2)
294 int x = max (xoff, bd[d]);
297 x = yoff + d, y = yoff;
305 /* Use the better of the two diagonals. */
306 if ((xlim + ylim) - bxybest < fxybest - (xoff + yoff))
309 part->ymid = fxybest - fxbest;
310 part->lo_minimal = 1;
311 part->hi_minimal = 0;
316 part->ymid = bxybest - bxbest;
317 part->lo_minimal = 0;
318 part->hi_minimal = 1;
325 /* Compare in detail contiguous subsequences of the two files
326 which are known, as a whole, to match each other.
328 The results are recorded in the vectors files[N].changed_flag, by
329 storing a 1 in the element for each line that is an insertion or deletion.
331 The subsequence of file 0 is [XOFF, XLIM) and likewise for file 1.
333 Note that XLIM, YLIM are exclusive bounds.
334 All line numbers are origin-0 and discarded lines are not counted.
336 If MINIMAL is nonzero, find a minimal difference no matter how
340 compareseq (xoff, xlim, yoff, ylim, minimal)
341 int xoff, xlim, yoff, ylim, minimal;
343 int * const xv = xvec; /* Help the compiler. */
344 int * const yv = yvec;
346 /* Slide down the bottom initial diagonal. */
347 while (xoff < xlim && yoff < ylim && xv[xoff] == yv[yoff])
349 /* Slide up the top initial diagonal. */
350 while (xlim > xoff && ylim > yoff && xv[xlim - 1] == yv[ylim - 1])
353 /* Handle simple cases. */
356 files[1].changed_flag[files[1].realindexes[yoff++]] = 1;
357 else if (yoff == ylim)
359 files[0].changed_flag[files[0].realindexes[xoff++]] = 1;
363 struct partition part;
365 /* Find a point of correspondence in the middle of the files. */
367 c = diag (xoff, xlim, yoff, ylim, minimal, &part);
371 /* This should be impossible, because it implies that
372 one of the two subsequences is empty,
373 and that case was handled above without calling `diag'.
374 Let's verify that this is true. */
377 /* The two subsequences differ by a single insert or delete;
378 record it and we are done. */
379 if (part.xmid - part.ymid < xoff - yoff)
380 files[1].changed_flag[files[1].realindexes[part.ymid - 1]] = 1;
382 files[0].changed_flag[files[0].realindexes[part.xmid]] = 1;
387 /* Use the partitions to split this problem into subproblems. */
388 compareseq (xoff, part.xmid, yoff, part.ymid, part.lo_minimal);
389 compareseq (part.xmid, xlim, part.ymid, ylim, part.hi_minimal);
394 /* Discard lines from one file that have no matches in the other file.
396 A line which is discarded will not be considered by the actual
397 comparison algorithm; it will be as if that line were not in the file.
398 The file's `realindexes' table maps virtual line numbers
399 (which don't count the discarded lines) into real line numbers;
400 this is how the actual comparison algorithm produces results
401 that are comprehensible when the discarded lines are counted.
403 When we discard a line, we also mark it as a deletion or insertion
404 so that it will be printed in the output. */
407 discard_confusing_lines (filevec)
408 struct file_data filevec[];
415 /* Allocate our results. */
416 p = (int *) xmalloc ((filevec[0].buffered_lines + filevec[1].buffered_lines)
417 * (2 * sizeof (int)));
418 for (f = 0; f < 2; f++)
420 filevec[f].undiscarded = p; p += filevec[f].buffered_lines;
421 filevec[f].realindexes = p; p += filevec[f].buffered_lines;
424 /* Set up equiv_count[F][I] as the number of lines in file F
425 that fall in equivalence class I. */
427 p = (int *) xmalloc (filevec[0].equiv_max * (2 * sizeof (int)));
429 equiv_count[1] = p + filevec[0].equiv_max;
430 bzero (p, filevec[0].equiv_max * (2 * sizeof (int)));
432 for (i = 0; i < filevec[0].buffered_lines; ++i)
433 ++equiv_count[0][filevec[0].equivs[i]];
434 for (i = 0; i < filevec[1].buffered_lines; ++i)
435 ++equiv_count[1][filevec[1].equivs[i]];
437 /* Set up tables of which lines are going to be discarded. */
439 discarded[0] = xmalloc (sizeof (char)
440 * (filevec[0].buffered_lines
441 + filevec[1].buffered_lines));
442 discarded[1] = discarded[0] + filevec[0].buffered_lines;
443 bzero (discarded[0], sizeof (char) * (filevec[0].buffered_lines
444 + filevec[1].buffered_lines));
446 /* Mark to be discarded each line that matches no line of the other file.
447 If a line matches many lines, mark it as provisionally discardable. */
449 for (f = 0; f < 2; f++)
451 unsigned int end = filevec[f].buffered_lines;
452 char *discards = discarded[f];
453 int *counts = equiv_count[1 - f];
454 int *equivs = filevec[f].equivs;
455 unsigned int many = 5;
456 unsigned int tem = end / 64;
458 /* Multiply MANY by approximate square root of number of lines.
459 That is the threshold for provisionally discardable lines. */
460 while ((tem = tem >> 2) > 0)
463 for (i = 0; i < end; i++)
468 nmatch = counts[equivs[i]];
471 else if (nmatch > many)
476 /* Don't really discard the provisional lines except when they occur
477 in a run of discardables, with nonprovisionals at the beginning
480 for (f = 0; f < 2; f++)
482 unsigned int end = filevec[f].buffered_lines;
483 register char *discards = discarded[f];
485 for (i = 0; i < end; i++)
487 /* Cancel provisional discards not in middle of run of discards. */
488 if (discards[i] == 2)
490 else if (discards[i] != 0)
492 /* We have found a nonprovisional discard. */
495 unsigned int provisional = 0;
497 /* Find end of this run of discardable lines.
498 Count how many are provisionally discardable. */
499 for (j = i; j < end; j++)
501 if (discards[j] == 0)
503 if (discards[j] == 2)
507 /* Cancel provisional discards at end, and shrink the run. */
508 while (j > i && discards[j - 1] == 2)
509 discards[--j] = 0, --provisional;
511 /* Now we have the length of a run of discardable lines
512 whose first and last are not provisional. */
515 /* If 1/4 of the lines in the run are provisional,
516 cancel discarding of all provisional lines in the run. */
517 if (provisional * 4 > length)
520 if (discards[--j] == 2)
525 register unsigned int consec;
526 unsigned int minimum = 1;
527 unsigned int tem = length / 4;
529 /* MINIMUM is approximate square root of LENGTH/4.
530 A subrun of two or more provisionals can stand
531 when LENGTH is at least 16.
532 A subrun of 4 or more can stand when LENGTH >= 64. */
533 while ((tem = tem >> 2) > 0)
537 /* Cancel any subrun of MINIMUM or more provisionals
538 within the larger run. */
539 for (j = 0, consec = 0; j < length; j++)
540 if (discards[i + j] != 2)
542 else if (minimum == ++consec)
543 /* Back up to start of subrun, to cancel it all. */
545 else if (minimum < consec)
548 /* Scan from beginning of run
549 until we find 3 or more nonprovisionals in a row
550 or until the first nonprovisional at least 8 lines in.
551 Until that point, cancel any provisionals. */
552 for (j = 0, consec = 0; j < length; j++)
554 if (j >= 8 && discards[i + j] == 1)
556 if (discards[i + j] == 2)
557 consec = 0, discards[i + j] = 0;
558 else if (discards[i + j] == 0)
566 /* I advances to the last line of the run. */
569 /* Same thing, from end. */
570 for (j = 0, consec = 0; j < length; j++)
572 if (j >= 8 && discards[i - j] == 1)
574 if (discards[i - j] == 2)
575 consec = 0, discards[i - j] = 0;
576 else if (discards[i - j] == 0)
588 /* Actually discard the lines. */
589 for (f = 0; f < 2; f++)
591 char *discards = discarded[f];
592 unsigned int end = filevec[f].buffered_lines;
594 for (i = 0; i < end; ++i)
595 if (no_discards || discards[i] == 0)
597 filevec[f].undiscarded[j] = filevec[f].equivs[i];
598 filevec[f].realindexes[j++] = i;
601 filevec[f].changed_flag[i] = 1;
602 filevec[f].nondiscarded_lines = j;
606 free (equiv_count[0]);
609 /* Adjust inserts/deletes of identical lines to join changes
612 We do something when a run of changed lines include a
613 line at one end and have an excluded, identical line at the other.
614 We are free to choose which identical line is included.
615 `compareseq' usually chooses the one at the beginning,
616 but usually it is cleaner to consider the following identical line
617 to be the "change". */
622 shift_boundaries (filevec)
623 struct file_data filevec[];
630 for (f = 0; f < 2; f++)
632 char *changed = filevec[f].changed_flag;
633 char const *other_changed = filevec[1-f].changed_flag;
634 int const *equivs = filevec[f].equivs;
637 int i_end = filevec[f].buffered_lines;
641 int runlength, start, corresponding;
643 /* Scan forwards to find beginning of another run of changes.
644 Also keep track of the corresponding point in the other file. */
646 while (i < i_end && changed[i] == 0)
648 while (other_changed[j++])
658 /* Find the end of this run of changes. */
662 while (other_changed[j])
667 /* Record the length of this run of changes, so that
668 we can later determine whether the run has grown. */
669 runlength = i - start;
671 /* Move the changed region back, so long as the
672 previous unchanged line matches the last changed one.
673 This merges with previous changed regions. */
675 while (start && equivs[start - 1] == equivs[i - 1])
677 changed[--start] = 1;
679 while (changed[start - 1])
681 while (other_changed[--j])
685 /* Set CORRESPONDING to the end of the changed run, at the last
686 point where it corresponds to a changed run in the other file.
687 CORRESPONDING == I_END means no such point has been found. */
688 corresponding = other_changed[j - 1] ? i : i_end;
690 /* Move the changed region forward, so long as the
691 first changed line matches the following unchanged one.
692 This merges with following changed regions.
693 Do this second, so that if there are no merges,
694 the changed region is moved forward as far as possible. */
696 while (i != i_end && equivs[start] == equivs[i])
698 changed[start++] = 0;
702 while (other_changed[++j])
706 while (runlength != i - start);
708 /* If possible, move the fully-merged run of changes
709 back to a corresponding run in the other file. */
711 while (corresponding < i)
713 changed[--start] = 1;
715 while (other_changed[--j])
722 /* Cons an additional entry onto the front of an edit script OLD.
723 LINE0 and LINE1 are the first affected lines in the two files (origin 0).
724 DELETED is the number of lines deleted here from file 0.
725 INSERTED is the number of lines inserted here in file 1.
727 If DELETED is 0 then LINE0 is the number of the line before
728 which the insertion was done; vice versa for INSERTED and LINE1. */
730 static struct change *
731 add_change (line0, line1, deleted, inserted, old)
732 int line0, line1, deleted, inserted;
735 struct change *new = (struct change *) xmalloc (sizeof (struct change));
739 new->inserted = inserted;
740 new->deleted = deleted;
745 /* Scan the tables of which lines are inserted and deleted,
746 producing an edit script in reverse order. */
748 static struct change *
749 build_reverse_script (filevec)
750 struct file_data const filevec[];
752 struct change *script = 0;
753 char *changed0 = filevec[0].changed_flag;
754 char *changed1 = filevec[1].changed_flag;
755 int len0 = filevec[0].buffered_lines;
756 int len1 = filevec[1].buffered_lines;
758 /* Note that changedN[len0] does exist, and contains 0. */
762 while (i0 < len0 || i1 < len1)
764 if (changed0[i0] || changed1[i1])
766 int line0 = i0, line1 = i1;
768 /* Find # lines changed here in each file. */
769 while (changed0[i0]) ++i0;
770 while (changed1[i1]) ++i1;
772 /* Record this change. */
773 script = add_change (line0, line1, i0 - line0, i1 - line1, script);
776 /* We have reached lines in the two files that match each other. */
783 /* Scan the tables of which lines are inserted and deleted,
784 producing an edit script in forward order. */
786 static struct change *
787 build_script (filevec)
788 struct file_data const filevec[];
790 struct change *script = 0;
791 char *changed0 = filevec[0].changed_flag;
792 char *changed1 = filevec[1].changed_flag;
793 int i0 = filevec[0].buffered_lines, i1 = filevec[1].buffered_lines;
795 /* Note that changedN[-1] does exist, and contains 0. */
797 while (i0 >= 0 || i1 >= 0)
799 if (changed0[i0 - 1] || changed1[i1 - 1])
801 int line0 = i0, line1 = i1;
803 /* Find # lines changed here in each file. */
804 while (changed0[i0 - 1]) --i0;
805 while (changed1[i1 - 1]) --i1;
807 /* Record this change. */
808 script = add_change (i0, i1, line0 - i0, line1 - i1, script);
811 /* We have reached lines in the two files that match each other. */
818 /* If CHANGES, briefly report that two files differed. */
820 briefly_report (changes, filevec)
822 struct file_data const filevec[];
825 message (no_details_flag ? "Files %s and %s differ\n"
826 : "Binary files %s and %s differ\n",
827 filevec[0].name, filevec[1].name);
830 /* Report the differences of two files. DEPTH is the current directory
833 diff_2_files (filevec, depth)
834 struct file_data filevec[];
839 struct change *e, *p;
840 struct change *script;
844 /* If we have detected that either file is binary,
845 compare the two files as binary. This can happen
846 only when the first chunk is read.
847 Also, --brief without any --ignore-* options means
848 we can speed things up by treating the files as binary. */
850 if (read_files (filevec, no_details_flag & ~ignore_some_changes))
852 /* Files with different lengths must be different. */
853 if (filevec[0].stat.st_size != filevec[1].stat.st_size
854 && (filevec[0].desc < 0 || S_ISREG (filevec[0].stat.st_mode))
855 && (filevec[1].desc < 0 || S_ISREG (filevec[1].stat.st_mode)))
858 /* Standard input equals itself. */
859 else if (filevec[0].desc == filevec[1].desc)
863 /* Scan both files, a buffer at a time, looking for a difference. */
865 /* Allocate same-sized buffers for both files. */
866 size_t buffer_size = buffer_lcm (STAT_BLOCKSIZE (filevec[0].stat),
867 STAT_BLOCKSIZE (filevec[1].stat));
868 for (i = 0; i < 2; i++)
869 filevec[i].buffer = xrealloc (filevec[i].buffer, buffer_size);
871 for (;; filevec[0].buffered_chars = filevec[1].buffered_chars = 0)
873 /* Read a buffer's worth from both files. */
874 for (i = 0; i < 2; i++)
875 if (0 <= filevec[i].desc)
876 while (filevec[i].buffered_chars != buffer_size)
878 int r = read (filevec[i].desc,
880 + filevec[i].buffered_chars,
881 buffer_size - filevec[i].buffered_chars);
885 pfatal_with_name (filevec[i].name);
886 filevec[i].buffered_chars += r;
889 /* If the buffers differ, the files differ. */
890 if (filevec[0].buffered_chars != filevec[1].buffered_chars
891 || (filevec[0].buffered_chars != 0
892 && memcmp (filevec[0].buffer,
894 filevec[0].buffered_chars) != 0))
900 /* If we reach end of file, the files are the same. */
901 if (filevec[0].buffered_chars != buffer_size)
909 briefly_report (changes, filevec);
913 /* Allocate vectors for the results of comparison:
914 a flag for each line of each file, saying whether that line
915 is an insertion or deletion.
916 Allocate an extra element, always zero, at each end of each vector. */
918 size_t s = filevec[0].buffered_lines + filevec[1].buffered_lines + 4;
919 filevec[0].changed_flag = xmalloc (s);
920 bzero (filevec[0].changed_flag, s);
921 filevec[0].changed_flag++;
922 filevec[1].changed_flag = filevec[0].changed_flag
923 + filevec[0].buffered_lines + 2;
925 /* Some lines are obviously insertions or deletions
926 because they don't match anything. Detect them now, and
927 avoid even thinking about them in the main comparison algorithm. */
929 discard_confusing_lines (filevec);
931 /* Now do the main comparison algorithm, considering just the
932 undiscarded lines. */
934 xvec = filevec[0].undiscarded;
935 yvec = filevec[1].undiscarded;
936 diags = filevec[0].nondiscarded_lines + filevec[1].nondiscarded_lines + 3;
937 fdiag = (int *) xmalloc (diags * (2 * sizeof (int)));
938 bdiag = fdiag + diags;
939 fdiag += filevec[1].nondiscarded_lines + 1;
940 bdiag += filevec[1].nondiscarded_lines + 1;
942 /* Set TOO_EXPENSIVE to be approximate square root of input size,
943 bounded below by 256. */
945 for (i = filevec[0].nondiscarded_lines + filevec[1].nondiscarded_lines;
948 too_expensive = max (256, too_expensive);
950 files[0] = filevec[0];
951 files[1] = filevec[1];
953 compareseq (0, filevec[0].nondiscarded_lines,
954 0, filevec[1].nondiscarded_lines, no_discards);
956 free (fdiag - (filevec[1].nondiscarded_lines + 1));
958 /* Modify the results slightly to make them prettier
959 in cases where that can validly be done. */
961 shift_boundaries (filevec);
963 /* Get the results of comparison in the form of a chain
964 of `struct change's -- an edit script. */
966 if (output_style == OUTPUT_ED)
967 script = build_reverse_script (filevec);
969 script = build_script (filevec);
971 /* Set CHANGES if we had any diffs.
972 If some changes are ignored, we must scan the script to decide. */
973 if (ignore_blank_lines_flag || ignore_regexp_list)
975 struct change *next = script;
978 while (next && changes == 0)
980 struct change *this, *end;
981 int first0, last0, first1, last1, deletes, inserts;
983 /* Find a set of changes that belong together. */
985 end = find_change (next);
987 /* Disconnect them from the rest of the changes, making them
988 a hunk, and remember the rest for next iteration. */
992 /* Determine whether this hunk is really a difference. */
993 analyze_hunk (this, &first0, &last0, &first1, &last1,
996 /* Reconnect the script so it will all be freed properly. */
999 if (deletes || inserts)
1004 changes = (script != 0);
1006 if (no_details_flag)
1007 briefly_report (changes, filevec);
1010 if (changes || ! no_diff_means_no_output)
1012 /* Record info for starting up output,
1013 to be used if and when we have some output to print. */
1014 setup_output (files[0].name, files[1].name, depth);
1016 switch (output_style)
1018 case OUTPUT_CONTEXT:
1019 print_context_script (script, 0);
1022 case OUTPUT_UNIFIED:
1023 print_context_script (script, 1);
1027 print_ed_script (script);
1030 case OUTPUT_FORWARD_ED:
1031 pr_forward_ed_script (script);
1035 print_rcs_script (script);
1039 print_normal_script (script);
1043 print_ifdef_script (script);
1047 print_sdiff_script (script);
1054 free (filevec[0].undiscarded);
1056 free (filevec[0].changed_flag - 1);
1058 for (i = 1; i >= 0; --i)
1059 free (filevec[i].equivs);
1061 for (i = 0; i < 2; ++i)
1062 free (filevec[i].linbuf + filevec[i].linbuf_base);
1064 for (e = script; e; e = p)
1070 if (! ROBUST_OUTPUT_STYLE (output_style))
1071 for (i = 0; i < 2; ++i)
1072 if (filevec[i].missing_newline)
1074 error ("No newline at end of file %s", filevec[i].name, "");
1079 if (filevec[0].buffer != filevec[1].buffer)
1080 free (filevec[0].buffer);
1081 free (filevec[1].buffer);