diff --git a/src/bytes/tests.rs b/src/bytes/tests.rs index 9e8b9d38..968d8f52 100644 --- a/src/bytes/tests.rs +++ b/src/bytes/tests.rs @@ -10,6 +10,7 @@ use crate::Parser; use crate::{ branch::alt, bytes::complete::{escaped, escaped_transform, tag}, + character::complete::anychar, combinator::{map, value}, lib::std::string::String, lib::std::vec::Vec, @@ -278,6 +279,27 @@ fn escape_transform_str() { assert_eq!(esc3("a␛0bc␛n"), Ok(("", String::from("a\0bc\n")))); } +#[cfg(feature = "std")] +#[test] +fn escape_transform_string() { + fn esc(i: &str) -> IResult<&str, String> { + escaped_transform( + alpha, + '\\', + alt(( + value(String::from("\""), tag("\"")), + map(anychar, |c| format!("\\{}", c)), + )), + )(i) + } + + assert_eq!(esc("abcd;"), Ok((";", String::from("abcd")))); + assert_eq!(esc(r#"ab\"cd;"#), Ok((";", String::from(r#"ab"cd"#)))); + assert_eq!(esc(r#"\"abcd;"#), Ok((";", String::from(r#""abcd"#)))); + assert_eq!(esc(r#"\n;"#), Ok((";", String::from(r#"\n"#)))); + assert_eq!(esc(r#"ab\"12"#), Ok(("12", String::from(r#"ab""#)))); +} + #[test] fn take_until_incomplete() { use crate::bytes::streaming::take_until; diff --git a/src/traits.rs b/src/traits.rs index 782bc4aa..78d25c1d 100644 --- a/src/traits.rs +++ b/src/traits.rs @@ -1209,6 +1209,21 @@ impl ExtendInto for &str { } } +#[cfg(feature = "alloc")] +impl ExtendInto for String { + type Item = char; + type Extender = String; + + #[inline] + fn new_builder(&self) -> String { + String::new() + } + #[inline] + fn extend_into(&self, acc: &mut String) { + acc.push_str(self); + } +} + #[cfg(feature = "alloc")] impl ExtendInto for char { type Item = char;