Skip to main content

nearest_within

Function nearest_within 

Source
pub fn nearest_within<'a, I, S>(
    input: &str,
    candidates: I,
    max_distance: usize,
) -> Option<&'a str>
where I: IntoIterator<Item = S>, S: Into<&'a str>,
Expand description

Return the candidate nearest to input within max_distance edits.

Matching is case-insensitive. A candidate qualifies either by an Optimal String Alignment (restricted Damerau-Levenshtein) edit distance within max_distance — counting an adjacent transposition as a single edit — or, as a fallback, by being an abbreviation of inputinput (of at least two characters) is a subsequence of a candidate no more than four times its length, e.g. fmtformat. Among qualifying candidates the smallest score wins; ties break first toward a candidate whose leading character matches input’s, then lexicographically, so the result is deterministic regardless of iteration order. Returns None when no candidate is close enough, so a far-off token yields no misleading hint.

§Examples

use rskit_util::strings::nearest_within;
assert_eq!(nearest_within("buld", ["build", "check"], 2), Some("build"));
assert_eq!(nearest_within("teh", ["the", "then"], 1), Some("the"));
assert_eq!(nearest_within("xyz", ["build"], 2), None);