Skip to content

Commit

Permalink
Added support for the unicode \000 charater incase of no quote to write
Browse files Browse the repository at this point in the history
  • Loading branch information
tinygrasshopper committed Dec 9, 2014
1 parent 65b8456 commit 78c1d10
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
4 changes: 4 additions & 0 deletions writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ func (w *Writer) WriteAll(records [][]string) (err error) {
// Empty fields, files with a Comma, fields with a quote or newline, and
// fields which start with a space must be enclosed in quotes.
func (w *Writer) fieldNeedsQuotes(field string) bool {
if w.Quote == 0 {
return false
}

if len(field) == 0 || strings.IndexRune(field, w.Comma) >= 0 || strings.IndexRune(field, w.Quote) >= 0 || strings.IndexAny(field, "\r\n") >= 0 {
return true
}
Expand Down
15 changes: 10 additions & 5 deletions writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ import (
)

var writeTests = []struct {
Input [][]string
Output string
UseCRLF bool
Quote rune
Comma rune
Input [][]string
Output string
UseCRLF bool
Quote rune
Comma rune
ZeroQuote bool
}{
{Input: [][]string{{"abc"}}, Output: "abc\n"},
{Input: [][]string{{"abc"}}, Output: "abc\r\n", UseCRLF: true},
Expand All @@ -34,6 +35,7 @@ var writeTests = []struct {
{Input: [][]string{{"abc", "def"}}, Output: "abc;def\n", Comma: ';'},
{Input: [][]string{{`a|b`}}, Output: `|a||b|` + "\n", Quote: '|'},
{Input: [][]string{{`|a|b|`}}, Output: `|||a||b|||` + "\n", Quote: '|'},
{Input: [][]string{{`a`, ``, `b`}}, Output: `a,,b` + "\n", ZeroQuote: true},
}

func TestWrite(t *testing.T) {
Expand All @@ -47,6 +49,9 @@ func TestWrite(t *testing.T) {
if tt.Quote != 0 {
f.Quote = tt.Quote
}
if tt.ZeroQuote {
f.Quote = '\000'
}
err := f.WriteAll(tt.Input)
if err != nil {
t.Errorf("Unexpected error: %s\n", err)
Expand Down

0 comments on commit 78c1d10

Please sign in to comment.