pub fn to_double_quoted_str(txt: &str) -> String {
let esc: String = txt
.chars()
.map(|c| match c {
'\\' => r"\\".to_string(),
'\"' => "\\\"".to_string(),
'\n' => "\\n".to_string(),
'\0' => panic!("Found null byte in to_double_quoted_str"),
c => c.to_string(),
})
.collect();
"\"".to_string() + &esc + "\""
}
#[cfg(test)]
mod tests {
use super::to_double_quoted_str;
#[test]
fn test_to_double_quoted_str() {
assert_eq!("\"hello world\"", to_double_quoted_str("hello world"));
assert_eq!("\"hello world\"", to_double_quoted_str("hello world"));
assert_eq!("\"hello\\nworld\"", to_double_quoted_str("hello\nworld"));
assert_eq!("\"hello\\\\ world\"", to_double_quoted_str("hello\\ world"));
assert_eq!("\"hello\\\"world\"", to_double_quoted_str("hello\"world"));
assert_eq!("\"\\\"\\\"\\\"\\n\\\\\"", to_double_quoted_str("\"\"\"\n\\"));
assert_eq!("\"\\\\n\"", to_double_quoted_str("\\n"));
assert_eq!("\"\\\\\\n\"", to_double_quoted_str("\\\n"));
}
}