2024 年 Clojure 调查问卷!中分享您的想法。

欢迎!有关如何操作的更多信息,请参阅关于页面。

+1
打印

如果格式字符串以 ~@ 结尾,则 cl-format 中有代码检测此情况
并引发格式错误。然而,错误检测错误,并不会报告格式错误
而是发生 NullException。

要重现此问题,只需评估以下内容。

clojure.pprint> (cl-format false "~@")
执行错误 (NullPointerException) at 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])]))

旧版

登录注册以回答此问题。

...