use std::io::{self, BufRead};
pub type Fields = Vec<Option<Vec<u8>>>;
fn is_space(b: u8) -> bool {
matches!(b, b' ' | b'\t' | b'\n' | 0x0B | 0x0C | b'\r')
}
fn trailing(buf: &[u8], len: usize) -> usize {
let mut last = [0u8; 2];
for &b in &buf[..len] {
last[0] = last[1];
last[1] = b;
}
match last[1] {
b'\n' if last[0] == b'\r' => len - 2,
b'\n' | b'\r' => len - 1,
_ => len,
}
}
fn field_end(temp: &[u8]) -> usize {
trailing(temp, temp.len())
}
struct Parser<'a> {
delimiter: u8,
enclosure: u8,
escape: Option<u8>,
next_line: Option<&'a mut dyn FnMut() -> io::Result<Option<Vec<u8>>>>,
}
impl Parser<'_> {
fn parse(&mut self, line: Vec<u8>) -> io::Result<Option<Fields>> {
let mut values: Vec<Option<Vec<u8>>> = Vec::new();
let mut len = line.len();
let mut buf = line;
buf.push(0);
let at = |buf: &Vec<u8>, i: usize| buf.get(i).copied().unwrap_or(0);
let mut bptr = 0usize;
let mut limit = trailing(&buf, len);
let mut line_end = limit;
let mut line_end_len = len - limit;
let mut first_field = true;
loop {
let mut temp: Vec<u8> = Vec::new();
let mut inc = usize::from(bptr < limit);
if inc == 1 {
let mut t = bptr;
while at(&buf, t) != self.delimiter && is_space(at(&buf, t)) {
t += 1;
}
if at(&buf, t) == self.enclosure && t < limit {
bptr = t;
}
}
if first_field && bptr == line_end {
return Ok(None);
}
first_field = false;
let comp_end;
if inc != 0 && at(&buf, bptr) == self.enclosure {
let mut state = 0;
bptr += 1;
let mut hunk = bptr;
loop {
if inc == 0 {
if state == 2 {
temp.extend_from_slice(&buf[hunk..bptr - 1]);
hunk = bptr;
break;
}
if state == 1 {
temp.extend_from_slice(&buf[hunk..bptr]);
hunk = bptr;
}
if hunk != line_end {
temp.extend_from_slice(&buf[hunk..bptr]);
hunk = bptr;
}
temp.extend_from_slice(&buf[line_end..line_end + line_end_len]);
let next = match self.next_line.as_mut() {
Some(next) => next()?,
None => None,
};
match next {
None => {
if bptr > limit {
if hunk == bptr {
hunk -= 1;
}
bptr -= 1;
}
break;
}
Some(next) => {
len = next.len();
buf = next;
buf.push(0);
bptr = 0;
hunk = 0;
limit = trailing(&buf, len);
line_end = limit;
line_end_len = len - limit;
state = 0;
}
}
} else {
match state {
1 => {
bptr += 1;
state = 0;
}
2 => {
if at(&buf, bptr) != self.enclosure {
temp.extend_from_slice(&buf[hunk..bptr - 1]);
hunk = bptr;
break;
}
temp.extend_from_slice(&buf[hunk..bptr]);
bptr += 1;
hunk = bptr;
state = 0;
}
_ => {
let b = at(&buf, bptr);
if b == self.enclosure {
state = 2;
} else if self.escape == Some(b) {
state = 1;
}
bptr += 1;
}
}
}
inc = usize::from(bptr < limit);
}
while inc != 0 && at(&buf, bptr) != self.delimiter {
bptr += 1;
inc = usize::from(bptr < limit);
}
temp.extend_from_slice(&buf[hunk..bptr]);
bptr += inc;
comp_end = temp.len();
} else {
let hunk = bptr;
while inc != 0 && at(&buf, bptr) != self.delimiter {
bptr += 1;
inc = usize::from(bptr < limit);
}
temp.extend_from_slice(&buf[hunk..bptr]);
comp_end = field_end(&temp);
if at(&buf, bptr) == self.delimiter {
bptr += 1;
}
}
temp.truncate(comp_end);
values.push(Some(temp));
if inc == 0 {
break;
}
}
Ok(Some(values))
}
}
pub fn str_getcsv(input: &[u8], delimiter: u8, enclosure: u8, escape: Option<u8>) -> Fields {
let mut p = Parser { delimiter, enclosure, escape, next_line: None };
p.parse(input.to_vec()).ok().flatten().unwrap_or_else(|| vec![None])
}
fn read_line<R: BufRead>(reader: &mut R) -> io::Result<Option<Vec<u8>>> {
let mut line = Vec::new();
let n = reader.read_until(b'\n', &mut line)?;
Ok(if n == 0 { None } else { Some(line) })
}
pub fn fgetcsv<R: BufRead>(
reader: &mut R,
delimiter: u8,
enclosure: u8,
escape: Option<u8>,
) -> io::Result<Option<Fields>> {
let Some(first) = read_line(reader)? else {
return Ok(None);
};
let mut next = || read_line(reader);
let mut p = Parser { delimiter, enclosure, escape, next_line: Some(&mut next) };
Ok(Some(p.parse(first)?.unwrap_or_else(|| vec![None])))
}
#[cfg(test)]
mod tests {
use super::*;
fn f(s: &str) -> Vec<Option<String>> {
str_getcsv(s.as_bytes(), b',', b'"', Some(b'"'))
.into_iter()
.map(|v| v.map(|b| String::from_utf8(b).unwrap()))
.collect()
}
#[test]
fn fields() {
assert_eq!(f("a,b,c"), [Some("a".into()), Some("b".into()), Some("c".into())]);
assert_eq!(f(""), [None]);
assert_eq!(f("\"a,b\",c"), [Some("a,b".into()), Some("c".into())]);
assert_eq!(f("\"a\"\"b\""), [Some("a\"b".into())]);
assert_eq!(f("a ,b"), [Some("a ".into()), Some("b".into())]);
assert_eq!(f("a,"), [Some("a".into()), Some("".into())]);
}
#[test]
fn multi_line() {
let mut r = io::Cursor::new(b"\"a\nb\",c\nd\n".to_vec());
let row = fgetcsv(&mut r, b',', b'"', Some(b'"')).unwrap().unwrap();
assert_eq!(row, vec![Some(b"a\nb".to_vec()), Some(b"c".to_vec())]);
let row = fgetcsv(&mut r, b',', b'"', Some(b'"')).unwrap().unwrap();
assert_eq!(row, vec![Some(b"d".to_vec())]);
assert!(fgetcsv(&mut r, b',', b'"', Some(b'"')).unwrap().is_none());
}
}