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)