Skip to content

Commit

Permalink
fix trailing new line chars handling (#507)
Browse files Browse the repository at this point in the history
  • Loading branch information
goccy authored Nov 6, 2024
1 parent 7edcf34 commit 29b57b4
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 19 deletions.
6 changes: 0 additions & 6 deletions decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2763,18 +2763,12 @@ func TestDecoder_LiteralWithNewLine(t *testing.T) {
{
Node: "hello\nworld\n",
},
{
Node: "hello\nworld\n\n",
},
{
LastNode: "hello\nworld",
},
{
LastNode: "hello\nworld\n",
},
{
LastNode: "hello\nworld\n\n",
},
}
// struct(want) -> Marshal -> Unmarchal -> struct(got)
for _, want := range tests {
Expand Down
25 changes: 25 additions & 0 deletions lexer/lexer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2172,6 +2172,31 @@ s: >-3
},
},
},
{
YAML: `
|
a
`,
Tokens: token.Tokens{
{
Type: token.LiteralType,
CharacterType: token.CharacterTypeIndicator,
Indicator: token.BlockScalarIndicator,
Value: "|",
Origin: "\n|\n",
},
{
Type: token.StringType,
CharacterType: token.CharacterTypeMiscellaneous,
Indicator: token.NotIndicator,
Value: "a\n",
Origin: " a\n\n\n\n",
},
},
},
}
for _, test := range tests {
t.Run(test.YAML, func(t *testing.T) {
Expand Down
38 changes: 25 additions & 13 deletions scanner/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,27 +273,39 @@ func (c *Context) existsBuffer() bool {

func (c *Context) bufferedSrc() []rune {
src := c.buf[:c.notSpaceCharPos]
if c.isDocument() && (strings.HasPrefix(c.docOpt, "-") || strings.HasSuffix(c.docOpt, "-")) {
// remove end '\n' character and trailing empty lines
if c.isDocument() {
// remove end '\n' character and trailing empty lines.
// https://yaml.org/spec/1.2.2/#8112-block-chomping-indicator
for {
if len(src) > 0 && src[len(src)-1] == '\n' {
src = src[:len(src)-1]
continue
if c.hasTrimAllEndNewlineOpt() {
// If the '-' flag is specified, all trailing newline characters will be removed.
src = []rune(strings.TrimRight(string(src), "\n"))
} else {
// Normally, all but one of the trailing newline characters are removed.
var newLineCharCount int
for i := len(src) - 1; i >= 0; i-- {
if src[i] == '\n' {
newLineCharCount++
continue
}
break
}
break
}
for {
if len(src) > 0 && src[len(src)-1] == ' ' {
src = src[:len(src)-1]
continue
removedNewLineCharCount := newLineCharCount - 1
for removedNewLineCharCount > 0 {
src = []rune(strings.TrimSuffix(string(src), "\n"))
removedNewLineCharCount--
}
break
}

// If the text ends with a space character, remove all of them.
src = []rune(strings.TrimRight(string(src), " "))
}
return src
}

func (c *Context) hasTrimAllEndNewlineOpt() bool {
return strings.HasPrefix(c.docOpt, "-") || strings.HasSuffix(c.docOpt, "-")
}

func (c *Context) bufferedToken(pos *token.Position) *token.Token {
if c.idx == 0 {
return nil
Expand Down

0 comments on commit 29b57b4

Please sign in to comment.