Support for Windows newlines.

This commit is contained in:
Joern Barthel 2017-04-07 10:41:55 +02:00
parent 059a1b2c0f
commit 9622b49c45
2 changed files with 34 additions and 5 deletions

View File

@ -462,11 +462,12 @@ func interpolationFuncCeil() ast.Function {
// interpolationFuncChomp removes trailing newlines from the given string // interpolationFuncChomp removes trailing newlines from the given string
func interpolationFuncChomp() ast.Function { func interpolationFuncChomp() ast.Function {
newlines := regexp.MustCompile(`(?:\r\n?|\n)*\z`)
return ast.Function{ return ast.Function{
ArgTypes: []ast.Type{ast.TypeString}, ArgTypes: []ast.Type{ast.TypeString},
ReturnType: ast.TypeString, ReturnType: ast.TypeString,
Callback: func(args []interface{}) (interface{}, error) { Callback: func(args []interface{}) (interface{}, error) {
return strings.TrimRight(args[0].(string), "\n"), nil return newlines.ReplaceAllString(args[0].(string), ""), nil
}, },
} }
} }

View File

@ -386,10 +386,38 @@ func TestInterpolateFuncChomp(t *testing.T) {
}, },
{ {
`${chomp("goodbye\ncruel\nworld\n")}`, fmt.Sprintf(`${chomp("%s")}`, "goodbye\ncruel\nworld"),
`goodbye "goodbye\ncruel\nworld",
cruel false,
world`, },
{
fmt.Sprintf(`${chomp("%s")}`, "goodbye\r\nwindows\r\nworld"),
"goodbye\r\nwindows\r\nworld",
false,
},
{
fmt.Sprintf(`${chomp("%s")}`, "goodbye\ncruel\nworld\n"),
"goodbye\ncruel\nworld",
false,
},
{
fmt.Sprintf(`${chomp("%s")}`, "goodbye\ncruel\nworld\n\n\n\n"),
"goodbye\ncruel\nworld",
false,
},
{
fmt.Sprintf(`${chomp("%s")}`, "goodbye\r\nwindows\r\nworld\r\n"),
"goodbye\r\nwindows\r\nworld",
false,
},
{
fmt.Sprintf(`${chomp("%s")}`, "goodbye\r\nwindows\r\nworld\r\n\r\n\r\n\r\n"),
"goodbye\r\nwindows\r\nworld",
false, false,
}, },
}, },