From a30fe0c6abd9ffbaff20af3da00eeea50d407f42 Mon Sep 17 00:00:00 2001 From: Ryo Nihei Date: Thu, 22 Jul 2021 16:22:03 +0900 Subject: Add spec.EscapePattern function --- spec/util.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 spec/util.go (limited to 'spec/util.go') diff --git a/spec/util.go b/spec/util.go new file mode 100644 index 0000000..56e9121 --- /dev/null +++ b/spec/util.go @@ -0,0 +1,21 @@ +package spec + +import "strings" + +var rep = strings.NewReplacer( + `.`, `\.`, + `*`, `\*`, + `+`, `\+`, + `?`, `\?`, + `|`, `\|`, + `(`, `\(`, + `)`, `\)`, + `[`, `\[`, + `\`, `\\`, +) + +// EscapePattern escapes the special characters. +// For example, EscapePattern(`+`) returns `\+`. +func EscapePattern(s string) string { + return rep.Replace(s) +} -- cgit v1.2.3