truncateUtf8 function

String truncateUtf8(
  1. String text,
  2. int maxBytes
)

Safely truncates text to at most maxBytes bytes in UTF-8 encoding without splitting multi-byte characters or Unicode code points.

Implementation

String truncateUtf8(String text, int maxBytes) {
  if (maxBytes <= 0) return text;
  final bytes = utf8.encode(text);
  if (bytes.length <= maxBytes) return text;

  int cutIndex = maxBytes;
  if ((bytes[cutIndex] & 0xC0) == 0x80) {
    while (cutIndex > 0 && (bytes[cutIndex] & 0xC0) == 0x80) {
      cutIndex--;
    }
  }
  return utf8.decode(bytes.sublist(0, cutIndex), allowMalformed: true);
}