fuzzy

package
v0.16.0 Latest Latest
Warning

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

Go to latest
Published: Aug 25, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package fuzzy ranks candidates against what someone has typed.

Every character of the pattern has to appear in the candidate, in order, and the score depends on where. A match at the start of a word counts for more than one in the middle of one, characters that run together count for more than characters spread out, and a pattern typed in the same case as the candidate counts for a little more than one that was not.

It answers in byte offsets rather than character counts so callers can address the original UTF-8 string without converting between coordinate systems.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Match

type Match struct {
	// Score is meaningful only against other scores for the same pattern. Higher is
	// better, and a match is never worth less than one, so that "matched at all" and
	// "did not match" cannot be confused.
	Score int
	// At is the byte offset in the candidate of each character the pattern matched,
	// ascending. An offset can fall inside a grapheme cluster — a pattern character
	// can match a combining mark — so something highlighting the matches should ask
	// whether a cluster contains an offset rather than begins at one.
	At []int
}

Match is how well a candidate answered a pattern.

func Score

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

Score matches pattern against candidate, case-insensitively, and reports whether it matched at all. An empty pattern matches everything, with nothing highlighted.

What it does not do

It is not a full alignment search. Where a pattern character could bind to several places, this tries the first one and every one that begins a word, and keeps the best of those — which is what makes "st" find the "status" in "test_status" instead of settling for the "st" in "test". A placement that is better for some other reason, in a candidate with no word boundary to hint at it, can still be missed. That is a deliberate stopping point: candidates are palette-sized, patterns are short, and the alternative is a scoring matrix per candidate.

type Ranked

type Ranked struct {
	Index int
	Match Match
}

Ranked is one candidate that matched, named by its position in what was searched.

It is an index rather than the string so that a caller ranking its own items — files with a kind, commands with a description — gets back something it can look its own item up with, instead of having to match strings back up afterwards.

func Filter

func Filter(pattern string, candidates []string) []Ranked

Filter scores every candidate and returns those that matched, best first.

Candidates that score the same stay in the order they were given, so a caller that sorted its items meaningfully keeps that order for ties — which is where a caller's own idea of importance belongs.

Jump to

Keyboard shortcuts

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