Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
CLJS-400: stop using goog.string.quote when printing strings
  • Loading branch information
cemerick authored and David Nolen committed Oct 22, 2012
1 parent 82c5ca7 commit 3b32345
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/cljs/cljs/core.cljs
Expand Up @@ -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)))
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions test/cljs/cljs/core_test.cljs
Expand Up @@ -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
Expand Down

0 comments on commit 3b32345

Please sign in to comment.