Permalink
Browse files

Implement prn-str, print-str, and println-str

  • Loading branch information...
brandonbloom authored and David Nolen committed Mar 18, 2012
1 parent 29d60c7 commit 3a3d9b1e3ab8858f26711a8860f48619736eba2d
Showing with 41 additions and 8 deletions.
  1. +4 −4 devnotes/corelib.org
  2. +29 −4 src/cljs/cljs/core.cljs
  3. +8 −0 test/cljs/cljs/core_test.cljs
View
@@ -391,17 +391,17 @@ dunno about printing
* DONE prefer-method
* DONE prefers
* primitives-classnames
-* TODO print
+* DONE print
* print-ctor
* TODO print-dup
* TODO print-method
* TODO print-simple
-* TODO print-str
+* DONE print-str
* printf
* DONE println
-* TODO println-str
+* DONE println-str
* DONE prn
-* TODO prn-str
+* DONE prn-str
* promise
* proxy
* proxy-call-with-super
View
@@ -3058,17 +3058,27 @@ reduces them without incurring seq initialization"
(-pr-seq obj opts)
(list "#<" (str obj) ">")))))
-(defn pr-str-with-opts
- "Prints a sequence of objects to a string, observing all the
- options given in opts"
- [objs opts]
+(defn- pr-sb [objs opts]
(let [first-obj (first objs)
sb (gstring/StringBuffer.)]
(doseq [obj objs]
(when-not (identical? obj first-obj)
(.append sb " "))
(doseq [string (pr-seq obj opts)]
(.append sb string)))
+ sb))
+
+(defn pr-str-with-opts
+ "Prints a sequence of objects to a string, observing all the
+ options given in opts"
+ [objs opts]
+ (str (pr-sb objs opts)))
+
+(defn prn-str-with-opts
+ "Same as pr-str-with-opts followed by (newline)"
+ [objs opts]
+ (let [sb (pr-sb objs opts)]
+ (.append sb \newline)
(str sb)))
(defn pr-with-opts
@@ -3103,6 +3113,11 @@ reduces them without incurring seq initialization"
[& objs]
(pr-str-with-opts objs (pr-opts)))
+(defn prn-str
+ "Same as pr-str followed by (newline)"
+ [& objs]
+ (prn-str-with-opts objs (pr-opts)))
+
(defn pr
"Prints the object(s) using string-print. Prints the
object(s), separated by spaces if there is more than one.
@@ -3118,12 +3133,22 @@ reduces them without incurring seq initialization"
(fn cljs-core-print [& objs]
(pr-with-opts objs (assoc (pr-opts) :readably false))))
+(defn print-str
+ "print to a string, returning it"
+ [& objs]
+ (pr-str-with-opts objs (assoc (pr-opts) :readably false)))
+
(defn println
"Same as print followed by (newline)"
[& objs]
(pr-with-opts objs (assoc (pr-opts) :readably false))
(newline (pr-opts)))
+(defn println-str
+ "println to a string, returning it"
+ [& objs]
+ (prn-str-with-opts objs (assoc (pr-opts) :readably false)))
+
(defn prn
"Same as pr followed by (newline)."
[& objs]
@@ -181,9 +181,17 @@
(assert (= (hash-map :foo 5)
(assoc (cljs.core.ObjMap. nil (array) (js-obj)) :foo 5)))
+ (assert (= "\"asdf\"" (pr-str "asdf")))
(assert (= "[1 true {:a 2, :b 42} #<Array [3, 4]>]"
(pr-str [1 true {:a 2 :b 42} (array 3 4)])))
+ (assert (= "\"asdf\"\n" (prn-str "asdf")))
+ (assert (= "[1 true {:a 2, :b 42} #<Array [3, 4]>]\n"
+ (prn-str [1 true {:a 2 :b 42} (array 3 4)])))
+
+ (assert (= "asdf" (print-str "asdf")))
+ (assert (= "asdf\n" (println-str "asdf")))
+
;;this fails in v8 - why?
;(assert (= "symbol\"'string" (pr-str (str 'symbol \" \' "string"))))

0 comments on commit 3a3d9b1

Please sign in to comment.