Skip to content

Commit 3a3d9b1

Browse files
brandonbloomDavid Nolen
authored andcommitted
Implement prn-str, print-str, and println-str
1 parent 29d60c7 commit 3a3d9b1

3 files changed

Lines changed: 41 additions & 8 deletions

File tree

‎devnotes/corelib.org‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -391,17 +391,17 @@ dunno about printing
391391
* DONE prefer-method
392392
* DONE prefers
393393
* primitives-classnames
394-
* TODO print
394+
* DONE print
395395
* print-ctor
396396
* TODO print-dup
397397
* TODO print-method
398398
* TODO print-simple
399-
* TODO print-str
399+
* DONE print-str
400400
* printf
401401
* DONE println
402-
* TODO println-str
402+
* DONE println-str
403403
* DONE prn
404-
* TODO prn-str
404+
* DONE prn-str
405405
* promise
406406
* proxy
407407
* proxy-call-with-super

‎src/cljs/cljs/core.cljs‎

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3058,17 +3058,27 @@ reduces them without incurring seq initialization"
30583058
(-pr-seq obj opts)
30593059
(list "#<" (str obj) ">")))))
30603060

3061-
(defn pr-str-with-opts
3062-
"Prints a sequence of objects to a string, observing all the
3063-
options given in opts"
3064-
[objs opts]
3061+
(defn- pr-sb [objs opts]
30653062
(let [first-obj (first objs)
30663063
sb (gstring/StringBuffer.)]
30673064
(doseq [obj objs]
30683065
(when-not (identical? obj first-obj)
30693066
(.append sb " "))
30703067
(doseq [string (pr-seq obj opts)]
30713068
(.append sb string)))
3069+
sb))
3070+
3071+
(defn pr-str-with-opts
3072+
"Prints a sequence of objects to a string, observing all the
3073+
options given in opts"
3074+
[objs opts]
3075+
(str (pr-sb objs opts)))
3076+
3077+
(defn prn-str-with-opts
3078+
"Same as pr-str-with-opts followed by (newline)"
3079+
[objs opts]
3080+
(let [sb (pr-sb objs opts)]
3081+
(.append sb \newline)
30723082
(str sb)))
30733083

30743084
(defn pr-with-opts
@@ -3103,6 +3113,11 @@ reduces them without incurring seq initialization"
31033113
[& objs]
31043114
(pr-str-with-opts objs (pr-opts)))
31053115

3116+
(defn prn-str
3117+
"Same as pr-str followed by (newline)"
3118+
[& objs]
3119+
(prn-str-with-opts objs (pr-opts)))
3120+
31063121
(defn pr
31073122
"Prints the object(s) using string-print. Prints the
31083123
object(s), separated by spaces if there is more than one.
@@ -3118,12 +3133,22 @@ reduces them without incurring seq initialization"
31183133
(fn cljs-core-print [& objs]
31193134
(pr-with-opts objs (assoc (pr-opts) :readably false))))
31203135

3136+
(defn print-str
3137+
"print to a string, returning it"
3138+
[& objs]
3139+
(pr-str-with-opts objs (assoc (pr-opts) :readably false)))
3140+
31213141
(defn println
31223142
"Same as print followed by (newline)"
31233143
[& objs]
31243144
(pr-with-opts objs (assoc (pr-opts) :readably false))
31253145
(newline (pr-opts)))
31263146

3147+
(defn println-str
3148+
"println to a string, returning it"
3149+
[& objs]
3150+
(prn-str-with-opts objs (assoc (pr-opts) :readably false)))
3151+
31273152
(defn prn
31283153
"Same as pr followed by (newline)."
31293154
[& objs]

‎test/cljs/cljs/core_test.cljs‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,17 @@
181181
(assert (= (hash-map :foo 5)
182182
(assoc (cljs.core.ObjMap. nil (array) (js-obj)) :foo 5)))
183183

184+
(assert (= "\"asdf\"" (pr-str "asdf")))
184185
(assert (= "[1 true {:a 2, :b 42} #<Array [3, 4]>]"
185186
(pr-str [1 true {:a 2 :b 42} (array 3 4)])))
186187

188+
(assert (= "\"asdf\"\n" (prn-str "asdf")))
189+
(assert (= "[1 true {:a 2, :b 42} #<Array [3, 4]>]\n"
190+
(prn-str [1 true {:a 2 :b 42} (array 3 4)])))
191+
192+
(assert (= "asdf" (print-str "asdf")))
193+
(assert (= "asdf\n" (println-str "asdf")))
194+
187195
;;this fails in v8 - why?
188196
;(assert (= "symbol\"'string" (pr-str (str 'symbol \" \' "string"))))
189197

0 commit comments

Comments
 (0)