forked from fengdh/mdict-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitstring.min.js
1 lines (1 loc) · 2.6 KB
/
bitstring.min.js
1
function BitString(t,e){this.expr=t,this.options=e||{},this.types=[],this.cache={},this.byteLength=0,(this.options.compile||!0)&&this.compile()}DataView.prototype.getInt64=function(t,e){return this.getInt32(t,e)<<32|this.getInt32(t+4,e)},DataView.prototype.setInt64=function(t,e,n){return this.setInt32(t,e>>32,n)},DataView.prototype.getUint64=function(t,e){return this.getUint32(t,e)<<32|this.getUint32(t+4,e)},DataView.prototype.setUint64=function(t,e,n){return this.setUint32(t,e>>32,n)},Array.prototype.chunk=function(t){var e=this;return[].concat.apply([],e.map(function(n,i){return i%t?[]:[e.slice(i,i+t)]}))},BitString.UNPACK_OP=0,BitString.PACK_OP=1,BitString.SPECIFIER_SEPARATOR="-",BitString.ATTR_SEPARATOR=":",BitString.SPECIFIER_DELIMITER="/",BitString.SEGMENT_DELIMITER=",",BitString.util={dvdup:function(t,e){for(var n=new Uint8Array(t),i=new Uint8Array(new ArrayBuffer(e)),r=0,o=n.length;o>r;++r)i[r]=n[r];return new DataView(i.buffer)},str2dv:function(t){for(var e=new ArrayBuffer(t.length),n=new Uint8Array(e),i=0,r=t.length;r>i;i++)n[i]=t.charCodeAt(i);return new DataView(e)}},BitString.prototype.compile=function(){for(var t=this.expr.split(BitString.SEGMENT_DELIMITER),e=0,n=t.length;n>e;++e){var i,r,o,s,a;i=t[e].split(BitString.SPECIFIER_DELIMITER),o=i[1]||"",r=i[0].split(BitString.ATTR_SEPARATOR),s=o.split(BitString.SPECIFIER_SEPARATOR)||[],a={spec:r[1],size:parseInt("_"!=r[1]?r[1]:0),value:r[0],unit:this.unit(o),ref:!1,sign:"Uint"},s.indexOf("signed")<0&&s.indexOf("int")<0||(a.sign="Int"),s.indexOf("int")<0||(a.size=4,a.unit=0),isNaN(a.size)&&0==this.cache[a.spec]&&(a.ref=!0),this.byteLength+=a.size||0,this.types.push(a),this.cache[a.value]=0}},BitString.prototype.unit=function(t){var e=t.split(BitString.ATTR_SEPARATOR),n=2==e.length;return s=n?parseInt(e[1]):e[0],n?s:"char"==s?1:"int"==s?4:"double"==s?8:0},BitString.prototype.__op=function(t,e){var n,i,r=BitString.util;e==BitString.UNPACK_OP?(n=r.str2dv(t),i={}):(n=new DataView(new ArrayBuffer(this.byteLength)),i=t);for(var o=0,s=this.types,a=!this.options.bigEndian,l=0,u=s.length;u>l;++l){var p=s[l].size,c=s[l].value,d=s[l].unit,h=s[l].ref,g=s[l].sign,f=0,v=d*p||1,m=d?d:p,y=i[c],w=null;h&&(s[l].size=i[s[l].spec],p=s[l].size,v=d*p,e&&(n=r.dvdup(n.buffer,n.buffer.byteLength+v))),p&&(w=(e?"set":"get")+g+8*m);do e?p&&n[w](o,d?i[c][f]:i[c],a):(y=w?n[w](o,a):t.substr(o),d?(i[c]=i[c]||[]).push(y):i[c]=y),o+=m,--v,++f;while(v);this.cache[c]=y}return e?String.fromCharCode.apply(null,new Uint8Array(n.buffer)):i},BitString.prototype.unpack=function(t){return this.__op(t,BitString.UNPACK_OP)},BitString.prototype.pack=function(t){return this.__op(t,BitString.PACK_OP)};