Skip to content

Commit 2e6ea42

Browse files
committed
refactor(serverHandler/responseData): prevent memory reallocation
1 parent a6e312d commit 2e6ea42

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

src/serverHandler/responseData.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,10 @@ func isSlash(c rune) bool {
7272
}
7373

7474
func getPathEntries(path string, tailSlash bool) []pathEntry {
75-
paths := []string{"/"}
76-
paths = append(paths, strings.FieldsFunc(path, isSlash)...)
75+
restPaths := strings.FieldsFunc(path, isSlash)
76+
paths := make([]string, 1, len(restPaths)+1)
77+
paths[0] = "/"
78+
paths = append(paths, restPaths...)
7779

7880
displayPathsCount := len(paths)
7981

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package serverHandler
2+
3+
import "testing"
4+
5+
func TestGetPathEntries(t *testing.T) {
6+
var result []pathEntry
7+
8+
result = getPathEntries("/a/b/c", false)
9+
if len(result) != 4 {
10+
t.Error(len(result))
11+
}
12+
if result[0].Path != "../../" || result[0].Name != "/" {
13+
t.Error(result[0])
14+
}
15+
if result[1].Path != "../" || result[1].Name != "a" {
16+
t.Error(result[1])
17+
}
18+
if result[2].Path != "./" || result[2].Name != "b" {
19+
t.Error(result[2])
20+
}
21+
if result[3].Path != "./c/" || result[3].Name != "c" {
22+
t.Error(result[3])
23+
}
24+
25+
result = getPathEntries("/a/b/c", true)
26+
if len(result) != 4 {
27+
t.Error(len(result))
28+
}
29+
if result[0].Path != "../../../" || result[0].Name != "/" {
30+
t.Error(result[0])
31+
}
32+
if result[1].Path != "../../" || result[1].Name != "a" {
33+
t.Error(result[1])
34+
}
35+
if result[2].Path != "../" || result[2].Name != "b" {
36+
t.Error(result[2])
37+
}
38+
if result[3].Path != "./" || result[3].Name != "c" {
39+
t.Error(result[3])
40+
}
41+
}

0 commit comments

Comments
 (0)