[whatwg] API for encoding/decoding ArrayBuffers into text
Glenn Maynard
glenn at zewt.org
Thu Mar 22 06:47:05 PDT 2012
On Wed, Mar 21, 2012 at 2:42 PM, Anne van Kesteren <annevk at opera.com> wrote:
> As for the API, how about:
>
> enc = new Encoder("euc-kr")
> string1 = enc.encode(bytes1)
> string2 = enc.encode(bytes2)
> string3 = enc.eof() // might return empty string if all is fine
>
A problem with this is that the bugs resulting from not calling eof() are
subtle. The only thing eof() would ever do, I think, is return U+FFFD
characters if there are leftover characters in the internal buffer; if you
never call eof(), you'll never get incorrect results unless you test with
invalid inputs.
It's minor, as subtle-edge-cases-that-people-won't-get-right go, but it's
at least worth a mention. Maybe people who would use this API instead of
the simpler non-streaming version (which could be a thin wrapper on this)
in the first place are also more likely to get this right.
I'm guessing a common, incorrect pattern would be:
string = new Encoder("euc-kr").encode(bytes);
which would *not* be equivalent to bytes.encode("euc-kr").
--
Glenn Maynard
More information about the whatwg
mailing list