Skip to content

Commit 04a7263

Browse files
committed
fix(serverHandler): make sort correctly
`names` array items are not swaped before, which makes name comparison works incorrectly.
1 parent 487b4f4 commit 04a7263

File tree

3 files changed

+51
-25
lines changed

3 files changed

+51
-25
lines changed

src/serverHandler/responseData.go

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"os"
88
"path"
99
"path/filepath"
10-
"sort"
1110
"strings"
1211
)
1312

@@ -201,30 +200,6 @@ func getItemName(info os.FileInfo, r *http.Request) (itemName string) {
201200
return
202201
}
203202

204-
func sortSubItems(subInfos []os.FileInfo) {
205-
names := make([][]byte, len(subInfos))
206-
for i := range subInfos {
207-
names[i] = []byte(subInfos[i].Name())
208-
}
209-
210-
sort.Slice(
211-
subInfos,
212-
func(prevIndex, nextIndex int) bool {
213-
prevInfo := subInfos[prevIndex]
214-
nextInfo := subInfos[nextIndex]
215-
216-
prevIsDir := prevInfo.IsDir()
217-
nextIsDir := nextInfo.IsDir()
218-
219-
if prevIsDir != nextIsDir {
220-
return prevIsDir
221-
}
222-
223-
return util.CompareNumInStr(names[prevIndex], names[nextIndex])
224-
},
225-
)
226-
}
227-
228203
func getStatusByErr(err error) int {
229204
switch {
230205
case os.IsPermission(err):

src/serverHandler/sort.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package serverHandler
2+
3+
import (
4+
"../util"
5+
"os"
6+
"sort"
7+
)
8+
9+
type sortableFileInfos struct {
10+
infos []os.FileInfo
11+
names [][]byte
12+
}
13+
14+
func newSortableFileInfos(infos []os.FileInfo) sortableFileInfos {
15+
names := make([][]byte, len(infos))
16+
for i := range infos {
17+
names[i] = []byte(infos[i].Name())
18+
}
19+
20+
return sortableFileInfos{infos, names}
21+
}
22+
23+
func (sInfos sortableFileInfos) Len() int {
24+
return len(sInfos.names)
25+
}
26+
27+
func (sInfos sortableFileInfos) Less(i, j int) bool {
28+
prevIsDir := sInfos.infos[i].IsDir()
29+
nextIsDir := sInfos.infos[j].IsDir()
30+
if prevIsDir != nextIsDir {
31+
return prevIsDir
32+
}
33+
34+
return util.CompareNumInStr(sInfos.names[i], sInfos.names[j])
35+
}
36+
37+
func (sInfos sortableFileInfos) Swap(i, j int) {
38+
sInfos.infos[i], sInfos.infos[j] = sInfos.infos[j], sInfos.infos[i]
39+
sInfos.names[i], sInfos.names[j] = sInfos.names[j], sInfos.names[i]
40+
}
41+
42+
func sortSubItems(subInfos []os.FileInfo) {
43+
sortSubInfos := newSortableFileInfos(subInfos)
44+
sort.Sort(sortSubInfos)
45+
}

src/util/compareNumInStr_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ func TestCompareNumInStr(t *testing.T) {
3434
if CompareNumInStr([]byte(prev), []byte(next)) != true {
3535
t.Error(prev, next)
3636
}
37+
38+
prev = "name"
39+
next = "name-suffix"
40+
if CompareNumInStr([]byte(prev), []byte(next)) != true {
41+
t.Error(prev, next)
42+
}
3743
}
3844

3945
func TestExtractPrefixDigits(t *testing.T) {

0 commit comments

Comments
 (0)