Documentation
¶
Index ¶
- func CountLines(s string) int
- func DefaultEditThreshold(query, target string) int
- func FormatDidYouMean(suggestions []string) string
- func Levenshtein(a, b string) int
- func RankBy[T any](items []T, limit int, key func(T) string, ...) (ranked []T, total int)
- func RankMatch(query, target string, maxDist int) (tier, score int, ok bool)
- func SuggestClosestStrings(query string, targets []string, limit int) []string
- func TopClosestIDs(matches []ClosestMatch, limit int) []string
- func TruncateBytes(s string, maxBytes int) (string, bool)
- func TruncateHead(s string, maxLines, maxBytes int) (string, bool)
- func TruncateLine(s string, maxBytes int, suffix string) (string, bool)
- type BestRank
- type ClosestMatch
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CountLines ¶
CountLines returns the number of lines in s (1-based for non-empty strings, ignoring a single trailing newline).
func DefaultEditThreshold ¶
DefaultEditThreshold returns a conservative maximum Levenshtein edit distance for typo recovery between query and target (one-third of the shorter string's rune length, with a floor of 1 for non-empty inputs).
func FormatDidYouMean ¶
FormatDidYouMean formats ranked suggestions as " (did you mean \"a\", \"b\"?)" or returns "" when suggestions is empty.
func Levenshtein ¶
Levenshtein returns the rune-wise Levenshtein edit distance between a and b.
func RankBy ¶
func RankBy[T any](items []T, limit int, key func(T) string, match func(T) (tier, score int, ok bool)) (ranked []T, total int)
RankBy filters items to those for which match returns ok == true and orders them by ascending (tier, score, key(item)). If limit > 0, the returned slice is capped to limit elements; total always reports the full match count prior to truncation.
func RankMatch ¶
RankMatch evaluates how closely target matches query (case-insensitively), classifying matches across three tiers:
- Tier 0: exact equality (score 0)
- Tier 1: target contains query as a substring (score = Levenshtein(q, t))
- Tier 2: bounded edit distance <= maxDist between comparably sized strings (score = Levenshtein(q, t)); pass maxDist < 0 to disable edit-distance matching.
func SuggestClosestStrings ¶
SuggestClosestStrings returns up to limit targets matching query using case-insensitive exact, substring, and bounded Levenshtein matching.
func TopClosestIDs ¶
func TopClosestIDs(matches []ClosestMatch, limit int) []string
TopClosestIDs deduplicates matches by ID (keeping each ID's lowest Tier and Score), orders them by ascending (Tier, Score, ID), and returns up to limit IDs.
func TruncateBytes ¶
TruncateBytes truncates s to at most maxBytes without splitting multi-byte UTF-8 runes. It returns the (possibly truncated) string and true if s was truncated.
func TruncateHead ¶
TruncateHead truncates multi-line text s so that it contains at most maxLines lines and at most maxBytes bytes (UTF-8 safe). If maxLines <= 0, line count is not bounded; if maxBytes <= 0, byte count is not bounded. Trailing CR/LF characters are trimmed when truncation occurs.
Types ¶
type BestRank ¶
type BestRank struct {
// contains filtered or unexported fields
}
BestRank accumulates the best (lowest Tier, then lowest Score) fuzzy match across multiple candidate fields of a single item.
type ClosestMatch ¶
ClosestMatch represents a ranked candidate identifier returned by fuzzy matching. Lower Tier and lower Score represent closer matches (Tier 0 = exact, 1 = substring, 2 = bounded edit distance).