Restrict suggestions to threshold, like gcc does.

Signed-off-by: Pablo Neira Ayuso <pa...@netfilter.org>
---
 src/misspell.c | 21 ++++++++++++++++++---
 1 file changed, 18 insertions(+), 3 deletions(-)

diff --git a/src/misspell.c b/src/misspell.c
index 922d305d5e01..059d2e20de7a 100644
--- a/src/misspell.c
+++ b/src/misspell.c
@@ -78,11 +78,26 @@ void string_misspell_init(struct string_misspell_state *st)
 int string_misspell_update(const char *a, const char *b,
                           void *obj, struct string_misspell_state *st)
 {
-       unsigned int distance;
+       unsigned int len_a, len_b, max_len, min_len, distance, threshold;
 
-       distance = string_distance(a, b);
+       len_a = strlen(a);
+       len_b = strlen(b);
+
+       max_len = max(len_a, len_b);
+       min_len = min(len_a, len_b);
+
+       if (max_len <= 1)
+               return 0;
 
-       if (distance < st->min_distance) {
+       if (max_len - min_len <= 1)
+               threshold = max(div_round_up(max_len, 3), 1);
+       else
+               threshold = div_round_up(max_len + 2, 3);
+
+       distance = string_distance(a, b);
+       if (distance > threshold)
+               return 0;
+       else if (distance < st->min_distance) {
                st->min_distance = distance;
                st->obj = obj;
                return 1;
-- 
2.11.0


Reply via email to