forked from k8w/tsbuffer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
VarintUtil.ts
32 lines (29 loc) · 1.03 KB
/
VarintUtil.ts
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
import { LongBits } from './LongBits';
export class VarintUtil {
static measureUint32(uint32Value: number): number {
return (uint32Value = uint32Value >>> 0) < 128 ? 1
: uint32Value < 16384 ? 2
: uint32Value < 2097152 ? 3
: uint32Value < 268435456 ? 4
: 5
}
static encodeUint32(uint32Value: number, arr: Uint8Array, pos: number) {
while (uint32Value > 127) {
arr[pos++] = uint32Value & 127 | 128;
uint32Value >>>= 7;
}
arr[pos] = uint32Value;
}
static encodeUint64(value: LongBits, arr: Uint8Array, pos: number) {
while (value.high) {
arr[pos++] = value.low & 127 | 128;
value.low = (value.low >>> 7 | value.high << 25) >>> 0;
value.high >>>= 7;
}
while (value.low > 127) {
arr[pos++] = value.low & 127 | 128;
value.low = value.low >>> 7;
}
arr[pos++] = value.low;
}
}