csv: fix last-field-empty error

pull/4648/head
yuyi 2020-04-29 22:50:02 +08:00 committed by GitHub
parent 3e4cd12fd0
commit b2a076e8b8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 12 deletions

View File

@ -129,7 +129,9 @@ fn (r mut Reader) read_record() ?[]string {
// quoted // quoted
else { else {
line = line[1..] line = line[1..]
if j := line.index('"') { j := line.index('"') or {
break
}
if j+1 == line.len { if j+1 == line.len {
// last record // last record
fields << line[..j] fields << line[..j]
@ -141,7 +143,6 @@ fn (r mut Reader) read_record() ?[]string {
line = line[j..] line = line[j..]
continue continue
} }
}
line = line[1..] line = line[1..]
} }
if i <= -1 && fields.len == 0 { if i <= -1 && fields.len == 0 {

View File

@ -120,3 +120,30 @@ fn test_no_line_ending() {
assert row_count == 2 assert row_count == 2
} }
fn test_last_field_empty() {
data := '"name","description","value"\n"one","first","1"\n"two","second",\n'
mut csv_reader := csv.new_reader(data)
mut row_count := 0
for {
row := csv_reader.read() or {
break
}
row_count++
if row_count == 1 {
assert row[0] == 'name'
assert row[1] == 'description'
assert row[2] == 'value'
}
if row_count == 2 {
assert row[0] == 'one'
assert row[1] == 'first'
assert row[2] == '1'
}
if row_count == 3 {
assert row[0] == 'two'
assert row[1] == 'second'
}
}
}