UNPKG

varbyte-printable

Version:

A library for encoding variable-length bytes into a compact, printable, and JSON/JavaScript-safe string format.

2 lines (1 loc) 620 B
function s(t){let e=i(t.length);for(let r=0;r<t.length;r++)e+=i(t[r]);return e}function i(t){if(t===65535)return String.fromCharCode(126);let e="",r=t;for(let n=46;;n=0){let a=r%46,o=r-a;if(e=f(a+n)+e,o===0)return e;r=o/46}}function f(t){let e=t+32;if(e>=34)e++;if(e>=92)e++;return String.fromCharCode(e)}function l(t,e){let r={pos:0},n=new t(u(e,r)),a=0;while(r.pos<e.length)n[a++]=u(e,r);return n}function u(t,e){let r=0;for(;;){let n=t.charCodeAt(e.pos++);if(n===126)return 65535;if(n>=92)n--;if(n>=34)n--;let a=n-32;if(a>=46)return r+a-46;r+=a,r*=46}}export{s as encodeVarbytePrintable,l as decodeVarbytePrintable};