Repository: goccy/go-yaml
Pull request #562 (2024-12-01 11:22:58)

Fix escape character in double-quote
fix https://github.com/goccy/go-yaml/issues/561

codecov-commenter: :warning: Please install the !['codecov app svg image'](https://github.com/codecov/engineering-team/assets/152432831/e90313f4-9d3a-4b63-8b54-cfe14e7ec20d) to ensure uploads and comments are reliably processed by Codecov.

## [Codecov](https://app.codecov.io/gh/goccy/go-yaml/pull/562?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Masaaki+Goshima) Report
Attention: Patch coverage is `95.23810%` with `2 lines` in your changes missing coverage. Please review.
> Project coverage is 77.31%. Comparing base [(`f4ccce9`)](https://app.codecov.io/gh/goccy/go-yaml/commit/f4ccce9f01a41e6da7dfbfad021f1f96afbbb76f?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Masaaki+Goshima) to head [(`cc09683`)](https://app.codecov.io/gh/goccy/go-yaml/commit/cc09683873913683b40bd6b32c0a520e0cb122cc?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Masaaki+Goshima).

:exclamation: Your organization needs to install the [Codecov GitHub app](https://github.com/apps/codecov/installations/select_target) to enable full functionality.

<details><summary>Additional details and impacted files</summary>


```diff
@@            Coverage Diff             @@
##           master     #562      +/-   ##
==========================================
+ Coverage   76.95%   77.31%   +0.35%     
==========================================
  Files          21       21              
  Lines        7429     7440      +11     
==========================================
+ Hits         5717     5752      +35     
+ Misses       1314     1290      -24     
  Partials      398      398              
```

</details>

Patch:
diff --git a/scanner/scanner.go b/scanner/scanner.go
index bc2710f..a379a8c 100644
--- a/scanner/scanner.go
+++ b/scanner/scanner.go
@@ -360,54 +360,74 @@ func (s *Scanner) scanDoubleQuote(ctx *Context) (*token.Token, error) {
 			nextChar := src[idx+1]
 			progress := 0
 			switch nextChar {
-			case 'b':
+			case '0':
 				progress = 1
 				ctx.addOriginBuf(nextChar)
-				value = append(value, '\b')
-			case 'e':
+				value = append(value, 0x00)
+			case 'a':
 				progress = 1
 				ctx.addOriginBuf(nextChar)
-				value = append(value, '\x1B')
-			case 'f':
+				value = append(value, 0x07)
+			case 'b':
 				progress = 1
 				ctx.addOriginBuf(nextChar)
-				value = append(value, '\f')
+				value = append(value, 0x08)
+			case 't':
+				progress = 1
+				ctx.addOriginBuf(nextChar)
+				value = append(value, 0x09)
 			case 'n':
 				progress = 1
 				ctx.addOriginBuf(nextChar)
-				value = append(value, '\n')
+				value = append(value, 0x0A)
+			case 'v':
+				progress = 1
+				ctx.addOriginBuf(nextChar)
+				value = append(value, 0x0B)
+			case 'f':
+				progress = 1
+				ctx.addOriginBuf(nextChar)
+				value = append(value, 0x0C)
 			case 'r':
 				progress = 1
 				ctx.addOriginBuf(nextChar)
-				value = append(value, '\r')
-			case 't':
+				value = append(value, 0x0D)
+			case 'e':
 				progress = 1
 				ctx.addOriginBuf(nextChar)
-				value = append(value, '\t')
-			case 'v':
+				value = append(value, 0x1B)
+			case ' ':
 				progress = 1
 				ctx.addOriginBuf(nextChar)
-				value = append(value, '\v')
-			case 'L': // LS (#x2028)
+				value = append(value, 0x20)
+			case '"':
 				progress = 1
 				ctx.addOriginBuf(nextChar)
-				value = append(value, []rune{'\xE2', '\x80', '\xA8'}...)
-			case 'N': // NEL (#x85)
+				value = append(value, 0x22)
+			case '/':
 				progress = 1
 				ctx.addOriginBuf(nextChar)
-				value = append(value, []rune{'\xC2', '\x85'}...)
-			case 'P': // PS (#x2029)
+				value = append(value, 0x2F)
+			case '\\':
 				progress = 1
 				ctx.addOriginBuf(nextChar)
-				value = append(value, []rune{'\xE2', '\x80', '\xA9'}...)
-			case '_': // #xA0
+				value = append(value, 0x5C)
+			case 'N':
 				progress = 1
 				ctx.addOriginBuf(nextChar)
-				value = append(value, []rune{'\xC2', '\xA0'}...)
-			case '"':
+				value = append(value, 0x85)
+			case '_':
 				progress = 1
 				ctx.addOriginBuf(nextChar)
-				value = append(value, nextChar)
+				value = append(value, 0xA0)
+			case 'L':
+				progress = 1
+				ctx.addOriginBuf(nextChar)
+				value = append(value, 0x2028)
+			case 'P':
+				progress = 1
+				ctx.addOriginBuf(nextChar)
+				value = append(value, 0x2029)
 			case 'x':
 				if idx+3 >= size {
 					progress = 1
@@ -438,10 +458,6 @@ func (s *Scanner) scanDoubleQuote(ctx *Context) (*token.Token, error) {
 					codeNum := hexRunesToInt(src[idx+2 : idx+progress+1])
 					value = append(value, rune(codeNum))
 				}
-			case '\\':
-				progress = 1
-				ctx.addOriginBuf(nextChar)
-				value = append(value, c)
 			case '\n':
 				isFirstLineChar = true
 				isNewLine = true
@@ -454,12 +470,6 @@ func (s *Scanner) scanDoubleQuote(ctx *Context) (*token.Token, error) {
 				progress = 1
 				ctx.addOriginBuf(nextChar)
 				value = append(value, nextChar)
-			case '0':
-				progress = 1
-				ctx.addOriginBuf(nextChar)
-				value = append(value, '\x00')
-			case ' ':
-				// skip escape character.
 			default:
 				s.progressColumn(ctx, 1)
 				return nil, ErrInvalidToken(


Test patch:
diff --git a/decode_test.go b/decode_test.go
index 85b7712..b95cc0d 100644
--- a/decode_test.go
+++ b/decode_test.go
@@ -1107,6 +1107,16 @@ c:
 			"v: あいうえお\nv2: かきくけこ",
 			map[string]string{"v": "あいうえお", "v2": "かきくけこ"},
 		},
+		{
+			`
+- "Fun with \\"
+- "\" \a \b \e \f"
+- "\n \r \t \v \0"
+- "\  \_ \N \L \P \
+  \x41 \u0041 \U00000041"
+`,
+			[]string{"Fun with \\", "\" \u0007 \b \u001b \f", "\n \r \t \u000b \u0000", "\u0020 \u00a0 \u0085 \u2028 \u2029 A A A"},
+		},
 	}
 	for _, test := range tests {
 		t.Run(test.source, func(t *testing.T) {