Fuzzy matching algorithm based on Sublime Text's string search. Iterates through characters of a search string and calculates a score.
The score is based on several factors:
t
in some_thing
get a bonus (bonus_word_start
)bonus_consecutive
)T
-> T
instead of t
-> T
) in case of a case-insensitive search get a bonus (bonus_match_case
)penalty_distance
penalty and subtracted from the scoreThe default scoring is configured to give a lot of weight to word starts. So a pattern scc
will match
SoccerCartoonController, not SoccerCartoonController.
With default weighting.
| Pattern | Target string | Result |
| ----------- | ------------------------- | ----------------------------------- |
| scc
| SoccerCartoonController
| SoccerCartoonController |
| something
| some search thing
| some search thing |
Basic usage:
```rust use sublimefuzzy::bestmatch;
let result = best_match("something", "some search thing");
assert!(result.is_some()); ```
Match::continuous_matches
returns an iter of consecutive matches. Based on those the input
string can be formatted.
format_simple
provides a simple formatting that wraps matches in tags:
```rust use sublimefuzzy::{bestmatch, format_simple};
let target = "some search thing";
let result = best_match("something", target).unwrap();
asserteq!( formatsimple(&result, target, "", ""), "some search thing" ); ```
The weighting of the different factors can be adjusted:
```rust use sublime_fuzzy::{FuzzySearch, Scoring};
// Or pick from one of the provided Scoring::...
methods like emphasize_word_starts
let scoring = Scoring {
bonusconsecutive: 128,
bonusword_start: 0,
..Scoring::default()
};
let result = FuzzySearch::new("something", "some search thing") .casesensitive() .scorewith(&scoring) .best_match();
assert!(result.is_some()) ```
Note: Any whitespace in the pattern ('something'
in the examples above) will be removed.
Check out the documentation at docs.rs.