如果格式字符串以 ~@ 结尾,cl-format 中有代码可以检测到这一点
并发起格式错误。然而,这个错误检测是错误的,并且发生了 null 异常
而不是报告格式错误。
要重现这个问题,只需评估以下内容。
clojure.pprint> (cl-format false "~@")
执行错误(NullPointerException)在 nrepl.middleware.interruptible-eval/evaluate$fn$fn(interruptible_eval.clj:87)。
我对这个问题有解决方案,并希望贡献。
以下是 compile-directive
的新代码版本
(defn- compile-directive [s offset]
(let [[raw-params [rest offset]] (extract-params s offset)
[_ [rest offset flags]] (extract-flags rest offset)
directive (first rest)
;; previous version was missing (if directive ...) here thus (format-error ...) never reached
def (if directive (get directive-table (Character/toUpperCase ^Character directive)))
params (if def (map-params def (map translate-param raw-params) flags offset))]
(if (not directive)
(format-error "Format string ended in the middle of a directive" offset))
(if (not def)
(format-error (str "Directive \"" directive "\" is undefined") offset))
[(struct compiled-directive ((:generator-fn def) params offset) def params offset)
(let [remainder (subs rest 1)
offset (inc offset)
trim? (and (= \newline (:directive def))
(not (:colon params)))
trim-count (if trim? (prefix-count remainder [\space \tab]) 0)
remainder (subs remainder trim-count)
offset (+ offset trim-count)]
[remainder offset])]))
旧版本