Skip to content

Instantly share code, notes, and snippets.

@francoisdevlin
Created June 2, 2010 13:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save francoisdevlin/422392 to your computer and use it in GitHub Desktop.
Save francoisdevlin/422392 to your computer and use it in GitHub Desktop.
(defn replace-by
[^CharSequence s re f]
(let [m (re-matcher re s)]
(let [buffer (StringBuffer. (.length s))]
(loop []
[^CharSequence s match replacement]
(let [s (.toString s)]
(cond
(instance? Character match) (.replace s ^Character match ^Character replacement)
(instance? CharSequence match) (.replace s ^CharSequence match ^CharSequence replacement)
(instance? Pattern match) (if (string? replacement)
(.replaceAll (re-matcher ^Pattern match s) ^CharSequence replacement)
(replace-by s match replacement))
:else (throw (IllegalArgumentException. (str "Invalid match arg: " match))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment