v/vlib/encoding/csv/writer.v

81 lines
1.5 KiB
V
Raw Normal View History

// Copyright (c) 2019-2021 Alexander Medvednikov. All rights reserved.
2019-08-17 14:51:20 +02:00
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
module csv
import strings
struct Writer {
mut:
2021-01-05 19:14:35 +01:00
sb strings.Builder
2019-12-13 18:45:48 +01:00
pub mut:
2021-01-05 19:14:35 +01:00
use_crlf bool
2019-08-17 14:51:20 +02:00
delimiter byte
}
2019-09-03 13:57:04 +02:00
pub fn new_writer() &Writer {
2019-08-17 14:51:20 +02:00
return &Writer{
2021-01-05 19:14:35 +01:00
delimiter: `,`
2019-08-17 14:51:20 +02:00
sb: strings.new_builder(200)
}
}
// write writes a single record
2020-05-17 13:51:18 +02:00
pub fn (mut w Writer) write(record []string) ?bool {
2019-08-17 14:51:20 +02:00
if !valid_delim(w.delimiter) {
return err_invalid_delim
}
le := if w.use_crlf { '\r\n' } else { '\n' }
for n, field_ in record {
mut field := field_
2019-08-17 14:51:20 +02:00
if n > 0 {
2021-01-05 19:14:35 +01:00
w.sb.write(w.delimiter.ascii_str())
2019-08-17 14:51:20 +02:00
}
if !w.field_needs_quotes(field) {
w.sb.write(field)
continue
}
w.sb.write('"')
for field.len > 0 {
mut i := field.index_any('"\r\n')
if i < 0 {
i = field.len
}
w.sb.write(field[..i])
field = field[i..]
2019-08-17 14:51:20 +02:00
if field.len > 0 {
z := field[0]
2019-10-27 10:36:38 +01:00
match z {
2021-01-05 19:14:35 +01:00
`"` { w.sb.write('""') }
`\r`, `\n` { w.sb.write(le) }
else {}
2019-10-27 10:36:38 +01:00
}
field = field[1..]
2019-08-17 14:51:20 +02:00
}
}
w.sb.write('"')
}
w.sb.write(le)
return true
}
// Once we have multi dimensional array
// pub fn (w &Writer) write_all(records [][]string) {
// for _, record in records {
// w.write(record)
// }
// }
fn (w &Writer) field_needs_quotes(field string) bool {
if field == '' {
return false
}
2021-01-05 19:14:35 +01:00
if field.contains(w.delimiter.ascii_str()) || (field.index_any('"\r\n') != -1) {
2019-08-17 14:51:20 +02:00
return true
}
return false
}
2020-05-17 13:51:18 +02:00
pub fn (mut w Writer) str() string {
2019-08-17 14:51:20 +02:00
return w.sb.str()
}