Switch to escaped_transform to unescape the text.

This commit is contained in:
Tom Alexander 2020-04-06 21:09:06 -04:00
parent 5463ed2cdf
commit a6e48aee6a
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
1 changed files with 7 additions and 3 deletions

View File

@ -1,5 +1,6 @@
use nom::branch::alt;
use nom::bytes::complete::escaped;
use nom::bytes::complete::escaped_transform;
use nom::bytes::complete::is_a;
use nom::bytes::complete::is_not;
use nom::bytes::complete::tag;
@ -328,10 +329,10 @@ pub fn template(i: &str) -> IResult<&str, Template> {
Ok((remaining, Template { contents: contents }))
}
fn quoted_string(i: &str) -> IResult<&str, &str> {
fn quoted_string(i: &str) -> IResult<&str, String> {
delimited(
tag(r#"""#),
escaped(is_not(r#"\""#), '\\', one_of(r#"\""#)),
escaped_transform(is_not(r#"\""#), '\\', one_of(r#"\""#)),
tag(r#"""#),
)(i)
}
@ -614,6 +615,9 @@ mod tests {
#[test]
fn test_temp_string() {
assert_eq!(quoted_string(r#""foo\"bar""#), Ok(("", r#"foo\"bar"#)));
assert_eq!(
quoted_string(r#""foo\"bar""#),
Ok(("", r#"foo"bar"#.to_owned()))
);
}
}