From 0ab374bc261f871ab8fbbc13e0096f44225e2a3f Mon Sep 17 00:00:00 2001 From: Joel Martin Date: Sat, 25 Oct 2014 12:41:24 -0500 Subject: rust: add step2_eval. --- rust/src/printer.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 rust/src/printer.rs (limited to 'rust/src/printer.rs') 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") +} -- cgit v1.2.3