Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
eerohele committed Oct 31, 2024
1 parent f079c1d commit 972f1f1
Showing 1 changed file with 48 additions and 38 deletions.
86 changes: 48 additions & 38 deletions src/me/flowthing/pp.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
:license "MIT"
:git/url "https://github.com/eerohele/pp.git"})

#?(:clj (set! *warn-on-reflection* true))
#?(:clj
(do
(set! *warn-on-reflection* true)
(set! *unchecked-math* :warn-on-boxed)))

(defn ^:private strip-ns
"Given a (presumably qualified) ident, return an unqualified version
Expand Down Expand Up @@ -77,7 +80,7 @@
"Given a string, return the length of the string.
Since java.lang.String isn't counted?, (.length s) is faster than (count s)."
[s]
^long [s]
#?(:clj (.length ^String s) :cljs (.-length s)))

(defn ^:private count-keeping-writer
Expand All @@ -95,10 +98,10 @@
(reify CountKeepingWriter
(write [_ s]
(write-into writer ^String s)
(vswap! c #(+ % (strlen ^String s)))
(vswap! c (fn [^long n] (unchecked-add-int n (strlen ^String s))))
nil)
(remaining [_]
(- max-width @c))
(unchecked-subtract-int max-width @c))
(nl [_]
(write-into writer "\n")
(vreset! c 0)
Expand Down Expand Up @@ -334,7 +337,7 @@
;; form in linear style on this line, do so.
;;
;; Otherwise, print the form in miser style.
(if (<= (strlen s) (- (remaining writer) reserve-chars))
(if (<= (strlen s) (unchecked-subtract-int (remaining writer) reserve-chars))
:linear
:miser)))

Expand Down Expand Up @@ -604,40 +607,47 @@
(assert (or (nat-int? max-width) (= max-width ##Inf))
":max-width must be a natural int or ##Inf")

(letfn
[(pp [writer]
(let [writer (count-keeping-writer writer {:max-width max-width})]
(-pprint x writer
(assoc opts
:map-entry-separator map-entry-separator
:level 0
:indentation ""
:reserve-chars 0))
(nl writer)))]
#?(:clj
(do
(assert (instance? java.io.Writer writer)
"first arg to pprint must be a java.io.Writer")

(if *print-dup*
(do
(print-dup x writer)
(.write ^java.io.Writer writer "\n"))
(pp writer))
;; Allowing ##Inf was a mistake, because it's a double and therefore doesn't
;; make sense. If the user passes ##Inf, convert it to Integer/MAX_VALUE,
;; which is functionally the same in this case.
(let [max-width (case max-width
##Inf #?(:clj Integer/MAX_VALUE
:cljs js/Number.MAX_SAFE_INTEGER)
max-width)]
(letfn
[(pp [writer]
(let [writer (count-keeping-writer writer {:max-width max-width})]
(-pprint x writer
(assoc opts
:map-entry-separator map-entry-separator
:level 0
:indentation ""
:reserve-chars 0))
(nl writer)))]
#?(:clj
(do
(assert (instance? java.io.Writer writer)
"first arg to pprint must be a java.io.Writer")

(if *print-dup*
(do
(print-dup x writer)
(.write ^java.io.Writer writer "\n"))
(pp writer))

(when *flush-on-newline* (.flush ^java.io.Writer writer)))
(when *flush-on-newline* (.flush ^java.io.Writer writer)))

:cljs
(if writer
(do
(assert (satisfies? cljs.core.IWriter writer)
"first arg to pprint must be a cljs.core.IWriter")
:cljs
(if writer
(do
(assert (satisfies? cljs.core.IWriter writer)
"first arg to pprint must be a cljs.core.IWriter")

(pp writer))
(pp writer))

;; ClojureScript does not have *out* bound by default.
(let [sb (goog.string.StringBuffer.)
writer (StringBufferWriter. sb)]
(pp writer)
(-> sb str string-print)
(when *flush-on-newline* (-flush writer))))))))
;; ClojureScript does not have *out* bound by default.
(let [sb (goog.string.StringBuffer.)
writer (StringBufferWriter. sb)]
(pp writer)
(-> sb str string-print)
(when *flush-on-newline* (-flush writer)))))))))

0 comments on commit 972f1f1

Please sign in to comment.