DataHoarder
e1d0e27c29
All checks were successful
continuous-integration/drone/push Build is passing
38 lines
590 B
Go
38 lines
590 B
Go
package utilities
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/binary"
|
|
)
|
|
|
|
func EncodeIntegerList(data []int) []byte {
|
|
message := &bytes.Buffer{}
|
|
buf := make([]byte, binary.MaxVarintLen64)
|
|
|
|
for _, i := range data {
|
|
n := binary.PutVarint(buf, int64(i))
|
|
_, _ = message.Write(buf[:n])
|
|
}
|
|
|
|
return message.Bytes()
|
|
}
|
|
func DecodeIntegerList(data []byte) []int {
|
|
buf := bytes.NewBuffer(data)
|
|
var result []int
|
|
|
|
for {
|
|
if buf.Len() <= 0 {
|
|
break
|
|
}
|
|
|
|
i, err := binary.ReadVarint(buf)
|
|
if err != nil {
|
|
//TODO: maybe should error
|
|
break
|
|
}
|
|
result = append(result, int(i))
|
|
}
|
|
|
|
return result
|
|
}
|