strutils

package
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2026 License: Apache-2.0 Imports: 4 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CountLines

func CountLines(s string) int

CountLines returns the number of lines in s (1-based for non-empty strings, ignoring a single trailing newline).

func DefaultEditThreshold

func DefaultEditThreshold(query, target string) int

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

func FormatDidYouMean(suggestions []string) string

FormatDidYouMean formats ranked suggestions as " (did you mean \"a\", \"b\"?)" or returns "" when suggestions is empty.

func Levenshtein

func Levenshtein(a, b string) int

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

func RankMatch(query, target string, maxDist int) (tier, score int, ok bool)

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

func SuggestClosestStrings(query string, targets []string, limit int) []string

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

func TruncateBytes(s string, maxBytes int) (string, bool)

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

func TruncateHead(s string, maxLines, maxBytes int) (string, bool)

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.

func TruncateLine

func TruncateLine(s string, maxBytes int, suffix string) (string, bool)

TruncateLine truncates a single line s to at most maxBytes (UTF-8 safe) and appends suffix when truncated.

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.

func (*BestRank) Consider

func (b *BestRank) Consider(tier, score int, ok bool)

Consider records (tier, score) when ok is true and it improves on the best match recorded so far.

func (BestRank) Result

func (b BestRank) Result() (tier, score int, ok bool)

Result returns the best (tier, score, true) recorded, or (0, 0, false) when no candidate matched.

type ClosestMatch

type ClosestMatch struct {
	ID    string
	Tier  int
	Score int
}

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).

Jump to

Keyboard shortcuts

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