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 */
|
|
|
|
}
|
|
|
|
|
2017-02-10 19:23:01 +00:00
|
|
|
function s2a(s/*:string*/) {
|
2015-04-02 20:32:22 +00:00
|
|
|
if(has_buf) return new Buffer(s, "binary");
|
|
|
|
return s.split("").map(function(x){ return x.charCodeAt(0) & 0xff; });
|
|
|
|
}
|
|
|
|
|
2017-12-30 05:40:35 +00:00
|
|
|
function s2ab(s/*:string*/) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
function arr2str(data/*:any*/)/*:string*/ {
|
|
|
|
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("");
|
|
|
|
}
|
|
|
|
|
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;
|