Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow for escaping the delimiter like the python counterpart (Write) #234

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
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