mirror of
https://github.com/go-delve/delve.git
synced 2025-10-29 01:27:16 +08:00
pkg: refactor sort.Sort to slices.SortFunc (#3880)
This commit is contained in:
@ -2,6 +2,7 @@ package proc
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"cmp"
|
||||
"debug/dwarf"
|
||||
"debug/elf"
|
||||
"debug/macho"
|
||||
@ -2305,7 +2306,7 @@ func loadBinaryInfoGoRuntimeCommon(bi *BinaryInfo, image *Image, cu *compileUnit
|
||||
for i := range inlFuncs {
|
||||
bi.Functions = append(bi.Functions, *inlFuncs[i])
|
||||
}
|
||||
sort.Sort(functionsDebugInfoByEntry(bi.Functions))
|
||||
slices.SortFunc(bi.Functions, func(a, b Function) int { return cmp.Compare(a.Entry, b.Entry) })
|
||||
for f := range image.symTable.Files {
|
||||
bi.Sources = append(bi.Sources, f)
|
||||
}
|
||||
@ -2534,9 +2535,9 @@ func (bi *BinaryInfo) loadDebugInfoMaps(image *Image, debugInfoBytes, debugLineB
|
||||
}
|
||||
}
|
||||
|
||||
sort.Sort(compileUnitsByOffset(image.compileUnits))
|
||||
sort.Sort(functionsDebugInfoByEntry(bi.Functions))
|
||||
sort.Sort(packageVarsByAddr(bi.packageVars))
|
||||
slices.SortFunc(image.compileUnits, func(a, b *compileUnit) int { return cmp.Compare(a.offset, b.offset) })
|
||||
slices.SortFunc(bi.Functions, func(a, b Function) int { return cmp.Compare(a.Entry, b.Entry) })
|
||||
slices.SortFunc(bi.packageVars, func(a, b packageVar) int { return cmp.Compare(a.addr, b.addr) })
|
||||
|
||||
bi.lookupFunc = nil
|
||||
bi.lookupGenericFunc = nil
|
||||
|
||||
@ -38,24 +38,6 @@ func pointerTo(typ godwarf.Type, arch *Arch) godwarf.Type {
|
||||
}
|
||||
}
|
||||
|
||||
type functionsDebugInfoByEntry []Function
|
||||
|
||||
func (v functionsDebugInfoByEntry) Len() int { return len(v) }
|
||||
func (v functionsDebugInfoByEntry) Less(i, j int) bool { return v[i].Entry < v[j].Entry }
|
||||
func (v functionsDebugInfoByEntry) Swap(i, j int) { v[i], v[j] = v[j], v[i] }
|
||||
|
||||
type compileUnitsByOffset []*compileUnit
|
||||
|
||||
func (v compileUnitsByOffset) Len() int { return len(v) }
|
||||
func (v compileUnitsByOffset) Less(i int, j int) bool { return v[i].offset < v[j].offset }
|
||||
func (v compileUnitsByOffset) Swap(i int, j int) { v[i], v[j] = v[j], v[i] }
|
||||
|
||||
type packageVarsByAddr []packageVar
|
||||
|
||||
func (v packageVarsByAddr) Len() int { return len(v) }
|
||||
func (v packageVarsByAddr) Less(i int, j int) bool { return v[i].addr < v[j].addr }
|
||||
func (v packageVarsByAddr) Swap(i int, j int) { v[i], v[j] = v[j], v[i] }
|
||||
|
||||
type loadDebugInfoMapsContext struct {
|
||||
ardr *reader.Reader
|
||||
abstractOriginTable map[dwarf.Offset]int
|
||||
|
||||
@ -2,6 +2,7 @@ package proc
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"cmp"
|
||||
"debug/dwarf"
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
@ -11,7 +12,7 @@ import (
|
||||
"math"
|
||||
"math/bits"
|
||||
"reflect"
|
||||
"sort"
|
||||
"slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
@ -2295,7 +2296,9 @@ func (cm constantsMap) Get(typ godwarf.Type) *constantType {
|
||||
typepkg := packageName(typ.String()) + "."
|
||||
if !ctyp.initialized {
|
||||
ctyp.initialized = true
|
||||
sort.Sort(constantValuesByValue(ctyp.values))
|
||||
slices.SortFunc(ctyp.values, func(a, b constantValue) int {
|
||||
return cmp.Compare(a.value, b.value)
|
||||
})
|
||||
for i := range ctyp.values {
|
||||
ctyp.values[i].name = strings.TrimPrefix(ctyp.values[i].name, typepkg)
|
||||
if bits.OnesCount64(uint64(ctyp.values[i].value)) == 1 {
|
||||
@ -2355,12 +2358,6 @@ func (v *variablesByDepthAndDeclLine) Swap(i int, j int) {
|
||||
v.vars[i], v.vars[j] = v.vars[j], v.vars[i]
|
||||
}
|
||||
|
||||
type constantValuesByValue []constantValue
|
||||
|
||||
func (v constantValuesByValue) Len() int { return len(v) }
|
||||
func (v constantValuesByValue) Less(i int, j int) bool { return v[i].value < v[j].value }
|
||||
func (v constantValuesByValue) Swap(i int, j int) { v[i], v[j] = v[j], v[i] }
|
||||
|
||||
const (
|
||||
timeTimeWallHasMonotonicBit uint64 = (1 << 63) // hasMonotonic bit of time.Time.wall
|
||||
|
||||
|
||||
@ -8,7 +8,7 @@ import (
|
||||
"reflect"
|
||||
"regexp"
|
||||
"runtime"
|
||||
"sort"
|
||||
"slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
@ -450,23 +450,6 @@ func TestMultilineVariableEvaluation(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
type varArray []*proc.Variable
|
||||
|
||||
// Len is part of sort.Interface.
|
||||
func (s varArray) Len() int {
|
||||
return len(s)
|
||||
}
|
||||
|
||||
// Swap is part of sort.Interface.
|
||||
func (s varArray) Swap(i, j int) {
|
||||
s[i], s[j] = s[j], s[i]
|
||||
}
|
||||
|
||||
// Less is part of sort.Interface. It is implemented by calling the "by" closure in the sorter.
|
||||
func (s varArray) Less(i, j int) bool {
|
||||
return s[i].Name < s[j].Name
|
||||
}
|
||||
|
||||
func TestLocalVariables(t *testing.T) {
|
||||
testcases := []struct {
|
||||
fn func(*proc.EvalScope, proc.LoadConfig) ([]*proc.Variable, error)
|
||||
@ -534,7 +517,9 @@ func TestLocalVariables(t *testing.T) {
|
||||
vars, err := tc.fn(scope, pnormalLoadConfig)
|
||||
assertNoError(err, t, "LocalVariables() returned an error")
|
||||
|
||||
sort.Sort(varArray(vars))
|
||||
slices.SortFunc(vars, func(a, b *proc.Variable) int {
|
||||
return strings.Compare(a.Name, b.Name)
|
||||
})
|
||||
|
||||
if len(tc.output) != len(vars) {
|
||||
t.Fatalf("Invalid variable count. Expected %d got %d.", len(tc.output), len(vars))
|
||||
|
||||
@ -7,6 +7,7 @@ package terminal
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"cmp"
|
||||
"errors"
|
||||
"fmt"
|
||||
"go/parser"
|
||||
@ -19,6 +20,7 @@ import (
|
||||
"reflect"
|
||||
"regexp"
|
||||
"runtime"
|
||||
"slices"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
@ -102,14 +104,6 @@ var (
|
||||
ShortLoadConfig = api.LoadConfig{MaxStringLen: 64, MaxStructFields: 3}
|
||||
)
|
||||
|
||||
// byFirstAlias will sort by the first
|
||||
// alias of a command.
|
||||
type byFirstAlias []command
|
||||
|
||||
func (a byFirstAlias) Len() int { return len(a) }
|
||||
func (a byFirstAlias) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
||||
func (a byFirstAlias) Less(i, j int) bool { return a[i].aliases[0] < a[j].aliases[0] }
|
||||
|
||||
// DebugCommands returns a Commands struct with default commands defined.
|
||||
func DebugCommands(client service.Client) *Commands {
|
||||
c := &Commands{client: client}
|
||||
@ -688,7 +682,9 @@ Currently, rev next, step, step-instruction and stepout commands are supported.`
|
||||
})
|
||||
}
|
||||
|
||||
sort.Sort(byFirstAlias(c.cmds))
|
||||
slices.SortFunc(c.cmds, func(a, b command) int {
|
||||
return strings.Compare(a.aliases[0], b.aliases[0])
|
||||
})
|
||||
return c
|
||||
}
|
||||
|
||||
@ -814,12 +810,6 @@ func (c *Commands) help(t *Term, ctx callContext, args string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
type byThreadID []*api.Thread
|
||||
|
||||
func (a byThreadID) Len() int { return len(a) }
|
||||
func (a byThreadID) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
||||
func (a byThreadID) Less(i, j int) bool { return a[i].ID < a[j].ID }
|
||||
|
||||
func threads(t *Term, ctx callContext, args string) error {
|
||||
threads, err := t.client.ListThreads()
|
||||
if err != nil {
|
||||
@ -829,7 +819,7 @@ func threads(t *Term, ctx callContext, args string) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
sort.Sort(byThreadID(threads))
|
||||
slices.SortFunc(threads, func(a, b *api.Thread) int { return cmp.Compare(a.ID, b.ID) })
|
||||
done := false
|
||||
t.stdout.pw.PageMaybe(func() { done = false })
|
||||
for _, th := range threads {
|
||||
@ -880,12 +870,6 @@ func thread(t *Term, ctx callContext, args string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
type byGoroutineID []*api.Goroutine
|
||||
|
||||
func (a byGoroutineID) Len() int { return len(a) }
|
||||
func (a byGoroutineID) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
||||
func (a byGoroutineID) Less(i, j int) bool { return a[i].ID < a[j].ID }
|
||||
|
||||
func (c *Commands) printGoroutines(t *Term, ctx callContext, indent string, gs []*api.Goroutine, fgl api.FormatGoroutineLoc, flags api.PrintGoroutinesFlags, depth int, cmd string, pdone *bool, state *api.DebuggerState) error {
|
||||
for _, g := range gs {
|
||||
if t.longCommandCanceled() || (pdone != nil && *pdone) {
|
||||
@ -961,7 +945,7 @@ func (c *Commands) goroutines(t *Term, ctx callContext, argstr string) error {
|
||||
fmt.Fprintf(t.stdout, "Too many groups\n")
|
||||
}
|
||||
} else {
|
||||
sort.Sort(byGoroutineID(gs))
|
||||
slices.SortFunc(gs, func(a, b *api.Goroutine) int { return cmp.Compare(a.ID, b.ID) })
|
||||
err = c.printGoroutines(t, ctx, "", gs, fgl, flags, depth, cmd, &done, state)
|
||||
if err != nil {
|
||||
return err
|
||||
@ -1741,19 +1725,12 @@ func toggle(t *Term, ctx callContext, args string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// byID sorts breakpoints by ID.
|
||||
type byID []*api.Breakpoint
|
||||
|
||||
func (a byID) Len() int { return len(a) }
|
||||
func (a byID) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
||||
func (a byID) Less(i, j int) bool { return a[i].ID < a[j].ID }
|
||||
|
||||
func breakpoints(t *Term, ctx callContext, args string) error {
|
||||
breakPoints, err := t.client.ListBreakpoints(args == "-a")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
sort.Sort(byID(breakPoints))
|
||||
slices.SortFunc(breakPoints, func(a, b *api.Breakpoint) int { return cmp.Compare(a.ID, b.ID) })
|
||||
for _, bp := range breakPoints {
|
||||
enabled := "(enabled)"
|
||||
if bp.Disabled {
|
||||
|
||||
Reference in New Issue
Block a user