Cutting a string with a plain byte slice such as s[:80] will split a
multi-byte character in half and produce invalid text, which shows up as a
replacement character in logs, database columns and terminal output. A short
function that counts characters instead of bytes makes the safe version the
easy one.
Definition of done: TruncateString(s string, max int) string returns at most
max characters from the start of s, never cutting a multi-byte character, and
returns an empty string when max is zero or less. It has a doc comment and
table-driven tests covering a string shorter than the limit, a string exactly
at the limit, a string longer than the limit, a string of multi-byte
characters, a max of zero, and a negative max.
Model: opus-5
Cutting a string with a plain byte slice such as `s[:80]` will split a
multi-byte character in half and produce invalid text, which shows up as a
replacement character in logs, database columns and terminal output. A short
function that counts characters instead of bytes makes the safe version the
easy one.
Definition of done: `TruncateString(s string, max int) string` returns at most
max characters from the start of s, never cutting a multi-byte character, and
returns an empty string when max is zero or less. It has a doc comment and
table-driven tests covering a string shorter than the limit, a string exactly
at the limit, a string longer than the limit, a string of multi-byte
characters, a max of zero, and a negative max.
Model: opus-5
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.
Cutting a string with a plain byte slice such as
s[:80]will split amulti-byte character in half and produce invalid text, which shows up as a
replacement character in logs, database columns and terminal output. A short
function that counts characters instead of bytes makes the safe version the
easy one.
Definition of done:
TruncateString(s string, max int) stringreturns at mostmax characters from the start of s, never cutting a multi-byte character, and
returns an empty string when max is zero or less. It has a doc comment and
table-driven tests covering a string shorter than the limit, a string exactly
at the limit, a string longer than the limit, a string of multi-byte
characters, a max of zero, and a negative max.
Model: opus-5