W3C home > Mailing lists > Public > whatwg@whatwg.org > March 2012

[whatwg] API for encoding/decoding ArrayBuffers into text

From: James Graham <jgraham@opera.com>
Date: Thu, 22 Mar 2012 10:53:36 +0100
Message-ID: <4F6AF6A0.7050402@opera.com>
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)
Received on Thursday, 22 March 2012 02:53:36 UTC

This archive was generated by hypermail 2.4.0 : Wednesday, 22 January 2020 16:59:40 UTC