Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions marshaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -517,12 +517,26 @@ func (enc *Encoder) encodeQuotedString(multiline bool, b []byte, v string) []byt
del = 0x7f
)

for _, r := range []byte(v) {
bv := []byte(v)
for i := 0; i < len(bv); i++ {
r := bv[i]
switch r {
case '\\':
b = append(b, `\\`...)
case '"':
b = append(b, `\"`...)
if multiline {
// Quotation marks do not need to be quoted in multiline strings unless
// it contains 3 consecutive. If 3+ quotes appear, quote all of them
// because it's visually better
if i+2 > len(bv) || bv[i+1] != '"' || bv[i+2] != '"' {
b = append(b, r)
} else {
b = append(b, `\"\"\"`...)
i += 2
}
} else {
b = append(b, `\"`...)
}
case '\b':
b = append(b, `\b`...)
case '\f':
Expand Down
48 changes: 48 additions & 0 deletions marshaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,54 @@ name = 'Alice'
expected: `A = """
hello
world"""
`,
},
{
desc: "multi-line quotation",
v: struct {
A string `toml:",multiline"`
}{
A: "hello\n\"world\"",
},
expected: `A = """
hello
"world""""
`,
},
{
desc: "multi-line triple quotation",
v: struct {
A string `toml:",multiline"`
}{
A: "hello\n\"\"\"world\"",
},
expected: `A = """
hello
\"\"\"world""""
`,
},
{
desc: "multi-line triple quotation",
v: struct {
A string `toml:",multiline"`
}{
A: "hello\n\"world\"\"\"",
},
expected: `A = """
hello
"world\"\"\""""
`,
},
{
desc: "multi-line sextuple quotation",
v: struct {
A string `toml:",multiline"`
}{
A: "hello\n\"\"\"\"\"\"world\"",
},
expected: `A = """
hello
\"\"\"\"\"\"world""""
`,
},
{
Expand Down
Loading