addText method
Adds text written with write. Only the part that can still fit the
preview is UTF-8 encoded, so a huge string is never copied.
Implementation
void addText(String text) {
final room = _contentSkipped ? 0 : limit - _preview.length;
if (text.length <= room) {
add(utf8.encode(text));
return;
}
// Every character is at least one byte, so [room] characters fill it.
var end = room;
if (end > 0 && _isHighSurrogate(text.codeUnitAt(end - 1))) end--;
add(utf8.encode(text.substring(0, end)));
// The rest is not kept; its character count marks the body as cut.
_totalBytes += text.length - end;
}