From f607561e026d28c618a8b11d149fb99453c5d46a Mon Sep 17 00:00:00 2001 From: Dave Henderson Date: Wed, 6 May 2020 22:17:19 -0400 Subject: [PATCH] Add function regexp.QuoteMeta Signed-off-by: Dave Henderson --- docs-src/content/functions/regexp.yml | 15 ++++++++++ docs/content/functions/regexp.md | 41 +++++++++++++++++++++++---- funcs/regexp.go | 5 ++++ regexp/regexp.go | 5 ++++ regexp/regexp_test.go | 4 +++ tests/integration/regexp_test.go | 6 ++++ 6 files changed, 70 insertions(+), 6 deletions(-) diff --git a/docs-src/content/functions/regexp.yml b/docs-src/content/functions/regexp.yml index db46f797..f1a50626 100644 --- a/docs-src/content/functions/regexp.yml +++ b/docs-src/content/functions/regexp.yml @@ -72,6 +72,21 @@ funcs: - | $ gomplate -i '{{ if (.Env.USER | regexp.Match `^h`) }}username ({{.Env.USER}}) starts with h!{{end}}' username (hairyhenderson) starts with h! + - name: regexp.QuoteMeta + description: | + Escapes all regular expression metacharacters in the input. The returned string is a regular expression matching the literal text. + + This function provides the same behaviour as Go's + [`regexp.QuoteMeta`](https://golang.org/pkg/regexp/#Regexp.QuoteMeta) function. + pipeline: true + arguments: + - name: input + required: true + description: The input to escape + examples: + - | + $ gomplate -i '{{ `{hello}` | regexp.QuoteMeta }}' + \{hello\} - name: regexp.Replace description: | Replaces matches of a regular expression with the replacement string. diff --git a/docs/content/functions/regexp.md b/docs/content/functions/regexp.md index 715e9d0d..8554f787 100644 --- a/docs/content/functions/regexp.md +++ b/docs/content/functions/regexp.md @@ -59,10 +59,10 @@ This function provides the same behaviour as Go's ### Usage ```go -regexp.FindAll expression [false] input +regexp.FindAll expression [n] input ``` ```go -input | regexp.FindAll expression [false] +input | regexp.FindAll expression [n] ``` ### Arguments @@ -70,7 +70,7 @@ input | regexp.FindAll expression [false] | name | description | |------|-------------| | `expression` | _(required)_ The regular expression | -| `false` | _(optional)_ The number of matches to return | +| `n` | _(optional)_ The number of matches to return | | `input` | _(required)_ The input to search | ### Examples @@ -113,6 +113,35 @@ $ gomplate -i '{{ if (.Env.USER | regexp.Match `^h`) }}username ({{.Env.USER}}) username (hairyhenderson) starts with h! ``` +## `regexp.QuoteMeta` + +Escapes all regular expression metacharacters in the input. The returned string is a regular expression matching the literal text. + +This function provides the same behaviour as Go's +[`regexp.QuoteMeta`](https://golang.org/pkg/regexp/#Regexp.QuoteMeta) function. + +### Usage + +```go +regexp.QuoteMeta input +``` +```go +input | regexp.QuoteMeta +``` + +### Arguments + +| name | description | +|------|-------------| +| `input` | _(required)_ The input to escape | + +### Examples + +```console +$ gomplate -i '{{ `{hello}` | regexp.QuoteMeta }}' +\{hello\} +``` + ## `regexp.Replace` Replaces matches of a regular expression with the replacement string. @@ -205,10 +234,10 @@ This function provides the same behaviour as Go's ### Usage ```go -regexp.Split expression [false] input +regexp.Split expression [n] input ``` ```go -input | regexp.Split expression [false] +input | regexp.Split expression [n] ``` ### Arguments @@ -216,7 +245,7 @@ input | regexp.Split expression [false] | name | description | |------|-------------| | `expression` | _(required)_ The regular expression | -| `false` | _(optional)_ The number of matches to return | +| `n` | _(optional)_ The number of matches to return | | `input` | _(required)_ The input to search | ### Examples diff --git a/funcs/regexp.go b/funcs/regexp.go index b987132d..ce7feec9 100644 --- a/funcs/regexp.go +++ b/funcs/regexp.go @@ -58,6 +58,11 @@ func (f *ReFuncs) Match(re, input interface{}) bool { return regexp.Match(conv.ToString(re), conv.ToString(input)) } +// QuoteMeta - +func (f *ReFuncs) QuoteMeta(in interface{}) string { + return regexp.QuoteMeta(conv.ToString(in)) +} + // Replace - func (f *ReFuncs) Replace(re, replacement, input interface{}) string { return regexp.Replace(conv.ToString(re), diff --git a/regexp/regexp.go b/regexp/regexp.go index 86c7d601..e2d1df1c 100644 --- a/regexp/regexp.go +++ b/regexp/regexp.go @@ -27,6 +27,11 @@ func Match(expression, input string) bool { return re.MatchString(input) } +// QuoteMeta - +func QuoteMeta(input string) string { + return stdre.QuoteMeta(input) +} + // Replace - func Replace(expression, replacement, input string) string { re := stdre.MustCompile(expression) diff --git a/regexp/regexp_test.go b/regexp/regexp_test.go index 89f6cb86..1a6bb858 100644 --- a/regexp/regexp_test.go +++ b/regexp/regexp_test.go @@ -107,3 +107,7 @@ func TestSplit(t *testing.T) { assert.EqualValues(t, d.expected, f) } } + +func TestQuoteMeta(t *testing.T) { + assert.Equal(t, `foo\{\(\\`, QuoteMeta(`foo{(\`)) +} diff --git a/tests/integration/regexp_test.go b/tests/integration/regexp_test.go index 9ea2d240..d5cd9ff3 100644 --- a/tests/integration/regexp_test.go +++ b/tests/integration/regexp_test.go @@ -17,3 +17,9 @@ func (s *RegexpSuite) TestReplace(c *C) { `{{ "1.2.3-59" | regexp.Replace "-([0-9]*)" ".$1" }}`) result.Assert(c, icmd.Expected{ExitCode: 0, Out: "1.2.3.59"}) } + +func (s *RegexpSuite) TestQuoteMeta(c *C) { + result := icmd.RunCommand(GomplateBin, "-i", + "{{ regexp.QuoteMeta `foo{(\\` }}") + result.Assert(c, icmd.Expected{ExitCode: 0, Out: `foo\{\(\\`}) +}