From 0db0510ba6cfc01bdfed76f86af33107a639a085 Mon Sep 17 00:00:00 2001 From: Hrvoje Niksic Date: Mon, 25 Apr 2022 21:47:56 +0200 Subject: [PATCH 1/2] Don't add a quoted empty field to empty records if quote style is QuoteStyle::Never --- csv-core/src/writer.rs | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/csv-core/src/writer.rs b/csv-core/src/writer.rs index 4f943017..81d20a1b 100644 --- a/csv-core/src/writer.rs +++ b/csv-core/src/writer.rs @@ -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, } @@ -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); @@ -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); From 557cfcdfc09db4d4225adbc11f3482364b13e3f2 Mon Sep 17 00:00:00 2001 From: Hrvoje Niksic Date: Mon, 25 Apr 2022 21:57:20 +0200 Subject: [PATCH 2/2] Test the new behavior --- csv-core/src/writer.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/csv-core/src/writer.rs b/csv-core/src/writer.rs index 81d20a1b..a6186da4 100644 --- a/csv-core/src/writer.rs +++ b/csv-core/src/writer.rs @@ -683,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(); @@ -692,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();