levenshtein

package
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package levenshtein provides edit distance calculation and typo suggestions for commands and flags.

Performance & Allocation Invariant: ASCII string comparisons use a dedicated byte-based algorithm operating directly on string headers without converting to []byte, avoiding string-to-byte conversion allocations. For words <= 32 characters, stack arrays are used to eliminate heap allocations entirely. Do NOT unify distanceBytes and distanceRunes into a generic slice function; converting strings to []byte inside Suggest loops introduces heap allocations on every candidate comparison.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Distance

func Distance(source, target string) int

Distance computes the Levenshtein edit distance between source and target, measured in Unicode code points. ASCII inputs take a byte-based fast path; non-ASCII inputs are compared rune-by-rune.

func SuggestFor added in v0.9.0

func SuggestFor(input string, candidates []string) string

SuggestFor finds the best matching candidate for input, comparing case insensitively. A candidate qualifies when its edit distance is at most the threshold for the input's length, so every suggester (flags, commands, config keys, features, tags, predictors, groups) judges near misses the same way:

1 rune      0 (no suggestion short of a case-insensitive match)
2-3 runes   1
4+ runes    min(4, max(2, runes/3))

A transposition counts as two edits, so "prot" still suggests "port". The cap keeps a long input from accepting an unrelated candidate.

Types

This section is empty.

Jump to

Keyboard shortcuts

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