Skip to content

Commit

Permalink
care whitespace
Browse files Browse the repository at this point in the history
  • Loading branch information
goccy committed Dec 2, 2022
1 parent db6dd54 commit 6a9ddb5
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
2 changes: 1 addition & 1 deletion decode_test.go
Expand Up @@ -2148,7 +2148,7 @@ b: *a
t.Parallel()
yml := `
a:
"b": 2
"b" : 2
'c': true
`
var v struct {
Expand Down
14 changes: 14 additions & 0 deletions scanner/context.go
Expand Up @@ -6,6 +6,8 @@ import (
"github.com/goccy/go-yaml/token"
)

const whitespace = ' '

// Context context at scanning
type Context struct {
idx int
Expand Down Expand Up @@ -149,6 +151,18 @@ func (c *Context) currentChar() rune {
return rune(0)
}

func (c *Context) currentCharWithSkipWhitespace() rune {
idx := c.idx
for c.size > idx {
ch := c.src[idx]
if ch != whitespace {
return ch
}
idx++
}
return rune(0)
}

func (c *Context) nextChar() rune {
if c.size > c.idx+1 {
return c.src[c.idx+1]
Expand Down
4 changes: 2 additions & 2 deletions scanner/scanner.go
Expand Up @@ -811,9 +811,9 @@ func (s *Scanner) scan(ctx *Context) (pos int) {
token, progress := s.scanQuote(ctx, c)
ctx.addToken(token)
pos += progress
// If the quote is immediately followed by ':', the quote should be treated as a map key.
// If the non-whitespace character immediately following the quote is ':', the quote should be treated as a map key.
// Therefore, do not return and continue processing as a normal map key.
if ctx.currentChar() == ':' {
if ctx.currentCharWithSkipWhitespace() == ':' {
continue
}
return
Expand Down

0 comments on commit 6a9ddb5

Please sign in to comment.