Input was getting mangled when it was in UTF-8

If your input is in UTF-8 it got truncated because it looks like the code assumed to be given a length in bytes, not in number of characters.