5 releases

0.2.2 Feb 15, 2024
0.2.1 Feb 3, 2024
0.2.0 Feb 2, 2024
0.1.1 Feb 1, 2024
0.1.0 Feb 1, 2024

#1110 in Algorithms

40 downloads per month

MIT license

22KB
286 lines

Fuzzy string matching for code

crates.io docs.rs

Fuzzy string matching inspired by Visual Studio Code.

The fuzzy matching algorithm used in this crate is optimized for use cases such as command palettes, quick file navigation, and code searching. It does not use Levenshtein distance, which is more suited to use cases like spell checking.

The algorithm only allows matches where the characters in the query string are present and in the same order as the characters in the target string. All queries are substring queries, so it is not a major hit to the match score to search for a term in the middle of the target string. The algorithm prefers matches that are at the beginning of words in the target string, with words treated as they might appear in code (letters following a separator or in camel case are treated as a word). Sequential matches are also favored.

Example usage

let mut matcher = code_fuzzy_match::FuzzyMatcher::new();
let matches = matcher.fuzzy_match("the quick brown fox", "bro fox");
assert!(matches.is_some());
let no_match = matcher.fuzzy_match("the quick brown fox", "cat");
assert!(no_match.is_none());

let high_score = matcher.fuzzy_match("Example string", "example");
let lower_score = matcher.fuzzy_match("Example string", "str");
assert!(high_score.unwrap() > lower_score.unwrap());

No runtime deps