Skip to content

Commit

Permalink
refactor: simplify slice logic
Browse files Browse the repository at this point in the history
  • Loading branch information
tri-adam committed Mar 19, 2024
1 parent 096293c commit 7ad27b6
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 50 deletions.
45 changes: 15 additions & 30 deletions pkg/integrity/select.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2020-2023, Sylabs Inc. All rights reserved.
// Copyright (c) 2020-2024, Sylabs Inc. All rights reserved.
// This software is licensed under a 3-clause BSD license. Please consult the LICENSE.md file
// distributed with the sources of this project regarding your rights to use or distribute this
// software.
Expand All @@ -7,9 +7,10 @@ package integrity

import (
"bytes"
"cmp"
"errors"
"fmt"
"sort"
"slices"

"github.com/ProtonMail/go-crypto/openpgp/clearsign"
"github.com/sylabs/sif/v2/pkg/sif"
Expand All @@ -20,21 +21,18 @@ var (
errNoGroupsFound = errors.New("no groups found")
)

// insertSorted inserts unique vals into the sorted slice s.
func insertSorted(s []uint32, vals ...uint32) []uint32 {
for _, val := range vals {
val := val

i := sort.Search(len(s), func(i int) bool { return s[i] >= val })
if i < len(s) && s[i] == val {
continue
}
// insertSorted inserts v into the sorted slice s. If s already contains v, the original slice is
// returned.
func insertSorted[S ~[]E, E cmp.Ordered](s S, v E) S { //nolint:ireturn
return insertSortedFunc(s, v, cmp.Compare[E])
}

s = append(s, 0)
copy(s[i+1:], s[i:])
s[i] = val
// insertSorted inserts v into the sorted slice s, using comparison function cmp. If s already
// contains v, the original slice is returned.
func insertSortedFunc[S ~[]E, E any](s S, v E, cmp func(E, E) int) S { //nolint:ireturn
if i, found := slices.BinarySearchFunc(s, v, cmp); !found {
return slices.Insert(s, i, v)
}

return s
}

Expand Down Expand Up @@ -191,22 +189,9 @@ func getFingerprints(sigs []sif.Descriptor) ([][]byte, error) {
return nil, err
}

if len(fp) == 0 {
continue
}

// Check if fingerprint is already in list.
i := sort.Search(len(fps), func(i int) bool {
return bytes.Compare(fps[i], fp) >= 0
})
if i < len(fps) && bytes.Equal(fps[i], fp) {
continue
if len(fp) > 0 {
fps = insertSortedFunc(fps, fp, bytes.Compare)
}

// Insert into (sorted) list.
fps = append(fps, []byte{})
copy(fps[i+1:], fps[i:])
fps[i] = fp
}

return fps, nil
Expand Down
14 changes: 4 additions & 10 deletions pkg/integrity/sign.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2020-2023, Sylabs Inc. All rights reserved.
// Copyright (c) 2020-2024, Sylabs Inc. All rights reserved.
// This software is licensed under a 3-clause BSD license. Please consult the LICENSE.md file
// distributed with the sources of this project regarding your rights to use or distribute this
// software.
Expand All @@ -13,7 +13,7 @@ import (
"errors"
"fmt"
"io"
"sort"
"slices"
"time"

"github.com/ProtonMail/go-crypto/openpgp"
Expand Down Expand Up @@ -138,13 +138,7 @@ func (gs *groupSigner) addObject(od sif.Descriptor) error {
}

// Insert into sorted descriptor list, if not already present.
i := sort.Search(len(gs.ods), func(i int) bool { return gs.ods[i].ID() >= od.ID() })
if i < len(gs.ods) && gs.ods[i].ID() == od.ID() {
return nil
}
gs.ods = append(gs.ods, sif.Descriptor{})
copy(gs.ods[i+1:], gs.ods[i:])
gs.ods[i] = od
gs.ods = insertSortedFunc(gs.ods, od, func(a, b sif.Descriptor) int { return int(a.ID()) - int(b.ID()) })

return nil
}
Expand Down Expand Up @@ -284,7 +278,7 @@ func withGroupedObjects(f *sif.FileImage, ids []uint32, fn func(uint32, []uint32
groupObjectIDs[groupID] = append(groupObjectIDs[groupID], id)
}

sort.Slice(groupIDs, func(i, j int) bool { return groupIDs[i] < groupIDs[j] })
slices.Sort(groupIDs)

for _, groupID := range groupIDs {
if err := fn(groupID, groupObjectIDs[groupID]); err != nil {
Expand Down
10 changes: 5 additions & 5 deletions pkg/integrity/sign_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2020-2023, Sylabs Inc. All rights reserved.
// Copyright (c) 2020-2024, Sylabs Inc. All rights reserved.
// This software is licensed under a 3-clause BSD license. Please consult the LICENSE.md file
// distributed with the sources of this project regarding your rights to use or distribute this
// software.
Expand All @@ -12,7 +12,7 @@ import (
"errors"
"os"
"path/filepath"
"reflect"
"slices"
"testing"

"github.com/ProtonMail/go-crypto/openpgp"
Expand Down Expand Up @@ -82,7 +82,7 @@ func TestOptSignGroupObjects(t *testing.T) {
for _, od := range gs.ods {
got = append(got, od.ID())
}
if want := tt.ids; !reflect.DeepEqual(got, want) {
if want := tt.ids; !slices.Equal(got, want) {
t.Errorf("got objects %v, want %v", got, want)
}
}
Expand Down Expand Up @@ -221,7 +221,7 @@ func TestNewGroupSigner(t *testing.T) {
for _, od := range s.ods {
got = append(got, od.ID())
}
if want := tt.wantObjects; !reflect.DeepEqual(got, want) {
if want := tt.wantObjects; !slices.Equal(got, want) {
t.Errorf("got objects %v, want %v", got, want)
}

Expand Down Expand Up @@ -561,7 +561,7 @@ func TestNewSigner(t *testing.T) {
got = append(got, od.ID())
}

if !reflect.DeepEqual(got, want) {
if !slices.Equal(got, want) {
t.Errorf("got objects %v, want %v", got, want)
}
}
Expand Down
8 changes: 3 additions & 5 deletions pkg/integrity/verify.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2020-2023, Sylabs Inc. All rights reserved.
// Copyright (c) 2020-2024, Sylabs Inc. All rights reserved.
// This software is licensed under a 3-clause BSD license. Please consult the LICENSE.md file
// distributed with the sources of this project regarding your rights to use or distribute this
// software.
Expand All @@ -14,7 +14,7 @@ import (
"errors"
"fmt"
"io"
"sort"
"slices"
"strings"

"github.com/ProtonMail/go-crypto/openpgp"
Expand Down Expand Up @@ -572,9 +572,7 @@ func (v *Verifier) fingerprints(any bool) ([][]byte, error) {
}
}

sort.Slice(fps, func(i, j int) bool {
return bytes.Compare(fps[i], fps[j]) < 0
})
slices.SortFunc(fps, bytes.Compare)

return fps, nil
}
Expand Down

0 comments on commit 7ad27b6

Please sign in to comment.