-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbundleparse_bench_test.go
More file actions
147 lines (126 loc) · 2.43 KB
/
Copy pathbundleparse_bench_test.go
File metadata and controls
147 lines (126 loc) · 2.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package bundleparse
import (
"os"
"strings"
"testing"
"unicode"
)
func parseLineWithUnicode(line string) (map[string]string, error) {
result := make(map[string]string)
var i int
n := len(line)
for i < n && unicode.IsSpace(rune(line[i])) {
i++
}
if i >= n || line[i] == '#' {
return result, nil
}
start := i
for i < n && !unicode.IsSpace(rune(line[i])) && line[i] != '#' {
i++
}
bundle := line[start:i]
if bundle == "" {
return result, nil
}
result["bundle"] = bundle
for i < n {
for i < n && unicode.IsSpace(rune(line[i])) {
i++
}
if i >= n {
break
}
if line[i] == '#' {
break
}
start = i
for i < n && line[i] != ':' && !unicode.IsSpace(rune(line[i])) {
i++
}
if i >= n || line[i] != ':' {
return nil, nil
}
key := line[start:i]
i++
if i >= n {
return nil, nil
}
var val strings.Builder
switch line[i] {
case '"':
i++
for i < n {
if line[i] == '\\' {
i++
if i >= n {
return nil, nil
}
val.WriteByte(line[i])
i++
continue
}
if line[i] == '"' {
i++
break
}
val.WriteByte(line[i])
i++
}
case '\'':
i++
for i < n && line[i] != '\'' {
val.WriteByte(line[i])
i++
}
if i >= n {
return nil, nil
}
i++
default:
for i < n && !unicode.IsSpace(rune(line[i])) && line[i] != '#' {
if line[i] == '\\' {
i++
if i >= n {
return nil, nil
}
val.WriteByte(line[i])
i++
continue
}
val.WriteByte(line[i])
i++
}
}
result[key] = val.String()
}
return result, nil
}
func BenchmarkParseLineMap(b *testing.B) {
line := `foo/bar kind:zsh pin:v1 branch:main conditional:if-true autoload:yes pre:"echo hi" post:'echo bye' fpath-rule:prepend unknown:yes`
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_, _ = ParseLine(line)
}
}
func BenchmarkParseLineUnicode(b *testing.B) {
line := `foo/bar kind:zsh pin:v1 branch:main conditional:if-true autoload:yes pre:"echo hi" post:'echo bye' fpath-rule:prepend unknown:yes`
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_, _ = parseLineWithUnicode(line)
}
}
func BenchmarkParseLargeBundleFile(b *testing.B) {
data, err := os.ReadFile("tests/data/zsh_plugins_big.txt")
if err != nil {
b.Fatalf("read test data: %v", err)
}
input := string(data)
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_, err := ParseBundles(input)
if err != nil {
b.Fatalf("parse bundles: %v", err)
}
}
}