From bf607023c99d8cc8008e6962ad87fc40a1694e53 Mon Sep 17 00:00:00 2001 From: Jan De Dobbeleer Date: Tue, 14 Feb 2023 16:06:18 +0100 Subject: [PATCH] fix: parse multiline quoted string corectly --- decode_test.go | 8 ++++++++ scanner/scanner.go | 18 +++++++++++++++--- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/decode_test.go b/decode_test.go index 2373804d..0c835ceb 100644 --- a/decode_test.go +++ b/decode_test.go @@ -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"}, diff --git a/scanner/scanner.go b/scanner/scanner.go index b0eac48d..6369cfee 100644 --- a/scanner/scanner.go +++ b/scanner/scanner.go @@ -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) @@ -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)