I want to parse a string containing ASCII characters between single quotes and that can contain escaped single quotes by two ' in a row.
'string value contained between single quotes -> '' and so on...'
which should result in:
string value contained between single quotes -> ' and so on...
use nom::{
bytes::complete::{tag, take_while},
error::{ErrorKind, ParseError},
sequence::delimited,
IResult,
};
fn main() {
let res = string_value::<(&str, ErrorKind)>("'abc''def'");
assert_eq!(res, Ok(("", "abc\'def")));
}
pub fn is_ascii_char(chr: char) -> bool {
chr.is_ascii()
}
fn string_value<'a, E: ParseError<&'a str>>(i: &'a str) -> IResult<&'a str, &'a str, E> {
delimited(tag("'"), take_while(is_ascii_char), tag("'"))(i)
}
How can I detect escaped quotes and not the end of the string?