pub(crate) fn suggest(word: &str, candidates: &[&'static str]) -> Option<&'static str> {
let has_letter = |s: &str| s.bytes().any(|b| b.is_ascii_alphabetic());
if !has_letter(word) {
return None;
}
let word = word.to_ascii_lowercase();
let length = word.chars().count();
let max_distance = length.max(3) / 3;
candidates
.iter()
.filter(|candidate| has_letter(candidate))
.filter(|candidate| candidate.chars().count().abs_diff(length) <= max_distance)
.map(|&candidate| (distance(&word, &candidate.to_ascii_lowercase()), candidate))
.filter(|&(d, _)| (1..=max_distance).contains(&d))
.min_by_key(|&(d, _)| d)
.map(|(_, candidate)| candidate)
}
fn distance(a: &str, b: &str) -> usize {
#[cfg(test)]
tests::COMPARISONS.with(|count| count.set(count.get() + 1));
let a: Vec<char> = a.chars().collect();
let b: Vec<char> = b.chars().collect();
let mut d = vec![vec![0; b.len() + 1]; a.len() + 1];
for (i, row) in d.iter_mut().enumerate() {
row[0] = i;
}
for (j, cell) in d[0].iter_mut().enumerate() {
*cell = j;
}
for i in 1..=a.len() {
for j in 1..=b.len() {
let cost = usize::from(a[i - 1] != b[j - 1]);
let mut best = (d[i - 1][j] + 1)
.min(d[i][j - 1] + 1)
.min(d[i - 1][j - 1] + cost);
if i > 1 && j > 1 && a[i - 1] == b[j - 2] && a[i - 2] == b[j - 1] {
best = best.min(d[i - 2][j - 2] + 1);
}
d[i][j] = best;
}
}
d[a.len()][b.len()]
}
#[cfg(test)]
mod tests {
use super::*;
use assert2::check;
use std::cell::Cell;
thread_local! {
pub(super) static COMPARISONS: Cell<usize> = const { Cell::new(0) };
}
const SECTIONS: &[&str] = &[":predicates", ":functions", ":constants", ":action"];
#[test]
fn distance_counts_edits() {
check!(distance("kitten", "sitting") == 3);
check!(distance("", "abc") == 3);
check!(distance("abc", "abc") == 0);
check!(distance("strat", "start") == 1);
}
#[test]
fn close_words_are_suggested() {
check!(suggest(":predicats", SECTIONS) == Some(":predicates"));
check!(suggest(":PREDICATS", SECTIONS) == Some(":predicates"));
check!(suggest(":actoin", SECTIONS) == Some(":action"));
check!(suggest("strat", &["start", "end"]) == Some("start"));
}
#[test]
fn distant_words_exact_matches_and_operators_are_not() {
check!(suggest(":bogus", SECTIONS) == None);
check!(suggest(":action", SECTIONS) == None);
check!(suggest("x", &["*", "+"]) == None);
check!(suggest("<", &["<=", ">="]) == None);
}
#[test]
fn a_long_word_is_not_compared() {
let word = "x".repeat(1_000_000);
COMPARISONS.with(|count| count.set(0));
check!(suggest(&word, SECTIONS) == None);
check!(COMPARISONS.with(Cell::get) == 0);
}
}