[whatwg] API for encoding/decoding ArrayBuffers into text

2012/3/22 Anne van Kesteren <annevk at opera.com>:
> 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)

Usually, strings are encoded to bytes.
Therefore that encode/decode methods should be reversed like:

?enc = new Encoding("gb18030")
?bytes1 ?= enc.encode(string1)
?string2 = enc.decode(bytes2)

Or if it may cause confusion use getBytes/getChars like Java and C#.
http://docs.oracle.com/javase/7/docs/api/java/lang/String.html
http://msdn.microsoft.com/en-us/library/system.text.encoder(v=vs.110).aspx#Y1873
http://msdn.microsoft.com/en-us/library/system.text.Decoder(v=vs.110).aspx#Y1873

?enc = new Encoding("gb18030")
?bytes1 ?= enc.getBytes(string1)
?string2 = enc.getChars(bytes2)

-- 
NARUSE, Yui ?<naruse at airemix.jp>

Received on Thursday, 22 March 2012 07:37:24 UTC