Skip to content

Commit 0d0e3d8

Browse files
committed
perf(util/wildcard): instantiate Replacer only once
1 parent fb4dbbb commit 0d0e3d8

File tree

1 file changed

+20
-21
lines changed

1 file changed

+20
-21
lines changed

src/util/wildcard.go

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,26 @@ import (
44
"strings"
55
)
66

7-
func WildcardToRegexp(wildcard string) string {
8-
replacer := strings.NewReplacer(
9-
"(", "\\(",
10-
")", "\\)",
11-
"[", "\\[",
12-
"]", "\\]",
13-
"{", "\\{",
14-
"}", "\\}",
15-
"<", "\\<",
16-
">", "\\>",
17-
"^", "\\^",
18-
"$", "\\$",
19-
"|", "\\|",
20-
"+", "\\+",
21-
"\\", "\\\\",
22-
".", "\\.",
23-
"?", ".",
24-
"*", ".*?",
25-
)
26-
27-
exp := "^" + replacer.Replace(wildcard) + "$"
7+
var regexpEscapeReplacer = strings.NewReplacer(
8+
"(", "\\(",
9+
")", "\\)",
10+
"[", "\\[",
11+
"]", "\\]",
12+
"{", "\\{",
13+
"}", "\\}",
14+
"<", "\\<",
15+
">", "\\>",
16+
"^", "\\^",
17+
"$", "\\$",
18+
"|", "\\|",
19+
"+", "\\+",
20+
"\\", "\\\\",
21+
".", "\\.",
22+
"?", ".",
23+
"*", ".*?",
24+
)
2825

26+
func WildcardToRegexp(wildcard string) string {
27+
exp := "^" + regexpEscapeReplacer.Replace(wildcard) + "$"
2928
return exp
3029
}

0 commit comments

Comments
 (0)