| 1 | /* |
| 2 | * fstrcmp - fuzzy string compare library |
| 3 | * Copyright (C) 2009 Peter Miller |
| 4 | * |
| 5 | * |
| 6 | * Derived from gettext 0.17 |
| 7 | * Copyright (C) 1988-2006 Free Software Foundation, Inc. |
| 8 | * |
| 9 | * This program is free software: you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 3 of the License, or (at |
| 12 | * your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 17 | * General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 21 | * |
| 22 | * |
| 23 | * Derived from GNU diff 2.7, analyze.c et al. |
| 24 | * |
| 25 | * The basic idea is to consider two vectors as similar if, when |
| 26 | * transforming the first vector into the second vector through a |
| 27 | * sequence of edits (inserts and deletes of one element each), this |
| 28 | * sequence is short - or equivalently, if the ordered list of elements |
| 29 | * that are untouched by these edits is long. For a good introduction |
| 30 | * to the subject, read about the "Levenshtein distance" in Wikipedia. |
| 31 | * |
| 32 | * The basic algorithm is described in: |
| 33 | * "An O(ND) Difference Algorithm and its Variations", Eugene Myers, |
| 34 | * Algorithmica Vol. 1 No. 2, 1986, pp. 251-266; |
| 35 | * see especially section 4.2, which describes the variation used below. |
| 36 | * |
| 37 | * The basic algorithm was independently discovered as described in: |
| 38 | * "Algorithms for Approximate String Matching", E. Ukkonen, Information |
| 39 | * and Control Vol. 64, 1985, pp. 100-118. |
| 40 | * |
| 41 | * Unless the 'find_minimal' flag is set, this code uses the |
| 42 | * TOO_EXPENSIVE heuristic, by Paul Eggert, to limit the cost to |
| 43 | * O(N**1.5 log N) at the price of producing suboptimal output for large |
| 44 | * inputs with many differences. |
| 45 | */ |
| 46 | |
| 47 | #include <lib/fstrcmp.h> |
| 48 | |
| 49 | #include <lib/ac/string.h> |
| 50 | #include <lib/ac/stdio.h> |
| 51 | #include <lib/ac/stdlib.h> |
| 52 | #include <lib/ac/wchar.h> |
| 53 | #include <limits.h> |
| 54 | |
| 55 | #include <lib/minmax.h> |
| 56 | #include <lib/nmalloc.h> |
| 57 | |
| 58 | #ifndef uintptr_t |
| 59 | # define uintptr_t unsigned long |
| 60 | #endif |
| 61 | |
| 62 | |
| 63 | #define ELEMENT wchar_t |
| 64 | #define EQUAL(x,y) ((x) == (y)) |
| 65 | #define OFFSET_T ssize_t |
| 66 | #define OFFSET OFFSET_T |
| 67 | #define EXTRA_CONTEXT_FIELDS \ |
| 68 | /* The number of elements inserted or deleted. */ \ |
| 69 | size_t xvec_edit_count; \ |
| 70 | size_t yvec_edit_count; |
| 71 | #define NOTE_DELETE(ctxt, xoff) ctxt->xvec_edit_count++ |
| 72 | #define NOTE_INSERT(ctxt, yoff) ctxt->yvec_edit_count++ |
| 73 | /* |
| 74 | * We don't need USE_HEURISTIC, since it is unlikely in typical uses of |
| 75 | * fstrcmp(). |
| 76 | */ |
| 77 | #include <lib/diffseq.h> |
| 78 | |
| 79 | |
| 80 | /* |
| 81 | * Because fstrcmp is typically called multiple times, attempt to |
| 82 | * minimize the number of memory allocations performed. Thus, let a |
| 83 | * call reuse the memory already allocated by the previous call, if |
| 84 | * it is sufficient. |
| 85 | * |
| 86 | * This isn't thread safe. |
| 87 | */ |
| 88 | |
| 89 | static OFFSET_T *buffer; |
| 90 | static size_t bufmax; |
| 91 | |
| 92 | |
| 93 | double |
| 94 | fwcscmp(const wchar_t *wcs1, const wchar_t *wcs2) |
| 95 | { |
| 96 | struct context ctxt; |
| 97 | int i; |
| 98 | size_t fdiag_len; |
| 99 | size_t size1; |
| 100 | size_t size2; |
| 101 | |
| 102 | /* |
| 103 | * set the info for each string. |
| 104 | */ |
| 105 | ctxt.xvec = wcs1; |
| 106 | ctxt.yvec = wcs2; |
| 107 | |
| 108 | /* |
| 109 | * short-circuit obvious comparisons |
| 110 | */ |
| 111 | size1 = wcslen(wcs1); |
| 112 | size2 = wcslen(wcs2); |
| 113 | if (size1 == 0 && size2 == 0) |
| 114 | return 1.0; |
| 115 | if (size1 == 0 || size2 == 0) |
| 116 | return 0.0; |
| 117 | |
| 118 | /* |
| 119 | * Set TOO_EXPENSIVE to be approximate square root of input size, |
| 120 | * bounded below by 256. |
| 121 | */ |
| 122 | ctxt.too_expensive = 1; |
| 123 | for (i = size1 + size2; i != 0; i >>= 2) |
| 124 | ctxt.too_expensive <<= 1; |
| 125 | if (ctxt.too_expensive < 256) |
| 126 | ctxt.too_expensive = 256; |
| 127 | |
| 128 | /* |
| 129 | * Allocate memory for fdiag and bdiag. |
| 130 | */ |
| 131 | fdiag_len = size1 + size2 + 3; |
| 132 | if (fdiag_len > bufmax) |
| 133 | { |
| 134 | /* Need more memory. */ |
| 135 | bufmax = 2 * bufmax; |
| 136 | if (fdiag_len > bufmax) |
| 137 | bufmax = fdiag_len; |
| 138 | /* |
| 139 | * Calling realloc() would be a waste: buffer's contents do not |
| 140 | * need to be preserved. |
| 141 | */ |
| 142 | if (buffer != NULL) |
| 143 | free(buffer); |
| 144 | buffer = (OFFSET_T *)fstrcmp_nmalloc(bufmax, 2 * sizeof(OFFSET_T)); |
| 145 | if (!buffer) |
| 146 | return FSTRCMP_ERROR; |
| 147 | } |
| 148 | ctxt.fdiag = buffer + size2 + 1; |
| 149 | ctxt.bdiag = ctxt.fdiag + fdiag_len; |
| 150 | |
| 151 | /* |
| 152 | * Now do the main comparison algorithm |
| 153 | */ |
| 154 | ctxt.xvec_edit_count = 0; |
| 155 | ctxt.yvec_edit_count = 0; |
| 156 | compareseq(0, size1, 0, size2, 0, &ctxt); |
| 157 | |
| 158 | /* |
| 159 | * The result is |
| 160 | * |
| 161 | * ((number of chars in common) / (average length of the strings)). |
| 162 | * |
| 163 | * This is admittedly biased towards finding that the strings are |
| 164 | * similar, however it does produce meaningful results. |
| 165 | */ |
| 166 | return |
| 167 | ( |
| 168 | (size1 + size2 - ctxt.yvec_edit_count - ctxt.xvec_edit_count) |
| 169 | / |
| 170 | (double)(size1 + size2) |
| 171 | ); |
| 172 | } |