aboutsummaryrefslogtreecommitdiff
path: root/rust/src/printer.rs
diff options
context:
space:
mode:
authorJoel Martin <github@martintribe.org>2014-10-25 12:41:24 -0500
committerJoel Martin <github@martintribe.org>2015-01-06 21:58:57 -0600
commit0ab374bc261f871ab8fbbc13e0096f44225e2a3f (patch)
tree18bf694e55ee767e5d55edf73819affa3f0214b5 /rust/src/printer.rs
parentabdd56ebc0e01cd92f694ef2bcafcc394453d055 (diff)
downloadmal-0ab374bc261f871ab8fbbc13e0096f44225e2a3f.tar.gz
mal-0ab374bc261f871ab8fbbc13e0096f44225e2a3f.zip
rust: add step2_eval.
Diffstat (limited to 'rust/src/printer.rs')
-rw-r--r--rust/src/printer.rs26
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")
+}