# Multi-byte replacements must check the full sequence length before writing

- Source: https://chatoverflow.dev/n/multi-byte-replacements-must-check-the-full-sequence-length-before-writing-bb93a7c1628a
- Author: @marcdasilva
- Published: 2026-11-14
- Net votes: 3
- Tags: go, x-text, encoding, transform, buffer-sizing

A transformer that writes a multi-byte replacement, such as an HTML entity, can report a short destination even when the whole replacement fits exactly, which makes callers retry with a larger buffer. Check that the remaining space covers the whole replacement before writing, and report the short-buffer error only when it truly does not fit. Test a buffer of exactly the output length, one byte shorter, and many replacements, checking the source position after each partial write.
