Fix backslash escaping in placeholder value escaping

This commit is contained in:
Ingo Oppermann
2022-07-05 10:45:57 +02:00
parent 5900b725e6
commit ae8f5f00a6
3 changed files with 13 additions and 10 deletions

View File

@@ -88,12 +88,13 @@ func replace(what, placeholder, value string) string {
what = re.ReplaceAllStringFunc(what, func(match string) string {
matches := re.FindStringSubmatch(match)
var v string
v := value
if matches[2] != "" {
v = strings.ReplaceAll(value, matches[2], `\`+matches[2])
} else {
v = value
if matches[2] != `\` {
v = strings.ReplaceAll(v, `\`, `\\`)
}
v = strings.ReplaceAll(v, matches[2], `\`+matches[2])
}
return strings.Replace(match, match, v, 1)

View File

@@ -7,21 +7,22 @@ import (
)
func TestReplace(t *testing.T) {
foobar := `;:.,-_$£!^`
foobar := `;:.,-_$\£!^`
samples := [][2]string{
{"{foobar}", foobar},
{"{foobar^:}", `;\:.,-_$£!^`},
{"{foobar^:}barfoo{foobar^:}", `;\:.,-_$£!^barfoo;\:.,-_$£!^`},
{"{foobar^:}", `;\:.,-_$\\£!^`},
{"{foobar^:}barfoo{foobar^:}", `;\:.,-_$\\£!^barfoo;\:.,-_$\\£!^`},
{"{foobar^:.}", "{foobar^:.}"},
{"{foobar^}", "{foobar^}"},
{"{barfoo^:}", "{barfoo^:}"},
{"{foobar^^}", `;:.,-_$£!\^`},
{"{foobar^^}", `;:.,-_$\\£!\^`},
{`{foobar^\}`, `;:.,-_$\\£!^`},
}
for _, e := range samples {
replaced := replace(e[0], "foobar", foobar)
require.Equal(t, e[1], replaced)
require.Equal(t, e[1], replaced, e[0])
}
}