1
0
mirror of https://github.com/golang/go synced 2024-11-23 18:20:04 -07:00

encoding/csv: disallow quote for use as Comma

'"' has special semantic meaning that conflicts with using it as Comma.

Change-Id: Ife25ba43ca25dba2ea184c1bb7579a230d376059
Reviewed-on: https://go-review.googlesource.com/99696
Run-TryBot: Joe Tsai <thebrokentoaster@gmail.com>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
Joe Tsai 2018-03-08 14:39:43 -08:00 committed by Joe Tsai
parent 5d22cebb12
commit 2cc15b18db
3 changed files with 15 additions and 3 deletions

View File

@ -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.

View File

@ -359,6 +359,10 @@ x,,,
Error: errInvalidDelim,
}, {
Name: "BadComma3",
Comma: '"',
Error: errInvalidDelim,
}, {
Name: "BadComma4",
Comma: utf8.RuneError,
Error: errInvalidDelim,
}, {

View File

@ -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 {