Skip to content

Commit

Permalink
cmd/go: handle \r in input text
Browse files Browse the repository at this point in the history
Remove carriage returns from //go:generate lines.
Carriage returns are the predecessor of BOMs and still
live on Windows.

Fixes golang#9264

Change-Id: I637748c74335c696b3630f52f2100061153fcdb4
Reviewed-on: https://go-review.googlesource.com/1564
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Reviewed-by: Andrew Gerrand <adg@golang.org>
  • Loading branch information
robpike committed Dec 15, 2014
1 parent ab96371 commit fde3ab8
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/cmd/go/generate.go
Expand Up @@ -255,6 +255,10 @@ func (g *Generator) split(line string) []string {
// Parse line, obeying quoted strings.
var words []string
line = line[len("//go:generate ") : len(line)-1] // Drop preamble and final newline.
// There may still be a carriage return.
if len(line) > 0 && line[len(line)-1] == '\r' {
line = line[:len(line)-1]
}
// One (possibly quoted) word per iteration.
Words:
for {
Expand Down
6 changes: 6 additions & 0 deletions src/cmd/go/generate_test.go
Expand Up @@ -40,9 +40,15 @@ func TestGenerateCommandParse(t *testing.T) {
}
g.setShorthand([]string{"-command", "yacc", "go", "tool", "yacc"})
for _, test := range splitTests {
// First with newlines.
got := g.split("//go:generate " + test.in + "\n")
if !reflect.DeepEqual(got, test.out) {
t.Errorf("split(%q): got %q expected %q", test.in, got, test.out)
}
// Then with CRLFs, thank you Windows.
got = g.split("//go:generate " + test.in + "\r\n")
if !reflect.DeepEqual(got, test.out) {
t.Errorf("split(%q): got %q expected %q", test.in, got, test.out)
}
}
}

0 comments on commit fde3ab8

Please sign in to comment.