Skip to content

Instantly share code, notes, and snippets.

@fbstj
Created January 7, 2012 01:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fbstj/1573377 to your computer and use it in GitHub Desktop.
Save fbstj/1573377 to your computer and use it in GitHub Desktop.
some extension methods and utility functions for ArrayBuffer
var Buffer = {
_for: function(n, cb){ for(var i = 0; i < n; i++) cb(i) },
_to: function(x, size, cb) {
var v = new DataView(x), out = [], l = x.byteLength/size
this._for(l, function (i) { out[i] = v[cb](i*size) })
return out
},
toBytes: function(b) { return this._to(b, 1, 'getUint8') },
toWords: function(b) { return this._to(b, 2, 'getUint16') },
toLongs: function(b) { return this._to(b, 4, 'getUint32') },
toString: function(b) { return String.fromCharCode.apply(null, this.toBytes(b)) },
_from: function(x, size, cb){
var b = new ArrayBuffer(x.length * size), v = new DataView(b)
this._for(x.length, function(i) { v[cb](i*size, x[i]) })
return b
},
fromBytes: function(b) { return this._from(b, 1, 'setUint8') },
fromWords: function(w) { return this._from(w, 2, 'setUint16') },
fromLongs: function(l) { return this._from(l, 4, 'setUint32') },
fromString: function(s) {
var b = new ArrayBuffer(s.length), v = new DataView(b)
this._for(s.length, function(i) { v.setUint8(i, s.charCodeAt(i)) })
return b
}
}
DataView.prototype._types = {
s8: { size: 1, signed: true, get: 'getInt8', set: 'setInt8' },
u8: { size: 1, signed: false, get: 'getUint8', set: 'setUint8' },
s16: { size: 2, signed: true, get: 'getInt16', set: 'setInt16' },
u16: { size: 2, signed: false, get: 'getUint16', set: 'setUint16' },
s32: { size: 4, signed: true, get: 'getInt32', set: 'setInt32' },
u32: { size: 4, signed: false, get: 'getUint32', set: 'setUint32' },
str: { size: 1, get: 'getChar', set: 'setChar', slice: 'getString', splice: 'setString' },
}
DataView.prototype._get = function(type, offset){
var t = this._types[type]
return this[t.get](t.size * offset)
}
DataView.prototype._set = function(type, offset, value){
var t = this._types[type]
return this[t.set](t.size * offset, value)
}
DataView.prototype._slice = function(type, offset, length){
var out = []
for(var i = 0; i < length; i++)
out[i] = this._get(type, offset + i)
return out
}
DataView.prototype._splice = function(type, offset, values){
for(var i = 0; i < values.length; i++)
this._set(type, offset + i, values[i])
}
DataView.prototype.getChar = function(byteOffset) {
return String.fromCharCode(this.getUint8(byteOffset))
}
DataView.prototype.setChar = function(byteOffset, value)
{
this.setUint8(byteOffset, value.charCodeAt(0))
}
DataView.prototype.getString = function(byteOffset, length){
return this._slice('str', byteOffset, length).join('')
}
DataView.prototype.setString = function(byteOffset, value){
this._splice('str', byteOffset, value);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment