Skip to content

Commit

Permalink
all: swap sort for slices for []string values
Browse files Browse the repository at this point in the history
For example, sort.Strings now directly calls slices.Sort since Go 1.22.
As we are gradually replacing the sort package for the new generic
sort APIs in the slices package, this helps with consistency.

Signed-off-by: Daniel Martí <[email protected]>
Change-Id: I3766518829b0f11e419918b9641332b38f270712
Dispatch-Trailer: {"type":"trybot","CL":1206381,"patchset":1,"ref":"refs/changes/81/1206381/1","targetBranch":"master"}
  • Loading branch information
mvdan authored and cueckoo committed Dec 27, 2024
1 parent 89c861f commit a0b8aa5
Show file tree
Hide file tree
Showing 11 changed files with 22 additions and 22 deletions.
6 changes: 3 additions & 3 deletions cue/build/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
package build

import (
"sort"
"slices"
"strconv"

"cuelang.org/go/cue/errors"
Expand Down Expand Up @@ -78,7 +78,7 @@ func (inst *Instance) complete() errors.Error {
}
}

sort.Strings(paths)
slices.Sort(paths)

if inst.loadFunc != nil {
for i, path := range paths {
Expand Down Expand Up @@ -147,7 +147,7 @@ func (inst *Instance) complete() errors.Error {
for dep := range deps {
inst.Deps = append(inst.Deps, dep)
}
sort.Strings(inst.Deps)
slices.Sort(inst.Deps)

for _, dep := range inst.Deps {
p1 := deps[dep]
Expand Down
4 changes: 2 additions & 2 deletions cue/load/instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"context"
"fmt"
"io/fs"
"sort"
"slices"
"strconv"
"strings"

Expand Down Expand Up @@ -218,7 +218,7 @@ func loadPackages(
for p := range pkgPaths {
pkgPathSlice = append(pkgPathSlice, p)
}
sort.Strings(pkgPathSlice)
slices.Sort(pkgPathSlice)
return modpkgload.LoadPackages(
ctx,
cfg.Module,
Expand Down
3 changes: 1 addition & 2 deletions cue/load/loader_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
pathpkg "path"
"path/filepath"
"slices"
"sort"
"strconv"
"strings"

Expand Down Expand Up @@ -350,7 +349,7 @@ func cleanImports(m map[string][]token.Pos) ([]string, map[string][]token.Pos) {
for path := range m {
all = append(all, path)
}
sort.Strings(all)
slices.Sort(all)
return all, m
}

Expand Down
4 changes: 2 additions & 2 deletions encoding/jsonschema/external_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"io"
"os"
"path"
"sort"
"slices"
"strings"
"testing"

Expand Down Expand Up @@ -259,7 +259,7 @@ func sortedKeys[T any](m map[string]T) []string {
for k := range m {
ks = append(ks, k)
}
sort.Strings(ks)
slices.Sort(ks)
return ks
}

Expand Down
4 changes: 2 additions & 2 deletions encoding/jsonschema/teststats.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"log"
"os"
"path"
"sort"
"slices"

"cuelang.org/go/cue/token"
"cuelang.org/go/encoding/jsonschema/internal/externaltest"
Expand Down Expand Up @@ -85,6 +85,6 @@ func sortedKeys[T any](m map[string]T) []string {
for k := range m {
ks = append(ks, k)
}
sort.Strings(ks)
slices.Sort(ks)
return ks
}
3 changes: 2 additions & 1 deletion encoding/openapi/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"math"
"path"
"regexp"
"slices"
"sort"
"strings"

Expand Down Expand Up @@ -152,7 +153,7 @@ func schemas(g *Generator, inst cue.InstanceOrValue) (schemas *ast.StructLit, er
for k := range c.externalRefs {
external = append(external, k)
}
sort.Strings(external)
slices.Sort(external)

for _, k := range external {
ext := c.externalRefs[k]
Expand Down
4 changes: 2 additions & 2 deletions internal/core/adt/eval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"flag"
"fmt"
"path/filepath"
"sort"
"slices"
"strings"
"testing"

Expand Down Expand Up @@ -197,7 +197,7 @@ func runEvalTest(t *cuetxtar.Test, version internal.EvaluatorVersion, flags cued
keys[i] = k
i++
}
sort.Strings(keys)
slices.Sort(keys)
for _, s := range keys {
t.Errorf(" -- path: %s", s)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/mod/modimports/modimports.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"
"io/fs"
"path"
"sort"
"slices"
"strconv"
"strings"

Expand Down Expand Up @@ -60,7 +60,7 @@ func AllImports(modFilesIter func(func(ModuleFile, error) bool)) (_ []string, re
for p := range pkgPaths {
pkgPathSlice = append(pkgPathSlice, p)
}
sort.Strings(pkgPathSlice)
slices.Sort(pkgPathSlice)
return pkgPathSlice, nil
}

Expand Down
3 changes: 1 addition & 2 deletions internal/mod/modpkgload/pkgload.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"io/fs"
"runtime"
"slices"
"sort"
"strings"
"sync/atomic"

Expand Down Expand Up @@ -310,7 +309,7 @@ func (pkgs *Packages) load(ctx context.Context, pkg *Package) {
for imp := range importsMap {
imports = append(imports, imp)
}
sort.Strings(imports) // Make the algorithm deterministic for tests.
slices.Sort(imports) // Make the algorithm deterministic for tests.

pkg.imports = make([]*Package, 0, len(imports))
var importFlags Flags
Expand Down
4 changes: 2 additions & 2 deletions internal/vcs/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"context"
"fmt"
"path/filepath"
"sort"
"slices"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -72,7 +72,7 @@ func (v gitVCS) ListFiles(ctx context.Context, dir string, paths ...string) ([]s
return nil, nil
}
files := strings.Split(out, "\x00")
sort.Strings(files)
slices.Sort(files)
return files, nil
}

Expand Down
5 changes: 3 additions & 2 deletions pkg/list/sort.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package list

import (
"slices"
"sort"

"cuelang.org/go/cue"
Expand Down Expand Up @@ -212,7 +213,7 @@ func SortStable(list []cue.Value, cmp cue.Value) (sorted []cue.Value, err error)

// SortStrings sorts a list of strings in increasing order.
func SortStrings(a []string) []string {
sort.Strings(a)
slices.Sort(a)
return a
}

Expand All @@ -226,5 +227,5 @@ func IsSorted(list []cue.Value, cmp cue.Value) bool {

// IsSortedStrings tests whether a list is a sorted lists of strings.
func IsSortedStrings(a []string) bool {
return sort.StringsAreSorted(a)
return slices.IsSorted(a)
}

0 comments on commit a0b8aa5

Please sign in to comment.