UNPKG

starling-framework

Version:

A fast, productive library for 2D cross-platform development.

281 lines (264 loc) 7.14 kB
// Class: haxe.io.Bytes var $global = typeof window != "undefined" ? window : typeof global != "undefined" ? global : typeof self != "undefined" ? self : this $global.Object.defineProperty(exports, "__esModule", {value: true}); var __map_reserved = {}; // Imports var $hxClasses = require("./../../hxClasses_stub").default; var $hxEnums = require("./../../hxEnums_stub").default; var $import = require("./../../import_stub").default; function js__$Boot_HaxeError() {return require("./../../js/_Boot/HaxeError");} function haxe_io_Error() {return require("./../../haxe/io/Error");} function haxe__$Int64__$_$_$Int64() {return require("./../../haxe/_Int64/___Int64");} function haxe_io_Encoding() {return require("./../../haxe/io/Encoding");} function StringBuf() {return require("./../../StringBuf");} function HxOverrides() {return require("./../../HxOverrides");} function StringTools() {return require("./../../StringTools");} // Constructor var Bytes = function(data) { this.length = data.byteLength; this.b = new Uint8Array(data); this.b.bufferValue = data; data.hxBytes = this; data.bytes = this.b; } // Meta Bytes.__name__ = "haxe.io.Bytes"; Bytes.__isInterface__ = false; Bytes.prototype = { get: function(pos) { return this.b[pos]; }, set: function(pos,v) { this.b[pos] = v; }, blit: function(pos,src,srcpos,len) { if(pos < 0 || srcpos < 0 || len < 0 || pos + len > this.length || srcpos + len > src.length) { throw new (js__$Boot_HaxeError().default)((haxe_io_Error().default).OutsideBounds); } if(srcpos == 0 && len == src.b.byteLength) { this.b.set(src.b,pos); } else { this.b.set(src.b.subarray(srcpos,srcpos + len),pos); } }, fill: function(pos,len,value) { var _g = 0; var _g1 = len; while(_g < _g1) { var i = _g++; this.set(pos++,value); } }, sub: function(pos,len) { if(pos < 0 || len < 0 || pos + len > this.length) { throw new (js__$Boot_HaxeError().default)((haxe_io_Error().default).OutsideBounds); } return new Bytes(this.b.buffer.slice(pos + this.b.byteOffset,pos + this.b.byteOffset + len)); }, compare: function(other) { var b1 = this.b; var b2 = other.b; var len = this.length < other.length ? this.length : other.length; var _g = 0; var _g1 = len; while(_g < _g1) { var i = _g++; if(b1[i] != b2[i]) { return b1[i] - b2[i]; } } return this.length - other.length; }, initData: function() { if(this.data == null) { this.data = new DataView(this.b.buffer,this.b.byteOffset,this.b.byteLength); } }, getDouble: function(pos) { this.initData(); return this.data.getFloat64(pos,true); }, getFloat: function(pos) { this.initData(); return this.data.getFloat32(pos,true); }, setDouble: function(pos,v) { this.initData(); this.data.setFloat64(pos,v,true); }, setFloat: function(pos,v) { this.initData(); this.data.setFloat32(pos,v,true); }, getUInt16: function(pos) { this.initData(); return this.data.getUint16(pos,true); }, setUInt16: function(pos,v) { this.initData(); this.data.setUint16(pos,v,true); }, getInt32: function(pos) { this.initData(); return this.data.getInt32(pos,true); }, setInt32: function(pos,v) { this.initData(); this.data.setInt32(pos,v,true); }, getInt64: function(pos) { var this1 = new (haxe__$Int64__$_$_$Int64().default)(this.getInt32(pos + 4),this.getInt32(pos)); return this1; }, setInt64: function(pos,v) { this.setInt32(pos,v.low); this.setInt32(pos + 4,v.high); }, getString: function(pos,len,encoding) { if(pos < 0 || len < 0 || pos + len > this.length) { throw new (js__$Boot_HaxeError().default)((haxe_io_Error().default).OutsideBounds); } if(encoding == null) { encoding = (haxe_io_Encoding().default).UTF8; } var s = ""; var b = this.b; var i = pos; var max = pos + len; switch(encoding._hx_index) { case 0: var debug = pos > 0; while(i < max) { var c = b[i++]; if(c < 128) { if(c == 0) { break; } s += String.fromCodePoint(c); } else if(c < 224) { var code = (c & 63) << 6 | b[i++] & 127; s += String.fromCodePoint(code); } else if(c < 240) { var c2 = b[i++]; var code1 = (c & 31) << 12 | (c2 & 127) << 6 | b[i++] & 127; s += String.fromCodePoint(code1); } else { var c21 = b[i++]; var c3 = b[i++]; var u = (c & 15) << 18 | (c21 & 127) << 12 | (c3 & 127) << 6 | b[i++] & 127; s += String.fromCodePoint(u); } } break; case 1: while(i < max) { var c1 = b[i++] | b[i++] << 8; s += String.fromCodePoint(c1); } break; } return s; }, readString: function(pos,len) { return this.getString(pos,len); }, toString: function() { return this.getString(0,this.length); }, toHex: function() { var s = new (StringBuf().default)(); var chars = []; var str = "0123456789abcdef"; var _g = 0; var _g1 = str.length; while(_g < _g1) { var i = _g++; chars.push((HxOverrides().default).cca(str,i)); } var _g2 = 0; var _g3 = this.length; while(_g2 < _g3) { var i1 = _g2++; var c = this.get(i1); s.addChar(chars[c >> 4]); s.addChar(chars[c & 15]); } return s.toString(); }, getData: function() { return this.b.bufferValue; } }; Bytes.prototype.__class__ = Bytes.prototype.constructor = $hxClasses["haxe.io.Bytes"] = Bytes; // Init // Statics Bytes.alloc = function(length) { return new Bytes(new ArrayBuffer(length)); } Bytes.ofString = function(s,encoding) { if(encoding == (haxe_io_Encoding().default).RawNative) { var buf = new Uint8Array(s.length << 1); var _g = 0; var _g1 = s.length; while(_g < _g1) { var i = _g++; var c = (StringTools().default).fastCodeAt(s,i); buf[i << 1] = c & 255; buf[i << 1 | 1] = c >> 8; } return new Bytes(buf.buffer); } var a = []; var i1 = 0; while(i1 < s.length) { var c1 = (StringTools().default).fastCodeAt(s,i1++); if(55296 <= c1 && c1 <= 56319) { c1 = c1 - 55232 << 10 | (StringTools().default).fastCodeAt(s,i1++) & 1023; } if(c1 <= 127) { a.push(c1); } else if(c1 <= 2047) { a.push(192 | c1 >> 6); a.push(128 | c1 & 63); } else if(c1 <= 65535) { a.push(224 | c1 >> 12); a.push(128 | c1 >> 6 & 63); a.push(128 | c1 & 63); } else { a.push(240 | c1 >> 18); a.push(128 | c1 >> 12 & 63); a.push(128 | c1 >> 6 & 63); a.push(128 | c1 & 63); } } return new Bytes(new Uint8Array(a).buffer); } Bytes.ofData = function(b) { var hb = b.hxBytes; if(hb != null) { return hb; } return new Bytes(b); } Bytes.ofHex = function(s) { if((s.length & 1) != 0) { throw new (js__$Boot_HaxeError().default)("Not a hex string (odd number of digits)"); } var a = []; var i = 0; var len = s.length >> 1; while(i < len) { var high = (StringTools().default).fastCodeAt(s,i * 2); var low = (StringTools().default).fastCodeAt(s,i * 2 + 1); high = (high & 15) + ((high & 64) >> 6) * 9; low = (low & 15) + ((low & 64) >> 6) * 9; a.push((high << 4 | low) & 255); ++i; } return new Bytes(new Uint8Array(a).buffer); } Bytes.fastGet = function(b,pos) { return b.bytes[pos]; } // Export exports.default = Bytes;