Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix trailing new line chars handling #507

Merged
merged 1 commit into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading