[Public WebGL] Question about Strings from TypedArrays

Jeff Schiller [email protected]
Tue Feb 15 16:31:31 PST 2011


Hello,

I really liked the TypedArray spec.  Excited to see two browser vendors
beginning to support it.

I'd like to resurrect the discussions around conversion of Strings from/to
TypedArrays [1].  I take it from internal discussions with Kenneth that
nothing has happened yet on this front.  Caveat upfront: I am not a browser
developer, I'm a front-end web developer.

Background: I was revisiting my implementation of unzip/inflate in
JavaScript [2] and decided to investigate using TypedArrays instead of
binary strings.  However, I need some way to extract strings from chunks of
bytes.  In my code, I wanted to avoid looping through each byte, using
String.fromCharCode() on it and appending to a JS String for some chunk of
bytes.

I found out the following hack works in Firefox 4:

  var bb = new Uint8Array(new ArrayBuffer(10));
  bb[2] = 65;
  bb[3] = 66;
  bb[4] = 67;
  String.fromCharCode.apply(null, bb.subarray(2,5))

Output: "ABC"

But this doesn't work in Chrome, unfortunately.  I'm not sure which browser
is following the letter of the ECMAScript spec there.

What do you folks think of something as straightforward as this:

  String.fromTypedArray(aTypedArray, "utf-8");

which would return a JavaScript string with exceptions being thrown for
invalid encodings, etc.  For the reverse conversion we could add another
constructor that takes a string and an encoding in.

Thanks,
Jeff Schiller

[1]
https://www.khronos.org/webgl/public-mailing-list/archives/1006/msg00191.html
[2] http://code.google.com/p/kthoom/source/browse/unzip.js
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://khronos.org/pipermail/public_webgl_khronos.org/attachments/20110215/2734f375/attachment.html>


More information about the public_webgl mailing list