Crate sublime_fuzzy [] [src]

Fuzzy matching algorithm based on Sublime Text's string search. Iterates through characters of a search string and calculates a score based on matching consecutive/close groups of characters.

Walks all paths through the string that is being searched.

Usage

Basic usage:

use sublime_fuzzy::best_match;
 
let s = "some search thing";
let search = "something";
let result = best_match(search, s).unwrap();
 
// Output: score: 368
println!("score: {:?}", result.score());

Match.continuous_matches() returns a list of consecutive matches ((start_index, length)). Based on those the input string can be formatted. sublime_fuzzy provides a simple formatting function that wraps matches in tags.

use sublime_fuzzy::{best_match, format_simple};
 
let s = "some search thing";
let search = "something";
let result = best_match(search, s).unwrap();
 
// Output: <span>some</span> search <span>thing</span>
println!("formatted: {:?}", format_simple(&result, s, "<span>", "</span>"));

Adjust scoring:

use sublime_fuzzy::{FuzzySearch, ScoreConfig};
 
let mut search = FuzzySearch::new("something", "some search thing");
 
let config = ScoreConfig {
    bonus_consecutive: 20,
    penalty_distance: 8
};
// Weight consecutive matching chars less.
search.set_score_config(config);
 
println!("result: {:?}", search.best_match());

Note: This module removes any whitespace in the pattern ('something' in the examples above). It does not apply any other formatting. Lowercasing the inputs for example has to be done manually.

Structs

FuzzySearch

Container for search configuration. Allows for adjusting the factors used to calculate the match score.

Match

A single search result. Contains the calculated match score and all matches.

ScoreConfig

Functions

best_match

Returns the best match for search in target.

format_simple

Formats a Match by appending before before any matches and after after any matches.