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

Function encodeVarint

proto/encode.go:29–114  ·  view source on GitHub ↗
(b []byte, v uint64)

Source from the content-addressed store, hash-verified

27type encodeFunc = func([]byte, unsafe.Pointer, flags) (int, error)
28
29func encodeVarint(b []byte, v uint64) (int, error) {
30 n := sizeOfVarint(v)
31
32 if len(b) < n {
33 return 0, io.ErrShortBuffer
34 }
35
36 switch n {
37 case 1:
38 b[0] = byte(v)
39
40 case 2:
41 b[0] = byte(v) | 0x80
42 b[1] = byte(v >> 7)
43
44 case 3:
45 b[0] = byte(v) | 0x80
46 b[1] = byte(v>>7) | 0x80
47 b[2] = byte(v >> 14)
48
49 case 4:
50 b[0] = byte(v) | 0x80
51 b[1] = byte(v>>7) | 0x80
52 b[2] = byte(v>>14) | 0x80
53 b[3] = byte(v >> 21)
54
55 case 5:
56 b[0] = byte(v) | 0x80
57 b[1] = byte(v>>7) | 0x80
58 b[2] = byte(v>>14) | 0x80
59 b[3] = byte(v>>21) | 0x80
60 b[4] = byte(v >> 28)
61
62 case 6:
63 b[0] = byte(v) | 0x80
64 b[1] = byte(v>>7) | 0x80
65 b[2] = byte(v>>14) | 0x80
66 b[3] = byte(v>>21) | 0x80
67 b[4] = byte(v>>28) | 0x80
68 b[5] = byte(v >> 35)
69
70 case 7:
71 b[0] = byte(v) | 0x80
72 b[1] = byte(v>>7) | 0x80
73 b[2] = byte(v>>14) | 0x80
74 b[3] = byte(v>>21) | 0x80
75 b[4] = byte(v>>28) | 0x80
76 b[5] = byte(v>>35) | 0x80
77 b[6] = byte(v >> 42)
78
79 case 8:
80 b[0] = byte(v) | 0x80
81 b[1] = byte(v>>7) | 0x80
82 b[2] = byte(v>>14) | 0x80
83 b[3] = byte(v>>21) | 0x80
84 b[4] = byte(v>>28) | 0x80
85 b[5] = byte(v>>35) | 0x80
86 b[6] = byte(v>>42) | 0x80

Callers 15

encodeUintFunction · 0.85
encodeInt64Function · 0.85
encodeIntFunction · 0.85
AppendFunction · 0.85
AppendVarintFunction · 0.85
messageEncodeFuncOfFunction · 0.85
encodeUint64Function · 0.85
encodeStringFunction · 0.85
mapEncodeFuncOfFunction · 0.85
customEncodeFuncOfFunction · 0.85

Calls 1

sizeOfVarintFunction · 0.85

Tested by 3

TestEncodeDecodeVarintFunction · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…