fstrcmp.c

filter: Statementstmt 74.5% 117 / 157 Branchbran 73.5% 78 / 106 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 74.1% 195 / 263 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
↑ Summary Similarity.xs »
1 /* Functions to make fuzzy comparisons between strings
2 Copyright (C) 1988, 1989, 1992, 1993, 1995 Free Software Foundation, Inc.
3  
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.
8  
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.
13  
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.
17  
18  
19 Derived from GNU diff 2.7, analyze.c et al.
20  
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.
25  
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.
29  
30 Modified to work on strings rather than files
31 by Peter Miller <pmiller@agso.gov.au>, October 1995
32  
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
35 <schmorp@schmorp.de>.
36  
37 Modified to work on unicode (actually 31 bit are allowed) by Marc Lehmann
38 <schmorp@schmorp.de>.
39 */
40  
41 #include <string.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <limits.h>
45  
46 #include "fstrcmp.h"
47  
48 #define PARAMS(proto) proto
49  
50 /*
51 * Data on one input string being compared.
52 */
53 struct string_data
54 {
55 /* The string to be compared. */
56 const UV *data;
57  
58 /* The length of the string to be compared. */
59 int data_length;
60  
61 /* The number of characters inserted or deleted. */
62 int edit_count;
63 };
64  
65 static struct string_data string[2];
66  
67 static int max_edits; /* compareseq stops when edits > max_edits */
68  
69 #ifdef MINUS_H_FLAG
70  
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. */
76 static int heuristic;
77  
78 #endif
79  
80  
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
83 edit matrix. */
84 static int *fdiag;
85  
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
88 matrix. */
89 static int *bdiag;
90  
91 /* Edit scripts longer than this are too expensive to compute. */
92 static int too_expensive;
93  
94 /* Snakes bigger than this are considered `big'. */
95 #define SNAKE_LIMIT 20
96  
97 struct partition
98 {
99 /* Midpoints of this partition. */
100 int xmid, ymid;
101  
102 /* Nonzero if low half will be analyzed minimally. */
103 int lo_minimal;
104  
105 /* Likewise for high half. */
106 int hi_minimal;
107 };
108  
109  
110 /* NAME
111 diag - find diagonal path
112  
113 SYNOPSIS
114 int diag(int xoff, int xlim, int yoff, int ylim, int minimal,
115 struct partition *part);
116  
117 DESCRIPTION
118 Find the midpoint of the shortest edit script for a specified
119 portion of the two strings.
120  
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.
125  
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.
129  
130 RETURNS
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.
138  
139 Set PART->LEFT_MINIMAL to nonzero iff the minimal edit script
140 for the left half of the partition is known; similarly for
141 PART->RIGHT_MINIMAL.
142  
143 CAVEAT
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
148 going to specify.
149  
150 If we return the "wrong" partitions, the worst this can do is
151 cause suboptimal diff output. It cannot cause incorrect diff
152 output. */
153  
154 static int diag PARAMS ((int, int, int, int, int, struct partition *));
155  
156 static int
157 15 diag (xoff, xlim, yoff, ylim, minimal, part)
158 int xoff;
159 int xlim;
160 int yoff;
161 int ylim;
162 int minimal;
163 struct partition *part;
164 {
165 15 int *const fd = fdiag; /* Give the compiler a chance. */
166 15 int *const bd = bdiag; /* Additional help for the compiler. */
167 15 const UV *const xv = string[0].data; /* Still more help for the compiler. */
168 15 const UV *const yv = string[1].data; /* And more and more . . . */
169 15 const int dmin = xoff - ylim; /* Minimum valid diagonal. */
170 15 const int dmax = xlim - yoff; /* Maximum valid diagonal. */
171 15 const int fmid = xoff - yoff; /* Center diagonal of top-down search. */
172 15 const int bmid = xlim - ylim; /* Center diagonal of bottom-up search. */
173 15 int fmin = fmid;
174 15 int fmax = fmid; /* Limits of top-down search. */
175 15 int bmin = bmid;
176 15 int bmax = bmid; /* Limits of bottom-up search. */
177 int c; /* Cost. */
178 15 int odd = (fmid - bmid) & 1;
179  
180 /*
181 * True if southeast corner is on an odd diagonal with respect
182 * to the northwest.
183 */
184 15 fd[fmid] = xoff;
185 15 bd[bmid] = xlim;
186 15 for (c = 1;; ++c)
187 21 {
188 int d; /* Active diagonal. */
189 int big_snake;
190  
191 36 big_snake = 0;
192 /* Extend the top-down search by an edit step in each diagonal. */
193 36 ▶ if (fmin > dmin)
Branch: 1 branch
BranchTrueFalseTotal
if (fmin > dmin) 34 2 36
194 34 fd[--fmin - 1] = -1;
195 else
196 2 ++fmin;
197 36 ▶ if (fmax < dmax)
Branch: 1 branch
BranchTrueFalseTotal
if (fmax < dmax) 32 4 36
198 32 fd[++fmax + 1] = -1;
199 else
200 4 --fmax;
201 126 ▶ for (d = fmax; d >= fmin; d -= 2)
Branch: 1 branch
BranchTrueFalseTotal
for (d = fmax; d >= fmin; d -= 2) 96 30 126
202 {
203 int x;
204 int y;
205 int oldx;
206 int tlo;
207 int thi;
208  
209 96 tlo = fd[d - 1],
210 96 thi = fd[d + 1];
211  
212 96 ▶ if (tlo >= thi)
Branch: 1 branch
BranchTrueFalseTotal
if (tlo >= thi) 34 62 96
213 34 x = tlo + 1;
214 else
215 62 x = thi;
216 96 oldx = x;
217 96 y = x - d;
218 103 ▶ while (x < xlim && y < ylim && xv[x] == yv[y])
Branch: 3 branches
BranchTrueFalseTotal
while (x < xlim && y < ylim && xv[x] == yv[y]) 87 16 103
while (x < xlim && y < ylim && xv[x] == yv[y]) 78 9 87
while (x < xlim && y < ylim && xv[x] == yv[y]) 7 71 78
219 {
220 7 ++x;
221 7 ++y;
222 }
223 96 ▶ if (x - oldx > SNAKE_LIMIT)
Branch: 1 branch
BranchTrueFalseTotal
if (x - oldx > SNAKE_LIMIT) 0 96 96
224 0 big_snake = 1;
225 96 fd[d] = x;
226 96 ▶ if (odd && bmin <= d && d <= bmax && bd[d] <= x)
Branch: 4 branches
BranchTrueFalseTotal
if (odd && bmin <= d && d <= bmax && bd[d] <= x) 30 66 96
if (odd && bmin <= d && d <= bmax && bd[d] <= x) 22 8 30
if (odd && bmin <= d && d <= bmax && bd[d] <= x) 12 10 22
if (odd && bmin <= d && d <= bmax && bd[d] <= x) 6 6 12
227 {
228 6 part->xmid = x;
229 6 part->ymid = y;
230 6 part->lo_minimal = part->hi_minimal = 1;
231 6 return 2 * c - 1;
232 }
233 }
234 /* Similarly extend the bottom-up search. */
235 30 ▶ if (bmin > dmin)
Branch: 1 branch
BranchTrueFalseTotal
if (bmin > dmin) 28 2 30
236 28 bd[--bmin - 1] = INT_MAX;
237 else
238 2 ++bmin;
239 30 ▶ if (bmax < dmax)
Branch: 1 branch
BranchTrueFalseTotal
if (bmax < dmax) 30 0 30
240 30 bd[++bmax + 1] = INT_MAX;
241 else
242 0 --bmax;
243 97 ▶ for (d = bmax; d >= bmin; d -= 2)
Branch: 1 branch
BranchTrueFalseTotal
for (d = bmax; d >= bmin; d -= 2) 76 21 97
244 {
245 int x;
246 int y;
247 int oldx;
248 int tlo;
249 int thi;
250  
251 76 tlo = bd[d - 1],
252 76 thi = bd[d + 1];
253 76 ▶ if (tlo < thi)
Branch: 1 branch
BranchTrueFalseTotal
if (tlo < thi) 54 22 76
254 54 x = tlo;
255 else
256 22 x = thi - 1;
257 76 oldx = x;
258 76 y = x - d;
259 83 ▶ while (x > xoff && y > yoff && xv[x - 1] == yv[y - 1])
Branch: 3 branches
BranchTrueFalseTotal
while (x > xoff && y > yoff && xv[x - 1] == yv[y - 1]) 74 9 83
while (x > xoff && y > yoff && xv[x - 1] == yv[y - 1]) 66 8 74
while (x > xoff && y > yoff && xv[x - 1] == yv[y - 1]) 7 59 66
260 {
261 7 --x;
262 7 --y;
263 }
264 76 ▶ if (oldx - x > SNAKE_LIMIT)
Branch: 1 branch
BranchTrueFalseTotal
if (oldx - x > SNAKE_LIMIT) 0 76 76
265 0 big_snake = 1;
266 76 bd[d] = x;
267 76 ▶ if (!odd && fmin <= d && d <= fmax && x <= fd[d])
Branch: 4 branches
BranchTrueFalseTotal
if (!odd && fmin <= d && d <= fmax && x <= fd[d]) 54 22 76
if (!odd && fmin <= d && d <= fmax && x <= fd[d]) 54 0 54
if (!odd && fmin <= d && d <= fmax && x <= fd[d]) 54 0 54
if (!odd && fmin <= d && d <= fmax && x <= fd[d]) 9 45 54
268 {
269 9 part->xmid = x;
270 9 part->ymid = y;
271 9 part->lo_minimal = part->hi_minimal = 1;
272 9 return 2 * c;
273 }
274 }
275  
276 21 ▶ if (minimal)
Branch: 1 branch
BranchTrueFalseTotal
if (minimal) 10 11 21
277 10 continue;
278  
279 #ifdef MINUS_H_FLAG
280 /* Heuristic: check occasionally for a diagonal that has made lots
281 of progress compared with the edit distance. If we have any
282 such, find the one that has made the most progress and return
283 it as if it had succeeded.
284  
285 With this heuristic, for strings with a constant small density
286 of changes, the algorithm is linear in the strings size. */
287 if (c > 200 && big_snake && heuristic)
288 {
289 int best;
290  
291 best = 0;
292 for (d = fmax; d >= fmin; d -= 2)
293 {
294 int dd;
295 int x;
296 int y;
297 int v;
298  
299 dd = d - fmid;
300 x = fd[d];
301 y = x - d;
302 v = (x - xoff) * 2 - dd;
303  
304 if (v > 12 * (c + (dd < 0 ? -dd : dd)))
305 {
306 if
307 (
308 v > best
309 &&
310 xoff + SNAKE_LIMIT <= x
311 &&
312 x < xlim
313 &&
314 yoff + SNAKE_LIMIT <= y
315 &&
316 y < ylim
317 )
318 {
319 /* We have a good enough best diagonal; now insist
320 that it end with a significant snake. */
321 int k;
322  
323 for (k = 1; xv[x - k] == yv[y - k]; k++)
324 {
325 if (k == SNAKE_LIMIT)
326 {
327 best = v;
328 part->xmid = x;
329 part->ymid = y;
330 break;
331 }
332 }
333 }
334 }
335 }
336 if (best > 0)
337 {
338 part->lo_minimal = 1;
339 part->hi_minimal = 0;
340 return 2 * c - 1;
341 }
342 best = 0;
343 for (d = bmax; d >= bmin; d -= 2)
344 {
345 int dd;
346 int x;
347 int y;
348 int v;
349  
350 dd = d - bmid;
351 x = bd[d];
352 y = x - d;
353 v = (xlim - x) * 2 + dd;
354  
355 if (v > 12 * (c + (dd < 0 ? -dd : dd)))
356 {
357 if (v > best && xoff < x && x <= xlim - SNAKE_LIMIT &&
358 yoff < y && y <= ylim - SNAKE_LIMIT)
359 {
360 /* We have a good enough best diagonal; now insist
361 that it end with a significant snake. */
362 int k;
363  
364 for (k = 0; xv[x + k] == yv[y + k]; k++)
365 {
366 if (k == SNAKE_LIMIT - 1)
367 {
368 best = v;
369 part->xmid = x;
370 part->ymid = y;
371 break;
372 }
373 }
374 }
375 }
376 }
377 if (best > 0)
378 {
379 part->lo_minimal = 0;
380 part->hi_minimal = 1;
381 return 2 * c - 1;
382 }
383 }
384 #endif /* MINUS_H_FLAG */
385  
386 /* Heuristic: if we've gone well beyond the call of duty, give up
387 and report halfway between our best results so far. */
388 11 ▶ if (c >= too_expensive)
Branch: 1 branch
BranchTrueFalseTotal
if (c >= too_expensive) 0 11 11
389 {
390 int fxybest;
391 int fxbest;
392 int bxybest;
393 int bxbest;
394  
395 /* Pacify `gcc -Wall'. */
396 0 fxbest = 0;
397 0 bxbest = 0;
398  
399 /* Find forward diagonal that maximizes X + Y. */
400 0 fxybest = -1;
401 0 ▶ for (d = fmax; d >= fmin; d -= 2)
Branch: 1 branch
BranchTrueFalseTotal
for (d = fmax; d >= fmin; d -= 2) 0 0 0
402 {
403 int x;
404 int y;
405  
406 0 x = fd[d] < xlim ? fd[d] : xlim;
407 0 y = x - d;
408  
409 0 ▶ if (ylim < y)
Branch: 1 branch
BranchTrueFalseTotal
if (ylim < y) 0 0 0
410 {
411 0 x = ylim + d;
412 0 y = ylim;
413 }
414 0 ▶ if (fxybest < x + y)
Branch: 1 branch
BranchTrueFalseTotal
if (fxybest < x + y) 0 0 0
415 {
416 0 fxybest = x + y;
417 0 fxbest = x;
418 }
419 }
420 /* Find backward diagonal that minimizes X + Y. */
421 0 bxybest = INT_MAX;
422 0 ▶ for (d = bmax; d >= bmin; d -= 2)
Branch: 1 branch
BranchTrueFalseTotal
for (d = bmax; d >= bmin; d -= 2) 0 0 0
423 {
424 int x;
425 int y;
426  
427 0 x = xoff > bd[d] ? xoff : bd[d];
428 0 y = x - d;
429  
430 0 ▶ if (y < yoff)
Branch: 1 branch
BranchTrueFalseTotal
if (y < yoff) 0 0 0
431 {
432 0 x = yoff + d;
433 0 y = yoff;
434 }
435 0 ▶ if (x + y < bxybest)
Branch: 1 branch
BranchTrueFalseTotal
if (x + y < bxybest) 0 0 0
436 {
437 0 bxybest = x + y;
438 0 bxbest = x;
439 }
440 }
441 /* Use the better of the two diagonals. */
442 0 ▶ if ((xlim + ylim) - bxybest < fxybest - (xoff + yoff))
Branch: 1 branch
BranchTrueFalseTotal
if ((xlim + ylim) - bxybest < fxybest - (xoff + yoff)) 0 0 0
443 {
444 0 part->xmid = fxbest;
445 0 part->ymid = fxybest - fxbest;
446 0 part->lo_minimal = 1;
447 0 part->hi_minimal = 0;
448 }
449 else
450 {
451 0 part->xmid = bxbest;
452 0 part->ymid = bxybest - bxbest;
453 0 part->lo_minimal = 0;
454 0 part->hi_minimal = 1;
455 }
456 0 return 2 * c - 1;
457 }
458 }
459 }
460  
461  
462 /* NAME
463 compareseq - find edit sequence
464  
465 SYNOPSIS
466 void compareseq(int xoff, int xlim, int yoff, int ylim, int minimal);
467  
468 DESCRIPTION
469 Compare in detail contiguous subsequences of the two strings
470 which are known, as a whole, to match each other.
471  
472 The subsequence of string 0 is [XOFF, XLIM) and likewise for
473 string 1.
474  
475 Note that XLIM, YLIM are exclusive bounds. All character
476 numbers are origin-0.
477  
478 If MINIMAL is nonzero, find a minimal difference no matter how
479 expensive it is. */
480  
481 static void compareseq PARAMS ((int, int, int, int, int));
482  
483 static void
484 39 compareseq (xoff, xlim, yoff, ylim, minimal)
485 int xoff;
486 int xlim;
487 int yoff;
488 int ylim;
489 int minimal;
490 {
491 39 const UV *const xv = string[0].data; /* Help the compiler. */
492 39 const UV *const yv = string[1].data;
493  
494 39 ▶ if (string[1].edit_count + string[0].edit_count > max_edits)
Branch: 1 branch
BranchTrueFalseTotal
if (string[1].edit_count + string[0].edit_count > max_edits) 0 39 39
495 0 return;
496  
497 /* Slide down the bottom initial diagonal. */
498 112 ▶ while (xoff < xlim && yoff < ylim && xv[xoff] == yv[yoff])
Branch: 3 branches
BranchTrueFalseTotal
while (xoff < xlim && yoff < ylim && xv[xoff] == yv[yoff]) 98 14 112
while (xoff < xlim && yoff < ylim && xv[xoff] == yv[yoff]) 89 9 98
while (xoff < xlim && yoff < ylim && xv[xoff] == yv[yoff]) 73 16 89
499 {
500 73 ++xoff;
501 73 ++yoff;
502 }
503  
504 /* Slide up the top initial diagonal. */
505 43 ▶ while (xlim > xoff && ylim > yoff && xv[xlim - 1] == yv[ylim - 1])
Branch: 3 branches
BranchTrueFalseTotal
while (xlim > xoff && ylim > yoff && xv[xlim - 1] == yv[ylim - 1]) 28 15 43
while (xlim > xoff && ylim > yoff && xv[xlim - 1] == yv[ylim - 1]) 19 9 28
while (xlim > xoff && ylim > yoff && xv[xlim - 1] == yv[ylim - 1]) 4 15 19
506 {
507 4 --xlim;
508 4 --ylim;
509 }
510  
511 /* Handle simple cases. */
512 39 ▶ if (xoff == xlim)
Branch: 1 branch
BranchTrueFalseTotal
if (xoff == xlim) 15 24 39
513 {
514 32 ▶ while (yoff < ylim)
Branch: 1 branch
BranchTrueFalseTotal
while (yoff < ylim) 17 15 32
515 {
516 17 ++string[1].edit_count;
517 17 ++yoff;
518 }
519 }
520 24 ▶ else if (yoff == ylim)
Branch: 1 branch
BranchTrueFalseTotal
else if (yoff == ylim) 9 15 24
521 {
522 25 ▶ while (xoff < xlim)
Branch: 1 branch
BranchTrueFalseTotal
while (xoff < xlim) 16 9 25
523 {
524 16 ++string[0].edit_count;
525 16 ++xoff;
526 }
527 }
528 else
529 {
530 int c;
531 struct partition part;
532  
533 /* Find a point of correspondence in the middle of the strings. */
534 15 c = diag (xoff, xlim, yoff, ylim, minimal, &part);
535 15 ▶ if (c == 1)
Branch: 1 branch
BranchTrueFalseTotal
if (c == 1) 0 15 15
536 {
537 #if 0
538 /* This should be impossible, because it implies that one of
539 the two subsequences is empty, and that case was handled
540 above without calling `diag'. Let's verify that this is
541 true. */
542 abort ();
543 #else
544 /* The two subsequences differ by a single insert or delete;
545 record it and we are done. */
546 0 ▶ if (part.xmid - part.ymid < xoff - yoff)
Branch: 1 branch
BranchTrueFalseTotal
if (part.xmid - part.ymid < xoff - yoff) 0 0 0
547 0 ++string[1].edit_count;
548 else
549 0 ++string[0].edit_count;
550 #endif
551 }
552 else
553 {
554 /* Use the partitions to split this problem into subproblems. */
555 15 compareseq (xoff, part.xmid, yoff, part.ymid, part.lo_minimal);
556 15 compareseq (part.xmid, xlim, part.ymid, ylim, part.hi_minimal);
557 }
558 }
559 }
560  
561  
562 /* NAME
563 fstrcmp - fuzzy string compare
564  
565 SYNOPSIS
566 double fstrcmp(const ChaR *s1, int l1, const UV *s2, int l2, double);
567  
568 DESCRIPTION
569 The fstrcmp function may be used to compare two string for
570 similarity. It is very useful in reducing "cascade" or
571 "secondary" errors in compilers or other situations where
572 symbol tables occur.
573  
574 RETURNS
575 double; 0 if the strings are entirly dissimilar, 1 if the
576 strings are identical, and a number in between if they are
577 similar. */
578  
579 double
580 10 fstrcmp (const UV *string1, int length1,
581 const UV *string2, int length2,
582 double minimum)
583 {
584 int i;
585  
586 size_t fdiag_len;
587 static int *fdiag_buf;
588 static size_t fdiag_max;
589  
590 /* set the info for each string. */
591 10 string[0].data = string1;
592 10 string[0].data_length = length1;
593 10 string[1].data = string2;
594 10 string[1].data_length = length2;
595  
596 /* short-circuit obvious comparisons */
597 10 ▶ if (string[0].data_length == 0 && string[1].data_length == 0)
Branch: 2 branches
BranchTrueFalseTotal
if (string[0].data_length == 0 && string[1].data_length == 0) 1 9 10
if (string[0].data_length == 0 && string[1].data_length == 0) 1 0 1
598 1 return 1.0;
599 9 ▶ if (string[0].data_length == 0 || string[1].data_length == 0)
Branch: 2 branches
BranchTrueFalseTotal
if (string[0].data_length == 0 || string[1].data_length == 0) 9 0 9
if (string[0].data_length == 0 || string[1].data_length == 0) 0 9 9
600 0 return 0.0;
601  
602 /* Set TOO_EXPENSIVE to be approximate square root of input size,
603 bounded below by 256. */
604 9 too_expensive = 1;
605 26 ▶ for (i = string[0].data_length + string[1].data_length; i != 0; i >>= 2)
Branch: 1 branch
BranchTrueFalseTotal
for (i = string[0].data_length + string[1].data_length; i != 0; i >>= 2) 17 9 26
606 17 too_expensive <<= 1;
607 9 ▶ if (too_expensive < 256)
Branch: 1 branch
BranchTrueFalseTotal
if (too_expensive < 256) 9 0 9
608 9 too_expensive = 256;
609  
610 /* Because fstrcmp is typically called multiple times, while scanning
611 symbol tables, etc, attempt to minimize the number of memory
612 allocations performed. Thus, we use a static buffer for the
613 diagonal vectors, and never free them. */
614 9 fdiag_len = string[0].data_length + string[1].data_length + 3;
615 9 ▶ if (fdiag_len > fdiag_max)
Branch: 1 branch
BranchTrueFalseTotal
if (fdiag_len > fdiag_max) 1 8 9
616 {
617 1 fdiag_max = fdiag_len;
618 1 fdiag_buf = realloc (fdiag_buf, fdiag_max * (2 * sizeof (int)));
619 }
620 9 fdiag = fdiag_buf + string[1].data_length + 1;
621 9 bdiag = fdiag + fdiag_len;
622  
623 9 max_edits = 1 + (string[0].data_length + string[1].data_length) * (1. - minimum);
624  
625 /* Now do the main comparison algorithm */
626 9 string[0].edit_count = 0;
627 9 string[1].edit_count = 0;
628 9 compareseq (0, string[0].data_length, 0, string[1].data_length, 0);
629  
630 /* The result is
631 ((number of chars in common) / (average length of the strings)).
632 This is admittedly biased towards finding that the strings are
633 similar, however it does produce meaningful results. */
634 9 return ((double)
635 9 (string[0].data_length + string[1].data_length - string[1].edit_count - string[0].edit_count)
636 9 / (string[0].data_length + string[1].data_length));
637  
638 }
↑ Summary Similarity.xs »