textutil

package
v0.5.4 Latest Latest
Warning

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

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

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