Skip to content

Commit

Permalink
Merge pull request #57 from goccy/feature/fix-right-unnecessary-spaces
Browse files Browse the repository at this point in the history
Remove unnecessary spaces at right side
  • Loading branch information
goccy authored Dec 11, 2019
2 parents d65a7f6 + 8f13c01 commit 45e0b4b
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
29 changes: 29 additions & 0 deletions parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,35 @@ a: |
ccccccc
d: eeeeeeeeeeeeeeeee
`,
},
{
`
a: b
c
`,
`
a: b c
`,
},
{
`
a:
b: c
`,
`
a:
b: c
`,
},
{
`
a: b
c: d
`,
`
a: b
c: d
`,
},
}
Expand Down
11 changes: 11 additions & 0 deletions scanner/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,17 @@ func (c *Context) addOriginBuf(r rune) {
c.obuf = append(c.obuf, r)
}

func (c *Context) removeRightSpaceFromBuf() int {
trimmedBuf := strings.TrimRight(string(c.obuf), " ")
buflen := len([]rune(trimmedBuf))
diff := len(c.obuf) - buflen
if diff > 0 {
c.obuf = c.obuf[:buflen]
c.buf = []rune(c.bufferedSrc())
}
return diff
}

func (c *Context) isDocument() bool {
return c.isLiteral || c.isFolded || c.isRawFolded
}
Expand Down
13 changes: 13 additions & 0 deletions scanner/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,19 @@ func (s *Scanner) scanNewLine(ctx *Context, c rune) {
s.savedPos = s.pos()
s.savedPos.Column -= len([]rune(ctx.bufferedSrc()))
}

// if the following case, origin buffer has unnecessary two spaces.
// So, `removeRightSpaceFromOriginBuf` remove them, also fix column number too.
// ---
// a:[space][space]
// b: c
removedNum := ctx.removeRightSpaceFromBuf()
if removedNum > 0 {
s.column -= removedNum
s.offset -= removedNum
s.savedPos.Column -= removedNum
}

if ctx.isEOS() {
s.addBufferedTokenIfExists(ctx)
} else if s.isAnchor {
Expand Down

0 comments on commit 45e0b4b

Please sign in to comment.