Skip to content

Commit

Permalink
csv reader: fix EOF on quotes
Browse files Browse the repository at this point in the history
This commit fixes an with the csv reader where and EOF occurring on a
quoted field would cause a parsing error.

Closes #4732
  • Loading branch information
mattnibs committed Jul 17, 2024
1 parent 6e61673 commit 4cd528d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
19 changes: 12 additions & 7 deletions zio/csvio/preprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,21 @@ func (p *preprocess) parseField() ([]byte, error) {
}
if c == '"' {
hasstr = true
str, err := p.parseString()
var str []byte
str, err = p.parseString()
p.scratch = append(p.scratch, str...)
if err != nil {
if err == nil {
continue
}
if err != io.EOF {
return p.scratch, err
}
continue
}
if rune(c) == p.delimiter || c == '\n' {
ending := []byte{c}
if rune(c) == p.delimiter || c == '\n' || err == io.EOF {
var ending []byte
if err != io.EOF {
ending = []byte{c}
}
if hasstr {
// If field had quotes wrap entire field in quotes.
if last := len(p.scratch) - 1; last > 0 && p.scratch[last] == '\r' {
Expand All @@ -92,7 +98,7 @@ func (p *preprocess) parseField() ([]byte, error) {
p.scratch = append([]byte{'"'}, bytes.TrimSpace(p.scratch)...)
}
p.scratch = append(p.scratch, ending...)
return p.scratch, nil
return p.scratch, err
}
p.scratch = append(p.scratch, c)
}
Expand All @@ -108,7 +114,6 @@ func (p *preprocess) parseString() ([]byte, error) {
if c == '"' {
next, err := p.scanner.ReadByte()
if err != nil {
str = append(str, c)
return str, err
}
if next == '"' {
Expand Down
6 changes: 4 additions & 2 deletions zio/csvio/preprocess_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ field1,"field"2,field"3" my friend
field4,"field"5 with "multiple" quotes "to" escape,field6
""",""",""" has a couple "" embedded quotes and a , comma",""" """
x,"hello,
"" world , " foo,y`
"" world , " foo,y
field1,field2,"test eof with quotes"`
const expected = `
,,
"","",""
Expand All @@ -28,7 +29,8 @@ field1,"field2","field3 my friend"
field4,"field5 with multiple quotes to escape",field6
""",""",""" has a couple "" embedded quotes and a , comma",""" """
x,"hello,
"" world , foo",y`
"" world , foo",y
field1,field2,"test eof with quotes"`

p := newPreprocess(strings.NewReader(input), ',')
var buf bytes.Buffer
Expand Down

0 comments on commit 4cd528d

Please sign in to comment.