Skip to content

Commit 7377d0c

Browse files
committed
go/*: use sort.Slice to simplify some code
Skip the ones that have multiple uses for now. Also had to rename the importComment variable as it shadowed the top-level func by the same name. Change-Id: I796285aa7b4fdf2c39e652666390427d37b063ee Reviewed-on: https://go-review.googlesource.com/63150 Run-TryBot: Daniel Martí <[email protected]> Run-TryBot: Ian Lance Taylor <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent eb2dc3d commit 7377d0c

File tree

3 files changed

+27
-44
lines changed

3 files changed

+27
-44
lines changed

src/go/ast/import.go

+19-29
Original file line numberDiff line numberDiff line change
@@ -123,22 +123,34 @@ func sortSpecs(fset *token.FileSet, f *File, specs []Spec) []Spec {
123123
comments := f.Comments[cstart:cend]
124124

125125
// Assign each comment to the import spec preceding it.
126-
importComment := map[*ImportSpec][]*CommentGroup{}
126+
importComments := map[*ImportSpec][]*CommentGroup{}
127127
specIndex := 0
128128
for _, g := range comments {
129129
for specIndex+1 < len(specs) && pos[specIndex+1].Start <= g.Pos() {
130130
specIndex++
131131
}
132132
s := specs[specIndex].(*ImportSpec)
133-
importComment[s] = append(importComment[s], g)
133+
importComments[s] = append(importComments[s], g)
134134
}
135135

136136
// Sort the import specs by import path.
137137
// Remove duplicates, when possible without data loss.
138138
// Reassign the import paths to have the same position sequence.
139139
// Reassign each comment to abut the end of its spec.
140140
// Sort the comments by new position.
141-
sort.Sort(byImportSpec(specs))
141+
sort.Slice(specs, func(i, j int) bool {
142+
ipath := importPath(specs[i])
143+
jpath := importPath(specs[j])
144+
if ipath != jpath {
145+
return ipath < jpath
146+
}
147+
iname := importName(specs[i])
148+
jname := importName(specs[j])
149+
if iname != jname {
150+
return iname < jname
151+
}
152+
return importComment(specs[i]) < importComment(specs[j])
153+
})
142154

143155
// Dedup. Thanks to our sorting, we can just consider
144156
// adjacent pairs of imports.
@@ -161,38 +173,16 @@ func sortSpecs(fset *token.FileSet, f *File, specs []Spec) []Spec {
161173
}
162174
s.Path.ValuePos = pos[i].Start
163175
s.EndPos = pos[i].End
164-
for _, g := range importComment[s] {
176+
for _, g := range importComments[s] {
165177
for _, c := range g.List {
166178
c.Slash = pos[i].End
167179
}
168180
}
169181
}
170182

171-
sort.Sort(byCommentPos(comments))
183+
sort.Slice(comments, func(i, j int) bool {
184+
return comments[i].Pos() < comments[j].Pos()
185+
})
172186

173187
return specs
174188
}
175-
176-
type byImportSpec []Spec // slice of *ImportSpec
177-
178-
func (x byImportSpec) Len() int { return len(x) }
179-
func (x byImportSpec) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
180-
func (x byImportSpec) Less(i, j int) bool {
181-
ipath := importPath(x[i])
182-
jpath := importPath(x[j])
183-
if ipath != jpath {
184-
return ipath < jpath
185-
}
186-
iname := importName(x[i])
187-
jname := importName(x[j])
188-
if iname != jname {
189-
return iname < jname
190-
}
191-
return importComment(x[i]) < importComment(x[j])
192-
}
193-
194-
type byCommentPos []*CommentGroup
195-
196-
func (x byCommentPos) Len() int { return len(x) }
197-
func (x byCommentPos) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
198-
func (x byCommentPos) Less(i, j int) bool { return x[i].Pos() < x[j].Pos() }

src/go/doc/example.go

+4-7
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,10 @@ func Examples(files ...*ast.File) []*Example {
9494
}
9595
list = append(list, flist...)
9696
}
97-
sort.Sort(exampleByName(list))
97+
// sort by name
98+
sort.Slice(list, func(i, j int) bool {
99+
return list[i].Name < list[j].Name
100+
})
98101
return list
99102
}
100103

@@ -135,12 +138,6 @@ func isTest(name, prefix string) bool {
135138
return !unicode.IsLower(rune)
136139
}
137140

138-
type exampleByName []*Example
139-
140-
func (s exampleByName) Len() int { return len(s) }
141-
func (s exampleByName) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
142-
func (s exampleByName) Less(i, j int) bool { return s[i].Name < s[j].Name }
143-
144141
// playExample synthesizes a new *ast.File based on the provided
145142
// file with the provided function body as the body of main.
146143
func playExample(file *ast.File, body *ast.BlockStmt) *ast.File {

src/go/types/methodset.go

+4-8
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,10 @@ func NewMethodSet(T Type) *MethodSet {
190190
list = append(list, m)
191191
}
192192
}
193-
sort.Sort(byUniqueName(list))
193+
// sort by unique name
194+
sort.Slice(list, func(i, j int) bool {
195+
return list[i].obj.Id() < list[j].obj.Id()
196+
})
194197
return &MethodSet{list}
195198
}
196199

@@ -257,10 +260,3 @@ func ptrRecv(f *Func) bool {
257260
_, isPtr := deref(f.typ.(*Signature).recv.typ)
258261
return isPtr
259262
}
260-
261-
// byUniqueName function lists can be sorted by their unique names.
262-
type byUniqueName []*Selection
263-
264-
func (a byUniqueName) Len() int { return len(a) }
265-
func (a byUniqueName) Less(i, j int) bool { return a[i].obj.Id() < a[j].obj.Id() }
266-
func (a byUniqueName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }

0 commit comments

Comments
 (0)