1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

encoding.csv: add write support

This commit is contained in:
joe-conigliaro
2019-08-17 22:51:20 +10:00
committed by Alexander Medvednikov
parent 56566ba3d0
commit 1ba701e036
2 changed files with 114 additions and 12 deletions

View File

@ -1,9 +1,9 @@
module csv
// Copyright (c) 2019 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
module csv
// Once interfaces are further along the idea would be to have something similar to
// go's io.reader & bufio.reader rather than reading the whole file into string, this
// would then satisfy that interface. I designed it this way to be easily adapted.
@ -21,6 +21,7 @@ struct Reader {
// has_header bool
// headings []string
data string
pub:
mut:
delimiter byte
comment byte
@ -92,6 +93,9 @@ fn (r mut Reader) read_record() ?[]string {
if r.delimiter == r.comment {
return err_comment_is_delim
}
if !valid_delim(r.delimiter) {
return err_invalid_delim
}
mut line := ''
for {
l := r.read_line() or {
@ -122,16 +126,18 @@ fn (r mut Reader) read_record() ?[]string {
else {
line = line.right(1)
i = line.index('"')
if i+1 == line.len {
// last record
fields << line.left(i)
break
}
next := line[i+1]
if next == r.delimiter {
fields << line.left(i)
line = line.right(i)
continue
if i != -1 {
if i+1 == line.len {
// last record
fields << line.left(i)
break
}
next := line[i+1]
if next == r.delimiter {
fields << line.left(i)
line = line.right(i)
continue
}
}
line = line.right(1)
}
@ -142,3 +148,10 @@ fn (r mut Reader) read_record() ?[]string {
return fields
}
fn valid_delim(b byte) bool {
return b != 0 &&
b != `"` &&
b != `\r` &&
b != `\n`
}