MCPcopy Create free account
hub / github.com/segmentio/encoding / decodeVarint

Function decodeVarint

proto/decode.go:32–56  ·  view source on GitHub ↗
(b []byte)

Source from the content-addressed store, hash-verified

30var errVarintOverflow = errors.New("varint overflowed 64 bits integer")
31
32func decodeVarint(b []byte) (uint64, int, error) {
33 if len(b) != 0 && b[0] < 0x80 {
34 // Fast-path for decoding the common case of varints that fit on a
35 // single byte.
36 //
37 // This path is ~60% faster than calling binary.Uvarint.
38 return uint64(b[0]), 1, nil
39 }
40
41 var x uint64
42 var s uint
43
44 for i, c := range b {
45 if c < 0x80 {
46 if i > 9 || i == 9 && c > 1 {
47 return 0, i, errVarintOverflow
48 }
49 return x | uint64(c)<<s, i + 1, nil
50 }
51 x |= uint64(c&0x7f) << s
52 s += 7
53 }
54
55 return x, len(b), io.ErrUnexpectedEOF
56}
57
58func decodeVarintZigZag(b []byte) (int64, int, error) {
59 v, n, err := decodeVarint(b)

Callers 13

decodeUintFunction · 0.85
decodeInt64Function · 0.85
decodeIntFunction · 0.85
ParseFunction · 0.85
VarintMethod · 0.85
decodeUint64Function · 0.85
structDecodeFuncOfFunction · 0.85
decodeUint32Function · 0.85
decodeInt32Function · 0.85
decodeVarintZigZagFunction · 0.85
decodeTagFunction · 0.85
decodeVarlenFunction · 0.85

Calls

no outgoing calls

Tested by 1

TestEncodeDecodeVarintFunction · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…