Skip to content

Commit

Permalink
Add for escaping the delimiter in writer
Browse files Browse the repository at this point in the history
  • Loading branch information
leaty committed Jun 3, 2021
1 parent 938a372 commit 46a8554
Showing 1 changed file with 40 additions and 2 deletions.
42 changes: 40 additions & 2 deletions csv-core/src/writer.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use core::fmt;
use core::str;

use memchr::memchr;
use memchr::{memchr, memchr2};

use crate::{QuoteStyle, Terminator};

Expand Down Expand Up @@ -299,7 +299,7 @@ impl Writer {
let (res, i, o) = if self.state.quoting {
quote(input, output, self.quote, self.escape, self.double_quote)
} else {
write_optimistic(input, output)
escape(input, output, self.escape, self.delimiter)
};
nin += i;
nout += o;
Expand Down Expand Up @@ -572,6 +572,44 @@ pub fn quote(
}
}

pub fn escape(
mut input: &[u8],
mut output: &mut [u8],
escape: u8,
delimiter: u8,
) -> (WriteResult, usize, usize) {
let (mut nin, mut nout) = (0, 0);
loop {
match memchr2(delimiter, escape, input) {
None => {
let (res, i, o) = write_optimistic(input, output);
nin += i;
nout += o;
return (res, nin, nout);
}
Some(next) => {
let char = input[next];
let (res, i, o) = write_optimistic(&input[..next], output);
input = &input[i..];
output = &mut moving(output)[o..];
nin += i;
nout += o;
if let WriteResult::OutputFull = res {
return (res, nin, nout);
}
let (res, o) = write_pessimistic(&[escape, char], output);
if let WriteResult::OutputFull = res {
return (res, nin, nout);
}
nout += o;
output = &mut moving(output)[o..];
nin += 1;
input = &input[1..];
}
}
}
}

/// Copy the bytes from `input` to `output`. If `output` is too small to fit
/// everything from `input`, then copy `output.len()` bytes from `input`.
/// Otherwise, copy everything from `input` into `output`.
Expand Down

0 comments on commit 46a8554

Please sign in to comment.