-
Notifications
You must be signed in to change notification settings - Fork 3
/
core.clj
328 lines (300 loc) · 16.3 KB
/
core.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
; Copyright (c) Shantanu Kumar. All rights reserved.
; The use and distribution terms for this software are covered by the
; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
; which can be found in the file LICENSE at the root of this distribution.
; By using this software in any fashion, you are agreeing to be bound by
; the terms of this license.
; You must not remove this notice, or any other, from this software.
(ns stringer.core
(:require [stringer.internal :as i]))
(defmacro with-obj-str
"Given a single-binding vector (or a local symbol that's automatically bound to a new StringBuilder instance),
execute the specified body of code in the binding context and return the string representation of the local."
[bindings & body]
(when-not (or (symbol? bindings) (vector? bindings))
(throw (IllegalArgumentException.
(str "Expected symbol or vector but found " (pr-str bindings)))))
(when (and (vector? bindings)
(not= 2 (count bindings)) (symbol? (first bindings)))
(throw (IllegalArgumentException.
(str "Expected single-binding vector but found " (pr-str bindings)))))
(let [[local-symbol string-builder] (if (symbol? bindings)
[(vary-meta bindings assoc :tag `java.lang.StringBuilder) '(StringBuilder.)]
bindings)]
`(let [~local-symbol ~string-builder]
~@body
(.toString ~local-symbol))))
(defmacro append!
"Append all arguments to the StringBuilder instance."
[holder & args]
(let [each-append (fn [x]
`(let [x# ~x]
(when-not (nil? x#) ; NULL doesn't work with StringBuilder
(.append ~holder x#))))
all-appends (->> args
(remove nil?)
i/precompile
(map each-append))]
`(do ~@all-appends)))
(defmacro join-all!
"Interpose args with delimiter and apply each element of the result sequence to the (.append holder %) method."
[holder delimiter & args]
`(append! ~holder ~@(interpose delimiter args)))
(defmacro join!
"Like clojure.string/join, but appends tokens to a holder instead of returning a string."
[holder delimiter coll]
`(loop [coll# (seq ~coll)
del?# false]
(when coll#
(when del?#
(append! ~holder ~delimiter))
(append! ~holder (first coll#))
(recur (next coll#) true))))
(defmacro strcat
"Concatenate strings faster than 'str'. Note that this is a macro, hence
cannot be used as a function."
([]
"")
([token]
(if (i/stringable? token)
(str token)
`(let [x# ~token]
(if (nil? x#)
""
(String/valueOf x#)))))
([token & more]
(let [w (gensym)]
`(with-obj-str [~(vary-meta w assoc :tag `java.lang.StringBuilder)
(StringBuilder. (strcat ~token))]
(append! ~w ~@more)))))
(defmacro strdel
"Concatenate tokens with specified delimiter."
[delimiter & args]
(if (i/stringable? delimiter)
`(strcat ~@(doall (interpose delimiter args)))
(let [delim (gensym)
delimited-args (doall (interpose delim args))]
`(let [~delim ~delimiter]
(strcat
~@delimited-args)))))
(defmacro strfmt
"Format (like clojure.core/format but only %b, %d, %f, %h, %n, %o, %s, %x supported) given string with arguments. The
format-string must resolve as a string at compile time. Flags and width/precision specifiers are not supported."
[format-str & args]
(let [format-str (let [fmts (eval format-str)]
(i/expected string? "first argument to 'strfmt' to be string at compile time" fmts)
fmts)
sb (gensym "sb-")
conj-str (fn [exps buf]
(if (seq buf)
(let [^String s (apply str buf)]
(conj exps `(.append ~sb ~s)))
exps))
head-arg (fn [arg-seq]
(if (seq arg-seq)
(first arg-seq)
(throw (IllegalArgumentException. "Insufficient arguments to 'strfmt'"))))
conj-exp (fn [exps buf new-exp]
(-> exps
(conj-str buf)
(conj new-exp)))]
(loop [fmt format-str ; remaining format string to process
buf [] ; buffer for the current contiguous string
args args ; remaining args to process
exps []] ; expressions to evaluate later
(if (empty? fmt)
`(with-obj-str [~sb (StringBuilder. ~(count format-str))]
~@(conj-str exps buf))
(let [ch (first fmt)
c2 (second fmt)]
(if (= ch \%)
(case c2
\% (recur (subs fmt 2) (conj buf c2) args exps)
\B (recur (subs fmt 2) [] (next args) (conj-exp exps buf
`(.append ~sb (.toUpperCase
(String/valueOf
(boolean ~(head-arg args)))))))
\b (recur (subs fmt 2) [] (next args) (conj-exp exps buf
`(.append ~sb (boolean ~(head-arg args)))))
\d (recur (subs fmt 2) [] (next args) (conj-exp exps buf
`(.append ~sb (let [arg# ~(head-arg args)]
(if (nil? arg#)
"null"
(long arg#))))))
\f (recur (subs fmt 2) [] (next args) (conj-exp exps buf
`(let [arg# ~(head-arg args)]
(if (nil? arg#)
(.append ~sb "null")
(let [s# (String/valueOf (double arg#))
n# (.length s#)
i# (.indexOf s# ~(int \.))
d# (-> (unchecked-subtract n# i#)
unchecked-dec)]
(if (< d# 6)
(do
(.append ~sb s#)
(case d#
1 (.append ~sb "00000")
2 (.append ~sb "0000")
3 (.append ~sb "000")
4 (.append ~sb "00")
5 (.append ~sb "0")))
(.append ~sb s# 0 (-> (unchecked-subtract n# d#)
(unchecked-add 6)))))))))
\H (recur (subs fmt 2) [] (next args) (conj-exp exps buf
`(.append ~sb (let [arg# ~(head-arg args)]
(if (nil? arg#)
"NULL"
(.toUpperCase
(Integer/toHexString
(.hashCode ^Object arg#))))))))
\h (recur (subs fmt 2) [] (next args) (conj-exp exps buf
`(.append ~sb (let [arg# ~(head-arg args)]
(if (nil? arg#)
"null"
(Integer/toHexString
(.hashCode ^Object arg#)))))))
\n (recur (subs fmt 2) [] args (conj-exp exps buf
`(.append ~sb i/line-separator)))
\o (recur (subs fmt 2) [] (next args) (conj-exp exps buf
`(.append ~sb (let [arg# ~(head-arg args)]
(if (nil? arg#)
"null"
(Long/toOctalString
(long arg#)))))))
\S (recur (subs fmt 2) [] (next args) (conj-exp exps buf
`(.append ~sb (let [arg# ~(head-arg args)]
(if (nil? arg#)
"NULL"
(.toUpperCase
(String/valueOf arg#)))))))
\s (recur (subs fmt 2) [] (next args) (conj-exp exps buf
`(.append ~sb (let [arg# ~(head-arg args)]
(if (nil? arg#)
"null"
(String/valueOf arg#))))))
\X (recur (subs fmt 2) [] (next args) (conj-exp exps buf
`(.append ~sb (let [arg# ~(head-arg args)]
(if (nil? arg#)
"NULL"
(.toUpperCase
(Long/toHexString
(long arg#))))))))
\x (recur (subs fmt 2) [] (next args) (conj-exp exps buf
`(.append ~sb (let [arg# ~(head-arg args)]
(if (nil? arg#)
"null"
(Long/toHexString
(long arg#)))))))
nil (i/expected "either %b, %d, %f, %h, %n, %o, %s or %x" "%")
(i/expected "either %b, %d, %f, %h, %n, %o, %s or %x" (str \% c2)))
(recur (subs fmt 1) (conj buf ch) args exps)))))))
(defn repchar
"Repeat a char given number of times and return the final string."
[^long n ch]
(if (pos? n)
(.replace (String. (char-array n)) (char 0) (char ch))
""))
(defn repstr
"Repeat a string given number of times and return the final string."
[^long n token]
(if (pos? n)
(if (nil? token)
""
(let [^String str-token (if (instance? String token) token (.toString ^Object token))]
(with-obj-str [sb (StringBuilder. (unchecked-multiply n (.length str-token)))]
(dotimes [_ n]
(append! sb str-token)))))
""))
(defn strtbl
"Return string representation of a textual table. Like clojure.pprint/print-table, but faster and does not print."
([ks rows]
(let [^objects
karray (object-array ks)
kcount (alength karray)
rowstr (mapv (fn [m]
(let [^objects cols (object-array kcount)]
(amap cols i r (strcat (get m (aget karray i))))))
rows)
^ints
widths (int-array kcount)
addrow (fn [^StringBuilder buffer ^String left-token ^String mid-token ^String right-token ^objects cols]
(append! buffer left-token)
(dotimes [i kcount]
(when (pos? i)
(append! buffer mid-token))
(let [^String colstr (aget cols i)]
(append! buffer (repchar (unchecked-subtract (aget widths i) (.length colstr)) \space))
(append! buffer colstr)))
(append! buffer right-token))]
;; set widths
(dotimes [i kcount]
(aset widths i
^int (apply max (.length ^String (strcat (aget karray i)))
(map #(.length ^String (aget ^objects % i)) rowstr))))
;; build string buffer
(with-obj-str buffer
;; add header
(addrow buffer "\n| " " | " " |" (amap karray i r (str (aget karray i))))
(addrow buffer "\n|-" "-+-" "-|" (object-array (map #(repchar % \-) widths)))
;; add rows
(doseq [^objects cols rowstr]
(addrow buffer "\n| " " | " " |" cols)))))
([rows]
(strtbl (keys (first rows)) rows)))
(defmacro nformat
"Render a format-string having named parameters in {param} form.
Named parameters are assumed to be local vars unless specified
in a map argument, eg. {:param \"value\"}."
([format-string]
(let [tokens (->> (let [fmts (eval format-string)]
(i/expected string? "first argument to 'nformat' to be string at compile time" fmts)
fmts)
i/nparse
(mapv (fn [each]
(if (keyword? each)
(symbol (name each))
each))))]
`(strcat ~@tokens)))
([format-string params]
(i/expected string? "a format-string" format-string)
(let [tokens (->> (let [fmts (eval format-string)]
(i/expected string? "first argument to 'nformat' to be string at compile time" fmts)
fmts)
i/nparse
(mapv (fn [each]
(if (string? each)
each
`(get ~params ~each)))))]
`(strcat ~@tokens))))
(defn nrender
"Render format-tokens or format-string having named parameters in {param} form. Like `nformat`, but slower and can
handle non-literal format string."
[format-tokens-or-string params]
(as-> format-tokens-or-string $
(cond
(string? $) (i/nparse $)
(vector? $) $
:otherwise (i/expected "format-tokens or format-string" $))
(with-obj-str [^StringBuilder sb (StringBuilder. (count $))]
(run! (fn [token] (if (string? token)
(.append sb ^String token)
(when-some [value (get params token)]
(if (string? value)
(.append sb ^String value)
(.append sb ^Object value)))))
$))))
(defmacro fmt
"Make a function that renders given format-string."
[format-string]
(let [params (gensym)
tokens (->> (let [fmts (eval format-string)]
(i/expected string? "first argument to 'fmt' to be string at compile time" fmts)
fmts)
i/nparse
(mapv (fn [each]
(if (string? each)
each
`(get ~params ~each)))))]
`(fn [~params]
(strcat ~@tokens))))