| 1 |
package csv |
| 2 |
|
| 3 |
import ( |
| 4 |
"encoding/csv" |
| 5 |
"fmt" |
| 6 |
"io" |
| 7 |
"reflect" |
| 8 |
"strings" |
| 9 |
"testing" |
| 10 |
"unicode" |
| 11 |
|
| 12 |
"github.com/gabriel-vasile/mimetype/internal/scan" |
| 13 |
) |
| 14 |
|
| 15 |
type line struct { |
| 16 |
fields int |
| 17 |
|
| 18 |
indexes []int |
| 19 |
hasMore bool |
| 20 |
} |
| 21 |
|
| 22 |
var testcases = []struct { |
| 23 |
name string |
| 24 |
csv string |
| 25 |
comma byte |
| 26 |
comment byte |
| 27 |
}{{ |
| 28 |
"empty", "", ',', '#', |
| 29 |
}, { |
| 30 |
"simple", |
| 31 |
`foo,bar,baz |
| 32 |
1,2,3 |
| 33 |
"1","a",b`, |
| 34 |
',', '#', |
| 35 |
}, { |
| 36 |
"crlf line endings", |
| 37 |
"foo,bar,baz\r\n1,2,3\r\n", |
| 38 |
',', '#', |
| 39 |
}, { |
| 40 |
"leading and trailing space", |
| 41 |
`1, abc ,3`, |
| 42 |
',', '#', |
| 43 |
}, { |
| 44 |
"empty quote", |
| 45 |
`1,"",3`, |
| 46 |
',', '#', |
| 47 |
}, { |
| 48 |
"quotes with comma", |
| 49 |
`1,",",3`, |
| 50 |
',', '#', |
| 51 |
}, { |
| 52 |
"quotes with quote", |
| 53 |
`1,""",3`, |
| 54 |
',', '#', |
| 55 |
}, { |
| 56 |
"fewer fields", |
| 57 |
`foo,bar,baz |
| 58 |
1,2`, |
| 59 |
',', '#', |
| 60 |
}, { |
| 61 |
"more fields", |
| 62 |
`1,2,3,4`, |
| 63 |
',', '#', |
| 64 |
}, { |
| 65 |
"forgot quote", |
| 66 |
`1,"Forgot,3`, |
| 67 |
',', '#', |
| 68 |
}, { |
| 69 |
"unescaped quote", |
| 70 |
`1,"abc"def",3`, |
| 71 |
',', '#', |
| 72 |
}, { |
| 73 |
"unescaped quote", |
| 74 |
`1,"abc"def",3`, |
| 75 |
',', '#', |
| 76 |
}, { |
| 77 |
"unescaped quote2", |
| 78 |
`1,abc"quote"def,3`, |
| 79 |
',', '#', |
| 80 |
}, { |
| 81 |
"escaped quote", |
| 82 |
`1,abc""def,3`, |
| 83 |
',', '#', |
| 84 |
}, { |
| 85 |
"new line", |
| 86 |
`1,abc |
| 87 |
def,3`, |
| 88 |
',', '#', |
| 89 |
}, { |
| 90 |
"new line quotes", |
| 91 |
`1,"abc |
| 92 |
def",3`, |
| 93 |
',', '#', |
| 94 |
}, { |
| 95 |
"quoted field at end", |
| 96 |
`1,"abc"`, |
| 97 |
',', '#', |
| 98 |
}, { |
| 99 |
"not ended quoted field at end", |
| 100 |
`1,"abc`, |
| 101 |
',', '#', |
| 102 |
}, { |
| 103 |
"empty field", |
| 104 |
`1,,3`, |
| 105 |
',', '#', |
| 106 |
}, { |
| 107 |
"unicode fields", |
| 108 |
`💁,👌,🎍,😍`, |
| 109 |
',', '#', |
| 110 |
}, { |
| 111 |
"comment", |
| 112 |
`#comment`, |
| 113 |
',', '#', |
| 114 |
}, { |
| 115 |
"line with \\r at the end", |
| 116 |
"123\r\n456\r", |
| 117 |
',', '#', |
| 118 |
}, { |
| 119 |
`from fuzz \"\"\r\n0`, |
| 120 |
"\"\"\r\n0", |
| 121 |
',', '\x11', |
| 122 |
}} |
| 123 |
|
| 124 |
|
| 125 |
func TestParser(t *testing.T) { |
| 126 |
for _, tc := range testcases { |
| 127 |
t.Run(tc.name, func(t *testing.T) { |
| 128 |
expected, recs, _ := stdlibLines(tc.csv, tc.comma, tc.comment) |
| 129 |
got := ourLines(tc.csv, tc.comma, tc.comment) |
| 130 |
if !reflect.DeepEqual(expected, got) { |
| 131 |
t.Errorf(`%s |
| 132 |
expected: %v |
| 133 |
got: %v |
| 134 |
records: %v`, tc.csv, expected, got, recs) |
| 135 |
} |
| 136 |
}) |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
func ourLines(data string, comma, comment byte) []line { |
| 141 |
s := scan.Bytes(data) |
| 142 |
p := NewParser(comma, comment, &s) |
| 143 |
lines := []line{} |
| 144 |
for { |
| 145 |
fields, indexes, hasMore := p.CountFields(true) |
| 146 |
if !hasMore { |
| 147 |
break |
| 148 |
} |
| 149 |
lines = append(lines, line{fields, indexes, hasMore}) |
| 150 |
} |
| 151 |
return lines |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
func stdlibLines(data string, comma, comment byte) ([]line, [][]string, error) { |
| 156 |
if comma > unicode.MaxASCII || comment > unicode.MaxASCII { |
| 157 |
return nil, nil, fmt.Errorf("comma or comment not ASCII") |
| 158 |
} |
| 159 |
|
| 160 |
if strings.IndexByte(data, 0) != -1 { |
| 161 |
return nil, nil, fmt.Errorf("CSV contains null byte 0x00") |
| 162 |
} |
| 163 |
r := csv.NewReader(strings.NewReader(data)) |
| 164 |
r.Comma = rune(comma) |
| 165 |
r.ReuseRecord = true |
| 166 |
r.FieldsPerRecord = -1 |
| 167 |
r.LazyQuotes = true |
| 168 |
r.Comment = rune(comment) |
| 169 |
|
| 170 |
var err error |
| 171 |
lines := []line{} |
| 172 |
|
| 173 |
records := [][]string{} |
| 174 |
for { |
| 175 |
l, err := r.Read() |
| 176 |
if err == io.EOF { |
| 177 |
break |
| 178 |
} |
| 179 |
if err != nil { |
| 180 |
return nil, nil, err |
| 181 |
} |
| 182 |
indexes := []int{} |
| 183 |
for i := 0; i < len(l); i++ { |
| 184 |
_, c := r.FieldPos(i) |
| 185 |
|
| 186 |
|
| 187 |
indexes = append(indexes, c-1) |
| 188 |
} |
| 189 |
lines = append(lines, line{len(l), indexes, err != io.EOF}) |
| 190 |
records = append(records, l) |
| 191 |
} |
| 192 |
|
| 193 |
return lines, records, err |
| 194 |
} |
| 195 |
|
| 196 |
var sample = ` |
| 197 |
1,2,3 |
| 198 |
"a", "b", "c" |
| 199 |
a,b,c` + "\r\n1,2,3\r\na,b,c\r" |
| 200 |
|
| 201 |
func BenchmarkCSVStdlibDecoder(b *testing.B) { |
| 202 |
b.ReportAllocs() |
| 203 |
|
| 204 |
r := strings.NewReader(sample) |
| 205 |
for b.Loop() { |
| 206 |
_, err := r.Seek(0, 0) |
| 207 |
if err != nil { |
| 208 |
b.Fatalf("reader cannot seek: %s", err) |
| 209 |
} |
| 210 |
d := csv.NewReader(r) |
| 211 |
d.ReuseRecord = true |
| 212 |
d.FieldsPerRecord = -1 |
| 213 |
d.LazyQuotes = true |
| 214 |
for { |
| 215 |
_, err := d.Read() |
| 216 |
if err == io.EOF { |
| 217 |
break |
| 218 |
} else if err != nil { |
| 219 |
b.Fatalf("error parsing CSV: %s", err) |
| 220 |
} |
| 221 |
} |
| 222 |
} |
| 223 |
} |
| 224 |
func BenchmarkCSVOurParser(b *testing.B) { |
| 225 |
b.ReportAllocs() |
| 226 |
|
| 227 |
r := scan.Bytes(sample) |
| 228 |
rr := r[:] |
| 229 |
p := NewParser(',', '#', &rr) |
| 230 |
for b.Loop() { |
| 231 |
rr = r[:] |
| 232 |
p.s = &rr |
| 233 |
for { |
| 234 |
_, _, hasMore := p.CountFields(false) |
| 235 |
if !hasMore { |
| 236 |
break |
| 237 |
} |
| 238 |
} |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
func FuzzParser(f *testing.F) { |
| 243 |
for _, p := range testcases { |
| 244 |
f.Add(p.csv, byte(','), byte('#')) |
| 245 |
} |
| 246 |
f.Fuzz(func(t *testing.T, data string, comma, comment byte) { |
| 247 |
expected, _, err := stdlibLines(data, comma, comment) |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
if err != nil { |
| 252 |
t.Skipf("not testable: %v", err) |
| 253 |
} |
| 254 |
got := ourLines(data, comma, comment) |
| 255 |
if !reflect.DeepEqual(got, expected) { |
| 256 |
t.Logf("input: %v, comma: %c, comment: %c", data, comma, comment) |
| 257 |
t.Errorf(` |
| 258 |
expected: %v, |
| 259 |
got: %v`, expected, got) |
| 260 |
} |
| 261 |
}) |
| 262 |
} |