📄 fstrcmp.c
字号:
/* Functions to make fuzzy comparisons between strings Copyright (C) 1988, 1989, 1992, 1993, 1995 Free Software Foundation, Inc. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. Derived from GNU diff 2.7, analyze.c et al. The basic algorithm is described in: "An O(ND) Difference Algorithm and its Variations", Eugene Myers, Algorithmica Vol. 1 No. 2, 1986, pp. 251-266; see especially section 4.2, which describes the variation used below. The basic algorithm was independently discovered as described in: "Algorithms for Approximate String Matching", E. Ukkonen, Information and Control Vol. 64, 1985, pp. 100-118. Modified to work on strings rather than files by Peter Miller <pmiller@agso.gov.au>, October 1995 Modified to accept a "minimum similarity limit" to stop analyzing the string when the similarity drops below the given limit by Marc Lehmann <pcg@goof.com>.*/#include <string.h>#include <stdio.h>#include <stdlib.h>#include <limits.h>#include "fstrcmp.h"/* * Data on one input string being compared. */struct string_data{ /* The string to be compared. */ const char *data; /* The length of the string to be compared. */ int data_length; /* The number of characters inserted or deleted. */ int edit_count;};static struct string_data string[2];static int max_edits; /* compareseq stops when edits > max_edits */#ifdef MINUS_H_FLAG/* This corresponds to the diff -H flag. With this heuristic, for strings with a constant small density of changes, the algorithm is linear in the strings size. This is unlikely in typical uses of fstrcmp, and so is usually compiled out. Besides, there is no interface to set it true. */static int heuristic;#endif/* Vector, indexed by diagonal, containing 1 + the X coordinate of the point furthest along the given diagonal in the forward search of the edit matrix. */static int *fdiag;/* Vector, indexed by diagonal, containing the X coordinate of the point furthest along the given diagonal in the backward search of the edit matrix. */static int *bdiag;/* Edit scripts longer than this are too expensive to compute. */static int too_expensive;/* Snakes bigger than this are considered `big'. */#define SNAKE_LIMIT 20struct partition{ /* Midpoints of this partition. */ int xmid, ymid; /* Nonzero if low half will be analyzed minimally. */ int lo_minimal; /* Likewise for high half. */ int hi_minimal;};/* NAME diag - find diagonal path SYNOPSIS int diag(int xoff, int xlim, int yoff, int ylim, int minimal, struct partition *part); DESCRIPTION Find the midpoint of the shortest edit script for a specified portion of the two strings. Scan from the beginnings of the strings, and simultaneously from the ends, doing a breadth-first search through the space of edit-sequence. When the two searches meet, we have found the midpoint of the shortest edit sequence. If MINIMAL is nonzero, find the minimal edit script regardless of expense. Otherwise, if the search is too expensive, use heuristics to stop the search and report a suboptimal answer. RETURNS Set PART->(XMID,YMID) to the midpoint (XMID,YMID). The diagonal number XMID - YMID equals the number of inserted characters minus the number of deleted characters (counting only characters before the midpoint). Return the approximate edit cost; this is the total number of characters inserted or deleted (counting only characters before the midpoint), unless a heuristic is used to terminate the search prematurely. Set PART->LEFT_MINIMAL to nonzero iff the minimal edit script for the left half of the partition is known; similarly for PART->RIGHT_MINIMAL. CAVEAT This function assumes that the first characters of the specified portions of the two strings do not match, and likewise that the last characters do not match. The caller must trim matching characters from the beginning and end of the portions it is going to specify. If we return the "wrong" partitions, the worst this can do is cause suboptimal diff output. It cannot cause incorrect diff output. */static int diag PARAMS ((int, int, int, int, int, struct partition *));static intdiag (xoff, xlim, yoff, ylim, minimal, part) int xoff; int xlim; int yoff; int ylim; int minimal; struct partition *part;{ int *const fd = fdiag; /* Give the compiler a chance. */ int *const bd = bdiag; /* Additional help for the compiler. */ const char *const xv = string[0].data; /* Still more help for the compiler. */ const char *const yv = string[1].data; /* And more and more . . . */ const int dmin = xoff - ylim; /* Minimum valid diagonal. */ const int dmax = xlim - yoff; /* Maximum valid diagonal. */ const int fmid = xoff - yoff; /* Center diagonal of top-down search. */ const int bmid = xlim - ylim; /* Center diagonal of bottom-up search. */ int fmin = fmid; int fmax = fmid; /* Limits of top-down search. */ int bmin = bmid; int bmax = bmid; /* Limits of bottom-up search. */ int c; /* Cost. */ int odd = (fmid - bmid) & 1; /* * True if southeast corner is on an odd diagonal with respect * to the northwest. */ fd[fmid] = xoff; bd[bmid] = xlim; for (c = 1;; ++c) { int d; /* Active diagonal. */ int big_snake; big_snake = 0; /* Extend the top-down search by an edit step in each diagonal. */ if (fmin > dmin) fd[--fmin - 1] = -1; else ++fmin; if (fmax < dmax) fd[++fmax + 1] = -1; else --fmax; for (d = fmax; d >= fmin; d -= 2) { int x; int y; int oldx; int tlo; int thi; tlo = fd[d - 1], thi = fd[d + 1]; if (tlo >= thi) x = tlo + 1; else x = thi; oldx = x; y = x - d; while (x < xlim && y < ylim && xv[x] == yv[y]) { ++x; ++y; } if (x - oldx > SNAKE_LIMIT) big_snake = 1; fd[d] = x; if (odd && bmin <= d && d <= bmax && bd[d] <= x) { part->xmid = x; part->ymid = y; part->lo_minimal = part->hi_minimal = 1; return 2 * c - 1; } } /* Similarly extend the bottom-up search. */ if (bmin > dmin) bd[--bmin - 1] = INT_MAX; else ++bmin; if (bmax < dmax) bd[++bmax + 1] = INT_MAX; else --bmax; for (d = bmax; d >= bmin; d -= 2) { int x; int y; int oldx; int tlo; int thi; tlo = bd[d - 1], thi = bd[d + 1]; if (tlo < thi) x = tlo; else x = thi - 1; oldx = x; y = x - d; while (x > xoff && y > yoff && xv[x - 1] == yv[y - 1]) { --x; --y; } if (oldx - x > SNAKE_LIMIT) big_snake = 1; bd[d] = x; if (!odd && fmin <= d && d <= fmax && x <= fd[d]) { part->xmid = x; part->ymid = y; part->lo_minimal = part->hi_minimal = 1; return 2 * c; } } if (minimal) continue;#ifdef MINUS_H_FLAG /* Heuristic: check occasionally for a diagonal that has made lots of progress compared with the edit distance. If we have any such, find the one that has made the most progress and return it as if it had succeeded. With this heuristic, for strings with a constant small density of changes, the algorithm is linear in the strings size. */ if (c > 200 && big_snake && heuristic) { int best; best = 0; for (d = fmax; d >= fmin; d -= 2) { int dd; int x; int y; int v; dd = d - fmid; x = fd[d]; y = x - d; v = (x - xoff) * 2 - dd; if (v > 12 * (c + (dd < 0 ? -dd : dd))) { if ( v > best && xoff + SNAKE_LIMIT <= x && x < xlim && yoff + SNAKE_LIMIT <= y && y < ylim ) { /* We have a good enough best diagonal; now insist that it end with a significant snake. */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -