mirror of
https://github.com/golang/go
synced 2024-11-12 03:10:22 -07:00
encoding/csv: skip blank lines when FieldsPerRecord >= 0
Fixes #11050. Change-Id: Ie5d16960a1f829af947d82a63fe414924cd02ff6 Reviewed-on: https://go-review.googlesource.com/10666 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
This commit is contained in:
parent
4105265c66
commit
ab89378cb7
@ -228,16 +228,15 @@ func (r *Reader) parseRecord() (fields []string, err error) {
|
||||
}
|
||||
r.r.UnreadRune()
|
||||
|
||||
// If FieldsPerRecord is greater then 0 we can assume the final
|
||||
// length of fields to be equal to FieldsPerRecord.
|
||||
if r.FieldsPerRecord > 0 {
|
||||
fields = make([]string, 0, r.FieldsPerRecord)
|
||||
}
|
||||
|
||||
// At this point we have at least one field.
|
||||
for {
|
||||
haveField, delim, err := r.parseField()
|
||||
if haveField {
|
||||
// If FieldsPerRecord is greater then 0 we can assume the final
|
||||
// length of fields to be equal to FieldsPerRecord.
|
||||
if r.FieldsPerRecord > 0 && fields == nil {
|
||||
fields = make([]string, 0, r.FieldsPerRecord)
|
||||
}
|
||||
fields = append(fields, r.field.String())
|
||||
}
|
||||
if delim == '\n' || err == io.EOF {
|
||||
|
@ -86,6 +86,15 @@ field"`,
|
||||
{"d", "e", "f"},
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "BlankLineFieldCount",
|
||||
Input: "a,b,c\n\nd,e,f\n\n",
|
||||
UseFieldsPerRecord: true,
|
||||
Output: [][]string{
|
||||
{"a", "b", "c"},
|
||||
{"d", "e", "f"},
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "TrimSpace",
|
||||
Input: " a, b, c\n",
|
||||
|
Loading…
Reference in New Issue
Block a user