mirror of
https://github.com/datarhei/core.git
synced 2025-09-26 20:11:29 +08:00
Fix backslash escaping in placeholder value escaping
This commit is contained in:
@@ -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)
|
||||
|
@@ -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])
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user