1 /* Functions to make fuzzy comparisons between strings
2 Copyright (C) 1988, 1989, 1992, 1993, 1995 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or (at
7 your option) any later version.
9 This program is distributed in the hope that it will be useful, but
10 WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 Derived from GNU diff 2.7, analyze.c et al.
21 The basic algorithm is described in:
22 "An O(ND) Difference Algorithm and its Variations", Eugene Myers,
23 Algorithmica Vol. 1 No. 2, 1986, pp. 251-266;
24 see especially section 4.2, which describes the variation used below.
26 The basic algorithm was independently discovered as described in:
27 "Algorithms for Approximate String Matching", E. Ukkonen,
28 Information and Control Vol. 64, 1985, pp. 100-118.
30 Modified to work on strings rather than files
31 by Peter Miller <pmiller@agso.gov.au>, October 1995
33 Modified to accept a "minimum similarity limit" to stop analyzing the
34 string when the similarity drops below the given limit by Marc Lehmann
47 #pragma warning( disable : 4244 )
51 * Data on one input string being compared.
55 /* The string to be compared. */
58 /* The length of the string to be compared. */
61 /* The number of characters inserted or deleted. */
65 static struct string_data string
[2];
67 static int max_edits
; /* compareseq stops when edits > max_edits */
71 /* This corresponds to the diff -H flag. With this heuristic, for
72 strings with a constant small density of changes, the algorithm is
73 linear in the strings size. This is unlikely in typical uses of
74 fstrcmp, and so is usually compiled out. Besides, there is no
75 interface to set it true. */
81 /* Vector, indexed by diagonal, containing 1 + the X coordinate of the
82 point furthest along the given diagonal in the forward search of the
86 /* Vector, indexed by diagonal, containing the X coordinate of the point
87 furthest along the given diagonal in the backward search of the edit
91 /* Edit scripts longer than this are too expensive to compute. */
92 static int too_expensive
;
94 /* Snakes bigger than this are considered `big'. */
95 #define SNAKE_LIMIT 20
99 /* Midpoints of this partition. */
102 /* Nonzero if low half will be analyzed minimally. */
105 /* Likewise for high half. */
111 diag - find diagonal path
114 int diag(int xoff, int xlim, int yoff, int ylim, int minimal,
115 struct partition *part);
118 Find the midpoint of the shortest edit script for a specified
119 portion of the two strings.
121 Scan from the beginnings of the strings, and simultaneously from
122 the ends, doing a breadth-first search through the space of
123 edit-sequence. When the two searches meet, we have found the
124 midpoint of the shortest edit sequence.
126 If MINIMAL is nonzero, find the minimal edit script regardless
127 of expense. Otherwise, if the search is too expensive, use
128 heuristics to stop the search and report a suboptimal answer.
131 Set PART->(XMID,YMID) to the midpoint (XMID,YMID). The diagonal
132 number XMID - YMID equals the number of inserted characters
133 minus the number of deleted characters (counting only characters
134 before the midpoint). Return the approximate edit cost; this is
135 the total number of characters inserted or deleted (counting
136 only characters before the midpoint), unless a heuristic is used
137 to terminate the search prematurely.
139 Set PART->LEFT_MINIMAL to nonzero iff the minimal edit script
140 for the left half of the partition is known; similarly for
144 This function assumes that the first characters of the specified
145 portions of the two strings do not match, and likewise that the
146 last characters do not match. The caller must trim matching
147 characters from the beginning and end of the portions it is
150 If we return the "wrong" partitions, the worst this can do is
151 cause suboptimal diff output. It cannot cause incorrect diff
154 static int diag
PARAMS ((int, int, int, int, int, struct partition
*));
157 diag (int xoff
, int xlim
, int yoff
, int ylim
, int minimal
, struct partition
*part
)
159 int *const fd
= fdiag
; /* Give the compiler a chance. */
160 int *const bd
= bdiag
; /* Additional help for the compiler. */
161 const char *const xv
= string
[0].data
; /* Still more help for the compiler. */
162 const char *const yv
= string
[1].data
; /* And more and more . . . */
163 const int dmin
= xoff
- ylim
; /* Minimum valid diagonal. */
164 const int dmax
= xlim
- yoff
; /* Maximum valid diagonal. */
165 const int fmid
= xoff
- yoff
; /* Center diagonal of top-down search. */
166 const int bmid
= xlim
- ylim
; /* Center diagonal of bottom-up search. */
168 int fmax
= fmid
; /* Limits of top-down search. */
170 int bmax
= bmid
; /* Limits of bottom-up search. */
172 int odd
= (fmid
- bmid
) & 1;
175 * True if southeast corner is on an odd diagonal with respect
182 int d
; /* Active diagonal. */
186 /* Extend the top-down search by an edit step in each diagonal. */
195 for (d
= fmax
; d
>= fmin
; d
-= 2)
212 while (x
< xlim
&& y
< ylim
&& xv
[x
] == yv
[y
])
217 if (x
- oldx
> SNAKE_LIMIT
)
220 if (odd
&& bmin
<= d
&& d
<= bmax
&& bd
[d
] <= x
)
224 part
->lo_minimal
= part
->hi_minimal
= 1;
228 /* Similarly extend the bottom-up search. */
230 bd
[--bmin
- 1] = INT_MAX
;
234 bd
[++bmax
+ 1] = INT_MAX
;
237 for (d
= bmax
; d
>= bmin
; d
-= 2)
253 while (x
> xoff
&& y
> yoff
&& xv
[x
- 1] == yv
[y
- 1])
258 if (oldx
- x
> SNAKE_LIMIT
)
261 if (!odd
&& fmin
<= d
&& d
<= fmax
&& x
<= fd
[d
])
265 part
->lo_minimal
= part
->hi_minimal
= 1;
274 /* Heuristic: check occasionally for a diagonal that has made lots
275 of progress compared with the edit distance. If we have any
276 such, find the one that has made the most progress and return
277 it as if it had succeeded.
279 With this heuristic, for strings with a constant small density
280 of changes, the algorithm is linear in the strings size. */
281 if (c
> 200 && big_snake
&& heuristic
)
286 for (d
= fmax
; d
>= fmin
; d
-= 2)
296 v
= (x
- xoff
) * 2 - dd
;
298 if (v
> 12 * (c
+ (dd
< 0 ? -dd
: dd
)))
304 xoff
+ SNAKE_LIMIT
<= x
308 yoff
+ SNAKE_LIMIT
<= y
313 /* We have a good enough best diagonal; now insist
314 that it end with a significant snake. */
317 for (k
= 1; xv
[x
- k
] == yv
[y
- k
]; k
++)
319 if (k
== SNAKE_LIMIT
)
332 part
->lo_minimal
= 1;
333 part
->hi_minimal
= 0;
337 for (d
= bmax
; d
>= bmin
; d
-= 2)
347 v
= (xlim
- x
) * 2 + dd
;
349 if (v
> 12 * (c
+ (dd
< 0 ? -dd
: dd
)))
351 if (v
> best
&& xoff
< x
&& x
<= xlim
- SNAKE_LIMIT
&&
352 yoff
< y
&& y
<= ylim
- SNAKE_LIMIT
)
354 /* We have a good enough best diagonal; now insist
355 that it end with a significant snake. */
358 for (k
= 0; xv
[x
+ k
] == yv
[y
+ k
]; k
++)
360 if (k
== SNAKE_LIMIT
- 1)
373 part
->lo_minimal
= 0;
374 part
->hi_minimal
= 1;
378 #endif /* MINUS_H_FLAG */
380 /* Heuristic: if we've gone well beyond the call of duty, give up
381 and report halfway between our best results so far. */
382 if (c
>= too_expensive
)
389 /* Pacify `gcc -Wall'. */
393 /* Find forward diagonal that maximizes X + Y. */
395 for (d
= fmax
; d
>= fmin
; d
-= 2)
400 x
= fd
[d
] < xlim
? fd
[d
] : xlim
;
414 /* Find backward diagonal that minimizes X + Y. */
416 for (d
= bmax
; d
>= bmin
; d
-= 2)
421 x
= xoff
> bd
[d
] ? xoff
: bd
[d
];
435 /* Use the better of the two diagonals. */
436 if ((xlim
+ ylim
) - bxybest
< fxybest
- (xoff
+ yoff
))
439 part
->ymid
= fxybest
- fxbest
;
440 part
->lo_minimal
= 1;
441 part
->hi_minimal
= 0;
446 part
->ymid
= bxybest
- bxbest
;
447 part
->lo_minimal
= 0;
448 part
->hi_minimal
= 1;
457 compareseq - find edit sequence
460 void compareseq(int xoff, int xlim, int yoff, int ylim, int minimal);
463 Compare in detail contiguous subsequences of the two strings
464 which are known, as a whole, to match each other.
466 The subsequence of string 0 is [XOFF, XLIM) and likewise for
469 Note that XLIM, YLIM are exclusive bounds. All character
470 numbers are origin-0.
472 If MINIMAL is nonzero, find a minimal difference no matter how
475 static void compareseq
PARAMS ((int, int, int, int, int));
478 compareseq (int xoff
, int xlim
, int yoff
, int ylim
, int minimal
)
480 const char *const xv
= string
[0].data
; /* Help the compiler. */
481 const char *const yv
= string
[1].data
;
483 if (string
[1].edit_count
+ string
[0].edit_count
> max_edits
)
486 /* Slide down the bottom initial diagonal. */
487 while (xoff
< xlim
&& yoff
< ylim
&& xv
[xoff
] == yv
[yoff
])
493 /* Slide up the top initial diagonal. */
494 while (xlim
> xoff
&& ylim
> yoff
&& xv
[xlim
- 1] == yv
[ylim
- 1])
500 /* Handle simple cases. */
505 ++string
[1].edit_count
;
509 else if (yoff
== ylim
)
513 ++string
[0].edit_count
;
520 struct partition part
;
522 /* Find a point of correspondence in the middle of the strings. */
523 c
= diag (xoff
, xlim
, yoff
, ylim
, minimal
, &part
);
528 / * This should be impossible, because it implies that one of
529 the two subsequences is empty, and that case was handled
530 above without calling `diag'. Let's verify that this is
535 /* The two subsequences differ by a single insert or delete;
536 record it and we are done. */
537 if (part
.xmid
- part
.ymid
< xoff
- yoff
)
538 ++string
[1].edit_count
;
540 ++string
[0].edit_count
;
545 /* Use the partitions to split this problem into subproblems. */
546 compareseq (xoff
, part
.xmid
, yoff
, part
.ymid
, part
.lo_minimal
);
547 compareseq (part
.xmid
, xlim
, part
.ymid
, ylim
, part
.hi_minimal
);
554 fstrcmp - fuzzy string compare
557 double fstrcmp(const char *, const char *, double);
560 The fstrcmp function may be used to compare two string for
561 similarity. It is very useful in reducing "cascade" or
562 "secondary" errors in compilers or other situations where
566 double; 0 if the strings are entirly dissimilar, 1 if the
567 strings are identical, and a number in between if they are
571 fstrcmp (const char *string1
, const char *string2
, double minimum
)
576 static int *fdiag_buf
;
577 static size_t fdiag_max
;
579 /* set the info for each string. */
580 string
[0].data
= string1
;
581 string
[0].data_length
= (int)strlen (string1
);
582 string
[1].data
= string2
;
583 string
[1].data_length
= (int)strlen (string2
);
585 /* short-circuit obvious comparisons */
586 if (string
[0].data_length
== 0 && string
[1].data_length
== 0)
588 if (string
[0].data_length
== 0 || string
[1].data_length
== 0)
591 /* Set TOO_EXPENSIVE to be approximate square root of input size,
592 bounded below by 256. */
594 for (i
= string
[0].data_length
+ string
[1].data_length
; i
!= 0; i
>>= 2)
596 if (too_expensive
< 256)
599 /* Because fstrcmp is typically called multiple times, while scanning
600 symbol tables, etc, attempt to minimize the number of memory
601 allocations performed. Thus, we use a static buffer for the
602 diagonal vectors, and never free them. */
603 fdiag_len
= string
[0].data_length
+ string
[1].data_length
+ 3;
604 if (fdiag_len
> fdiag_max
)
606 fdiag_max
= fdiag_len
;
607 fdiag_buf
= (int*)realloc (fdiag_buf
, fdiag_max
* (2 * sizeof (int)));
609 fdiag
= fdiag_buf
+ string
[1].data_length
+ 1;
610 bdiag
= fdiag
+ fdiag_len
;
612 max_edits
= (int) (1 + (string
[0].data_length
+ string
[1].data_length
) * (1. - minimum
));
614 /* Now do the main comparison algorithm */
615 string
[0].edit_count
= 0;
616 string
[1].edit_count
= 0;
617 compareseq (0, string
[0].data_length
, 0, string
[1].data_length
, 0);
620 ((number of chars in common) / (average length of the strings)).
621 This is admittedly biased towards finding that the strings are
622 similar, however it does produce meaningful results. */
624 (string
[0].data_length
+ string
[1].data_length
- string
[1].edit_count
- string
[0].edit_count
)
625 / (string
[0].data_length
+ string
[1].data_length
));