MCPcopy
hub / github.com/go-git/go-git / ReadVariableWidthInt

Function ReadVariableWidthInt

utils/binary/read.go:94–119  ·  view source on GitHub ↗

ReadVariableWidthInt reads and returns an int in Git VLQ special format: Ordinary VLQ has some redundancies, example: the number 358 can be encoded as the 2-octet VLQ 0x8166 or the 3-octet VLQ 0x808166 or the 4-octet VLQ 0x80808166 and so forth. To avoid these redundancies, the VLQ format used in

(r io.Reader)

Source from the content-addressed store, hash-verified

92// dheader[--pos] = 128 | (--ofs & 127);
93//
94func ReadVariableWidthInt(r io.Reader) (int64, error) {
95 var c byte
96 if err := Read(r, &c); err != nil {
97 return 0, err
98 }
99
100 var v = int64(c & maskLength)
101 for c&maskContinue > 0 {
102 // Reject input that, after the v++ and shift below, would
103 // not fit in an int64. With v < (MaxInt64-127)>>7, the
104 // post-increment v is at most (MaxInt64-127)>>7 and the
105 // final (v << 7) + (c & 0x7F) stays within int64.
106 if v >= (math.MaxInt64-int64(maskLength))>>lengthBits {
107 return 0, ErrIntegerOverflow
108 }
109
110 v++
111 if err := Read(r, &c); err != nil {
112 return 0, err
113 }
114
115 v = (v << lengthBits) + int64(c&maskLength)
116 }
117
118 return v, nil
119}
120
121const (
122 maskContinue = uint8(128) // 1000 000

Calls 1

ReadFunction · 0.85

Used in the wild real call sites across dependent graphs

searching dependent graphs…