DecodeVarint reads a base-128 [varint encoded] integer from p and returns the value and the number of bytes that were consumed. [varint encoded]: https://developers.google.com/protocol-buffers/docs/encoding#varints
(p []byte)
| 989 | // |
| 990 | // [varint encoded]: https://developers.google.com/protocol-buffers/docs/encoding#varints |
| 991 | func DecodeVarint(p []byte) (v uint64, n int, err error) { |
| 992 | if len(p) == 0 { |
| 993 | return 0, 0, ErrInvalidVarintData |
| 994 | } |
| 995 | // single-byte values don't need any processing |
| 996 | if p[0] < 0x80 { |
| 997 | return uint64(p[0]), 1, nil |
| 998 | } |
| 999 | // 2-9 byte values |
| 1000 | if len(p) < 10 { |
| 1001 | for shift := uint(0); shift < 64; shift += 7 { |
| 1002 | if n >= len(p) { |
| 1003 | return 0, 0, io.ErrUnexpectedEOF |
| 1004 | } |
| 1005 | b := uint64(p[n]) |
| 1006 | n++ |
| 1007 | v |= (b & 0x7f << shift) |
| 1008 | if (b & 0x80) == 0 { |
| 1009 | return v, n, nil |
| 1010 | } |
| 1011 | } |
| 1012 | return 0, 0, ErrValueOverflow |
| 1013 | } |
| 1014 | // 10-byte values |
| 1015 | // . we already know the first byte has the high-bit set, so grab it's value then walk bytes 2-10 |
| 1016 | v = uint64(p[0] & 0x7f) |
| 1017 | for i, shift := 1, 7; i < 10; i, shift = i+1, shift+7 { |
| 1018 | b := uint64(p[i]) |
| 1019 | v |= (b & 0x7f) << shift |
| 1020 | if (b & 0x80) == 0 { |
| 1021 | return v, i + 1, nil |
| 1022 | } |
| 1023 | } |
| 1024 | return 0, 0, ErrValueOverflow |
| 1025 | } |
| 1026 | |
| 1027 | // DecodeZigZag32 reads a base-128 [zig zag encoded] 32-bit integer from p and returns the value and the |
| 1028 | // number of bytes that were consumed. |
no outgoing calls
no test coverage detected