Documentation
¶
Overview ¶
Package fuzzy ranks candidate strings against a short query typed by a user.
Matching is subsequence-based: the query's runes must appear in the candidate in order but need not be adjacent, so "fwc" finds "forward-char". Scoring rewards matches that land at the start of a word and runs of adjacent runes, and charges for the gaps between them, so the ranking reflects how a reader would judge the match rather than merely whether one exists.
Ordering is total and stable, and pinned by test against a fixed candidate set: predictability is a feature here, because a menu that reorders for reasons the user cannot see is worse than one that ranks imperfectly.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Match ¶
type Match struct {
// Score is higher for a better match. It is comparable only between
// candidates scored against the same query.
Score int
// Indices are the RUNE offsets in the candidate that the query matched,
// strictly ascending, one per query rune. Callers emphasise exactly these
// positions, which is why they are rune offsets and not byte offsets.
Indices []int
}
Match describes how a query matched one candidate.
type Ranked ¶
Ranked is one candidate and how it matched.
func Rank ¶
Rank returns the matching candidates, best first.
The order is total: by score descending, then by shorter candidate, then by the candidate's position in the input. That last tie-break comes free from a stable sort over a slice built in input order - an explicit position map would cost an allocation per candidate on every keystroke for nothing.