Skip to content

Commit

Permalink
Changed formatter to be a macro so that the string is compiled at rea…
Browse files Browse the repository at this point in the history
…d time.
  • Loading branch information
tomfaulhaber committed Mar 17, 2009
1 parent 15cbdd1 commit e1ab16b
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions com/infolace/format_base.clj
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
(defstruct #^{:private true}
arg-navigator :seq :rest :pos )

(defn- init-navigator [s]
(defn init-navigator [s]
"Create a new arg-navigator from the sequence with the position set to 0"
(struct arg-navigator s s 0))

Expand Down Expand Up @@ -1692,7 +1692,7 @@ column number or pretty printing"
true
(recur (next format))))))

(defn- execute-format [stream format args]
(defn execute-format [stream format args]
(let [real-stream (cond
(not stream) (java.io.StringWriter.)
(true? stream) *out*
Expand Down Expand Up @@ -1720,14 +1720,15 @@ column number or pretty printing"
(if (not stream) (.toString real-stream)))))


(defn formatter
(defmacro formatter
"Makes a function which can directly run format-in. The function is
fn [stream & args] ... and returns nil unless the stream is nil (meaning
output to a string) in which case it returns the resulting string.
format-in can be either a control string or a previously compiled format."
[format-in]
(let [compiled-format (if (string? format-in) (compile-format format-in) format-in)]
(fn [stream & args]
(let [navigator (init-navigator args)]
(execute-format stream compiled-format navigator)))))
`(let [compiled-format# (if (string? ~format-in) (compile-format ~format-in) ~format-in)
func# (fn [stream# & args#]
(let [navigator# (init-navigator args#)]
(execute-format stream# compiled-format# navigator#)))]
func#))

0 comments on commit e1ab16b

Please sign in to comment.