Skip to content

Commit 78c1d10

Browse files
Added support for the unicode \000 charater incase of no quote to write
1 parent 65b8456 commit 78c1d10

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

writer.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,10 @@ func (w *Writer) WriteAll(records [][]string) (err error) {
120120
// Empty fields, files with a Comma, fields with a quote or newline, and
121121
// fields which start with a space must be enclosed in quotes.
122122
func (w *Writer) fieldNeedsQuotes(field string) bool {
123+
if w.Quote == 0 {
124+
return false
125+
}
126+
123127
if len(field) == 0 || strings.IndexRune(field, w.Comma) >= 0 || strings.IndexRune(field, w.Quote) >= 0 || strings.IndexAny(field, "\r\n") >= 0 {
124128
return true
125129
}

writer_test.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@ import (
1111
)
1212

1313
var writeTests = []struct {
14-
Input [][]string
15-
Output string
16-
UseCRLF bool
17-
Quote rune
18-
Comma rune
14+
Input [][]string
15+
Output string
16+
UseCRLF bool
17+
Quote rune
18+
Comma rune
19+
ZeroQuote bool
1920
}{
2021
{Input: [][]string{{"abc"}}, Output: "abc\n"},
2122
{Input: [][]string{{"abc"}}, Output: "abc\r\n", UseCRLF: true},
@@ -34,6 +35,7 @@ var writeTests = []struct {
3435
{Input: [][]string{{"abc", "def"}}, Output: "abc;def\n", Comma: ';'},
3536
{Input: [][]string{{`a|b`}}, Output: `|a||b|` + "\n", Quote: '|'},
3637
{Input: [][]string{{`|a|b|`}}, Output: `|||a||b|||` + "\n", Quote: '|'},
38+
{Input: [][]string{{`a`, ``, `b`}}, Output: `a,,b` + "\n", ZeroQuote: true},
3739
}
3840

3941
func TestWrite(t *testing.T) {
@@ -47,6 +49,9 @@ func TestWrite(t *testing.T) {
4749
if tt.Quote != 0 {
4850
f.Quote = tt.Quote
4951
}
52+
if tt.ZeroQuote {
53+
f.Quote = '\000'
54+
}
5055
err := f.WriteAll(tt.Input)
5156
if err != nil {
5257
t.Errorf("Unexpected error: %s\n", err)

0 commit comments

Comments
 (0)