Selftest framework
[official-gcc.git] / gcc / spellcheck.c
blobceb60168bf167ea00759ead506e5dbf395017670
1 /* Find near-matches for strings.
2 Copyright (C) 2015-2016 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
24 #include "tree.h"
25 #include "spellcheck.h"
26 #include "selftest.h"
28 /* The Levenshtein distance is an "edit-distance": the minimal
29 number of one-character insertions, removals or substitutions
30 that are needed to change one string into another.
32 This implementation uses the Wagner-Fischer algorithm. */
34 edit_distance_t
35 levenshtein_distance (const char *s, int len_s,
36 const char *t, int len_t)
38 const bool debug = false;
40 if (debug)
42 printf ("s: \"%s\" (len_s=%i)\n", s, len_s);
43 printf ("t: \"%s\" (len_t=%i)\n", t, len_t);
46 if (len_s == 0)
47 return len_t;
48 if (len_t == 0)
49 return len_s;
51 /* We effectively build a matrix where each (i, j) contains the
52 Levenshtein distance between the prefix strings s[0:j]
53 and t[0:i].
54 Rather than actually build an (len_t + 1) * (len_s + 1) matrix,
55 we simply keep track of the last row, v0 and a new row, v1,
56 which avoids an (len_t + 1) * (len_s + 1) allocation and memory accesses
57 in favor of two (len_s + 1) allocations. These could potentially be
58 statically-allocated if we impose a maximum length on the
59 strings of interest. */
60 edit_distance_t *v0 = new edit_distance_t[len_s + 1];
61 edit_distance_t *v1 = new edit_distance_t[len_s + 1];
63 /* The first row is for the case of an empty target string, which
64 we can reach by deleting every character in the source string. */
65 for (int i = 0; i < len_s + 1; i++)
66 v0[i] = i;
68 /* Build successive rows. */
69 for (int i = 0; i < len_t; i++)
71 if (debug)
73 printf ("i:%i v0 = ", i);
74 for (int j = 0; j < len_s + 1; j++)
75 printf ("%i ", v0[j]);
76 printf ("\n");
79 /* The initial column is for the case of an empty source string; we
80 can reach prefixes of the target string of length i
81 by inserting i characters. */
82 v1[0] = i + 1;
84 /* Build the rest of the row by considering neighbors to
85 the north, west and northwest. */
86 for (int j = 0; j < len_s; j++)
88 edit_distance_t cost = (s[j] == t[i] ? 0 : 1);
89 edit_distance_t deletion = v1[j] + 1;
90 edit_distance_t insertion = v0[j + 1] + 1;
91 edit_distance_t substitution = v0[j] + cost;
92 edit_distance_t cheapest = MIN (deletion, insertion);
93 cheapest = MIN (cheapest, substitution);
94 v1[j + 1] = cheapest;
97 /* Prepare to move on to next row. */
98 for (int j = 0; j < len_s + 1; j++)
99 v0[j] = v1[j];
102 if (debug)
104 printf ("final v1 = ");
105 for (int j = 0; j < len_s + 1; j++)
106 printf ("%i ", v1[j]);
107 printf ("\n");
110 edit_distance_t result = v1[len_s];
111 delete[] v0;
112 delete[] v1;
113 return result;
116 /* Calculate Levenshtein distance between two nil-terminated strings. */
118 edit_distance_t
119 levenshtein_distance (const char *s, const char *t)
121 return levenshtein_distance (s, strlen (s), t, strlen (t));
124 /* Given TARGET, a non-NULL string, and CANDIDATES, a non-NULL ptr to
125 an autovec of non-NULL strings, determine which element within
126 CANDIDATES has the lowest edit distance to TARGET. If there are
127 multiple elements with the same minimal distance, the first in the
128 vector wins.
130 If more than half of the letters were misspelled, the suggestion is
131 likely to be meaningless, so return NULL for this case. */
133 const char *
134 find_closest_string (const char *target,
135 const auto_vec<const char *> *candidates)
137 gcc_assert (target);
138 gcc_assert (candidates);
140 int i;
141 const char *candidate;
142 const char *best_candidate = NULL;
143 edit_distance_t best_distance = MAX_EDIT_DISTANCE;
144 size_t len_target = strlen (target);
145 FOR_EACH_VEC_ELT (*candidates, i, candidate)
147 gcc_assert (candidate);
148 edit_distance_t dist
149 = levenshtein_distance (target, len_target,
150 candidate, strlen (candidate));
151 if (dist < best_distance)
153 best_distance = dist;
154 best_candidate = candidate;
158 /* If more than half of the letters were misspelled, the suggestion is
159 likely to be meaningless. */
160 if (best_candidate)
162 unsigned int cutoff = MAX (len_target, strlen (best_candidate)) / 2;
163 if (best_distance > cutoff)
164 return NULL;
167 return best_candidate;
170 #if CHECKING_P
172 namespace selftest {
174 /* Selftests. */
176 /* Verify that the levenshtein_distance (A, B) equals the expected
177 value. */
179 static void
180 levenshtein_distance_unit_test_oneway (const char *a, const char *b,
181 edit_distance_t expected)
183 edit_distance_t actual = levenshtein_distance (a, b);
184 ASSERT_EQ (actual, expected);
187 /* Verify that both
188 levenshtein_distance (A, B)
190 levenshtein_distance (B, A)
191 equal the expected value, to ensure that the function is symmetric. */
193 static void
194 levenshtein_distance_unit_test (const char *a, const char *b,
195 edit_distance_t expected)
197 levenshtein_distance_unit_test_oneway (a, b, expected);
198 levenshtein_distance_unit_test_oneway (b, a, expected);
201 /* Verify levenshtein_distance for a variety of pairs of pre-canned
202 inputs, comparing against known-good values. */
204 void
205 spellcheck_c_tests ()
207 levenshtein_distance_unit_test ("", "nonempty", strlen ("nonempty"));
208 levenshtein_distance_unit_test ("saturday", "sunday", 3);
209 levenshtein_distance_unit_test ("foo", "m_foo", 2);
210 levenshtein_distance_unit_test ("hello_world", "HelloWorld", 3);
211 levenshtein_distance_unit_test
212 ("the quick brown fox jumps over the lazy dog", "dog", 40);
213 levenshtein_distance_unit_test
214 ("the quick brown fox jumps over the lazy dog",
215 "the quick brown dog jumps over the lazy fox",
217 levenshtein_distance_unit_test
218 ("Lorem ipsum dolor sit amet, consectetur adipiscing elit,",
219 "All your base are belong to us",
220 44);
223 } // namespace selftest
225 #endif /* #if CHECKING_P */