[whatwg] API for encoding/decoding ArrayBuffers into text

Jonas Sicking jonas at sicking.cc
Thu Mar 15 16:51:25 PDT 2012


On Wed, Mar 14, 2012 at 3:33 PM, Joshua Bell <jsbell at chromium.org> wrote:
> FYI, I've updated http://wiki.whatwg.org/wiki/StringEncoding

A few comments:

What's the use-case for the "stringLength" function? You can't decode
into an existing datastructure anyway, so you're ultimately forced to
call "decode" at which point the "stringLength" function hasn't helped
you.


Currently the use-case of simply wanting to convert a string to a
binary buffer is a bit cumbersome. You first have to call the
"encodedLength" function, then allocate a buffer of the right size,
then call the "encode" function. Could we add a function with
something like the following signature:

ArrayBufferView encode(DOMString value, optional DOMString encoding);


It doesn't seem possible to implement the 'encode' function without
doing multiple scans over the string. The implementation seems
required both to check that the data can be decoded using the
specified encoding, as well as check that the data will fit in the
passed in buffer. Only then can the implementation start decoding the
data. This seems problematic.


I also don't think it's a good idea to throw an exception for encoding
errors. Better to convert characters to the unicode replacement
character. I believe we made a similar change to the WebSockets
specification recently.

/ Jonas



More information about the whatwg mailing list