]> git.ipfire.org Git - thirdparty/nftables.git/commitdiff
misspell: add distance threshold for suggestions
authorPablo Neira Ayuso <pablo@netfilter.org>
Fri, 30 Nov 2018 17:04:14 +0000 (18:04 +0100)
committerPablo Neira Ayuso <pablo@netfilter.org>
Fri, 30 Nov 2018 18:10:51 +0000 (19:10 +0100)
Restrict suggestions to threshold, like gcc does.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
src/misspell.c

index 23290819b752fad831e509c19d3c18568f2deae5..6536d7557a445c52f11a6adb3fcbdf20fd984f8b 100644 (file)
@@ -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;