fuzzy

package
v0.7.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 27, 2026 License: MIT Imports: 4 Imported by: 0

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.

func Score

func Score(query, candidate string) (Match, bool)

Score reports how well query matches candidate, and whether it matches at all.

An empty query matches every candidate with score 0 and no indices, so a prompt showing everything before the user types needs no special case.

type Ranked

type Ranked struct {
	Candidate string
	Match     Match
}

Ranked is one candidate and how it matched.

func Rank

func Rank(query string, candidates []string) []Ranked

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL