mirror of
https://github.com/restic/restic.git
synced 2026-11-19 23:01:08 +00:00
restic: Clean up restic.IDs type
IDs.Less can be rewritten as string(list[i][:]) < string(list[j][:]) Note that this does not copy the ID's. The Uniq method was no longer used. The String method has been reimplemented without first copying into a separate slice of a custom type.
This commit is contained in:
1 parent
0c749dd358
commit
63bed34608
4 files changed
+34
-99
No files matched your search
+18
-47
@@ -2,10 +2,10 @@ package restic
|
||||
|
||||
import (
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// IDs is an ordered list of IDs that implements sort.Interface.
|
||||
// IDs is a slice of IDs that implements sort.Interface and fmt.Stringer.
|
||||
type IDs []ID
|
||||
|
||||
func (ids IDs) Len() int {
|
||||
@@ -13,57 +13,28 @@ func (ids IDs) Len() int {
|
||||
}
|
||||
|
||||
func (ids IDs) Less(i, j int) bool {
|
||||
if len(ids[i]) < len(ids[j]) {
|
||||
return true
|
||||
}
|
||||
|
||||
for k, b := range ids[i] {
|
||||
if b == ids[j][k] {
|
||||
continue
|
||||
}
|
||||
|
||||
if b < ids[j][k] {
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
return false
|
||||
return string(ids[i][:]) < string(ids[j][:])
|
||||
}
|
||||
|
||||
func (ids IDs) Swap(i, j int) {
|
||||
ids[i], ids[j] = ids[j], ids[i]
|
||||
}
|
||||
|
||||
// Uniq returns list without duplicate IDs. The returned list retains the order
|
||||
// of the original list so that the order of the first occurrence of each ID
|
||||
// stays the same.
|
||||
func (ids IDs) Uniq() (list IDs) {
|
||||
seen := NewIDSet()
|
||||
|
||||
for _, id := range ids {
|
||||
if seen.Has(id) {
|
||||
continue
|
||||
}
|
||||
|
||||
list = append(list, id)
|
||||
seen.Insert(id)
|
||||
}
|
||||
|
||||
return list
|
||||
}
|
||||
|
||||
type shortID ID
|
||||
|
||||
func (id shortID) String() string {
|
||||
return hex.EncodeToString(id[:shortStr])
|
||||
}
|
||||
|
||||
func (ids IDs) String() string {
|
||||
elements := make([]shortID, 0, len(ids))
|
||||
for _, id := range ids {
|
||||
elements = append(elements, shortID(id))
|
||||
var sb strings.Builder
|
||||
sb.Grow(1 + (1+2*shortStr)*len(ids))
|
||||
|
||||
buf := make([]byte, 2*shortStr)
|
||||
|
||||
sb.WriteByte('[')
|
||||
for i, id := range ids {
|
||||
if i > 0 {
|
||||
sb.WriteByte(' ')
|
||||
}
|
||||
hex.Encode(buf, id[:shortStr])
|
||||
sb.Write(buf)
|
||||
}
|
||||
return fmt.Sprint(elements)
|
||||
sb.WriteByte(']')
|
||||
|
||||
return sb.String()
|
||||
}
|
||||
Reference in new issue
Block a user