Easy string conversions for Typed Arrays




7 years ago
4 years ago


(Reporter: philikon, Unassigned)



Firefox Tracking Flags

(Not tracked)


When you're futzing with binary data using Typed Arrays, it's not uncommon that one may want to extract strings from that data. Right now one would have to manually construct this string in a for-loop using String.fromCharCode().

It would be nice if I could walk up to an ArrayBufferView and ask it it to give me a subset of its data as a string. In a similar fashion, it should also be able to set data in the ArrayBuffer by passing it a string:

    DOMString getString(long begin, optional long end);
    void setString(long offset, DOMString value);

The character interpretation would depend on the ArrayBufferView's type. If it's an Int8/Uint8Array, it'd be a regular C-type string, an Int16/Uint16Array would yield a nice JS-type UCS-2 type string. For Int32/Uint32Array I propose padding with two zero bytes, and the float arrays should raise an exception.

Alternatively, instead of adding this capability to ArrayBufferView, it could also be added to DataView (bug 575688). In that case I propose the following methods:

  DOMString getString(unsigned long byteOffset,
                      unsigned short bytesPerChar,
                      optional boolean littleEndian);

  void setString(unsigned long byteOffset,
                 DOMString value,
                 unsigned short bytesPerChar,
                 optional boolean littleEndian);

Given consensus over the API and whether this is actually a Good Idea(tm), I would be willing to give the implementation a shot. This would make some of the B2G low level code a bit more elegant and possibly also faster/less allocation hungry.

P.S.: Bug 687418 seems related.


4 years ago
Assignee: general → nobody
You need to log in before you can comment on or make changes to this bug.