Add TruncateString to shorten a string without splitting a character #6
Reference in New Issue
Block a user
Delete Branch "clawbot/util:proposal-truncate-string"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Adds
TruncateString(s string, max int) string, which returns at mostmaxcharacters from the start of the string. A max of zero or less returns an empty
string, and a string already within the limit comes back unchanged.
This belongs here because every program that puts text into a fixed-width
column, a log line or a summary needs to shorten it, and the obvious way of
doing that,
s[:max], is wrong for any text that is not plain ASCII: it splitsa multi-byte character and produces invalid output. The correct version is
short but not obvious.
Things to know:
combining marks, or with characters that a terminal draws double width, will
still count as one character each, so this is not a way to fit text into a
fixed number of screen columns.
range, which yields the byteoffset of each character start, so nothing is allocated and no conversion to
a rune slice happens.
wants one can add it, and can decide whether it counts against the limit.
truncate.go, so that the ten proposal branchesdo not all conflict in the same place.
make teston this branch reports one failure,TestNowUnixMicro. That testalready fails on
masterand is unrelated to this change.Model: opus-5