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 208d19d commit 08e3f8d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
12 changes: 10 additions & 2 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: \"hello\\\n ...\\\n world\n\"",
map[string]string{"v": "hello...world "},
},
{
"a: !!binary gIGC\n",
map[string]string{"a": "\x80\x81\x82"},
Expand Down Expand Up @@ -2638,8 +2646,8 @@ func TestDecoder_LiteralWithNewLine(t *testing.T) {

func TestDecoder_TabCharacterAtRight(t *testing.T) {
yml := `
- a: [2 , 2]
b: [2 , 2]
- a: [2 , 2]
b: [2 , 2]
c: [2 , 2]`
var v []map[string][]int
if err := yaml.Unmarshal([]byte(yml), &v); err != nil {
Expand Down
14 changes: 12 additions & 2 deletions scanner/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,10 @@ 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, ' ')
isFirstLineChar = true
Expand All @@ -316,8 +320,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 08e3f8d

Please sign in to comment.