package util // TruncateString returns at most max characters from the start of s. It counts // characters rather than bytes, so a multi-byte character is never cut in half // and the result is always valid text. A max of zero or less returns an empty // string, and a string already at or below the limit is returned unchanged. func TruncateString(s string, max int) string { if max <= 0 { return "" } count := 0 for offset := range s { if count == max { return s[:offset] } count++ } return s }