diff options
Diffstat (limited to 'rust/src/printer.rs')
| -rw-r--r-- | rust/src/printer.rs | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/rust/src/printer.rs b/rust/src/printer.rs new file mode 100644 index 0000000..59821ec --- /dev/null +++ b/rust/src/printer.rs @@ -0,0 +1,26 @@ +pub fn escape_str(s: &str) -> String { + let mut escaped = String::new(); + escaped.push('"'); + for c in s.as_slice().chars() { + let _ = match c { + '"' => escaped.push_str("\\\""), + '\\' => escaped.push_str("\\\\"), + '\x08' => escaped.push_str("\\b"), + '\x0c' => escaped.push_str("\\f"), + '\n' => escaped.push_str("\\n"), + '\r' => escaped.push_str("\\r"), + '\t' => escaped.push_str("\\t"), + _ => escaped.push(c), + }; + }; + + escaped.push('"'); + + escaped +} + +pub fn unescape_str(s: &str) -> String { + let re1 = regex!(r#"\\""#); + let re2 = regex!(r#"\n"#); + re2.replace_all(re1.replace_all(s.as_slice(), "\"").as_slice(), "\n") +} |
