Skip to content

Commit

Permalink
[new] Update print impln utils
Browse files Browse the repository at this point in the history
  • Loading branch information
ptaoussanis committed Oct 29, 2024
1 parent c5778cf commit c9bf334
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 29 deletions.
51 changes: 37 additions & 14 deletions src/taoensso/encore.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -5020,18 +5020,37 @@
~(with-meta '__w {:tag 'java.io.Writer})]
(.write ~'__w ~form))))

#?(:clj (declare hex-ident-str))

(defn ^:no-doc str-impl
"Private, don't use."
{:added "Encore vX.Y.Z (YYYY-MM-DD)"}
[type-sym x tag? data]
(if data
(str (when tag? "#") type-sym "[" data #?@(:clj [" 0x" (hex-ident-str x)]) "]")
(str (when tag? "#") type-sym #?@(:clj [ "@" (hex-ident-str x)]))))

(comment
(do
(deftype MyType [] Object (toString [x] "MyType[]"))
(remove-method print-method MyType)
(remove-method print-dup MyType))
(str-impl `MyType :x :tag {})

{:str (str (MyType.))
:print-method (pr-str (MyType.))
:print-dup (binding [*print-dup* true] (pr-str (MyType.)))}
(defn as-strings [x]
{:str (str x)
:pr-str (pr-str x)
:pr-dup (or (catching (binding [*print-dup* true] (pr-str x))) :not-supported)})

(def-print-impl [x MyType] (str x))
(def-print-dup [x MyType] (str x)))
(do
(deftype T2 [x])
#_(deftype T2 [x] Object (toString [t] (str `T2 "@" (hex-ident-str t))))
(remove-method print-method T2)
(remove-method print-dup T2)
(def-print-impl [t T2] (str-impl `T2 t :tag {:x (.-x t)}))
(def-print-dup [t T2] (binding [*print-dup* false] (pr-str t))))

(do (as-strings (delay))) ; {:str "clojure.lang.Delay@6d1e93a6", :pr-str "#delay[{:status :pending, :val nil} 0x6d1e93a6]", :pr-dup :not-supported}
(do (deftype T1 [x]) (as-strings (T1. :x1))) ; {:str "taoensso.encore.T1@4795e014", :pr-str "#object[taoensso.encore.T1 0x4795e014 \"taoensso.encore.T1@4795e014\"]", :pr-dup :not-supported}
(do (defrecord R1 [x]) (as-strings (R1. :x1))) ; {:str "taoensso.encore.R1@1d8fbf", :pr-str "#taoensso.encore.R1{:x :x1}", :pr-dup "#taoensso.encore.R1[:x1]"}
(do (as-strings (T2. :x1))) ; {:str "taoensso.encore.T2@5c123fb2", :pr-str "#taoensso.encore.T2[{:x :x1} 0x5c123fb2]", :pr-dup "#taoensso.encore.T2[{:x :x1} 0x5c123fb2]"}
)

;;;; Thread locals

Expand Down Expand Up @@ -5255,10 +5274,10 @@
;;;; Hex strings

#?(:clj
(defn ident-hex-str
"Returns hex string of given Object's `identityHashCode` (e.g. \"0x5eeb49f2\")."
{:added "Encore v3.56.0 (2023-03-29)"}
^String [obj] (str "0x" (Integer/toHexString (System/identityHashCode obj)))))
(defn hex-ident-str
"Returns hex string of given Object's `identityHashCode` (e.g. \"5eeb49f2\")."
{:added "Encore vX.Y.Z (YYYY-MM-DD)"}
^String [obj] (Integer/toHexString (System/identityHashCode obj))))

#?(:clj
(do
Expand Down Expand Up @@ -7969,4 +7988,8 @@
(def* ^:no-doc ^:deprecated ?subvec<len "Prefer `subvec`." (comp not-empty get-subvector))
(def* ^:no-doc ^:deprecated subvec* "Prefer `subvec`." get-subvector)
(def* ^:no-doc ^:deprecated ?substr<idx "Prefer `substr`." (comp as-?nempty-str get-substr))
(def* ^:no-doc ^:deprecated ?substr<len "Prefer `substr`." (comp as-?nempty-str get-substring)))
(def* ^:no-doc ^:deprecated ?substr<len "Prefer `substr`." (comp as-?nempty-str get-substring))

#?(:clj
(defn ^:no-doc ^:deprecated ident-hex-str "Prefer `hex-ident-str`."
^String [obj] (str "0x" (hex-ident-str)))))
28 changes: 14 additions & 14 deletions src/taoensso/encore/stats.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@

(deftype SortedLongs [^longs a]
#?@(:clj
[Object (toString [_] (str "taoensso.encore.SortedLongs[len=" (alength a) "]"))
clojure.lang.Counted (count [_] (alength a))
[Object (toString [x] (enc/str-impl `SortedLongs x nil {:length (alength a)}))
clojure.lang.Counted (count [_] (alength a))
clojure.lang.Indexed
(nth [_ idx ] (aget a idx))
(nth [_ idx not-found]
Expand All @@ -42,8 +42,8 @@
init (range (alength a))))]

:cljs
[Object (toString [_] (str "taoensso.encore.SortedLongs[len=" (alength a) "]"))
ICounted (-count [_] (alength a))
[Object (toString [_] (enc/str-impl `SortedLongs x nil {:length (alength a)}))
ICounted (-count [_] (alength a))
IIndexed
(-nth [_ idx ] (aget a idx))
(-nth [_ idx not-found]
Expand All @@ -61,8 +61,8 @@

(deftype SortedDoubles [^doubles a]
#?@(:clj
[Object (toString [_] (str "taoensso.encore.SortedDoubles[len=" (alength a) "]"))
clojure.lang.Counted (count [_] (alength a))
[Object (toString [x] (enc/str-impl `SortedDoubles x nil {:length (alength a)}))
clojure.lang.Counted (count [_] (alength a))
clojure.lang.Indexed
(nth [_ idx ] (aget a idx))
(nth [_ idx not-found]
Expand All @@ -79,8 +79,8 @@
init (range (alength a))))]

:cljs
[Object (toString [_] (str "taoensso.encore.SortedDoubles[len=" (alength a) "]"))
ICounted (-count [_] (alength a))
[Object (toString [_] (enc/str-impl `SortedDoubles x nil {:length (alength a)}))
ICounted (-count [_] (alength a))
IIndexed
(-nth [_ idx ] (aget a idx))
(-nth [_ idx not-found]
Expand Down Expand Up @@ -247,7 +247,7 @@
^double xvar-sum
^double xmad-sum]

Object (toString [_] (str "taoensso.encore.SummaryStats[n=" nx "]"))
Object (toString [x] (enc/str-impl `SummaryStats x nil {:n nx}))
#?@(:clj [clojure.lang.IDeref ( deref [this] (deref-sstats this))]
:cljs [ IDeref (-deref [this] (deref-sstats this))]))

Expand Down Expand Up @@ -446,11 +446,11 @@

(deftype SummaryStatsBuffered [sstats_ buf_ buf-size merge-counter merge-cb]
Object
(toString [_] ; "...SummaryStatsBuffered[n=1, pending=8, merged=0]"
(str
"taoensso.encore.SummaryStatsBuffered[n=" (get (sstats_) :n 0)
", pending=" (buf-len (buf_))
(when-let [mc merge-counter] (str ", merged=" @mc)) "]"))
(toString [x]
(enc/str-impl `SummaryStatsBuffered x nil
{:n (get (sstats_) :n 0)
:pending (buf-len (buf_))
:merged (if-let [mc merge-counter] @mc 0)}))

#?@(:clj [clojure.lang.IDeref ( deref [this] (ssb-deref this))]
:cljs [ IDeref (-deref [this] (ssb-deref this))])
Expand Down
2 changes: 1 addition & 1 deletion test/taoensso/encore_tests.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -1600,7 +1600,7 @@
(let [ssb (stats/summary-stats-buffered {:buffer-size 10})]
(dotimes [n 1e5] (ssb n))
[(is (enc/submap? @@ssb {:n 100000 :min 0 :max 99999}))
(is (= (str ssb) "taoensso.encore.SummaryStatsBuffered[n=0, pending=0, merged=9091]"))])])
(is (enc/str-starts-with? (str ssb) "taoensso.encore.stats.SummaryStatsBuffered[{:n 0, :pending 0, :merged 9091}"))])])

;;;;

Expand Down

0 comments on commit c9bf334

Please sign in to comment.