2018-09-06 07:55:28 +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
|
|
|
|
2018-07-09 03:46:11 +00:00
|
|
|
var Buffer_from = /*::(*/function(){}/*:: :any)*/;
|
|
|
|
|
2018-05-05 06:34:37 +00:00
|
|
|
if(typeof Buffer !== 'undefined') {
|
2018-07-09 03:46:11 +00:00
|
|
|
var nbfs = !Buffer.from;
|
|
|
|
if(!nbfs) try { Buffer.from("foo", "utf8"); } catch(e) { nbfs = true; }
|
|
|
|
Buffer_from = nbfs ? function(buf, enc) { return (enc) ? new Buffer(buf, enc) : new Buffer(buf); } : Buffer.from.bind(Buffer);
|
2018-05-05 06:34:37 +00:00
|
|
|
// $FlowIgnore
|
|
|
|
if(!Buffer.alloc) Buffer.alloc = function(n) { return new Buffer(n); };
|
2018-09-06 07:55:28 +00:00
|
|
|
// $FlowIgnore
|
|
|
|
if(!Buffer.allocUnsafe) Buffer.allocUnsafe = function(n) { return new Buffer(n); };
|
2018-05-05 06:34:37 +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 */
|
2018-05-05 06:34:37 +00:00
|
|
|
return has_buf ? Buffer.alloc(len) : new Array(len);
|
2015-04-02 20:32:22 +00:00
|
|
|
/* jshint +W056 */
|
|
|
|
}
|
|
|
|
|
2018-09-06 07:55:28 +00:00
|
|
|
function new_unsafe_buf(len/*:number*/) {
|
|
|
|
/* jshint -W056 */
|
|
|
|
return has_buf ? Buffer.allocUnsafe(len) : new Array(len);
|
|
|
|
/* jshint +W056 */
|
|
|
|
}
|
|
|
|
|
2018-05-05 06:34:37 +00:00
|
|
|
var s2a = function s2a(s/*:string*/)/*:any*/ {
|
2018-07-09 03:46:11 +00:00
|
|
|
if(has_buf) return Buffer_from(s, "binary");
|
2018-04-21 15:16:37 +00:00
|
|
|
return s.split("").map(function(x/*:string*/)/*:number*/{ return x.charCodeAt(0) & 0xff; });
|
2018-05-05 06:34:37 +00:00
|
|
|
};
|
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*/ {
|
2019-08-04 19:50:49 +00:00
|
|
|
if(Array.isArray(data)) return data.map(function(c) { return String.fromCharCode(c); }).join("");
|
|
|
|
var o/*:Array<string>*/ = []; for(var i = 0; i < data.length; ++i) o[i] = String.fromCharCode(data[i]); return o.join("");
|
2017-12-30 05:40:35 +00:00
|
|
|
}
|
|
|
|
|
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;
|