From 6a080f2015646314d815754c1284506cd93408c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ken=E2=80=99ichiro=20Oyama?= Date: Wed, 24 May 2023 17:12:33 +0900 Subject: [PATCH] Handle `\r` in a double-quoted string the same as `\n` (#372) * Add tests for string containing `\n` or `\r` * Handle `\r` in a double-quoted string the same as `\n` Fix https://github.com/goccy/go-yaml/issues/371 --- decode_test.go | 8 ++++++++ scanner/scanner.go | 5 +++++ 2 files changed, 13 insertions(+) diff --git a/decode_test.go b/decode_test.go index 2549bea0..0ac4a7fa 100644 --- a/decode_test.go +++ b/decode_test.go @@ -425,6 +425,14 @@ func TestDecoder(t *testing.T) { `"1": "a\x2Fb\u002Fc\U0000002Fd"`, map[interface{}]interface{}{"1": `a/b/c/d`}, }, + { + "'1': \"2\\n3\"", + map[interface{}]interface{}{"1": "2\n3"}, + }, + { + "'1': \"2\\r\\n3\"", + map[interface{}]interface{}{"1": "2\r\n3"}, + }, { "a: -b_c", diff --git a/scanner/scanner.go b/scanner/scanner.go index ce9c6654..b0eac48d 100644 --- a/scanner/scanner.go +++ b/scanner/scanner.go @@ -339,6 +339,11 @@ func (s *Scanner) scanDoubleQuote(ctx *Context) (tk *token.Token, pos int) { value = append(value, '\n') idx++ continue + case 'r': + ctx.addOriginBuf(nextChar) + value = append(value, '\r') + idx++ + continue case 'v': ctx.addOriginBuf(nextChar) value = append(value, '\v')