Skip to content

Instantly share code, notes, and snippets.

@cemerick
Created October 19, 2012 15:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cemerick/3918764 to your computer and use it in GitHub Desktop.
Save cemerick/3918764 to your computer and use it in GitHub Desktop.
From 39f08c3d665004c881e2b6481c44bef670004f29 Mon Sep 17 00:00:00 2001
From: Chas Emerick <chas@cemerick.com>
Date: Fri, 19 Oct 2012 11:14:10 -0400
Subject: [PATCH] stop using goog.string.quote when printing strings
---
src/cljs/cljs/core.cljs | 19 +++++++++++++++++--
test/cljs/cljs/core_test.cljs | 1 +
2 files changed, 18 insertions(+), 2 deletions(-)
diff --git a/src/cljs/cljs/core.cljs b/src/cljs/cljs/core.cljs
index fa80728..a2b3063 100644
--- a/src/cljs/cljs/core.cljs
+++ b/src/cljs/cljs/core.cljs
@@ -6350,6 +6350,21 @@ reduces them without incurring seq initialization"
[fmt & args]
(print (apply format fmt args)))
+(def ^:private char-escapes {"\"" "\\\""
+ "\\" "\\\\"
+ "\b" "\\b"
+ "\f" "\\f"
+ "\n" "\\n"
+ "\r" "\\r"
+ "\t" "\\t"})
+
+(defn ^:private quote-string
+ [s]
+ (str \"
+ (.replace s (js/RegExp "[\\\\\"\b\f\n\r\t]" "g")
+ (fn [match] (get char-escapes match)))
+ \"))
+
(extend-protocol ^:deprecation-nowarn IPrintable
boolean
(-pr-seq [bool opts] (list (str bool)))
@@ -6374,7 +6389,7 @@ reduces them without incurring seq initialization"
(str nspc "/"))
(name obj)))
:else (list (if (:readably opts)
- (goog.string.quote obj)
+ (quote-string obj)
obj))))
function
@@ -6510,7 +6525,7 @@ reduces them without incurring seq initialization"
(write-all writer (str nspc) "/"))
(-write writer (name obj)))
:else (if (:readably opts)
- (-write writer (goog.string.quote obj))
+ (-write writer (quote-string obj))
(-write writer obj))))
function
diff --git a/test/cljs/cljs/core_test.cljs b/test/cljs/cljs/core_test.cljs
index 5ef8a79..568fc05 100644
--- a/test/cljs/cljs/core_test.cljs
+++ b/test/cljs/cljs/core_test.cljs
@@ -1650,6 +1650,7 @@
(assert (= (pr-str true) "true"))
(assert (= (pr-str false) "false"))
(assert (= (pr-str "string") "\"string\""))
+ (assert (= (pr-str ["üñîçó∂£" :ทดสอบ/你好 'こんにちは]) "[\"üñîçó∂£\" :ทดสอบ/你好 こんにちは]"))
(assert (= (pr-str "escape chars \t \r \n \\ \" \b \f") "\"escape chars \\t \\r \\n \\\\ \\\" \\b \\f\""))
;;; pr-str records
--
1.7.9.6 (Apple Git-31.1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment