Refactor utf8-parsing routines for better reuse

in the buffer module