Skip to content

Commit

Permalink
chomp carriage returns
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 727659339
  • Loading branch information
txtpbfmt-copybara-robot committed Feb 18, 2025
1 parent feedd82 commit 38aea86
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.18
go-version: 1.19

- name: Build
run: go build -v ./...
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/protocolbuffers/txtpbfmt

go 1.18
go 1.19

require (
github.com/golang/glog v1.2.4
Expand Down
5 changes: 4 additions & 1 deletion parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,7 @@ func (p *parser) parse(isRoot bool) (result []*ast.Node, endPos ast.Position, er
// p.parse is often invoked with the index pointing at the newline character
// after the previous item. We should still report that this item starts in
// the next line.
p.consume('\r')
p.consume('\n')
startPos := p.position()

Expand Down Expand Up @@ -853,13 +854,15 @@ func (p *parser) readFormatterDisabledBlock() (string, error) {
previousPos := p.position()
start := p.index
for p.index < p.length && p.isBlankSep(p.index) {
if p.consume('\n') {
if p.consume('\n') || (p.consume('\r') && p.consume('\n')) {
// Include up to one blank line before the 'off' directive.
start = p.index - 1
} else if p.consume(' ') {
// Do nothing. Side-effect is to advance p.index.
} else if p.consume('\t') {
// Do nothing. Side-effect is to advance p.index.
} else {
return "", fmt.Errorf("unhandled isBlankSep at %s", p.errorContext())
}
}
offStart := p.position()
Expand Down
7 changes: 6 additions & 1 deletion parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2664,7 +2664,12 @@ foo: "\"bar\""`,
foo: '"bar"'
`,
},
}
{
name: "carriage returns",
in: "a{\r\n}\r\n",
out: "a {\n}\n",
},
}
// Test FormatWithConfig with inputs.
for _, input := range inputs {
got, err := FormatWithConfig([]byte(input.in), input.config)
Expand Down

0 comments on commit 38aea86

Please sign in to comment.