You're viewing version 1.2.0 of escape. The latest stable version of Clojure Core is 1.3.0.
1.2.0 Arrow_down_16x16
  • (escape s cmap)
Return a new string, using cmap to escape each character ch
from s as follows:

If (cmap ch) is nil, append ch to the new string.
If (cmap ch) is non-nil, append (str (cmap ch)) instead.

1 Example top

  • ;; There should be no space between the \ and the &, but I don't know how
    ;; to create that in an example yet.
    user=> (clojure.string/escape "I want 1 < 2 as HTML, & other good things."
                   {\< "<", \> ">", \ & "&"})
    "I want 1 < 2 as HTML, & other good things."
Log in to add / edit an example.

See Also top

Log in to add a see also.

Plus_12x12 Minus_12x12 Source clojure/string.clj:238 top

(defn ^String escape
  "Return a new string, using cmap to escape each character ch
   from s as follows:
   
   If (cmap ch) is nil, append ch to the new string.
   If (cmap ch) is non-nil, append (str (cmap ch)) instead."
  {:added "1.2"}
  [^CharSequence s cmap]
  (loop [index (int 0)
         buffer (StringBuilder. (.length s))]
    (if (= (.length s) index)
      (.toString buffer)
      (let [ch (.charAt s index)]
        (if-let [replacement (cmap ch)]
          (.append buffer replacement)
          (.append buffer ch))
        (recur (inc index) buffer)))))
Vars in clojure.string/escape: = defn if-let inc let loop
Used in 0 other vars

Comments top

No comments for escape. Log in to add a comment.