]> Cypherpunks repositories - gostls13.git/commitdiff
[release-branch.go1.4] cmd/go: handle \r in input text
authorRob Pike <r@golang.org>
Mon, 15 Dec 2014 04:00:56 +0000 (15:00 +1100)
committerAndrew Gerrand <adg@golang.org>
Tue, 17 Feb 2015 06:48:22 +0000 (06:48 +0000)
Remove carriage returns from //go:generate lines.
Carriage returns are the predecessor of BOMs and still
live on Windows.

Fixes #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>
(cherry picked from commit fde3ab843faaf4ba7a741bfdc192dbb5f2ddf209)
Reviewed-on: https://go-review.googlesource.com/4999
Reviewed-by: David Symonds <dsymonds@golang.org>
src/cmd/go/generate.go
src/cmd/go/generate_test.go

index 8991fb6af22972e63047bea755fbf6aee38a3e05..3c0af8760b5eeeb00b17c7dedfea877303ce2eac 100644 (file)
@@ -256,6 +256,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 {
index 660ebabbe84d8b2d5593bca35fec4b137c9fd421..2ec548630acc095d3847de3723d48a70d35a8a38 100644 (file)
@@ -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)
+               }
        }
 }