"io"
"io/fs"
"path"
- "sort"
+ "slices"
"strings"
"time"
)
for k := range paxHdrs {
keys = append(keys, k)
}
- sort.Strings(keys)
+ slices.Sort(keys)
// Write each record to a buffer.
var buf strings.Builder
"os"
"path"
"reflect"
- "sort"
+ "slices"
"strings"
"testing"
"testing/fstest"
bytes.Index(buf.Bytes(), []byte("foo=foo")),
bytes.Index(buf.Bytes(), []byte("qux=qux")),
}
- if !sort.IntsAreSorted(indices) {
+ if !slices.IsSorted(indices) {
t.Fatal("PAX headers are not sorted")
}
}
"os"
"path"
"path/filepath"
- "sort"
+ "slices"
"strings"
"sync"
"time"
}
}
- sort.Slice(r.fileList, func(i, j int) bool { return fileEntryLess(r.fileList[i].name, r.fileList[j].name) })
+ slices.SortFunc(r.fileList, func(a, b fileListEntry) int {
+ return fileEntryCompare(a.name, b.name)
+ })
})
}
-func fileEntryLess(x, y string) bool {
+func fileEntryCompare(x, y string) int {
xdir, xelem, _ := split(x)
ydir, yelem, _ := split(y)
- return xdir < ydir || xdir == ydir && xelem < yelem
+ if xdir != ydir {
+ return strings.Compare(xdir, ydir)
+ }
+ return strings.Compare(xelem, yelem)
}
// Open opens the named file in the ZIP archive,
dir, elem, _ := split(name)
files := r.fileList
- i := sort.Search(len(files), func(i int) bool {
- idir, ielem, _ := split(files[i].name)
- return idir > dir || idir == dir && ielem >= elem
+ i, _ := slices.BinarySearchFunc(files, dir, func(a fileListEntry, dir string) (ret int) {
+ idir, ielem, _ := split(a.name)
+ if dir != idir {
+ return strings.Compare(idir, dir)
+ }
+ return strings.Compare(ielem, elem)
})
if i < len(files) {
fname := files[i].name
func (r *Reader) openReadDir(dir string) []fileListEntry {
files := r.fileList
- i := sort.Search(len(files), func(i int) bool {
- idir, _, _ := split(files[i].name)
- return idir >= dir
+ i, _ := slices.BinarySearchFunc(files, dir, func(a fileListEntry, dir string) int {
+ idir, _, _ := split(a.name)
+ if dir != idir {
+ return strings.Compare(idir, dir)
+ }
+ // find the first entry with dir
+ return +1
})
- j := sort.Search(len(files), func(j int) bool {
- jdir, _, _ := split(files[j].name)
- return jdir > dir
+ j, _ := slices.BinarySearchFunc(files, dir, func(a fileListEntry, dir string) int {
+ jdir, _, _ := split(a.name)
+ if dir != jdir {
+ return strings.Compare(jdir, dir)
+ }
+ // find the last entry with dir
+ return -1
})
return files[i:j]
}
import (
"bytes"
+ "cmp"
"errors"
"fmt"
"hash"
"internal/testenv"
"io"
"runtime"
- "sort"
+ "slices"
"strings"
"testing"
"time"
if len(p) == 0 {
return
}
- skipParts := sort.Search(len(r.buf), func(i int) bool {
- part := &r.buf[i]
- return part.off+part.n > off
+ skipParts, _ := slices.BinarySearchFunc(r.buf, off, func(rb repeatedByte, off int64) int {
+ return cmp.Compare(rb.off+rb.n, off)
})
parts := r.buf[skipParts:]
if len(parts) > 0 {
"fmt"
"io"
"os"
- "sort"
+ "slices"
"strconv"
"unicode"
)
// Binary search to find a matching byte slice.
var needle []byte
var haystack [][]byte // Assume sorted
- i := sort.Search(len(haystack), func(i int) bool {
- // Return haystack[i] >= needle.
- return bytes.Compare(haystack[i], needle) >= 0
- })
- if i < len(haystack) && bytes.Equal(haystack[i], needle) {
+ _, found := slices.BinarySearchFunc(haystack, needle, bytes.Compare)
+ if found {
// Found it!
}
}
tg.makeTempdir()
tg.setenv("GOCACHE", tg.tempdir)
- tg.run("list", "-test", "-deps", "sort")
- tg.grepStdout(`^sort.test$`, "missing test main")
- tg.grepStdout(`^sort$`, "missing real sort")
- tg.grepStdout(`^sort \[sort.test\]$`, "missing test copy of sort")
- tg.grepStdout(`^testing \[sort.test\]$`, "missing test copy of testing")
+ tg.run("list", "-test", "-deps", "bytes")
+ tg.grepStdout(`^bytes.test$`, "missing test main")
+ tg.grepStdout(`^bytes$`, "missing real bytes")
+ tg.grepStdout(`^bytes \[bytes.test\]$`, "missing test copy of bytes")
+ tg.grepStdout(`^testing \[bytes.test\]$`, "missing test copy of testing")
tg.grepStdoutNot(`^testing$`, "unexpected real copy of testing")
- tg.run("list", "-test", "sort")
- tg.grepStdout(`^sort.test$`, "missing test main")
- tg.grepStdout(`^sort$`, "missing real sort")
- tg.grepStdout(`^sort \[sort.test\]$`, "unexpected test copy of sort")
- tg.grepStdoutNot(`^testing \[sort.test\]$`, "unexpected test copy of testing")
+ tg.run("list", "-test", "bytes")
+ tg.grepStdout(`^bytes.test$`, "missing test main")
+ tg.grepStdout(`^bytes$`, "missing real bytes")
+ tg.grepStdout(`^bytes \[bytes.test\]$`, "unexpected test copy of bytes")
+ tg.grepStdoutNot(`^testing \[bytes.test\]$`, "unexpected test copy of testing")
tg.grepStdoutNot(`^testing$`, "unexpected real copy of testing")
tg.run("list", "-test", "cmd/buildid", "cmd/doc")
"os"
"reflect"
"slices"
- "sort"
"strings"
"testing"
"time"
}
// Check that the list is sorted according to the documented criteria.
- isBetter := func(a, b int) bool {
- aSuite, bSuite := cipherSuiteByID(prefOrder[a]), cipherSuiteByID(prefOrder[b])
- aName, bName := CipherSuiteName(prefOrder[a]), CipherSuiteName(prefOrder[b])
+ isBetter := func(a, b uint16) int {
+ aSuite, bSuite := cipherSuiteByID(a), cipherSuiteByID(b)
+ aName, bName := CipherSuiteName(a), CipherSuiteName(b)
// * < RC4
if !strings.Contains(aName, "RC4") && strings.Contains(bName, "RC4") {
- return true
+ return -1
} else if strings.Contains(aName, "RC4") && !strings.Contains(bName, "RC4") {
- return false
+ return +1
}
// * < CBC_SHA256
if !strings.Contains(aName, "CBC_SHA256") && strings.Contains(bName, "CBC_SHA256") {
- return true
+ return -1
} else if strings.Contains(aName, "CBC_SHA256") && !strings.Contains(bName, "CBC_SHA256") {
- return false
+ return +1
}
// * < 3DES
if !strings.Contains(aName, "3DES") && strings.Contains(bName, "3DES") {
- return true
+ return -1
} else if strings.Contains(aName, "3DES") && !strings.Contains(bName, "3DES") {
- return false
+ return +1
}
// ECDHE < *
if aSuite.flags&suiteECDHE != 0 && bSuite.flags&suiteECDHE == 0 {
- return true
+ return -1
} else if aSuite.flags&suiteECDHE == 0 && bSuite.flags&suiteECDHE != 0 {
- return false
+ return +1
}
// AEAD < CBC
if aSuite.aead != nil && bSuite.aead == nil {
- return true
+ return -1
} else if aSuite.aead == nil && bSuite.aead != nil {
- return false
+ return +1
}
// AES < ChaCha20
if strings.Contains(aName, "AES") && strings.Contains(bName, "CHACHA20") {
- return i == 0 // true for cipherSuitesPreferenceOrder
+ // negative for cipherSuitesPreferenceOrder
+ if i == 0 {
+ return -1
+ } else {
+ return +1
+ }
} else if strings.Contains(aName, "CHACHA20") && strings.Contains(bName, "AES") {
- return i != 0 // true for cipherSuitesPreferenceOrderNoAES
+ // negative for cipherSuitesPreferenceOrderNoAES
+ if i != 0 {
+ return -1
+ } else {
+ return +1
+ }
}
// AES-128 < AES-256
if strings.Contains(aName, "AES_128") && strings.Contains(bName, "AES_256") {
- return true
+ return -1
} else if strings.Contains(aName, "AES_256") && strings.Contains(bName, "AES_128") {
- return false
+ return +1
}
// ECDSA < RSA
if aSuite.flags&suiteECSign != 0 && bSuite.flags&suiteECSign == 0 {
- return true
+ return -1
} else if aSuite.flags&suiteECSign == 0 && bSuite.flags&suiteECSign != 0 {
- return false
+ return +1
}
t.Fatalf("two ciphersuites are equal by all criteria: %v and %v", aName, bName)
panic("unreachable")
}
- if !sort.SliceIsSorted(prefOrder, isBetter) {
+ if !slices.IsSortedFunc(prefOrder, isBetter) {
t.Error("preference order is not sorted according to the rules")
}
}
"os/exec"
"reflect"
"runtime"
- "sort"
+ "slices"
"strconv"
"strings"
"testing"
}
chainStrings = append(chainStrings, strings.Join(names, " -> "))
}
- sort.Strings(chainStrings)
+ slices.Sort(chainStrings)
return chainStrings
}
"fmt"
"math/big"
"reflect"
- "sort"
+ "slices"
"time"
"unicode/utf8"
)
e.Encode(l[i])
}
- sort.Slice(l, func(i, j int) bool {
- // Since we are using bytes.Compare to compare TLV encodings we
- // don't need to right pad s[i] and s[j] to the same length as
- // suggested in X690. If len(s[i]) < len(s[j]) the length octet of
- // s[i], which is the first determining byte, will inherently be
- // smaller than the length octet of s[j]. This lets us skip the
- // padding step.
- return bytes.Compare(l[i], l[j]) < 0
- })
+ // Since we are using bytes.Compare to compare TLV encodings we
+ // don't need to right pad s[i] and s[j] to the same length as
+ // suggested in X690. If len(s[i]) < len(s[j]) the length octet of
+ // s[i], which is the first determining byte, will inherently be
+ // smaller than the length octet of s[j]. This lets us skip the
+ // padding step.
+ slices.SortFunc(l, bytes.Compare)
var off int
for _, b := range l {
import (
"bytes"
+ "cmp"
"encoding/hex"
"fmt"
"io"
"math"
"reflect"
- "sort"
+ "slices"
"strings"
"testing"
)
for k, v := range m {
entries = append(entries, mapEntry{math.Float64bits(k), v})
}
- sort.Slice(entries, func(i, j int) bool {
- ei, ej := entries[i], entries[j]
- if ei.keyBits != ej.keyBits {
- return ei.keyBits < ej.keyBits
+ slices.SortFunc(entries, func(a, b mapEntry) int {
+ r := cmp.Compare(a.keyBits, b.keyBits)
+ if r != 0 {
+ return r
}
- return ei.value < ej.value
+ return cmp.Compare(a.value, b.value)
})
return entries
}
"encoding/base64"
"errors"
"io"
- "sort"
+ "slices"
"strings"
)
}
}
// For consistency of output, write other headers sorted by key.
- sort.Strings(h)
+ slices.Sort(h)
for _, k := range h {
if err := writeHeader(out, k, b.Headers[k]); err != nil {
return err
import (
"errors"
- "sort"
+ "slices"
)
// Errorf formats according to a format specifier and returns the string as a
err = w
default:
if p.reordered {
- sort.Ints(p.wrappedErrs)
+ slices.Sort(p.wrappedErrs)
}
var errs []error
for i, argNum := range p.wrappedErrs {
import (
"bytes"
+ "cmp"
"fmt"
"go/token"
- "sort"
+ "slices"
"strings"
)
-type byPos []*CommentGroup
-
-func (a byPos) Len() int { return len(a) }
-func (a byPos) Less(i, j int) bool { return a[i].Pos() < a[j].Pos() }
-func (a byPos) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
-
// sortComments sorts the list of comment groups in source order.
func sortComments(list []*CommentGroup) {
- // TODO(gri): Does it make sense to check for sorted-ness
- // first (because we know that sorted-ness is
- // very likely)?
- if orderedList := byPos(list); !sort.IsSorted(orderedList) {
- sort.Sort(orderedList)
- }
+ slices.SortFunc(list, func(a, b *CommentGroup) int {
+ return cmp.Compare(a.Pos(), b.Pos())
+ })
}
// A CommentMap maps an AST node to a list of comment groups
list = append(list, n)
return true
})
+
// Note: The current implementation assumes that Inspect traverses the
// AST in depth-first and thus _source_ order. If AST traversal
// does not follow source order, the sorting call below will be
// required.
- // sort.Sort(byInterval(list))
+ // slices.Sort(list, func(a, b Node) int {
+ // r := cmp.Compare(a.Pos(), b.Pos())
+ // if r != 0 {
+ // return r
+ // }
+ // return cmp.Compare(a.End(), b.End())
+ // })
+
return list
}
for node := range cmap {
nodes = append(nodes, node)
}
- sort.Sort(byInterval(nodes))
+ slices.SortFunc(nodes, func(a, b Node) int {
+ r := cmp.Compare(a.Pos(), b.Pos())
+ if r != 0 {
+ return r
+ }
+ return cmp.Compare(a.End(), b.End())
+ })
var buf strings.Builder
fmt.Fprintln(&buf, "CommentMap {")
import (
"go/token"
- "sort"
+ "slices"
)
// ----------------------------------------------------------------------------
maxPos = f.FileEnd
}
}
- sort.Strings(filenames)
+ slices.Sort(filenames)
// Collect package comments from all package files into a single
// CommentGroup - the collected package documentation. In general
package ast
import (
+ "cmp"
"go/token"
- "sort"
+ "slices"
"strconv"
)
// Reassign the import paths to have the same position sequence.
// Reassign each comment to the spec on the same line.
// Sort the comments by new position.
- sort.Slice(specs, func(i, j int) bool {
- ipath := importPath(specs[i])
- jpath := importPath(specs[j])
- if ipath != jpath {
- return ipath < jpath
+ slices.SortFunc(specs, func(a, b Spec) int {
+ ipath := importPath(a)
+ jpath := importPath(b)
+ r := cmp.Compare(ipath, jpath)
+ if r != 0 {
+ return r
}
- iname := importName(specs[i])
- jname := importName(specs[j])
- if iname != jname {
- return iname < jname
+ iname := importName(a)
+ jname := importName(b)
+ r = cmp.Compare(iname, jname)
+ if r != 0 {
+ return r
}
- return importComment(specs[i]) < importComment(specs[j])
+ return cmp.Compare(importComment(a), importComment(b))
})
// Dedup. Thanks to our sorting, we can just consider
}
}
- sort.Slice(comments, func(i, j int) bool {
- return comments[i].Pos() < comments[j].Pos()
+ slices.SortFunc(comments, func(a, b *CommentGroup) int {
+ return cmp.Compare(a.Pos(), b.Pos())
})
return specs
pathpkg "path"
"path/filepath"
"runtime"
- "sort"
+ "slices"
"strconv"
"strings"
"unicode"
for tag := range allTags {
p.AllTags = append(p.AllTags, tag)
}
- sort.Strings(p.AllTags)
+ slices.Sort(p.AllTags)
p.EmbedPatterns, p.EmbedPatternPos = cleanDecls(embedPos)
p.TestEmbedPatterns, p.TestEmbedPatternPos = cleanDecls(testEmbedPos)
// The standard assemblers expect .s files.
if len(p.CgoFiles) > 0 {
p.SFiles = append(p.SFiles, Sfiles...)
- sort.Strings(p.SFiles)
+ slices.Sort(p.SFiles)
} else {
p.IgnoredOtherFiles = append(p.IgnoredOtherFiles, Sfiles...)
- sort.Strings(p.IgnoredOtherFiles)
+ slices.Sort(p.IgnoredOtherFiles)
}
if badGoError != nil {
}
out := make([]string, len(list))
copy(out, list)
- sort.Strings(out)
+ slices.Sort(out)
uniq := out[:0]
for _, x := range out {
if len(uniq) == 0 || uniq[len(uniq)-1] != x {
for path := range m {
all = append(all, path)
}
- sort.Strings(all)
+ slices.Sort(all)
return all, m
}
"os"
"path/filepath"
"runtime"
- "sort"
+ "slices"
"strings"
"testing"
)
< context
< TIME;
- TIME, io, path, sort
+ TIME, io, path, slices
< io/fs;
# MATH is RUNTIME plus the basic math packages.
< math/big;
# compression
- FMT, encoding/binary, hash/adler32, hash/crc32
+ FMT, encoding/binary, hash/adler32, hash/crc32, sort
< compress/bzip2, compress/flate, compress/lzw, internal/zstd
< archive/zip, compress/gzip, compress/zlib;
< internal/lazytemplate;
# regexp
- FMT
+ FMT, sort
< regexp/syntax
< regexp
< internal/lazyregexp;
< index/suffixarray;
# executable parsing
- FMT, encoding/binary, compress/zlib, internal/saferio, internal/zstd
+ FMT, encoding/binary, compress/zlib, internal/saferio, internal/zstd, sort
< runtime/debug
< debug/dwarf
< debug/elf, debug/gosym, debug/macho, debug/pe, debug/plan9obj, internal/xcoff
< DEBUG;
# go parser and friends.
- FMT
+ FMT, sort
< internal/gover
< go/version
< go/token
< go/internal/typeparams;
FMT
- < go/build/constraint, go/doc/comment;
+ < go/build/constraint;
+
+ FMT, sort
+ < go/doc/comment;
go/internal/typeparams, go/build/constraint
< go/parser;
golang.org/x/net/lif,
golang.org/x/net/route;
- internal/bytealg, internal/itoa, math/bits, sort, strconv, unique
+ internal/bytealg, internal/itoa, math/bits, slices, strconv, unique
< net/netip;
# net is unavoidable when doing any networking,
internal/poll,
internal/singleflight,
net/netip,
- os
+ os,
+ sort
< net;
fmt, unicode !< net;
< net/http/fcgi;
# Profiling
- FMT, compress/gzip, encoding/binary, text/tabwriter
+ FMT, compress/gzip, encoding/binary, sort, text/tabwriter
< runtime/pprof;
OS, compress/gzip, internal/lazyregexp
syscall
< os/exec/internal/fdtest;
+ FMT, sort
+ < internal/diff;
+
FMT
- < internal/diff, internal/txtar;
+ < internal/txtar;
# v2 execution trace parser.
FMT
if err != nil {
t.Fatal(err)
}
- sort.Strings(all)
+ slices.Sort(all)
sawImport := map[string]map[string]bool{} // from package => to package => true
policy := depsPolicy(t)
}
}
}
- sort.Strings(imports)
+ slices.Sort(imports)
return imports, nil
}
"go/constant"
"go/token"
"math"
- "sort"
+ "slices"
)
func Example_complexNumbers() {
constant.MakeFromLiteral(`"a"`, token.STRING, 0),
}
- sort.Slice(vs, func(i, j int) bool {
- // Equivalent to vs[i] <= vs[j].
- return constant.Compare(vs[i], token.LEQ, vs[j])
+ slices.SortFunc(vs, func(a, b constant.Value) int {
+ if constant.Compare(a, token.LSS, b) {
+ return -1
+ }
+ if constant.Compare(a, token.GTR, b) {
+ return +1
+ }
+ return 0
})
for _, v := range vs {
import (
"internal/diff"
"internal/testenv"
- "sort"
+ "slices"
"strings"
"testing"
)
list = append(list, pkg)
}
}
- sort.Strings(list)
+ slices.Sort(list)
have := strings.Join(stdPkgs, "\n") + "\n"
want := strings.Join(list, "\n") + "\n"
package doc
import (
+ "cmp"
"go/ast"
"go/token"
"internal/lazyregexp"
"path"
- "sort"
+ "slices"
"strconv"
"strings"
"unicode"
list = append(list, flist...)
}
// sort by name
- sort.Slice(list, func(i, j int) bool {
- return list[i].Name < list[j].Name
+ slices.SortFunc(list, func(a, b *Example) int {
+ return cmp.Compare(a.Name, b.Name)
})
return list
}
decls = append(decls, depDecls...)
decls = append(decls, funcDecl)
- sort.Slice(decls, func(i, j int) bool {
- return decls[i].Pos() < decls[j].Pos()
+ slices.SortFunc(decls, func(a, b ast.Decl) int {
+ return cmp.Compare(a.Pos(), b.Pos())
})
- sort.Slice(comments, func(i, j int) bool {
- return comments[i].Pos() < comments[j].Pos()
+ slices.SortFunc(comments, func(a, b *ast.CommentGroup) int {
+ return cmp.Compare(a.Pos(), b.Pos())
})
// Synthesize file.
imps := make([]*ast.ImportSpec, len(origImps))
copy(imps, origImps)
// Assume the imports are sorted by position.
- sort.Slice(imps, func(i, j int) bool { return imps[i].Pos() < imps[j].Pos() })
+ slices.SortFunc(imps, func(a, b *ast.ImportSpec) int {
+ return cmp.Compare(a.Pos(), b.Pos())
+ })
// Assume gofmt has been applied, so there is a blank line between adjacent imps
// if and only if they are more than 2 positions apart (newline, tab).
var groupStarts []*ast.ImportSpec
// Sort list of example according to the user-specified suffix name.
for _, exs := range ids {
- sort.Slice((*exs), func(i, j int) bool {
- return (*exs)[i].Suffix < (*exs)[j].Suffix
+ slices.SortFunc(*exs, func(a, b *Example) int {
+ return cmp.Compare(a.Suffix, b.Suffix)
})
}
}
package doc
import (
+ "cmp"
"fmt"
"go/ast"
"go/token"
"internal/lazyregexp"
"path"
- "sort"
+ "slices"
"strconv"
"strings"
"unicode"
r.filenames[i] = filename
i++
}
- sort.Strings(r.filenames)
+ slices.Sort(r.filenames)
// process files in sorted order
for _, filename := range r.filenames {
// ----------------------------------------------------------------------------
// Sorting
-type data struct {
- n int
- swap func(i, j int)
- less func(i, j int) bool
-}
-
-func (d *data) Len() int { return d.n }
-func (d *data) Swap(i, j int) { d.swap(i, j) }
-func (d *data) Less(i, j int) bool { return d.less(i, j) }
-
-// sortBy is a helper function for sorting.
-func sortBy(less func(i, j int) bool, swap func(i, j int), n int) {
- sort.Sort(&data{n, swap, less})
-}
-
func sortedKeys(m map[string]int) []string {
list := make([]string, len(m))
i := 0
list[i] = key
i++
}
- sort.Strings(list)
+ slices.Sort(list)
return list
}
}
list = list[0:i]
- sortBy(
- func(i, j int) bool {
- if ni, nj := sortingName(list[i].Decl), sortingName(list[j].Decl); ni != nj {
- return ni < nj
- }
- return list[i].order < list[j].order
- },
- func(i, j int) { list[i], list[j] = list[j], list[i] },
- len(list),
- )
+ slices.SortFunc(list, func(a, b *Value) int {
+ r := strings.Compare(sortingName(a.Decl), sortingName(b.Decl))
+ if r != 0 {
+ return r
+ }
+ return cmp.Compare(a.order, b.order)
+ })
return list
}
i++
}
- sortBy(
- func(i, j int) bool { return list[i].Name < list[j].Name },
- func(i, j int) { list[i], list[j] = list[j], list[i] },
- len(list),
- )
+ slices.SortFunc(list, func(a, b *Type) int {
+ return strings.Compare(a.Name, b.Name)
+ })
return list
}
}
}
list = list[0:i]
- sortBy(
- func(i, j int) bool { return list[i].Name < list[j].Name },
- func(i, j int) { list[i], list[j] = list[j], list[i] },
- len(list),
- )
+ slices.SortFunc(list, func(a, b *Func) int {
+ return strings.Compare(a.Name, b.Name)
+ })
return list
}
return
}
-
-type byPath []*types.Package
-
-func (a byPath) Len() int { return len(a) }
-func (a byPath) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
-func (a byPath) Less(i, j int) bool { return a[i].Path() < a[j].Path() }
"io"
"math"
"math/big"
- "sort"
+ "slices"
"strings"
)
for name := range p.pkgIndex[localpkg] {
names = append(names, name)
}
- sort.Strings(names)
+ slices.Sort(names)
for _, name := range names {
p.doDecl(localpkg, name)
}
// record all referenced packages as imports
list := append(([]*types.Package)(nil), pkgList[1:]...)
- sort.Sort(byPath(list))
+ slices.SortFunc(list, func(a, b *types.Package) int {
+ return strings.Compare(a.Path(), b.Path())
+ })
localpkg.SetImports(list)
// package was imported completely and without errors
"go/types"
"internal/godebug"
"internal/pkgbits"
- "sort"
+ "slices"
+ "strings"
)
// A pkgReader holds the shared state for reading a unified IR package
imps = append(imps, imp)
}
}
- sort.Sort(byPath(imps))
+ slices.SortFunc(imps, func(a, b *types.Package) int {
+ return strings.Compare(a.Path(), b.Path())
+ })
pkg.SetImports(imps)
pkg.MarkComplete()
import (
"go/build/constraint"
- "sort"
+ "slices"
"text/tabwriter"
)
// Build sorted list of lines to delete from remainder of output.
toDelete := append(p.goBuild, p.plusBuild...)
- sort.Ints(toDelete)
+ slices.Sort(toDelete)
// Collect output after insertion point, with lines deleted, into after.
var after []byte
package token
import (
+ "cmp"
"fmt"
- "sort"
+ "slices"
"strconv"
"sync"
"sync/atomic"
}
func searchLineInfos(a []lineInfo, x int) int {
- return sort.Search(len(a), func(i int) bool { return a[i].Offset > x }) - 1
+ i, found := slices.BinarySearchFunc(a, x, func(a lineInfo, x int) int {
+ return cmp.Compare(a.Offset, x)
+ })
+ if !found {
+ // We want the lineInfo containing x, but if we didn't
+ // find x then i is the next one.
+ i--
+ }
+ return i
}
// unpack returns the filename and line and column number for a file offset.
}
func searchFiles(a []*File, x int) int {
- return sort.Search(len(a), func(i int) bool { return a[i].base > x }) - 1
+ i, found := slices.BinarySearchFunc(a, x, func(a *File, x int) int {
+ return cmp.Compare(a.base, x)
+ })
+ if !found {
+ // We want the File containing x, but if we didn't
+ // find x then i is the next one.
+ i--
+ }
+ return i
}
func (s *FileSet) file(p Pos) *File {
"internal/testenv"
"reflect"
"regexp"
- "sort"
+ "slices"
"strings"
"sync"
"testing"
for id, inst := range m {
instances = append(instances, recordedInstance{id, inst})
}
- sort.Slice(instances, func(i, j int) bool {
- return CmpPos(instances[i].Ident.Pos(), instances[j].Ident.Pos()) < 0
+ slices.SortFunc(instances, func(a, b recordedInstance) int {
+ return CmpPos(a.Ident.Pos(), b.Ident.Pos())
})
return instances
}
"go/types"
"log"
"regexp"
- "sort"
+ "slices"
"strings"
)
}
var items []string
for obj, uses := range usesByObj {
- sort.Strings(uses)
+ slices.Sort(uses)
item := fmt.Sprintf("%s:\n defined at %s\n used at %s",
types.ObjectString(obj, types.RelativeTo(pkg)),
fset.Position(obj.Pos()),
strings.Join(uses, ", "))
items = append(items, item)
}
- sort.Strings(items) // sort by line:col, in effect
+ slices.Sort(items) // sort by line:col, in effect
fmt.Println(strings.Join(items, "\n"))
fmt.Println()
mode(tv), tvstr)
items = append(items, buf.String())
}
- sort.Strings(items)
+ slices.Sort(items)
fmt.Println(strings.Join(items, "\n"))
// Output:
"go/token"
"internal/testenv"
"regexp"
- "sort"
+ "slices"
"strings"
"testing"
fact := fmt.Sprintf("L%d uses %s", fset.Position(id.Pos()).Line, obj)
facts = append(facts, fact)
}
- sort.Strings(facts)
+ slices.Sort(facts)
got := strings.Join(facts, "\n")
if got != want {
"go/importer"
"go/token"
"internal/testenv"
- "sort"
+ "slices"
"testing"
. "go/types"
}
// check the expected set of idents that are simultaneously uses and defs
- sort.Strings(both)
+ slices.Sort(both)
if got, want := fmt.Sprint(both), "[Mutex Stringer error]"; got != want {
t.Errorf("simultaneous uses/defs = %s, want %s", got, want)
}
"io"
"math"
"regexp"
+ "slices"
"sort"
)
if len(indices) == 0 {
return
}
- sort.Ints(indices)
+ slices.Sort(indices)
pairs := make([]int, 2*len(indices))
result = make([][]int, len(indices))
count := 0
if len(indices) == 0 {
return
}
- sort.Ints(indices)
+ slices.Sort(indices)
result = result[0:0]
prev := 0
for _, i := range indices {
"os"
"path/filepath"
"regexp"
+ "slices"
"sort"
"strings"
"testing"
// we cannot simply check that the res and exp lists are equal
// check that each result is in fact a correct match and there are no duplicates
- sort.Ints(res)
+ slices.Sort(res)
for i, r := range res {
if r < 0 || len(tc.source) <= r {
t.Errorf("test %q, lookup %q, result %d (n = %d): index %d out of range [0, %d[", tc.name, s, i, n, r, len(tc.source))
// emit coverage percentages.
import (
+ "cmp"
"fmt"
"internal/coverage"
"internal/coverage/cmerge"
"io"
- "sort"
+ "slices"
+ "strings"
"text/tabwriter"
)
// include function name as part of the sorting criteria, the thinking
// being that is better to provide things in the original source order.
func (p *pstate) sortUnits(units []extcu) {
- sort.Slice(units, func(i, j int) bool {
- ui := units[i]
- uj := units[j]
+ slices.SortFunc(units, func(ui, uj extcu) int {
ifile := p.funcs[ui.fnfid].file
jfile := p.funcs[uj.fnfid].file
- if ifile != jfile {
- return ifile < jfile
+ if r := strings.Compare(ifile, jfile); r != 0 {
+ return r
}
// NB: not taking function literal flag into account here (no
// need, since other fields are guaranteed to be distinct).
- if units[i].StLine != units[j].StLine {
- return units[i].StLine < units[j].StLine
+ if r := cmp.Compare(ui.StLine, uj.StLine); r != 0 {
+ return r
}
- if units[i].EnLine != units[j].EnLine {
- return units[i].EnLine < units[j].EnLine
+ if r := cmp.Compare(ui.EnLine, uj.EnLine); r != 0 {
+ return r
}
- if units[i].StCol != units[j].StCol {
- return units[i].StCol < units[j].StCol
+ if r := cmp.Compare(ui.StCol, uj.StCol); r != 0 {
+ return r
}
- if units[i].EnCol != units[j].EnCol {
- return units[i].EnCol < units[j].EnCol
+ if r := cmp.Compare(ui.EnCol, uj.EnCol); r != 0 {
+ return r
}
- return units[i].NxStmts < units[j].NxStmts
+ return cmp.Compare(ui.NxStmts, uj.NxStmts)
})
}
for importpath := range fm.pm {
pkgs = append(pkgs, importpath)
}
- sort.Strings(pkgs)
+ slices.Sort(pkgs)
for _, importpath := range pkgs {
p := fm.pm[importpath]
units := make([]extcu, 0, len(p.unitTable))
return nil
}
- sort.Strings(pkgs)
+ slices.Sort(pkgs)
var totalStmts, coveredStmts uint64
for _, importpath := range pkgs {
p := fm.pm[importpath]
for importpath := range fm.pm {
pkgs = append(pkgs, importpath)
}
- sort.Strings(pkgs)
+ slices.Sort(pkgs)
// Emit functions for each package, sorted by import path.
for _, importpath := range pkgs {
"internal/coverage/uleb128"
"io"
"os"
- "sort"
+ "slices"
)
// This package contains APIs and helpers for encoding initial portions
for k := range args {
akeys = append(akeys, k)
}
- sort.Strings(akeys)
+ slices.Sort(akeys)
wrULEB128 := func(v uint) error {
cfw.tmp = cfw.tmp[:0]
package pods
import (
+ "cmp"
"fmt"
"internal/coverage"
"os"
"path/filepath"
"regexp"
- "sort"
+ "slices"
"strconv"
+ "strings"
)
// Pod encapsulates a set of files emitted during the executions of a
}
pods := make([]Pod, 0, len(mm))
for _, p := range mm {
- sort.Slice(p.elements, func(i, j int) bool {
- if p.elements[i].origin != p.elements[j].origin {
- return p.elements[i].origin < p.elements[j].origin
+ slices.SortFunc(p.elements, func(a, b fileWithAnnotations) int {
+ if r := cmp.Compare(a.origin, b.origin); r != 0 {
+ return r
}
- return p.elements[i].file < p.elements[j].file
+ return strings.Compare(a.file, b.file)
})
pod := Pod{
MetaFile: p.mf,
}
pods = append(pods, pod)
}
- sort.Slice(pods, func(i, j int) bool {
- return pods[i].MetaFile < pods[j].MetaFile
+ slices.SortFunc(pods, func(a, b Pod) int {
+ return strings.Compare(a.MetaFile, b.MetaFile)
})
return pods
}
package dag
import (
+ "cmp"
"fmt"
- "sort"
+ "slices"
"strings"
)
for k := range g.edges[from] {
edges = append(edges, k)
}
- sort.Slice(edges, func(i, j int) bool { return g.byLabel[edges[i]] < g.byLabel[edges[j]] })
+ slices.SortFunc(edges, func(a, b string) int {
+ return cmp.Compare(g.byLabel[a], g.byLabel[b])
+ })
return edges
}
package fmtsort_test
import (
+ "cmp"
"fmt"
"internal/fmtsort"
"math"
"reflect"
"runtime"
- "sort"
+ "slices"
"strings"
"testing"
"unsafe"
for i := range cs {
pin.Pin(reflect.ValueOf(cs[i]).UnsafePointer())
}
- sort.Slice(cs, func(i, j int) bool {
- return uintptr(reflect.ValueOf(cs[i]).UnsafePointer()) < uintptr(reflect.ValueOf(cs[j]).UnsafePointer())
+ slices.SortFunc(cs, func(a, b chan int) int {
+ return cmp.Compare(reflect.ValueOf(a).Pointer(), reflect.ValueOf(b).Pointer())
})
return cs
}
"os/exec"
"reflect"
"runtime/metrics"
- "sort"
+ "slices"
"strings"
"testing"
)
want = append(want, fmt.Sprintf("godebug_test.go:%d", i+1))
}
}
- sort.Strings(want)
+ slices.Sort(want)
var have []string
for _, line := range strings.Split(string(out), "\n") {
have = append(have, line[strings.LastIndex(line, "godebug_test.go:"):])
}
}
- sort.Strings(have)
+ slices.Sort(have)
if !reflect.DeepEqual(have, want) {
t.Errorf("bad bisect output:\nhave %v\nwant %v\ncomplete output:\n%s", have, want, string(out))
return (*l)[i].ev.Ts < (*l)[j].ev.Ts
}
-type eventList []Event
-
-func (l *eventList) Len() int {
- return len(*l)
-}
-
-func (l *eventList) Less(i, j int) bool {
- return (*l)[i].Ts < (*l)[j].Ts
-}
-
-func (l *eventList) Swap(i, j int) {
- (*l)[i], (*l)[j] = (*l)[j], (*l)[i]
-}
-
func (h *orderEventList) Push(x orderEvent) {
*h = append(*h, x)
heapUp(h, len(*h)-1)
import (
"bytes"
+ "cmp"
"encoding/binary"
"errors"
"fmt"
"internal/trace/version"
"io"
"math"
+ "slices"
"sort"
)
// with original timestamps corresponding to when ReadTrace pulled the data
// off of the profBuf queue. Re-sort them by the timestamp we captured
// inside the signal handler.
- sort.Sort((*eventList)(&p.cpuSamples))
+ slices.SortFunc(p.cpuSamples, func(a, b Event) int {
+ return cmp.Compare(a.Ts, b.Ts)
+ })
allProcs := make([]proc, 0, len(p.batchOffsets))
for pid := range p.batchOffsets {
package trace
import (
+ "cmp"
"math"
- "sort"
+ "slices"
)
// mud is an updatable mutator utilization distribution.
// Sort edges.
edges := d.unsorted
- sort.Slice(edges, func(i, j int) bool {
- return edges[i].x < edges[j].x
+ slices.SortFunc(edges, func(a, b edge) int {
+ return cmp.Compare(a.x, b.x)
})
// Merge with sorted edges.
d.unsorted = nil
package trace
import (
- "sort"
+ "cmp"
+ "slices"
"strings"
"time"
)
g.finalize(s.lastTs, nil)
// Sort based on region start time.
- sort.Slice(g.Regions, func(i, j int) bool {
- x := g.Regions[i].Start
- y := g.Regions[j].Start
+ slices.SortFunc(g.Regions, func(a, b *UserRegionSummary) int {
+ x := a.Start
+ y := b.Start
if x == nil {
- return true
+ if y == nil {
+ return 0
+ }
+ return -1
}
if y == nil {
- return false
+ return +1
}
- return x.Time() < y.Time()
+ return cmp.Compare(x.Time(), y.Time())
})
g.goroutineSummary = nil
}
import (
"errors"
- "sort"
+ "internal/bytealg"
+ "slices"
)
// ReadDirFS is the interface implemented by a file system
}
list, err := dir.ReadDir(-1)
- sort.Slice(list, func(i, j int) bool { return list[i].Name() < list[j].Name() })
+ slices.SortFunc(list, func(a, b DirEntry) int {
+ return bytealg.CompareString(a.Name(), b.Name())
+ })
return list, err
}
"io"
"io/fs"
"os"
- "sort"
+ "slices"
+ "strings"
)
// ReadAll reads from r until an error or EOF and returns the data it read.
if err != nil {
return nil, err
}
- sort.Slice(list, func(i, j int) bool { return list[i].Name() < list[j].Name() })
+ slices.SortFunc(list, func(a, b os.FileInfo) int {
+ return strings.Compare(a.Name(), b.Name())
+ })
return list, nil
}
"bytes"
"fmt"
. "io"
- "sort"
+ "slices"
"strings"
"testing"
"time"
groups = append(groups, b[:n])
b = b[n:]
}
- sort.Slice(groups, func(i, j int) bool { return bytes.Compare(groups[i], groups[j]) < 0 })
+ slices.SortFunc(groups, bytes.Compare)
return bytes.Join(groups, nil)
}
import (
"fmt"
- "sort"
+ "slices"
"testing"
)
z = append(z, b)
}
}
- sort.Ints([]int(z))
+ slices.Sort([]int(z))
return z
}
import (
"errors"
"fmt"
- "sort"
+ "slices"
"strings"
"unicode"
)
for a := range param {
attrs = append(attrs, a)
}
- sort.Strings(attrs)
+ slices.Sort(attrs)
for _, attribute := range attrs {
value := param[attribute]
"fmt"
"io"
"net/textproto"
- "sort"
+ "slices"
"strings"
)
for k := range header {
keys = append(keys, k)
}
- sort.Strings(keys)
+ slices.Sort(keys)
for _, k := range keys {
for _, v := range header[k] {
fmt.Fprintf(&b, "%s: %s\r\n", k, v)
"io"
"os/exec"
"regexp"
- "sort"
+ "slices"
"strings"
"testing"
"time"
for k, v := range res {
outcomes = append(outcomes, fmt.Sprintf("%v: %d", k, v))
}
- sort.Strings(outcomes)
+ slices.Sort(outcomes)
got := strings.Join(outcomes, "\n")
want := `OK: 28934
invalid bytes after =: 3949
import (
"fmt"
- "sort"
+ "slices"
"strings"
"sync"
)
return nil, nil
}
ret := append([]string(nil), s.([]string)...)
- sort.Strings(ret)
+ slices.Sort(ret)
return ret, nil
}
package net
import (
+ "cmp"
"internal/bytealg"
"internal/itoa"
- "sort"
+ "slices"
_ "unsafe" // for go:linkname
"golang.org/x/net/dns/dnsmessage"
// byPriorityWeight sorts SRV records by ascending priority and weight.
type byPriorityWeight []*SRV
-func (s byPriorityWeight) Len() int { return len(s) }
-func (s byPriorityWeight) Less(i, j int) bool {
- return s[i].Priority < s[j].Priority || (s[i].Priority == s[j].Priority && s[i].Weight < s[j].Weight)
-}
-func (s byPriorityWeight) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
-
// shuffleByWeight shuffles SRV records by weight using the algorithm
// described in RFC 2782.
func (addrs byPriorityWeight) shuffleByWeight() {
// sort reorders SRV records as specified in RFC 2782.
func (addrs byPriorityWeight) sort() {
- sort.Sort(addrs)
+ slices.SortFunc(addrs, func(a, b *SRV) int {
+ if r := cmp.Compare(a.Priority, b.Priority); r != 0 {
+ return r
+ }
+ return cmp.Compare(a.Weight, b.Weight)
+ })
i := 0
for j := 1; j < len(addrs); j++ {
if addrs[i].Priority != addrs[j].Priority {
Pref uint16
}
-// byPref implements sort.Interface to sort MX records by preference
+// byPref sorts MX records by preference
type byPref []*MX
-func (s byPref) Len() int { return len(s) }
-func (s byPref) Less(i, j int) bool { return s[i].Pref < s[j].Pref }
-func (s byPref) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
-
// sort reorders MX records as specified in RFC 5321.
func (s byPref) sort() {
for i := range s {
j := randIntn(i + 1)
s[i], s[j] = s[j], s[i]
}
- sort.Sort(s)
+ slices.SortFunc(s, func(a, b *MX) int {
+ return cmp.Compare(a.Pref, b.Pref)
+ })
}
// An NS represents a single DNS NS record.
"net/http"
"os"
"path"
- "sort"
+ "slices"
"strings"
"time"
)
for k := range params {
keys = append(keys, k)
}
- sort.Strings(keys)
+ slices.Sort(keys)
for _, key := range keys {
fmt.Printf("param-%s=%s\r\n", key, params.Get(key))
}
for k := range envs {
keys = append(keys, k)
}
- sort.Strings(keys)
+ slices.Sort(keys)
for _, key := range keys {
fmt.Printf("env-%s=%s\r\n", key, envs[key])
}
"os"
"reflect"
"runtime"
- "sort"
+ "slices"
"strings"
"sync"
"sync/atomic"
for k := range r.Trailer {
decl = append(decl, k)
}
- sort.Strings(decl)
+ slices.Sort(decl)
slurp, err := io.ReadAll(r.Body)
if err != nil {
package cookiejar
import (
+ "cmp"
"errors"
"fmt"
"net"
"net/http"
"net/http/internal/ascii"
"net/url"
- "sort"
+ "slices"
"strings"
"sync"
"time"
// sort according to RFC 6265 section 5.4 point 2: by longest
// path and then by earliest creation time.
- sort.Slice(selected, func(i, j int) bool {
- s := selected
- if len(s[i].Path) != len(s[j].Path) {
- return len(s[i].Path) > len(s[j].Path)
+ slices.SortFunc(selected, func(a, b entry) int {
+ if r := cmp.Compare(b.Path, a.Path); r != 0 {
+ return r
}
- if ret := s[i].Creation.Compare(s[j].Creation); ret != 0 {
- return ret < 0
+ if r := a.Creation.Compare(b.Creation); r != 0 {
+ return r
}
- return s[i].seqNum < s[j].seqNum
+ return cmp.Compare(a.seqNum, b.seqNum)
})
for _, e := range selected {
cookies = append(cookies, &http.Cookie{Name: e.Name, Value: e.Value, Quoted: e.Quoted})
"fmt"
"net/http"
"net/url"
- "sort"
+ "slices"
"strings"
"testing"
"time"
cs = append(cs, cookie.Name+"="+v)
}
}
- sort.Strings(cs)
+ slices.Sort(cs)
got := strings.Join(cs, " ")
// Make sure jar content matches our expectations.
"net/url"
"os"
"reflect"
- "sort"
+ "slices"
"strconv"
"strings"
"sync"
}
}
}
- sort.Strings(cf)
+ slices.Sort(cf)
expectedValues := []string{"Upgrade", someConnHeader, fakeConnectionToken}
- sort.Strings(expectedValues)
+ slices.Sort(expectedValues)
if !reflect.DeepEqual(cf, expectedValues) {
t.Errorf("handler modified header %q = %q; want %q", "Connection", cf, expectedValues)
}
"net/netip"
"reflect"
"runtime"
- "sort"
+ "slices"
"strings"
"sync"
"sync/atomic"
if err != nil {
t.Fatal(err)
}
- sort.Strings(txts)
+ slices.Sort(txts)
want := []string{
strings.Repeat("abcdefghijklmnopqrstuvwxyABCDEFGHJIKLMNOPQRSTUVWXY", 10),
"gophers rule",
package net
import (
+ "cmp"
"context"
"encoding/json"
"errors"
"os/exec"
"reflect"
"regexp"
- "sort"
+ "slices"
"strings"
"syscall"
"testing"
if err != nil {
t.Skipf("skipping failed nslookup %s test: %s", server, err)
}
- sort.Sort(byPrefAndHost(expected))
- sort.Sort(byPrefAndHost(mx))
+ byPrefAndHost := func(a, b *MX) int {
+ if r := cmp.Compare(a.Pref, b.Pref); r != 0 {
+ return r
+ }
+ return strings.Compare(a.Host, b.Host)
+ }
+ slices.SortFunc(expected, byPrefAndHost)
+ slices.SortFunc(mx, byPrefAndHost)
if !reflect.DeepEqual(expected, mx) {
t.Errorf("different results %s:\texp:%v\tgot:%v", server, toJson(expected), toJson(mx))
}
if err != nil {
t.Skipf("skipping failed nslookup %s test: %s", server, err)
}
- sort.Sort(byHost(expected))
- sort.Sort(byHost(ns))
+ byHost := func(a, b *NS) int {
+ return strings.Compare(a.Host, b.Host)
+ }
+ slices.SortFunc(expected, byHost)
+ slices.SortFunc(ns, byHost)
if !reflect.DeepEqual(expected, ns) {
t.Errorf("different results %s:\texp:%v\tgot:%v", toJson(server), toJson(expected), ns)
}
if err != nil {
t.Skipf("skipping failed nslookup %s test: %s", server, err)
}
- sort.Strings(expected)
- sort.Strings(txt)
+ slices.Sort(expected)
+ slices.Sort(txt)
if !reflect.DeepEqual(expected, txt) {
t.Errorf("different results %s:\texp:%v\tgot:%v", server, toJson(expected), toJson(txt))
}
if err != nil {
t.Skipf("skipping failed lookup %s test: %s", addr.String(), err)
}
- sort.Strings(expected)
- sort.Strings(names)
+ slices.Sort(expected)
+ slices.Sort(names)
if !reflect.DeepEqual(expected, names) {
t.Errorf("different results %s:\texp:%v\tgot:%v", addr, toJson(expected), toJson(names))
}
t.Logf("skipping failed lookup %s test: %s", addr, err)
continue
}
- sort.Strings(expected)
- sort.Strings(names)
+ slices.Sort(expected)
+ slices.Sort(names)
if !reflect.DeepEqual(expected, names) {
t.Errorf("different results %s:\texp:%v\tgot:%v", addr, toJson(expected), toJson(names))
}
}
}
-type byPrefAndHost []*MX
-
-func (s byPrefAndHost) Len() int { return len(s) }
-func (s byPrefAndHost) Less(i, j int) bool {
- if s[i].Pref != s[j].Pref {
- return s[i].Pref < s[j].Pref
- }
- return s[i].Host < s[j].Host
-}
-func (s byPrefAndHost) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
-
-type byHost []*NS
-
-func (s byHost) Len() int { return len(s) }
-func (s byHost) Less(i, j int) bool { return s[i].Host < s[j].Host }
-func (s byHost) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
-
func nslookup(qtype, name string) (string, error) {
var out strings.Builder
var err strings.Builder
"net/internal/socktest"
"os"
"runtime"
- "sort"
+ "slices"
"strings"
"sync"
"testing"
}
gss = append(gss, stack)
}
- sort.Strings(gss)
+ slices.Sort(gss)
return gss
}
"os"
"os/exec"
"regexp"
- "sort"
+ "slices"
"strings"
"syscall"
"testing"
for _, ifi := range ift {
have = append(have, toString(ifi.Name, ifi.Flags&FlagUp != 0))
}
- sort.Strings(have)
+ slices.Sort(have)
ifaces := make(map[string]bool)
err = netshInterfaceIPShowInterface("ipv6", ifaces)
for name, isup := range ifaces {
want = append(want, toString(name, isup))
}
- sort.Strings(want)
+ slices.Sort(want)
if strings.Join(want, "/") != strings.Join(have, "/") {
t.Fatalf("unexpected interface list %q, want %q", have, want)
}
}
}
- sort.Strings(have)
+ slices.Sort(have)
want := netshInterfaceIPv4ShowAddress(ifi.Name, outIPV4)
wantIPv6 := netshInterfaceIPv6ShowAddress(ifi.Name, outIPV6)
want = append(want, wantIPv6...)
- sort.Strings(want)
+ slices.Sort(want)
if strings.Join(want, "/") != strings.Join(have, "/") {
t.Errorf("%s: unexpected addresses list %q, want %q", ifi.Name, have, want)
. "net/netip"
"reflect"
"slices"
- "sort"
"strings"
"testing"
"unique"
mustIP("8.8.8.8"),
mustIP("::1%foo"),
}
- sort.Slice(values, func(i, j int) bool { return values[i].Less(values[j]) })
+ slices.SortFunc(values, Addr.Compare)
got := fmt.Sprintf("%s", values)
want := `[invalid IP 1.2.3.4 8.8.8.8 ::1 ::1%foo ::2]`
if got != want {
mustIPPort("8.8.8.8:8080"),
mustIPPort("[::1%foo]:1024"),
}
- slices.SortFunc(values, func(a, b AddrPort) int { return a.Compare(b) })
+ slices.SortFunc(values, AddrPort.Compare)
got := fmt.Sprintf("%s", values)
want := `[invalid AddrPort 1.2.3.4:443 8.8.8.8:8080 [::1]:80 [::1%foo]:1024 [::2]:80]`
if got != want {
mustPrefix("fe80::/48"),
mustPrefix("1.2.0.0/24"),
}
- slices.SortFunc(values, func(a, b Prefix) int { return a.Compare(b) })
+ slices.SortFunc(values, Prefix.Compare)
got := fmt.Sprintf("%s", values)
want := `[invalid Prefix 1.2.0.0/16 1.2.0.0/24 1.2.3.0/24 fe80::/48 fe80::/64 fe90::/64]`
if got != want {
"errors"
"fmt"
"reflect"
- "sort"
+ "slices"
"testing"
"time"
for i, ip := range ips {
ret[i] = ip.String()
}
- sort.Strings(ret)
+ slices.Sort(ret)
return ret
}
"fmt"
"html/template"
"net/http"
- "sort"
+ "slices"
+ "strings"
)
const debugText = `<html>
type debugService struct {
Service *service
Name string
- Method methodArray
+ Method []debugMethod
}
type serviceArray []debugService
var services serviceArray
server.serviceMap.Range(func(snamei, svci any) bool {
svc := svci.(*service)
- ds := debugService{svc, snamei.(string), make(methodArray, 0, len(svc.method))}
+ ds := debugService{svc, snamei.(string), make([]debugMethod, 0, len(svc.method))}
for mname, method := range svc.method {
ds.Method = append(ds.Method, debugMethod{method, mname})
}
- sort.Sort(ds.Method)
+ slices.SortFunc(ds.Method, func(a, b debugMethod) int {
+ return strings.Compare(a.Name, b.Name)
+ })
services = append(services, ds)
return true
})
- sort.Sort(services)
+ slices.SortFunc(services, func(a, b debugService) int {
+ return strings.Compare(a.Name, b.Name)
+ })
err := debug.Execute(w, services)
if err != nil {
fmt.Fprintln(w, "rpc: error executing template:", err.Error())
"errors"
"fmt"
"path"
- "sort"
+ "slices"
"strconv"
"strings"
)
for k := range v {
keys = append(keys, k)
}
- sort.Strings(keys)
+ slices.Sort(keys)
for _, k := range keys {
vs := v[k]
keyEscaped := QueryEscape(k)
package os
import (
+ "internal/bytealg"
"internal/filepathlite"
"io"
"io/fs"
- "sort"
+ "slices"
)
type readdirMode int
defer f.Close()
dirs, err := f.ReadDir(-1)
- sort.Slice(dirs, func(i, j int) bool { return dirs[i].Name() < dirs[j].Name() })
+ slices.SortFunc(dirs, func(a, b DirEntry) int {
+ return bytealg.CompareString(a.Name(), b.Name())
+ })
return dirs, err
}
"reflect"
"runtime"
"runtime/debug"
- "sort"
+ "slices"
"strings"
"sync"
"syscall"
for i, fi := range fis {
s[i] = fi.Name()
}
- sort.Strings(s)
+ slices.Sort(s)
return s
}
"reflect"
"runtime"
"slices"
- "sort"
"strings"
"syscall"
"testing"
chdir(t, tmpdir)
want := []string{"file1", "file2", "file3", "gopher.txt"}
- sort.Strings(want)
+ slices.Sort(want)
for _, name := range want {
err := os.WriteFile(filepath.Join(tmpdir, name), nil, 0777)
if err != nil {
if err != nil {
t.Fatal(err)
}
- sort.Strings(have)
+ slices.Sort(have)
if strings.Join(want, "/") != strings.Join(have, "/") {
t.Fatalf("unexpected file list %q, want %q", have, want)
import (
"fmt"
- "sort"
+ "slices"
"strings"
"testing"
)
t.Errorf("ID list mismatch: got %v; want %v", got, want)
return
}
- sort.Strings(got)
- sort.Strings(want)
+ slices.Sort(got)
+ slices.Sort(want)
mismatch := -1
for i, g := range want {
if got[i] != g {
"internal/filepathlite"
"os"
"runtime"
- "sort"
+ "slices"
"strings"
"unicode/utf8"
)
defer d.Close()
names, _ := d.Readdirnames(-1)
- sort.Strings(names)
+ slices.Sort(names)
for _, n := range names {
matched, err := Match(pattern, n)
"internal/filepathlite"
"io/fs"
"os"
- "sort"
+ "slices"
)
const (
if err != nil {
return nil, err
}
- sort.Strings(names)
+ slices.Sort(names)
return names, nil
}
"reflect/internal/example1"
"reflect/internal/example2"
"runtime"
- "sort"
+ "slices"
"strconv"
"strings"
"sync"
for _, kv := range v.MapKeys() {
out = append(out, int(kv.Elem().Interface().(uintptr)))
}
- sort.Ints(out)
+ slices.Sort(out)
for j, k := range out {
if k != i*n+j {
t.Errorf("lost x[%d][%d] = %d, want %d", i, j, k, i*n+j)
line := fmt.Sprintf("%v: %v", it.Key(), it.Value())
got = append(got, line)
}
- sort.Strings(got)
+ slices.Sort(got)
return "[" + strings.Join(got, ", ") + "]"
}
import (
"runtime"
- "sort"
+ "slices"
"time"
)
// See the allocation at the top of the function.
sorted := stats.Pause[n : n+n]
copy(sorted, stats.Pause)
- sort.Slice(sorted, func(i, j int) bool { return sorted[i] < sorted[j] })
+ slices.Sort(sorted)
nq := len(stats.PauseQuantiles) - 1
for i := 0; i < nq; i++ {
stats.PauseQuantiles[i] = sorted[len(sorted)*i/nq]
"reflect"
"runtime"
"runtime/debug"
- "sort"
+ "slices"
"strings"
"sync"
"sync/atomic"
b.ReportMetric(0, "allocs/op")
// Sort latencies then report percentiles.
- sort.Slice(latencies, func(i, j int) bool {
- return latencies[i] < latencies[j]
- })
+ slices.Sort(latencies)
b.ReportMetric(float64(latencies[len(latencies)*50/100]), "p50-ns")
b.ReportMetric(float64(latencies[len(latencies)*90/100]), "p90-ns")
b.ReportMetric(float64(latencies[len(latencies)*99/100]), "p99-ns")
"os"
"reflect"
"runtime"
- "sort"
+ "slices"
"strconv"
"strings"
"sync"
values[i] = v[37]
i++
}
- sort.Strings(keys[:])
- sort.Strings(values[:])
+ slices.Sort(keys[:])
+ slices.Sort(values[:])
for i := 0; i < 100; i++ {
if keys[i] != fmt.Sprintf("string%02d", i) {
t.Errorf("#%d: missing key: %v", i, keys[i])
import (
"context"
"fmt"
- "sort"
+ "slices"
"strings"
)
keyVals = append(keyVals, fmt.Sprintf("%q:%q", k, v))
}
- sort.Strings(keyVals)
+ slices.Sort(keyVals)
return "{" + strings.Join(keyVals, ", ") + "}"
}
import (
"bufio"
+ "cmp"
"fmt"
"internal/abi"
"internal/profilerecord"
"io"
"runtime"
+ "slices"
"sort"
"strings"
"sync"
all = append(all, p)
}
- sort.Slice(all, func(i, j int) bool { return all[i].name < all[j].name })
+ slices.SortFunc(all, func(a, b *Profile) int {
+ return strings.Compare(a.name, b.name)
+ })
return all
}
p.mu.Unlock()
// Map order is non-deterministic; make output deterministic.
- sort.Slice(all, func(i, j int) bool {
- t, u := all[i], all[j]
- for k := 0; k < len(t) && k < len(u); k++ {
- if t[k] != u[k] {
- return t[k] < u[k]
- }
- }
- return len(t) < len(u)
- })
+ slices.SortFunc(all, slices.Compare)
return printCountProfile(w, debug, p.name, stackProfile(all))
}
return writeHeapProto(w, p, int64(runtime.MemProfileRate), defaultSampleType)
}
- sort.Slice(p, func(i, j int) bool { return p[i].InUseBytes() > p[j].InUseBytes() })
+ slices.SortFunc(p, func(a, b profilerecord.MemProfileRecord) int {
+ return cmp.Compare(a.InUseBytes(), b.InUseBytes())
+ })
b := bufio.NewWriter(w)
tw := tabwriter.NewWriter(b, 1, 8, 1, '\t', 0)
}
}
- sort.Slice(p, func(i, j int) bool { return p[i].Cycles > p[j].Cycles })
+ slices.SortFunc(p, func(a, b profilerecord.BlockProfileRecord) int {
+ return cmp.Compare(b.Cycles, a.Cycles)
+ })
if debug <= 0 {
return printCountCycleProfile(w, "contentions", "delay", p)
"io"
. "runtime"
"runtime/debug"
- "sort"
+ "slices"
"strings"
"sync"
"testing"
b.StopTimer()
// Sort latencies then report percentiles.
- sort.Slice(latencies, func(i, j int) bool {
- return latencies[i] < latencies[j]
- })
+ slices.Sort(latencies)
b.ReportMetric(float64(latencies[len(latencies)*50/100]), "p50-ns")
b.ReportMetric(float64(latencies[len(latencies)*90/100]), "p90-ns")
b.ReportMetric(float64(latencies[len(latencies)*99/100]), "p99-ns")
import (
"runtime"
"runtime/debug"
- "sort"
+ "slices"
. "sync"
"sync/atomic"
"testing"
}
// Get pause time stats.
- sort.Slice(pauses, func(i, j int) bool { return pauses[i] < pauses[j] })
+ slices.Sort(pauses)
var total uint64
for _, ns := range pauses {
total += ns
"os"
"path/filepath"
"runtime"
- "sort"
+ "slices"
"strconv"
"strings"
"syscall"
}
}
- sort.Strings(names)
+ slices.Sort(names)
t.Logf("names: %q", names)
if len(names) != 10 {
}
// Check results
- sort.Strings(files)
- sort.Strings(files2)
+ slices.Sort(files)
+ slices.Sort(files2)
if strings.Join(files, "|") != strings.Join(files2, "|") {
t.Errorf("bad file list: want\n%q\ngot\n%q", files, files2)
}
"fmt"
"os"
"path/filepath"
- "sort"
+ "slices"
"strings"
"syscall"
"testing"
}
names = append(names, ".", "..") // Getdirentries returns these also
- sort.Strings(names)
- sort.Strings(names2)
+ slices.Sort(names)
+ slices.Sort(names2)
if strings.Join(names, ":") != strings.Join(names2, ":") {
t.Errorf("names don't match\n names: %q\nnames2: %q", names, names2)
}
"os/exec"
"path/filepath"
"runtime"
- "sort"
+ "slices"
"strconv"
"strings"
"sync"
// https://github.com/golang/go/issues/46145
// Containers don't reliably output this line in sorted order so manually sort and compare that.
a := strings.Split(line[8:], " ")
- sort.Strings(a)
+ slices.Sort(a)
got := strings.Join(a, " ")
if got == expected[8:] {
foundAThread = true
"math"
"os"
"runtime"
- "sort"
+ "slices"
"strconv"
"strings"
"sync"
}
extraKeys = append(extraKeys, k)
}
- sort.Strings(extraKeys)
+ slices.Sort(extraKeys)
for _, k := range extraKeys {
buf.WriteByte('\t')
prettyPrint(buf, r.Extra[k], k)
import (
"bytes"
+ "cmp"
"runtime"
- "sort"
+ "slices"
"strings"
"sync/atomic"
"testing"
var compares int64
for i := 0; i < b.N; i++ {
s := []int{5, 4, 3, 2, 1}
- sort.Slice(s, func(i, j int) bool {
+ slices.SortFunc(s, func(a, b int) int {
compares++
- return s[i] < s[j]
+ return cmp.Compare(a, b)
})
}
// This metric is per-operation, so divide by b.N and
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
s := []int{5, 4, 3, 2, 1}
- sort.Slice(s, func(i, j int) bool {
+ slices.SortFunc(s, func(a, b int) int {
// Because RunParallel runs the function many
// times in parallel, we must increment the
// counter atomically to avoid racing writes.
compares.Add(1)
- return s[i] < s[j]
+ return cmp.Compare(a, b)
})
}
})
import (
"fmt"
- "sort"
+ "slices"
"strings"
"time"
)
func sortLines(output string) string {
lines := strings.Split(output, "\n")
- sort.Strings(lines)
+ slices.Sort(lines)
return strings.Join(lines, "\n")
}
"io"
"io/fs"
"path"
- "sort"
+ "slices"
"strings"
"time"
)
for name := range need {
list = append(list, mapFileInfo{name, &MapFile{Mode: fs.ModeDir | 0555}})
}
- sort.Slice(list, func(i, j int) bool {
- return list[i].name < list[j].name
+ slices.SortFunc(list, func(a, b mapFileInfo) int {
+ return strings.Compare(a.name, b.name)
})
if file == nil {
"io/fs"
"path"
"reflect"
- "sort"
+ "slices"
"strings"
"testing/iotest"
)
list = append(list, k)
}
}
- sort.Strings(list)
+ slices.Sort(list)
if len(list) > 15 {
list = append(list[:10], "...")
}
return
}
- if !sort.StringsAreSorted(names) {
+ if !slices.IsSorted(names) {
t.errorf("%s: Glob(%#q): unsorted output:\n%s", dir, glob, strings.Join(names, "\n"))
- sort.Strings(names)
+ slices.Sort(names)
}
var problems []string
return
}
- sort.Slice(diffs, func(i, j int) bool {
- fi := strings.Fields(diffs[i])
- fj := strings.Fields(diffs[j])
+ slices.SortFunc(diffs, func(a, b string) int {
+ fa := strings.Fields(a)
+ fb := strings.Fields(b)
// sort by name (i < j) and then +/- (j < i, because + < -)
- return fi[1]+" "+fj[0] < fj[1]+" "+fi[0]
+ return strings.Compare(fa[1]+" "+fb[0], fb[1]+" "+fa[0])
})
t.errorf("%s: diff %s:\n\t%s", dir, desc, strings.Join(diffs, "\n\t"))
"io/fs"
"os"
"path/filepath"
- "sort"
+ "slices"
+ "strings"
"testing"
)
//
// We do this to make sure that the TestFS test suite is not affected by the
// order of directory entries.
- sort.Slice(dirents, func(i, j int) bool {
- return dirents[i].Name() > dirents[j].Name()
+ slices.SortFunc(dirents, func(a, b fs.DirEntry) int {
+ return strings.Compare(b.Name(), a.Name())
})
return dirents, err
}
"runtime"
"runtime/debug"
"runtime/trace"
- "sort"
+ "slices"
"strconv"
"strings"
"sync"
list = append(list, fmt.Sprintf("%s (%v)", k.(string), highPrecisionTimeSince(v.(highPrecisionTime)).Round(time.Second)))
return true
})
- sort.Strings(list)
+ slices.Sort(list)
return list
}
"log"
"net/http"
"os"
- "sort"
+ "slices"
+ "strings"
"text/template"
"time"
)
if err != nil {
log.Fatal(err)
}
- sort.Slice(zs, func(i, j int) bool {
- return zs[i].UnixName < zs[j].UnixName
+ slices.SortFunc(zs, func(a, b *zone) int {
+ return strings.Compare(a.UnixName, b.UnixName)
})
var v = struct {
URL string