[whatwg] API for encoding/decoding ArrayBuffers into text

Mark Callow callow_mark at hicorp.co.jp
Wed Mar 21 19:12:25 PDT 2012


On 22/03/2012 04:42, Anne van Kesteren 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
>
> And similarly you would have
>
>   dec = new Decoder("shift_jis")
>   bytes = dec.decode(string)
>
> Or alternatively you could have a single object that exposes both
> encode() and decode() and tracks state for both:
>
>   enc = new Encoding("gb18030")
>   bytes1  = enc.decode(string1)
>   string2 = enc.encode(bytes2)

This has encode and decode reversed from my understanding. I regard the
string (wide-char) as the canonical form and the bytes as the encoded
form. This view is reflected in the widely used terminology "charset
encodings" which refers to the likes of euc-kr and shift_jis.

Regards

    -Mark



More information about the whatwg mailing list