all: Use slices.Equal

This commit is contained in:
minxinyi
2025-08-25 02:35:18 +08:00
committed by GitHub
parent ff3ae62933
commit ecdef2be70
3 changed files with 6 additions and 29 deletions

View File

@@ -20,6 +20,7 @@ import (
"net" "net"
"os" "os"
"path/filepath" "path/filepath"
"slices"
"sort" "sort"
"strings" "strings"
"unicode" "unicode"
@@ -228,17 +229,7 @@ func compareStringSlices(a, b []string) bool {
return false return false
} }
if len(a) != len(b) { return slices.Equal(a, b)
return false
}
for i := range a {
if a[i] != b[i] {
return false
}
}
return true
} }
// SliceToLower goes through the source slice and lowers all values. // SliceToLower goes through the source slice and lowers all values.

View File

@@ -28,7 +28,7 @@ func (entry menuCacheEntry) matches(menuList []Menu) bool {
return false return false
} }
for i, m := range menuList { for i, m := range menuList {
if !menuEqual(m, entry.in[i]) { if !slices.Equal(m, entry.in[i]) {
return false return false
} }
} }
@@ -46,21 +46,6 @@ type menuCache struct {
m map[string][]menuCacheEntry m map[string][]menuCacheEntry
} }
// menuEqual checks if two menus are equal.
func menuEqual(m1, m2 Menu) bool {
if len(m1) != len(m2) {
return false
}
for i := range m1 {
if m1[i] != m2[i] {
return false
}
}
return true
}
// get retrieves a menu from the cache based on the provided key and menuLists. // get retrieves a menu from the cache based on the provided key and menuLists.
// If the menu is not found, it applies the provided function and caches the result. // If the menu is not found, it applies the provided function and caches the result.
func (c *menuCache) get(key string, apply func(m Menu), menuLists ...Menu) (Menu, bool) { func (c *menuCache) get(key string, apply func(m Menu), menuLists ...Menu) (Menu, bool) {

View File

@@ -14,6 +14,7 @@
package navigation package navigation
import ( import (
"slices"
"sync" "sync"
"sync/atomic" "sync/atomic"
"testing" "testing"
@@ -64,8 +65,8 @@ func TestMenuCache(t *testing.T) {
l1.Unlock() l1.Unlock()
m2, c2 := c1.get("k1", nil, m) m2, c2 := c1.get("k1", nil, m)
c.Assert(c2, qt.Equals, true) c.Assert(c2, qt.Equals, true)
c.Assert(menuEqual(m, m2), qt.Equals, true) c.Assert(slices.Equal(m, m2), qt.Equals, true)
c.Assert(menuEqual(m, menu), qt.Equals, true) c.Assert(slices.Equal(m, menu), qt.Equals, true)
c.Assert(m, qt.Not(qt.IsNil)) c.Assert(m, qt.Not(qt.IsNil))
l2.Lock() l2.Lock()