github.com/QuangHoangHao/kafka-go@v0.4.36/protocol/size.go (about) 1 package protocol 2 3 import ( 4 "math/bits" 5 ) 6 7 func sizeOfVarString(s string) int { 8 return sizeOfVarInt(int64(len(s))) + len(s) 9 } 10 11 func sizeOfVarNullBytes(b []byte) int { 12 if b == nil { 13 return sizeOfVarInt(-1) 14 } 15 n := len(b) 16 return sizeOfVarInt(int64(n)) + n 17 } 18 19 func sizeOfVarNullBytesIface(b Bytes) int { 20 if b == nil { 21 return sizeOfVarInt(-1) 22 } 23 n := b.Len() 24 return sizeOfVarInt(int64(n)) + n 25 } 26 27 func sizeOfVarInt(i int64) int { 28 return sizeOfUnsignedVarInt(uint64((i << 1) ^ (i >> 63))) // zig-zag encoding 29 } 30 31 func sizeOfUnsignedVarInt(i uint64) int { 32 return (bits.Len64(i|1) + 6) / 7 33 }