FinalCommander/utilities/utilities.go
DataHoarder e1d0e27c29
All checks were successful
continuous-integration/drone/push Build is passing
Use backing database for aliases and/or access checks
2022-01-19 21:46:05 +01:00

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
}