From 4ef31701280b49fc5441e5cafb4b9e7b43417ad6 Mon Sep 17 00:00:00 2001
From: Yichao Yu <yyc1992@gmail.com>
Date: Fri, 20 Jun 2014 05:53:33 +0800
Subject: [PATCH] escape string

---
 src/c_wrapper/debug.cpp | 28 ++++++++++++++++++++++++++--
 1 file changed, 26 insertions(+), 2 deletions(-)

diff --git a/src/c_wrapper/debug.cpp b/src/c_wrapper/debug.cpp
index 95cb8084..fbc69c90 100644
--- a/src/c_wrapper/debug.cpp
+++ b/src/c_wrapper/debug.cpp
@@ -7,9 +7,33 @@ namespace pyopencl {
 void
 dbg_print_str(std::ostream &stm, const char *str, size_t len)
 {
-    // TODO escape
     stm << '"';
-    stm.write(str, len);
+    for (size_t i = 0;i < len;i++) {
+        char escaped = 0;
+#define escape_char(in, out)                    \
+        case in:                                \
+            escaped = out;                      \
+            break
+        switch (str[i]) {
+            escape_char('\'', '\'');
+            escape_char('\"', '\"');
+            escape_char('\?', '\?');
+            escape_char('\\', '\\');
+            escape_char('\0', '0');
+            escape_char('\a', 'a');
+            escape_char('\b', 'b');
+            escape_char('\f', 'f');
+            escape_char('\r', 'r');
+            escape_char('\v', 'v');
+        default:
+            break;
+        }
+        if (escaped) {
+            stm << '\\' << escaped;
+        } else {
+            stm << str[i];
+        }
+    }
     stm << '"';
 }
 
-- 
GitLab