Skip to content

Commit c3b6107

Browse files
committed
slightly speed up param parsing
1 parent ccc6c9b commit c3b6107

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

helpers.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ func stringFields(s string, sep byte) []string {
1818
return []string{s}
1919
}
2020

21+
s = fastTrim(s, sep)
22+
23+
count = strings.Count(s, string(sep))
24+
if count == 0 {
25+
return []string{s}
26+
}
27+
2128
out := make([]string, 0, count+1)
2229

2330
for {
@@ -45,6 +52,37 @@ func stringFields(s string, sep byte) []string {
4552
return out
4653
}
4754

55+
// fastTrim is a faster implementation of trimming a string against a single
56+
// byte.
57+
func fastTrim(s string, b byte) string {
58+
br := rune(b)
59+
60+
findLeft := true
61+
left := 0
62+
63+
prevB := false
64+
right := 0
65+
66+
for i, r := range s {
67+
if findLeft {
68+
if r != br {
69+
left = i
70+
findLeft = false
71+
}
72+
continue
73+
}
74+
75+
if r != br && prevB {
76+
prevB = false
77+
right = i
78+
} else {
79+
prevB = true
80+
}
81+
}
82+
83+
return s[left : right+1]
84+
}
85+
4886
var (
4987
tagEscapeReplacer = strings.NewReplacer(";", `\:`, " ", `\s`, "\r", `\r`, "\n", `\n`, "\\", `\\`)
5088
tagUnescapeReplacer = strings.NewReplacer(`\:`, ";", `\s`, " ", `\r`, "\r", `\n`, "\n", `\\`, "\\")

0 commit comments

Comments
 (0)