restic/internal/restic/ids.go

41 lines
652 B
Go
Raw Normal View History

2016-08-31 17:18:51 +00:00
package restic
import (
"encoding/hex"
"strings"
2016-08-31 17:18:51 +00:00
)
// IDs is a slice of IDs that implements sort.Interface and fmt.Stringer.
2016-08-31 17:18:51 +00:00
type IDs []ID
func (ids IDs) Len() int {
return len(ids)
}
func (ids IDs) Less(i, j int) bool {
return string(ids[i][:]) < string(ids[j][:])
2016-08-31 17:18:51 +00:00
}
func (ids IDs) Swap(i, j int) {
ids[i], ids[j] = ids[j], ids[i]
}
func (ids IDs) String() string {
var sb strings.Builder
sb.Grow(1 + (1+2*shortStr)*len(ids))
buf := make([]byte, 2*shortStr)
2016-08-31 17:18:51 +00:00
sb.WriteByte('[')
for i, id := range ids {
if i > 0 {
sb.WriteByte(' ')
2016-08-31 17:18:51 +00:00
}
hex.Encode(buf, id[:shortStr])
sb.Write(buf)
2016-08-31 17:18:51 +00:00
}
sb.WriteByte(']')
2016-08-31 17:18:51 +00:00
return sb.String()
2016-08-31 17:18:51 +00:00
}