diff --git a/src/encoding/csv/reader.go b/src/encoding/csv/reader.go index 2efc7ad0943..a2fd4c0970d 100644 --- a/src/encoding/csv/reader.go +++ b/src/encoding/csv/reader.go @@ -91,7 +91,7 @@ var ( var errInvalidDelim = errors.New("csv: invalid field or comment delimiter") func validDelim(r rune) bool { - return r != 0 && r != '\r' && r != '\n' && utf8.ValidRune(r) && r != utf8.RuneError + return r != 0 && r != '"' && r != '\r' && r != '\n' && utf8.ValidRune(r) && r != utf8.RuneError } // A Reader reads records from a CSV-encoded file. diff --git a/src/encoding/csv/reader_test.go b/src/encoding/csv/reader_test.go index 1fc69f9ab88..5121791cb36 100644 --- a/src/encoding/csv/reader_test.go +++ b/src/encoding/csv/reader_test.go @@ -359,6 +359,10 @@ x,,, Error: errInvalidDelim, }, { Name: "BadComma3", + Comma: '"', + Error: errInvalidDelim, + }, { + Name: "BadComma4", Comma: utf8.RuneError, Error: errInvalidDelim, }, { diff --git a/src/encoding/csv/writer_test.go b/src/encoding/csv/writer_test.go index 99bc84e998a..011f01c172a 100644 --- a/src/encoding/csv/writer_test.go +++ b/src/encoding/csv/writer_test.go @@ -13,7 +13,9 @@ import ( var writeTests = []struct { Input [][]string Output string + Error error UseCRLF bool + Comma rune }{ {Input: [][]string{{"abc"}}, Output: "abc\n"}, {Input: [][]string{{"abc"}}, Output: "abc\r\n", UseCRLF: true}, @@ -41,6 +43,9 @@ var writeTests = []struct { {Input: [][]string{{`\.`}}, Output: "\"\\.\"\n"}, {Input: [][]string{{"x09\x41\xb4\x1c", "aktau"}}, Output: "x09\x41\xb4\x1c,aktau\n"}, {Input: [][]string{{",x09\x41\xb4\x1c", "aktau"}}, Output: "\",x09\x41\xb4\x1c\",aktau\n"}, + {Input: [][]string{{"a", "a", ""}}, Output: "a|a|\n", Comma: '|'}, + {Input: [][]string{{",", ",", ""}}, Output: ",|,|\n", Comma: '|'}, + {Input: [][]string{{"foo"}}, Comma: '"', Error: errInvalidDelim}, } func TestWrite(t *testing.T) { @@ -48,9 +53,12 @@ func TestWrite(t *testing.T) { b := &bytes.Buffer{} f := NewWriter(b) f.UseCRLF = tt.UseCRLF + if tt.Comma != 0 { + f.Comma = tt.Comma + } err := f.WriteAll(tt.Input) - if err != nil { - t.Errorf("Unexpected error: %s\n", err) + if err != tt.Error { + t.Errorf("Unexpected error:\ngot %v\nwant %v", err, tt.Error) } out := b.String() if out != tt.Output {