Skip to content

Commit

Permalink
fix: parse multiline quoted string corectly
Browse files Browse the repository at this point in the history
  • Loading branch information
JanDeDobbeleer committed Oct 12, 2023
1 parent 1651603 commit 90b0bb6
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
8 changes: 8 additions & 0 deletions decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -844,6 +844,14 @@ func TestDecoder(t *testing.T) {
"v: |\n hello\n ...\n world\n",
map[string]string{"v": "hello\n...\nworld\n"},
},
{
"v: >\n hello\n ...\n world\n",
map[string]string{"v": "hello ... world"},
},
{
"v:\n \"hello\\\n ...\\\n world\n\"",
map[string]string{"v": "hello...world"},
},
{
"a: !!binary gIGC\n",
map[string]string{"a": "\x80\x81\x82"},
Expand Down
18 changes: 15 additions & 3 deletions scanner/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,8 +306,14 @@ func (s *Scanner) scanDoubleQuote(ctx *Context) (tk *token.Token, pos int) {
c := src[idx]
pos = idx + 1
ctx.addOriginBuf(c)
var nextChar rune
if idx+1 < size {
nextChar = src[idx+1]
}
if s.isNewLineChar(c) {
value = append(value, ' ')
if nextChar != '"' {
value = append(value, ' ')
}
isFirstLineChar = true
isNewLine = true
s.progressLine(ctx)
Expand All @@ -316,8 +322,14 @@ func (s *Scanner) scanDoubleQuote(ctx *Context) (tk *token.Token, pos int) {
continue
} else if c == '\\' {
isFirstLineChar = false
if idx+1 < size {
nextChar := src[idx+1]
if nextChar != 0 {
// we need to treat this as a newline immediately and ignore the \ character
if s.isNewLineChar(nextChar) {
isFirstLineChar = true
idx++
ctx.addOriginBuf(nextChar)
continue
}
switch nextChar {
case 'b':
ctx.addOriginBuf(nextChar)
Expand Down

0 comments on commit 90b0bb6

Please sign in to comment.