aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/gopkg.in/sourcemap.v1/base64vlq/base64_vlq.go
blob: 16cbfb56f09e60606d4e70b4170355a83a475229 (plain) (blame)
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package base64vlq

import (
    "io"
)

const encodeStd = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"

const (
    vlqBaseShift       = 5
    vlqBase            = 1 << vlqBaseShift
    vlqBaseMask        = vlqBase - 1
    vlqSignBit         = 1
    vlqContinuationBit = vlqBase
)

var decodeMap [256]byte

func init() {
    for i := 0; i < len(encodeStd); i++ {
        decodeMap[encodeStd[i]] = byte(i)
    }
}

func toVLQSigned(n int) int {
    if n < 0 {
        return -n<<1 + 1
    }
    return n << 1
}

func fromVLQSigned(n int) int {
    isNeg := n&vlqSignBit != 0
    n >>= 1
    if isNeg {
        return -n
    }
    return n
}

type Encoder struct {
    w io.ByteWriter
}

func NewEncoder(w io.ByteWriter) *Encoder {
    return &Encoder{
        w: w,
    }
}

func (enc Encoder) Encode(n int) error {
    n = toVLQSigned(n)
    for digit := vlqContinuationBit; digit&vlqContinuationBit != 0; {
        digit = n & vlqBaseMask
        n >>= vlqBaseShift
        if n > 0 {
            digit |= vlqContinuationBit
        }

        err := enc.w.WriteByte(encodeStd[digit])
        if err != nil {
            return err
        }
    }
    return nil
}

type Decoder struct {
    r io.ByteReader
}

func NewDecoder(r io.ByteReader) *Decoder {
    return &Decoder{
        r: r,
    }
}

func (dec Decoder) Decode() (n int, err error) {
    shift := uint(0)
    for continuation := true; continuation; {
        c, err := dec.r.ReadByte()
        if err != nil {
            return 0, err
        }

        c = decodeMap[c]
        continuation = c&vlqContinuationBit != 0
        n += int(c&vlqBaseMask) << shift
        shift += vlqBaseShift
    }
    return fromVLQSigned(n), nil
}