[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Public WebGL] Question about Strings from TypedArrays


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.

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