Created
June 2, 2010 13:52
-
-
Save francoisdevlin/422392 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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