Documentation
¶
Overview ¶
Package textutil holds small, dependency-free string helpers that every renderer needs and none should reimplement: truncation that never splits a UTF-8 sequence.
Byte slicing (s[:n]) cuts wherever n lands, which in a multibyte response body or a CJK title leaves half a rune at the end. JSON encoders then substitute U+FFFD, SARIF validators reject the file, and terminals print garbage. Every "cap this string" site should go through here instead.
Index ¶
- func Closest(typed string, candidates []string) string
- func CutIndex[T ~string | ~[]byte](s T, maxBytes int) int
- func Levenshtein(a, b string) int
- func SuggestionBudget(typed string) int
- func TruncateRunes(s string, maxRunes int) string
- func TruncateRunesEllipsis(s string, maxRunes int, ellipsis string) string
- func TruncateUTF8(s string, maxBytes int) string
- func TruncateUTF8Ellipsis(s string, maxBytes int, ellipsis string) string
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Closest ¶ added in v0.5.7
Closest is the candidate nearest typed (by Levenshtein distance) within SuggestionBudget, "" when none is: the "did you mean" for a mistyped name. Ties go to the first candidate, so the hint is stable when candidates come in a fixed order.
func CutIndex ¶
CutIndex returns the largest index i <= maxBytes such that s[:i] does not end in the middle of a multibyte UTF-8 sequence that starts before i. It returns len(s) when s already fits and 0 for a non-positive maxBytes.
It only moves the cut; it never rewrites bytes, so callers holding binary or non-UTF-8 data (an HTTP body) keep it byte-exact up to the cut.
func Levenshtein ¶
Levenshtein is the classic edit distance between a and b, by rune, computed with a two-row rolling buffer.
func SuggestionBudget ¶ added in v0.5.7
SuggestionBudget is the largest edit distance still considered a typo of typed. One edit for a short name, widening to a third of the name's length for longer ones, where the same distance leaves far more of the word intact.
func TruncateRunes ¶
TruncateRunes returns at most maxRunes runes of s, as valid UTF-8 (invalid bytes count as one rune each and are replaced with U+FFFD). Use it for character budgets — a chat-message limit, a title cap — where the bound is in characters rather than bytes.
func TruncateRunesEllipsis ¶
TruncateRunesEllipsis caps s at maxRunes runes in total, replacing the tail with ellipsis when it had to cut. When maxRunes cannot hold the ellipsis the plain truncation is returned.
func TruncateUTF8 ¶
TruncateUTF8 returns the longest prefix of s that is at most maxBytes long and does not split a UTF-8 sequence. The result is always valid UTF-8: invalid bytes inside the kept prefix are replaced with U+FFFD, dropping trailing runes as needed so the length bound still holds. A string that fits and is already valid is returned unchanged (no allocation).
func TruncateUTF8Ellipsis ¶
TruncateUTF8Ellipsis caps s at maxBytes bytes in total, replacing the tail with ellipsis when it had to cut. When maxBytes cannot hold the ellipsis the plain truncation is returned, so the bound is never exceeded.
Types ¶
This section is empty.