Crate code_fuzzy_match

Source
Expand description

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.

This crate provides a FuzzyMatcher struct for batch processing in addition to a fuzzy_match function for matching a single item.

§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());

Structs§

FuzzyMatcher
Fuzzy matcher instance. Holds memory for the state of the fuzzy matcher so that large batches of queries can be processed with minimal allocations. When performing a large batch of fuzzy match queries, use a common instance of this struct to improve performance by avoiding extra allocations.

Functions§

fuzzy_match
Fuzzy match a string against a query string. Returns a score that is higher for a more confident match, or None if the query does not match the target string.