diff options
author | Pablo Neira Ayuso <pablo@netfilter.org> | 2018-11-30 18:04:14 +0100 |
---|---|---|
committer | Pablo Neira Ayuso <pablo@netfilter.org> | 2018-11-30 19:10:51 +0100 |
commit | 0544632e14b993bed8f1ce4e3ad764f90740f96a (patch) | |
tree | 5abdcca773ad64976a8fee8ae0cd880b37bae74a /src/misspell.c | |
parent | 7dbbdc150292fa9076ce8e3b2c6a8937214d03e0 (diff) |
misspell: add distance threshold for suggestions
Restrict suggestions to threshold, like gcc does.
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
Diffstat (limited to 'src/misspell.c')
-rw-r--r-- | src/misspell.c | 21 |
1 files changed, 18 insertions, 3 deletions
diff --git a/src/misspell.c b/src/misspell.c index 23290819..6536d755 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; |