Skip to content

Commit

Permalink
add feature to have a multiple line with quote escaping
Browse files Browse the repository at this point in the history
  • Loading branch information
nicovogelaar committed Jun 28, 2022
1 parent 99df005 commit cbd8c4b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
8 changes: 6 additions & 2 deletions reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,13 +198,16 @@ func (r *Reader) readQuotedField() (string, error) {

s := &r.tmpBuf
defer r.tmpBuf.Reset() // TODO: Not using defer here is faster.
var previousChar rune
for {
char, _, err := r.r.ReadRune()
if err != nil {
return s.String(), err
}
if char != r.opts.QuoteChar {
s.WriteRune(char)
if char != r.opts.QuoteChar || previousChar == r.opts.EscapeChar {
if char != r.opts.EscapeChar {
s.WriteRune(char)
}
} else {
switch r.opts.DoubleQuote {
case DoDoubleQuote:
Expand Down Expand Up @@ -237,6 +240,7 @@ func (r *Reader) readQuotedField() (string, error) {
panic("Unrecognized double quote mode.")
}
}
previousChar = char
}

// Required by Go 1.0 to compile. Unreachable code.
Expand Down
15 changes: 15 additions & 0 deletions reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,3 +284,18 @@ func TestReadingWithComments(t *testing.T) {
t.Error("Expected EOF, but got:", err)
}
}

func TestReadingMultipleLineWithQuoteEscaping(t *testing.T) {
t.Parallel()

b := new(bytes.Buffer)
// a,"b \"c d\ne\"", f
b.WriteString(`a,"b \"c d
e\"",f` + "\n")
r := NewReader(b)

err := testReadingSingleLine(t, r, []string{"a", `b "c d` + "\n" + `e"`, "f"})
if err != nil && err != io.EOF {
t.Error("Unexpected error:", err)
}
}

0 comments on commit cbd8c4b

Please sign in to comment.