-
Notifications
You must be signed in to change notification settings - Fork 0
/
vlq-encoder.js
43 lines (40 loc) · 1.07 KB
/
vlq-encoder.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
var encodeUIntString=function(str){
return new TextEncoder().encode(str);
};
var VLQEncoder={};
VLQEncoder.encodeUInt=function(value){
if(value<0||value!==Math.floor(value))debugger;
var output=new ResizableUint8Array();
while(true){
var next_val=value%128;
value=Math.floor(value/128);
if(value>0){
output.push(128+next_val);
}
else{
output.push(next_val);
break;
}
}
return output.toUint8Array();
};
VLQEncoder.encodeInt=function(value){
if(value!==Math.floor(value))debugger;
var output=new ResizableUint8Array();
var is_neg=(value<0);
if(is_neg)value=-value-1;
while(true){
var next_val=value%128;
value=Math.floor(value/128);
if(value>0||next_val>=64){
if(is_neg)output.push((~next_val)&255);
else output.push(128+next_val);
}
else{
if(is_neg)output.push((~next_val)&127);
else output.push(next_val);
break;
}
}
return output.toUint8Array();
};