]> Cypherpunks repositories - gostls13.git/commitdiff
encoding/csv: truncate carriage returns at EOF
authorJoe Tsai <joetsai@digital-static.net>
Fri, 1 Dec 2017 19:41:46 +0000 (11:41 -0800)
committerJoe Tsai <thebrokentoaster@gmail.com>
Tue, 5 Dec 2017 18:44:31 +0000 (18:44 +0000)
This fixes a regression where only CRLF was folded into LF at EOF.
Now, we also truncate trailing CR at EOF to preserve the old behavior.

Every one of the test cases added exactly matches the behavior
of Go1.9, even if the results are somewhat unexpected.

Fixes #22937

Change-Id: I1bc6550533163ae489ea77ec1e598163267b7eec
Reviewed-on: https://go-review.googlesource.com/81577
Run-TryBot: Joe Tsai <thebrokentoaster@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
src/encoding/csv/reader.go
src/encoding/csv/reader_test.go

index 1350f3ebdd87cad6dbfd4200bb0cd748c8dcf7c9..2efc7ad0943082246e5990b41e3feeb0254a4b66 100644 (file)
@@ -224,6 +224,10 @@ func (r *Reader) readLine() ([]byte, error) {
        }
        if len(line) > 0 && err == io.EOF {
                err = nil
+               // For backwards compatibility, drop trailing \r before EOF.
+               if line[len(line)-1] == '\r' {
+                       line = line[:len(line)-1]
+               }
        }
        r.numLine++
        // Normalize \r\n to \n on all input lines.
index d62aa77382e43c8a78f09c321994c8d23a66b25e..1fc69f9ab88314eb45976762ac1fc5f9f7b4b3c2 100644 (file)
@@ -246,7 +246,43 @@ x,,,
        }, {
                Name:   "TrailingCR",
                Input:  "field1,field2\r",
-               Output: [][]string{{"field1", "field2\r"}},
+               Output: [][]string{{"field1", "field2"}},
+       }, {
+               Name:   "QuotedTrailingCR",
+               Input:  "\"field\"\r",
+               Output: [][]string{{"field"}},
+       }, {
+               Name:  "QuotedTrailingCRCR",
+               Input: "\"field\"\r\r",
+               Error: &ParseError{StartLine: 1, Line: 1, Column: 6, Err: ErrQuote},
+       }, {
+               Name:   "FieldCR",
+               Input:  "field\rfield\r",
+               Output: [][]string{{"field\rfield"}},
+       }, {
+               Name:   "FieldCRCR",
+               Input:  "field\r\rfield\r\r",
+               Output: [][]string{{"field\r\rfield\r"}},
+       }, {
+               Name:   "FieldCRCRLF",
+               Input:  "field\r\r\nfield\r\r\n",
+               Output: [][]string{{"field\r"}, {"field\r"}},
+       }, {
+               Name:   "FieldCRCRLFCR",
+               Input:  "field\r\r\n\rfield\r\r\n\r",
+               Output: [][]string{{"field\r"}, {"\rfield\r"}},
+       }, {
+               Name:   "FieldCRCRLFCRCR",
+               Input:  "field\r\r\n\r\rfield\r\r\n\r\r",
+               Output: [][]string{{"field\r"}, {"\r\rfield\r"}, {"\r"}},
+       }, {
+               Name:  "MultiFieldCRCRLFCRCR",
+               Input: "field1,field2\r\r\n\r\rfield1,field2\r\r\n\r\r,",
+               Output: [][]string{
+                       {"field1", "field2\r"},
+                       {"\r\rfield1", "field2\r"},
+                       {"\r\r", ""},
+               },
        }, {
                Name:             "NonASCIICommaAndComment",
                Input:            "a£b,c£ \td,e\n€ comment\n",