Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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

  • Loading branch information...
commit 3a3d9b1e3ab8858f26711a8860f48619736eba2d 1 parent 29d60c7
Brandon Bloom authored March 18, 2012 David Nolen committed March 30, 2012
8  devnotes/corelib.org
Source Rendered
@@ -391,17 +391,17 @@ dunno about printing
391 391
 * DONE prefer-method
392 392
 * DONE prefers
393 393
 * primitives-classnames
394  
-* TODO print
  394
+* DONE print
395 395
 * print-ctor
396 396
 * TODO print-dup
397 397
 * TODO print-method
398 398
 * TODO print-simple
399  
-* TODO print-str
  399
+* DONE print-str
400 400
 * printf
401 401
 * DONE println
402  
-* TODO println-str
  402
+* DONE println-str
403 403
 * DONE prn
404  
-* TODO prn-str
  404
+* DONE prn-str
405 405
 * promise
406 406
 * proxy
407 407
 * proxy-call-with-super
33  src/cljs/cljs/core.cljs
@@ -3058,10 +3058,7 @@ reduces them without incurring seq initialization"
3058 3058
               (-pr-seq obj opts)
3059 3059
               (list "#<" (str obj) ">")))))
3060 3060
 
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]
3065 3062
   (let [first-obj (first objs)
3066 3063
         sb (gstring/StringBuffer.)]
3067 3064
     (doseq [obj objs]
@@ -3069,6 +3066,19 @@ reduces them without incurring seq initialization"
3069 3066
         (.append sb " "))
3070 3067
       (doseq [string (pr-seq obj opts)]
3071 3068
         (.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)
3072 3082
     (str sb)))
3073 3083
 
3074 3084
 (defn pr-with-opts
@@ -3103,6 +3113,11 @@ reduces them without incurring seq initialization"
3103 3113
   [& objs]
3104 3114
   (pr-str-with-opts objs (pr-opts)))
3105 3115
 
  3116
+(defn prn-str
  3117
+  "Same as pr-str followed by (newline)"
  3118
+  [& objs]
  3119
+  (prn-str-with-opts objs (pr-opts)))
  3120
+
3106 3121
 (defn pr
3107 3122
   "Prints the object(s) using string-print.  Prints the
3108 3123
   object(s), separated by spaces if there is more than one.
@@ -3118,12 +3133,22 @@ reduces them without incurring seq initialization"
3118 3133
   (fn cljs-core-print [& objs]
3119 3134
     (pr-with-opts objs (assoc (pr-opts) :readably false))))
3120 3135
 
  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
+
3121 3141
 (defn println
3122 3142
   "Same as print followed by (newline)"
3123 3143
   [& objs]
3124 3144
   (pr-with-opts objs (assoc (pr-opts) :readably false))
3125 3145
   (newline (pr-opts)))
3126 3146
 
  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
+
3127 3152
 (defn prn
3128 3153
   "Same as pr followed by (newline)."
3129 3154
   [& objs]
8  test/cljs/cljs/core_test.cljs
@@ -181,9 +181,17 @@
181 181
   (assert (= (hash-map :foo 5)
182 182
              (assoc (cljs.core.ObjMap. nil (array) (js-obj)) :foo 5)))
183 183
 
  184
+  (assert (= "\"asdf\"" (pr-str "asdf")))
184 185
   (assert (= "[1 true {:a 2, :b 42} #<Array [3, 4]>]"
185 186
              (pr-str [1 true {:a 2 :b 42} (array 3 4)])))
186 187
 
  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
+
187 195
   ;;this fails in v8 - why?
188 196
   ;(assert (= "symbol\"'string" (pr-str (str 'symbol \" \' "string"))))
189 197
 

0 notes on commit 3a3d9b1

Please sign in to comment.
Something went wrong with that request. Please try again.