File: varint.py

package info (click to toggle)
python-linetable 0.0.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 88 kB
  • sloc: python: 246; makefile: 2
file content (35 lines) | stat: -rw-r--r-- 633 bytes parent folder | download | duplicates (2)
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
def read_varint(it):
    b = next(it)
    val = b & 63
    shift = 0
    while b & 64:
        b = next(it)
        shift += 6
        val |= (b & 63) << shift
    return val


def read_signed_varint(it):
    uval = read_varint(it)
    if uval & 1:
        return -(uval >> 1)
    else:
        return uval >> 1


def generate_varint(n):
    if n == 0:
        yield 0
    while n:
        if n > 63:
            yield n & 63 | 64
        else:
            yield n & 63
        n = n >> 6


def generate_signed_varint(s):
    if s < 0:
        return generate_varint(((-s) << 1) | 1)
    else:
        return generate_varint(s << 1)