(defn
escape
"Return a new string, using cmap to escape each character ch\n from s as follows:\n\n If (cmap ch) is nil, append ch to the new string.\n If (cmap ch) is non-nil, append (str (cmap ch)) instead."
[s cmap]
(let
[buffer (StringBuffer.) length (.-length s)]
(loop
[index 0]
(if
(== length index)
(. buffer (toString))
(let
[ch (.charAt s index) replacement (get cmap ch)]
(if-not
(nil? replacement)
(.append buffer (str replacement))
(.append buffer ch))
(recur (inc index)))))))