2017-04-02 06:47:25 +00:00
|
|
|
var has_buf = (typeof Buffer !== 'undefined' && typeof process !== 'undefined' && typeof process.versions !== 'undefined' && process.versions.node);
|
2015-04-02 20:32:22 +00:00
|
|
|
|
2017-02-10 19:23:01 +00:00
|
|
|
function new_raw_buf(len/*:number*/) {
|
2015-04-02 20:32:22 +00:00
|
|
|
/* jshint -W056 */
|
2017-07-26 08:35:28 +00:00
|
|
|
// $FlowIgnore
|
2015-04-02 20:32:22 +00:00
|
|
|
return new (has_buf ? Buffer : Array)(len);
|
|
|
|
/* jshint +W056 */
|
|
|
|
}
|
|
|
|
|
2018-04-21 15:16:37 +00:00
|
|
|
function s2a(s/*:string*/)/*:any*/ {
|
2015-04-02 20:32:22 +00:00
|
|
|
if(has_buf) return new Buffer(s, "binary");
|
2018-04-21 15:16:37 +00:00
|
|
|
return s.split("").map(function(x/*:string*/)/*:number*/{ return x.charCodeAt(0) & 0xff; });
|
2015-04-02 20:32:22 +00:00
|
|
|
}
|
|
|
|
|
2018-04-21 15:16:37 +00:00
|
|
|
function s2ab(s/*:string*/)/*:any*/ {
|
2017-12-30 05:40:35 +00:00
|
|
|
if(typeof ArrayBuffer === 'undefined') return s2a(s);
|
|
|
|
var buf = new ArrayBuffer(s.length), view = new Uint8Array(buf);
|
|
|
|
for (var i=0; i!=s.length; ++i) view[i] = s.charCodeAt(i) & 0xFF;
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2018-02-03 20:46:32 +00:00
|
|
|
function a2s(data/*:any*/)/*:string*/ {
|
2017-12-30 05:40:35 +00:00
|
|
|
if(Array.isArray(data)) return data.map(_chr).join("");
|
|
|
|
var o/*:Array<string>*/ = []; for(var i = 0; i < data.length; ++i) o[i] = _chr(data[i]); return o.join("");
|
|
|
|
}
|
|
|
|
|
2018-02-03 20:46:32 +00:00
|
|
|
function a2u(data/*:Array<number>*/)/*:Uint8Array*/ {
|
|
|
|
if(typeof Uint8Array === 'undefined') throw new Error("Unsupported");
|
|
|
|
return new Uint8Array(data);
|
|
|
|
}
|
|
|
|
|
2018-01-11 08:01:25 +00:00
|
|
|
function ab2a(data/*:ArrayBuffer|Uint8Array*/)/*:Array<number>*/ {
|
|
|
|
if(typeof ArrayBuffer == 'undefined') throw new Error("Unsupported");
|
|
|
|
if(data instanceof ArrayBuffer) return ab2a(new Uint8Array(data));
|
|
|
|
/*:: if(data instanceof ArrayBuffer) throw new Error("unreachable"); */
|
|
|
|
var o = new Array(data.length);
|
|
|
|
for(var i = 0; i < data.length; ++i) o[i] = data[i];
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
2015-04-02 20:32:22 +00:00
|
|
|
var bconcat = function(bufs) { return [].concat.apply([], bufs); };
|
|
|
|
|
2017-12-01 05:48:10 +00:00
|
|
|
var chr0 = /\u0000/g, chr1 = /[\u0001-\u0006]/g;
|