We might be able to pre-compute the size of this buffer and use a string instead, although at the cost of some temp data structure recording the work to be done.  Would need to benchmark to find out if worthwhile.