Skip to content

Commit

Permalink
Multiline strings can't end with "\"
Browse files Browse the repository at this point in the history
i.e. this is valid:

	key = """ foo \
	"""

But this is not:

	key = """ foo \ """

Fixes #322
  • Loading branch information
arp242 committed Feb 12, 2022
1 parent 51b22f2 commit 2249a9c
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
8 changes: 6 additions & 2 deletions parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ func (p *parser) value(it item, parentIsArray bool) (interface{}, tomlType) {
case itemString:
return p.replaceEscapes(it, it.val), p.typeOfPrimitive(it)
case itemMultilineString:
return p.replaceEscapes(it, stripFirstNewline(stripEscapedNewlines(it.val))), p.typeOfPrimitive(it)
return p.replaceEscapes(it, stripFirstNewline(p.stripEscapedNewlines(it.val))), p.typeOfPrimitive(it)
case itemRawString:
return it.val, p.typeOfPrimitive(it)
case itemRawMultilineString:
Expand Down Expand Up @@ -647,7 +647,7 @@ func stripFirstNewline(s string) string {
}

// Remove newlines inside triple-quoted strings if a line ends with "\".
func stripEscapedNewlines(s string) string {
func (p *parser) stripEscapedNewlines(s string) string {
split := strings.Split(s, "\n")
if len(split) < 1 {
return s
Expand Down Expand Up @@ -679,6 +679,10 @@ func stripEscapedNewlines(s string) string {
continue
}

if i == len(split)-1 {
p.panicf("invalid escape: '\\ '")
}

split[i] = line[:len(line)-1] // Remove \
if len(split)-1 > i {
split[i+1] = strings.TrimLeft(split[i+1], " \t\r")
Expand Down
2 changes: 0 additions & 2 deletions toml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,8 +299,6 @@ func TestToml(t *testing.T) {
"invalid/inline-table/add",
"invalid/table/duplicate-key-dotted-table",
"invalid/table/duplicate-key-dotted-table2",

"invalid/string/multiline-bad-escape-3", // https://github.com/BurntSushi/toml/issues/322
},
}

Expand Down

0 comments on commit 2249a9c

Please sign in to comment.