[whatwg] API for encoding/decoding ArrayBuffers into text

James Graham jgraham at opera.com
Thu Mar 22 02:53:36 PDT 2012


On 03/21/2012 04:53 PM, Joshua Bell 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)
>

I don't mind this API for complex usecases e.g. streaming, but it is 
massive overkill for the simple common case of "I have a list of bytes 
that I want to decode to a string" or "I have a string that I want to 
encode to bytes". For those cases I strongly prefer the earlier API 
along the lines of

String.prototype.encode(encoding)
ArrayBufferView.prototype.decode(encoding)



More information about the whatwg mailing list