textutil

package
v0.5.7 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2026 License: MIT Imports: 2 Imported by: 1 module, 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

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func Closest added in v0.5.7

func Closest(typed string, candidates []string) string

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

func CutIndex[T ~string | ~[]byte](s T, maxBytes int) int

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

func Levenshtein(a, b string) int

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

func SuggestionBudget(typed string) int

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

func TruncateRunes(s string, maxRunes int) string

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

func TruncateRunesEllipsis(s string, maxRunes int, ellipsis string) string

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

func TruncateUTF8(s string, maxBytes int) string

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

func TruncateUTF8Ellipsis(s string, maxBytes int, ellipsis string) string

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.

Jump to

Keyboard shortcuts

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