UNPKG

byte-encodings

Version:

Utilities for encoding and decoding common formats like hex, base64, and varint. Ported from Deno's @std/encoding.

2 lines (1 loc) 1.14 kB
var w=18446744073709551615n,p=10,x=5;var c=0x80n,s=7n,h=new ArrayBuffer(8),d=new Uint32Array(h),l=new BigUint64Array(h);function y(n,r=0){l[0]=0n;let e=0,t=0,o=r;if(n.length===0)throw new RangeError("Cannot read empty buffer");let i;do i=n[o],e|=(i&127)<<t,t===28&&(d[0]=e,e=(i&112)>>>4,t=-4),t+=7,o++;while((i&128)===128);let a=o-r;if(a===10&&e>-1||a===11||o>n.length)throw new RangeError("Cannot decode the varint input: Malformed or overflow varint");return d[+(a>4)]=e,[l[0],o]}function g(n,r=0){let e=0,t=0;for(let o=r;o<=Math.min(n.length,r+5);o+=1,e+=7){let i=n[o];if(t+=(i&127)*Math.pow(2,e),!(i&128))return[t,o+1]}throw new RangeError("Cannot decode the varint input: Malformed or overflow varint")}function M(n,r=new Uint8Array(10),e=0){if(n=BigInt(n),n<0n)throw new RangeError(`Cannot encode the input into varint as it should be non-negative integer: received ${n}`);for(let t=e;t<=Math.min(r.length,10);t+=1){if(n<c)return r[t]=Number(n),t+=1,[r.slice(e,t),t];r[t]=Number(n&0xFFn|c),n>>=s}throw new RangeError(`Cannot encode the input ${n} into varint as it overflows uint64`)}export{w as a,p as b,x as c,y as d,g as e,M as f};