all: use slices.Sort() to sort strings

This commit is contained in:
wit 2025-11-02 09:19:13 +08:00
parent 447b5f7e19
commit bf48780fcb
21 changed files with 40 additions and 42 deletions

View file

@ -23,7 +23,6 @@ import (
"reflect" "reflect"
"regexp" "regexp"
"slices" "slices"
"sort"
"strings" "strings"
"text/template" "text/template"
"unicode" "unicode"
@ -280,7 +279,7 @@ func iterSorted[V any](inp map[string]V, onItem func(string, V) error) error {
for key := range inp { for key := range inp {
sortedKeys = append(sortedKeys, key) sortedKeys = append(sortedKeys, key)
} }
sort.Strings(sortedKeys) slices.Sort(sortedKeys)
for _, key := range sortedKeys { for _, key := range sortedKeys {
if err := onItem(key, inp[key]); err != nil { if err := onItem(key, inp[key]); err != nil {

View file

@ -20,7 +20,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"net/netip" "net/netip"
"sort" "slices"
"strconv" "strconv"
"strings" "strings"
"time" "time"
@ -90,7 +90,7 @@ func showAttributeCounts(ns nodeSet) {
maxlength = len(key) maxlength = len(key)
} }
} }
sort.Strings(keys) slices.Sort(keys)
fmt.Println("ENR attribute counts:") fmt.Println("ENR attribute counts:")
for _, key := range keys { for _, key := range keys {
fmt.Printf("%s%s: %d\n", strings.Repeat(" ", maxlength-len(key)+1), key, attrcount[key]) fmt.Printf("%s%s: %d\n", strings.Repeat(" ", maxlength-len(key)+1), key, attrcount[key])

View file

@ -24,7 +24,7 @@ import (
"os/signal" "os/signal"
"path/filepath" "path/filepath"
"regexp" "regexp"
"sort" "slices"
"strings" "strings"
"sync" "sync"
"syscall" "syscall"
@ -314,7 +314,7 @@ func (c *Console) Welcome() {
for api, version := range apis { for api, version := range apis {
modules = append(modules, fmt.Sprintf("%s:%s", api, version)) modules = append(modules, fmt.Sprintf("%s:%s", api, version))
} }
sort.Strings(modules) slices.Sort(modules)
message += " modules: " + strings.Join(modules, " ") + "\n" message += " modules: " + strings.Join(modules, " ") + "\n"
} }
message += "\nTo exit, press ctrl-d or type exit" message += "\nTo exit, press ctrl-d or type exit"

View file

@ -20,7 +20,6 @@ import (
"encoding/json" "encoding/json"
"maps" "maps"
"slices" "slices"
"sort"
"strconv" "strconv"
"strings" "strings"
@ -56,7 +55,7 @@ func NewWitnessStats() *WitnessStats {
func (s *WitnessStats) Add(nodes map[string][]byte, owner common.Hash) { func (s *WitnessStats) Add(nodes map[string][]byte, owner common.Hash) {
// Extract paths from the nodes map // Extract paths from the nodes map
paths := slices.Collect(maps.Keys(nodes)) paths := slices.Collect(maps.Keys(nodes))
sort.Strings(paths) slices.Sort(paths)
for i, path := range paths { for i, path := range paths {
// If current path is a prefix of the next path, it's not a leaf. // If current path is a prefix of the next path, it's not a leaf.

View file

@ -19,7 +19,7 @@ package vm
import ( import (
"fmt" "fmt"
"math" "math"
"sort" "slices"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/tracing" "github.com/ethereum/go-ethereum/core/tracing"
@ -65,7 +65,7 @@ func ActivateableEips() []string {
for k := range activators { for k := range activators {
nums = append(nums, fmt.Sprintf("%d", k)) nums = append(nums, fmt.Sprintf("%d", k))
} }
sort.Strings(nums) slices.Sort(nums)
return nums return nums
} }

View file

@ -21,6 +21,7 @@ import (
"fmt" "fmt"
"math" "math"
mrand "math/rand" mrand "math/rand"
"slices"
"sort" "sort"
"time" "time"
@ -975,7 +976,7 @@ func (f *TxFetcher) forEachPeer(peers map[string]struct{}, do func(peer string))
for peer := range peers { for peer := range peers {
list = append(list, peer) list = append(list, peer)
} }
sort.Strings(list) slices.Sort(list)
rotateStrings(list, f.rand.Intn(len(list))) rotateStrings(list, f.rand.Intn(len(list)))
for _, peer := range list { for _, peer := range list {
do(peer) do(peer)

View file

@ -20,7 +20,6 @@ import (
"bytes" "bytes"
"crypto/rand" "crypto/rand"
"slices" "slices"
"sort"
"strconv" "strconv"
"testing" "testing"
@ -135,7 +134,7 @@ func TestDatabaseSuite(t *testing.T, New func() ethdb.KeyValueStore) {
defer db.Close() defer db.Close()
keys := []string{"1", "2", "3", "4", "6", "10", "11", "12", "20", "21", "22"} keys := []string{"1", "2", "3", "4", "6", "10", "11", "12", "20", "21", "22"}
sort.Strings(keys) // 1, 10, 11, etc slices.Sort(keys) // 1, 10, 11, etc
for _, k := range keys { for _, k := range keys {
if err := db.Put([]byte(k), nil); err != nil { if err := db.Put([]byte(k), nil); err != nil {
@ -904,7 +903,7 @@ func iterateKeys(it ethdb.Iterator) []string {
for it.Next() { for it.Next() {
keys = append(keys, string(it.Key())) keys = append(keys, string(it.Key()))
} }
sort.Strings(keys) slices.Sort(keys)
it.Release() it.Release()
return keys return keys
} }

View file

@ -20,7 +20,7 @@ package memorydb
import ( import (
"bytes" "bytes"
"errors" "errors"
"sort" "slices"
"strings" "strings"
"sync" "sync"
@ -184,7 +184,7 @@ func (db *Database) NewIterator(prefix []byte, start []byte) ethdb.Iterator {
} }
} }
// Sort the items and retrieve the associated values // Sort the items and retrieve the associated values
sort.Strings(keys) slices.Sort(keys)
for _, key := range keys { for _, key := range keys {
values = append(values, db.db[key]) values = append(values, db.db[key])
} }

View file

@ -21,7 +21,7 @@ import (
"io" "io"
"os" "os"
"path/filepath" "path/filepath"
"sort" "slices"
"strings" "strings"
) )
@ -71,6 +71,6 @@ func DiffHashes(a map[string][32]byte, b map[string][32]byte) []string {
updates = append(updates, file) updates = append(updates, file)
} }
} }
sort.Strings(updates) slices.Sort(updates)
return updates return updates
} }

View file

@ -20,7 +20,7 @@ import (
"fmt" "fmt"
"os" "os"
"regexp" "regexp"
"sort" "slices"
"strings" "strings"
"github.com/ethereum/go-ethereum/internal/version" "github.com/ethereum/go-ethereum/internal/version"
@ -277,7 +277,7 @@ func CheckEnvVars(ctx *cli.Context, flags []cli.Flag, prefix string) {
} }
} }
keyvals := os.Environ() keyvals := os.Environ()
sort.Strings(keyvals) slices.Sort(keyvals)
for _, keyval := range keyvals { for _, keyval := range keyvals {
key := strings.Split(keyval, "=")[0] key := strings.Split(keyval, "=")[0]

View file

@ -18,7 +18,7 @@ package jsre
import ( import (
"regexp" "regexp"
"sort" "slices"
"strings" "strings"
"github.com/dop251/goja" "github.com/dop251/goja"
@ -88,6 +88,6 @@ func getCompletions(vm *goja.Runtime, line string) (results []string) {
} }
} }
sort.Strings(results) slices.Sort(results)
return results return results
} }

View file

@ -20,7 +20,7 @@ import (
"fmt" "fmt"
"io" "io"
"reflect" "reflect"
"sort" "slices"
"strconv" "strconv"
"strings" "strings"
@ -221,8 +221,8 @@ func (ctx ppctx) fields(obj *goja.Object) []string {
} }
} }
iterOwnAndConstructorKeys(ctx.vm, obj, add) iterOwnAndConstructorKeys(ctx.vm, obj, add)
sort.Strings(vals) slices.Sort(vals)
sort.Strings(methods) slices.Sort(methods)
return append(vals, methods...) return append(vals, methods...)
} }

View file

@ -19,7 +19,7 @@ package prometheus
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"sort" "slices"
"strconv" "strconv"
"strings" "strings"
@ -144,7 +144,7 @@ func (c *collector) writeGaugeInfo(name string, value metrics.GaugeInfoValue) {
for k, v := range value { for k, v := range value {
kvs = append(kvs, fmt.Sprintf("%v=%q", k, v)) kvs = append(kvs, fmt.Sprintf("%v=%q", k, v))
} }
sort.Strings(kvs) slices.Sort(kvs)
c.buff.WriteString(fmt.Sprintf("{%v} 1\n\n", strings.Join(kvs, ", "))) c.buff.WriteString(fmt.Sprintf("{%v} 1\n\n", strings.Join(kvs, ", ")))
} }

View file

@ -20,7 +20,7 @@ package prometheus
import ( import (
"fmt" "fmt"
"net/http" "net/http"
"sort" "slices"
"github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/metrics" "github.com/ethereum/go-ethereum/metrics"
@ -34,7 +34,7 @@ func Handler(reg metrics.Registry) http.Handler {
reg.Each(func(name string, i interface{}) { reg.Each(func(name string, i interface{}) {
names = append(names, name) names = append(names, name)
}) })
sort.Strings(names) slices.Sort(names)
// Aggregate all the metrics into a Prometheus collector // Aggregate all the metrics into a Prometheus collector
c := newCollector() c := newCollector()

View file

@ -3,7 +3,7 @@ package metrics
import ( import (
"errors" "errors"
"fmt" "fmt"
"sort" "slices"
"strings" "strings"
"sync" "sync"
) )
@ -54,7 +54,7 @@ func (r *orderedRegistry) Each(f func(string, interface{})) {
for name := range reg { for name := range reg {
names = append(names, name) names = append(names, name)
} }
sort.Strings(names) slices.Sort(names)
for _, name := range names { for _, name := range names {
f(name, reg[name]) f(name, reg[name])
} }

View file

@ -24,7 +24,7 @@ import (
"io" "io"
"net" "net"
"net/http" "net/http"
"sort" "slices"
"strconv" "strconv"
"strings" "strings"
"sync" "sync"
@ -182,7 +182,7 @@ func (h *httpServer) start() error {
for path := range h.handlerNames { for path := range h.handlerNames {
paths = append(paths, path) paths = append(paths, path)
} }
sort.Strings(paths) slices.Sort(paths)
logged := make(map[string]bool, len(paths)) logged := make(map[string]bool, len(paths))
for _, path := range paths { for _, path := range paths {
name := h.handlerNames[path] name := h.handlerNames[path]

View file

@ -21,7 +21,7 @@ import (
"fmt" "fmt"
"go/format" "go/format"
"go/types" "go/types"
"sort" "slices"
"github.com/ethereum/go-ethereum/rlp/internal/rlpstruct" "github.com/ethereum/go-ethereum/rlp/internal/rlpstruct"
"golang.org/x/tools/go/packages" "golang.org/x/tools/go/packages"
@ -194,7 +194,7 @@ func (ctx *genContext) importsList() []string {
imp = append(imp, fmt.Sprintf("%s %q", p.alias, path)) imp = append(imp, fmt.Sprintf("%s %q", p.alias, path))
} }
} }
sort.Strings(imp) slices.Sort(imp)
return imp return imp
} }

View file

@ -26,7 +26,6 @@ import (
"reflect" "reflect"
"regexp" "regexp"
"slices" "slices"
"sort"
"strconv" "strconv"
"strings" "strings"
@ -417,7 +416,7 @@ func (typedData *TypedData) EncodeType(primaryType string) hexutil.Bytes {
deps := typedData.Dependencies(primaryType, []string{}) deps := typedData.Dependencies(primaryType, []string{})
if len(deps) > 0 { if len(deps) > 0 {
slicedDeps := deps[1:] slicedDeps := deps[1:]
sort.Strings(slicedDeps) slices.Sort(slicedDeps)
deps = append([]string{primaryType}, slicedDeps...) deps = append([]string{primaryType}, slicedDeps...)
} }

View file

@ -19,7 +19,7 @@ package tests
import ( import (
"fmt" "fmt"
"math/big" "math/big"
"sort" "slices"
"github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/params"
) )
@ -740,7 +740,7 @@ func AvailableForks() []string {
for k := range Forks { for k := range Forks {
availableForks = append(availableForks, k) availableForks = append(availableForks, k)
} }
sort.Strings(availableForks) slices.Sort(availableForks)
return availableForks return availableForks
} }

View file

@ -26,7 +26,7 @@ import (
"reflect" "reflect"
"regexp" "regexp"
"runtime" "runtime"
"sort" "slices"
"strings" "strings"
"testing" "testing"
@ -269,7 +269,7 @@ func sortedMapKeys(m reflect.Value) []string {
for i, k := range m.MapKeys() { for i, k := range m.MapKeys() {
keys[i] = k.String() keys[i] = k.String()
} }
sort.Strings(keys) slices.Sort(keys)
return keys return keys
} }

View file

@ -25,6 +25,7 @@ import (
"io" "io"
"math/rand" "math/rand"
"reflect" "reflect"
"slices"
"sort" "sort"
"strings" "strings"
"testing" "testing"
@ -1309,7 +1310,7 @@ func printSet(set *trienode.NodeSet) string {
for k := range set.Nodes { for k := range set.Nodes {
paths = append(paths, k) paths = append(paths, k)
} }
sort.Strings(paths) slices.Sort(paths)
for _, path := range paths { for _, path := range paths {
n := set.Nodes[path] n := set.Nodes[path]