@arcgis/core
Version:
ArcGIS Maps SDK for JavaScript: A complete 2D and 3D mapping and data visualization API
3 lines (2 loc) • 5.85 kB
JavaScript
/* COPYRIGHT Esri - https://js.arcgis.com/5.0.8/LICENSE.txt */
import has from"./has.js";import{estimateNumberArrayMemory as t}from"./memoryEstimations.js";import e from"./ObjectPool.js";const s=4294967296,i=new TextDecoder("utf-8"),r=has("safari")||has("ios")?6:has("ff")?12:32;class n{constructor(t,e,s=0,i=t?.byteLength??0){this._tag=0,this._dataType=99,this._init(t,e,s,i)}[Symbol.dispose](){this.release()}_init(t,e,s,i){this._data=t,this._dataView=e,this._pos=s,this._end=i}get usedMemory(){return 64+t(this._data)}asUnsafe(){return this}clone(){return new n(this._data,this._dataView,this._pos,this._end)}clonePooled(){return n.constructPooled(this._data,this._dataView,this._pos,this._end)}static constructPooled(t,e,s=0,i=t?.byteLength??0){const r=n.pool.acquire();return r._init(t,e,s,i),r}pos(){return this._pos}move(t){this._pos=t}nextTag(t){for(;;){if(this._pos===this._end)return!1;const e=this._decodeVarint();if(this._tag=e>>3,this._dataType=7&e,!t||t===this._tag)break;this.skip()}return!0}next(){if(this._pos===this._end)return!1;const t=this._decodeVarint();return this._tag=t>>3,this._dataType=7&t,!0}empty(){return this._pos>=this._end}tag(){return this._tag}getInt32(){return this._decodeVarint()}getInt64(){return this._decodeVarint()}getUInt32(){let t=4294967295;if(t=(127&this._data[this._pos])>>>0,this._data[this._pos++]<128)return t;if(t=(t|(127&this._data[this._pos])<<7)>>>0,this._data[this._pos++]<128)return t;if(t=(t|(127&this._data[this._pos])<<14)>>>0,this._data[this._pos++]<128)return t;if(t=(t|(127&this._data[this._pos])<<21)>>>0,this._data[this._pos++]<128)return t;if(t=(t|(15&this._data[this._pos])<<28)>>>0,this._data[this._pos++]<128)return t;throw new Error("Varint overflow")}getPackedEnd(){const t=this.getUInt32();return this._pos+t}getUInt64(){return this._decodeVarint()}getSInt32(){const t=this.getUInt32();return t>>>1^-(1&t)}getSInt64(){return this._decodeSVarint()}getBool(){const t=0!==this._data[this._pos];return this._skip(1),t}getEnum(){return this._decodeVarint()}getFixed64(){const t=this._dataView,e=this._pos,i=t.getUint32(e,!0)+t.getUint32(e+4,!0)*s;return this._skip(8),i}getSFixed64(){const t=this._dataView,e=this._pos,i=t.getUint32(e,!0)+t.getInt32(e+4,!0)*s;return this._skip(8),i}getDouble(){const t=this._dataView.getFloat64(this._pos,!0);return this._skip(8),t}getFixed32(){const t=this._dataView.getUint32(this._pos,!0);return this._skip(4),t}getSFixed32(){const t=this._dataView.getInt32(this._pos,!0);return this._skip(4),t}getFloat(){const t=this._dataView.getFloat32(this._pos,!0);return this._skip(4),t}getString(){const t=this._getLength(),e=this._pos,s=this._toString(this._data,e,e+t);return this._skip(t),s}getBytes(){const t=this._getLength(),e=this._pos,s=this._toBytes(this._data,e,e+t);return this._skip(t),s}getLength(){return this._getLengthUnsafe()}processMessageWithArgs(t,e,s,i){const r=this.getMessage(),n=t(r,e,s,i);return r.release(),n}processMessage(t){const e=this.getMessage(),s=t(e);return e.release(),s}getMessage(){const t=this._getLength(),e=n.pool.acquire();return e._init(this._data,this._dataView,this._pos,this._pos+t),this._skip(t),e}release(){n.pool.release(this)}dataType(){return this._dataType}skip(){switch(this._dataType){case 0:this._decodeVarint();break;case 1:this._skip(8);break;case 2:this._skip(this._getLength());break;case 5:this._skip(4);break;default:throw new Error("Invalid data type!")}}skipToEnd(){for(;this.next();)this.skip()}skipLen(t){this._skip(t)}_skip(t){if(this._pos+t>this._end)throw new Error("Attempt to skip past the end of buffer!");this._pos+=t}_decodeVarint(){const t=this._data;let e=this._pos,s=0,i=0;if(this._end-e>=10)do{if(i=t[e++],s|=127&i,!(128&i))break;if(i=t[e++],s|=(127&i)<<7,!(128&i))break;if(i=t[e++],s|=(127&i)<<14,!(128&i))break;if(i=t[e++],s|=(127&i)<<21,!(128&i))break;if(i=t[e++],s+=268435456*(127&i),!(128&i))break;if(i=t[e++],s+=34359738368*(127&i),!(128&i))break;if(i=t[e++],s+=4398046511104*(127&i),!(128&i))break;if(i=t[e++],s+=562949953421312*(127&i),!(128&i))break;if(i=t[e++],s+=72057594037927940*(127&i),!(128&i))break;if(i=t[e++],s+=0x8000000000000000*(127&i),!(128&i))break;throw new Error("Varint too long!")}while(0);else{let r=1;for(;e!==this._end&&(i=t[e],128&i);)++e,s+=(127&i)*r,r*=128;if(e===this._end)throw new Error("Varint overrun!");++e,s+=i*r}return this._pos=e,s}_decodeSVarint(){const t=this._data;let e,s=0,i=0;const r=1&t[this._pos];if(i=t[this._pos++],s|=127&i,!(128&i))return r?-(s+1)/2:s/2;if(i=t[this._pos++],s|=(127&i)<<7,!(128&i))return r?-(s+1)/2:s/2;if(i=t[this._pos++],s|=(127&i)<<14,!(128&i))return r?-(s+1)/2:s/2;if(i=t[this._pos++],s|=(127&i)<<21,!(128&i))return r?-(s+1)/2:s/2;if(i=t[this._pos++],s+=268435456*(127&i),!(128&i))return r?-(s+1)/2:s/2;if(i=t[this._pos++],s+=34359738368*(127&i),!(128&i))return r?-(s+1)/2:s/2;if(i=t[this._pos++],s+=4398046511104*(127&i),!(128&i))return r?-(s+1)/2:s/2;if(e=BigInt(s),i=t[this._pos++],e+=0x2000000000000n*BigInt(127&i),!(128&i))return Number(r?-(e+1n)/2n:e/2n);if(i=t[this._pos++],e+=0x100000000000000n*BigInt(127&i),!(128&i))return Number(r?-(e+1n)/2n:e/2n);if(i=t[this._pos++],e+=0x8000000000000000n*BigInt(127&i),!(128&i))return Number(r?-(e+1n)/2n:e/2n);throw new Error("Varint too long!")}_getLength(){if(2!==this._dataType)throw new Error("Not a delimited data type!");return this._decodeVarint()}_getLengthUnsafe(){return this.getUInt32()}_toString(t,e,s){if((s=Math.min(this._end,s))-e>r){const r=t.subarray(e,s);return i.decode(r)}let n="",o="";for(let i=e;i<s;++i){const e=t[i];128&e?o+="%"+e.toString(16):(n+=decodeURIComponent(o)+String.fromCharCode(e),o="")}return o.length&&(n+=decodeURIComponent(o)),n}_toBytes(t,e,s){return s=Math.min(this._end,s),new Uint8Array(t.buffer,e,s-e)}static{this.pool=new e(()=>new n(null,null),void 0,t=>{t._data=null,t._dataView=null})}}export{n as default};