shapeString function
Trims s and truncates to at most maxLength characters with optional
ellipsis. The result never exceeds maxLength.
Audited: 2026-06-12 11:26 EDT
Implementation
String shapeString(String s, {int? maxLength, String ellipsis = '...'}) {
String out = s.trim();
if (maxLength != null && out.length > maxLength) {
if (maxLength <= ellipsis.length) {
// No room for the ellipsis within the budget; hard-truncate so the result
// still fits maxLength (the old replaceRange could return '...' for
// maxLength 2, exceeding the requested limit).
out = out.substring(0, maxLength < 0 ? 0 : maxLength);
} else {
out = out.substring(0, maxLength - ellipsis.length) + ellipsis;
}
}
return out;
}