A CSV style quoted string, for the purposes of this question, is a string in which:
- The string starts and ends with exactly one
"
. - Two double quotes inside the string are collapsed to one double quote.
"Alo""ha"
→Alo"ha
. - "" on its own is an empty string.
- Error inputs, such as
"A""" e"
, cannot be parsed. It's anA"
, followed by junke"
.
I've tried several things, none of which have worked fully.
The closest I've gotten, thanks to some help from user pinkieval in #nom on the Mozilla IRC:
use std::error as stderror; /* Avoids needing nightly to compile */
named!(csv_style_string<&str, String>, map_res!(
terminated!(tag!("\""), not!(peek!(char!('"')))),
csv_string_to_string
));
fn csv_string_to_string(s: &str) -> Result<String, Box<stderror::Error>> {
Ok(s.to_string().replace("\"\"", "\""))
}
This does not catch the end of the string correctly.
I've also attempted to use the re_match!
macro with r#""([^"]|"")*""#
, but that always results in an Err::Incomplete(1)
.
I've determined that the given CSV example for Nom 1.0 doesn't work for a quoted CSV string as I'm describing it, but I do know implementations differ.