summaryrefslogtreecommitdiffstats
path: root/modules
diff options
context:
space:
mode:
authorKN4CK3R <admin@oldschoolhack.me>2023-08-30 08:55:25 +0200
committerGitHub <noreply@github.com>2023-08-30 06:55:25 +0000
commit5315153059529f03b06c8f25487ffcc21ae3163f (patch)
treeb5f90daf2f1916f37656369865706d95872a7ae7 /modules
parent815d267c8031daa19b82b291a6393a54715567c0 (diff)
downloadgitea-5315153059529f03b06c8f25487ffcc21ae3163f.tar.gz
gitea-5315153059529f03b06c8f25487ffcc21ae3163f.zip
Use `Set[Type]` instead of `map[Type]bool/struct{}`. (#26804)
Diffstat (limited to 'modules')
-rw-r--r--modules/assetfs/layered.go19
-rw-r--r--modules/templates/util_dict.go10
2 files changed, 12 insertions, 17 deletions
diff --git a/modules/assetfs/layered.go b/modules/assetfs/layered.go
index d69732f81b..9678d23ad6 100644
--- a/modules/assetfs/layered.go
+++ b/modules/assetfs/layered.go
@@ -14,6 +14,7 @@ import (
"sort"
"time"
+ "code.gitea.io/gitea/modules/container"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/process"
"code.gitea.io/gitea/modules/util"
@@ -130,7 +131,7 @@ func readDir(layer *Layer, name string) ([]fs.FileInfo, error) {
// * false: only directories will be returned.
// The returned files are sorted by name.
func (l *LayeredFS) ListFiles(name string, fileMode ...bool) ([]string, error) {
- fileMap := map[string]bool{}
+ fileSet := make(container.Set[string])
for _, layer := range l.layers {
infos, err := readDir(layer, name)
if err != nil {
@@ -138,14 +139,11 @@ func (l *LayeredFS) ListFiles(name string, fileMode ...bool) ([]string, error) {
}
for _, info := range infos {
if shouldInclude(info, fileMode...) {
- fileMap[info.Name()] = true
+ fileSet.Add(info.Name())
}
}
}
- files := make([]string, 0, len(fileMap))
- for file := range fileMap {
- files = append(files, file)
- }
+ files := fileSet.Values()
sort.Strings(files)
return files, nil
}
@@ -161,7 +159,7 @@ func (l *LayeredFS) ListAllFiles(name string, fileMode ...bool) ([]string, error
}
func listAllFiles(layers []*Layer, name string, fileMode ...bool) ([]string, error) {
- fileMap := map[string]bool{}
+ fileSet := make(container.Set[string])
var list func(dir string) error
list = func(dir string) error {
for _, layer := range layers {
@@ -172,7 +170,7 @@ func listAllFiles(layers []*Layer, name string, fileMode ...bool) ([]string, err
for _, info := range infos {
path := util.PathJoinRelX(dir, info.Name())
if shouldInclude(info, fileMode...) {
- fileMap[path] = true
+ fileSet.Add(path)
}
if info.IsDir() {
if err = list(path); err != nil {
@@ -186,10 +184,7 @@ func listAllFiles(layers []*Layer, name string, fileMode ...bool) ([]string, err
if err := list(name); err != nil {
return nil, err
}
- var files []string
- for file := range fileMap {
- files = append(files, file)
- }
+ files := fileSet.Values()
sort.Strings(files)
return files, nil
}
diff --git a/modules/templates/util_dict.go b/modules/templates/util_dict.go
index 79c307b68d..8d6376b522 100644
--- a/modules/templates/util_dict.go
+++ b/modules/templates/util_dict.go
@@ -9,6 +9,7 @@ import (
"html/template"
"reflect"
+ "code.gitea.io/gitea/modules/container"
"code.gitea.io/gitea/modules/json"
"code.gitea.io/gitea/modules/setting"
)
@@ -51,7 +52,7 @@ func dict(args ...any) (map[string]any, error) {
return m, nil
}
-func dumpVarMarshalable(v any, dumped map[uintptr]bool) (ret any, ok bool) {
+func dumpVarMarshalable(v any, dumped container.Set[uintptr]) (ret any, ok bool) {
if v == nil {
return nil, true
}
@@ -61,11 +62,10 @@ func dumpVarMarshalable(v any, dumped map[uintptr]bool) (ret any, ok bool) {
}
if e.CanAddr() {
addr := e.UnsafeAddr()
- if dumped[addr] {
+ if !dumped.Add(addr) {
return "[dumped]", false
}
- dumped[addr] = true
- defer delete(dumped, addr)
+ defer dumped.Remove(addr)
}
switch e.Kind() {
case reflect.Bool, reflect.String,
@@ -107,7 +107,7 @@ func dumpVar(v any) template.HTML {
if setting.IsProd {
return "<pre>dumpVar: only available in dev mode</pre>"
}
- m, ok := dumpVarMarshalable(v, map[uintptr]bool{})
+ m, ok := dumpVarMarshalable(v, make(container.Set[uintptr]))
var dumpStr string
jsonBytes, err := json.MarshalIndent(m, "", " ")
if err != nil {