Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 39 additions & 13 deletions csv-core/src/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,8 @@ struct WriterState {
/// The number of total bytes written for the current record.
///
/// If the writer is finished or a terminator is written when this is `0`,
/// then an empty field is added as a pair of adjacent quotes.
/// and quote style is not QuoteStyle::Never, then an empty field is added
/// as a pair of adjacent quotes.
record_bytes: u64,
}

Expand All @@ -232,13 +233,15 @@ impl Writer {
let mut nout = 0;
if self.state.record_bytes == 0 && self.state.in_field {
assert!(!self.state.quoting);
let (res, o) = self.write(&[self.quote, self.quote], output);
if o == 0 {
return (res, 0);
if !matches!(self.style, QuoteStyle::Never) {
let (res, o) = self.write(&[self.quote, self.quote], output);
if o == 0 {
return (res, 0);
}
output = &mut moving(output)[o..];
nout += o;
self.state.record_bytes += o as u64;
}
output = &mut moving(output)[o..];
nout += o;
self.state.record_bytes += o as u64;
}
if !self.state.quoting {
return (WriteResult::InputEmpty, nout);
Expand Down Expand Up @@ -354,13 +357,15 @@ impl Writer {
let mut nout = 0;
if self.state.record_bytes == 0 {
assert!(!self.state.quoting);
let (res, o) = self.write(&[self.quote, self.quote], output);
if o == 0 {
return (res, 0);
if !matches!(self.style, QuoteStyle::Never) {
let (res, o) = self.write(&[self.quote, self.quote], output);
if o == 0 {
return (res, 0);
}
output = &mut moving(output)[o..];
nout += o;
self.state.record_bytes += o as u64;
}
output = &mut moving(output)[o..];
nout += o;
self.state.record_bytes += o as u64;
}
if self.state.quoting {
let (res, o) = self.write(&[self.quote], output);
Expand Down Expand Up @@ -678,6 +683,17 @@ mod tests {
assert_write!(wtr, finish, &mut out[..], 0, InputEmpty, "");
}

#[test]
fn writer_one_empty_field_terminator_quote_style_never() {
let mut wtr = Writer::new();
wtr.style = QuoteStyle::Never;
let out = &mut [0; 1024];

assert_field!(wtr, b(""), &mut out[..], 0, 0, InputEmpty, "");
assert_write!(wtr, terminator, &mut out[..], 1, InputEmpty, "\n");
assert_write!(wtr, finish, &mut out[..], 0, InputEmpty, "");
}

#[test]
fn writer_one_empty_field_finish() {
let mut wtr = Writer::new();
Expand All @@ -687,6 +703,16 @@ mod tests {
assert_write!(wtr, finish, &mut out[..], 2, InputEmpty, "\"\"");
}

#[test]
fn writer_one_empty_field_finish_quote_style_never() {
let mut wtr = Writer::new();
wtr.style = QuoteStyle::Never;
let out = &mut [0; 1024];

assert_field!(wtr, b(""), &mut out[..], 0, 0, InputEmpty, "");
assert_write!(wtr, finish, &mut out[..], 0, InputEmpty, "");
}

#[test]
fn writer_many_one_empty_field_finish() {
let mut wtr = Writer::new();
Expand Down