var base64=require("./base64"),VLQ_BASE_SHIFT=5,VLQ_BASE=1<<VLQ_BASE_SHIFT,VLQ_BASE_MASK=VLQ_BASE-1,VLQ_CONTINUATION_BIT=VLQ_BASE;function toVLQSigned(e){return e<0?1+(-e<<1):0+(e<<1)}function fromVLQSigned(e){var r=e>>1;return 1==(1&e)?-r:r}exports.encode=function(e){var r,_="",n=toVLQSigned(e);do{r=n&VLQ_BASE_MASK,(n>>>=VLQ_BASE_SHIFT)>0&&(r|=VLQ_CONTINUATION_BIT),_+=base64.encode(r)}while(n>0);return _},exports.decode=function(e,r,_){var n,o,S=e.length,t=0,i=0;do{if(r>=S)throw new Error("Expected more digits in base 64 VLQ value.");if(-1===(o=base64.decode(e.charCodeAt(r++))))throw new Error("Invalid base64 digit: "+e.charAt(r-1));n=!!(o&VLQ_CONTINUATION_BIT),t+=(o&=VLQ_BASE_MASK)<<i,i+=VLQ_BASE_SHIFT}while(n);_.value=fromVLQSigned(t),_.rest=r};